summaryrefslogtreecommitdiff
path: root/sys/src/cmd/replica/updatedb.c
blob: d63a53c0f22253b16d854534277015fef3877811 (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
/*
 * generate a list of files and their metadata
 * using a given proto file.
 */
#include "all.h"

int changesonly;
char *uid;
Db *db;
Biobuf blog;
ulong now;
int n;
char **x;
int nx;
int justlog;
char *root=".";
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(int c, char *name, Dir *d)
{
	char *dname;

	dname = d->name;
	if(strcmp(dname, name) == 0)
		dname = "-";
	if(!justlog)
		Bprint(&blog, "%lud %d ", now, n++);
	Bprint(&blog, "%c %q %q %luo %q %q %lud %lld\n",
		c, name, dname, d->mode, uid ? uid : d->uid, d->gid, d->mtime, d->length);
}

void
walk(char *new, char *old, Dir *xd, void*)
{
	int i, change, len;
	Dir od, d;

	new = unroot(new, "/");
	old = unroot(old, root);

	if(!ismatch(new))
		return;
	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 = *xd;
	d.name = old;
	memset(&od, 0, sizeof od);
	change = 0;
	if(markdb(db, new, &od) < 0){
		if(!changesonly){
			xlog('a', new, &d);
			change = 1;
		}
	}else{
		if((d.mode&DMDIR)==0 && (od.mtime!=d.mtime || od.length!=d.length)){
			xlog('c', new, &d);
			change = 1;
		}
		if((!uid&&strcmp(od.uid,d.uid)!=0)
		|| strcmp(od.gid,d.gid)!=0 
		|| od.mode!=d.mode){
			xlog('m', new, &d);
			change = 1;
		}
	}
	if(!justlog && change){
		if(uid)
			d.uid = uid;
		d.muid = "mark";	/* mark bit */
		insertdb(db, new, &d);
	}
}

void
warn(char *msg, void*)
{
	char *p;

	fprint(2, "warning: %s\n", msg);

	/* find the %r in "can't open foo: %r" */
	p = strstr(msg, ": ");
	if(p)
		p += 2;

	/*
	 * if the error is about a remote server failing,
	 * then there's no point in continuing to look
	 * for changes -- we'll think everything got deleted!
	 *
	 * actual errors i see are:
	 *	"i/o on hungup channel" for a local hangup
	 *	"i/o on hungup channel" for a timeout (yank the network wire)
	 *	"'/n/sources/plan9' Hangup" for a remote hangup
	 * the rest is paranoia.
	 */
	if(p){
		if(cistrstr(p, "hungup") || cistrstr(p, "Hangup")
		|| cistrstr(p, "rpc error")
		|| cistrstr(p, "shut down")
		|| cistrstr(p, "i/o")
		|| cistrstr(p, "connection"))
			sysfatal("suspected network or i/o error - bailing out");
	}
}

void
usage(void)
{
	fprint(2, "usage: replica/updatedb [-c] [-p proto] [-r root] [-t now n] [-u uid] [-x path]... db [paths]\n");
	exits("usage");
}

void
main(int argc, char **argv)
{
	char *proto;
	Avlwalk *w;
	Dir d;
	Entry *e;

	quotefmtinstall();
	proto = "/sys/lib/sysconfig/proto/allproto";
	now = time(0);
	Binit(&blog, 1, OWRITE);
	ARGBEGIN{
	case 'c':
		changesonly = 1;
		break;
	case 'l':
		justlog = 1;
		break;
	case 'p':
		proto = EARGF(usage());
		break;
	case 'r':
		root = EARGF(usage());
		break;
	case 't':
		now = strtoul(EARGF(usage()), 0, 0);
		n = atoi(EARGF(usage()));
		break;
	case 'u':
		uid = EARGF(usage());
		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 <1)
		usage();

	match = argv+1;
	nmatch = argc-1;

	db = opendb(argv[0]);
	if(rdproto(proto, root, walk, warn, nil) < 0)
		sysfatal("rdproto: %r");

	if(!changesonly){
		w = avlwalk(db->avl);
		while(e = (Entry*)avlprev(w)){
			if(!ismatch(e->name))
				continue;
			if(!e->d.mark){		/* not visited during walk */
				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(!justlog)
					removedb(db, e->name);
			}
		}
	}

	if(Bterm(&blog) < 0)
		sysfatal("writing output: %r");

	exits(nil);
}