pattern.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <fcall.h>
  12. #include <bio.h>
  13. #include <regexp.h>
  14. #define Extern
  15. #include "exportfs.h"
  16. Reprog **exclude, **include;
  17. char *patternfile;
  18. void
  19. exclusions(void)
  20. {
  21. Biobuf *f;
  22. int ni, nmaxi, ne, nmaxe;
  23. char *line;
  24. if(patternfile == nil)
  25. return;
  26. f = Bopen(patternfile, OREAD);
  27. if(f == nil)
  28. fatal("cannot open patternfile");
  29. ni = 0;
  30. nmaxi = 100;
  31. include = malloc(nmaxi*sizeof(*include));
  32. if(include == nil)
  33. fatal("out of memory");
  34. include[0] = nil;
  35. ne = 0;
  36. nmaxe = 100;
  37. exclude = malloc(nmaxe*sizeof(*exclude));
  38. if(exclude == nil)
  39. fatal("out of memory");
  40. exclude[0] = nil;
  41. while(line = Brdline(f, '\n')){
  42. line[Blinelen(f) - 1] = 0;
  43. if(strlen(line) < 2 || line[1] != ' ')
  44. continue;
  45. switch(line[0]){
  46. case '+':
  47. if(ni+1 >= nmaxi){
  48. nmaxi = 2*nmaxi;
  49. include = realloc(include, nmaxi*sizeof(*include));
  50. if(include == nil)
  51. fatal("out of memory");
  52. }
  53. DEBUG(DFD, "\tinclude %s\n", line+2);
  54. include[ni] = regcomp(line+2);
  55. include[++ni] = nil;
  56. break;
  57. case '-':
  58. if(ne+1 >= nmaxe){
  59. nmaxe = 2*nmaxe;
  60. exclude = realloc(exclude, nmaxe*sizeof(*exclude));
  61. if(exclude == nil)
  62. fatal("out of memory");
  63. }
  64. DEBUG(DFD, "\texclude %s\n", line+2);
  65. exclude[ne] = regcomp(line+2);
  66. exclude[++ne] = nil;
  67. break;
  68. default:
  69. DEBUG(DFD, "ignoring pattern %s\n", line);
  70. break;
  71. }
  72. }
  73. Bterm(f);
  74. }
  75. int
  76. excludefile(char *path)
  77. {
  78. Reprog **re;
  79. char *p;
  80. if(*(path+1) == 0)
  81. p = "/";
  82. else
  83. p = path+1;
  84. DEBUG(DFD, "checking %s\n", path);
  85. for(re = include; *re != nil; re++){
  86. if(regexec(*re, p, nil, 0) != 1){
  87. DEBUG(DFD, "excluded+ %s\n", path);
  88. return -1;
  89. }
  90. }
  91. for(re = exclude; *re != nil; re++){
  92. if(regexec(*re, p, nil, 0) == 1){
  93. DEBUG(DFD, "excluded- %s\n", path);
  94. return -1;
  95. }
  96. }
  97. return 0;
  98. }
  99. int
  100. preaddir(Fid *f, uint8_t *data, int n, int64_t offset)
  101. {
  102. int r = 0, m;
  103. Dir *d;
  104. DEBUG(DFD, "\tpreaddir n=%d wo=%lld fo=%lld\n", n, offset, f->offset);
  105. if(offset == 0 && f->offset != 0){
  106. if(seek(f->fid, 0, 0) != 0)
  107. return -1;
  108. f->offset = f->cdir = f->ndir = 0;
  109. free(f->dir);
  110. f->dir = nil;
  111. }else if(offset != f->offset){
  112. werrstr("can't seek dir %lld to %lld", f->offset, offset);
  113. return -1;
  114. }
  115. while(n > 0){
  116. if(f->dir == nil){
  117. f->ndir = dirread(f->fid, &f->dir);
  118. if(f->ndir < 0)
  119. return f->ndir;
  120. if(f->ndir == 0)
  121. return r;
  122. }
  123. d = &f->dir[f->cdir++];
  124. if(exclude){
  125. char *p = makepath(f->f, d->name);
  126. if(excludefile(p)){
  127. free(p);
  128. goto skipentry;
  129. }
  130. free(p);
  131. }
  132. m = convD2M(d, data, n);
  133. DEBUG(DFD, "\t\tconvD2M %d\n", m);
  134. if(m <= BIT16SZ){
  135. DEBUG(DFD, "\t\t\tneeded %d\n", GBIT16(data));
  136. /* not enough room for full entry; leave for next time */
  137. f->cdir--;
  138. return r;
  139. }else{
  140. data += m;
  141. n -= m;
  142. r += m;
  143. f->offset += m;
  144. }
  145. skipentry: if(f->cdir >= f->ndir){
  146. f->cdir = f->ndir = 0;
  147. free(f->dir);
  148. f->dir = nil;
  149. }
  150. }
  151. return r;
  152. }