fs.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "stdinc.h"
  2. #include "vac.h"
  3. #include "dat.h"
  4. #include "fns.h"
  5. static char EBadVacFormat[] = "bad format for vac file";
  6. static VacFS *
  7. vfsAlloc(VtSession *z, int bsize, long ncache)
  8. {
  9. VacFS *fs;
  10. fs = vtMemAllocZ(sizeof(VacFS));
  11. fs->ref = 1;
  12. fs->z = z;
  13. fs->bsize = bsize;
  14. fs->cache = cacheAlloc(z, bsize, ncache);
  15. return fs;
  16. }
  17. static int
  18. readScore(char* file, uchar score[VtScoreSize])
  19. {
  20. char buf[44];
  21. int fd, i, n, c;
  22. fd = open(file, OREAD);
  23. if(fd < 0) {
  24. vtSetError("can't open '%s': %r", file);
  25. return 0;
  26. }
  27. n = readn(fd, buf, sizeof(buf));
  28. close(fd);
  29. if(n < sizeof(buf)) {
  30. vtSetError("read '%s': too short", file);
  31. return 0;
  32. }
  33. if(strncmp(buf, "vac:", 4) != 0) {
  34. vtSetError("'%s': not a vac file", file);
  35. return 0;
  36. }
  37. memset(score, 0, VtScoreSize);
  38. for(i=4; i<sizeof(buf); i++) {
  39. if(buf[i] >= '0' && buf[i] <= '9')
  40. c = buf[i] - '0';
  41. else if(buf[i] >= 'a' && buf[i] <= 'f')
  42. c = buf[i] - 'a' + 10;
  43. else if(buf[i] >= 'A' && buf[i] <= 'F')
  44. c = buf[i] - 'A' + 10;
  45. else {
  46. vtSetError("'%s': bad format for venti score", file);
  47. return 0;
  48. }
  49. if((i & 1) == 0)
  50. c <<= 4;
  51. score[(i>>1)-2] |= c;
  52. }
  53. return 1;
  54. }
  55. VacFS *
  56. vfsOpen(VtSession *z, char *file, int readOnly, long ncache)
  57. {
  58. VacFS *fs;
  59. int n;
  60. VtRoot rt;
  61. uchar score[VtScoreSize], buf[VtRootSize];
  62. VacFile *root;
  63. if(!readScore(file, score))
  64. return nil;
  65. n = vtRead(z, score, VtRootType, buf, VtRootSize);
  66. if(n < 0)
  67. return nil;
  68. if(n != VtRootSize) {
  69. vtSetError("vtRead on root too short");
  70. return nil;
  71. }
  72. if(!vtSha1Check(score, buf, VtRootSize)) {
  73. vtSetError("vtSha1Check failed on root block");
  74. return nil;
  75. }
  76. if(!vtRootUnpack(&rt, buf))
  77. return nil;
  78. if(strcmp(rt.type, "vac") != 0) {
  79. vtSetError("not a vac root");
  80. return nil;
  81. }
  82. fs = vfsAlloc(z, rt.blockSize, ncache);
  83. memmove(fs->score, score, VtScoreSize);
  84. fs->readOnly = readOnly;
  85. root = vfRoot(fs, rt.score);
  86. if(root == nil)
  87. goto Err;
  88. fs->root = root;
  89. return fs;
  90. Err:
  91. if(root)
  92. vfDecRef(root);
  93. vfsClose(fs);
  94. return nil;
  95. }
  96. VacFS *
  97. vacFsCreate(VtSession *z, int bsize, long ncache)
  98. {
  99. VacFS *fs;
  100. fs = vfsAlloc(z, bsize, ncache);
  101. return fs;
  102. }
  103. int
  104. vfsIsReadOnly(VacFS *fs)
  105. {
  106. return fs->readOnly != 0;
  107. }
  108. VacFile *
  109. vfsGetRoot(VacFS *fs)
  110. {
  111. return vfIncRef(fs->root);
  112. }
  113. int
  114. vfsGetBlockSize(VacFS *fs)
  115. {
  116. return fs->bsize;
  117. }
  118. int
  119. vfsGetScore(VacFS *fs, uchar score[VtScoreSize])
  120. {
  121. memmove(fs, score, VtScoreSize);
  122. return 1;
  123. }
  124. long
  125. vfsGetCacheSize(VacFS *fs)
  126. {
  127. return cacheGetSize(fs->cache);
  128. }
  129. int
  130. vfsSetCacheSize(VacFS *fs, long size)
  131. {
  132. return cacheSetSize(fs->cache, size);
  133. }
  134. int
  135. vfsSnapshot(VacFS *fs, char *src, char *dst)
  136. {
  137. USED(fs);
  138. USED(src);
  139. USED(dst);
  140. return 1;
  141. }
  142. int
  143. vfsSync(VacFS*)
  144. {
  145. return 1;
  146. }
  147. int
  148. vfsClose(VacFS *fs)
  149. {
  150. if(fs->root)
  151. vfDecRef(fs->root);
  152. fs->root = nil;
  153. cacheCheck(fs->cache);
  154. cacheFree(fs->cache);
  155. memset(fs, 0, sizeof(VacFS));
  156. vtMemFree(fs);
  157. return 1;
  158. }