file.c 19 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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. vfRUnlock(vf);
  862. return 0;
  863. }
  864. ret = lumpGetScore(u, off, score);
  865. lumpDecRef(u, 0);
  866. vfRUnlock(vf);
  867. return ret;
  868. }
  869. VacFile *
  870. vfGetParent(VacFile *vf)
  871. {
  872. if(vfIsRoot(vf))
  873. return vfIncRef(vf);
  874. return vfIncRef(vf->up);
  875. }
  876. static VacDirEnum *
  877. vdeAlloc(VacFile *vf)
  878. {
  879. VacDirEnum *ds;
  880. if(!(vf->dir.mode & ModeDir)) {
  881. vtSetError(ENotDir);
  882. vfDecRef(vf);
  883. return nil;
  884. }
  885. ds = vtMemAllocZ(sizeof(VacDirEnum));
  886. ds->file = vf;
  887. return ds;
  888. }
  889. VacDirEnum *
  890. vdeOpen(VacFS *fs, char *path)
  891. {
  892. VacFile *vf;
  893. vf = vfOpen(fs, path);
  894. if(vf == nil)
  895. return nil;
  896. return vdeAlloc(vf);
  897. }
  898. VacDirEnum *
  899. vfDirEnum(VacFile *vf)
  900. {
  901. return vdeAlloc(vfIncRef(vf));
  902. }
  903. static int
  904. dirEntrySize(Source *s, ulong elem, ulong gen, uvlong *size)
  905. {
  906. Lump *u;
  907. ulong bn;
  908. VtEntry e;
  909. bn = elem/s->epb;
  910. elem -= bn*s->epb;
  911. u = sourceGetLump(s, bn, 1, 1);
  912. if(u == nil)
  913. goto Err;
  914. if(u->asize < (elem+1)*VtEntrySize) {
  915. vtSetError(ENoDir);
  916. goto Err;
  917. }
  918. vtEntryUnpack(&e, u->data, elem);
  919. if(!(e.flags & VtEntryActive) || e.gen != gen) {
  920. fprint(2, "gen mismatch\n");
  921. vtSetError(ENoDir);
  922. goto Err;
  923. }
  924. *size = e.size;
  925. lumpDecRef(u, 1);
  926. return 1;
  927. Err:
  928. lumpDecRef(u, 1);
  929. return 0;
  930. }
  931. int
  932. vdeRead(VacDirEnum *ds, VacDir *dir, int n)
  933. {
  934. ulong nb;
  935. int i;
  936. Source *meta, *source;
  937. MetaBlock mb;
  938. MetaEntry me;
  939. Lump *u;
  940. vfRAccess(ds->file);
  941. if(!vfRLock(ds->file))
  942. return -1;
  943. i = 0;
  944. u = nil;
  945. source = ds->file->source;
  946. meta = ds->file->msource;
  947. nb = (sourceGetSize(meta) + meta->dsize - 1)/meta->dsize;
  948. if(ds->block >= nb)
  949. goto Exit;
  950. u = sourceGetLump(meta, ds->block, 1, 1);
  951. if(u == nil)
  952. goto Err;
  953. if(!mbUnpack(&mb, u->data, u->asize))
  954. goto Err;
  955. for(i=0; i<n; i++) {
  956. while(ds->index >= mb.nindex) {
  957. lumpDecRef(u, 1);
  958. u = nil;
  959. ds->index = 0;
  960. ds->block++;
  961. if(ds->block >= nb)
  962. goto Exit;
  963. u = sourceGetLump(meta, ds->block, 1, 1);
  964. if(u == nil)
  965. goto Err;
  966. if(!mbUnpack(&mb, u->data, u->asize))
  967. goto Err;
  968. }
  969. if(!meUnpack(&me, &mb, ds->index))
  970. goto Err;
  971. if(dir != nil) {
  972. if(!vdUnpack(&dir[i], &me))
  973. goto Err;
  974. if(!(dir[i].mode & ModeDir))
  975. if(!dirEntrySize(source, dir[i].entry, dir[i].gen, &dir[i].size))
  976. goto Err;
  977. }
  978. ds->index++;
  979. }
  980. Exit:
  981. lumpDecRef(u, 1);
  982. vfRUnlock(ds->file);
  983. return i;
  984. Err:
  985. lumpDecRef(u, 1);
  986. vfRUnlock(ds->file);
  987. n = i;
  988. for(i=0; i<n ; i++)
  989. vdCleanup(&dir[i]);
  990. return -1;
  991. }
  992. void
  993. vdeFree(VacDirEnum *ds)
  994. {
  995. if(ds == nil)
  996. return;
  997. vfDecRef(ds->file);
  998. vtMemFree(ds);
  999. }
  1000. static ulong
  1001. msAlloc(Source *ms, ulong start, int n)
  1002. {
  1003. ulong nb, i;
  1004. Lump *u;
  1005. MetaBlock mb;
  1006. nb = sourceGetNumBlocks(ms);
  1007. u = nil;
  1008. if(start > nb)
  1009. start = nb;
  1010. for(i=start; i<nb; i++) {
  1011. u = sourceGetLump(ms, i, 1, 1);
  1012. if(u == nil)
  1013. goto Err;
  1014. if(!mbUnpack(&mb, u->data, ms->dsize))
  1015. goto Err;
  1016. if(mb.maxsize - mb.size + mb.free >= n && mb.nindex < mb.maxindex)
  1017. break;
  1018. lumpDecRef(u, 1);
  1019. u = nil;
  1020. }
  1021. /* add block to meta file */
  1022. if(i == nb) {
  1023. if(!sourceSetDepth(ms, (i+1)*ms->dsize))
  1024. goto Err;
  1025. u = sourceGetLump(ms, i, 0, 1);
  1026. if(u == nil)
  1027. goto Err;
  1028. sourceSetSize(ms, (nb+1)*ms->dsize);
  1029. mbInit(&mb, u->data, u->asize);
  1030. mbPack(&mb);
  1031. }
  1032. lumpDecRef(u, 1);
  1033. return i;
  1034. Err:
  1035. lumpDecRef(u, 1);
  1036. return NilBlock;
  1037. }