ifile.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. int
  5. readIFile(IFile *f, char *name)
  6. {
  7. Part *p;
  8. ZBlock *b;
  9. int m;
  10. u8int *z;
  11. p = initPart(name, 1);
  12. if(p == nil)
  13. return 0;
  14. b = allocZBlock(8192, 1);
  15. if(b == nil){
  16. setErr(EOk, "can't alloc %s: %R", name);
  17. goto err;
  18. }
  19. if(p->size > PartBlank){
  20. /*
  21. * this is likely a real venti partition, in which case
  22. * we're looking for the config file stored as 8k at end of PartBlank.
  23. */
  24. if(!readPart(p, PartBlank-8192, b->data, 8192)){
  25. setErr(EOk, "can't read %s: %R", name);
  26. goto err;
  27. }
  28. m = 5+1+6+1;
  29. if(memcmp(b->data, "venti config\n", m) != 0){
  30. setErr(EOk, "bad venti config magic in %s", name);
  31. goto err;
  32. }
  33. b->data += m;
  34. b->len -= m;
  35. z = memchr(b->data, 0, b->len);
  36. if(z)
  37. b->len = z - b->data;
  38. }else if(p->size > 8192){
  39. setErr(EOk, "config file is too large");
  40. freePart(p);
  41. freeZBlock(b);
  42. return 0;
  43. }else{
  44. if(!readPart(p, 0, b->data, p->size)){
  45. setErr(EOk, "can't read %s: %R", name);
  46. goto err;
  47. }
  48. b->len = p->size;
  49. }
  50. f->name = name;
  51. f->b = b;
  52. f->pos = 0;
  53. return 1;
  54. err:
  55. freePart(p);
  56. if(b)
  57. freeZBlock(b);
  58. return 0;
  59. }
  60. void
  61. freeIFile(IFile *f)
  62. {
  63. freeZBlock(f->b);
  64. f->b = nil;
  65. f->pos = 0;
  66. }
  67. int
  68. partIFile(IFile *f, Part *part, u64int start, u32int size)
  69. {
  70. ZBlock *b;
  71. b = allocZBlock(size, 0);
  72. if(b == nil)
  73. return 0;
  74. if(!readPart(part, start, b->data, size)){
  75. setErr(EAdmin, "can't read %s: %R", part->name);
  76. freeZBlock(b);
  77. return 0;
  78. }
  79. f->name = part->name;
  80. f->b = b;
  81. f->pos = 0;
  82. return 1;
  83. }
  84. /*
  85. * return the next non-blank input line,
  86. * stripped of leading white space and with # comments eliminated
  87. */
  88. char*
  89. ifileLine(IFile *f)
  90. {
  91. char *s, *e, *t;
  92. int c;
  93. for(;;){
  94. s = (char*)&f->b->data[f->pos];
  95. e = memchr(s, '\n', f->b->len - f->pos);
  96. if(e == nil)
  97. return nil;
  98. *e++ = '\0';
  99. f->pos = e - (char*)f->b->data;
  100. t = strchr(s, '#');
  101. if(t != nil)
  102. *t = '\0';
  103. for(; c = *s; s++)
  104. if(c != ' ' && c != '\t' && c != '\r')
  105. return s;
  106. }
  107. }
  108. int
  109. ifileName(IFile *f, char *dst)
  110. {
  111. char *s;
  112. s = ifileLine(f);
  113. if(s == nil || strlen(s) >= ANameSize)
  114. return 0;
  115. nameCp(dst, s);
  116. return 1;
  117. }
  118. int
  119. ifileU32Int(IFile *f, u32int *r)
  120. {
  121. char *s;
  122. s = ifileLine(f);
  123. if(s == nil)
  124. return 0;
  125. return strU32Int(s, r);
  126. }