applylog.c 18 KB

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