Whamcloud - gitweb
libcom_err: deal with the fact that the Hurd error messages are not zero-based
[tools/e2fsprogs.git] / misc / filefrag.c
index d89d3c9..1eec146 100644 (file)
@@ -45,7 +45,9 @@ extern int optind;
 #include <sys/stat.h>
 #include <sys/vfs.h>
 #include <sys/ioctl.h>
+#ifdef HAVE_LINUX_FD_H
 #include <linux/fd.h>
+#endif
 #include <ext2fs/ext2fs.h>
 #include <ext2fs/ext2_types.h>
 #include <ext2fs/fiemap.h>
@@ -179,7 +181,7 @@ static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
        print_flag(&fe_flags, FIEMAP_EXTENT_SHARED, flags, "shared,");
        /* print any unknown flags as hex values */
        for (mask = 1; fe_flags != 0 && mask != 0; mask <<= 1) {
-               char hex[6];
+               char hex[sizeof(mask) * 2 + 4]; /* 2 chars/byte + 0x, + NUL */
 
                if ((fe_flags & mask) == 0)
                        continue;
@@ -208,7 +210,7 @@ static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
        __u64 buf[2048];        /* __u64 for proper field alignment */
        struct fiemap *fiemap = (struct fiemap *)buf;
        struct fiemap_extent *fm_ext = &fiemap->fm_extents[0];
-       struct fiemap_extent fm_last = {0};
+       struct fiemap_extent fm_last;
        int count = (sizeof(buf) - sizeof(*fiemap)) /
                        sizeof(struct fiemap_extent);
        unsigned long long expected = 0;
@@ -221,6 +223,7 @@ static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
        int rc;
 
        memset(fiemap, 0, sizeof(struct fiemap));
+       memset(&fm_last, 0, sizeof(fm_last));
 
        if (sync_file)
                flags |= FIEMAP_FLAG_SYNC;
@@ -306,8 +309,8 @@ static int filefrag_fibmap(int fd, int blk_shift, int *num_extents,
                fm_ext.fe_flags = FIEMAP_EXTENT_MERGED;
        }
 
-       if (sync_file)
-               fsync(fd);
+       if (sync_file && fsync(fd) != 0)
+               return -errno;
 
        for (i = 0, logical = 0, *num_extents = 0, count = last_block = 0;
             i < numblocks;
@@ -514,7 +517,7 @@ out_close:
 
 static void usage(const char *progname)
 {
-       fprintf(stderr, "Usage: %s [-b{blocksize}] [-BeklsvxX] file ...\n",
+       fprintf(stderr, "Usage: %s [-b{blocksize}] [-BeksvxX] file ...\n",
                progname);
        exit(1);
 }
@@ -534,15 +537,19 @@ int main(int argc, char**argv)
                                char *end;
                                blocksize = strtoul(optarg, &end, 0);
                                if (end) {
+#if __GNUC_PREREQ (7, 0)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
+#endif
                                        switch (end[0]) {
                                        case 'g':
                                        case 'G':
                                                blocksize *= 1024;
-                                               /* no break */
+                                               /* fall through */
                                        case 'm':
                                        case 'M':
                                                blocksize *= 1024;
-                                               /* no break */
+                                               /* fall through */
                                        case 'k':
                                        case 'K':
                                                blocksize *= 1024;
@@ -550,6 +557,9 @@ int main(int argc, char**argv)
                                        default:
                                                break;
                                        }
+#if __GNUC_PREREQ (7, 0)
+#pragma GCC diagnostic pop
+#endif
                                }
                        } else { /* Allow -b without argument for compat. Remove
                                  * this eventually so "-b {blocksize}" works */
@@ -588,7 +598,7 @@ int main(int argc, char**argv)
        if (optind == argc)
                usage(argv[0]);
 
-       for (cpp = argv + optind; *cpp != '\0'; cpp++) {
+       for (cpp = argv + optind; *cpp != NULL; cpp++) {
                int rc2 = frag_report(*cpp);
 
                if (rc2 < 0 && rc == 0)