file.c 19 KB

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