Whamcloud - gitweb
Use ext2_loff_t instead of loff_t
[tools/e2fsprogs.git] / misc / findsuper.c
index f1e2315..765295c 100644 (file)
@@ -6,20 +6,6 @@
  * using gpart program.
  *
  * Portions Copyright 1998-2000, Theodore Ts'o.
- * 
- * This program may be used under the provisions of the GNU Public
- * License, *EXCEPT* that a binary copy of the executable may not be
- * packaged as a part of binary package which is distributed as part
- * of a Linux distribution.  (Yes, this violates the Debian Free
- * Software Guidelines in terms of restricting its field of use.
- * That's the point.  I don't want this program being distributed in
- * Debian, because I don't care to support it, and the maintainer,
- * Yann Dirson, doesn't seem to pay attention to my wishes on this
- * matter.  So I'm delibiately adding this clause so it violates the
- * Debian Free Software Guidelines to force him to take it out.  If
- * this doesn't work, I'll have to remove it from the upstream source
- * distribution at the next release.  End of Rant.  :-)
- * 
  *
  * Well, here's my linux version of findsuper.
  * I'm sure you coulda done it faster.  :)
@@ -32,7 +18,7 @@
  *
  * I wanted to add msdos support, but I couldn't make heads or tails
  * of the kernel include files to find anything I could look for in msdos.
- * 
+ *
  * Reading every block of a Sun partition is fairly quick.  Doing the
  * same under linux (slower hardware I suppose) just isn't the same.
  * It might be more useful to default to reading the first (second?) block
@@ -44,7 +30,7 @@
  *   findsuper /dev/hda
  *   findsuper /dev/hda 437760 1024   (my disk has cyls of 855*512)
  *
- * I suppose the next step is to figgure out a way to determine if
+ * I suppose the next step is to figure out a way to determine if
  * the block found is the first superblock somehow, and if so, build
  * a partition table from the superblocks found... but this is still
  * useful as is.
  * Note that gpart does not appear to find all superblocks that aren't aligned
  * with the start of a possible partition, so it is not useful in systems
  * with LVM or similar setups which don't use fat partition alignment.
+ *
+ * %Begin-Header%
+ * This file may be redistributed under the terms of the GNU Public
+ * License.
+ * %End-Header%
  */
 
 /*
  * Documentation addendum added by Andreas dwguest@win.tue.nl/aeb@cwi.nl
- * 
+ *
  * The program findsuper is a utility that scans a disk and finds
- * copies of ext2 superblocks (by checking for the ext2 signature
- * 
+ * copies of ext2 superblocks (by checking for the ext2 signature).
+ *
  * For each superblock found, it prints the offset in bytes, the
- * offset in 1024-byte blocks, the size of ext2 partition in fs
+ * offset in 1024-byte blocks, the size of the ext2 partition in fs
  * blocks, the filesystem blocksize (in bytes), the block group number
  * (always 0 for older ext2 systems), and a timestamp (s_mtime).
- * 
+ *
  * This program can be used to retrieve partitions that have been
  * lost.  The superblock for block group 0 is found 1 block (2
  * sectors) after the partition start.
- * 
+ *
  * For new systems that have a block group number in the superblock it
  * is immediately clear which superblock is the first of a partition.
  * For old systems where no group numbers are given, the first
- * superblock can be recognised by the timestamp: all superblock
+ * superblock can be recognized by the timestamp: all superblock
  * copies have the creation time in s_mtime, except the first, which
  * has the last time e2fsck or tune2fs wrote to the filesystem.
- * 
+ *
  */
 
 #define _FILE_OFFSET_BITS 64
 
+#include "config.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
 
 #include "ext2fs/ext2_fs.h"
-#include "nls-enable.h"
+#include "ext2fs/ext2fs.h"
+#include "support/nls-enable.h"
 
 #undef DEBUG
 
 #define WHY(fmt, arg...) { continue; }
 #endif
 
