ifile.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. static char vcmagic[] = "venti config\n";
  5. enum {
  6. Maxconfig = 8 * 1024,
  7. Maglen = sizeof vcmagic - 1,
  8. };
  9. int
  10. readifile(IFile *f, char *name)
  11. {
  12. Part *p;
  13. ZBlock *b;
  14. u8int *z;
  15. p = initpart(name, OREAD);
  16. if(p == nil)
  17. return -1;
  18. b = alloczblock(Maxconfig+1, 1, 0);
  19. if(b == nil){
  20. seterr(EOk, "can't alloc for %s: %R", name);
  21. return -1;
  22. }
  23. if(p->size > PartBlank){
  24. /*
  25. * this is likely a real venti partition, in which case we're
  26. * looking for the config file stored as 8k at end of PartBlank.
  27. */
  28. if(readpart(p, PartBlank-Maxconfig, b->data, Maxconfig) < 0){
  29. seterr(EOk, "can't read %s: %r", name);
  30. freezblock(b);
  31. freepart(p);
  32. return -1;
  33. }
  34. b->data[Maxconfig] = '\0';
  35. if(memcmp(b->data, vcmagic, Maglen) != 0){
  36. seterr(EOk, "bad venti config magic in %s", name);
  37. freezblock(b);
  38. freepart(p);
  39. return -1;
  40. }
  41. /*
  42. * if we change b->data+b->_size, freezblock
  43. * will blow an assertion, so don't.
  44. */
  45. b->data += Maglen;
  46. b->_size -= Maglen;
  47. b->len -= Maglen;
  48. z = memchr(b->data, '\0', b->len);
  49. if(z)
  50. b->len = z - b->data;
  51. }else if(p->size > Maxconfig){
  52. seterr(EOk, "config file is too large");
  53. freepart(p);
  54. freezblock(b);
  55. return -1;
  56. }else{
  57. freezblock(b);
  58. b = readfile(name);
  59. if(b == nil){
  60. freepart(p);
  61. return -1;
  62. }
  63. }
  64. freepart(p);
  65. f->name = name;
  66. f->b = b;
  67. f->pos = 0;
  68. return 0;
  69. }
  70. void
  71. freeifile(IFile *f)
  72. {
  73. freezblock(f->b);
  74. f->b = nil;
  75. f->pos = 0;
  76. }
  77. int
  78. partifile(IFile *f, Part *part, u64int start, u32int size)
  79. {
  80. ZBlock *b;
  81. b = alloczblock(size, 0, part->blocksize);
  82. if(b == nil)
  83. return -1;
  84. if(readpart(part, start, b->data, size) < 0){
  85. seterr(EAdmin, "can't read %s: %r", part->name);
  86. freezblock(b);
  87. return -1;
  88. }
  89. f->name = part->name;
  90. f->b = b;
  91. f->pos = 0;
  92. return 0;
  93. }
  94. /*
  95. * return the next non-blank input line,
  96. * stripped of leading white space and with # comments eliminated
  97. */
  98. char*
  99. ifileline(IFile *f)
  100. {
  101. char *s, *e, *t;
  102. int c;
  103. for(;;){
  104. s = (char*)&f->b->data[f->pos];
  105. e = memchr(s, '\n', f->b->len - f->pos);
  106. if(e == nil)
  107. return nil;
  108. *e++ = '\0';
  109. f->pos = e - (char*)f->b->data;
  110. t = strchr(s, '#');
  111. if(t != nil)
  112. *t = '\0';
  113. for(; c = *s; s++)
  114. if(c != ' ' && c != '\t' && c != '\r')
  115. return s;
  116. }
  117. }
  118. int
  119. ifilename(IFile *f, char *dst)
  120. {
  121. char *s;
  122. s = ifileline(f);
  123. if(s == nil || strlen(s) >= ANameSize)
  124. return -1;
  125. namecp(dst, s);
  126. return 0;
  127. }
  128. int
  129. ifileu32int(IFile *f, u32int *r)
  130. {
  131. char *s;
  132. s = ifileline(f);
  133. if(s == nil)
  134. return -1;
  135. return stru32int(s, r);
  136. }