Blame


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