-
Notifications
You must be signed in to change notification settings - Fork 1
/
lister.c
533 lines (506 loc) · 13.9 KB
/
lister.c
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*-
* Copyright (c) 2003-2006, Maxime Henrion <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "attrstack.h"
#include "config.h"
#include "fattr.h"
#include "globtree.h"
#include "lister.h"
#include "misc.h"
#include "mux.h"
#include "proto.h"
#include "status.h"
#include "stream.h"
/* Internal error codes. */
#define LISTER_ERR_WRITE (-1) /* Error writing to server. */
#define LISTER_ERR_STATUS (-2) /* Status file error in lstr->errmsg. */
struct lister {
struct config *config;
struct stream *wr;
char *errmsg;
};
static int lister_batch(struct lister *);
static int lister_coll(struct lister *, struct coll *, struct status *);
static int lister_dodirdown(struct lister *, struct coll *,
struct statusrec *, struct attrstack *as);
static int lister_dodirup(struct lister *, struct coll *,
struct statusrec *, struct attrstack *as);
static int lister_dofile(struct lister *, struct coll *,
struct statusrec *);
static int lister_dodead(struct lister *, struct coll *,
struct statusrec *);
static int lister_dorcs(struct lister *, struct coll *,
struct statusrec *, int);
void *
lister(void *arg)
{
struct thread_args *args;
struct lister lbuf, *l;
int error;
args = arg;
l = &lbuf;
l->config = args->config;
l->wr = args->wr;
l->errmsg = NULL;
#ifdef LISTER_DEBUG
error = stream_log(l->wr, "lister.log");
assert(!error);
#endif
error = lister_batch(l);
switch (error) {
case LISTER_ERR_WRITE:
xasprintf(&args->errmsg,
"TreeList failed: Network write failure: %s",
strerror(errno));
args->status = STATUS_TRANSIENTFAILURE;
break;
case LISTER_ERR_STATUS:
xasprintf(&args->errmsg,
"TreeList failed: %s. Delete it and try again.",
l->errmsg);
free(l->errmsg);
args->status = STATUS_FAILURE;
break;
default:
assert(error == 0);
args->status = STATUS_SUCCESS;
};
return (NULL);
}
static int
lister_batch(struct lister *l)
{
struct config *config;
struct stream *wr;
struct status *st;
struct coll *coll;
int error;
config = l->config;
wr = l->wr;
STAILQ_FOREACH(coll, &config->colls, co_next) {
if (coll->co_options & CO_SKIP)
continue;
st = status_open(coll, -1, &l->errmsg);
if (st == NULL)
return (LISTER_ERR_STATUS);
error = proto_printf(wr, "COLL %s %s\n", coll->co_name,
coll->co_release);
if (error)
return (LISTER_ERR_WRITE);
stream_flush(wr);
if (coll->co_options & CO_COMPRESS)
stream_filter_start(wr, STREAM_FILTER_ZLIB, NULL);
error = lister_coll(l, coll, st);
status_close(st, NULL);
if (error)
return (error);
if (coll->co_options & CO_COMPRESS)
stream_filter_stop(wr);
stream_flush(wr);
}
error = proto_printf(wr, ".\n");
if (error)
return (LISTER_ERR_WRITE);
return (0);
}
/* List a single collection based on the status file. */
static int
lister_coll(struct lister *l, struct coll *coll, struct status *st)
{
struct stream *wr;
struct attrstack *as;
struct statusrec *sr;
struct fattr *fa;
size_t i;
int depth, error, ret, prunedepth;
wr = l->wr;
depth = 0;
prunedepth = INT_MAX;
as = attrstack_new();
while ((ret = status_get(st, NULL, 0, 0, &sr)) == 1) {
switch (sr->sr_type) {
case SR_DIRDOWN:
depth++;
if (depth < prunedepth) {
error = lister_dodirdown(l, coll, sr, as);
if (error < 0)
goto bad;
if (error)
prunedepth = depth;
}
break;
case SR_DIRUP:
if (depth < prunedepth) {
error = lister_dodirup(l, coll, sr, as);
if (error)
goto bad;
} else if (depth == prunedepth) {
/* Finished pruning. */
prunedepth = INT_MAX;
}
depth--;
continue;
case SR_CHECKOUTLIVE:
if (depth < prunedepth) {
error = lister_dofile(l, coll, sr);
if (error)
goto bad;
}
break;
case SR_CHECKOUTDEAD:
if (depth < prunedepth) {
error = lister_dodead(l, coll, sr);
if (error)
goto bad;
}
break;
case SR_FILEDEAD:
case SR_FILELIVE:
if (depth < prunedepth) {
if (!(coll->co_options & CO_CHECKOUTMODE)) {
error = lister_dorcs(l, coll, sr,
sr->sr_type == SR_FILEDEAD);
if (error)
goto bad;
}
}
break;
}
}
if (ret == -1) {
l->errmsg = status_errmsg(st);
error = LISTER_ERR_STATUS;
goto bad;
}
assert(status_eof(st));
assert(depth == 0);
error = proto_printf(wr, ".\n");
if (error)
error = LISTER_ERR_WRITE;
bad:
for (i = 0; i < attrstack_size(as); i++) {
fa = attrstack_pop(as);
fattr_free(fa);
}
attrstack_free(as);
return (error);
}
/* Handle a directory up entry found in the status file. */
static int
lister_dodirdown(struct lister *l, struct coll *coll, struct statusrec *sr,
struct attrstack *as)
{
struct config *config;
struct stream *wr;
struct fattr *fa, *fa2;
char *path;
int error;
config = l->config;
wr = l->wr;
if (!globtree_test(coll->co_dirfilter, sr->sr_file))
return (1);
if (coll->co_options & CO_TRUSTSTATUSFILE) {
fa = fattr_new(FT_DIRECTORY, -1);
} else {
xasprintf(&path, "%s/%s", coll->co_prefix, sr->sr_file);
fa = fattr_frompath(path, FATTR_NOFOLLOW);
if (fa == NULL) {
/* The directory doesn't exist, prune
* everything below it. */
free(path);
return (1);
}
if (fattr_type(fa) == FT_SYMLINK) {
fa2 = fattr_frompath(path, FATTR_FOLLOW);
if (fa2 != NULL && fattr_type(fa2) == FT_DIRECTORY) {
/* XXX - When not in checkout mode, CVSup warns
* here about the file being a symlink to a
* directory instead of a directory. */
fattr_free(fa);
fa = fa2;
} else {
fattr_free(fa2);
}
fa2 = NULL;
}
free(path);
}
if (fattr_type(fa) != FT_DIRECTORY) {
fattr_free(fa);
/* Report it as something bogus so
* that it will be replaced. */
error = proto_printf(wr, "F %s %A\n", pathlast(sr->sr_file),
fattr_bogus, config->fasupport, coll->co_attrignore);
if (error)
return (LISTER_ERR_WRITE);
return (1);
}
/* It really is a directory. */
attrstack_push(as, fa);
error = proto_printf(wr, "D %s\n", pathlast(sr->sr_file));
if (error)
return (LISTER_ERR_WRITE);
return (0);
}
/* Handle a directory up entry found in the status file. */
static int
lister_dodirup(struct lister *l, struct coll *coll, struct statusrec *sr,
struct attrstack *as)
{
struct config *config;
const struct fattr *sendattr;
struct stream *wr;
struct fattr *fa, *fa2;
int error;
config = l->config;
wr = l->wr;
fa = attrstack_pop(as);
if (coll->co_options & CO_TRUSTSTATUSFILE) {
fattr_free(fa);
fa = sr->sr_clientattr;
}
fa2 = sr->sr_clientattr;
if (fattr_equal(fa, fa2))
sendattr = fa;
else
sendattr = fattr_bogus;
error = proto_printf(wr, "U %A\n", sendattr, config->fasupport,
coll->co_attrignore);
if (error)
return (LISTER_ERR_WRITE);
if (!(coll->co_options & CO_TRUSTSTATUSFILE))
fattr_free(fa);
/* XXX CVSup flushes here for some reason with a comment saying
"Be smarter". We don't flush when listing other file types. */
stream_flush(wr);
return (0);
}
/* Handle a checkout live entry found in the status file. */
static int
lister_dofile(struct lister *l, struct coll *coll, struct statusrec *sr)
{
struct config *config;
struct stream *wr;
const struct fattr *sendattr, *fa;
struct fattr *fa2, *rfa;
char *path, *spath;
int error;
if (!globtree_test(coll->co_filefilter, sr->sr_file))
return (0);
config = l->config;
wr = l->wr;
rfa = NULL;
sendattr = NULL;
error = 0;
if (!(coll->co_options & CO_TRUSTSTATUSFILE)) {
path = checkoutpath(coll->co_prefix, sr->sr_file);
if (path == NULL) {
spath = coll_statuspath(coll);
xasprintf(&l->errmsg, "Error in \"%s\": "
"Invalid filename \"%s\"", spath, sr->sr_file);
free(spath);
return (LISTER_ERR_STATUS);
}
rfa = fattr_frompath(path, FATTR_NOFOLLOW);
free(path);
if (rfa == NULL) {
/*
* According to the checkouts file we should have
* this file but we don't. Maybe the user deleted
* the file, or maybe the checkouts file is wrong.
* List the file with bogus attributes to cause the
* server to get things back in sync again.
*/
sendattr = fattr_bogus;
goto send;
}
fa = rfa;
} else {
fa = sr->sr_clientattr;
}
fa2 = fattr_forcheckout(sr->sr_serverattr, coll->co_umask);
if (!fattr_equal(fa, sr->sr_clientattr) || !fattr_equal(fa, fa2) ||
strcmp(coll->co_tag, sr->sr_tag) != 0 ||
strcmp(coll->co_date, sr->sr_date) != 0) {
/*
* The file corresponds to the information we have
* recorded about it, and its moded is correct for
* the requested umask setting.
*/
sendattr = fattr_bogus;
} else {
/*
* Either the file has been touched, or we are asking
* for a different revision than the one we recorded
* information about, or its mode isn't right (because
* it was last updated using a version of CVSup that
* wasn't so strict about modes).
*/
sendattr = sr->sr_serverattr;
}
fattr_free(fa2);
if (rfa != NULL)
fattr_free(rfa);
send:
error = proto_printf(wr, "F %s %A\n", pathlast(sr->sr_file), sendattr,
config->fasupport, coll->co_attrignore);
if (error)
return (LISTER_ERR_WRITE);
return (0);
}
/* Handle a checkout dead entry found in the status file. */
static int
lister_dodead(struct lister *l, struct coll *coll, struct statusrec *sr)
{
struct config *config;
struct stream *wr;
const struct fattr *sendattr;
struct fattr *fa;
char *path, *spath;
int error;
if (!globtree_test(coll->co_filefilter, sr->sr_file))
return (0);
config = l->config;
wr = l->wr;
if (!(coll->co_options & CO_TRUSTSTATUSFILE)) {
path = checkoutpath(coll->co_prefix, sr->sr_file);
if (path == NULL) {
spath = coll_statuspath(coll);
xasprintf(&l->errmsg, "Error in \"%s\": "
"Invalid filename \"%s\"", spath, sr->sr_file);
free(spath);
return (LISTER_ERR_STATUS);
}
fa = fattr_frompath(path, FATTR_NOFOLLOW);
free(path);
if (fa != NULL && fattr_type(fa) != FT_DIRECTORY) {
/*
* We shouldn't have this file but we do. Report
* it to the server, which will either send a
* deletion request, of (if the file has come alive)
* sent the correct version.
*/
fattr_free(fa);
error = proto_printf(wr, "F %s %A\n",
pathlast(sr->sr_file), fattr_bogus,
config->fasupport, coll->co_attrignore);
if (error)
return (LISTER_ERR_WRITE);
return (0);
}
if (fa != NULL)
fattr_free(fa);
}
if (strcmp(coll->co_tag, sr->sr_tag) != 0 ||
strcmp(coll->co_date, sr->sr_date) != 0)
sendattr = fattr_bogus;
else
sendattr = sr->sr_serverattr;
error = proto_printf(wr, "f %s %A\n", pathlast(sr->sr_file), sendattr,
config->fasupport, coll->co_attrignore);
if (error)
return (LISTER_ERR_WRITE);
return (0);
}
/* Handle a rcs file (live or dead) entry found in the status file. */
static int
lister_dorcs(struct lister *l, struct coll *coll, struct statusrec *sr,
int isdead)
{
struct config *config;
struct stream *wr;
char sendcmd;
const struct fattr *sendattr;
struct fattr *fa;
char *path, *spath;
size_t len;
int error;
if (!isdead)
sendcmd = 'F';
else
sendcmd = 'f';
if (!globtree_test(coll->co_filefilter, sr->sr_file))
return (0);
config = l->config;
wr = l->wr;
fa = NULL;
if (!(coll->co_options & CO_TRUSTSTATUSFILE)) {
path = cvspath(coll->co_prefix, sr->sr_file, isdead);
if (path == NULL) {
spath = coll_statuspath(coll);
xasprintf(&l->errmsg, "Error in \"%s\": "
"Invalid filename \"%s\"", spath, sr->sr_file);
free(spath);
return (LISTER_ERR_STATUS);
}
fa = fattr_frompath(path, FATTR_NOFOLLOW);
free(path);
if (fa == NULL) {
/*
* According to the checkouts file we should have
* this file but we don't. Maybe the user deleted
* the file, or maybe the checkouts file is wrong.
* List the file with bogus attributes to cause the
* server to get things back in sync again.
*
* The sendattr variable will be set to fattr_bogus
* later on by virtue of fa being NULL.
*/
sendcmd = 'F';
}
} else
fa = sr->sr_clientattr;
if (fa != NULL && fattr_equal(fa, sr->sr_clientattr)) {
/*
* If the file is an RCS file, we use "loose" equality, so sizes
* may disagress because of differences in whitespace.
*/
if (isrcs(sr->sr_file, &len) &&
!(coll->co_options & CO_NORCS) &&
!(coll->co_options & CO_STRICTCHECKRCS)) {
fattr_maskout(fa, FA_SIZE);
}
sendattr = fa;
} else {
/*
* If different, the user may have changed it, so we report
* bogus attributes to force a full comparison.
*/
sendattr = fattr_bogus;
}
error = proto_printf(wr, "%c %s %A\n", sendcmd, pathlast(sr->sr_file),
sendattr, config->fasupport, coll->co_attrignore);
if (fa != NULL && fa != sr->sr_clientattr)
fattr_free(fa);
if (error)
return (LISTER_ERR_WRITE);
return (0);
}