Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / e2fsck / badblocks.c
index 62f99c9..c81baae 100644 (file)
@@ -6,6 +6,9 @@
  */
 
 #include <time.h>
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
 
 #include <et/com_err.h>
 #include "e2fsck.h"
@@ -16,7 +19,7 @@ static int check_bb_inode_blocks(ext2_filsys fs, blk_t *block_nr, int blockcnt,
 
 static void invalid_block(ext2_filsys fs, blk_t blk)
 {
-       printf("Bad block %lu out of range; ignored.\n", blk);
+       printf("Bad block %u out of range; ignored.\n", blk);
        return;
 }
 
@@ -26,6 +29,7 @@ void read_bad_blocks_file(ext2_filsys fs, const char *bad_blocks_file,
        errcode_t       retval;
        badblocks_list  bb_list = 0;
        FILE            *f;
+       char            buf[1024];
 
        read_bitmaps(fs);
 
@@ -41,7 +45,6 @@ void read_bad_blocks_file(ext2_filsys fs, const char *bad_blocks_file,
                fatal_error(0);
        }
        
-       
        /*
         * If we're appending to the bad blocks inode, read in the
         * current bad blocks.
@@ -56,16 +59,33 @@ void read_bad_blocks_file(ext2_filsys fs, const char *bad_blocks_file,
        }
        
        /*
-        * Now read in the bad blocks from the file.
+        * Now read in the bad blocks from the file; if
+        * bad_blocks_file is null, then try to run the badblocks
+        * command.
         */
-       f = fopen(bad_blocks_file, "r");
-       if (!f) {
-               com_err("read_bad_blocks_file", errno,
-                       "while trying to open %s", bad_blocks_file);
-               fatal_error(0);
+       if (bad_blocks_file) {
+               f = fopen(bad_blocks_file, "r");
+               if (!f) {
+                       com_err("read_bad_blocks_file", errno,
+                               "while trying to open %s", bad_blocks_file);
+                       fatal_error(0);
+               }
+       } else {
+               sprintf(buf, "badblocks %s%s %d", preen ? "" : "-s ",
+                       fs->device_name,
+                       fs->super->s_blocks_count);
+               f = popen(buf, "r");
+               if (!f) {
+                       com_err("read_bad_blocks_file", errno,
+                               "while trying popen '%s'", buf);
+                       fatal_error(0);
+               }
        }
        retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block);
-       fclose (f);
+       if (bad_blocks_file) 
+               fclose(f);
+       else
+               pclose(f);
        if (retval) {
                com_err("ext2fs_read_bb_FILE", retval,
                        "while reading in list of bad blocks from file");
@@ -86,6 +106,11 @@ void read_bad_blocks_file(ext2_filsys fs, const char *bad_blocks_file,
        return;
 }
 
+void test_disk(ext2_filsys fs)
+{
+       read_bad_blocks_file(fs, 0, 1);
+}
+
 static int check_bb_inode_blocks(ext2_filsys fs, blk_t *block_nr, int blockcnt,
                                 void *private)
 {
@@ -97,7 +122,7 @@ static int check_bb_inode_blocks(ext2_filsys fs, blk_t *block_nr, int blockcnt,
         */
        if (*block_nr >= fs->super->s_blocks_count ||
            *block_nr < fs->super->s_first_data_block) {
-               printf("Warning illegal block %lu found in bad block inode.  Cleared.\n", *block_nr);
+               printf("Warning illegal block %u found in bad block inode.  Cleared.\n", *block_nr);
                *block_nr = 0;
                return BLOCK_CHANGED;
        }
@@ -105,58 +130,3 @@ static int check_bb_inode_blocks(ext2_filsys fs, blk_t *block_nr, int blockcnt,
        return 0;
 }
 
-void test_disk(ext2_filsys fs)
-{
-       errcode_t       retval;
-       badblocks_list  bb_list = 0;
-       FILE            *f;
-       char            buf[1024];
-
-       read_bitmaps(fs);
-       
-       /*
-        * Always read in the current list of bad blocks.
-        */
-       retval = ext2fs_read_bb_inode(fs, &bb_list);
-       if (retval) {
-               com_err("ext2fs_read_bb_inode", retval,
-                       "while reading the bad blocks inode");
-               fatal_error(0);
-       }
-       
-       /*
-        * Now run the bad blocks program
-        */
-       sprintf(buf, "badblocks %s%s %ld", preen ? "" : "-s ",
-               fs->device_name,
-               fs->super->s_blocks_count);
-       if (verbose)
-               printf("Running command: %s\n", buf);
-       f = popen(buf, "r");
-       if (!f) {
-               com_err("popen", errno,
-                       "while trying to run %s", buf);
-               fatal_error(0);
-       }
-       retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block);
-       fclose (f);
-       if (retval) {
-               com_err("ext2fs_read_bb_FILE", retval,
-                       "while processing list of bad blocks from program");
-               fatal_error(0);
-       }
-       
-       /*
-        * Finally, update the bad blocks from the bad_block_map
-        */
-       retval = ext2fs_update_bb_inode(fs, bb_list);
-       if (retval) {
-               com_err("ext2fs_update_bb_inode", retval,
-                       "while updating bad block inode");
-               fatal_error(0);
-       }
-
-       badblocks_list_free(bb_list);
-       return;
-}
-