Whamcloud - gitweb
LU-17504 build: fix gcc-13 [-Werror=stringop-overread] error
[fs/lustre-release.git] / lustre / tests / statx.c
index aa7429b..b0c23b3 100644 (file)
@@ -50,7 +50,6 @@
 #include <inttypes.h>
 #include <fcntl.h>
 #include <locale.h>
-#include <libiberty.h>
 #include <linux/lustre/lustre_user.h>
 
 #ifdef HAVE_SELINUX
@@ -86,6 +85,21 @@ ssize_t statx(int dfd, const char *filename, int flags,
 }
 #endif /* HAVE_STATX */
 
+#define xstrdup(str) strdup(str)
+static inline
+char *xasprintf(const char *fmt, const char *old_fmt, const char *str)
+{
+       char *tmp = NULL;
+
+       if (asprintf(&tmp, fmt, old_fmt, str) < 0) {
+               fprintf(stderr, "asprintf allocation failed\n");
+               exit(1);
+       }
+
+       return tmp;
+}
+
+
 /* coreutils/lib/intprops.h */
 #define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED(__typeof__(t))
 
@@ -157,11 +171,10 @@ static char const printf_flags[] = "'-+ #0I";
 
 /* Formats for the --terse option. */
 static char const fmt_terse_fs[] = "%n %i %l %t %s %S %b %f %a %c %d\n";
-static char const fmt_terse_regular[] = "%n %s %b %f %u %g %D %i %h %t %T"
-                                       " %X %Y %Z %W %o\n";
-static char const fmt_terse_selinux[] = "%n %s %b %f %u %g %D %i %h %t %T"
-                                       " %X %Y %Z %W %o %C\n";
-
+static char const fmt_terse_regular[] = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o\n";
+#ifdef HAVE_SELINUX
+static char const fmt_terse_selinux[] = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o %C\n";
+#endif
 static char *format;
 
 /* Whether to follow symbolic links;  True for --dereference (-L).  */
@@ -190,7 +203,7 @@ static inline unsigned char to_uchar(char ch)
        return ch;
 }
 
-void usage(char *prog)
+static void usage(char *prog)
 {
        printf("Usage: %s [options] <FILE>...\n", prog);
        printf("Display file status via statx() syscall.\n"
@@ -232,7 +245,7 @@ void usage(char *prog)
               "\t%%n   file name\n"
               "\t%%N   quoted file name with dereference if symbolic link\n"
               "\t%%o   optimal I/O transfer size hint\n"
-              "\t%%q   Mask to show what's supported in stx_attributes\n"
+              "\t%%p   Mask to show what's supported in stx_attributes\n"
               "\t%%r   Flags conveying information about the file: "
               "stx_attributes\n"
               "\t%%s   total size, in bytes\n"
@@ -300,21 +313,21 @@ static char ftypelet(mode_t bits)
 static void strmode(mode_t mode, char *str)
 {
        str[0] = ftypelet(mode);
-       str[1] = mode & S_IRUSR ? 'r' : '-';
-       str[2] = mode & S_IWUSR ? 'w' : '-';
+       str[1] = mode & 0400 ? 'r' : '-';
+       str[2] = mode & 0200 ? 'w' : '-';
        str[3] = (mode & S_ISUID
-                       ? (mode & S_IXUSR ? 's' : 'S')
-                       : (mode & S_IXUSR ? 'x' : '-'));
-       str[4] = mode & S_IRGRP ? 'r' : '-';
-       str[5] = mode & S_IWGRP ? 'w' : '-';
+                       ? (mode & 0100 ? 's' : 'S')
+                       : (mode & 0100 ? 'x' : '-'));
+       str[4] = mode & 0040 ? 'r' : '-';
+       str[5] = mode & 0020 ? 'w' : '-';
        str[6] = (mode & S_ISGID
-                       ? (mode & S_IXGRP ? 's' : 'S')
-                       : (mode & S_IXGRP ? 'x' : '-'));
-       str[7] = mode & S_IROTH ? 'r' : '-';
-       str[8] = mode & S_IWOTH ? 'w' : '-';
+                       ? (mode & 0010 ? 's' : 'S')
+                       : (mode & 0010 ? 'x' : '-'));
+       str[7] = mode & 0004 ? 'r' : '-';
+       str[8] = mode & 0002 ? 'w' : '-';
        str[9] = (mode & S_ISVTX
-                       ? (mode & S_IXOTH ? 't' : 'T')
-                       : (mode & S_IXOTH ? 'x' : '-'));
+                       ? (mode & 0001 ? 't' : 'T')
+                       : (mode & 0001 ? 'x' : '-'));
        str[10] = ' ';
        str[11] = '\0';
 }
