applylog.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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((ld.mode&DMDIR) && (rd.mode&DMDIR))
  247. break;
  248. if(dbd.mtime==ld.mtime && dbd.length==ld.length)
  249. goto DoCreate;
  250. if(resolve=='s')
  251. goto DoCreate;
  252. else if(resolve == 'c')
  253. break;
  254. conflict(name, "locally modified; will not overwrite");
  255. skip = 1;
  256. continue;
  257. }
  258. DoCreate:
  259. if(notexists(remote)){
  260. addce(local);
  261. /* no skip=1 */
  262. break;;
  263. }
  264. chat("a %q %luo %q %q %lud\n", name, rd.mode, rd.uid, rd.gid, rd.mtime);
  265. if(donothing)
  266. break;
  267. if(rd.mode&DMDIR){
  268. if((fd = create(local, OREAD, DMDIR)) < 0){
  269. error("mkdir %q: %r", name);
  270. skip = 1;
  271. continue;
  272. }
  273. nulldir(&nd);
  274. nd.mode = rd.mode;
  275. if(dirfwstat(fd, &nd) < 0)
  276. fprint(2, "warning: cannot set mode on %q\n", local);
  277. nulldir(&nd);
  278. nd.gid = rd.gid;
  279. if(dirfwstat(fd, &nd) < 0)
  280. fprint(2, "warning: cannot set gid on %q\n", local);
  281. if(douid){
  282. nulldir(&nd);
  283. nd.uid = rd.uid;
  284. if(dirfwstat(fd, &nd) < 0)
  285. fprint(2, "warning: cannot set uid on %q\n", local);
  286. }
  287. close(fd);
  288. rd.mtime = now;
  289. }else{
  290. if(copyfile(local, remote, name, &rd, 1, &k) < 0){
  291. if(k)
  292. addce(local);
  293. skip = 1;
  294. continue;
  295. }
  296. }
  297. DoCreateDb:
  298. insertdb(clientdb, name, &rd);
  299. break;
  300. case 'c': /* change contents */
  301. if(!havedb){
  302. if(notexists(remote)){
  303. addce(local);
  304. /* no skip=1 */
  305. break;
  306. }
  307. if(resolve == 's')
  308. goto DoCopy;
  309. else if(resolve=='c')
  310. goto DoCopyDb;
  311. if(havelocal)
  312. conflict(name, "locally created; will not update");
  313. else
  314. conflict(name, "not replicated; will not update");
  315. skip = 1;
  316. continue;
  317. }
  318. if(dbd.mtime >= rd.mtime) /* already have/had this version; ignore */
  319. break;
  320. if(!havelocal){
  321. if(notexists(remote)){
  322. addce(local);
  323. /* no skip=1 */
  324. break;
  325. }
  326. if(resolve == 's')
  327. goto DoCopy;
  328. else if(resolve == 'c')
  329. break;
  330. conflict(name, "locally removed; will not update");
  331. skip = 1;
  332. continue;
  333. }
  334. assert(havedb && havelocal);
  335. if(dbd.mtime != ld.mtime || dbd.length != ld.length){
  336. if(notexists(remote)){
  337. addce(local);
  338. /* no skip=1 */
  339. break;
  340. }
  341. if(resolve == 's')
  342. goto DoCopy;
  343. else if(resolve == 'c')
  344. break;
  345. conflict(name, "locally modified; will not update");
  346. skip = 1;
  347. continue;
  348. }
  349. DoCopy:
  350. if(notexists(remote)){
  351. addce(local);
  352. /* no skip=1 */
  353. break;
  354. }
  355. chat("c %q\n", name);
  356. if(donothing)
  357. break;
  358. if(copyfile(local, remote, name, &rd, 0, &k) < 0){
  359. if(k)
  360. addce(local);
  361. skip = 1;
  362. continue;
  363. }
  364. DoCopyDb:
  365. if(!havedb){
  366. if(havelocal)
  367. dbd = ld;
  368. else
  369. dbd = rd;
  370. }
  371. dbd.mtime = rd.mtime;
  372. dbd.length = rd.length;
  373. insertdb(clientdb, name, &dbd);
  374. break;
  375. case 'm': /* change metadata */
  376. if(!havedb){
  377. if(notexists(remote)){
  378. addce(local);
  379. /* no skip=1 */
  380. break;
  381. }
  382. if(resolve == 's')
  383. goto DoCreate;
  384. else if(resolve == 'c')
  385. goto DoMetaDb;
  386. if(havelocal)
  387. conflict(name, "locally created; will not update metadata");
  388. else
  389. conflict(name, "not replicated; will not update metadata");
  390. skip = 1;
  391. continue;
  392. }
  393. if(!(dbd.mode&DMDIR) && dbd.mtime > rd.mtime) /* have newer version; ignore */
  394. break;
  395. if((dbd.mode&DMDIR) && dbd.mtime > now)
  396. break;
  397. if(havelocal && (!douid || strcmp(ld.uid, rd.uid)==0) && strcmp(ld.gid, rd.gid)==0 && ld.mode==rd.mode) /* nothing to do */
  398. goto DoMetaDb;
  399. if(!havelocal){
  400. if(notexists(remote)){
  401. addce(local);
  402. /* no skip=1 */
  403. break;
  404. }
  405. if(resolve == 's')
  406. goto DoCreate;
  407. else if(resolve == 'c')
  408. break;
  409. conflict(name, "locally removed; will not update metadata");
  410. skip = 1;
  411. continue;
  412. }
  413. if(!(dbd.mode&DMDIR) && (dbd.mtime != ld.mtime || dbd.length != ld.length)){ /* this check might be overkill */
  414. if(notexists(remote)){
  415. addce(local);
  416. /* no skip=1 */
  417. break;
  418. }
  419. if(resolve == 's')
  420. goto DoMeta;
  421. else if(resolve == 'c')
  422. break;
  423. conflict(name, "contents locally modified; will not update metadata to %s %s %luo",
  424. rd.uid, rd.gid, rd.mode);
  425. skip = 1;
  426. continue;
  427. }
  428. if((douid && strcmp(ld.uid, dbd.uid)!=0) || strcmp(ld.gid, dbd.gid)!=0 || ld.mode!=dbd.mode){
  429. if(notexists(remote)){
  430. addce(local);
  431. /* no skip=1 */
  432. break;
  433. }
  434. if(resolve == 's')
  435. goto DoMeta;
  436. else if(resolve == 'c')
  437. break;
  438. conflict(name, "metadata locally changed; will not update metadata to %s %s %luo", rd.uid, rd.gid, rd.mode);
  439. skip = 1;
  440. continue;
  441. }
  442. DoMeta:
  443. if(notexists(remote)){
  444. addce(local);
  445. /* no skip=1 */
  446. break;
  447. }
  448. chat("m %q %luo %q %q %lud\n", name, rd.mode, rd.uid, rd.gid, rd.mtime);
  449. if(donothing)
  450. break;
  451. nulldir(&nd);
  452. nd.gid = rd.gid;
  453. nd.mode = rd.mode;
  454. if(douid)
  455. nd.uid = rd.uid;
  456. if(dirwstat(local, &nd) < 0){
  457. error("dirwstat %q: %r", name);
  458. skip = 1;
  459. continue;
  460. }
  461. DoMetaDb:
  462. if(dbd.mode&DMDIR)
  463. dbd.mtime = now;
  464. dbd.gid = rd.gid;
  465. dbd.mode = rd.mode;
  466. if(douid)
  467. dbd.uid = rd.uid;
  468. insertdb(clientdb, name, &dbd);
  469. break;
  470. }
  471. if(!skip && !donothing){
  472. maxnow = now;
  473. maxn = n;
  474. }
  475. }
  476. w = avlwalk(copyerr->avl);
  477. while(e = (Entry*)avlnext(w))
  478. error("copying %q: %s\n", e->name, e->d.name);
  479. if(timefile)
  480. writetimefile();
  481. if(nconf)
  482. exits("conflicts");
  483. if(errors)
  484. exits("errors");
  485. exits(nil);
  486. }
  487. char*
  488. mkname(char *buf, int nbuf, char *a, char *b)
  489. {
  490. if(strlen(a)+strlen(b)+2 > nbuf)
  491. sysfatal("name too long");
  492. strcpy(buf, a);
  493. if(a[strlen(a)-1] != '/')
  494. strcat(buf, "/");
  495. strcat(buf, b);
  496. return buf;
  497. }
  498. int
  499. isdir(char *s)
  500. {
  501. ulong m;
  502. Dir *d;
  503. if((d = dirstat(s)) == nil)
  504. return 0;
  505. m = d->mode;
  506. free(d);
  507. return (m&DMDIR) != 0;
  508. }
  509. void
  510. conflict(char *name, char *f, ...)
  511. {
  512. char *s;
  513. va_list arg;
  514. va_start(arg, f);
  515. s = vsmprint(f, arg);
  516. va_end(arg);
  517. fprint(2, "%s: %s\n", name, s);
  518. free(s);
  519. nconf++;
  520. // if(nconf%16 == 0)
  521. // conf = erealloc(conf, (nconf+16)*sizeof(conf[0]));
  522. // conf[nconf++] = estrdup(name);
  523. }
  524. void
  525. error(char *f, ...)
  526. {
  527. char *s;
  528. va_list arg;
  529. va_start(arg, f);
  530. s = vsmprint(f, arg);
  531. va_end(arg);
  532. fprint(2, "error: %s\n", s);
  533. free(s);
  534. errors = 1;
  535. }
  536. int
  537. ismatch(char *s)
  538. {
  539. int i, len;
  540. if(nmatch == 0)
  541. return 1;
  542. for(i=0; i<nmatch; i++){
  543. if(strcmp(s, match[i]) == 0)
  544. return 1;
  545. len = strlen(match[i]);
  546. if(strncmp(s, match[i], len) == 0 && s[len]=='/')
  547. return 1;
  548. }
  549. return 0;
  550. }
  551. int
  552. localdirstat(char *name, Dir *d)
  553. {
  554. static Dir *d2;
  555. free(d2);
  556. if((d2 = dirstat(name)) == nil)
  557. return -1;
  558. *d = *d2;
  559. return 0;
  560. }
  561. enum { DEFB = 8192 };
  562. static int
  563. copy1(int fdf, int fdt, char *from, char *to)
  564. {
  565. char buf[DEFB];
  566. long n, n1, rcount;
  567. int rv;
  568. char err[ERRMAX];
  569. /* clear any residual error */
  570. err[0] = '\0';
  571. errstr(err, ERRMAX);
  572. rv = 0;
  573. for(rcount=0;; rcount++) {
  574. n = read(fdf, buf, DEFB);
  575. if(n <= 0)
  576. break;
  577. n1 = write(fdt, buf, n);
  578. if(n1 != n) {
  579. fprint(2, "error writing %q: %r\n", to);
  580. rv = -1;
  581. break;
  582. }
  583. }
  584. if(n < 0) {
  585. fprint(2, "error reading %q: %r\n", from);
  586. rv = -1;
  587. }
  588. return rv;
  589. }
  590. static int
  591. opentemp(char *template)
  592. {
  593. int fd, i;
  594. char *p;
  595. p = estrdup(template);
  596. fd = -1;
  597. for(i=0; i<10; i++){
  598. mktemp(p);
  599. if((fd=create(p, ORDWR|OEXCL|ORCLOSE, 0000)) >= 0)
  600. break;
  601. strcpy(p, template);
  602. }
  603. if(fd < 0)
  604. return -1;
  605. strcpy(template, p);
  606. free(p);
  607. return fd;
  608. }
  609. int
  610. copyfile(char *local, char *remote, char *name, Dir *d, int dowstat, int *printerror)
  611. {
  612. Dir *d0, *d1, *dl;
  613. Dir nd;
  614. int rfd, tfd, wfd, didcreate;
  615. char tmp[32], *p, *safe;
  616. char err[ERRMAX];
  617. Again:
  618. *printerror = 0;
  619. if((rfd = open(remote, OREAD)) < 0)
  620. return -1;
  621. d0 = dirfstat(rfd);
  622. if(d0 == nil){
  623. close(rfd);
  624. return -1;
  625. }
  626. *printerror = 1;
  627. if(!tempspool){
  628. tfd = rfd;
  629. goto DoCopy;
  630. }
  631. strcpy(tmp, "/tmp/replicaXXXXXXXX");
  632. tfd = opentemp(tmp);
  633. if(tfd < 0){
  634. close(rfd);
  635. free(d0);
  636. return -1;
  637. }
  638. if(copy1(rfd, tfd, remote, tmp) < 0 || (d1 = dirfstat(rfd)) == nil){
  639. close(rfd);
  640. close(tfd);
  641. free(d0);
  642. return -1;
  643. }
  644. close(rfd);
  645. if(d0->qid.path != d1->qid.path
  646. || d0->qid.vers != d1->qid.vers
  647. || d0->mtime != d1->mtime
  648. || d0->length != d1->length){
  649. /* file changed underfoot; go around again */
  650. close(tfd);
  651. free(d0);
  652. free(d1);
  653. goto Again;
  654. }
  655. free(d1);
  656. if(seek(tfd, 0, 0) != 0){
  657. close(tfd);
  658. free(d0);
  659. return -1;
  660. }
  661. DoCopy:
  662. /*
  663. * clumsy but important hack to do safeinstall-like installs.
  664. */
  665. p = strchr(name, '/');
  666. if(safeinstall && p && strncmp(p, "/bin/", 5) == 0 && access(local, AEXIST) >= 0){
  667. /*
  668. * remove bin/_targ
  669. */
  670. safe = emalloc(strlen(local)+2);
  671. strcpy(safe, local);
  672. p = strrchr(safe, '/')+1;
  673. memmove(p+1, p, strlen(p)+1);
  674. p[0] = '_';
  675. remove(safe); /* ignore failure */
  676. /*
  677. * rename bin/targ to bin/_targ
  678. */
  679. nulldir(&nd);
  680. nd.name = p;
  681. if(dirwstat(local, &nd) < 0)
  682. fprint(2, "warning: rename %s to %s: %r\n", local, p);
  683. }
  684. didcreate = 0;
  685. if((dl = dirstat(local)) == nil){
  686. if((wfd = create(local, OWRITE, 0)) >= 0){
  687. didcreate = 1;
  688. goto okay;
  689. }
  690. goto err;
  691. }else{
  692. if((wfd = open(local, OTRUNC|OWRITE)) >= 0)
  693. goto okay;
  694. rerrstr(err, sizeof err);
  695. if(strstr(err, "permission") == nil)
  696. goto err;
  697. nulldir(&nd);
  698. /*
  699. * Assume the person running pull is in the appropriate
  700. * groups. We could set 0666 instead, but I'm worried
  701. * about leaving the file world-readable or world-writable
  702. * when it shouldn't be.
  703. */
  704. nd.mode = dl->mode | 0660;
  705. if(nd.mode == dl->mode)
  706. goto err;
  707. if(dirwstat(local, &nd) < 0)
  708. goto err;
  709. if((wfd = open(local, OTRUNC|OWRITE)) >= 0){
  710. nd.mode = dl->mode;
  711. if(dirfwstat(wfd, &nd) < 0)
  712. fprint(2, "warning: set mode on %s to 0660 to open; cannot set back to %luo: %r\n", local, nd.mode);
  713. goto okay;
  714. }
  715. nd.mode = dl->mode;
  716. if(dirwstat(local, &nd) < 0)
  717. 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);
  718. goto err;
  719. }
  720. err:
  721. close(tfd);
  722. free(d0);
  723. free(dl);
  724. return -1;
  725. okay:
  726. free(dl);
  727. if(copy1(tfd, wfd, tmp, local) < 0){
  728. close(tfd);
  729. close(wfd);
  730. free(d0);
  731. return -1;
  732. }
  733. close(tfd);
  734. if(didcreate || dowstat){
  735. nulldir(&nd);
  736. nd.mode = d->mode;
  737. if(dirfwstat(wfd, &nd) < 0)
  738. fprint(2, "warning: cannot set mode on %s\n", local);
  739. nulldir(&nd);
  740. nd.gid = d->gid;
  741. if(dirfwstat(wfd, &nd) < 0)
  742. fprint(2, "warning: cannot set gid on %s\n", local);
  743. if(douid){
  744. nulldir(&nd);
  745. nd.uid = d->uid;
  746. if(dirfwstat(wfd, &nd) < 0)
  747. fprint(2, "warning: cannot set uid on %s\n", local);
  748. }
  749. }
  750. d->mtime = d0->mtime;
  751. d->length = d0->length;
  752. nulldir(&nd);
  753. nd.mtime = d->mtime;
  754. if(dirfwstat(wfd, &nd) < 0)
  755. fprint(2, "warning: cannot set mtime on %s\n", local);
  756. free(d0);
  757. close(wfd);
  758. return 0;
  759. }
  760. /*
  761. * Applylog might try to overwrite itself.
  762. * To avoid problems with this, we copy ourselves
  763. * into /tmp and then re-exec.
  764. */
  765. char *rmargv0;
  766. static void
  767. rmself(void)
  768. {
  769. remove(rmargv0);
  770. }
  771. static int
  772. genopentemp(char *template, int mode, int perm)
  773. {
  774. int fd, i;
  775. char *p;
  776. p = estrdup(template);
  777. fd = -1;
  778. for(i=0; i<10; i++){
  779. mktemp(p);
  780. if(access(p, 0) < 0 && (fd=create(p, mode, perm)) >= 0)
  781. break;
  782. strcpy(p, template);
  783. }
  784. if(fd < 0)
  785. sysfatal("could not create temporary file");
  786. strcpy(template, p);
  787. free(p);
  788. return fd;
  789. }
  790. static void
  791. membogus(char **argv)
  792. {
  793. int n, fd, wfd;
  794. char template[50], buf[1024];
  795. if(strncmp(argv[0], "/tmp/_applylog_", 1+3+1+1+8+1)==0) {
  796. rmargv0 = argv[0];
  797. atexit(rmself);
  798. return;
  799. }
  800. if((fd = open(argv[0], OREAD)) < 0)
  801. return;
  802. strcpy(template, "/tmp/_applylog_XXXXXX");
  803. if((wfd = genopentemp(template, OWRITE, 0700)) < 0)
  804. return;
  805. while((n = read(fd, buf, sizeof buf)) > 0)
  806. if(write(wfd, buf, n) != n)
  807. goto Error;
  808. if(n != 0)
  809. goto Error;
  810. close(fd);
  811. close(wfd);
  812. argv[0] = template;
  813. exec(template, argv);
  814. fprint(2, "exec error %r\n");
  815. Error:
  816. close(fd);
  817. close(wfd);
  818. remove(template);
  819. return;
  820. }