file.c 19 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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. #ifdef notdef
  866. int
  867. vfGetBlockScore(VacFile *vf, ulong bn, uchar score[VtScoreSize])
  868. {
  869. Lump *u;
  870. int ret, off;
  871. Source *r;
  872. if(!vfRLock(vf))
  873. return 0;
  874. r = vf->source;
  875. u = sourceWalk(r, bn, 1, &off);
  876. if(u == nil){
  877. vfRUnlock(vf);
  878. return 0;
  879. }
  880. ret = lumpGetScore(u, off, score);
  881. lumpDecRef(u, 0);
  882. vfRUnlock(vf);
  883. return ret;
  884. }
  885. #endif
  886. VacFile *
  887. vfGetParent(VacFile *vf)
  888. {
  889. if(vfIsRoot(vf))
  890. return vfIncRef(vf);
  891. return vfIncRef(vf->up);
  892. }
  893. static VacDirEnum *
  894. vdeAlloc(VacFile *vf)
  895. {
  896. VacDirEnum *ds;
  897. if(!(vf->dir.mode & ModeDir)) {
  898. vtSetError(ENotDir);
  899. vfDecRef(vf);
  900. return nil;
  901. }
  902. ds = vtMemAllocZ(sizeof(VacDirEnum));
  903. ds->file = vf;
  904. return ds;
  905. }
  906. VacDirEnum *
  907. vdeOpen(VacFS *fs, char *path)
  908. {
  909. VacFile *vf;
  910. vf = vfOpen(fs, path);
  911. if(vf == nil)
  912. return nil;
  913. return vdeAlloc(vf);
  914. }
  915. VacDirEnum *
  916. vfDirEnum(VacFile *vf)
  917. {
  918. return vdeAlloc(vfIncRef(vf));
  919. }
  920. static int
  921. dirEntrySize(Source *s, ulong elem, ulong gen, uvlong *size)
  922. {
  923. Lump *u;
  924. ulong bn;
  925. VtEntry e;
  926. bn = elem/s->epb;
  927. elem -= bn*s->epb;
  928. u = sourceGetLump(s, bn, 1, 1);
  929. if(u == nil)
  930. goto Err;
  931. if(u->asize < (elem+1)*VtEntrySize) {
  932. vtSetError(ENoDir);
  933. goto Err;
  934. }
  935. vtEntryUnpack(&e, u->data, elem);
  936. if(!(e.flags & VtEntryActive) || e.gen != gen) {
  937. fprint(2, "gen mismatch\n");
  938. vtSetError(ENoDir);
  939. goto Err;
  940. }
  941. *size = e.size;
  942. lumpDecRef(u, 1);
  943. return 1;
  944. Err:
  945. lumpDecRef(u, 1);
  946. return 0;
  947. }
  948. int
  949. vdeRead(VacDirEnum *ds, VacDir *dir, int n)
  950. {
  951. ulong nb;
  952. int i;
  953. Source *meta, *source;
  954. MetaBlock mb;
  955. MetaEntry me;
  956. Lump *u;
  957. vfRAccess(ds->file);
  958. if(!vfRLock(ds->file))
  959. return -1;
  960. i = 0;
  961. u = nil;
  962. source = ds->file->source;
  963. meta = ds->file->msource;
  964. nb = (sourceGetSize(meta) + meta->dsize - 1)/meta->dsize;
  965. if(ds->block >= nb)
  966. goto Exit;
  967. u = sourceGetLump(meta, ds->block, 1, 1);
  968. if(u == nil)
  969. goto Err;
  970. if(!mbUnpack(&mb, u->data, u->asize))
  971. goto Err;
  972. for(i=0; i<n; i++) {
  973. while(ds->index >= mb.nindex) {
  974. lumpDecRef(u, 1);
  975. u = nil;
  976. ds->index = 0;
  977. ds->block++;
  978. if(ds->block >= nb)
  979. goto Exit;
  980. u = sourceGetLump(meta, ds->block, 1, 1);
  981. if(u == nil)
  982. goto Err;
  983. if(!mbUnpack(&mb, u->data, u->asize))
  984. goto Err;
  985. }
  986. if(!meUnpack(&me, &mb, ds->index))
  987. goto Err;
  988. if(dir != nil) {
  989. if(!vdUnpack(&dir[i], &me))
  990. goto Err;
  991. if(!(dir[i].mode & ModeDir))
  992. if(!dirEntrySize(source, dir[i].entry, dir[i].gen, &dir[i].size))
  993. goto Err;
  994. }
  995. ds->index++;
  996. }
  997. Exit:
  998. lumpDecRef(u, 1);
  999. vfRUnlock(ds->file);
  1000. return i;
  1001. Err:
  1002. lumpDecRef(u, 1);
  1003. vfRUnlock(ds->file);
  1004. n = i;
  1005. for(i=0; i<n ; i++)
  1006. vdCleanup(&dir[i]);
  1007. return -1;
  1008. }
  1009. void
  1010. vdeFree(VacDirEnum *ds)
  1011. {
  1012. if(ds == nil)
  1013. return;
  1014. vfDecRef(ds->file);
  1015. vtMemFree(ds);
  1016. }
  1017. static ulong
  1018. msAlloc(Source *ms, ulong start, int n)
  1019. {
  1020. ulong nb, i;
  1021. Lump *u;
  1022. MetaBlock mb;
  1023. nb = sourceGetNumBlocks(ms);
  1024. u = nil;
  1025. if(start > nb)
  1026. start = nb;
  1027. for(i=start; i<nb; i++) {
  1028. u = sourceGetLump(ms, i, 1, 1);
  1029. if(u == nil)
  1030. goto Err;
  1031. if(!mbUnpack(&mb, u->data, ms->dsize))
  1032. goto Err;
  1033. if(mb.maxsize - mb.size + mb.free >= n && mb.nindex < mb.maxindex)
  1034. break;
  1035. lumpDecRef(u, 1);
  1036. u = nil;
  1037. }
  1038. /* add block to meta file */
  1039. if(i == nb) {
  1040. if(!sourceSetDepth(ms, (i+1)*ms->dsize))
  1041. goto Err;
  1042. u = sourceGetLump(ms, i, 0, 1);
  1043. if(u == nil)
  1044. goto Err;
  1045. sourceSetSize(ms, (nb+1)*ms->dsize);
  1046. mbInit(&mb, u->data, u->asize);
  1047. mbPack(&mb);
  1048. }
  1049. lumpDecRef(u, 1);
  1050. return i;
  1051. Err:
  1052. lumpDecRef(u, 1);
  1053. return NilBlock;
  1054. }
  1055. VacFS *
  1056. vacfs(VacFile *vf)
  1057. {
  1058. if (vf == nil)
  1059. return nil;
  1060. return vf->fs;
  1061. }
  1062. /*
  1063. * path may be nil; it's the right-hand part of the path so far.
  1064. * result is malloced, path must be malloced or nil.
  1065. */
  1066. char *
  1067. vfName(VacFile *vf, char *path)
  1068. {
  1069. char *nname, *rname, *elem;
  1070. if (vf == nil || vf == vf->up) { /* at the root? */
  1071. if (path == nil)
  1072. return strdup("/");
  1073. return path;
  1074. }
  1075. elem = vf->dir.elem;
  1076. if (elem != nil && path != nil)
  1077. rname = smprint("%s/%s", elem, path);
  1078. else if (elem != nil)
  1079. rname = strdup(elem);
  1080. else
  1081. return vfName(vf->up, path);
  1082. nname = vfName(vf->up, rname);
  1083. if (nname != rname)
  1084. free(rname);
  1085. return nname;
  1086. }