util.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <auth.h>
  4. #include <fcall.h>
  5. #include <bio.h>
  6. #include "tapefs.h"
  7. Idmap *
  8. getpass(char *file)
  9. {
  10. Biobuf *bp;
  11. char *cp;
  12. Idmap *up;
  13. int nid, maxid;
  14. char *line[4];
  15. if ((bp = Bopen(file, OREAD)) == 0)
  16. error("Can't open passwd/group");
  17. up = emalloc(1*sizeof(Idmap));
  18. maxid = 1;
  19. nid = 0;
  20. while ((cp = Brdline(bp, '\n'))) {
  21. int nf;
  22. cp[Blinelen(bp)-1] = 0;
  23. nf = getfields(cp, line, 3, 0, ":\n");
  24. if (nf<3) {
  25. fprint(2, "bad format in %s\n", file);
  26. break;
  27. }
  28. if (nid>=maxid) {
  29. maxid *= 2;
  30. up = (Idmap *)erealloc(up, maxid*sizeof(Idmap));
  31. }
  32. up[nid].id = atoi(line[2]);
  33. up[nid].name = strdup(line[0]);
  34. nid++;
  35. }
  36. Bterm(bp);
  37. up[nid].name = 0;
  38. return up;
  39. }
  40. char *
  41. mapid(Idmap *up, int id)
  42. {
  43. char buf[16];
  44. if (up)
  45. while (up->name){
  46. if (up->id==id)
  47. return strdup(up->name);
  48. up++;
  49. }
  50. sprint(buf, "%d", id);
  51. return strdup(buf);
  52. }
  53. Ram *
  54. poppath(Fileinf fi, int new)
  55. {
  56. char *suffix, *origname;
  57. Ram *dir, *ent;
  58. Fileinf f;
  59. if (*fi.name=='\0')
  60. return 0;
  61. origname = estrdup(fi.name);
  62. if (suffix=strrchr(fi.name, '/')){
  63. *suffix = 0;
  64. suffix++;
  65. if (*suffix=='\0'){
  66. fi.mode |= DMDIR;
  67. free(origname);
  68. return poppath(fi, 1);
  69. }
  70. /*
  71. * create parent directory of suffix;
  72. * may recurse, thus shortening fi.name even further.
  73. */
  74. f = fi;
  75. f.size = 0;
  76. f.addr = 0;
  77. f.mode = 0555|DMDIR;
  78. dir = poppath(f, 0);
  79. if (dir==0)
  80. dir = ram;
  81. } else {
  82. suffix = fi.name;
  83. dir = ram;
  84. if (strcmp(suffix, ".")==0) {
  85. free(origname);
  86. return dir;
  87. }
  88. }
  89. ent = lookup(dir, suffix);
  90. fi.mode |= 0400; /* at least user read */
  91. if (ent){
  92. if (((fi.mode&DMDIR)!=0) != ((ent->qid.type&QTDIR)!=0)){
  93. fprint(2,
  94. "%s file type changed; probably due to union dir.; ignoring\n",
  95. origname);
  96. free(origname);
  97. return ent;
  98. }
  99. if (new) {
  100. ent->ndata = fi.size;
  101. ent->addr = fi.addr;
  102. ent->data = fi.data;
  103. ent->perm = fi.mode;
  104. ent->mtime = fi.mdate;
  105. ent->user = mapid(uidmap, fi.uid);
  106. ent->group = mapid(gidmap, fi.gid);
  107. }
  108. } else {
  109. fi.name = suffix;
  110. ent = popfile(dir, fi);
  111. }
  112. free(origname);
  113. return ent;
  114. }
  115. Ram *
  116. popfile(Ram *dir, Fileinf fi)
  117. {
  118. Ram *ent = (Ram *)emalloc(sizeof(Ram));
  119. if (*fi.name=='\0')
  120. return 0;
  121. ent->busy = 1;
  122. ent->open = 0;
  123. ent->parent = dir;
  124. ent->next = dir->child;
  125. dir->child = ent;
  126. ent->child = 0;
  127. ent->qid.path = ++path;
  128. ent->qid.vers = 0;
  129. if(fi.mode&DMDIR)
  130. ent->qid.type = QTDIR;
  131. else
  132. ent->qid.type = QTFILE;
  133. ent->perm = fi.mode;
  134. ent->name = estrdup(fi.name);
  135. ent->atime = ent->mtime = fi.mdate;
  136. ent->user = mapid(uidmap, fi.uid);
  137. ent->group = mapid(gidmap, fi.gid);
  138. ent->ndata = fi.size;
  139. ent->data = fi.data;
  140. ent->addr = fi.addr;
  141. ent->replete |= replete;
  142. return ent;
  143. }
  144. Ram *
  145. lookup(Ram *dir, char *name)
  146. {
  147. Ram *r;
  148. if (dir==0)
  149. return 0;
  150. for (r=dir->child; r; r=r->next){
  151. if (r->busy==0 || strcmp(r->name, name)!=0)
  152. continue;
  153. return r;
  154. }
  155. return 0;
  156. }