fs.c 2.8 KB

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