applylog.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. #include "all.h"
  2. int localdirstat(char*, Dir*);
  3. int ismatch(char*);
  4. void conflict(char*, char*, ...);
  5. void error(char*, ...);
  6. int isdir(char*);
  7. int errors;
  8. int nconf;
  9. int donothing;
  10. char **conf;
  11. int verbose;
  12. char **match;
  13. int nmatch;
  14. int resolve;
  15. int notempspool;
  16. char *lroot;
  17. char *rroot;
  18. Db *clientdb;
  19. int skip;
  20. int douid;
  21. char *mkname(char*, int, char*, char*);
  22. char localbuf[10240];
  23. char remotebuf[10240];
  24. int copyfile(char*, char*, Dir*, int, int*);
  25. ulong maxnow;
  26. int maxn;
  27. char *timefile;
  28. int timefd;
  29. Db *copyerr;
  30. void
  31. readtimefile(void)
  32. {
  33. int n;
  34. char buf[24];
  35. if((timefd = open(timefile, ORDWR)) < 0
  36. && (timefd = create(timefile, ORDWR|OEXCL, 0666)) < 0)
  37. return;
  38. n = readn(timefd, buf, sizeof buf);
  39. if(n < sizeof buf)
  40. return;
  41. maxnow = atoi(buf);
  42. maxn = atoi(buf+12);
  43. }
  44. void
  45. writetimefile(void)
  46. {
  47. char buf[24+1];
  48. snprint(buf, sizeof buf, "%11lud %11d ", maxnow, maxn);
  49. pwrite(timefd, buf, 24, 0);
  50. }
  51. static void membogus(char**);
  52. void
  53. addce(char *local)
  54. {
  55. char e[ERRMAX];
  56. Dir d;
  57. memset(&d, 0, sizeof d);
  58. rerrstr(e, sizeof e);
  59. d.name = atom(e);
  60. d.uid = "";
  61. d.gid = "";
  62. insertdb(copyerr, atom(local), &d);
  63. }
  64. void
  65. delce(char *local)
  66. {
  67. removedb(copyerr, local);
  68. }
  69. void
  70. chat(char *f, ...)
  71. {
  72. Fmt fmt;
  73. char buf[256];
  74. va_list arg;
  75. if(!verbose)
  76. return;
  77. fmtfdinit(&fmt, 1, buf, sizeof buf);
  78. va_start(arg, f);
  79. fmtvprint(&fmt, f, arg);
  80. va_end(arg);
  81. fmtfdflush(&fmt);
  82. }
  83. void
  84. usage(void)
  85. {
  86. fprint(2, "usage: replica/applylog [-cnsuv] [-T timefile] clientdb clientroot serverroot [path ...]\n");
  87. exits("usage");
  88. }
  89. int
  90. notexists(char *path)
  91. {
  92. char buf[ERRMAX];
  93. if(access(path, AEXIST) >= 0)
  94. return 0;
  95. rerrstr(buf, sizeof buf);
  96. if(strstr(buf, "entry not found") || strstr(buf, "not exist"))
  97. return 1;
  98. /* some other error, like network hangup */
  99. return 0;
  100. }
  101. void
  102. main(int argc, char **argv)
  103. {
  104. char *f[10], *local, *name, *remote, *s, *t, verb;
  105. int fd, havedb, havelocal, k, n, nf, skip;
  106. ulong now;
  107. Biobuf bin;
  108. Dir dbd, ld, nd, rd;
  109. Avlwalk *w;
  110. Entry *e;
  111. membogus(argv);
  112. quotefmtinstall();
  113. ARGBEGIN{
  114. case 's':
  115. case 'c':
  116. resolve = ARGC();
  117. break;
  118. case 'n':
  119. donothing = 1;
  120. verbose = 1;
  121. break;
  122. case 'T':
  123. timefile = EARGF(usage());
  124. break;
  125. case 't':
  126. notempspool = 1;
  127. break;
  128. case 'u':
  129. douid = 1;
  130. break;
  131. case 'v':
  132. verbose = 1;
  133. break;
  134. default:
  135. usage();
  136. }ARGEND
  137. if(argc < 3)
  138. usage();
  139. if(timefile)
  140. readtimefile();
  141. lroot = argv[1];
  142. if(!isdir(lroot))
  143. sysfatal("bad local root directory");
  144. rroot = argv[2];
  145. if(!isdir(rroot))
  146. sysfatal("bad remote root directory");
  147. if((clientdb = opendb(argv[0])) == nil)
  148. sysfatal("opendb %q: %r", argv[2]);
  149. match = argv+3;
  150. nmatch = argc-3;
  151. copyerr = opendb(nil);
  152. skip = 0;
  153. Binit(&bin, 0, OREAD);
  154. for(; s=Brdstr(&bin, '\n', 1); free(s)){
  155. t = estrdup(s);
  156. nf = tokenize(s, f, nelem(f));
  157. if(nf != 10 || strlen(f[2]) != 1){
  158. skip = 1;
  159. fprint(2, "warning: skipping bad log entry <%s>\n", t);
  160. free(t);
  161. continue;
  162. }
  163. free(t);
  164. now = strtoul(f[0], 0, 0);
  165. n = atoi(f[1]);
  166. verb = f[2][0];
  167. name = f[3];
  168. if(now < maxnow || (now==maxnow && n <= maxn))
  169. continue;
  170. if(!ismatch(name)){
  171. skip = 1;
  172. continue;
  173. }
  174. local = mkname(localbuf, sizeof localbuf, lroot, name);
  175. if(strcmp(f[4], "-") == 0)
  176. f[4] = f[3];
  177. remote = mkname(remotebuf, sizeof remotebuf, rroot, f[4]);
  178. rd.name = f[4];
  179. rd.mode = strtoul(f[5], 0, 8);
  180. rd.uid = f[6];
  181. rd.gid = f[7];
  182. rd.mtime = strtoul(f[8], 0, 10);
  183. rd.length = strtoll(f[9], 0, 10);
  184. havedb = finddb(clientdb, name, &dbd)>=0;
  185. havelocal = localdirstat(local, &ld)>=0;
  186. switch(verb){
  187. case 'd': /* delete file */
  188. delce(local);
  189. if(!havelocal) /* doesn't exist; who cares? */
  190. break;
  191. if(!havedb){
  192. if(resolve == 's')
  193. goto DoRemove;
  194. else if(resolve == 'c')
  195. goto DoRemoveDb;
  196. conflict(name, "locally created; will not remove");
  197. skip = 1;
  198. continue;
  199. }
  200. assert(havelocal && havedb);
  201. if(dbd.mtime > rd.mtime) /* we have a newer file than what was deleted */
  202. break;
  203. if(!(dbd.mode&DMDIR) && (dbd.mtime != ld.mtime || dbd.length != ld.length)){ /* locally modified since we downloaded it */
  204. if(resolve == 's')
  205. goto DoRemove;
  206. else if(resolve == 'c')
  207. break;
  208. conflict(name, "locally modified; will not remove");
  209. skip = 1;
  210. continue;
  211. }
  212. DoRemove:
  213. chat("d %q\n", name);
  214. if(donothing)
  215. break;
  216. if(remove(local) < 0){
  217. error("removing %q", name);
  218. skip = 1;
  219. continue;
  220. }
  221. DoRemoveDb:
  222. removedb(clientdb, name);
  223. break;
  224. case 'a': /* add file */
  225. if(!havedb){
  226. if(!havelocal)
  227. goto DoCreate;
  228. if((ld.mode&DMDIR) && (rd.mode&DMDIR))
  229. break;
  230. if(resolve == 's')
  231. goto DoCreate;
  232. else if(resolve == 'c')
  233. goto DoCreateDb;
  234. conflict(name, "locally created; will not overwrite");
  235. skip = 1;
  236. continue;
  237. }
  238. assert(havedb);
  239. if(dbd.mtime >= rd.mtime) /* already created this file; ignore */
  240. break;
  241. if(havelocal){
  242. if(dbd.mtime==ld.mtime && dbd.length==ld.length)
  243. goto DoCreate;
  244. if(resolve=='s')
  245. goto DoCreate;
  246. else if(resolve == 'c')
  247. break;
  248. conflict(name, "locally modified; will not overwrite");
  249. skip = 1;
  250. continue;
  251. }
  252. DoCreate:
  253. if(notexists(remote)){
  254. addce(local);
  255. /* no skip=1 */
  256. break;;
  257. }
  258. chat("a %q %luo %q %q %lud\n", name, rd.mode, rd.uid, rd.gid, rd.mtime);
  259. if(donothing)
  260. break;
  261. if(rd.mode&DMDIR){
  262. if((fd = create(local, OREAD, DMDIR)) < 0){
  263. error("mkdir %q: %r", name);
  264. skip = 1;
  265. continue;
  266. }
  267. nulldir(&nd);
  268. nd.mode = rd.mode;
  269. if(dirfwstat(fd, &nd) < 0)
  270. fprint(2, "warning: cannot set mode on %q\n", local);
  271. nulldir(&nd);
  272. nd.gid = rd.gid;
  273. if(dirfwstat(fd, &nd) < 0)
  274. fprint(2, "warning: cannot set gid on %q\n", local);
  275. if(douid){
  276. nulldir(&nd);
  277. nd.uid = rd.uid;
  278. if(dirfwstat(fd, &nd) < 0)
  279. fprint(2, "warning: cannot set uid on %q\n", local);
  280. }
  281. close(fd);
  282. rd.mtime = now;
  283. }else{
  284. if(copyfile(local, remote, &rd, 1, &k) < 0){
  285. if(k)
  286. addce(local);
  287. skip = 1;
  288. continue;
  289. }
  290. }
  291. DoCreateDb:
  292. insertdb(clientdb, name, &rd);
  293. break;
  294. case 'c': /* change contents */
  295. if(!havedb){
  296. if(notexists(remote)){
  297. addce(local);
  298. /* no skip=1 */
  299. break;
  300. }
  301. if(resolve == 's')
  302. goto DoCopy;
  303. else if(resolve=='c')
  304. goto DoCopyDb;
  305. if(havelocal)
  306. conflict(name, "locally created; will not update");
  307. else
  308. conflict(name, "not replicated; will not update");
  309. skip = 1;
  310. continue;
  311. }
  312. if(dbd.mtime >= rd.mtime) /* already have/had this version; ignore */
  313. break;
  314. if(!havelocal){
  315. if(notexists(remote)){
  316. addce(local);
  317. /* no skip=1 */
  318. break;
  319. }
  320. if(resolve == 's')
  321. goto DoCopy;
  322. else if(resolve == 'c')
  323. break;
  324. conflict(name, "locally removed; will not update");
  325. skip = 1;
  326. continue;
  327. }
  328. assert(havedb && havelocal);
  329. if(dbd.mtime != ld.mtime || dbd.length != ld.length){
  330. if(notexists(remote)){
  331. addce(local);
  332. /* no skip=1 */
  333. break;
  334. }
  335. if(resolve == 's')
  336. goto DoCopy;
  337. else if(resolve == 'c')
  338. break;
  339. conflict(name, "locally modified; will not update");
  340. skip = 1;
  341. continue;
  342. }
  343. DoCopy:
  344. if(notexists(remote)){
  345. addce(local);
  346. /* no skip=1 */
  347. break;
  348. }
  349. chat("c %q\n", name);
  350. if(donothing)
  351. break;
  352. if(copyfile(local, remote, &rd, 0, &k) < 0){
  353. if(k)
  354. addce(local);
  355. skip = 1;
  356. continue;
  357. }
  358. DoCopyDb:
  359. if(!havedb){
  360. if(havelocal)
  361. dbd = ld;
  362. else
  363. dbd = rd;
  364. }
  365. dbd.mtime = rd.mtime;
  366. dbd.length = rd.length;
  367. insertdb(clientdb, name, &dbd);
  368. break;
  369. case 'm': /* change metadata */
  370. if(!havedb){
  371. if(notexists(remote)){
  372. addce(local);
  373. /* no skip=1 */
  374. break;
  375. }
  376. if(resolve == 's')
  377. goto DoCreate;
  378. else if(resolve == 'c')
  379. goto DoMetaDb;
  380. if(havelocal)
  381. conflict(name, "locally created; will not update metadata");
  382. else
  383. conflict(name, "not replicated; will not update metadata");
  384. skip = 1;
  385. continue;
  386. }
  387. if(!(dbd.mode&DMDIR) && dbd.mtime > rd.mtime) /* have newer version; ignore */
  388. break;
  389. if((dbd.mode&DMDIR) && dbd.mtime > now)
  390. break;
  391. if(havelocal && (!douid || strcmp(ld.uid, rd.uid)==0) && strcmp(ld.gid, rd.gid)==0 && ld.mode==rd.mode) /* nothing to do */
  392. goto DoMetaDb;
  393. if(!havelocal){
  394. if(notexists(remote)){
  395. addce(local);
  396. /* no skip=1 */
  397. break;
  398. }
  399. if(resolve == 's')
  400. goto DoCreate;
  401. else if(resolve == 'c')
  402. break;
  403. conflict(name, "locally removed; will not update metadata");
  404. skip = 1;
  405. continue;
  406. }
  407. if(!(dbd.mode&DMDIR) && (dbd.mtime != ld.mtime || dbd.length != ld.length)){ /* this check might be overkill */
  408. if(notexists(remote)){
  409. addce(local);
  410. /* no skip=1 */
  411. break;
  412. }
  413. if(resolve == 's')
  414. goto DoMeta;
  415. else if(resolve == 'c')
  416. break;
  417. conflict(name, "contents locally modified; will not update metadata to %s %s %luo",
  418. rd.uid, rd.gid, rd.mode);
  419. skip = 1;
  420. continue;
  421. }
  422. if((douid && strcmp(ld.uid, dbd.uid)!=0) || strcmp(ld.gid, dbd.gid)!=0 || ld.mode!=dbd.mode){
  423. if(notexists(remote)){
  424. addce(local);
  425. /* no skip=1 */
  426. break;
  427. }
  428. if(resolve == 's')
  429. goto DoMeta;
  430. else if(resolve == 'c')
  431. break;
  432. conflict(name, "metadata locally changed; will not update metadata to %s %s %luo", rd.uid, rd.gid, rd.mode);
  433. skip = 1;
  434. continue;
  435. }
  436. DoMeta:
  437. if(notexists(remote)){
  438. addce(local);
  439. /* no skip=1 */
  440. break;
  441. }
  442. chat("m %q %luo %q %q %lud\n", name, rd.mode, rd.uid, rd.gid, rd.mtime);
  443. if(donothing)
  444. break;
  445. nulldir(&nd);
  446. nd.gid = rd.gid;
  447. nd.mode = rd.mode;
  448. if(douid)
  449. nd.uid = rd.uid;
  450. if(dirwstat(local, &nd) < 0){
  451. error("dirwstat %q: %r", name);
  452. skip = 1;
  453. continue;
  454. }
  455. DoMetaDb:
  456. if(dbd.mode&DMDIR)
  457. dbd.mtime = now;
  458. dbd.gid = rd.gid;
  459. dbd.mode = rd.mode;
  460. if(douid)
  461. dbd.uid = rd.uid;
  462. insertdb(clientdb, name, &dbd);
  463. break;
  464. }
  465. if(!skip && !donothing){
  466. maxnow = now;
  467. maxn = n;
  468. }
  469. }
  470. w = avlwalk(copyerr->avl);
  471. while(e = (Entry*)avlnext(w))
  472. error("copying %q: %s\n", e->name, e->d.name);
  473. if(timefile)
  474. writetimefile();
  475. if(nconf)
  476. exits("conflicts");
  477. if(errors)
  478. exits("errors");
  479. exits(nil);
  480. }
  481. char*
  482. mkname(char *buf, int nbuf, char *a, char *b)
  483. {
  484. if(strlen(a)+strlen(b)+2 > nbuf)
  485. sysfatal("name too long");
  486. strcpy(buf, a);
  487. if(a[strlen(a)-1] != '/')
  488. strcat(buf, "/");
  489. strcat(buf, b);
  490. return buf;
  491. }
  492. int
  493. isdir(char *s)
  494. {
  495. ulong m;
  496. Dir *d;
  497. if((d = dirstat(s)) == nil)
  498. return 0;
  499. m = d->mode;
  500. free(d);
  501. return (m&DMDIR) != 0;
  502. }
  503. void
  504. conflict(char *name, char *f, ...)
  505. {
  506. char *s;
  507. va_list arg;
  508. va_start(arg, f);
  509. s = vsmprint(f, arg);
  510. va_end(arg);
  511. fprint(2, "%s: %s\n", name, s);
  512. free(s);
  513. nconf++;
  514. // if(nconf%16 == 0)
  515. // conf = erealloc(conf, (nconf+16)*sizeof(conf[0]));
  516. // conf[nconf++] = estrdup(name);
  517. }
  518. void
  519. error(char *f, ...)
  520. {
  521. char *s;
  522. va_list arg;
  523. va_start(arg, f);
  524. s = vsmprint(f, arg);
  525. va_end(arg);
  526. fprint(2, "error: %s\n", s);
  527. free(s);
  528. errors = 1;
  529. }
  530. int
  531. ismatch(char *s)
  532. {
  533. int i, len;
  534. if(nmatch == 0)
  535. return 1;
  536. for(i=0; i<nmatch; i++){
  537. if(strcmp(s, match[i]) == 0)
  538. return 1;
  539. len = strlen(match[i]);
  540. if(strncmp(s, match[i], len) == 0 && s[len]=='/')
  541. return 1;
  542. }
  543. return 0;
  544. }
  545. int
  546. localdirstat(char *name, Dir *d)
  547. {
  548. static Dir *d2;
  549. free(d2);
  550. if((d2 = dirstat(name)) == nil)
  551. return -1;
  552. *d = *d2;
  553. return 0;
  554. }
  555. enum { DEFB = 8192 };
  556. static int
  557. copy1(int fdf, int fdt, char *from, char *to)
  558. {
  559. char buf[DEFB];
  560. long n, n1, rcount;
  561. int rv;
  562. char err[ERRMAX];
  563. /* clear any residual error */
  564. err[0] = '\0';
  565. errstr(err, ERRMAX);
  566. rv = 0;
  567. for(rcount=0;; rcount++) {
  568. n = read(fdf, buf, DEFB);
  569. if(n <= 0)
  570. break;
  571. n1 = write(fdt, buf, n);
  572. if(n1 != n) {
  573. fprint(2, "error writing %q: %r\n", to);
  574. rv = -1;
  575. break;
  576. }
  577. }
  578. if(n < 0) {
  579. fprint(2, "error reading %q: %r\n", from);
  580. rv = -1;
  581. }
  582. return rv;
  583. }
  584. static int
  585. opentemp(char *template)
  586. {
  587. int fd, i;
  588. char *p;
  589. p = estrdup(template);
  590. fd = -1;
  591. for(i=0; i<10; i++){
  592. mktemp(p);
  593. if((fd=create(p, ORDWR|OEXCL|ORCLOSE, 0000)) >= 0)
  594. break;
  595. strcpy(p, template);
  596. }
  597. if(fd < 0)
  598. return -1;
  599. strcpy(template, p);
  600. free(p);
  601. return fd;
  602. }
  603. int
  604. copyfile(char *local, char *remote, Dir *d, int dowstat, int *printerror)
  605. {
  606. Dir *d0, *d1, *dl;
  607. Dir nd;
  608. int rfd, tfd, wfd, didcreate;
  609. char tmp[32];
  610. char err[ERRMAX];
  611. Again:
  612. *printerror = 0;
  613. if((rfd = open(remote, OREAD)) < 0)
  614. return -1;
  615. d0 = dirfstat(rfd);
  616. if(d0 == nil){
  617. close(rfd);
  618. return -1;
  619. }
  620. *printerror = 1;
  621. if(notempspool){
  622. tfd = rfd;
  623. goto DoCopy;
  624. }
  625. strcpy(tmp, "/tmp/replicaXXXXXXXX");
  626. tfd = opentemp(tmp);
  627. if(tfd < 0){
  628. close(rfd);
  629. free(d0);
  630. return -1;
  631. }
  632. if(copy1(rfd, tfd, remote, tmp) < 0 || (d1 = dirfstat(rfd)) == nil){
  633. close(rfd);
  634. close(tfd);
  635. free(d0);
  636. return -1;
  637. }
  638. close(rfd);
  639. if(d0->qid.path != d1->qid.path
  640. || d0->qid.vers != d1->qid.vers
  641. || d0->mtime != d1->mtime
  642. || d0->length != d1->length){
  643. /* file changed underfoot; go around again */
  644. close(tfd);
  645. free(d0);
  646. free(d1);
  647. goto Again;
  648. }
  649. free(d1);
  650. if(seek(tfd, 0, 0) != 0){
  651. close(tfd);
  652. free(d0);
  653. return -1;
  654. }
  655. DoCopy:
  656. didcreate = 0;
  657. if((dl = dirstat(local)) == nil){
  658. if((wfd = create(local, OWRITE, 0)) >= 0){
  659. didcreate = 1;
  660. goto okay;
  661. }
  662. goto err;
  663. }else{
  664. if((wfd = open(local, OTRUNC|OWRITE)) >= 0)
  665. goto okay;
  666. rerrstr(err, sizeof err);
  667. if(strstr(err, "permission") == nil)
  668. goto err;
  669. nulldir(&nd);
  670. /*
  671. * Assume the person running pull is in the appropriate
  672. * groups. We could set 0666 instead, but I'm worried
  673. * about leaving the file world-readable or world-writable
  674. * when it shouldn't be.
  675. */
  676. nd.mode = dl->mode | 0660;
  677. if(nd.mode == dl->mode)
  678. goto err;
  679. if(dirwstat(local, &nd) < 0)
  680. goto err;
  681. if((wfd = open(local, OTRUNC|OWRITE)) >= 0){
  682. nd.mode = dl->mode;
  683. if(dirfwstat(wfd, &nd) < 0)
  684. fprint(2, "warning: set mode on %s to 0660 to open; cannot set back to %luo: %r\n", local, nd.mode);
  685. goto okay;
  686. }
  687. nd.mode = dl->mode;
  688. if(dirwstat(local, &nd) < 0)
  689. fprint(2, "warning: set mode on %s to %luo to open; open failed; cannot set mode back to %luo: %r\n", local, nd.mode|0660, nd.mode);
  690. goto err;
  691. }
  692. err:
  693. close(tfd);
  694. free(d0);
  695. free(dl);
  696. return -1;
  697. okay:
  698. free(dl);
  699. if(copy1(tfd, wfd, tmp, local) < 0){
  700. close(tfd);
  701. close(wfd);
  702. free(d0);
  703. return -1;
  704. }
  705. close(tfd);
  706. if(didcreate || dowstat){
  707. nulldir(&nd);
  708. nd.mode = d->mode;
  709. if(dirfwstat(wfd, &nd) < 0)
  710. fprint(2, "warning: cannot set mode on %s\n", local);
  711. nulldir(&nd);
  712. nd.gid = d->gid;
  713. if(dirfwstat(wfd, &nd) < 0)
  714. fprint(2, "warning: cannot set gid on %s\n", local);
  715. if(douid){
  716. nulldir(&nd);
  717. nd.uid = d->uid;
  718. if(dirfwstat(wfd, &nd) < 0)
  719. fprint(2, "warning: cannot set uid on %s\n", local);
  720. }
  721. }
  722. d->mtime = d0->mtime;
  723. d->length = d0->length;
  724. nulldir(&nd);
  725. nd.mtime = d->mtime;
  726. if(dirfwstat(wfd, &nd) < 0)
  727. fprint(2, "warning: cannot set mtime on %s\n", local);
  728. close(wfd);
  729. free(d0);
  730. return 0;
  731. }
  732. /*
  733. * Applylog might try to overwrite itself.
  734. * To avoid problems with this, we copy ourselves
  735. * into /tmp and then re-exec.
  736. */
  737. char *rmargv0;
  738. static void
  739. rmself(void)
  740. {
  741. remove(rmargv0);
  742. }
  743. static int
  744. genopentemp(char *template, int mode, int perm)
  745. {
  746. int fd, i;
  747. char *p;
  748. p = estrdup(template);
  749. fd = -1;
  750. for(i=0; i<10; i++){
  751. mktemp(p);
  752. if(access(p, 0) < 0 && (fd=create(p, mode, perm)) >= 0)
  753. break;
  754. strcpy(p, template);
  755. }
  756. if(fd < 0)
  757. sysfatal("could not create temporary file");
  758. strcpy(template, p);
  759. free(p);
  760. return fd;
  761. }
  762. static void
  763. membogus(char **argv)
  764. {
  765. int n, fd, wfd;
  766. char template[50], buf[1024];
  767. if(strncmp(argv[0], "/tmp/_applylog_", 1+3+1+1+8+1)==0) {
  768. rmargv0 = argv[0];
  769. atexit(rmself);
  770. return;
  771. }
  772. if((fd = open(argv[0], OREAD)) < 0)
  773. return;
  774. strcpy(template, "/tmp/_applylog_XXXXXX");
  775. if((wfd = genopentemp(template, OWRITE, 0700)) < 0)
  776. return;
  777. while((n = read(fd, buf, sizeof buf)) > 0)
  778. if(write(wfd, buf, n) != n)
  779. goto Error;
  780. if(n != 0)
  781. goto Error;
  782. close(fd);
  783. close(wfd);
  784. argv[0] = template;
  785. exec(template, argv);
  786. fprint(2, "exec error %r\n");
  787. Error:
  788. close(fd);
  789. close(wfd);
  790. remove(template);
  791. return;
  792. }