fs.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. vfsGetScore(fs, score);
  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. return vfsAlloc(z, bsize, ncache);
  100. }
  101. int
  102. vfsIsReadOnly(VacFS *fs)
  103. {
  104. return fs->readOnly != 0;
  105. }
  106. VacFile *
  107. vfsGetRoot(VacFS *fs)
  108. {
  109. return vfIncRef(fs->root);
  110. }
  111. int
  112. vfsGetBlockSize(VacFS *fs)
  113. {
  114. return fs->bsize;
  115. }
  116. int
  117. vfsGetScore(VacFS *fs, uchar score[VtScoreSize])
  118. {
  119. memmove(fs->score, score, VtScoreSize);
  120. return 1;
  121. }
  122. long
  123. vfsGetCacheSize(VacFS *fs)
  124. {
  125. return cacheGetSize(fs->cache);
  126. }
  127. int
  128. vfsSetCacheSize(VacFS *fs, long size)
  129. {
  130. return cacheSetSize(fs->cache, size);
  131. }
  132. int
  133. vfsSnapshot(VacFS *fs, char *src, char *dst)
  134. {
  135. USED(fs);
  136. USED(src);
  137. USED(dst);
  138. return 1;
  139. }
  140. int
  141. vfsSync(VacFS*)
  142. {
  143. return 1;
  144. }
  145. int
  146. vfsClose(VacFS *fs)
  147. {
  148. if(fs->root)
  149. vfDecRef(fs->root);
  150. fs->root = nil;
  151. cacheCheck(fs->cache);
  152. cacheFree(fs->cache);
  153. memset(fs, 0, sizeof(VacFS));
  154. vtMemFree(fs);
  155. return 1;
  156. }