applylog.c 17 KB

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