Whamcloud - gitweb
fuse2fs: add support for 32-bit uids and gids
[tools/e2fsprogs.git] / misc / e2fuzz.c
index 644c9c5..8576d4e 100644 (file)
@@ -18,6 +18,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <stdint.h>
+#include <unistd.h>
 #ifdef HAVE_GETOPT_H
 #include <getopt.h>
 #endif
@@ -31,7 +32,17 @@ static int metadata_only = 1;
 static unsigned long long user_corrupt_bytes = 0;
 static double user_corrupt_pct = 0.0;
 
-int getseed(void)
+#if !defined HAVE_PWRITE64 && !defined HAVE_PWRITE
+static ssize_t my_pwrite(int fd, const void *buf, size_t count, off_t offset)
+{
+       if (lseek(fd, offset, SEEK_SET) < 0)
+               return 0;
+
+       return write(fd, buf, count);
+}
+#endif /* !defined HAVE_PWRITE64 && !defined HAVE_PWRITE */
+
+static int getseed(void)
 {
        int r;
        int fd;
@@ -41,7 +52,8 @@ int getseed(void)
                perror("open");
                exit(0);
        }
-       read(fd, &r, sizeof(r));
+       if (read(fd, &r, sizeof(r)) != sizeof(r))
+               printf("Unable to read random seed!\n");
        close(fd);
        return r;
 }
@@ -53,8 +65,11 @@ struct find_block {
        blk64_t corrupt_blocks;
 };
 
-int find_block_helper(ext2_filsys fs, blk64_t *blocknr, e2_blkcnt_t blockcnt,
-                     blk64_t ref_blk, int ref_offset, void *priv_data)
+static int find_block_helper(ext2_filsys fs EXT2FS_ATTR((unused)),
+                            blk64_t *blocknr, e2_blkcnt_t blockcnt,
+                            blk64_t ref_blk EXT2FS_ATTR((unused)),
+                            int ref_offset EXT2FS_ATTR((unused)),
+                            void *priv_data)
 {
        struct find_block *fb = (struct find_block *)priv_data;
 
@@ -66,11 +81,11 @@ int find_block_helper(ext2_filsys fs, blk64_t *blocknr, e2_blkcnt_t blockcnt,
        return 0;
 }
 
-errcode_t find_metadata_blocks(ext2_filsys fs, ext2fs_block_bitmap bmap,
-                              off_t *corrupt_bytes)
+static errcode_t find_metadata_blocks(ext2_filsys fs, ext2fs_block_bitmap bmap,
+                                     off_t *corrupt_bytes)
 {
        dgrp_t i;
-       blk64_t b, c, d, j, old_desc_blocks;
+       blk64_t b, c;
        ext2_inode_scan scan;
        ext2_ino_t ino;
        struct ext2_inode inode;
@@ -79,12 +94,6 @@ errcode_t find_metadata_blocks(ext2_filsys fs, ext2fs_block_bitmap bmap,
 
        *corrupt_bytes = 0;
        fb.corrupt_blocks = 0;
-       if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
-                                     EXT2_FEATURE_INCOMPAT_META_BG))
-               old_desc_blocks = fs->super->s_first_meta_bg;
-       else
-               old_desc_blocks = fs->desc_blocks +
-                                 fs->super->s_reserved_gdt_blocks;
 
        /* Construct bitmaps of super/descriptor blocks */
        for (i = 0; i < fs->group_desc_count; i++) {
@@ -154,10 +163,10 @@ out:
        return retval;
 }
 
-uint64_t rand_num(uint64_t min, uint64_t max)
+static uint64_t rand_num(uint64_t min, uint64_t max)
 {
        uint64_t x;
-       int i;
+       unsigned int i;
        uint8_t *px = (uint8_t *)&x;
 
        for (i = 0; i < sizeof(x); i++)
@@ -166,7 +175,7 @@ uint64_t rand_num(uint64_t min, uint64_t max)
        return min + (uint64_t)((double)(max - min) * (x / (UINT64_MAX + 1.0)));
 }
 
-int process_fs(const char *fsname)
+static int process_fs(const char *fsname)
 {
        errcode_t ret;
        int flags, fd;
@@ -174,7 +183,7 @@ int process_fs(const char *fsname)
        ext2fs_block_bitmap corrupt_map;
        off_t hsize, count, off, offset, corrupt_bytes;
        unsigned char c;
-       unsigned long i;
+       off_t i;
 
        /* If mounted rw, force dryrun mode */
        ret = ext2fs_check_if_mounted(fsname, &flags);
@@ -267,39 +276,49 @@ int process_fs(const char *fsname)
                if ((rand() % 2) && c < 128)
                        c |= 0x80;
                if (verbose)
-                       printf("Corrupting byte %jd in block %jd to 0x%x\n",
-                              off % fs->blocksize, off / fs->blocksize, c);
+                       printf("Corrupting byte %lld in block %lld to 0x%x\n",
+                              (long long) off % fs->blocksize,
+                              (long long) off / fs->blocksize, c);
                if (dryrun)
                        continue;
+#ifdef HAVE_PWRITE64
                if (pwrite64(fd, &c, sizeof(c), off) != sizeof(c)) {
                        perror(fsname);
                        goto fail3;
                }
+#elif HAVE_PWRITE
+               if (pwrite(fd, &c, sizeof(c), off) != sizeof(c)) {
+                       perror(fsname);
+                       goto fail3;
+               }
+#else
+               if (my_pwrite(fd, &c, sizeof(c), off) != sizeof(c)) {
+                       perror(fsname);
+                       goto fail3;
+               }
+#endif
        }
        close(fd);
 
        /* Clean up */
-       ret = ext2fs_close(fs);
+       ret = ext2fs_close_free(&fs);
        if (ret) {
-               fs = NULL;
                fprintf(stderr, "%s: error while closing filesystem\n",
                        fsname);
-               goto fail2;
+               return 1;
        }
 
        return 0;
 fail3:
        close(fd);
-fail2:
        if (corrupt_map != fs->block_map)
                ext2fs_free_block_bitmap(corrupt_map);
 fail:
-       if (fs)
-               ext2fs_close(fs);
+       ext2fs_close_free(&fs);
        return 1;
 }
 
-void print_help(const char *progname)
+static void print_help(const char *progname)
 {
        printf("Usage: %s OPTIONS device\n", progname);
        printf("-b:     Corrupt this many bytes.\n");