applylog.c 21 KB

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