summaryrefslogtreecommitdiff
path: root/appl/cmd/install/updatelog.b
blob: d9c6959e7a8b554126491e10a0c3434e0a56657d (plain)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
implement Updatelog;

include "sys.m";
	sys: Sys;

include "draw.m";

include "bufio.m";
	bufio: Bufio;
	Iobuf: import bufio;

include "daytime.m";
	daytime: Daytime;

include "string.m";
	str: String;

include "keyring.m";
	kr: Keyring;

include "logs.m";
	logs: Logs;
	Db, Entry, Byname, Byseq: import logs;
	S, mkpath: import logs;
	Log: type Entry;

include "fsproto.m";
	fsproto: FSproto;
	Direntry: import fsproto;

include "arg.m";

Updatelog: module
{
	init:	fn(nil: ref Draw->Context, nil: list of string);
};

now: int;
gen := 0;
changesonly := 0;
uid: string;
gid: string;
debug := 0;
state: ref Db;
rootdir := ".";
scanonly: list of string;
exclude: list of string;
sums := 0;
stderr: ref Sys->FD;
Seen: con 1<<31;
bout: ref Iobuf;

init(nil: ref Draw->Context, args: list of string)
{
	sys = load Sys Sys->PATH;
	bufio = load Bufio Bufio->PATH;
	ensure(bufio, Bufio->PATH);
	fsproto = load FSproto FSproto->PATH;
	ensure(fsproto, FSproto->PATH);
	daytime = load Daytime Daytime->PATH;
	ensure(daytime, Daytime->PATH);
	str = load String String->PATH;
	ensure(str, String->PATH);
	logs = load Logs Logs->PATH;
	ensure(logs, Logs->PATH);
	kr = load Keyring Keyring->PATH;
	ensure(kr, Keyring->PATH);

	arg := load Arg Arg->PATH;
	if(arg == nil)
		error(sys->sprint("can't load %s: %r", Arg->PATH));

	protofile := "/lib/proto/all";
	arg->init(args);
	arg->setusage("updatelog [-p proto] [-r root] [-t now gen] [-c] [-x path] x.log [path ...]");
	while((o := arg->opt()) != 0)
		case o {
		'D' =>
			debug = 1;
		'p' =>
			protofile = arg->earg();
		'r' =>
			rootdir = arg->earg();
		'c' =>
			changesonly = 1;
		'u' =>
			uid = arg->earg();
		'g' =>
			gid = arg->earg();
		's' =>
			sums = 1;
		't' =>
			now = int arg->earg();
			gen = int arg->earg();
		'x' =>
			s := arg->earg();
			exclude = trimpath(s) :: exclude;
		* =>
			arg->usage();
		}
	args = arg->argv();
	if(args == nil)
		arg->usage();
	arg = nil;

	stderr = sys->fildes(2);
	bout = bufio->fopen(sys->fildes(1), Bufio->OWRITE);

	fsproto->init();
	logs->init(bufio);

	logfile := hd args;
	while((args = tl args) != nil)
		scanonly = trimpath(hd args) :: scanonly;
	checkroot(rootdir, "replica root");

	state = Db.new("server state");

	#
	# replay log to rebuild server state
	#
	logfd := sys->open(logfile, Sys->OREAD);
	if(logfd == nil)
		error(sys->sprint("can't open %s: %r", logfile));
	f := bufio->fopen(logfd, Sys->OREAD);
	if(f == nil)
		error(sys->sprint("can't open %s: %r", logfile));
	while((log := readlog(f)) != nil)
		replaylog(state, log);

	#
	# walk the set of names produced by the proto file, comparing against the server state
	#
	now = daytime->now();
	doproto(rootdir, protofile);

	if(changesonly){
		bout.flush();
		exit;
	}

	#
	# names in the original state that we didn't see in the walk must have been removed:
	# print 'd' log entries for them, in reverse lexicographic order (children before parents)
	#
	state.sort(Logs->Byname);
	for(i := state.nstate; --i >= 0;){
		e := state.state[i];
		if((e.x & Seen) == 0 && considered(e.path)){
			change('d', e, e.seq, e.d, e.path, e.serverpath, e.contents);	# TO DO: content
			if(debug)
				sys->fprint(sys->fildes(2), "remove %q\n", e.path);
		}
	}
	bout.flush();
}

ensure[T](m: T, path: string)
{
	if(m == nil)
		error(sys->sprint("can't load %s: %r", path));
}

checkroot(dir: string, what: string)
{
	(ok, d) := sys->stat(dir);
	if(ok < 0)
		error(sys->sprint("can't stat %s %q: %r", what, dir));
	if((d.mode & Sys->DMDIR) == 0)
		error(sys->sprint("%s %q: not a directory", what, dir));
}

considered(s: string): int
{
	if(scanonly != nil && !islisted(s, scanonly))
		return 0;
	return exclude == nil || !islisted(s, exclude);
}

readlog(in: ref Iobuf): ref Log
{
	(e, err) := Entry.read(in);
	if(err != nil)
		error(err);
	return e;
}

