applychanges.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * push changes from client to server.
  11. */
  12. #include "all.h"
  13. int douid;
  14. Db *db;
  15. char **x;
  16. int nx;
  17. int justshow;
  18. int verbose;
  19. int conflicts;
  20. char newpath[10000];
  21. char oldpath[10000];
  22. char *clientroot;
  23. char *serverroot;
  24. int copyfile(char*, char*, Dir*, int);
  25. int metafile(char*, Dir*);
  26. char **match;
  27. int nmatch;
  28. int
  29. ismatch(char *s)
  30. {
  31. int i, len;
  32. if(nmatch == 0)
  33. return 1;
  34. for(i=0; i<nmatch; i++){
  35. if(strcmp(s, match[i]) == 0)
  36. return 1;
  37. len = strlen(match[i]);
  38. if(strncmp(s, match[i], len) == 0 && s[len]=='/')
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. void
  44. xlog(char c, char *path, Dir *d)
  45. {
  46. if(!verbose)
  47. return;
  48. print("%c %s %luo %s %s %lud\n", c, path, d->mode, d->uid, d->gid, d->mtime);
  49. }
  50. void
  51. walk(char *new, char *old, Dir *pd, void*)
  52. {
  53. int i, len;
  54. Dir od, d;
  55. static Dir *xd;
  56. new = unroot(new, "/");
  57. old = unroot(old, serverroot);
  58. if(!ismatch(new))
  59. return;
  60. if(xd != nil){
  61. free(xd);
  62. xd = nil;
  63. }
  64. for(i=0; i<nx; i++){
  65. if(strcmp(new, x[i]) == 0)
  66. return;
  67. len = strlen(x[i]);
  68. if(strncmp(new, x[i], len)==0 && new[len]=='/')
  69. return;
  70. }
  71. d = *pd;
  72. d.name = old;
  73. memset(&od, 0, sizeof od);
  74. snprint(newpath, sizeof newpath, "%s/%s", clientroot, new);
  75. snprint(oldpath, sizeof oldpath, "%s/%s", serverroot, old);
  76. xd = dirstat(oldpath);
  77. if(markdb(db, new, &od) < 0){
  78. if(xd != nil){
  79. print("x %s create/create conflict\n", new);
  80. conflicts = 1;
  81. return;
  82. }
  83. xlog('a', new, &d);
  84. d.muid = "mark"; /* mark bit */
  85. if(!justshow){
  86. if(copyfile(newpath, oldpath, &d, 1) == 0)
  87. insertdb(db, new, &d);
  88. }
  89. }else{
  90. if((d.mode&DMDIR)==0 && od.mtime!=d.mtime){
  91. if(xd==nil){
  92. print("%s update/remove conflict\n", new);
  93. conflicts = 1;
  94. return;
  95. }
  96. if(xd->mtime != od.mtime){
  97. print("%s update/update conflict\n", new);
  98. conflicts = 1;
  99. return;
  100. }
  101. od.mtime = d.mtime;
  102. od.muid = "mark";
  103. xlog('c', new, &od);
  104. if(!justshow){
  105. if(copyfile(newpath, oldpath, &od, 0) == 0)
  106. insertdb(db, new, &od);
  107. }
  108. }
  109. if((douid&&strcmp(od.uid,d.uid)!=0)
  110. || strcmp(od.gid,d.gid)!=0
  111. || od.mode!=d.mode){
  112. if(xd==nil){
  113. print("%s metaupdate/remove conflict\n", new);
  114. conflicts = 1;
  115. return;
  116. }
  117. if((douid&&strcmp(od.uid,xd->uid)!=0)
  118. || strcmp(od.uid,xd->gid)!=0
  119. || od.mode!=xd->mode){
  120. print("%s metaupdate/metaupdate conflict\n", new);
  121. conflicts = 1;
  122. return;
  123. }
  124. if(douid)
  125. od.uid = d.uid;
  126. od.gid = d.gid;
  127. od.mode = d.mode;
  128. od.muid = "mark";
  129. xlog('m', new, &od);
  130. if(!justshow){
  131. if(metafile(oldpath, &od) == 0)
  132. insertdb(db, new, &od);
  133. }
  134. }
  135. }
  136. }
  137. void
  138. usage(void)
  139. {
  140. fprint(2, "usage: replica/applychanges [-nuv] [-p proto] [-x path]... clientdb clientroot serverroot [path ...]\n");
  141. exits("usage");
  142. }
  143. void
  144. main(int argc, char **argv)
  145. {
  146. char *proto;
  147. Avlwalk *w;
  148. Dir *xd, d;
  149. Entry *e;
  150. quotefmtinstall();
  151. proto = "/sys/lib/sysconfig/proto/allproto";
  152. ARGBEGIN{
  153. case 'n':
  154. justshow = 1;
  155. verbose = 1;
  156. break;
  157. case 'p':
  158. proto = EARGF(usage());
  159. break;
  160. case 'u':
  161. douid = 1;
  162. break;
  163. case 'v':
  164. verbose = 1;
  165. break;
  166. case 'x':
  167. if(nx%16 == 0)
  168. x = erealloc(x, (nx+16)*sizeof(x[0]));
  169. x[nx++] = EARGF(usage());
  170. break;
  171. default:
  172. usage();
  173. }ARGEND
  174. if(argc < 3)
  175. usage();
  176. db = opendb(argv[0]);
  177. clientroot = argv[1];
  178. serverroot = argv[2];
  179. match = argv+3;
  180. nmatch = argc-3;
  181. if(revrdproto(proto, clientroot, serverroot, walk, nil, nil) < 0)
  182. sysfatal("rdproto: %r");
  183. w = avlwalk(db->avl);
  184. while(e = (Entry*)avlprev(w)){
  185. if(!ismatch(e->name))
  186. continue;
  187. if(!e->d.mark){ /* not visited during walk */
  188. snprint(newpath, sizeof newpath, "%s/%s", clientroot, e->name);
  189. snprint(oldpath, sizeof oldpath, "%s/%s", serverroot, e->d.name);
  190. xd = dirstat(oldpath);
  191. if(xd == nil){
  192. removedb(db, e->name);
  193. continue;
  194. }
  195. if(xd->mtime != e->d.mtime && (e->d.mode&xd->mode&DMDIR)==0){
  196. print("x %q remove/update conflict\n", e->name);
  197. free(xd);
  198. continue;
  199. }
  200. memset(&d, 0, sizeof d);
  201. d.name = e->d.name;
  202. d.uid = e->d.uid;
  203. d.gid = e->d.gid;
  204. d.mtime = e->d.mtime;
  205. d.mode = e->d.mode;
  206. xlog('d', e->name, &d);
  207. if(!justshow){
  208. if(remove(oldpath) == 0)
  209. removedb(db, e->name);
  210. }
  211. free(xd);
  212. }
  213. }
  214. if(conflicts)
  215. exits("conflicts");
  216. exits(nil);
  217. }
  218. enum { DEFB = 8192 };
  219. static int
  220. copy1(int fdf, int fdt, char *from, char *to)
  221. {
  222. char buf[DEFB];
  223. int32_t n, n1, rcount;
  224. int rv;
  225. char err[ERRMAX];
  226. /* clear any residual error */
  227. err[0] = '\0';
  228. errstr(err, ERRMAX);
  229. rv = 0;
  230. for(rcount=0;; rcount++) {
  231. n = read(fdf, buf, DEFB);
  232. if(n <= 0)
  233. break;
  234. n1 = write(fdt, buf, n);
  235. if(n1 != n) {
  236. fprint(2, "error writing %q: %r\n", to);
  237. rv = -1;
  238. break;
  239. }
  240. }
  241. if(n < 0) {
  242. fprint(2, "error reading %q: %r\n", from);
  243. rv = -1;
  244. }
  245. return rv;
  246. }
  247. int
  248. copyfile(char *from, char *to, Dir *d, int dowstat)
  249. {
  250. Dir nd;
  251. int rfd, wfd, didcreate;
  252. if((rfd = open(from, OREAD)) < 0)
  253. return -1;
  254. didcreate = 0;
  255. if(d->mode&DMDIR){
  256. if((wfd = create(to, OREAD, DMDIR)) < 0){
  257. fprint(2, "mkdir %q: %r\n", to);
  258. close(rfd);
  259. return -1;
  260. }
  261. }else{
  262. if((wfd = open(to, OTRUNC|OWRITE)) < 0){
  263. if((wfd = create(to, OWRITE, 0)) < 0){
  264. close(rfd);
  265. return -1;
  266. }
  267. didcreate = 1;
  268. }
  269. if(copy1(rfd, wfd, from, to) < 0){
  270. close(rfd);
  271. close(wfd);
  272. return -1;
  273. }
  274. }
  275. close(rfd);
  276. if(didcreate || dowstat){
  277. nulldir(&nd);
  278. nd.mode = d->mode;
  279. if(dirfwstat(wfd, &nd) < 0)
  280. fprint(2, "warning: cannot set mode on %q\n", to);
  281. nulldir(&nd);
  282. nd.gid = d->gid;
  283. if(dirfwstat(wfd, &nd) < 0)
  284. fprint(2, "warning: cannot set gid on %q\n", to);
  285. if(douid){
  286. nulldir(&nd);
  287. nd.uid = d->uid;
  288. if(dirfwstat(wfd, &nd) < 0)
  289. fprint(2, "warning: cannot set uid on %q\n", to);
  290. }
  291. }
  292. nulldir(&nd);
  293. nd.mtime = d->mtime;
  294. if(dirfwstat(wfd, &nd) < 0)
  295. fprint(2, "warning: cannot set mtime on %q\n", to);
  296. close(wfd);
  297. return 0;
  298. }
  299. int
  300. metafile(char *path, Dir *d)
  301. {
  302. Dir nd;
  303. nulldir(&nd);
  304. nd.gid = d->gid;
  305. nd.mode = d->mode;
  306. if(douid)
  307. nd.uid = d->uid;
  308. if(dirwstat(path, &nd) < 0){
  309. fprint(2, "dirwstat %q: %r\n", path);
  310. return -1;
  311. }
  312. return 0;
  313. }