file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. #include "stdinc.h"
  2. #include "vac.h"
  3. #include "dat.h"
  4. #include "fns.h"
  5. #include "error.h"
  6. /*
  7. * locking order is upwards. A thread can hold the lock for a VacFile
  8. * and then acquire the lock of its parent
  9. */
  10. struct VacFile {
  11. /* meta data for file: protected by the lk in the parent */
  12. int ref; /* holds this data structure up */
  13. VacFS *fs; /* immutable */
  14. int removed; /* file has been removed */
  15. int dirty; /* dir is dirty with respect to meta data in block */
  16. ulong block; /* block offset withing msource for this file's meta data */
  17. VacDir dir; /* meta data for this file */
  18. VacFile *up; /* parent file */
  19. VacFile *next; /* sibling */
  20. /* data for file */
  21. VtLock *lk; /* lock for source and msource */
  22. Source *source;
  23. Source *msource; /* for directories: meta data for children */
  24. VacFile *down; /* children */
  25. };
  26. char *vfName(VacFile *, char *);
  27. static int vfMetaFlush(VacFile*);
  28. static ulong msAlloc(Source *ms, ulong, int n);
  29. static void
  30. vfRUnlock(VacFile *vf)
  31. {
  32. vtRUnlock(vf->lk);
  33. }
  34. static int
  35. vfRLock(VacFile *vf)
  36. {
  37. vtRLock(vf->lk);
  38. if(vf->source == nil) {
  39. vfRUnlock(vf);
  40. vtSetError(ERemoved);
  41. return 0;
  42. }
  43. return 1;
  44. }
  45. static void
  46. vfUnlock(VacFile *vf)
  47. {
  48. vtUnlock(vf->lk);
  49. }
  50. static int
  51. vfLock(VacFile *vf)
  52. {
  53. vtLock(vf->lk);
  54. if(vf->source == nil) {
  55. vfUnlock(vf);
  56. vtSetError(ERemoved);
  57. return 0;
  58. }
  59. return 1;
  60. }
  61. static void
  62. vfMetaLock(VacFile *vf)
  63. {
  64. assert(vf->up->msource != nil);
  65. vtLock(vf->up->lk);
  66. }
  67. static void
  68. vfMetaUnlock(VacFile *vf)
  69. {
  70. vtUnlock(vf->up->lk);
  71. }
  72. static void
  73. vfRAccess(VacFile* vf)
  74. {
  75. vfMetaLock(vf);
  76. vf->dir.atime = time(0L);
  77. vf->dirty = 1;
  78. vfMetaUnlock(vf);
  79. vfMetaFlush(vf);
  80. }
  81. static void
  82. vfWAccess(VacFile* vf, char *mid)
  83. {
  84. vfMetaLock(vf);
  85. vf->dir.atime = vf->dir.mtime = time(0L);
  86. if(strcmp(vf->dir.mid, mid) != 0) {
  87. vtMemFree(vf->dir.mid);
  88. vf->dir.mid = vtStrDup(mid);
  89. }
  90. vf->dir.mcount++;
  91. vf->dirty = 1;
  92. vfMetaUnlock(vf);
  93. vfMetaFlush(vf);
  94. }
  95. void
  96. vdCleanup(VacDir *dir)
  97. {
  98. vtMemFree(dir->elem);
  99. dir->elem = nil;
  100. vtMemFree(dir->uid);
  101. dir->uid = nil;
  102. vtMemFree(dir->gid);
  103. dir->gid = nil;
  104. vtMemFree(dir->mid);
  105. dir->mid = nil;
  106. }
  107. void
  108. vdCopy(VacDir *dst, VacDir *src)
  109. {
  110. *dst = *src;
  111. dst->elem = vtStrDup(src->elem);
  112. dst->uid = vtStrDup(src->uid);
  113. dst->gid = vtStrDup(src->gid);
  114. dst->mid = vtStrDup(src->mid);
  115. }
  116. static int
  117. mbSearch(MetaBlock *mb, char *elem, int *ri, MetaEntry *me)
  118. {
  119. int i;
  120. int b, t, x;
  121. /* binary search within block */
  122. b = 0;
  123. t = mb->nindex;
  124. while(b < t) {
  125. i = (b+t)>>1;
  126. if(!meUnpack(me, mb, i))
  127. return 0;
  128. if(mb->unbotch)
  129. x = meCmpNew(me, elem);
  130. else
  131. x = meCmp(me, elem);
  132. if(x == 0) {
  133. *ri = i;
  134. return 1;
  135. }
  136. if(x < 0)
  137. b = i+1;
  138. else /* x > 0 */
  139. t = i;
  140. }
  141. assert(b == t);
  142. *ri = b; /* b is the index to insert this entry */
  143. memset(me, 0, sizeof(*me));
  144. return 1;
  145. }
  146. static void
  147. mbInit(MetaBlock *mb, uchar *p, int n)
  148. {
  149. memset(mb, 0, sizeof(MetaBlock));
  150. mb->maxsize = n;
  151. mb->buf = p;
  152. mb->maxindex = n/100;
  153. mb->size = MetaHeaderSize + mb->maxindex*MetaIndexSize;
  154. }
  155. static int
  156. vfMetaFlush(VacFile *vf)
  157. {
  158. VacFile *vfp;
  159. Lump *u;
  160. MetaBlock mb;
  161. MetaEntry me, nme;
  162. uchar *p;
  163. int i, n, moved;
  164. //print("vfMetaFlush %s\n", vf->dir.elem);
  165. /* assume name has not changed for the moment */
  166. vfMetaLock(vf);
  167. vfp = vf->up;
  168. moved = 0;
  169. u = sourceGetLump(vfp->msource, vf->block, 0, 1);
  170. if(u == nil)
  171. goto Err;
  172. if(!mbUnpack(&mb, u->data, u->asize))
  173. goto Err;
  174. if(!mbSearch(&mb, vf->dir.elem, &i, &me) || me.p == nil)
  175. goto Err;
  176. nme = me;
  177. n = vdSize(&vf->dir);
  178. //print("old size %d new size %d\n", me.size, n);
  179. if(n <= nme.size) {
  180. nme.size = n;
  181. } else {
  182. /* try expand entry? */
  183. p = mbAlloc(&mb, n);
  184. //print("alloced %ld\n", p - mb.buf);
  185. if(p == nil) {
  186. assert(0);
  187. /* much more work */
  188. }
  189. nme.p = p;
  190. nme.size = n;
  191. }
  192. mbDelete(&mb, i, &me);
  193. memset(me.p, 0, me.size);
  194. if(!moved) {
  195. vdPack(&vf->dir, &nme);
  196. mbInsert(&mb, i, &nme);
  197. }
  198. mbPack(&mb);
  199. lumpDecRef(u, 1);
  200. vf->dirty = 0;
  201. vfMetaUnlock(vf);
  202. return 1;
  203. Err:
  204. lumpDecRef(u, 1);
  205. vfMetaUnlock(vf);
  206. return 0;
  207. }
  208. static VacFile *
  209. vfAlloc(VacFS *fs)
  210. {
  211. VacFile *vf;
  212. vf = vtMemAllocZ(sizeof(VacFile));
  213. vf->lk = vtLockAlloc();
  214. vf->ref = 1;
  215. vf->fs = fs;
  216. return vf;
  217. }
  218. static void
  219. vfFree(VacFile *vf)
  220. {
  221. sourceFree(vf->source);
  222. vtLockFree(vf->lk);
  223. sourceFree(vf->msource);
  224. vdCleanup(&vf->dir);
  225. vtMemFree(vf);
  226. }
  227. /* the file is locked already */
  228. static VacFile *
  229. dirLookup(VacFile *vf, char *elem)
  230. {
  231. int i, j, nb;
  232. MetaBlock mb;
  233. MetaEntry me;
  234. Lump *u;
  235. Source *meta;
  236. VacFile *nvf;
  237. meta = vf->msource;
  238. u = nil;
  239. nb = sourceGetNumBlocks(meta);
  240. for(i=0; i<nb; i++) {
  241. u = sourceGetLump(meta, i, 1, 1);
  242. if(u == nil)
  243. goto Err;
  244. if(!mbUnpack(&mb, u->data, u->asize))
  245. goto Err;
  246. if(!mbSearch(&mb, elem, &j, &me))
  247. goto Err;
  248. if(me.p != nil) {
  249. nvf = vfAlloc(vf->fs);
  250. if(!vdUnpack(&nvf->dir, &me)) {
  251. vfFree(nvf);
  252. goto Err;
  253. }
  254. lumpDecRef(u, 1);
  255. nvf->block = i;
  256. return nvf;
  257. }
  258. lumpDecRef(u, 1);
  259. u = nil;
  260. }
  261. vtSetError("file does not exist");
  262. /* fall through */
  263. Err:
  264. lumpDecRef(u, 1);
  265. return nil;
  266. }
  267. /* point r back at vf */
  268. static void
  269. pointback(Source *r, VacFile *vf)
  270. {
  271. assert(r->vf == nil);
  272. r->vf = vf;
  273. }
  274. VacFile *
  275. vfRoot(VacFS *fs, uchar *score)
  276. {
  277. VtEntry e;
  278. Lump *u, *v;
  279. Source *r, *r0, *r1, *r2;
  280. MetaBlock mb;
  281. MetaEntry me;
  282. VacFile *root, *mr;
  283. root = nil;
  284. mr = nil;
  285. r0 = nil;
  286. r1 = nil;
  287. r2 = nil;
  288. v = nil;
  289. r = nil;
  290. u = cacheGetLump(fs->cache, score, VtDirType, fs->bsize);
  291. if(u == nil)
  292. goto Err;
  293. if(!fs->readOnly) {
  294. v = cacheAllocLump(fs->cache, VtDirType, fs->bsize, 1);
  295. if(v == nil) {
  296. vtUnlock(u->lk);
  297. goto Err;
  298. }
  299. v->gen = u->gen;
  300. v->asize = u->asize;
  301. v->state = LumpActive;
  302. memmove(v->data, u->data, v->asize);
  303. lumpDecRef(u, 1);
  304. u = v;
  305. v = nil;
  306. }
  307. vtUnlock(u->lk);
  308. vtEntryUnpack(&e, u->data, 2);
  309. if(e.flags == 0){ /* just one entry */
  310. r = sourceAlloc(fs->cache, u, 0, 0, fs->readOnly);
  311. if(r == nil)
  312. goto Err;
  313. r0 = sourceOpen(r, 0, fs->readOnly);
  314. if(r0 == nil)
  315. goto Err;
  316. r1 = sourceOpen(r, 1, fs->readOnly);
  317. if(r1 == nil)
  318. goto Err;
  319. r2 = sourceOpen(r, 2, fs->readOnly);
  320. if(r2 == nil)
  321. goto Err;
  322. sourceFree(r);
  323. r = nil;
  324. }else{
  325. r0 = sourceAlloc(fs->cache, u, 0, 0, fs->readOnly);
  326. if(r0 == nil)
  327. goto Err;
  328. r1 = sourceAlloc(fs->cache, u, 0, 1, fs->readOnly);
  329. if(r1 == nil)
  330. goto Err;
  331. r2 = sourceAlloc(fs->cache, u, 0, 2, fs->readOnly);
  332. if(r2 == nil)
  333. goto Err;
  334. }
  335. lumpDecRef(u, 0);
  336. u = sourceGetLump(r2, 0, 1, 0);
  337. if(u == nil)
  338. goto Err;
  339. mr = vfAlloc(fs);
  340. mr->msource = r2;
  341. pointback(r2, mr);
  342. r2 = nil;
  343. root = vfAlloc(fs);
  344. root->up = mr;
  345. root->source = r0;
  346. pointback(r0, root);
  347. r0 = nil;
  348. root->msource = r1;
  349. pointback(r1, root);
  350. r1 = nil;
  351. mr->down = root;
  352. if(!mbUnpack(&mb, u->data, u->asize))
  353. goto Err;
  354. if(!meUnpack(&me, &mb, 0))
  355. goto Err;
  356. if(!vdUnpack(&root->dir, &me))
  357. goto Err;
  358. vfRAccess(root);
  359. lumpDecRef(u, 0);
  360. sourceFree(r2);
  361. return root;
  362. Err:
  363. lumpDecRef(u, 0);
  364. lumpDecRef(v, 0);
  365. if(r0)
  366. sourceFree(r0);
  367. if(r1)
  368. sourceFree(r1);
  369. if(r2)
  370. sourceFree(r2);
  371. if(r)
  372. sourceFree(r);
  373. if(mr)
  374. vfFree(mr);
  375. if(root)
  376. vfFree(root);
  377. return nil;
  378. }
  379. VacFile *
  380. vfWalk(VacFile *vf, char *elem)
  381. {
  382. VacFile *nvf;
  383. vfRAccess(vf);
  384. if(elem[0] == 0) {
  385. vtSetError("illegal path element");
  386. return nil;
  387. }
  388. if(!vfIsDir(vf)) {
  389. vtSetError("not a directory");
  390. return nil;
  391. }
  392. if(strcmp(elem, ".") == 0) {
  393. return vfIncRef(vf);
  394. }
  395. if(strcmp(elem, "..") == 0) {
  396. if(vfIsRoot(vf))
  397. return vfIncRef(vf);
  398. return vfIncRef(vf->up);
  399. }
  400. if(!vfLock(vf))
  401. return nil;
  402. for(nvf = vf->down; nvf; nvf=nvf->next) {
  403. if(strcmp(elem, nvf->dir.elem) == 0 && !nvf->removed) {
  404. nvf->ref++;
  405. goto Exit;
  406. }
  407. }
  408. nvf = dirLookup(vf, elem);
  409. if(nvf == nil)
  410. goto Err;
  411. nvf->source = sourceOpen(vf->source, nvf->dir.entry, vf->fs->readOnly);
  412. if(nvf->source == nil)
  413. goto Err;
  414. pointback(nvf->source, nvf);
  415. if(nvf->dir.mode & ModeDir) {
  416. nvf->msource = sourceOpen(vf->source, nvf->dir.mentry, vf->fs->readOnly);
  417. if(nvf->msource == nil)
  418. goto Err;
  419. pointback(nvf->msource, nvf);
  420. }
  421. /* link in and up parent ref count */
  422. nvf->next = vf->down;
  423. vf->down = nvf;
  424. nvf->up = vf;
  425. vfIncRef(vf);
  426. Exit:
  427. vfUnlock(vf);
  428. return nvf;
  429. Err:
  430. vfUnlock(vf);
  431. if(nvf != nil)
  432. vfFree(nvf);
  433. return nil;
  434. }
  435. VacFile *
  436. vfOpen(VacFS *fs, char *path)
  437. {
  438. VacFile *vf, *nvf;
  439. char *p, elem[VtMaxStringSize];
  440. int n;
  441. vf = fs->root;
  442. vfIncRef(vf);
  443. while(*path != 0) {
  444. for(p = path; *p && *p != '/'; p++)
  445. ;
  446. n = p - path;
  447. if(n > 0) {
  448. if(n > VtMaxStringSize) {
  449. vtSetError("path element too long");
  450. goto Err;
  451. }
  452. memmove(elem, path, n);
  453. elem[n] = 0;
  454. nvf = vfWalk(vf, elem);
  455. if(nvf == nil)
  456. goto Err;
  457. vfDecRef(vf);
  458. vf = nvf;
  459. }
  460. if(*p == '/')
  461. p++;
  462. path = p;
  463. }
  464. return vf;
  465. Err:
  466. vfDecRef(vf);
  467. return nil;
  468. }
  469. VacFile *
  470. vfCreate(VacFile *vf, char *elem, ulong mode, char *user)
  471. {
  472. VacFile *nvf;
  473. VacDir *dir;
  474. int n, i;
  475. uchar *p;
  476. Source *pr, *r, *mr;
  477. int isdir;
  478. MetaBlock mb;
  479. MetaEntry me;
  480. Lump *u;
  481. if(!vfLock(vf))
  482. return nil;
  483. r = nil;
  484. mr = nil;
  485. u = nil;
  486. for(nvf = vf->down; nvf; nvf=nvf->next) {
  487. if(strcmp(elem, nvf->dir.elem) == 0 && !nvf->removed) {
  488. nvf = nil;
  489. vtSetError(EExists);
  490. goto Err;
  491. }
  492. }
  493. nvf = dirLookup(vf, elem);
  494. if(nvf != nil) {
  495. vtSetError(EExists);
  496. goto Err;
  497. }
  498. nvf = vfAlloc(vf->fs);
  499. isdir = mode & ModeDir;
  500. pr = vf->source;
  501. r = sourceCreate(pr, pr->psize, pr->dsize, isdir, 0);
  502. if(r == nil)
  503. goto Err;
  504. if(isdir) {
  505. mr = sourceCreate(pr, pr->psize, pr->dsize, 0, r->block*pr->epb + r->entry);
  506. if(mr == nil)
  507. goto Err;
  508. }
  509. dir = &nvf->dir;
  510. dir->elem = vtStrDup(elem);
  511. dir->entry = r->block*pr->epb + r->entry;
  512. dir->gen = r->gen;
  513. if(isdir) {
  514. dir->mentry = mr->block*pr->epb + mr->entry;
  515. dir->mgen = mr->gen;
  516. }
  517. dir->size = 0;
  518. dir->qid = vf->fs->qid++;
  519. dir->uid = vtStrDup(user);
  520. dir->gid = vtStrDup(vf->dir.gid);
  521. dir->mid = vtStrDup(user);
  522. dir->mtime = time(0L);
  523. dir->mcount = 0;
  524. dir->ctime = dir->mtime;
  525. dir->atime = dir->mtime;
  526. dir->mode = mode;
  527. n = vdSize(dir);
  528. nvf->block = msAlloc(vf->msource, 0, n);
  529. if(nvf->block == NilBlock)
  530. goto Err;
  531. u = sourceGetLump(vf->msource, nvf->block, 0, 1);
  532. if(u == nil)
  533. goto Err;
  534. if(!mbUnpack(&mb, u->data, u->asize))
  535. goto Err;
  536. p = mbAlloc(&mb, n);
  537. if(p == nil)
  538. goto Err;
  539. if(!mbSearch(&mb, elem, &i, &me))
  540. goto Err;
  541. assert(me.p == nil);
  542. me.p = p;
  543. me.size = n;
  544. vdPack(dir, &me);
  545. mbInsert(&mb, i, &me);
  546. mbPack(&mb);
  547. lumpDecRef(u, 1);
  548. nvf->source = r;
  549. pointback(r, nvf);
  550. nvf->msource = mr;
  551. pointback(mr, vf);
  552. /* link in and up parent ref count */
  553. nvf->next = vf->down;
  554. vf->down = nvf;
  555. nvf->up = vf;
  556. vfIncRef(vf);
  557. vfWAccess(vf, user);
  558. vfUnlock(vf);
  559. return nvf;
  560. Err:
  561. lumpDecRef(u, 1);
  562. if(r)
  563. sourceRemove(r);
  564. if(mr)
  565. sourceRemove(mr);
  566. if(nvf)
  567. vfFree(nvf);
  568. vfUnlock(vf);
  569. return 0;
  570. }
  571. int
  572. vfRead(VacFile *vf, void *buf, int cnt, vlong offset)
  573. {
  574. Source *s;
  575. uvlong size;
  576. ulong bn;
  577. int off, dsize, n, nn;
  578. Lump *u;
  579. uchar *b;
  580. if(0)fprint(2, "vfRead: %s %d, %lld\n", vf->dir.elem, cnt, offset);
  581. if(!vfRLock(vf))
  582. return -1;
  583. s = vf->source;
  584. dsize = s->dsize;
  585. size = sourceGetSize(s);
  586. if(offset < 0) {
  587. vtSetError(EBadOffset);
  588. goto Err;
  589. }
  590. vfRAccess(vf);
  591. if(offset >= size)
  592. offset = size;
  593. if(cnt > size-offset)
  594. cnt = size-offset;
  595. bn = offset/dsize;
  596. off = offset%dsize;
  597. b = buf;
  598. while(cnt > 0) {
  599. u = sourceGetLump(s, bn, 1, 0);
  600. if(u == nil)
  601. goto Err;
  602. if(u->asize <= off) {
  603. lumpDecRef(u, 0);
  604. goto Err;
  605. }
  606. n = cnt;
  607. if(n > dsize-off)
  608. n = dsize-off;
  609. nn = u->asize-off;
  610. if(nn > n)
  611. nn = n;
  612. memmove(b, u->data+off, nn);
  613. memset(b+nn, 0, n-nn);
  614. off = 0;
  615. bn++;
  616. cnt -= n;
  617. b += n;
  618. lumpDecRef(u, 0);
  619. }
  620. vfRUnlock(vf);
  621. return b-(uchar*)buf;
  622. Err:
  623. vfRUnlock(vf);
  624. return -1;
  625. }
  626. int
  627. vfWrite(VacFile *vf, void *buf, int cnt, vlong offset, char *user)
  628. {
  629. Source *s;
  630. ulong bn;
  631. int off, dsize, n;
  632. Lump *u;
  633. uchar *b;
  634. USED(user);
  635. if(!vfLock(vf))
  636. return -1;
  637. if(vf->fs->readOnly) {
  638. vtSetError(EReadOnly);
  639. goto Err;
  640. }
  641. if(vf->dir.mode & ModeDir) {
  642. vtSetError(ENotFile);
  643. goto Err;
  644. }
  645. if(0)fprint(2, "vfWrite: %s %d, %lld\n", vf->dir.elem, cnt, offset);
  646. s = vf->source;
  647. dsize = s->dsize;
  648. if(offset < 0) {
  649. vtSetError(EBadOffset);
  650. goto Err;
  651. }
  652. vfWAccess(vf, user);
  653. bn = offset/dsize;
  654. off = offset%dsize;
  655. b = buf;
  656. while(cnt > 0) {
  657. n = cnt;
  658. if(n > dsize-off)
  659. n = dsize-off;
  660. if(!sourceSetDepth(s, offset+n))
  661. goto Err;
  662. u = sourceGetLump(s, bn, 0, 0);
  663. if(u == nil)
  664. goto Err;
  665. if(u->asize < dsize) {
  666. vtSetError("runt block");
  667. lumpDecRef(u, 0);
  668. goto Err;
  669. }
  670. memmove(u->data+off, b, n);
  671. off = 0;
  672. cnt -= n;
  673. b += n;
  674. offset += n;
  675. bn++;
  676. lumpDecRef(u, 0);
  677. if(!sourceSetSize(s, offset))
  678. goto Err;
  679. }
  680. vfLock(vf);
  681. return b-(uchar*)buf;
  682. Err:
  683. vfLock(vf);
  684. return -1;
  685. }
  686. int
  687. vfGetDir(VacFile *vf, VacDir *dir)
  688. {
  689. if(!vfRLock(vf))
  690. return 0;
  691. vfMetaLock(vf);
  692. vdCopy(dir, &vf->dir);
  693. vfMetaUnlock(vf);
  694. if(!vfIsDir(vf))
  695. dir->size = sourceGetSize(vf->source);
  696. vfRUnlock(vf);
  697. return 1;
  698. }
  699. uvlong
  700. vfGetId(VacFile *vf)
  701. {
  702. /* immutable */
  703. return vf->dir.qid;
  704. }
  705. ulong
  706. vfGetMcount(VacFile *vf)
  707. {
  708. ulong mcount;
  709. vfMetaLock(vf);
  710. mcount = vf->dir.mcount;
  711. vfMetaUnlock(vf);
  712. return mcount;
  713. }
  714. int
  715. vfIsDir(VacFile *vf)
  716. {
  717. /* immutable */
  718. return (vf->dir.mode & ModeDir) != 0;
  719. }
  720. int
  721. vfIsRoot(VacFile *vf)
  722. {
  723. return vf == vf->fs->root;
  724. }
  725. int
  726. vfGetSize(VacFile *vf, uvlong *size)
  727. {
  728. if(!vfRLock(vf))
  729. return 0;
  730. *size = sourceGetSize(vf->source);
  731. vfRUnlock(vf);
  732. return 1;
  733. }
  734. static int
  735. vfMetaRemove(VacFile *vf, char *user)
  736. {
  737. Lump *u;
  738. MetaBlock mb;
  739. MetaEntry me;
  740. int i;
  741. VacFile *vfp;
  742. vfp = vf->up;
  743. vfWAccess(vfp, user);
  744. vfMetaLock(vf);
  745. u = sourceGetLump(vfp->msource, vf->block, 0, 1);
  746. if(u == nil)
  747. goto Err;
  748. if(!mbUnpack(&mb, u->data, u->asize))
  749. goto Err;
  750. if(!mbSearch(&mb, vf->dir.elem, &i, &me) || me.p == nil)
  751. goto Err;
  752. print("deleting %d entry\n", i);
  753. mbDelete(&mb, i, &me);
  754. memset(me.p, 0, me.size);
  755. mbPack(&mb);
  756. lumpDecRef(u, 1);
  757. vf->removed = 1;
  758. vf->block = NilBlock;
  759. vfMetaUnlock(vf);
  760. return 1;
  761. Err:
  762. lumpDecRef(u, 1);
  763. vfMetaUnlock(vf);
  764. return 0;
  765. }
  766. static int
  767. vfCheckEmpty(VacFile *vf)
  768. {
  769. int i, n;
  770. Lump *u;
  771. MetaBlock mb;
  772. Source *r;
  773. r = vf->msource;
  774. n = sourceGetNumBlocks(r);
  775. for(i=0; i<n; i++) {
  776. u = sourceGetLump(r, i, 1, 1);
  777. if(u == nil)
  778. goto Err;
  779. if(!mbUnpack(&mb, u->data, u->asize))
  780. goto Err;
  781. if(mb.nindex > 0) {
  782. vtSetError(ENotEmpty);
  783. goto Err;
  784. }
  785. lumpDecRef(u, 1);
  786. }
  787. return 1;
  788. Err:
  789. lumpDecRef(u, 1);
  790. return 0;
  791. }
  792. int
  793. vfRemove(VacFile *vf, char *user)
  794. {
  795. /* can not remove the root */
  796. if(vfIsRoot(vf)) {
  797. vtSetError(ERoot);
  798. return 0;
  799. }
  800. if(!vfLock(vf))
  801. return 0;
  802. if(vfIsDir(vf) && !vfCheckEmpty(vf))
  803. goto Err;
  804. assert(vf->down == nil);
  805. sourceRemove(vf->source);
  806. vf->source = nil;
  807. if(vf->msource) {
  808. sourceRemove(vf->msource);
  809. vf->msource = nil;
  810. }
  811. vfUnlock(vf);
  812. if(!vfMetaRemove(vf, user))
  813. return 0;
  814. return 1;
  815. Err:
  816. vfUnlock(vf);
  817. return 0;
  818. }
  819. VacFile *
  820. vfIncRef(VacFile *vf)
  821. {
  822. vfMetaLock(vf);
  823. assert(vf->ref > 0);
  824. vf->ref++;
  825. vfMetaUnlock(vf);
  826. return vf;
  827. }
  828. void
  829. vfDecRef(VacFile *vf)
  830. {
  831. VacFile *p, *q, **qq;
  832. if(vf->up == nil) {
  833. vfFree(vf);
  834. return;
  835. }
  836. vfMetaLock(vf);
  837. vf->ref--;
  838. if(vf->ref > 0) {
  839. vfMetaUnlock(vf);
  840. return;
  841. }
  842. assert(vf->ref == 0);
  843. assert(vf->down == nil);
  844. p = vf->up;
  845. qq = &p->down;
  846. for(q = *qq; q; qq=&q->next,q=*qq)
  847. if(q == vf)
  848. break;
  849. assert(q != nil);
  850. *qq = vf->next;
  851. vfMetaUnlock(vf);
  852. vfFree(vf);
  853. vfDecRef(p);
  854. }
  855. int
  856. vfGetVtEntry(VacFile *vf, VtEntry *e)
  857. {
  858. int res;
  859. if(!vfRLock(vf))
  860. return 0;
  861. res = sourceGetVtEntry(vf->source, e);
  862. vfRUnlock(vf);
  863. return res;
  864. }
  865. int
  866. vfGetBlockScore(VacFile *vf, ulong bn, uchar score[VtScoreSize])
  867. {
  868. Lump *u;
  869. int ret, off;
  870. Source *r;
  871. if(!vfRLock(vf))
  872. return 0;
  873. r = vf->source;
  874. u = sourceWalk(r, bn, 1, &off);
  875. if(u == nil){
  876. vfRUnlock(vf);
  877. return 0;
  878. }
  879. ret = lumpGetScore(u, off, score);
  880. lumpDecRef(u, 0);
  881. vfRUnlock(vf);
  882. return ret;
  883. }
  884. VacFile *
  885. vfGetParent(VacFile *vf)
  886. {
  887. if(vfIsRoot(vf))
  888. return vfIncRef(vf);
  889. return vfIncRef(vf->up);
  890. }
  891. static VacDirEnum *
  892. vdeAlloc(VacFile *vf)
  893. {
  894. VacDirEnum *ds;
  895. if(!(vf->dir.mode & ModeDir)) {
  896. vtSetError(ENotDir);
  897. vfDecRef(vf);
  898. return nil;
  899. }
  900. ds = vtMemAllocZ(sizeof(VacDirEnum));
  901. ds->file = vf;
  902. return ds;
  903. }
  904. VacDirEnum *
  905. vdeOpen(VacFS *fs, char *path)
  906. {
  907. VacFile *vf;
  908. vf = vfOpen(fs, path);
  909. if(vf == nil)
  910. return nil;
  911. return vdeAlloc(vf);
  912. }
  913. VacDirEnum *
  914. vfDirEnum(VacFile *vf)
  915. {
  916. return vdeAlloc(vfIncRef(vf));
  917. }
  918. static int
  919. dirEntrySize(Source *s, ulong elem, ulong gen, uvlong *size)
  920. {
  921. Lump *u;
  922. ulong bn;
  923. VtEntry e;
  924. bn = elem/s->epb;
  925. elem -= bn*s->epb;
  926. u = sourceGetLump(s, bn, 1, 1);
  927. if(u == nil)
  928. goto Err;
  929. if(u->asize < (elem+1)*VtEntrySize) {
  930. vtSetError(ENoDir);
  931. goto Err;
  932. }
  933. vtEntryUnpack(&e, u->data, elem);
  934. if(!(e.flags & VtEntryActive) || e.gen != gen) {
  935. fprint(2, "gen mismatch\n");
  936. vtSetError(ENoDir);
  937. goto Err;
  938. }
  939. *size = e.size;
  940. lumpDecRef(u, 1);
  941. return 1;
  942. Err:
  943. lumpDecRef(u, 1);
  944. return 0;
  945. }
  946. int
  947. vdeRead(VacDirEnum *ds, VacDir *dir, int n)
  948. {
  949. ulong nb;
  950. int i;
  951. Source *meta, *source;
  952. MetaBlock mb;
  953. MetaEntry me;
  954. Lump *u;
  955. vfRAccess(ds->file);
  956. if(!vfRLock(ds->file))
  957. return -1;
  958. i = 0;
  959. u = nil;
  960. source = ds->file->source;
  961. meta = ds->file->msource;
  962. nb = (sourceGetSize(meta) + meta->dsize - 1)/meta->dsize;
  963. if(ds->block >= nb)
  964. goto Exit;
  965. u = sourceGetLump(meta, ds->block, 1, 1);
  966. if(u == nil)
  967. goto Err;
  968. if(!mbUnpack(&mb, u->data, u->asize))
  969. goto Err;
  970. for(i=0; i<n; i++) {
  971. while(ds->index >= mb.nindex) {
  972. lumpDecRef(u, 1);
  973. u = nil;
  974. ds->index = 0;
  975. ds->block++;
  976. if(ds->block >= nb)
  977. goto Exit;
  978. u = sourceGetLump(meta, ds->block, 1, 1);
  979. if(u == nil)
  980. goto Err;
  981. if(!mbUnpack(&mb, u->data, u->asize))
  982. goto Err;
  983. }
  984. if(!meUnpack(&me, &mb, ds->index))
  985. goto Err;
  986. if(dir != nil) {
  987. if(!vdUnpack(&dir[i], &me))
  988. goto Err;
  989. if(!(dir[i].mode & ModeDir))
  990. if(!dirEntrySize(source, dir[i].entry, dir[i].gen, &dir[i].size))
  991. goto Err;
  992. }
  993. ds->index++;
  994. }
  995. Exit:
  996. lumpDecRef(u, 1);
  997. vfRUnlock(ds->file);
  998. return i;
  999. Err:
  1000. lumpDecRef(u, 1);
  1001. vfRUnlock(ds->file);
  1002. n = i;
  1003. for(i=0; i<n ; i++)
  1004. vdCleanup(&dir[i]);
  1005. return -1;
  1006. }
  1007. void
  1008. vdeFree(VacDirEnum *ds)
  1009. {
  1010. if(ds == nil)
  1011. return;
  1012. vfDecRef(ds->file);
  1013. vtMemFree(ds);
  1014. }
  1015. static ulong
  1016. msAlloc(Source *ms, ulong start, int n)
  1017. {
  1018. ulong nb, i;
  1019. Lump *u;
  1020. MetaBlock mb;
  1021. nb = sourceGetNumBlocks(ms);
  1022. u = nil;
  1023. if(start > nb)
  1024. start = nb;
  1025. for(i=start; i<nb; i++) {
  1026. u = sourceGetLump(ms, i, 1, 1);
  1027. if(u == nil)
  1028. goto Err;
  1029. if(!mbUnpack(&mb, u->data, ms->dsize))
  1030. goto Err;
  1031. if(mb.maxsize - mb.size + mb.free >= n && mb.nindex < mb.maxindex)
  1032. break;
  1033. lumpDecRef(u, 1);
  1034. u = nil;
  1035. }
  1036. /* add block to meta file */
  1037. if(i == nb) {
  1038. if(!sourceSetDepth(ms, (i+1)*ms->dsize))
  1039. goto Err;
  1040. u = sourceGetLump(ms, i, 0, 1);
  1041. if(u == nil)
  1042. goto Err;
  1043. sourceSetSize(ms, (nb+1)*ms->dsize);
  1044. mbInit(&mb, u->data, u->asize);
  1045. mbPack(&mb);
  1046. }
  1047. lumpDecRef(u, 1);
  1048. return i;
  1049. Err:
  1050. lumpDecRef(u, 1);
  1051. return NilBlock;
  1052. }
  1053. VacFS *
  1054. vacfs(VacFile *vf)
  1055. {
  1056. if (vf == nil)
  1057. return nil;
  1058. return vf->fs;
  1059. }
  1060. /*
  1061. * path may be nil; it's the right-hand part of the path so far.
  1062. * result is malloced, path must be malloced or nil.
  1063. */
  1064. char *
  1065. vfName(VacFile *vf, char *path)
  1066. {
  1067. char *nname, *rname, *elem;
  1068. if (vf == nil || vf == vf->up) { /* at the root? */
  1069. if (path == nil)
  1070. return strdup("/");
  1071. return path;
  1072. }
  1073. elem = vf->dir.elem;
  1074. if (elem != nil && path != nil)
  1075. rname = smprint("%s/%s", elem, path);
  1076. else if (elem != nil)
  1077. rname = strdup(elem);
  1078. else
  1079. return vfName(vf->up, path);
  1080. nname = vfName(vf->up, rname);
  1081. if (nname != rname)
  1082. free(rname);
  1083. return nname;
  1084. }