proto.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <auth.h>
  5. #include <fcall.h>
  6. #include <disk.h>
  7. enum {
  8. LEN = 8*1024,
  9. HUNKS = 128,
  10. };
  11. typedef struct File File;
  12. struct File{
  13. char *new;
  14. char *elem;
  15. char *old;
  16. char *uid;
  17. char *gid;
  18. ulong mode;
  19. };
  20. typedef void Mkfserr(char*, void*);
  21. typedef void Mkfsenum(char*, char*, Dir*, void*);
  22. typedef struct Name Name;
  23. struct Name {
  24. int n;
  25. char *s;
  26. };
  27. typedef struct Mkaux Mkaux;
  28. struct Mkaux {
  29. Mkfserr *warn;
  30. Mkfsenum *mkenum;
  31. char *root;
  32. char *proto;
  33. jmp_buf jmp;
  34. Biobuf *b;
  35. Name oldfile;
  36. Name fullname;
  37. int lineno;
  38. int indent;
  39. void *a;
  40. };
  41. static void domkfs(Mkaux *mkaux, File *me, int level);
  42. static int copyfile(Mkaux*, File*, Dir*, int);
  43. static void freefile(File*);
  44. static File* getfile(Mkaux*, File*);
  45. static char* getmode(Mkaux*, char*, ulong*);
  46. static char* getname(Mkaux*, char*, char**);
  47. static char* getpath(Mkaux*, char*);
  48. static int mkfile(Mkaux*, File*);
  49. static char* mkpath(Mkaux*, char*, char*);
  50. static void mktree(Mkaux*, File*, int);
  51. static void setnames(Mkaux*, File*);
  52. static void skipdir(Mkaux*);
  53. static void warn(Mkaux*, char *, ...);
  54. //static void
  55. //mprint(char *new, char *old, Dir *d, void*)
  56. //{
  57. // print("%s %s %D\n", new, old, d);
  58. //}
  59. int
  60. rdproto(char *proto, char *root, Mkfsenum *mkenum, Mkfserr *mkerr, void *a)
  61. {
  62. Mkaux mx, *m;
  63. File file;
  64. int rv;
  65. m = &mx;
  66. memset(&mx, 0, sizeof mx);
  67. if(root == nil)
  68. root = "/";
  69. m->root = root;
  70. m->warn = mkerr;
  71. m->mkenum = mkenum;
  72. m->a = a;
  73. m->proto = proto;
  74. m->lineno = 0;
  75. m->indent = 0;
  76. if((m->b = Bopen(proto, OREAD)) == nil) {
  77. werrstr("open '%s': %r", proto);
  78. return -1;
  79. }
  80. memset(&file, 0, sizeof file);
  81. file.new = "";
  82. file.old = nil;
  83. rv = 0;
  84. if(setjmp(m->jmp) == 0)
  85. domkfs(m, &file, -1);
  86. else
  87. rv = -1;
  88. free(m->oldfile.s);
  89. free(m->fullname.s);
  90. return rv;
  91. }
  92. static void*
  93. emalloc(Mkaux *mkaux, ulong n)
  94. {
  95. void *v;
  96. v = malloc(n);
  97. if(v == nil)
  98. longjmp(mkaux->jmp, 1); /* memory leak */
  99. memset(v, 0, n);
  100. return v;
  101. }
  102. static char*
  103. estrdup(Mkaux *mkaux, char *s)
  104. {
  105. s = strdup(s);
  106. if(s == nil)
  107. longjmp(mkaux->jmp, 1); /* memory leak */
  108. return s;
  109. }
  110. static void
  111. domkfs(Mkaux *mkaux, File *me, int level)
  112. {
  113. File *child;
  114. int rec;
  115. child = getfile(mkaux, me);
  116. if(!child)
  117. return;
  118. if((child->elem[0] == '+' || child->elem[0] == '*') && child->elem[1] == '\0'){
  119. rec = child->elem[0] == '+';
  120. free(child->new);
  121. child->new = estrdup(mkaux, me->new);
  122. setnames(mkaux, child);
  123. mktree(mkaux, child, rec);
  124. freefile(child);
  125. child = getfile(mkaux, me);
  126. }
  127. while(child && mkaux->indent > level){
  128. if(mkfile(mkaux, child))
  129. domkfs(mkaux, child, mkaux->indent);
  130. freefile(child);
  131. child = getfile(mkaux, me);
  132. }
  133. if(child){
  134. freefile(child);
  135. Bseek(mkaux->b, -Blinelen(mkaux->b), 1);
  136. mkaux->lineno--;
  137. }
  138. }
  139. static void
  140. mktree(Mkaux *mkaux, File *me, int rec)
  141. {
  142. File child;
  143. Dir *d;
  144. int i, n, fd;
  145. fd = open(mkaux->oldfile.s, OREAD);
  146. if(fd < 0){
  147. warn(mkaux, "can't open %s: %r", mkaux->oldfile.s);
  148. return;
  149. }
  150. child = *me;
  151. while((n = dirread(fd, &d)) > 0){
  152. for(i = 0; i < n; i++){
  153. child.new = mkpath(mkaux, me->new, d[i].name);
  154. if(me->old)
  155. child.old = mkpath(mkaux, me->old, d[i].name);
  156. child.elem = d[i].name;
  157. setnames(mkaux, &child);
  158. if((!(d[i].mode&DMDIR) || rec) && copyfile(mkaux, &child, &d[i], 1) && rec)
  159. mktree(mkaux, &child, rec);
  160. free(child.new);
  161. if(child.old)
  162. free(child.old);
  163. }
  164. }
  165. close(fd);
  166. }
  167. static int
  168. mkfile(Mkaux *mkaux, File *f)
  169. {
  170. Dir *d;
  171. if((d = dirstat(mkaux->oldfile.s)) == nil){
  172. warn(mkaux, "can't stat file %s: %r", mkaux->oldfile.s);
  173. skipdir(mkaux);
  174. return 0;
  175. }
  176. return copyfile(mkaux, f, d, 0);
  177. }
  178. enum {
  179. SLOP = 30
  180. };
  181. static void
  182. setname(Mkaux *mkaux, Name *name, char *s1, char *s2)
  183. {
  184. int l;
  185. l = strlen(s1)+strlen(s2)+1;
  186. if(name->n < l+SLOP/2) {
  187. free(name->s);
  188. name->s = emalloc(mkaux, l+SLOP);
  189. name->n = l+SLOP;
  190. }
  191. snprint(name->s, name->n, "%s%s%s", s1, s1[0]==0 || s1[strlen(s1)-1]!='/' ? "/" : "", s2);
  192. }
  193. static int
  194. copyfile(Mkaux *mkaux, File *f, Dir *d, int permonly)
  195. {
  196. Dir *nd;
  197. ulong xmode;
  198. char *p;
  199. setname(mkaux, &mkaux->fullname, mkaux->root, f->old ? f->old : f->new);
  200. /*
  201. * Extra stat here is inefficient but accounts for binds.
  202. */
  203. if((nd = dirstat(mkaux->fullname.s)) != nil)
  204. d = nd;
  205. d->name = f->elem;
  206. if(d->type != 'M'){
  207. d->uid = "sys";
  208. d->gid = "sys";
  209. xmode = (d->mode >> 6) & 7;
  210. d->mode |= xmode | (xmode << 3);
  211. }
  212. if(strcmp(f->uid, "-") != 0)
  213. d->uid = f->uid;
  214. if(strcmp(f->gid, "-") != 0)
  215. d->gid = f->gid;
  216. if(f->mode != ~0){
  217. if(permonly)
  218. d->mode = (d->mode & ~0666) | (f->mode & 0666);
  219. else if((d->mode&DMDIR) != (f->mode&DMDIR))
  220. warn(mkaux, "inconsistent mode for %s", f->new);
  221. else
  222. d->mode = f->mode;
  223. }
  224. if(p = strrchr(f->new, '/'))
  225. d->name = p+1;
  226. else
  227. d->name = f->new;
  228. mkaux->mkenum(f->new, mkaux->fullname.s, d, mkaux->a);
  229. xmode = d->mode;
  230. free(nd);
  231. return (xmode&DMDIR) != 0;
  232. }
  233. static char *
  234. mkpath(Mkaux *mkaux, char *prefix, char *elem)
  235. {
  236. char *p;
  237. int n;
  238. n = strlen(prefix) + strlen(elem) + 2;
  239. p = emalloc(mkaux, n);
  240. strcpy(p, prefix);
  241. strcat(p, "/");
  242. strcat(p, elem);
  243. return p;
  244. }
  245. static void
  246. setnames(Mkaux *mkaux, File *f)
  247. {
  248. if(f->old){
  249. if(f->old[0] == '/')
  250. setname(mkaux, &mkaux->oldfile, f->old, "");
  251. else
  252. setname(mkaux, &mkaux->oldfile, mkaux->root, f->old);
  253. } else
  254. setname(mkaux, &mkaux->oldfile, mkaux->root, f->new);
  255. }
  256. static void
  257. freefile(File *f)
  258. {
  259. if(f->old)
  260. free(f->old);
  261. if(f->new)
  262. free(f->new);
  263. free(f);
  264. }
  265. /*
  266. * skip all files in the proto that
  267. * could be in the current dir
  268. */
  269. static void
  270. skipdir(Mkaux *mkaux)
  271. {
  272. char *p, c;
  273. int level;
  274. if(mkaux->indent < 0)
  275. return;
  276. level = mkaux->indent;
  277. for(;;){
  278. mkaux->indent = 0;
  279. p = Brdline(mkaux->b, '\n');
  280. mkaux->lineno++;
  281. if(!p){
  282. mkaux->indent = -1;
  283. return;
  284. }
  285. while((c = *p++) != '\n')
  286. if(c == ' ')
  287. mkaux->indent++;
  288. else if(c == '\t')
  289. mkaux->indent += 8;
  290. else
  291. break;
  292. if(mkaux->indent <= level){
  293. Bseek(mkaux->b, -Blinelen(mkaux->b), 1);
  294. mkaux->lineno--;
  295. return;
  296. }
  297. }
  298. }
  299. static File*
  300. getfile(Mkaux *mkaux, File *old)
  301. {
  302. File *f;
  303. char *elem;
  304. char *p;
  305. int c;
  306. if(mkaux->indent < 0)
  307. return 0;
  308. loop:
  309. mkaux->indent = 0;
  310. p = Brdline(mkaux->b, '\n');
  311. mkaux->lineno++;
  312. if(!p){
  313. mkaux->indent = -1;
  314. return 0;
  315. }
  316. while((c = *p++) != '\n')
  317. if(c == ' ')
  318. mkaux->indent++;
  319. else if(c == '\t')
  320. mkaux->indent += 8;
  321. else
  322. break;
  323. if(c == '\n' || c == '#')
  324. goto loop;
  325. p--;
  326. f = emalloc(mkaux, sizeof *f);
  327. p = getname(mkaux, p, &elem);
  328. if(p == nil)
  329. return nil;
  330. f->new = mkpath(mkaux, old->new, elem);
  331. free(elem);
  332. f->elem = utfrrune(f->new, L'/') + 1;
  333. p = getmode(mkaux, p, &f->mode);
  334. p = getname(mkaux, p, &f->uid); /* LEAK */
  335. if(p == nil)
  336. return nil;
  337. if(!*f->uid)
  338. strcpy(f->uid, "-");
  339. p = getname(mkaux, p, &f->gid); /* LEAK */
  340. if(p == nil)
  341. return nil;
  342. if(!*f->gid)
  343. strcpy(f->gid, "-");
  344. f->old = getpath(mkaux, p);
  345. if(f->old && strcmp(f->old, "-") == 0){
  346. free(f->old);
  347. f->old = 0;
  348. }
  349. setnames(mkaux, f);
  350. return f;
  351. }
  352. static char*
  353. getpath(Mkaux *mkaux, char *p)
  354. {
  355. char *q, *new;
  356. int c, n;
  357. while((c = *p) == ' ' || c == '\t')
  358. p++;
  359. q = p;
  360. while((c = *q) != '\n' && c != ' ' && c != '\t')
  361. q++;
  362. if(q == p)
  363. return 0;
  364. n = q - p;
  365. new = emalloc(mkaux, n + 1);
  366. memcpy(new, p, n);
  367. new[n] = 0;
  368. return new;
  369. }
  370. static char*
  371. getname(Mkaux *mkaux, char *p, char **buf)
  372. {
  373. char *s, *start;
  374. int c;
  375. while((c = *p) == ' ' || c == '\t')
  376. p++;
  377. start = p;
  378. while((c = *p) != '\n' && c != ' ' && c != '\t')
  379. p++;
  380. *buf = malloc(p+2-start); /* +2: need at least 2 bytes; might strcpy "-" into buf */
  381. if(*buf == nil)
  382. return nil;
  383. memmove(*buf, start, p-start);
  384. (*buf)[p-start] = '\0';
  385. if(**buf == '$'){
  386. s = getenv(*buf+1);
  387. if(s == 0){
  388. warn(mkaux, "can't read environment variable %s", *buf+1);
  389. skipdir(mkaux);
  390. free(*buf);
  391. return nil;
  392. }
  393. free(*buf);
  394. *buf = s;
  395. }
  396. return p;
  397. }
  398. static char*
  399. getmode(Mkaux *mkaux, char *p, ulong *xmode)
  400. {
  401. char *buf, *s;
  402. ulong m;
  403. *xmode = ~0;
  404. p = getname(mkaux, p, &buf);
  405. if(p == nil)
  406. return nil;
  407. s = buf;
  408. if(!*s || strcmp(s, "-") == 0)
  409. return p;
  410. m = 0;
  411. if(*s == 'd'){
  412. m |= DMDIR;
  413. s++;
  414. }
  415. if(*s == 'a'){
  416. m |= DMAPPEND;
  417. s++;
  418. }
  419. if(*s == 'l'){
  420. m |= DMEXCL;
  421. s++;
  422. }
  423. if(s[0] < '0' || s[0] > '7'
  424. || s[1] < '0' || s[1] > '7'
  425. || s[2] < '0' || s[2] > '7'
  426. || s[3]){
  427. warn(mkaux, "bad mode specification %s", buf);
  428. free(buf);
  429. return p;
  430. }
  431. *xmode = m | strtoul(s, 0, 8);
  432. free(buf);
  433. return p;
  434. }
  435. static void
  436. warn(Mkaux *mkaux, char *fmt, ...)
  437. {
  438. char buf[256];
  439. va_list va;
  440. va_start(va, fmt);
  441. vseprint(buf, buf+sizeof(buf), fmt, va);
  442. va_end(va);
  443. if(mkaux->warn)
  444. mkaux->warn(buf, mkaux->a);
  445. else
  446. fprint(2, "warning: %s\n", buf);
  447. }