Merge branches 'hammer2' and 'master' of ssh://crater.dragonflybsd.org/repository...
[dragonfly.git] / lib / libstand / hammer2.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <sys/uuid.h>
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include <strings.h>
13 #include <errno.h>
14
15 #include <hammer2/hammer2_disk.h>
16
17 struct hammer2 {
18         int                             fd;     /* Device fd */
19         struct hammer2_blockref         sroot;  /* Superroot blockref */
20 };
21
22 struct inode {
23         struct hammer2_inode_data       dat;    /* raw inode data */
24         off_t                           doff;   /* disk inode offset */
25 };
26
27 off_t blockoff(ref)
28         struct hammer2_blockref ref;
29 {
30
31 }
32
33 hinit(hfs)
34         struct hammer2 *hfs;
35 {
36         struct hammer2_volume_data volhdr;
37         ssize_t rc;
38         hammer2_crc_t crc0;
39
40         rc = pread(hfs->fd, &volhdr, HAMMER2_VOLUME_SIZE, 0);
41         if (volhdr.magic == HAMMER2_VOLUME_ID_HBO) {
42                 printf("Valid HAMMER2 filesystem\n");
43         } else {
44                 return (-1);
45         }
46
47         hfs->sroot = volhdr.sroot_blockref;
48         return (0);
49 }
50
51 shread(hfs, ino, buf, off, len)
52         struct hammer2 *hfs;
53         struct inode *ino;
54         char *buf;
55         off_t off;
56         size_t len;
57 {
58         /*
59          * Read [off, off+len) from inode ino rather than from disk
60          * offsets; correctly decodes blockrefs/indirs/...
61          */
62 }
63
64 struct inode *hlookup1(hfs, ino, name)
65         struct hammer2 *hfs;
66         struct inode *ino;
67         char *name;
68 {
69         static struct inode filino;
70         off_t off;
71         int rc;
72
73         bzero(&filino, sizeof(struct inode));
74
75         for (off = 0;
76              off < ino->dat.size;
77              off += sizeof(struct hammer2_inode_data))
78         {
79                 rc = shread(hfs, ino, &filino.dat, off,
80                             sizeof(struct hammer2_inode_data));
81                 if (rc != sizeof(struct hammer2_inode_data))
82                         continue;
83                 if (strcmp(name, &filino.dat.filename) == 0)
84                         return (&filino);
85         }
86
87         return (NULL);
88 }
89
90 struct inode *hlookup(hfs, name)
91         struct hammer2 *hfs;
92         char *name;
93 {
94         /* Name is of form /SUPERROOT/a/b/c/file */
95
96 }
97
98 void hstat(hfs, ino, sb)
99         struct hammer2 *hfs;
100         struct inode *ino;
101         struct stat *sb;
102 {
103
104 }
105
106 main(argc, argv)
107         int argc;
108         char *argv[];
109 {
110         struct hammer2 hammer2;
111         struct inode *ino;
112         struct stat sb;
113         int i;
114
115         if (argc < 2) {
116                 fprintf(stderr, "usage: hammer2 <dev>\n");
117                 exit(1);
118         }
119
120         hammer2.fd = open(argv[1], O_RDONLY);
121         if (hammer2.fd < 0) {
122                 fprintf(stderr, "unable to open %s\n", argv[1]);
123                 exit(1);
124         }
125
126         if (hinit(&hammer2)) {
127                 fprintf(stderr, "invalid fs\n");
128                 close(hammer2.fd);
129                 exit(1);
130         }
131
132         for (i = 2; i < argc; i++) {
133                 ino = hlookup(&hammer2, argv[i]);
134                 if (ino == NULL) {
135                         fprintf(stderr, "hlookup %s\n", argv[i]);
136                         continue;
137                 }
138                 hstat(&hammer2, ino, &sb);
139
140                 printf("%s %lld", argv[i], sb.st_size);
141
142         }
143 }