@@ -367,19 +380,6 @@ static void strmode(mode_t mode, char *str)
 static void filemodestring(struct statx const *stxp, char *str)
 {
        strmode(stxp->stx_mode, str);
-
-/*
-       if (S_TYPEISSEM(statp))
-               str[0] = 'F';
-       else if (IS_MIGRATED_FILE (statp))
-               str[0] = 'M';
-       else if (S_TYPEISMQ (statp))
-               str[0] = 'Q';
-       else if (S_TYPEISSHM (statp))
-               str[0] = 'S';
-       else if (S_TYPEISTMO (statp))
-               str[0] = 'T';
- */
 }
 
 /* gnulib/lib/file-type.c */
@@ -847,6 +847,7 @@ static void out_epoch_sec(char *pformat, size_t prefix_len,
                                        if (w > 1) {
                                                char *dst = pformat;
                                                char const *src = dst;
+
                                                for (; src < p; src++) {
                                                        if (*src == '-')
                                                                frac_left_adjust = true;
@@ -925,26 +926,27 @@ static int out_file_context(char *pformat, size_t prefix_len,
 
        strcpy(pformat + prefix_len, "s");
        printf(pformat, (scontext ? scontext : "?"));
+#ifdef HAVE_SELINUX
        if (scontext)
                freecon(scontext);
+#endif
        return rc;
 }
 
 /* Map a TS with negative TS.tv_nsec to {0,0}.  */
 static inline struct timespec neg_to_zero(struct timespec ts)
 {
-       if (ts.tv_nsec >= 0) {
+       if (ts.tv_nsec >= 0)
                return ts;
-       } else {
-               struct timespec z = {0, 0};
 
-               return z;
-       }
+       struct timespec z = {0, 0};
+
+       return z;
 }
 
 /* All the mode bits that can be affected by chmod.  */
 #define CHMOD_MODE_BITS \
-       (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
+       (S_ISUID | S_ISGID | S_ISVTX | 0700 | 0070 | 0007)
 
 /* Print statx info.  Return zero upon success, nonzero upon failure.  */
 static int print_statx(char *pformat, size_t prefix_len, unsigned int m,
@@ -1291,6 +1293,7 @@ static char *default_format(bool fs, bool terse, bool device)
 ");
                        free(temp);
 
+#ifdef HAVE_SELINUX
                        if (is_selinux_enabled() > 0) {
                                temp = format;
                                /* TRANSLATORS: This string uses format
@@ -1301,7 +1304,7 @@ static char *default_format(bool fs, bool terse, bool device)
                                                   "Context: %C\n");
                                free(temp);
                        }
-
+#endif
                        temp = format;
                        /* TRANSLATORS: This string uses format specifiers from
                         * 'stat --help' without --file-system, and NOT from
@@ -1371,6 +1374,8 @@ static int do_dir_list(char const *dirname, unsigned int request_mask,
        DIR *dir;
        struct dirent *ent;
        char fullname[PATH_MAX];
+       size_t size = sizeof(fullname);
+       int namelen;
        int rc = 0;
 
        dir = opendir(dirname);
@@ -1406,8 +1411,11 @@ static int do_dir_list(char const *dirname, unsigned int request_mask,
                                        rc = -ENAMETOOLONG;
                                continue;
                        }
-                       snprintf(fullname, PATH_MAX, "%s/%s",
-                                dirname, ent->d_name);
+                       namelen = snprintf(fullname, size, "%s/%s",
+                                          dirname, ent->d_name);
+                       if (namelen >= size)
+                               fullname[size - 1] = '\0';
+
                        ret = do_statx(fullname, request_mask, flags);
                        if (!ret)
                                putchar('\n');