#
# replay a log to reach the state wrt files previously taken from the server
#
replaylog(db: ref Db, log: ref Log)
{
	e := db.look(log.path);
	indb := e != nil && !e.removed();
	case log.action {
	'a' =>	# add new file
		if(indb){
			note(sys->sprint("%q duplicate create", log.path));
			return;
		}
	'c' =>	# contents
		if(!indb){
			note(sys->sprint("%q contents but no entry", log.path));
			return;
		}
	'd' =>	# delete
		if(!indb){
			note(sys->sprint("%q deleted but no entry", log.path));
			return;
		}
		if(e.d.mtime > log.d.mtime){
			note(sys->sprint("%q deleted but it's newer", log.path));
			return;
		}
	'm' =>	# metadata
		if(!indb){
			note(sys->sprint("%q metadata but no entry", log.path));
			return;
		}
	* =>
		error(sys->sprint("bad log entry: %bd %bd", log.seq>>32, log.seq & big 16rFFFFFFFF));
	}
	update(db, e, log);
}

#
# update file state e to reflect the effect of the log,
# creating a new entry if necessary
#
update(db: ref Db, e: ref Entry, log: ref Entry)
{
	if(e == nil)
		e = db.entry(log.seq, log.path, log.d);
	e.update(log);
}

doproto(tree: string, protofile: string)
{
	entries := chan of Direntry;
	warnings := chan of (string, string);
	err := fsproto->readprotofile(protofile, tree, entries, warnings);
	if(err != nil)
		error(sys->sprint("can't read %s: %s", protofile, err));
	for(;;)alt{
	(old, new, d) := <-entries =>
		if(d == nil)
			return;
		if(debug)
			sys->fprint(stderr, "old=%q new=%q length=%bd\n", old, new, d.length);
		while(new != nil && new[0] == '/')
			new = new[1:];
		if(!considered(new))
			continue;
		if(sums && (d.mode & Sys->DMDIR) == 0)
			digests := md5sum(old) :: nil;
		if(uid != nil)
			d.uid = uid;
		if(gid != nil)
			d.gid = gid;
		old = relative(old, rootdir);
		db := state.look(new);
		if(db == nil){
			if(!changesonly){
				db = state.entry(nextseq(), new, *d);
				change('a', db, db.seq, db.d, db.path, old, digests);
			}
		}else{
			if(!samestat(db.d, *d))
				change('c', db, nextseq(), *d, new, old, digests);
			if(!samemeta(db.d, *d))
				change('m', db, nextseq(), *d, new, old, nil);	# need digest?
		}
		if(db != nil)
			db.x |= Seen;
	(old, msg) := <-warnings =>
		#if(contains(msg, "entry not found") || contains(msg, "not exist"))
		#	break;
		sys->fprint(sys->fildes(2), "updatelog: warning[old=%s]: %s\n", old, msg);
	}
}

change(action: int, e: ref Entry, seq: big, d: Sys->Dir, path: string, serverpath: string, digests: list of string)
{
	log := ref Entry;
	log.seq = seq;
	log.action = action;
	log.d = d;
	log.path = path;
	log.serverpath = serverpath;
	log.contents = digests;
	e.update(log);
	bout.puts(log.logtext()+"\n");
}

samestat(a: Sys->Dir, b: Sys->Dir): int
{
	# doesn't check permission/ownership, does check QTDIR/QTFILE
	if(a.mode & Sys->DMDIR)
		return (b.mode & Sys->DMDIR) != 0;
	return a.length == b.length && a.mtime == b.mtime && a.qid.qtype == b.qid.qtype;	# TO DO: a.name==b.name?
}

samemeta(a: Sys->Dir, b: Sys->Dir): int
{
	return a.mode == b.mode && (uid == nil || a.uid == b.uid) && (gid == nil || a.gid == b.gid) && samestat(a, b);
}

nextseq(): big
{
	return (big now << 32) | big gen++;
}

error(s: string)
{
	sys->fprint(sys->fildes(2), "updatelog: %s\n", s);
	raise "fail:error";
}

note(s: string)
{
	sys->fprint(sys->fildes(2), "updatelog: note: %s\n", s);
}

contains(s: string, sub: string): int
{
	return str->splitstrl(s, sub).t1 != nil;
}

isprefix(a, b: string): int
{
	la := len a;
	lb := len b;
	if(la > lb)
		return 0;
	if(la == lb)
		return a == b;
	return a == b[0:la] && b[la] == '/';
}

trimpath(s: string): string
{
	while(len s > 1 && s[len s-1] == '/')
		s = s[0:len s-1];
	while(s != nil && s[0] == '/')
		s = s[1:];
	return s;
}

relative(name: string, root: string): string
{
	if(root == nil || name == nil)
		return name;
	if(isprefix(root, name)){
		name = name[len root:];
		while(name != nil && name[0] == '/')
			name = name[1:];
	}
	return name;
}

islisted(s: string, l: list of string): int
{
	for(; l != nil; l = tl l)
		if(isprefix(hd l, s))
			return 1;
	return 0;
}

md5sum(file: string): string
{
	fd := sys->open(file, Sys->OREAD);
	if(fd == nil)
		error(sys->sprint("can't open %s: %r", file));
	ds: ref Keyring->DigestState;
	buf := array[Sys->ATOMICIO] of byte;
	while((n := sys->read(fd, buf, len buf)) > 0)
		ds = kr->md5(buf, n, nil, ds);
	if(n < 0)
		error(sys->sprint("error reading %s: %r", file));
	digest := array[Keyring->MD5dlen] of byte;
	kr->md5(nil, 0, digest, ds);
	s: string;
	for(i := 0; i < len digest; i++)
		s += sys->sprint("%.2ux", int digest[i]);
	return s;
}