6 #define blockWrite _blockWrite /* hack */
8 static void usage(void);
9 static u64int fdsize(int fd);
10 static void partition(int fd, int bsize, Header *h);
11 static u64int unittoull(char *s);
12 static u32int blockAlloc(int type, u32int tag);
13 static void blockRead(int part, u32int addr);
14 static void blockWrite(int part, u32int addr);
15 static void superInit(char *label, u32int root, uchar[VtScoreSize]);
16 static void rootMetaInit(Entry *e);
17 static u32int rootInit(Entry *e);
18 static void topLevel(char *name);
19 static int parseScore(uchar[VtScoreSize], char*);
20 static u32int ventiRoot(char*, char*);
23 #define TWID64 ((u64int)~(u64int)0)
39 fprint(2, "%s [y/n]: ", msg);
40 n = read(0, buf, sizeof buf - 1);
49 threadmain(int argc, char *argv[])
66 bsize = unittoull(EARGF(usage()));
71 host = EARGF(usage());
74 iso9660file = EARGF(usage());
75 iso9660off = atoi(EARGF(usage()));
78 label = EARGF(usage());
81 score = EARGF(usage());
85 * This is -y instead of -f because flchk has a
86 * (frequently used) -f option. I type flfmt instead
87 * of flchk all the time, and want to make it hard
88 * to reformat my file system accidentally.
98 if(iso9660file && score)
99 sysfatal("cannot use -i with -v");
101 fmtinstall('V', scoreFmt);
102 fmtinstall('L', labelFmt);
104 fd = open(argv[0], ORDWR);
106 sysfatal("could not open file: %s: %r", argv[0]);
108 buf = vtmallocz(bsize);
109 if(pread(fd, buf, bsize, HeaderOffset) != bsize)
110 sysfatal("could not read fs header block: %r");
112 if(headerUnpack(&h, buf) && !force
113 && !confirm("fs header block already exists; are you sure?"))
116 if((d = dirfstat(fd)) == nil)
117 sysfatal("dirfstat: %r");
119 if(d->type == 'M' && !force
120 && !confirm("fs file is mounted via devmnt (is not a kernel device); are you sure?"))
123 partition(fd, bsize, &h);
125 if(pwrite(fd, buf, bsize, HeaderOffset) < bsize)
126 sysfatal("could not write fs header: %r");
128 disk = diskAlloc(fd);
130 sysfatal("could not open disk: %r");
133 iso9660init(fd, &h, iso9660file, iso9660off);
136 memset(buf, 0, bsize);
137 for(bn = 0; bn < diskSize(disk, PartLabel); bn++)
138 blockWrite(PartLabel, bn);
141 iso9660labels(disk, buf, blockWrite);
144 root = ventiRoot(host, score);
150 superInit(label, root, vtzeroscore);
168 sysfatal("could not stat file: %r");
177 fprint(2, "usage: %s [-b blocksize] [-h host] [-i file offset] "
178 "[-l label] [-v score] [-y] file\n", argv0);
179 threadexitsall("usage");
183 partition(int fd, int bsize, Header *h)
185 ulong nblock, ndata, nlabel;
189 sysfatal("block size must be a multiple of 512 bytes");
190 if(bsize > VtMaxLumpSize)
191 sysfatal("block size must be less than %d", VtMaxLumpSize);
193 memset(h, 0, sizeof(*h));
194 h->blockSize = bsize;
196 lpb = bsize/LabelSize;
198 nblock = fdsize(fd)/bsize;
201 if(nblock < (HeaderOffset*10)/bsize)
202 sysfatal("file too small");
204 h->super = (HeaderOffset + 2*bsize)/bsize;
205 h->label = h->super + 1;
206 ndata = ((u64int)lpb)*(nblock - h->label)/(lpb+1);
207 nlabel = (ndata + lpb - 1)/lpb;
208 h->data = h->label + nlabel;
209 h->end = h->data + ndata;
231 e->psize = bsize/VtEntrySize*VtEntrySize;
232 e->flags = VtEntryActive;
235 memmove(e->score, vtzeroscore, VtScoreSize);
242 rootMetaInit(Entry *e)
250 memset(&de, 0, sizeof(de));
251 de.elem = vtstrdup("root");
258 de.uid = vtstrdup("adm");
259 de.gid = vtstrdup("adm");
260 de.mid = vtstrdup("adm");
265 de.mode = ModeDir | 0555;
268 addr = blockAlloc(BtData, tag);
270 /* build up meta block */
271 memset(buf, 0, bsize);
272 mbInit(&mb, buf, bsize, bsize/100);
273 me.size = deSize(&de);
274 me.p = mbAlloc(&mb, me.size);
277 mbInsert(&mb, 0, &me);
279 blockWrite(PartData, addr);
282 /* build up entry for meta block */
284 e->flags |= VtEntryLocal;
287 localToGlobal(addr, e->score);
298 addr = blockAlloc(BtDir, tag);
299 memset(buf, 0, bsize);
301 /* root meta data is in the third entry */
302 entryPack(e, buf, 2);
305 e->flags |= _VtEntryDir;
306 entryPack(e, buf, 0);
309 entryPack(e, buf, 1);
311 blockWrite(PartData, addr);
314 e->flags |= VtEntryLocal|_VtEntryDir;
315 e->size = VtEntrySize*3;
317 localToGlobal(addr, e->score);
319 addr = blockAlloc(BtDir, RootTag);
320 memset(buf, 0, bsize);
321 entryPack(e, buf, 0);
323 blockWrite(PartData, addr);
330 blockAlloc(int type, u32int tag)
336 lpb = bsize/LabelSize;
338 blockRead(PartLabel, addr/lpb);
339 if(!labelUnpack(&l, buf, addr % lpb))
340 sysfatal("bad label: %r");
341 if(l.state != BsFree)
342 sysfatal("want to allocate block already in use");
344 l.epochClose = ~(u32int)0;
348 labelPack(&l, buf, addr % lpb);
349 blockWrite(PartLabel, addr/lpb);
354 superInit(char *label, u32int root, uchar score[VtScoreSize])
358 memset(buf, 0, bsize);
359 memset(&s, 0, sizeof(s));
360 s.version = SuperVersion;
366 s.current = NilBlock;
367 strecpy(s.name, s.name+sizeof(s.name), label);
368 memmove(s.last, score, VtScoreSize);
371 blockWrite(PartSuper, 0);
382 n = strtoul(s, &es, 0);
383 if(*es == 'k' || *es == 'K'){
386 }else if(*es == 'm' || *es == 'M'){
389 }else if(*es == 'g' || *es == 'G'){
399 blockRead(int part, u32int addr)
401 if(!diskReadRaw(disk, part, addr, buf))
402 sysfatal("read failed: %r");
406 blockWrite(int part, u32int addr)
408 if(!diskWriteRaw(disk, part, addr, buf))
409 sysfatal("write failed: %r");
413 addFile(File *root, char *name, uint mode)
417 f = fileCreate(root, name, mode | ModeDir, "adm");
419 sysfatal("could not create file: %s: %r", name);
429 /* ok, now we can open as a fs */
430 fs = fsOpen(name, z, 100, OReadWrite);
432 sysfatal("could not open file system: %r");
434 root = fsGetRoot(fs);
436 sysfatal("could not open root: %r");
437 addFile(root, "active", 0555);
438 addFile(root, "archive", 0555);
439 addFile(root, "snapshot", 0555);
448 ventiRead(uchar score[VtScoreSize], int type)
452 n = vtread(z, score, type, buf, bsize);
454 sysfatal("ventiRead %V (%d) failed: %r", score, type);
455 vtzeroextend(type, buf, n, bsize);
460 ventiRoot(char *host, char *s)
463 uchar score[VtScoreSize];
471 if(!parseScore(score, s))
472 sysfatal("bad score '%s'", s);
474 if((z = vtdial(host)) == nil
476 sysfatal("connect to venti: %r");
479 addr = blockAlloc(BtDir, tag);
481 ventiRead(score, VtRootType);
482 if(vtrootunpack(&root, buf) < 0)
483 sysfatal("corrupted root: vtrootunpack");
484 n = ventiRead(root.score, VtDirType);
487 * Fossil's vac archives start with an extra layer of source,
490 if(n <= 2*VtEntrySize){
491 if(!entryUnpack(&e, buf, 0))
492 sysfatal("bad root: top entry");
493 n = ventiRead(e.score, VtDirType);
497 * There should be three root sources (and nothing else) here.
500 if(!entryUnpack(&e, buf, i)
501 || !(e.flags&VtEntryActive)
504 sysfatal("bad root: entry %d", i);
505 fprint(2, "%V\n", e.score);
507 if(n > 3*VtEntrySize)
508 sysfatal("bad root: entry count");
510 blockWrite(PartData, addr);
513 * Maximum qid is recorded in root's msource, entry #2 (conveniently in e).
515 ventiRead(e.score, VtDataType);
516 if(!mbUnpack(&mb, buf, bsize))
517 sysfatal("bad root: mbUnpack");
518 meUnpack(&me, &mb, 0);
519 if(!deUnpack(&de, &me))
520 sysfatal("bad root: dirUnpack");
522 sysfatal("bad root: no qidSpace");
526 * Recreate the top layer of source.
529 e.flags |= VtEntryLocal|_VtEntryDir;
530 e.size = VtEntrySize*3;
532 localToGlobal(addr, e.score);
534 addr = blockAlloc(BtDir, RootTag);
535 memset(buf, 0, bsize);
536 entryPack(&e, buf, 0);
537 blockWrite(PartData, addr);
543 parseScore(uchar *score, char *buf)
547 memset(score, 0, VtScoreSize);
549 if(strlen(buf) < VtScoreSize*2)
551 for(i=0; i<VtScoreSize*2; i++){
552 if(buf[i] >= '0' && buf[i] <= '9')
554 else if(buf[i] >= 'a' && buf[i] <= 'f')
555 c = buf[i] - 'a' + 10;
556 else if(buf[i] >= 'A' && buf[i] <= 'F')
557 c = buf[i] - 'A' + 10;