file.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. #include "error.h"
  5. /*
  6. * locking order is upwards. A thread can hold the lock for a File
  7. * and then acquire the lock of its parent
  8. */
  9. struct File {
  10. Fs *fs; /* immutable */
  11. /* meta data for file: protected by the lk in the parent */
  12. int ref; /* holds this data structure up */
  13. int partial; /* file was never really open */
  14. int removed; /* file has been removed */
  15. int dirty; /* dir is dirty with respect to meta data in block */
  16. u32int boff; /* block offset within msource for this file's meta data */
  17. DirEntry dir; /* meta data for this file */
  18. File *up; /* parent file */
  19. File *next; /* sibling */
  20. /* data for file */
  21. VtLock *lk; /* lock for the following */
  22. Source *source;
  23. Source *msource; /* for directories: meta data for children */
  24. File *down; /* children */
  25. int mode;
  26. };
  27. static int fileMetaFlush2(File*, char*);
  28. static u32int fileMetaAlloc(File*, DirEntry*, u32int);
  29. static int fileRLock(File*);
  30. static void fileRUnlock(File*);
  31. static int fileLock(File*);
  32. static void fileUnlock(File*);
  33. static void fileMetaLock(File*);
  34. static void fileMetaUnlock(File*);
  35. static void fileRAccess(File*);
  36. static void fileWAccess(File*, char*);
  37. static File *
  38. fileAlloc(Fs *fs)
  39. {
  40. File *f;
  41. f = vtMemAllocZ(sizeof(File));
  42. f->lk = vtLockAlloc();
  43. f->ref = 1;
  44. f->fs = fs;
  45. f->boff = NilBlock;
  46. f->mode = fs->mode;
  47. return f;
  48. }
  49. static void
  50. fileFree(File *f)
  51. {
  52. sourceClose(f->source);
  53. vtLockFree(f->lk);
  54. sourceClose(f->msource);
  55. deCleanup(&f->dir);
  56. memset(f, ~0, sizeof(File));
  57. vtMemFree(f);
  58. }
  59. /*
  60. * the file is locked already
  61. * f->msource is unlocked
  62. */
  63. static File *
  64. dirLookup(File *f, char *elem)
  65. {
  66. int i;
  67. MetaBlock mb;
  68. MetaEntry me;
  69. Block *b;
  70. Source *meta;
  71. File *ff;
  72. u32int bo, nb;
  73. meta = f->msource;
  74. b = nil;
  75. if(!sourceLock(meta, -1))
  76. return nil;
  77. nb = (sourceGetSize(meta)+meta->dsize-1)/meta->dsize;
  78. for(bo=0; bo<nb; bo++){
  79. b = sourceBlock(meta, bo, OReadOnly);
  80. if(b == nil)
  81. goto Err;
  82. if(!mbUnpack(&mb, b->data, meta->dsize))
  83. goto Err;
  84. if(mbSearch(&mb, elem, &i, &me)){
  85. ff = fileAlloc(f->fs);
  86. if(!deUnpack(&ff->dir, &me)){
  87. fileFree(ff);
  88. goto Err;
  89. }
  90. sourceUnlock(meta);
  91. blockPut(b);
  92. ff->boff = bo;
  93. ff->mode = f->mode;
  94. return ff;
  95. }
  96. blockPut(b);
  97. b = nil;
  98. }
  99. vtSetError(ENoFile);
  100. /* fall through */
  101. Err:
  102. sourceUnlock(meta);
  103. blockPut(b);
  104. return nil;
  105. }
  106. File *
  107. fileRoot(Source *r)
  108. {
  109. Block *b;
  110. Source *r0, *r1, *r2;
  111. MetaBlock mb;
  112. MetaEntry me;
  113. File *root, *mr;
  114. Fs *fs;
  115. b = nil;
  116. root = nil;
  117. mr = nil;
  118. r1 = nil;
  119. r2 = nil;
  120. fs = r->fs;
  121. if(!sourceLock(r, -1))
  122. return nil;
  123. r0 = sourceOpen(r, 0, fs->mode);
  124. if(r0 == nil)
  125. goto Err;
  126. r1 = sourceOpen(r, 1, fs->mode);
  127. if(r1 == nil)
  128. goto Err;
  129. r2 = sourceOpen(r, 2, fs->mode);
  130. if(r2 == nil)
  131. goto Err;
  132. mr = fileAlloc(fs);
  133. mr->msource = r2;
  134. r2 = nil;
  135. root = fileAlloc(fs);
  136. root->boff = 0;
  137. root->up = mr;
  138. root->source = r0;
  139. r0 = nil;
  140. root->msource = r1;
  141. r1 = nil;
  142. mr->down = root;
  143. if(!sourceLock(mr->msource, -1))
  144. goto Err;
  145. b = sourceBlock(mr->msource, 0, OReadOnly);
  146. sourceUnlock(mr->msource);
  147. if(b == nil)
  148. goto Err;
  149. if(!mbUnpack(&mb, b->data, mr->msource->dsize))
  150. goto Err;
  151. meUnpack(&me, &mb, 0);
  152. if(!deUnpack(&root->dir, &me))
  153. goto Err;
  154. blockPut(b);
  155. sourceUnlock(r);
  156. fileRAccess(root);
  157. return root;
  158. Err:
  159. blockPut(b);
  160. if(r0)
  161. sourceClose(r0);
  162. if(r1)
  163. sourceClose(r1);
  164. if(r2)
  165. sourceClose(r2);
  166. if(mr)
  167. fileFree(mr);
  168. if(root)
  169. fileFree(root);
  170. sourceUnlock(r);
  171. return nil;
  172. }
  173. static Source *
  174. fileOpenSource(File *f, u32int offset, u32int gen, int dir, uint mode)
  175. {
  176. Source *r;
  177. if(!sourceLock(f->source, mode))
  178. return nil;
  179. r = sourceOpen(f->source, offset, mode);
  180. sourceUnlock(f->source);
  181. if(r == nil)
  182. return nil;
  183. if(r->gen != gen){
  184. vtSetError(ERemoved);
  185. goto Err;
  186. }
  187. if(r->dir != dir && r->mode != -1){
  188. fprint(2, "fileOpenSource: dir mismatch %d %d\n", r->dir, dir);
  189. vtSetError(EBadMeta);
  190. goto Err;
  191. }
  192. return r;
  193. Err:
  194. sourceClose(r);
  195. return nil;
  196. }
  197. File *
  198. _fileWalk(File *f, char *elem, int partial)
  199. {
  200. File *ff;
  201. fileRAccess(f);
  202. if(elem[0] == 0){
  203. vtSetError(EBadPath);
  204. return nil;
  205. }
  206. if(!fileIsDir(f)){
  207. vtSetError(ENotDir);
  208. return nil;
  209. }
  210. if(strcmp(elem, ".") == 0){
  211. return fileIncRef(f);
  212. }
  213. if(strcmp(elem, "..") == 0){
  214. if(fileIsRoot(f))
  215. return fileIncRef(f);
  216. return fileIncRef(f->up);
  217. }
  218. if(!fileLock(f))
  219. return nil;
  220. for(ff = f->down; ff; ff=ff->next){
  221. if(strcmp(elem, ff->dir.elem) == 0 && !ff->removed){
  222. ff->ref++;
  223. goto Exit;
  224. }
  225. }
  226. ff = dirLookup(f, elem);
  227. if(ff == nil)
  228. goto Err;
  229. if(ff->dir.mode & ModeSnapshot)
  230. ff->mode = OReadOnly;
  231. if(partial){
  232. /*
  233. * Do nothing. We're opening this file only so we can clri it.
  234. * Usually the sources can't be opened, hence we won't even bother.
  235. * Be VERY careful with the returned file. If you hand it to a routine
  236. * expecting ff->source and/or ff->msource to be non-nil, we're
  237. * likely to dereference nil. FileClri should be the only routine
  238. * setting partial.
  239. */
  240. ff->partial = 1;
  241. }else if(ff->dir.mode & ModeDir){
  242. ff->source = fileOpenSource(f, ff->dir.entry, ff->dir.gen, 1, ff->mode);
  243. ff->msource = fileOpenSource(f, ff->dir.mentry, ff->dir.mgen, 0, ff->mode);
  244. if(ff->source == nil || ff->msource == nil)
  245. goto Err;
  246. }else{
  247. ff->source = fileOpenSource(f, ff->dir.entry, ff->dir.gen, 0, ff->mode);
  248. if(ff->source == nil)
  249. goto Err;
  250. }
  251. /* link in and up parent ref count */
  252. ff->next = f->down;
  253. f->down = ff;
  254. ff->up = f;
  255. fileIncRef(f);
  256. Exit:
  257. fileUnlock(f);
  258. return ff;
  259. Err:
  260. fileUnlock(f);
  261. if(ff != nil)
  262. fileDecRef(ff);
  263. return nil;
  264. }
  265. File *
  266. fileWalk(File *f, char *elem)
  267. {
  268. return _fileWalk(f, elem, 0);
  269. }
  270. File *
  271. _fileOpen(Fs *fs, char *path, int partial)
  272. {
  273. File *f, *ff;
  274. char *p, elem[VtMaxStringSize];
  275. int n;
  276. f = fs->file;
  277. fileIncRef(f);
  278. while(*path != 0){
  279. for(p = path; *p && *p != '/'; p++)
  280. ;
  281. n = p - path;
  282. if(n > 0){
  283. if(n > VtMaxStringSize){
  284. vtSetError(EBadPath);
  285. goto Err;
  286. }
  287. memmove(elem, path, n);
  288. elem[n] = 0;
  289. ff = _fileWalk(f, elem, partial && *p=='\0');
  290. if(ff == nil)
  291. goto Err;
  292. fileDecRef(f);
  293. f = ff;
  294. }
  295. if(*p == '/')
  296. p++;
  297. path = p;
  298. }
  299. return f;
  300. Err:
  301. fileDecRef(f);
  302. return nil;
  303. }
  304. File*
  305. fileOpen(Fs *fs, char *path)
  306. {
  307. return _fileOpen(fs, path, 0);
  308. }
  309. File *
  310. fileCreate(File *f, char *elem, ulong mode, char *uid)
  311. {
  312. File *ff;
  313. DirEntry *dir;
  314. Source *pr, *r, *mr;
  315. int isdir;
  316. if(!fileLock(f))
  317. return nil;
  318. r = nil;
  319. mr = nil;
  320. for(ff = f->down; ff; ff=ff->next){
  321. if(strcmp(elem, ff->dir.elem) == 0 && !ff->removed){
  322. ff = nil;
  323. vtSetError(EExists);
  324. goto Err1;
  325. }
  326. }
  327. ff = dirLookup(f, elem);
  328. if(ff != nil){
  329. vtSetError(EExists);
  330. goto Err1;
  331. }
  332. pr = f->source;
  333. if(pr->mode != OReadWrite){
  334. vtSetError(EReadOnly);
  335. goto Err1;
  336. }
  337. if(!sourceLock2(f->source, f->msource, -1))
  338. goto Err1;
  339. ff = fileAlloc(f->fs);
  340. isdir = mode & ModeDir;
  341. r = sourceCreate(pr, pr->dsize, isdir, 0);
  342. if(r == nil)
  343. goto Err;
  344. if(isdir){
  345. mr = sourceCreate(pr, pr->dsize, 0, r->offset);
  346. if(mr == nil)
  347. goto Err;
  348. }
  349. dir = &ff->dir;
  350. dir->elem = vtStrDup(elem);
  351. dir->entry = r->offset;
  352. dir->gen = r->gen;
  353. if(isdir){
  354. dir->mentry = mr->offset;
  355. dir->mgen = mr->gen;
  356. }
  357. dir->size = 0;
  358. if(!fsNextQid(f->fs, &dir->qid))
  359. goto Err;
  360. dir->uid = vtStrDup(uid);
  361. dir->gid = vtStrDup(f->dir.gid);
  362. dir->mid = vtStrDup(uid);
  363. dir->mtime = time(0L);
  364. dir->mcount = 0;
  365. dir->ctime = dir->mtime;
  366. dir->atime = dir->mtime;
  367. dir->mode = mode;
  368. ff->boff = fileMetaAlloc(f, dir, 0);
  369. if(ff->boff == NilBlock)
  370. goto Err;
  371. sourceUnlock(f->source);
  372. sourceUnlock(f->msource);
  373. ff->source = r;
  374. ff->msource = mr;
  375. /* link in and up parent ref count */
  376. ff->next = f->down;
  377. f->down = ff;
  378. ff->up = f;
  379. fileIncRef(f);
  380. fileWAccess(f, uid);
  381. fileUnlock(f);
  382. return ff;
  383. Err:
  384. sourceUnlock(f->source);
  385. sourceUnlock(f->msource);
  386. Err1:
  387. if(r)
  388. sourceRemove(r);
  389. if(mr)
  390. sourceRemove(mr);
  391. if(ff)
  392. fileDecRef(ff);
  393. fileUnlock(f);
  394. return 0;
  395. }
  396. int
  397. fileRead(File *f, void *buf, int cnt, vlong offset)
  398. {
  399. Source *s;
  400. uvlong size;
  401. u32int bn;
  402. int off, dsize, n, nn;
  403. Block *b;
  404. uchar *p;
  405. if(0)fprint(2, "fileRead: %s %d, %lld\n", f->dir.elem, cnt, offset);
  406. if(!fileRLock(f))
  407. return -1;
  408. if(offset < 0){
  409. vtSetError(EBadOffset);
  410. goto Err1;
  411. }
  412. fileRAccess(f);
  413. if(!sourceLock(f->source, OReadOnly))
  414. goto Err1;
  415. s = f->source;
  416. dsize = s->dsize;
  417. size = sourceGetSize(s);
  418. if(offset >= size)
  419. offset = size;
  420. if(cnt > size-offset)
  421. cnt = size-offset;
  422. bn = offset/dsize;
  423. off = offset%dsize;
  424. p = buf;
  425. while(cnt > 0){
  426. b = sourceBlock(s, bn, OReadOnly);
  427. if(b == nil)
  428. goto Err;
  429. n = cnt;
  430. if(n > dsize-off)
  431. n = dsize-off;
  432. nn = dsize-off;
  433. if(nn > n)
  434. nn = n;
  435. memmove(p, b->data+off, nn);
  436. memset(p+nn, 0, nn-n);
  437. off = 0;
  438. bn++;
  439. cnt -= n;
  440. p += n;
  441. blockPut(b);
  442. }
  443. sourceUnlock(s);
  444. fileRUnlock(f);
  445. return p-(uchar*)buf;
  446. Err:
  447. sourceUnlock(s);
  448. Err1:
  449. fileRUnlock(f);
  450. return -1;
  451. }
  452. int
  453. fileWrite(File *f, void *buf, int cnt, vlong offset, char *uid)
  454. {
  455. Source *s;
  456. ulong bn;
  457. int off, dsize, n;
  458. Block *b;
  459. uchar *p;
  460. vlong eof;
  461. if(0)fprint(2, "fileWrite: %s %d, %lld\n", f->dir.elem, cnt, offset);
  462. if(!fileLock(f))
  463. return -1;
  464. s = nil;
  465. if(f->dir.mode & ModeDir){
  466. vtSetError(ENotFile);
  467. goto Err;
  468. }
  469. if(f->source->mode != OReadWrite){
  470. vtSetError(EReadOnly);
  471. goto Err;
  472. }
  473. if(offset < 0){
  474. vtSetError(EBadOffset);
  475. goto Err;
  476. }
  477. fileWAccess(f, uid);
  478. if(!sourceLock(f->source, -1))
  479. goto Err;
  480. s = f->source;
  481. dsize = s->dsize;
  482. eof = sourceGetSize(s);
  483. if(f->dir.mode & ModeAppend)
  484. offset = eof;
  485. bn = offset/dsize;
  486. off = offset%dsize;
  487. p = buf;
  488. while(cnt > 0){
  489. n = cnt;
  490. if(n > dsize-off)
  491. n = dsize-off;
  492. b = sourceBlock(s, bn, n<dsize?OReadWrite:OOverWrite);
  493. if(b == nil){
  494. if(offset > eof)
  495. sourceSetSize(s, offset);
  496. goto Err;
  497. }
  498. memmove(b->data+off, p, n);
  499. off = 0;
  500. cnt -= n;
  501. p += n;
  502. offset += n;
  503. bn++;
  504. blockDirty(b);
  505. blockPut(b);
  506. }
  507. if(offset > eof && !sourceSetSize(s, offset))
  508. goto Err;
  509. sourceUnlock(s);
  510. fileUnlock(f);
  511. return p-(uchar*)buf;
  512. Err:
  513. if(s)
  514. sourceUnlock(s);
  515. fileUnlock(f);
  516. return -1;
  517. }
  518. int
  519. fileGetDir(File *f, DirEntry *dir)
  520. {
  521. if(!fileRLock(f))
  522. return 0;
  523. fileMetaLock(f);
  524. deCopy(dir, &f->dir);
  525. fileMetaUnlock(f);
  526. if(!fileIsDir(f)){
  527. if(!sourceLock(f->source, OReadOnly)){
  528. fileRUnlock(f);
  529. return 0;
  530. }
  531. dir->size = sourceGetSize(f->source);
  532. sourceUnlock(f->source);
  533. }
  534. fileRUnlock(f);
  535. return 1;
  536. }
  537. int
  538. fileTruncate(File *f, char *uid)
  539. {
  540. if(fileIsDir(f)){
  541. vtSetError(ENotFile);
  542. return 0;
  543. }
  544. if(!fileLock(f))
  545. return 0;
  546. if(f->source->mode != OReadWrite){
  547. vtSetError(EReadOnly);
  548. fileUnlock(f);
  549. return 0;
  550. }
  551. if(!sourceLock(f->source, -1)){
  552. fileUnlock(f);
  553. return 0;
  554. }
  555. if(!sourceTruncate(f->source)){
  556. sourceUnlock(f->source);
  557. fileUnlock(f);
  558. return 0;
  559. }
  560. sourceUnlock(f->source);
  561. fileUnlock(f);
  562. fileWAccess(f->up, uid);
  563. return 1;
  564. }
  565. int
  566. fileSetDir(File *f, DirEntry *dir, char *uid)
  567. {
  568. File *ff;
  569. char *oelem;
  570. u32int mask;
  571. u64int size;
  572. /* can not set permissions for the root */
  573. if(fileIsRoot(f)){
  574. vtSetError(ERoot);
  575. return 0;
  576. }
  577. if(!fileLock(f))
  578. return 0;
  579. if(f->source->mode != OReadWrite){
  580. vtSetError(EReadOnly);
  581. fileUnlock(f);
  582. return 0;
  583. }
  584. fileMetaLock(f);
  585. /* check new name does not already exist */
  586. if(strcmp(f->dir.elem, dir->elem) != 0){
  587. for(ff = f->up->down; ff; ff=ff->next){
  588. if(strcmp(dir->elem, ff->dir.elem) == 0 && !ff->removed){
  589. vtSetError(EExists);
  590. goto Err;
  591. }
  592. }
  593. ff = dirLookup(f->up, dir->elem);
  594. if(ff != nil){
  595. fileDecRef(ff);
  596. vtSetError(EExists);
  597. goto Err;
  598. }
  599. }
  600. if(!fileIsDir(f)){
  601. if(!sourceLock(f->source, -1))
  602. goto Err;
  603. size = sourceGetSize(f->source);
  604. if(size != dir->size){
  605. if(!sourceSetSize(f->source, dir->size)){
  606. sourceUnlock(f->source);
  607. goto Err;
  608. }
  609. /* commited to changing it now */
  610. }
  611. sourceUnlock(f->source);
  612. }
  613. /* commited to changing it now */
  614. oelem = nil;
  615. if(strcmp(f->dir.elem, dir->elem) != 0){
  616. oelem = f->dir.elem;
  617. f->dir.elem = vtStrDup(dir->elem);
  618. }
  619. if(strcmp(f->dir.uid, dir->uid) != 0){
  620. vtMemFree(f->dir.uid);
  621. f->dir.uid = vtStrDup(dir->uid);
  622. }
  623. if(strcmp(f->dir.gid, dir->gid) != 0){
  624. vtMemFree(f->dir.gid);
  625. f->dir.gid = vtStrDup(dir->gid);
  626. }
  627. f->dir.mtime = dir->mtime;
  628. f->dir.atime = dir->atime;
  629. //fprint(2, "mode %x %x ", f->dir.mode, dir->mode);
  630. mask = ~(ModeDir|ModeSnapshot);
  631. f->dir.mode &= ~mask;
  632. f->dir.mode |= mask & dir->mode;
  633. f->dirty = 1;
  634. //fprint(2, "->%x\n", f->dir.mode);
  635. fileMetaFlush2(f, oelem);
  636. vtMemFree(oelem);
  637. fileMetaUnlock(f);
  638. fileUnlock(f);
  639. fileWAccess(f->up, uid);
  640. return 1;
  641. Err:
  642. fileMetaUnlock(f);
  643. fileUnlock(f);
  644. return 0;
  645. }
  646. int
  647. fileSetQidSpace(File *f, u64int offset, u64int max)
  648. {
  649. int ret;
  650. if(!fileLock(f))
  651. return 0;
  652. fileMetaLock(f);
  653. f->dir.qidSpace = 1;
  654. f->dir.qidOffset = offset;
  655. f->dir.qidMax = max;
  656. ret = fileMetaFlush2(f, nil);
  657. fileMetaUnlock(f);
  658. fileUnlock(f);
  659. return ret;
  660. }
  661. uvlong
  662. fileGetId(File *f)
  663. {
  664. /* immutable */
  665. return f->dir.qid;
  666. }
  667. ulong
  668. fileGetMcount(File *f)
  669. {
  670. ulong mcount;
  671. fileMetaLock(f);
  672. mcount = f->dir.mcount;
  673. fileMetaUnlock(f);
  674. return mcount;
  675. }
  676. ulong
  677. fileGetMode(File *f)
  678. {
  679. ulong mode;
  680. fileMetaLock(f);
  681. mode = f->dir.mode;
  682. fileMetaUnlock(f);
  683. return mode;
  684. }
  685. int
  686. fileIsDir(File *f)
  687. {
  688. /* immutable */
  689. return (f->dir.mode & ModeDir) != 0;
  690. }
  691. int
  692. fileIsRoot(File *f)
  693. {
  694. return f == f->fs->file;
  695. }
  696. int
  697. fileIsRoFs(File *f)
  698. {
  699. return f->fs->mode == OReadOnly;
  700. }
  701. int
  702. fileGetSize(File *f, uvlong *size)
  703. {
  704. if(!fileRLock(f))
  705. return 0;
  706. if(!sourceLock(f->source, OReadOnly)){
  707. fileRUnlock(f);
  708. return 0;
  709. }
  710. *size = sourceGetSize(f->source);
  711. sourceUnlock(f->source);
  712. fileRUnlock(f);
  713. return 1;
  714. }
  715. void
  716. fileMetaFlush(File *f, int rec)
  717. {
  718. File **kids, *p;
  719. int nkids;
  720. int i;
  721. fileMetaLock(f);
  722. fileMetaFlush2(f, nil);
  723. fileMetaUnlock(f);
  724. if(!rec || !fileIsDir(f))
  725. return;
  726. if(!fileLock(f))
  727. return;
  728. nkids = 0;
  729. for(p=f->down; p; p=p->next)
  730. nkids++;
  731. kids = vtMemAlloc(nkids*sizeof(File*));
  732. i = 0;
  733. for(p=f->down; p; p=p->next){
  734. kids[i++] = p;
  735. p->ref++;
  736. }
  737. fileUnlock(f);
  738. for(i=0; i<nkids; i++){
  739. fileMetaFlush(kids[i], 1);
  740. fileDecRef(kids[i]);
  741. }
  742. vtMemFree(kids);
  743. }
  744. /* assumes metaLock is held */
  745. static int
  746. fileMetaFlush2(File *f, char *oelem)
  747. {
  748. File *fp;
  749. Block *b, *bb;
  750. MetaBlock mb;
  751. MetaEntry me, me2;
  752. int i, n;
  753. u32int boff;
  754. if(!f->dirty)
  755. return 1;
  756. if(oelem == nil)
  757. oelem = f->dir.elem;
  758. //print("fileMetaFlush %s->%s\n", oelem, f->dir.elem);
  759. fp = f->up;
  760. if(!sourceLock(fp->msource, -1))
  761. return 0;
  762. b = sourceBlock(fp->msource, f->boff, OReadWrite);
  763. if(b == nil)
  764. goto Err1;
  765. if(!mbUnpack(&mb, b->data, fp->msource->dsize))
  766. goto Err;
  767. if(!mbSearch(&mb, oelem, &i, &me))
  768. goto Err;
  769. n = deSize(&f->dir);
  770. if(0)fprint(2, "old size %d new size %d\n", me.size, n);
  771. if(mbResize(&mb, &me, n)){
  772. /* fits in the block */
  773. mbDelete(&mb, i);
  774. if(strcmp(f->dir.elem, oelem) != 0)
  775. mbSearch(&mb, f->dir.elem, &i, &me2);
  776. dePack(&f->dir, &me);
  777. mbInsert(&mb, i, &me);
  778. mbPack(&mb);
  779. blockDirty(b);
  780. blockPut(b);
  781. sourceUnlock(fp->msource);
  782. f->dirty = 0;
  783. return 1;
  784. }
  785. /*
  786. * moving entry to another block
  787. * it is feasible for the fs to crash leaving two copies
  788. * of the directory entry. This is just too much work to
  789. * fix. Given that entries are only allocated in a block that
  790. * is less than PercentageFull, most modifications of meta data
  791. * will fit within the block. i.e. this code should almost
  792. * never be executed.
  793. */
  794. boff = fileMetaAlloc(fp, &f->dir, f->boff+1);
  795. if(boff == NilBlock){
  796. /* mbResize might have modified block */
  797. mbPack(&mb);
  798. blockDirty(b);
  799. goto Err;
  800. }
  801. fprint(2, "fileMetaFlush moving entry from %ud -> %ud\n", f->boff, boff);
  802. f->boff = boff;
  803. /* make sure deletion goes to disk after new entry */
  804. bb = sourceBlock(fp->msource, f->boff, OReadWrite);
  805. mbDelete(&mb, i);
  806. mbPack(&mb);
  807. blockDependency(b, bb, -1, nil, nil);
  808. blockPut(bb);
  809. blockDirty(b);
  810. blockPut(b);
  811. sourceUnlock(fp->msource);
  812. f->dirty = 0;
  813. return 1;
  814. Err:
  815. blockPut(b);
  816. Err1:
  817. sourceUnlock(fp->msource);
  818. return 0;
  819. }
  820. static int
  821. fileMetaRemove(File *f, char *uid)
  822. {
  823. Block *b;
  824. MetaBlock mb;
  825. MetaEntry me;
  826. int i;
  827. File *up;
  828. up = f->up;
  829. fileWAccess(up, uid);
  830. fileMetaLock(f);
  831. sourceLock(up->msource, OReadWrite);
  832. b = sourceBlock(up->msource, f->boff, OReadWrite);
  833. if(b == nil)
  834. goto Err;
  835. if(!mbUnpack(&mb, b->data, up->msource->dsize))
  836. {
  837. fprint(2, "U\n");
  838. goto Err;
  839. }
  840. if(!mbSearch(&mb, f->dir.elem, &i, &me))
  841. {
  842. fprint(2, "S\n");
  843. goto Err;
  844. }
  845. mbDelete(&mb, i);
  846. mbPack(&mb);
  847. sourceUnlock(up->msource);
  848. blockDirty(b);
  849. blockPut(b);
  850. f->removed = 1;
  851. f->boff = NilBlock;
  852. f->dirty = 0;
  853. fileMetaUnlock(f);
  854. return 1;
  855. Err:
  856. sourceUnlock(up->msource);
  857. blockPut(b);
  858. fileMetaUnlock(f);
  859. return 0;
  860. }
  861. /* assume file is locked, assume f->msource is locked */
  862. static int
  863. fileCheckEmpty(File *f)
  864. {
  865. u32int i, n;
  866. Block *b;
  867. MetaBlock mb;
  868. Source *r;
  869. r = f->msource;
  870. n = (sourceGetSize(r)+r->dsize-1)/r->dsize;
  871. for(i=0; i<n; i++){
  872. b = sourceBlock(r, i, OReadOnly);
  873. if(b == nil)
  874. goto Err;
  875. if(!mbUnpack(&mb, b->data, r->dsize))
  876. goto Err;
  877. if(mb.nindex > 0){
  878. vtSetError(ENotEmpty);
  879. goto Err;
  880. }
  881. blockPut(b);
  882. }
  883. return 1;
  884. Err:
  885. blockPut(b);
  886. return 0;
  887. }
  888. int
  889. fileRemove(File *f, char *uid)
  890. {
  891. File *ff;
  892. /* can not remove the root */
  893. if(fileIsRoot(f)){
  894. vtSetError(ERoot);
  895. return 0;
  896. }
  897. if(!fileLock(f))
  898. return 0;
  899. if(f->source->mode != OReadWrite){
  900. vtSetError(EReadOnly);
  901. goto Err1;
  902. }
  903. if(!sourceLock2(f->source, f->msource, -1))
  904. goto Err1;
  905. if(fileIsDir(f) && !fileCheckEmpty(f))
  906. goto Err;
  907. for(ff=f->down; ff; ff=ff->next)
  908. assert(ff->removed);
  909. sourceRemove(f->source);
  910. f->source = nil;
  911. if(f->msource){
  912. sourceRemove(f->msource);
  913. f->msource = nil;
  914. }
  915. fileUnlock(f);
  916. if(!fileMetaRemove(f, uid))
  917. return 0;
  918. return 1;
  919. Err:
  920. sourceUnlock(f->source);
  921. if(f->msource)
  922. sourceUnlock(f->msource);
  923. Err1:
  924. fileUnlock(f);
  925. return 0;
  926. }
  927. int
  928. fileClri(Fs *fs, char *path, char *uid)
  929. {
  930. int r;
  931. File *f;
  932. f = _fileOpen(fs, path, 1);
  933. if(f == nil)
  934. return 0;
  935. if(f->up->source->mode != OReadWrite){
  936. vtSetError(EReadOnly);
  937. fileDecRef(f);
  938. return 0;
  939. }
  940. r = fileMetaRemove(f, uid);
  941. fileDecRef(f);
  942. return r;
  943. }
  944. File *
  945. fileIncRef(File *vf)
  946. {
  947. fileMetaLock(vf);
  948. assert(vf->ref > 0);
  949. vf->ref++;
  950. fileMetaUnlock(vf);
  951. return vf;
  952. }
  953. int
  954. fileDecRef(File *f)
  955. {
  956. File *p, *q, **qq;
  957. if(f->up == nil){
  958. /* never linked in */
  959. assert(f->ref == 1);
  960. fileFree(f);
  961. return 1;
  962. }
  963. fileMetaLock(f);
  964. f->ref--;
  965. if(f->ref > 0){
  966. fileMetaUnlock(f);
  967. return 0;
  968. }
  969. assert(f->ref == 0);
  970. assert(f->down == nil);
  971. fileMetaFlush2(f, nil);
  972. p = f->up;
  973. qq = &p->down;
  974. for(q = *qq; q; q = *qq){
  975. if(q == f)
  976. break;
  977. qq = &q->next;
  978. }
  979. assert(q != nil);
  980. *qq = f->next;
  981. fileMetaUnlock(f);
  982. fileFree(f);
  983. fileDecRef(p);
  984. return 1;
  985. }
  986. File *
  987. fileGetParent(File *f)
  988. {
  989. if(fileIsRoot(f))
  990. return fileIncRef(f);
  991. return fileIncRef(f->up);
  992. }
  993. DirEntryEnum *
  994. deeOpen(File *f)
  995. {
  996. DirEntryEnum *dee;
  997. File *p;
  998. if(!fileIsDir(f)){
  999. vtSetError(ENotDir);
  1000. fileDecRef(f);
  1001. return nil;
  1002. }
  1003. /* flush out meta data */
  1004. if(!fileLock(f))
  1005. return nil;
  1006. for(p=f->down; p; p=p->next)
  1007. fileMetaFlush2(p, nil);
  1008. fileUnlock(f);
  1009. dee = vtMemAllocZ(sizeof(DirEntryEnum));
  1010. dee->file = fileIncRef(f);
  1011. return dee;
  1012. }
  1013. static int
  1014. dirEntrySize(Source *s, ulong elem, ulong gen, uvlong *size)
  1015. {
  1016. Block *b;
  1017. ulong bn;
  1018. Entry e;
  1019. int epb;
  1020. epb = s->dsize/VtEntrySize;
  1021. bn = elem/epb;
  1022. elem -= bn*epb;
  1023. b = sourceBlock(s, bn, OReadOnly);
  1024. if(b == nil)
  1025. goto Err;
  1026. if(!entryUnpack(&e, b->data, elem))
  1027. goto Err;
  1028. /* hanging entries are returned as zero size */
  1029. if(!(e.flags & VtEntryActive) || e.gen != gen)
  1030. *size = 0;
  1031. else
  1032. *size = e.size;
  1033. blockPut(b);
  1034. return 1;
  1035. Err:
  1036. blockPut(b);
  1037. return 0;
  1038. }
  1039. static int
  1040. deeFill(DirEntryEnum *dee)
  1041. {
  1042. int i, n;
  1043. Source *meta, *source;
  1044. MetaBlock mb;
  1045. MetaEntry me;
  1046. File *f;
  1047. Block *b;
  1048. DirEntry *de;
  1049. /* clean up first */
  1050. for(i=dee->i; i<dee->n; i++)
  1051. deCleanup(dee->buf+i);
  1052. vtMemFree(dee->buf);
  1053. dee->buf = nil;
  1054. dee->i = 0;
  1055. dee->n = 0;
  1056. f = dee->file;
  1057. source = f->source;
  1058. meta = f->msource;
  1059. b = sourceBlock(meta, dee->boff, OReadOnly);
  1060. if(b == nil)
  1061. goto Err;
  1062. if(!mbUnpack(&mb, b->data, meta->dsize))
  1063. goto Err;
  1064. n = mb.nindex;
  1065. dee->buf = vtMemAlloc(n * sizeof(DirEntry));
  1066. for(i=0; i<n; i++){
  1067. de = dee->buf + i;
  1068. meUnpack(&me, &mb, i);
  1069. if(!deUnpack(de, &me))
  1070. goto Err;
  1071. dee->n++;
  1072. if(!(de->mode & ModeDir))
  1073. if(!dirEntrySize(source, de->entry, de->gen, &de->size))
  1074. goto Err;
  1075. }
  1076. dee->boff++;
  1077. blockPut(b);
  1078. return 1;
  1079. Err:
  1080. blockPut(b);
  1081. return 0;
  1082. }
  1083. int
  1084. deeRead(DirEntryEnum *dee, DirEntry *de)
  1085. {
  1086. int ret, didread;
  1087. File *f;
  1088. u32int nb;
  1089. f = dee->file;
  1090. if(!fileRLock(f))
  1091. return -1;
  1092. if(!sourceLock2(f->source, f->msource, OReadOnly)){
  1093. fileRUnlock(f);
  1094. return -1;
  1095. }
  1096. nb = (sourceGetSize(f->msource)+f->msource->dsize-1)/f->msource->dsize;
  1097. didread = 0;
  1098. while(dee->i >= dee->n){
  1099. if(dee->boff >= nb){
  1100. ret = 0;
  1101. goto Return;
  1102. }
  1103. didread = 1;
  1104. if(!deeFill(dee)){
  1105. ret = -1;
  1106. goto Return;
  1107. }
  1108. }
  1109. memmove(de, dee->buf + dee->i, sizeof(DirEntry));
  1110. dee->i++;
  1111. ret = 1;
  1112. Return:
  1113. sourceUnlock(f->source);
  1114. sourceUnlock(f->msource);
  1115. fileRUnlock(f);
  1116. if(didread)
  1117. fileRAccess(f);
  1118. return ret;
  1119. }
  1120. void
  1121. deeClose(DirEntryEnum *dee)
  1122. {
  1123. int i;
  1124. if(dee == nil)
  1125. return;
  1126. for(i=dee->i; i<dee->n; i++)
  1127. deCleanup(dee->buf+i);
  1128. vtMemFree(dee->buf);
  1129. fileDecRef(dee->file);
  1130. vtMemFree(dee);
  1131. }
  1132. /*
  1133. * caller must lock f->source and f->msource
  1134. * caller must NOT lock the source and msource
  1135. * referenced by dir.
  1136. */
  1137. static u32int
  1138. fileMetaAlloc(File *f, DirEntry *dir, u32int start)
  1139. {
  1140. u32int nb, bo;
  1141. Block *b, *bb;
  1142. MetaBlock mb;
  1143. int nn;
  1144. uchar *p;
  1145. int i, n, epb;
  1146. MetaEntry me;
  1147. Source *s, *ms;
  1148. s = f->source;
  1149. ms = f->msource;
  1150. n = deSize(dir);
  1151. nb = (sourceGetSize(ms)+ms->dsize-1)/ms->dsize;
  1152. b = nil;
  1153. if(start > nb)
  1154. start = nb;
  1155. for(bo=start; bo<nb; bo++){
  1156. b = sourceBlock(ms, bo, OReadWrite);
  1157. if(b == nil)
  1158. goto Err;
  1159. if(!mbUnpack(&mb, b->data, ms->dsize))
  1160. goto Err;
  1161. nn = (mb.maxsize*FullPercentage/100) - mb.size + mb.free;
  1162. if(n <= nn && mb.nindex < mb.maxindex)
  1163. break;
  1164. blockPut(b);
  1165. b = nil;
  1166. }
  1167. /* add block to meta file */
  1168. if(b == nil){
  1169. b = sourceBlock(ms, bo, OReadWrite);
  1170. if(b == nil)
  1171. goto Err;
  1172. sourceSetSize(ms, (nb+1)*ms->dsize);
  1173. mbInit(&mb, b->data, ms->dsize, ms->dsize/BytesPerEntry);
  1174. }
  1175. p = mbAlloc(&mb, n);
  1176. if(p == nil){
  1177. /* mbAlloc might have changed block */
  1178. mbPack(&mb);
  1179. blockDirty(b);
  1180. vtSetError(EBadMeta);
  1181. goto Err;
  1182. }
  1183. mbSearch(&mb, dir->elem, &i, &me);
  1184. assert(me.p == nil);
  1185. me.p = p;
  1186. me.size = n;
  1187. dePack(dir, &me);
  1188. mbInsert(&mb, i, &me);
  1189. mbPack(&mb);
  1190. /* meta block depends on super block for qid ... */
  1191. bb = cacheLocal(b->c, PartSuper, 0, OReadOnly);
  1192. blockDependency(b, bb, -1, nil, nil);
  1193. blockPut(bb);
  1194. /* ... and one or two dir entries */
  1195. epb = s->dsize/VtEntrySize;
  1196. bb = sourceBlock(s, dir->entry/epb, OReadOnly);
  1197. blockDependency(b, bb, -1, nil, nil);
  1198. blockPut(bb);
  1199. if(dir->mode & ModeDir){
  1200. bb = sourceBlock(s, dir->mentry/epb, OReadOnly);
  1201. blockDependency(b, bb, -1, nil, nil);
  1202. blockPut(bb);
  1203. }
  1204. blockDirty(b);
  1205. blockPut(b);
  1206. return bo;
  1207. Err:
  1208. blockPut(b);
  1209. return NilBlock;
  1210. }
  1211. static int
  1212. chkSource(File *f)
  1213. {
  1214. if(f->partial)
  1215. return 1;
  1216. if(f->source == nil || (f->dir.mode & ModeDir) && f->msource == nil){
  1217. vtSetError(ERemoved);
  1218. return 0;
  1219. }
  1220. return 1;
  1221. }
  1222. static int
  1223. fileRLock(File *f)
  1224. {
  1225. assert(!vtCanLock(f->fs->elk));
  1226. vtRLock(f->lk);
  1227. if(!chkSource(f)){
  1228. fileRUnlock(f);
  1229. return 0;
  1230. }
  1231. return 1;
  1232. }
  1233. static void
  1234. fileRUnlock(File *f)
  1235. {
  1236. vtRUnlock(f->lk);
  1237. }
  1238. static int
  1239. fileLock(File *f)
  1240. {
  1241. assert(!vtCanLock(f->fs->elk));
  1242. vtLock(f->lk);
  1243. if(!chkSource(f)){
  1244. fileUnlock(f);
  1245. return 0;
  1246. }
  1247. return 1;
  1248. }
  1249. static void
  1250. fileUnlock(File *f)
  1251. {
  1252. vtUnlock(f->lk);
  1253. }
  1254. /*
  1255. * f->source and f->msource must NOT be locked.
  1256. * fileMetaFlush locks the fileMeta and then the source (in fileMetaFlush2).
  1257. * We have to respect that ordering.
  1258. */
  1259. static void
  1260. fileMetaLock(File *f)
  1261. {
  1262. if(f->up == nil)
  1263. fprint(2, "f->elem = %s\n", f->dir.elem);
  1264. assert(f->up != nil);
  1265. assert(!vtCanLock(f->fs->elk));
  1266. vtLock(f->up->lk);
  1267. }
  1268. static void
  1269. fileMetaUnlock(File *f)
  1270. {
  1271. vtUnlock(f->up->lk);
  1272. }
  1273. /*
  1274. * f->source and f->msource must NOT be locked.
  1275. * see fileMetaLock.
  1276. */
  1277. static void
  1278. fileRAccess(File* f)
  1279. {
  1280. if(f->mode == OReadOnly)
  1281. return;
  1282. fileMetaLock(f);
  1283. f->dir.atime = time(0L);
  1284. f->dirty = 1;
  1285. fileMetaUnlock(f);
  1286. }
  1287. /*
  1288. * f->source and f->msource must NOT be locked.
  1289. * see fileMetaLock.
  1290. */
  1291. static void
  1292. fileWAccess(File* f, char *mid)
  1293. {
  1294. if(f->mode == OReadOnly)
  1295. return;
  1296. fileMetaLock(f);
  1297. f->dir.atime = f->dir.mtime = time(0L);
  1298. if(strcmp(f->dir.mid, mid) != 0){
  1299. vtMemFree(f->dir.mid);
  1300. f->dir.mid = vtStrDup(mid);
  1301. }
  1302. f->dir.mcount++;
  1303. f->dirty = 1;
  1304. fileMetaUnlock(f);
  1305. }
  1306. static void
  1307. markCopied(Block *b)
  1308. {
  1309. Block *lb;
  1310. Label l;
  1311. if(globalToLocal(b->score) == NilBlock)
  1312. return;
  1313. if(!(b->l.state & BsCopied)){
  1314. /*
  1315. * We need to record that there are now pointers in
  1316. * b that are not unique to b. We do this by marking
  1317. * b as copied. Since we don't return the label block,
  1318. * the caller can't get the dependencies right. So we have
  1319. * to flush the block ourselves. This is a rare occurrence.
  1320. */
  1321. l = b->l;
  1322. l.state |= BsCopied;
  1323. lb = _blockSetLabel(b, &l);
  1324. WriteAgain:
  1325. while(!blockWrite(lb)){
  1326. fprint(2, "getEntry: could not write label block\n");
  1327. sleep(10*1000);
  1328. }
  1329. while(lb->iostate != BioClean && lb->iostate != BioDirty){
  1330. assert(lb->iostate == BioWriting);
  1331. vtSleep(lb->ioready);
  1332. }
  1333. if(lb->iostate == BioDirty)
  1334. goto WriteAgain;
  1335. blockPut(lb);
  1336. }
  1337. }
  1338. static int
  1339. getEntry(Source *r, Entry *e, int mark)
  1340. {
  1341. Block *b;
  1342. if(r == nil){
  1343. memset(&e, 0, sizeof e);
  1344. return 1;
  1345. }
  1346. b = cacheGlobal(r->fs->cache, r->score, BtDir, r->tag, OReadOnly);
  1347. if(b == nil)
  1348. return 0;
  1349. if(!entryUnpack(e, b->data, r->offset % r->epb)){
  1350. blockPut(b);
  1351. return 0;
  1352. }
  1353. if(mark)
  1354. markCopied(b);
  1355. blockPut(b);
  1356. return 1;
  1357. }
  1358. static int
  1359. setEntry(Source *r, Entry *e)
  1360. {
  1361. Block *b;
  1362. Entry oe;
  1363. b = cacheGlobal(r->fs->cache, r->score, BtDir, r->tag, OReadWrite);
  1364. if(0) fprint(2, "setEntry: b %#ux %d score=%V\n", b->addr, r->offset % r->epb, e->score);
  1365. if(b == nil)
  1366. return 0;
  1367. if(!entryUnpack(&oe, b->data, r->offset % r->epb)){
  1368. blockPut(b);
  1369. return 0;
  1370. }
  1371. e->gen = oe.gen;
  1372. entryPack(e, b->data, r->offset % r->epb);
  1373. /* BUG b should depend on the entry pointer */
  1374. markCopied(b);
  1375. blockDirty(b);
  1376. blockPut(b);
  1377. return 1;
  1378. }
  1379. /* assumes hold elk */
  1380. int
  1381. fileSnapshot(File *dst, File *src, u32int epoch, int doarchive)
  1382. {
  1383. Entry e, ee;
  1384. /* add link to snapshot */
  1385. if(!getEntry(src->source, &e, 1) || !getEntry(src->msource, &ee, 1))
  1386. return 0;
  1387. e.snap = epoch;
  1388. e.archive = doarchive;
  1389. ee.snap = epoch;
  1390. ee.archive = doarchive;
  1391. if(!setEntry(dst->source, &e) || !setEntry(dst->msource, &ee))
  1392. return 0;
  1393. return 1;
  1394. }
  1395. int
  1396. fileGetSources(File *f, Entry *e, Entry *ee, int mark)
  1397. {
  1398. if(!getEntry(f->source, e, mark)
  1399. || !getEntry(f->msource, ee, mark))
  1400. return 0;
  1401. return 1;
  1402. }
  1403. int
  1404. fileWalkSources(File *f)
  1405. {
  1406. if(f->mode == OReadOnly)
  1407. return 1;
  1408. if(!sourceLock2(f->source, f->msource, OReadWrite))
  1409. return 0;
  1410. sourceUnlock(f->source);
  1411. sourceUnlock(f->msource);
  1412. return 1;
  1413. }