Skip to content

Commit 33c01e6

Browse files
committed
lib/compat: add strmode() and xbps_strmode()
for printing file permissions and type
1 parent c78231f commit 33c01e6

5 files changed

Lines changed: 117 additions & 0 deletions

File tree

configure

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,28 @@ else
549549
fi
550550
rm -f _$func.c _$func
551551

552+
#
553+
# Check for strmode().
554+
func=strmode
555+
printf "Checking for $func() ... "
556+
cat <<EOF > _$func.c
557+
#include <unistd.h>
558+
559+
int main(void) {
560+
const char dest[] = "";
561+
strmode(0104644, dest);
562+
return 0;
563+
}
564+
EOF
565+
if $XCC _$func.c -o _$func 2>/dev/null; then
566+
echo yes.
567+
echo "CPPFLAGS+= -DHAVE_STRMODE" >>$CONFIG_MK
568+
else
569+
echo no.
570+
echo "COMPAT_OBJS+= compat/strmode.o" >>$CONFIG_MK
571+
fi
572+
rm -f _$func.c _$func
573+
552574
#
553575
# Check for rbtree_ininit().
554576
#

include/compat.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ int HIDDEN vasprintf(char **, const char *, va_list);
3737
int HIDDEN humanize_number(char *, size_t, int64_t, const char *, int, int);
3838
#endif
3939

40+
#ifndef HAVE_STRMODE
41+
void HIDDEN strmode(mode_t, char *);
42+
#endif
43+
4044
#endif /* COMPAT_H */

include/xbps.h.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,6 +2213,14 @@ size_t xbps_strlcat(char *dst, const char *src, size_t dstsize);
22132213
*/
22142214
size_t xbps_strlcpy(char *dst, const char *src, size_t dstsize);
22152215

2216+
/**
2217+
* Convert a \a mode from stat to a string in \a buf.
2218+
*
2219+
* @param[in] mode Mode to convert.
2220+
* @param[out] buf Buffer to store the resulting string.
2221+
*/
2222+
void xbps_strmode(mode_t mode, char *buf);
2223+
22162224
/**
22172225
* Tests if pkgver is reverted by pkg
22182226
*

lib/compat/strmode.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*-
2+
* Copyright (c) 2023 classabbyamp.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
#include <sys/types.h>
27+
#include <sys/stat.h>
28+
29+
#include "compat.h"
30+
31+
void HIDDEN
32+
strmode(mode_t mode, char *buf) {
33+
switch (mode & S_IFMT) {
34+
// many of these are not currently packageable, but why not keep them for future compat?
35+
case S_IFSOCK: *buf++ = 's'; break;
36+
case S_IFLNK: *buf++ = 'l'; break;
37+
case S_IFREG: *buf++ = '-'; break;
38+
case S_IFBLK: *buf++ = 'b'; break;
39+
case S_IFDIR: *buf++ = 'd'; break;
40+
case S_IFCHR: *buf++ = 'c'; break;
41+
case S_IFIFO: *buf++ = 'p'; break;
42+
#ifdef S_IFWHT
43+
case S_IFWHT: *buf++ = 'w'; break;
44+
#endif
45+
default: *buf++ = '?'; break;
46+
}
47+
*buf++ = (mode & S_IRUSR) ? 'r' : '-';
48+
*buf++ = (mode & S_IWUSR) ? 'w' : '-';
49+
switch (mode & (S_IXUSR | S_ISUID)) {
50+
case (S_IXUSR | S_ISUID): *buf++ = 's'; break;
51+
case S_ISUID: *buf++ = 'S'; break;
52+
case S_IXUSR: *buf++ = 'x'; break;
53+
default: *buf++ = '-'; break;
54+
}
55+
56+
*buf++ = (mode & S_IRGRP) ? 'r' : '-';
57+
*buf++ = (mode & S_IWGRP) ? 'w' : '-';
58+
switch (mode & (S_IXGRP | S_ISGID)) {
59+
case S_IXGRP | S_ISGID: *buf++ = 's'; break;
60+
case S_ISUID: *buf++ = 'S'; break;
61+
case S_IXGRP: *buf++ = 'x'; break;
62+
default: *buf++ = '-'; break;
63+
}
64+
65+
*buf++ = (mode & S_IROTH) ? 'r' : '-';
66+
*buf++ = (mode & S_IWOTH) ? 'w' : '-';
67+
switch (mode & (S_IXOTH | S_ISVTX)) {
68+
case S_IXOTH | S_ISVTX: *buf++ = 't'; break;
69+
case S_ISVTX: *buf++ = 'T'; break;
70+
case S_IXOTH: *buf++ = 'x'; break;
71+
default: *buf++ = '-'; break;
72+
}
73+
74+
*buf = '\0';
75+
}

lib/util.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,14 @@ xbps_strlcpy(char *dest, const char *src, size_t siz)
532532
return strlcpy(dest, src, siz);
533533
}
534534

535+
void
536+
xbps_strmode(mode_t mode, char *buf) {
537+
assert(mode);
538+
assert(buf);
539+
540+
return strmode(mode, buf);
541+
}
542+
535543
/*
536544
* Check if pkg is explicitly marked to replace a specific installed version.
537545
*/

0 commit comments

Comments
 (0)