Blob


1 #include <inttypes.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <fcntl.h>
6 #include <err.h>
7 #include "sufs.h"
9 static int fd;
10 static char *block;
11 static struct sufs_superblock sb;
13 #define print(sb, field) do_print (#field, (sb).field)
14 static void do_print (const char *name, uint64_t value)
15 {
16 printf ("%s: %"PRIu64"\n", name, value);
17 }
19 static void read_block (uint64_t blkno)
20 {
21 ssize_t x;
22 if (lseek (fd, blkno * sb.bsize, SEEK_SET) < 0)
23 err (1, "read_block(%"PRIu64"): lseek()", blkno);
24 x = read (fd, block, sb.bsize);
25 if (x != sb.bsize)
26 err (1, "read_block(%"PRIu64"): read(): %zd", blkno, x);
27 }
29 static uint64_t count (uint64_t begin, uint64_t num)
30 {
31 uint64_t cnt = 0;
32 uint64_t n = (num + 7) / 8;
34 for (uint64_t i = 0; i < n; i += sb.bsize) {
35 read_block (begin + i);
36 for (uint64_t j = 0; j < sb.bsize && j < n; ++j) {
37 for (unsigned k = 0; k < 8 && (((i + j) * 8 + k) < num); ++k) {
38 if ((block[j] >> k) & 1)
39 ++cnt;
40 }
41 }
42 }
44 return cnt;
45 }
47 int main (int argc, char *argv[])
48 {
49 if (argc != 2)
50 errx (1, "usage: dumpfs_sufs file");
52 fd = open (argv[1], O_RDONLY);
53 if (fd < 0)
54 err (1, "open()");
56 if (read (fd, &sb, sizeof (sb)) != sizeof (sb))
57 err (1, "read()");
59 if (sb.magic != SUFS_MAGIC) {
60 if (lseek (fd, SUFS_BOOTSIZE, SEEK_SET) < 0)
61 err (1, "lseek()");
63 if (read (fd, &sb, sizeof (sb)) != sizeof (sb))
64 err (1, "read()");
65 }
67 block = malloc (sb.bsize);
69 printf ("magic: %#08"PRIx32" (%s)\n", sb.magic, sb.magic == SUFS_MAGIC ? "OK" : "INVAL");
70 print (sb, bsize);
71 print (sb, nino);
72 print (sb, ndata);
73 print (sb, ibmoff);
74 print (sb, dbmoff);
75 print (sb, ioff);
76 print (sb, doff);
77 print (sb, clean);
78 do_print ("ifree", count (sb.ibmoff, sb.nino));
79 do_print ("dfree", count (sb.dbmoff, sb.ndata));
81 close (fd);
82 return 0;
83 }