+static void usage(void)
+{
+       fprintf(stderr,
+               _("Usage:  findsuper device [skipbytes [startkb]]\n"));
+       exit(1);
+}
+
+
 int main(int argc, char *argv[])
 {
        int skiprate=512;               /* one sector */
@@ -119,53 +120,79 @@ int main(int argc, char *argv[])
        char *s;
        time_t tm, last = time(0);
        loff_t interval = 1024 * 1024;
-
+       int c, print_jnl_copies = 0;
+       const char * device_name;
        struct ext2_super_block ext2;
        /* interesting fields: EXT2_SUPER_MAGIC
         *      s_blocks_count s_log_block_size s_mtime s_magic s_lastcheck */
 
 #ifdef ENABLE_NLS
        setlocale(LC_MESSAGES, "");
+       setlocale(LC_CTYPE, "");
        bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
        textdomain(NLS_CAT_NAME);
+       set_com_err_gettext(gettext);
 #endif
-       if (argc<2) {
-               fprintf(stderr,
-                       _("Usage:  findsuper device [skipbytes [startkb]]\n"));
-               exit(1);
+
+       while ((c = getopt (argc, argv, "j")) != EOF) {
+               switch (c) {
+               case 'j':
+                       print_jnl_copies++;
+                       break;
+               default:
+                       usage();
+               }
        }
-       if (argc>2)
-               skiprate = strtol(argv[2], &s, 0);
-       if (s == argv[2]) {
-               fprintf(stderr,_("skiprate should be a number, not %s\n"), s);
-               exit(1);
+
+       if (optind == argc)
+               usage();
+
+       device_name = argv[optind++];
+
+       if (optind < argc) {
+               skiprate = strtol(argv[optind], &s, 0);
+               if (s == argv[optind]) {
+                       fprintf(stderr,_("skipbytes should be a number, not %s\n"), s);
+                       exit(1);
+               }
+               optind++;
        }
        if (skiprate & 0x1ff) {
                fprintf(stderr,
                        _("skipbytes must be a multiple of the sector size\n"));
                exit(2);
        }
-       if (argc>3)
-               sk = skl = strtoll(argv[3], &s, 0) << 10;
-       if (s == argv[3]) {
-               fprintf(stderr,_("startkb should be a number, not %s\n"), s);
-               exit(1);
+       if (optind < argc) {
+               sk = skl = strtoll(argv[optind], &s, 0) << 10;
+               if (s == argv[optind]) {
+                       fprintf(stderr,
+                               _("startkb should be a number, not %s\n"), s);
+                       exit(1);
+               }
+               optind++;
        }
        if (sk < 0) {
-               fprintf(stderr,_("startkb should be positive, not %Ld\n"), sk);
+               fprintf(stderr, _("startkb should be positive, not %llu\n"),sk);
                exit(1);
        }
-       fd = open(argv[1], O_RDONLY);
+
+       fd = open(device_name, O_RDONLY);
        if (fd < 0) {
-               perror(argv[1]);
+               perror(device_name);
                exit(1);
        }
-       /* Now, go looking for the superblock ! */
-       printf(_("starting at %Ld, with %d byte increments\n"), sk, skiprate);
-       printf(_("       thisoff     block fs_blk_sz  blksz grp last_mount\n"));
+
+       /* Now, go looking for the superblock! */
+       printf(_("starting at %llu, with %u byte increments\n"), sk, skiprate);
+       if (print_jnl_copies)
+               printf(_("[*] probably superblock written in the ext3 "
+                        "journal superblock,\n\tso start/end/grp wrong\n"));
+       printf(_("byte_offset  byte_start     byte_end  fs_blocks blksz  grp  mkfs/mount_time           sb_uuid label\n"));
        for (; lseek64(fd, sk, SEEK_SET) != -1 &&
               read(fd, &ext2, 512) == 512; sk += skiprate) {
+               static unsigned char last_uuid[16] = "blah";
+               unsigned long long bsize, grpsize;
+               int jnl_copy, sb_offset;
 
                if (sk && !(sk & (interval - 1))) {
                        time_t now, diff;
@@ -176,7 +203,7 @@ int main(int argc, char *argv[])
                        if (diff > 0) {
                                s = ctime(&now);
                                s[24] = 0;
-                               printf("\r%14Ld: %8LdkB/s @ %s", sk,
+                               printf("\r%11Lu: %8LukB/s @ %s", sk,
                                       (((sk - skl)) / diff) >> 10, s);
                                fflush(stdout);
                        }
@@ -189,27 +216,53 @@ int main(int argc, char *argv[])
                }
                if (ext2.s_magic != EXT2_SUPER_MAGIC)
                        continue;
-               if (ext2.s_log_block_size > 4)
-                       WHY("log block size > 4 (%d)\n", ext2.s_log_block_size);
-               if (ext2.s_r_blocks_count > ext2.s_blocks_count)
-                       WHY("r_blocks_count > blocks_count (%d > %d)\n",
-                           ext2.s_r_blocks_count, ext2.s_blocks_count);
-               if (ext2.s_free_blocks_count > ext2.s_blocks_count)
-                       WHY("free_blocks_count > blocks_count\n (%d > %d)\n",
-                           ext2.s_free_blocks_count, ext2.s_blocks_count);
+               if (ext2.s_log_block_size > 6)
+                       WHY("log block size > 6 (%u)\n", ext2.s_log_block_size);
+               if (ext2fs_r_blocks_count(&ext2) > ext2fs_blocks_count(&ext2))
+                       WHY("r_blocks_count > blocks_count (%u > %u)\n",
+                           ext2fs_r_blocks_count(&ext2),
+                           ext2fs_blocks_count(&ext2));
+               if (ext2fs_free_blocks_count(&ext2) > ext2fs_blocks_count(&ext2))
+                       WHY("free_blocks_count > blocks_count\n (%u > %u)\n",
+                           ext2fs_free_blocks_count(&ext2),
+                           ext2fs_blocks_count(&ext2));
                if (ext2.s_free_inodes_count > ext2.s_inodes_count)
-                       WHY("free_inodes_count > inodes_count (%d > %d)\n",
+                       WHY("free_inodes_count > inodes_count (%u > %u)\n",
                            ext2.s_free_inodes_count, ext2.s_inodes_count);
 
-               tm = ext2.s_mtime;
-               s=ctime(&tm);
-               s[24]=0;
-               printf("\r%14Ld %9Ld %9d %5d %4d %s\n",
-                      sk, sk >> 10, ext2.s_blocks_count,
-                      1 << (ext2.s_log_block_size + 10),
-                      ext2.s_block_group_nr, s);
+               if (ext2.s_mkfs_time != 0)
+                       tm = ext2.s_mkfs_time;
+               else
+                       tm = ext2.s_mtime;
+               s = ctime(&tm);
+               s[24] = 0;
+               bsize = 1 << (ext2.s_log_block_size + 10);
+               grpsize = bsize * ext2.s_blocks_per_group;
+               if (memcmp(ext2.s_uuid, last_uuid, sizeof(last_uuid)) == 0 &&
+                   ext2.s_rev_level > 0 && ext2.s_block_group_nr == 0) {
+                       jnl_copy = 1;
+               } else {
+                       jnl_copy = 0;
+                       memcpy(last_uuid, ext2.s_uuid, sizeof(last_uuid));
+               }
+               if (ext2.s_block_group_nr == 0 || bsize == 1024)
+                       sb_offset = 1024;
+               else
+                       sb_offset = 0;
+               if (jnl_copy && !print_jnl_copies)
+                       continue;
+               printf("\r%11Lu %11Lu%s %11Lu%s %9u %5Lu %4u%s %s %02x%02x%02x%02x %.*s\n",
+                      sk, sk - ext2.s_block_group_nr * grpsize - sb_offset,
+                      jnl_copy ? "*":" ",
+                      sk + ext2fs_blocks_count(&ext2) * bsize -
+                           ext2.s_block_group_nr * grpsize - sb_offset,
+                      jnl_copy ? "*" : " ", ext2fs_blocks_count(&ext2), bsize,
+                      ext2.s_block_group_nr, jnl_copy ? "*" : " ", s,
+                      ext2.s_uuid[0], ext2.s_uuid[1],
+                      ext2.s_uuid[2], ext2.s_uuid[3],
+                      EXT2_LEN_STR(ext2.s_volume_name));
        }
-       printf(_("\n%14Ld: finished with errno %d\n"), sk, errno);
+       printf(_("\n%11Lu: finished with errno %d\n"), sk, errno);
        close(fd);
 
        return errno;