-
Notifications
You must be signed in to change notification settings - Fork 1
/
updater.c
2091 lines (1958 loc) · 54.8 KB
/
updater.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* 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 <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
#include "diff.h"
#include "fattr.h"
#include "fixups.h"
#include "keyword.h"
#include "updater.h"
#include "misc.h"
#include "mux.h"
#include "proto.h"
#include "rcsfile.h"
#include "status.h"
#include "stream.h"
/* Internal error codes. */
#define UPDATER_ERR_PROTO (-1) /* Protocol error. */
#define UPDATER_ERR_MSG (-2) /* Error is in updater->errmsg. */
#define UPDATER_ERR_READ (-3) /* Error reading from server. */
#define UPDATER_ERR_DELETELIM (-4) /* File deletion limit exceeded. */
#define BUFSIZE 4096
/* Everything needed to update a file. */
struct file_update {
struct statusrec srbuf;
char *destpath;
char *temppath;
char *origpath;
char *coname; /* Points somewhere in destpath. */
char *wantmd5;
int isfixup;
int free_wantmd5; /* Indicates if we need to free the wantmd5
field in fup_reset(). */
struct coll *coll;
struct status *st;
/* Those are only used for diff updating. */
char *author;
struct stream *orig;
struct stream *to;
int attic;
int expand;
};
struct updater {
struct config *config;
struct stream *rd;
char *errmsg;
int deletecount;
};
static void fup_init(struct file_update *, struct coll *, struct status *);
static int fup_prepare(struct file_update *, char *, int);
static void fup_reset(struct file_update *);
static void updater_prunedirs(char *, char *);
static int updater_batch(struct updater *, int);
static int updater_docoll(struct updater *, struct file_update *, int);
static int updater_delete(struct updater *, struct file_update *);
static void updater_deletefile(const char *);
static int updater_checkout(struct updater *, struct file_update *);
static int updater_addfile(struct updater *, struct file_update *);
static int updater_addelta(struct rcsfile *rf, struct stream *rd,
char *revnum, char *diffbase, char *revdate, char *author);
static int updater_setattrs(struct updater *, struct file_update *,
char *, char *, char *, char *, char *, struct fattr *);
static int updater_setdirattrs(struct updater *, struct coll *,
struct file_update *, char *, char *);
static int updater_updatefile(struct updater *, struct file_update *fup,
const char *);
static int updater_updatenode(struct updater *, struct coll *,
struct file_update *, char *);
static int updater_diff(struct updater *, struct file_update *);
static int updater_diff_batch(struct updater *, struct file_update *);
static int updater_diff_apply(struct updater *, struct file_update *,
char *);
static void updater_openrcs(struct rcsfile **, struct file_update *,
const char *, const char *);
static int updater_rcsedit(struct updater *, struct file_update *, char *,
char *);
int updater_append_file(struct updater *, struct file_update *,
off_t);
static int updater_rsync(struct updater *, struct file_update *, size_t);
static int updater_read_checkout(struct stream *, struct stream *);
static void
fup_init(struct file_update *fup, struct coll *coll, struct status *st)
{
memset(fup, 0, sizeof(*fup));
fup->coll = coll;
fup->st = st;
fup->free_wantmd5 = 1;
}
static int
fup_prepare(struct file_update *fup, char *name, int attic)
{
struct coll *coll;
coll = fup->coll;
fup->attic = 0;
fup->origpath = NULL;
fup->free_wantmd5 = 1;
if (coll->co_options & CO_CHECKOUTMODE)
fup->destpath = checkoutpath(coll->co_prefix, name);
else {
fup->destpath = cvspath(coll->co_prefix, name, attic);
fup->origpath = atticpath(coll->co_prefix, name);
/* If they're equal, we don't need special care. */
if (fup->origpath != NULL &&
strcmp(fup->origpath, fup->destpath) == 0) {
free(fup->origpath);
fup->origpath = NULL;
}
fup->attic = attic;
}
if (fup->destpath == NULL)
return (-1);
fup->coname = fup->destpath + coll->co_prefixlen + 1;
return (0);
}
/* Called after each file update to reinit the structure. */
static void
fup_reset(struct file_update *fup)
{
struct statusrec *sr;
sr = &fup->srbuf;
if (fup->destpath != NULL) {
free(fup->destpath);
fup->destpath = NULL;
}
if (fup->temppath != NULL) {
free(fup->temppath);
fup->temppath = NULL;
}
if (fup->origpath != NULL) {
free(fup->origpath);
fup->origpath = NULL;
}
fup->coname = NULL;
if (fup->author != NULL) {
free(fup->author);
fup->author = NULL;
}
fup->expand = 0;
if (fup->wantmd5 != NULL) {
if (fup->free_wantmd5)
free(fup->wantmd5);
fup->wantmd5 = NULL;
}
fup->free_wantmd5 = 1;
fup->isfixup = 0;
if (fup->orig != NULL) {
stream_close(fup->orig);
fup->orig = NULL;
}
if (fup->to != NULL) {
stream_close(fup->to);
fup->to = NULL;
}
if (sr->sr_file != NULL)
free(sr->sr_file);
if (sr->sr_tag != NULL)
free(sr->sr_tag);
if (sr->sr_date != NULL)
free(sr->sr_date);
if (sr->sr_revnum != NULL)
free(sr->sr_revnum);
if (sr->sr_revdate != NULL)
free(sr->sr_revdate);
fattr_free(sr->sr_clientattr);
fattr_free(sr->sr_serverattr);
memset(sr, 0, sizeof(*sr));
}
void *
updater(void *arg)
{
struct thread_args *args;
struct updater upbuf, *up;
int error;
args = arg;
up = &upbuf;
up->config = args->config;
up->rd = args->rd;
up->errmsg = NULL;
up->deletecount = 0;
#ifdef UPDATER_DEBUG
error = stream_log(up->rd, "updater.log");
assert(!error);
#endif
error = updater_batch(up, 0);
/*
* Make sure to close the fixups even in case of an error,
* so that the detailer thread doesn't block indefinitely.
*/
fixups_close(up->config->fixups);
if (!error)
error = updater_batch(up, 1);
switch (error) {
case UPDATER_ERR_PROTO:
xasprintf(&args->errmsg, "Updater failed: Protocol error");
args->status = STATUS_FAILURE;
break;
case UPDATER_ERR_MSG:
xasprintf(&args->errmsg, "Updater failed: %s", up->errmsg);
free(up->errmsg);
args->status = STATUS_FAILURE;
break;
case UPDATER_ERR_READ:
if (stream_eof(up->rd)) {
xasprintf(&args->errmsg, "Updater failed: "
"Premature EOF from server");
} else {
xasprintf(&args->errmsg, "Updater failed: "
"Network read failure: %s", strerror(errno));
}
args->status = STATUS_TRANSIENTFAILURE;
break;
case UPDATER_ERR_DELETELIM:
xasprintf(&args->errmsg, "Updater failed: "
"File deletion limit exceeded");
args->status = STATUS_FAILURE;
break;
default:
assert(error == 0);
args->status = STATUS_SUCCESS;
};
return (NULL);
}
static int
updater_batch(struct updater *up, int isfixups)
{
struct stream *rd;
struct coll *coll;
struct status *st;
struct file_update fup;
char *line, *cmd, *errmsg, *collname, *release;
int error;
rd = up->rd;
STAILQ_FOREACH(coll, &up->config->colls, co_next) {
if (coll->co_options & CO_SKIP)
continue;
umask(coll->co_umask);
line = stream_getln(rd, NULL);
if (line == NULL)
return (UPDATER_ERR_READ);
cmd = proto_get_ascii(&line);
collname = proto_get_ascii(&line);
release = proto_get_ascii(&line);
if (release == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
if (strcmp(cmd, "COLL") != 0 ||
strcmp(collname, coll->co_name) != 0 ||
strcmp(release, coll->co_release) != 0)
return (UPDATER_ERR_PROTO);
if (!isfixups)
lprintf(1, "Updating collection %s/%s\n", coll->co_name,
coll->co_release);
if (coll->co_options & CO_COMPRESS)
stream_filter_start(rd, STREAM_FILTER_ZLIB, NULL);
st = status_open(coll, coll->co_scantime, &errmsg);
if (st == NULL) {
up->errmsg = errmsg;
return (UPDATER_ERR_MSG);
}
fup_init(&fup, coll, st);
error = updater_docoll(up, &fup, isfixups);
status_close(st, &errmsg);
fup_reset(&fup);
if (errmsg != NULL) {
/* Discard previous error. */
if (up->errmsg != NULL)
free(up->errmsg);
up->errmsg = errmsg;
return (UPDATER_ERR_MSG);
}
if (error)
return (error);
if (coll->co_options & CO_COMPRESS)
stream_filter_stop(rd);
}
line = stream_getln(rd, NULL);
if (line == NULL)
return (UPDATER_ERR_READ);
if (strcmp(line, ".") != 0)
return (UPDATER_ERR_PROTO);
return (0);
}
static int
updater_docoll(struct updater *up, struct file_update *fup, int isfixups)
{
struct stream *rd;
struct coll *coll;
struct statusrec srbuf, *sr;
struct fattr *rcsattr, *tmp;
char *attr, *blocksize, *line, *msg;
char *name, *tag, *date, *revdate;
char *expand, *wantmd5, *revnum;
char *rcsopt, *pos;
time_t t;
off_t position;
int attic, cmd, error, needfixupmsg;
cmd = -1;
error = 0;
rd = up->rd;
coll = fup->coll;
needfixupmsg = isfixups;
while ((line = stream_getln(rd, NULL)) != NULL) {
cmd = proto_get_char(&line);
if (cmd == '.' && line == NULL)
break;
memset(&srbuf, 0, sizeof(srbuf));
if (needfixupmsg) {
lprintf(1, "Applying fixups for collection %s/%s\n",
coll->co_name, coll->co_release);
needfixupmsg = 0;
}
switch (cmd) {
case 'T':
/* Update recorded information for checked-out file. */
name = proto_get_ascii(&line);
tag = proto_get_ascii(&line);
date = proto_get_ascii(&line);
revnum = proto_get_ascii(&line);
revdate = proto_get_ascii(&line);
attr = proto_get_ascii(&line);
if (attr == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
rcsattr = fattr_decode(attr);
if (rcsattr == NULL)
return (UPDATER_ERR_PROTO);
error = fup_prepare(fup, name, 0);
if (error)
return (UPDATER_ERR_PROTO);
error = updater_setattrs(up, fup, name, tag, date,
revnum, revdate, rcsattr);
fattr_free(rcsattr);
if (error)
return (error);
break;
case 'c':
/* Checkout dead file. */
name = proto_get_ascii(&line);
tag = proto_get_ascii(&line);
date = proto_get_ascii(&line);
attr = proto_get_ascii(&line);
if (attr == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
error = fup_prepare(fup, name, 0);
if (error)
return (UPDATER_ERR_PROTO);
/* Theoritically, the file does not exist on the client.
Just to make sure, we'll delete it here, if it
exists. */
if (access(fup->destpath, F_OK) == 0) {
error = updater_delete(up, fup);
if (error)
return (error);
}
sr = &srbuf;
sr->sr_type = SR_CHECKOUTDEAD;
sr->sr_file = name;
sr->sr_tag = tag;
sr->sr_date = date;
sr->sr_serverattr = fattr_decode(attr);
if (sr->sr_serverattr == NULL)
return (UPDATER_ERR_PROTO);
error = status_put(fup->st, sr);
fattr_free(sr->sr_serverattr);
if (error) {
up->errmsg = status_errmsg(fup->st);
return (UPDATER_ERR_MSG);
}
break;
case 'U':
/* Update live checked-out file. */
name = proto_get_ascii(&line);
tag = proto_get_ascii(&line);
date = proto_get_ascii(&line);
proto_get_ascii(&line); /* XXX - oldRevNum */
proto_get_ascii(&line); /* XXX - fromAttic */
proto_get_ascii(&line); /* XXX - logLines */
expand = proto_get_ascii(&line);
attr = proto_get_ascii(&line);
wantmd5 = proto_get_ascii(&line);
if (wantmd5 == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
sr = &fup->srbuf;
sr->sr_type = SR_CHECKOUTLIVE;
sr->sr_file = xstrdup(name);
sr->sr_date = xstrdup(date);
sr->sr_tag = xstrdup(tag);
sr->sr_serverattr = fattr_decode(attr);
if (sr->sr_serverattr == NULL)
return (UPDATER_ERR_PROTO);
fup->expand = keyword_decode_expand(expand);
if (fup->expand == -1)
return (UPDATER_ERR_PROTO);
error = fup_prepare(fup, name, 0);
if (error)
return (UPDATER_ERR_PROTO);
fup->wantmd5 = xstrdup(wantmd5);
fup->temppath = tempname(fup->destpath);
error = updater_diff(up, fup);
if (error)
return (error);
break;
case 'u':
/* Update dead checked-out file. */
name = proto_get_ascii(&line);
tag = proto_get_ascii(&line);
date = proto_get_ascii(&line);
attr = proto_get_ascii(&line);
if (attr == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
error = fup_prepare(fup, name, 0);
if (error)
return (UPDATER_ERR_PROTO);
error = updater_delete(up, fup);
if (error)
return (error);
sr = &srbuf;
sr->sr_type = SR_CHECKOUTDEAD;
sr->sr_file = name;
sr->sr_tag = tag;
sr->sr_date = date;
sr->sr_serverattr = fattr_decode(attr);
if (sr->sr_serverattr == NULL)
return (UPDATER_ERR_PROTO);
error = status_put(fup->st, sr);
fattr_free(sr->sr_serverattr);
if (error) {
up->errmsg = status_errmsg(fup->st);
return (UPDATER_ERR_MSG);
}
break;
case 'C': /* Checkout file. */
case 'Y': /* Receive checkout-mode fixup. */
name = proto_get_ascii(&line);
tag = proto_get_ascii(&line);
date = proto_get_ascii(&line);
revnum = proto_get_ascii(&line);
revdate = proto_get_ascii(&line);
attr = proto_get_ascii(&line);
if (attr == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
sr = &fup->srbuf;
sr->sr_type = SR_CHECKOUTLIVE;
sr->sr_file = xstrdup(name);
sr->sr_tag = xstrdup(tag);
sr->sr_date = xstrdup(date);
sr->sr_revnum = xstrdup(revnum);
sr->sr_revdate = xstrdup(revdate);
sr->sr_serverattr = fattr_decode(attr);
if (sr->sr_serverattr == NULL)
return (UPDATER_ERR_PROTO);
t = rcsdatetotime(revdate);
if (t == -1)
return (UPDATER_ERR_PROTO);
sr->sr_clientattr = fattr_new(FT_FILE, t);
tmp = fattr_forcheckout(sr->sr_serverattr,
coll->co_umask);
fattr_override(sr->sr_clientattr, tmp, FA_MASK);
fattr_free(tmp);
fattr_mergedefault(sr->sr_clientattr);
error = fup_prepare(fup, name, 0);
if (error)
return (UPDATER_ERR_PROTO);
fup->temppath = tempname(fup->destpath);
fup->isfixup = cmd == 'Y';
error = updater_checkout(up, fup);
if (error)
return (error);
break;
case 'D':
/* Delete file. */
name = proto_get_ascii(&line);
if (name == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
error = fup_prepare(fup, name, 0);
if (error)
return (UPDATER_ERR_PROTO);
error = updater_delete(up, fup);
if (error)
return (error);
error = status_delete(fup->st, name, 0);
if (error) {
up->errmsg = status_errmsg(fup->st);
return (UPDATER_ERR_MSG);
}
break;
case 'A':
case 'a':
case 'R':
/* Add/replace regular file. */
name = proto_get_ascii(&line);
attr = proto_get_ascii(&line);
if (attr == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
attic = cmd == 'a';
error = fup_prepare(fup, name, attic);
if (error)
return (UPDATER_ERR_PROTO);
fup->temppath = tempname(fup->destpath);
sr = &fup->srbuf;
sr->sr_type = attic ? SR_FILEDEAD : SR_FILELIVE;
sr->sr_file = xstrdup(name);
sr->sr_serverattr = fattr_decode(attr);
if (sr->sr_serverattr == NULL)
return (UPDATER_ERR_PROTO);
if (attic)
lprintf(1, " Create %s -> Attic\n", name);
else
lprintf(1, " Create %s\n", name);
error = updater_addfile(up, fup);
if (error)
return (error);
break;
case 'r':
/* Update regular file with rsync algorithm. */
name = proto_get_ascii(&line);
attr = proto_get_ascii(&line);
blocksize = proto_get_ascii(&line);
wantmd5 = proto_get_ascii(&line);
if (wantmd5 == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
error = fup_prepare(fup, name, 0);
if (error)
return (UPDATER_ERR_PROTO);
fup->wantmd5 = xstrdup(wantmd5);
fup->temppath = tempname(fup->destpath);
sr = &fup->srbuf;
sr->sr_file = xstrdup(name);
sr->sr_serverattr = fattr_decode(attr);
sr->sr_type = SR_FILELIVE;
if (sr->sr_serverattr == NULL)
return (UPDATER_ERR_PROTO);
error = updater_rsync(up, fup, strtol(blocksize, NULL,
10));
if (error)
return (error);
break;
case 'I':
/* Create directory and add DirDown entry in status
file. */
name = proto_get_ascii(&line);
if (name == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
error = fup_prepare(fup, name, 0);
if (error)
return (UPDATER_ERR_PROTO);
sr = &fup->srbuf;
sr->sr_type = SR_DIRDOWN;
sr->sr_file = xstrdup(name);
sr->sr_serverattr = NULL;
sr->sr_clientattr = fattr_new(FT_DIRECTORY, -1);
fattr_mergedefault(sr->sr_clientattr);
error = mkdirhier(fup->destpath, coll->co_umask);
if (error)
return (UPDATER_ERR_PROTO);
if (access(fup->destpath, F_OK) != 0) {
lprintf(1, " Mkdir %s\n", name);
error = fattr_makenode(sr->sr_clientattr,
fup->destpath);
if (error)
return (UPDATER_ERR_PROTO);
}
error = status_put(fup->st, sr);
if (error) {
up->errmsg = status_errmsg(fup->st);
return (UPDATER_ERR_MSG);
}
break;
case 'i':
/* Remove DirDown entry in status file. */
name = proto_get_ascii(&line);
if (name == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
error = fup_prepare(fup, name, 0);
if (error)
return (UPDATER_ERR_PROTO);
error = status_delete(fup->st, name, 0);
if (error) {
up->errmsg = status_errmsg(fup->st);
return (UPDATER_ERR_MSG);
}
break;
case 'J':
/* Set attributes of directory and update DirUp entry
in status file. */
name = proto_get_ascii(&line);
attr = proto_get_ascii(&line);
if (attr == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
error = fup_prepare(fup, name, 0);
if (error)
return (UPDATER_ERR_PROTO);
error = updater_setdirattrs(up, coll, fup, name, attr);
if (error)
return (error);
break;
case 'j':
/* Remove directory and delete its DirUp entry in
status file. */
name = proto_get_ascii(&line);
if (name == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
error = fup_prepare(fup, name, 0);
if (error)
return (UPDATER_ERR_PROTO);
lprintf(1, " Rmdir %s\n", name);
updater_deletefile(fup->destpath);
error = status_delete(fup->st, name, 0);
if (error) {
up->errmsg = status_errmsg(fup->st);
return (UPDATER_ERR_MSG);
}
break;
case 'L':
case 'l':
/* Update recorded information for cvs file. */
name = proto_get_ascii(&line);
attr = proto_get_ascii(&line);
if (attr == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
attic = cmd == 'l';
sr = &fup->srbuf;
sr->sr_type = attic ? SR_FILEDEAD : SR_FILELIVE;
sr->sr_file = xstrdup(name);
sr->sr_serverattr = fattr_decode(attr);
sr->sr_clientattr = fattr_decode(attr);
if (sr->sr_serverattr == NULL ||
sr->sr_clientattr == NULL)
return (UPDATER_ERR_PROTO);
/* Save space. Described in detail in updatefile. */
if (!(fattr_getmask(sr->sr_clientattr) & FA_LINKCOUNT)
|| fattr_getlinkcount(sr->sr_clientattr) <= 1)
fattr_maskout(sr->sr_clientattr,
FA_DEV | FA_INODE);
fattr_maskout(sr->sr_clientattr, FA_FLAGS);
error = status_put(fup->st, sr);
if (error) {
up->errmsg = status_errmsg(fup->st);
return (UPDATER_ERR_MSG);
}
break;
case 'N':
case 'n':
/* Create a node. */
name = proto_get_ascii(&line);
attr = proto_get_ascii(&line);
if (attr == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
attic = cmd == 'n';
error = fup_prepare(fup, name, attic);
if (error)
return (UPDATER_ERR_PROTO);
sr = &fup->srbuf;
sr->sr_type = (attic ? SR_FILEDEAD : SR_FILELIVE);
sr->sr_file = xstrdup(name);
sr->sr_serverattr = fattr_decode(attr);
if (sr->sr_serverattr == NULL)
return (UPDATER_ERR_PROTO);
sr->sr_clientattr = fattr_new(FT_SYMLINK, -1);
fattr_mergedefault(sr->sr_clientattr);
fattr_maskout(sr->sr_clientattr, FA_FLAGS);
error = updater_updatenode(up, coll, fup, name);
if (error)
return (error);
break;
case 'V':
case 'v':
/* Edit RCS file. */
name = proto_get_ascii(&line);
attr = proto_get_ascii(&line);
rcsopt = proto_get_ascii(&line);
wantmd5 = proto_get_ascii(&line);
if (wantmd5 == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
attic = cmd == 'v';
error = fup_prepare(fup, name, attic);
if (error)
return (UPDATER_ERR_PROTO);
fup->temppath = tempname(fup->destpath);
fup->wantmd5 = xstrdup(wantmd5);
sr = &fup->srbuf;
sr->sr_type = attic ? SR_FILEDEAD : SR_FILELIVE;
sr->sr_file = xstrdup(name);
sr->sr_serverattr = fattr_decode(attr);
if (sr->sr_serverattr == NULL)
return (UPDATER_ERR_PROTO);
error = updater_rcsedit(up, fup, name, rcsopt);
if (error)
return (error);
break;
case 'X':
case 'x':
/* Receive RCS file fixup. */
name = proto_get_ascii(&line);
attr = proto_get_ascii(&line);
if (attr == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
attic = cmd == 'x';
error = fup_prepare(fup, name, attic);
if (error)
return (UPDATER_ERR_PROTO);
fup->temppath = tempname(fup->destpath);
fup->isfixup = 1;
sr = &fup->srbuf;
sr->sr_type = attic ? SR_FILEDEAD : SR_FILELIVE;
sr->sr_file = xstrdup(name);
sr->sr_serverattr = fattr_decode(attr);
if (sr->sr_serverattr == NULL)
return (UPDATER_ERR_PROTO);
lprintf(1, " Fixup %s\n", name);
error = updater_addfile(up, fup);
if (error)
return (error);
break;
case 'Z':
/* Append to regular file. */
name = proto_get_ascii(&line);
attr = proto_get_ascii(&line);
pos = proto_get_ascii(&line);
if (pos == NULL || line != NULL)
return (UPDATER_ERR_PROTO);
error = fup_prepare(fup, name, 0);
fup->temppath = tempname(fup->destpath);
sr = &fup->srbuf;
sr->sr_type = SR_FILELIVE;
sr->sr_file = xstrdup(name);
sr->sr_serverattr = fattr_decode(attr);
if (sr->sr_serverattr == NULL)
return (UPDATER_ERR_PROTO);
position = strtol(pos, NULL, 10);
lprintf(1, " Append to %s\n", name);
error = updater_append_file(up, fup, position);
if (error)
return (error);
break;
case '!':
/* Warning from server. */
msg = proto_get_rest(&line);
if (msg == NULL)
return (UPDATER_ERR_PROTO);
lprintf(-1, "Server warning: %s\n", msg);
break;
default:
return (UPDATER_ERR_PROTO);
}
fup_reset(fup);
}
if (line == NULL && cmd != '.')
return (UPDATER_ERR_READ);
return (0);
}
/* Delete file. */
static int
updater_delete(struct updater *up, struct file_update *fup)
{
struct config *config;
struct coll *coll;
config = up->config;
coll = fup->coll;
if (coll->co_options & CO_DELETE) {
lprintf(1, " Delete %s\n", fup->coname);
if (config->deletelim >= 0 &&
up->deletecount >= config->deletelim)
return (UPDATER_ERR_DELETELIM);
up->deletecount++;
updater_deletefile(fup->destpath);
if (coll->co_options & CO_CHECKOUTMODE)
updater_prunedirs(coll->co_prefix, fup->destpath);
} else {
lprintf(1," NoDelete %s\n", fup->coname);
}
return (0);
}
static void
updater_deletefile(const char *path)
{
int error;
error = fattr_delete(path);
if (error && errno != ENOENT) {
lprintf(-1, "Cannot delete \"%s\": %s\n",
path, strerror(errno));
}
}
static int
updater_setattrs(struct updater *up, struct file_update *fup, char *name,
char *tag, char *date, char *revnum, char *revdate, struct fattr *rcsattr)
{
struct statusrec sr;
struct status *st;
struct coll *coll;
struct fattr *fileattr, *fa;
char *path;
int error, rv;
coll = fup->coll;
st = fup->st;
path = fup->destpath;
fileattr = fattr_frompath(path, FATTR_NOFOLLOW);
if (fileattr == NULL) {
/* The file has vanished. */
error = status_delete(st, name, 0);
if (error) {
up->errmsg = status_errmsg(st);
return (UPDATER_ERR_MSG);
}
return (0);
}
fa = fattr_forcheckout(rcsattr, coll->co_umask);
fattr_override(fileattr, fa, FA_MASK);
fattr_free(fa);
rv = fattr_install(fileattr, path, NULL);
if (rv == -1) {
lprintf(1, " SetAttrs %s\n", fup->coname);
fattr_free(fileattr);
xasprintf(&up->errmsg, "Cannot set attributes for \"%s\": %s",
path, strerror(errno));
return (UPDATER_ERR_MSG);
}
if (rv == 1) {
lprintf(1, " SetAttrs %s\n", fup->coname);
fattr_free(fileattr);
fileattr = fattr_frompath(path, FATTR_NOFOLLOW);
if (fileattr == NULL) {
/* We're being very unlucky. */
error = status_delete(st, name, 0);
if (error) {
up->errmsg = status_errmsg(st);
return (UPDATER_ERR_MSG);
}
return (0);
}
}
fattr_maskout(fileattr, FA_COIGNORE);
sr.sr_type = SR_CHECKOUTLIVE;
sr.sr_file = name;
sr.sr_tag = tag;
sr.sr_date = date;
sr.sr_revnum = revnum;
sr.sr_revdate = revdate;
sr.sr_clientattr = fileattr;
sr.sr_serverattr = rcsattr;
error = status_put(st, &sr);
fattr_free(fileattr);
if (error) {
up->errmsg = status_errmsg(st);
return (UPDATER_ERR_MSG);
}
return (0);
}
static void
updater_needfixup(struct updater *up, struct file_update *fup, const char *msg)
{
struct statusrec *sr;
struct coll *coll;
coll = fup->coll;
sr = &fup->srbuf;
lprintf(-1, "%s: %s -- ", fup->coname, msg);
if (fup->isfixup) {
lprintf(-1, "file not updated\n");
} else {
lprintf(-1, "will transfer entire file\n");
fixups_put(up->config->fixups, fup->coll, sr->sr_file);
}
if (coll->co_options & CO_KEEPBADFILES)
lprintf(-1, "Bad version saved in %s\n", fup->temppath);
else
updater_deletefile(fup->temppath);
}
static int
updater_updatefile(struct updater *up, struct file_update *fup, const char *md5)
{
struct coll *coll;
struct status *st;
struct statusrec *sr;
struct fattr *fileattr;
int error, rv;
coll = fup->coll;
sr = &fup->srbuf;
st = fup->st;
if (strcmp(fup->wantmd5, md5) != 0) {
updater_needfixup(up, fup, "Checksum mismatch");
return (0);
}
fattr_umask(sr->sr_clientattr, coll->co_umask);
rv = fattr_install(sr->sr_clientattr, fup->destpath, fup->temppath);
if (rv == -1) {
xasprintf(&up->errmsg, "Cannot install \"%s\" to \"%s\": %s",
fup->temppath, fup->destpath, strerror(errno));
return (UPDATER_ERR_MSG);
}
/* XXX Executes */
/*
* We weren't necessarily able to set all the file attributes to the
* desired values, and any executes may have altered the attributes.
* To make sure we record the actual attribute values, we fetch
* them from the file.
*
* However, we preserve the link count as received from the
* server. This is important for preserving hard links in mirror
* mode.
*/
fileattr = fattr_frompath(fup->destpath, FATTR_NOFOLLOW);
if (fileattr == NULL) {
xasprintf(&up->errmsg, "Cannot stat \"%s\": %s", fup->destpath,
strerror(errno));