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