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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
|
/* remsync 1.5 - remotely synchronize file trees Author: Kees J. Bot
* 10 Jun 1994
*/
#define nil 0
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include <time.h>
#include <utime.h>
#define arraysize(a) (sizeof(a) / sizeof((a)[0]))
#define arraylimit(a) ((a) + arraysize(a))
#ifndef major
#define major(dev) ((int) ((dev) >> 8))
#define minor(dev) ((int) ((dev) & 0xFF))
#endif
#ifndef S_ISLNK
/* There were no symlinks in medieval times. */
#define S_ISLNK(mode) (0)
#define lstat stat
#define symlink(path1, path2) (errno= ENOSYS, -1)
#define readlink(path, buf, len) (errno= ENOSYS, -1)
#endif
int sflag; /* Make state file. */
int dflag; /* Make list of differences. */
int uflag; /* Only update files with newer versions. */
int xflag; /* Do not cross device boundaries. */
int Dflag; /* Debug: Readable differences, no file contents. */
int vflag; /* Verbose. */
#define NO_DEVICE (-1)
dev_t xdev= NO_DEVICE; /* The device that you should stay within. */
int excode= 0; /* Exit(excode); */
#define BASE_INDENT 2 /* State file basic indent. */
void report(const char *label)
{
fprintf(stderr, "remsync: %s: %s\n", label, strerror(errno));
excode= 1;
}
void fatal(const char *label)
{
report(label);
exit(1);
}
void *allocate(void *mem, size_t size)
{
if ((mem= mem == nil ? malloc(size) : realloc(mem, size)) == nil) {
fprintf(stderr, "remsync: Out of memory: %s\n",
strerror(errno));
exit(1);
}
return mem;
}
void deallocate(void *mem)
{
if (mem != nil) free(mem);
}
/* One needs to slowly forget two sets of objects: for the code that reads
* the state file, and for the code that traverses trees.
*/
int keep;
#define KEEP_STATE 0
#define KEEP_TRAVERSE 1
void forget(void *mem)
/* Some objects must be deleted in time, but not just yet. */
{
static void *death_row[2][50];
static void **dp[2]= { death_row[0], death_row[1] };
deallocate(*dp[keep]);
*dp[keep]++= mem;
if (dp[keep] == arraylimit(death_row[keep])) dp[keep]= death_row[keep];
}
char *copystr(const char *s)
{
char *c= allocate(nil, (strlen(s) + 1) * sizeof(c[0]));
strcpy(c, s);
return c;
}
typedef struct pathname {
char *path; /* The actual pathname. */
size_t idx; /* Index for the terminating null byte. */
size_t lim; /* Actual length of the path array. */
} pathname_t;
void path_init(pathname_t *pp)
/* Initialize a pathname to the null string. */
{
pp->path= allocate(nil, (pp->lim= 16) * sizeof(pp->path[0]));
pp->path[pp->idx= 0]= 0;
}
void path_add(pathname_t *pp, const char *name)
/* Add a component to a pathname. */
{
size_t lim;
char *p;
int slash;
lim= pp->idx + strlen(name) + 2;
if (lim > pp->lim) {
pp->lim= lim + lim/2; /* add an extra 50% growing space. */
pp->path= allocate(pp->path, pp->lim * sizeof(pp->path[0]));
}
p= pp->path + pp->idx;
slash= (pp->idx > 0);
if (pp->idx == 1 && p[-1] == '/') p--;
while (*name != 0) {
if (*name == '/') {
slash= 1;
} else {
if (slash) { *p++ = '/'; slash= 0; }
*p++= *name;
}
name++;
}
if (slash && p == pp->path) *p++= '/';
*p = 0;
pp->idx= p - pp->path;
}
void path_trunc(pathname_t *pp, size_t didx)
/* Delete part of a pathname to a remembered length. */
{
pp->path[pp->idx= didx]= 0;
}
#if kept_for_comments_only
const char *path_name(const pathname_t *pp)
/* Return the actual name as a char array. */
{
return pp->path;
}
size_t path_length(const pathname_t *pp)
/* The length of the pathname. */
{
return pp->idx;
}
void path_drop(pathname_t *pp)
/* Release the storage occupied by the pathname. */
{
free(pp->path);
}
#endif
#define path_name(pp) ((const char *) (pp)->path)
#define path_length(pp) ((pp)->idx)
#define path_drop(pp) free((void *) (pp)->path)
typedef struct namelist { /* Obviously a list of names. */
struct namelist *next;
char *name;
} namelist_t;
char *rdlink(const char *link, off_t size)
/* Look where "link" points. */
{
static char *path= nil;
static size_t len= 0;
size_t n;
if (len <= size) {
path= allocate(path, (len= size * 2) * sizeof(path[0]));
}
if ((n= readlink(link, path, len)) == -1) return nil;
path[n]= 0;
return path;
}
void sort(namelist_t **anl)
/* A stable mergesort disguised as line noise. Must be called like this:
* if (L!=nil && L->next!=nil) sort(&L);
*/
{
/* static */ namelist_t *nl1, **mid; /* Need not be local */
namelist_t *nl2;
nl1= *(mid= &(*anl)->next);
do {
if ((nl1= nl1->next) == nil) break;
mid= &(*mid)->next;
} while ((nl1= nl1->next) != nil);
nl2= *mid;
*mid= nil;
if ((*anl)->next != nil) sort(anl);
if (nl2->next != nil) sort(&nl2);
nl1= *anl;
for (;;) {
if (strcmp(nl1->name, nl2->name)<=0) {
if ((nl1= *(anl= &nl1->next)) == nil) {
*anl= nl2;
break;
}
} else {
*anl= nl2;
nl2= *(anl= &nl2->next);
*anl= nl1;
if (nl2 == nil) break;
}
}
}
namelist_t *collect(const char *dir)
/* Return a sorted list of directory entries. Returns null with errno != 0
* on error.
*/
{
namelist_t *names, **pn= &names;
DIR *dp;
struct dirent *entry;
if ((dp= opendir(dir)) == nil) return nil;
while ((entry= readdir(dp)) != nil) {
if (entry->d_name[0] == '.'
&& (entry->d_name[1] == 0
|| (entry->d_name[1] == '.'
&& entry->d_name[2] == 0))) {
continue;
}
*pn= allocate(nil, sizeof(**pn));
(*pn)->name= copystr(entry->d_name);
pn= &(*pn)->next;
}
closedir(dp);
*pn= nil;
errno= 0;
if (names != nil && names->next != nil) sort(&names);
return names;
}
char *pop_name(namelist_t **names)
/* Return one name of a name list. */
{
char *name;
namelist_t *junk;
junk= *names;
*names= junk->next;
name= junk->name;
deallocate(junk);
forget(name);
return name;
}
typedef enum filetype { /* The files we know about. */
F_DIR,
F_FILE,
F_BLK,
F_CHR,
F_PIPE,
F_LINK
} filetype_t;
typedef struct entry { /* One file. */
int depth; /* Depth in directory tree. */
const char *name; /* Name of entry. */
const char *path; /* Path name. */
int ignore; /* Ignore this entry (errno number.) */
unsigned long fake_ino; /* Fake inode number for hard links. */
int linked; /* Is the file hard linked? */
int lastlink; /* Is it the last link? */
char *link; /* Where a (sym)link points to. */
filetype_t type;
mode_t mode; /* Not unlike those in struct stat. */
uid_t uid;
gid_t gid;
off_t size;
time_t mtime;
dev_t rdev;
} entry_t;
void linked(entry_t *entry, struct stat *stp)
/* Return an "inode number" if a file could have links (link count > 1).
* Also return a path to the first link if you see the file again.
*/
{
static unsigned long new_fake_ino= 0;
static struct links {
struct links *next;
char *path;
ino_t ino;
dev_t dev;
nlink_t nlink;
unsigned long fake_ino;
} *links[1024];
struct links **plp, *lp;
entry->linked= entry->lastlink= 0;
entry->fake_ino= 0;
entry->link= nil;
if (S_ISDIR(stp->st_mode) || stp->st_nlink < 2) return;
plp= &links[stp->st_ino % arraysize(links)];
while ((lp= *plp) != nil && (lp->ino != stp->st_ino
|| lp->dev != stp->st_dev)) plp= &lp->next;
if (lp == nil) {
/* New file, store it with a new fake inode number. */
*plp= lp= allocate(nil, sizeof(*lp));
lp->next= nil;
lp->path= copystr(entry->path);
lp->ino= stp->st_ino;
lp->dev= stp->st_dev;
lp->nlink= stp->st_nlink;
lp->fake_ino= ++new_fake_ino;
} else {
entry->link= lp->path;
entry->linked= 1;
}
entry->fake_ino= lp->fake_ino;
if (--lp->nlink == 0) {
/* No need to remember this one, no more links coming. */
*plp= lp->next;
forget(lp->path);
deallocate(lp);
entry->lastlink= 1;
}
}
char *tree; /* Tree to work on. */
FILE *statefp; /* State file. */
char *state_file;
FILE *difffp; /* File of differences. */
char *diff_file;
entry_t *traverse(void)
/* Get one name from the directory tree. */
{
static int depth;
static pathname_t path;
static entry_t entry;
static namelist_t **entries;
static size_t *trunc;
static size_t deep;
static namelist_t *newentries;
struct stat st;
recurse:
keep= KEEP_TRAVERSE;
if (deep == 0) {
/* Initialize for the root of the tree. */
path_init(&path);
path_add(&path, tree);
entries= allocate(nil, 1 * sizeof(entries[0]));
entries[0]= allocate(nil, sizeof(*entries[0]));
entries[0]->next= nil;
entries[0]->name= copystr("/");
trunc= allocate(nil, 1 * sizeof(trunc[0]));
trunc[0]= path_length(&path);
deep= 1;
} else
if (newentries != nil) {
/* Last entry was a directory, need to go down. */
if (entry.ignore) {
/* Ouch, it is to be ignored! */
while (newentries != nil) (void) pop_name(&newentries);
goto recurse;
}
if (++depth == deep) {
deep++;
entries= allocate(entries, deep * sizeof(entries[0]));
trunc= allocate(trunc, deep * sizeof(trunc[0]));
}
entries[depth]= newentries;
newentries= nil;
trunc[depth]= path_length(&path);
} else {
/* Pop up out of emptied directories. */
while (entries[depth] == nil) {
if (depth == 0) return nil; /* Back at the root. */
/* Go up one level. */
depth--;
}
}
entry.name= pop_name(&entries[depth]);
path_trunc(&path, trunc[depth]);
path_add(&path, entry.name);
if (depth == 0) {
entry.path= "/";
} else {
entry.path= path_name(&path) + trunc[0];
if (entry.path[0] == '/') entry.path++;
}
entry.depth= depth;
entry.ignore= 0;
if (lstat(path_name(&path), &st) < 0) {
if (depth == 0 || errno != ENOENT) {
/* Something wrong with this entry, complain about
* it and ignore it further.
*/
entry.ignore= errno;
report(path_name(&path));
return &entry;
} else {
/* Entry strangely nonexistent; simply continue. */
goto recurse;
}
}
/* Don't cross mountpoints if -x is set. */
if (xflag) {
if (xdev == NO_DEVICE) xdev= st.st_dev;
if (st.st_dev != xdev) {
/* Ignore the mountpoint. */
entry.ignore= EXDEV;
return &entry;
}
}
entry.mode= st.st_mode & 07777;
entry.uid= st.st_uid;
entry.gid= st.st_gid;
entry.size= st.st_size;
entry.mtime= st.st_mtime;
entry.rdev= st.st_rdev;
linked(&entry, &st);
if (S_ISDIR(st.st_mode)) {
/* A directory. */
entry.type= F_DIR;
/* Gather directory entries for the next traverse. */
if ((newentries= collect(path_name(&path))) == nil
&& errno != 0) {
entry.ignore= errno;
report(path_name(&path));
}
} else
if (S_ISREG(st.st_mode)) {
/* A plain file. */
entry.type= F_FILE;
} else
if (S_ISBLK(st.st_mode)) {
/* A block special file. */
entry.type= F_BLK;
} else
if (S_ISCHR(st.st_mode)) {
/* A character special file. */
entry.type= F_CHR;
} else
if (S_ISFIFO(st.st_mode)) {
/* A named pipe. */
entry.type= F_PIPE;
} else
if (S_ISLNK(st.st_mode)) {
/* A symbolic link. */
entry.type= F_LINK;
if ((entry.link= rdlink(path_name(&path), st.st_size)) == nil) {
entry.ignore= errno;
report(path_name(&path));
}
} else {
/* Unknown type of file. */
entry.ignore= EINVAL;
}
return &entry;
}
void checkstate(void)
{
if (ferror(statefp)) fatal(state_file);
}
void indent(int depth)
/* Provide indentation to show directory depth. */
{
int n= BASE_INDENT * (depth - 1);
while (n >= 8) {
if (putc('\t', statefp) == EOF) checkstate();
n-= 8;
}
while (n > 0) {
if (putc(' ', statefp) == EOF) checkstate();
n--;
}
}
int print_name(FILE *fp, const char *name)
/* Encode a name. */
{
const char *p;
int c;
for (p= name; (c= (unsigned char) *p) != 0; p++) {
if (c <= ' ' || c == '\\') {
fprintf(fp, "\\%03o", c);
if (ferror(fp)) return 0;
} else {
if (putc(c, fp) == EOF) return 0;
}
}
return 1;
}
void mkstatefile(void)
/* Make a state file out of the directory tree. */
{
entry_t *entry;
while ((entry= traverse()) != nil) {
indent(entry->depth);
if (!print_name(statefp, entry->name)) checkstate();
if (entry->ignore) {
fprintf(statefp, "\tignore (%s)\n",
strerror(entry->ignore));
checkstate();
continue;
}
switch (entry->type) {
case F_DIR:
fprintf(statefp, "\td%03o %u %u",
(unsigned) entry->mode,
(unsigned) entry->uid, (unsigned) entry->gid);
break;
case F_FILE:
fprintf(statefp, "\t%03o %u %u %lu %lu",
(unsigned) entry->mode,
(unsigned) entry->uid, (unsigned) entry->gid,
(unsigned long) entry->size,
(unsigned long) entry->mtime);
break;
case F_BLK:
case F_CHR:
fprintf(statefp, "\t%c%03o %u %u %x",
entry->type == F_BLK ? 'b' : 'c',
(unsigned) entry->mode,
(unsigned) entry->uid, (unsigned) entry->gid,
(unsigned) entry->rdev);
break;
case F_PIPE:
fprintf(statefp, "\tp%03o %u %u",
(unsigned) entry->mode,
(unsigned) entry->uid, (unsigned) entry->gid);
break;
case F_LINK:
fprintf(statefp, "\t-> ");
checkstate();
(void) print_name(statefp, entry->link);
break;
}
checkstate();
if (entry->fake_ino != 0)
fprintf(statefp, " %lu", entry->fake_ino);
if (entry->lastlink)
fprintf(statefp, " last");
if (fputc('\n', statefp) == EOF) checkstate();
}
fflush(statefp);
checkstate();
}
char *read1line(FILE *fp)
/* Read one line from a file. Return null on EOF or error. */
{
static char *line;
static size_t len;
size_t idx;
int c;
if (len == 0) line= allocate(nil, (len= 16) * sizeof(line[0]));
idx= 0;
while ((c= getc(fp)) != EOF && c != '\n') {
if (c < '\t') {
/* Control characters are not possible. */
fprintf(stderr,
"remsync: control character in data file!\n");
exit(1);
}
line[idx++]= c;
if (idx == len) {
line= allocate(line, (len*= 2) * sizeof(line[0]));
}
}
if (c == EOF) {
if (ferror(fp)) return nil;
if (idx == 0) return nil;
}
line[idx]= 0;
return line;
}
void getword(char **pline, char **parg, size_t *plen)
/* Get one word from a line, interpret octal escapes. */
{
char *line= *pline;
char *arg= *parg;
size_t len= *plen;
int i;
int c;
size_t idx;
idx= 0;
while ((c= *line) != 0 && c != ' ' && c != '\t') {
line++;
if (c == '\\') {
c= 0;
for (i= 0; i < 3; i++) {
if ((unsigned) (*line - '0') >= 010) break;
c= (c << 3) | (*line - '0');
line++;
}
}
arg[idx++]= c;
if (idx == len) arg= allocate(arg, (len*= 2) * sizeof(arg[0]));
}
arg[idx]= 0;
*pline= line;
*parg= arg;
*plen= len;
}
void splitline(char *line, char ***pargv, size_t *pargc)
/* Split a line into an array of words. */
{
static char **argv;
static size_t *lenv;
static size_t len;
size_t idx;
idx= 0;
for (;;) {
while (*line == ' ' || *line == '\t') line++;
if (*line == 0) break;
if (idx == len) {
len++;
argv= allocate(argv, len * sizeof(argv[0]));
lenv= allocate(lenv, len * sizeof(lenv[0]));
argv[idx]= allocate(nil, 16 * sizeof(argv[idx][0]));
lenv[idx]= 16;
}
getword(&line, &argv[idx], &lenv[idx]);
idx++;
}
*pargv= argv;
*pargc= idx;
}
int getattributes(entry_t *entry, int argc, char **argv)
/* Convert state or difference file info into file attributes. */
{
int i;
int attr;
#define A_MODE1 0x01 /* Some of these attributes follow the name */
#define A_MODE 0x02
#define A_OWNER 0x04
#define A_SIZETIME 0x08
#define A_DEV 0x10
#define A_LINK 0x20
switch (argv[0][0]) {
case 'd':
/* Directory. */
entry->type= F_DIR;
attr= A_MODE1 | A_OWNER;
break;
case 'b':
/* Block device. */
entry->type= F_BLK;
attr= A_MODE1 | A_OWNER | A_DEV;
break;
case 'c':
/* Character device. */
entry->type= F_CHR;
attr= A_MODE1 | A_OWNER | A_DEV;
break;
case 'p':
/* Named pipe. */
entry->type= F_PIPE;
attr= A_MODE1 | A_OWNER;
break;
case '-':
/* Symlink. */
entry->type= F_LINK;
attr= A_LINK;
break;
default:
/* Normal file. */
entry->type= F_FILE;
attr= A_MODE | A_OWNER | A_SIZETIME;
}
if (attr & (A_MODE | A_MODE1)) {
entry->mode= strtoul(argv[0] + (attr & A_MODE1), nil, 010);
}
i= 1;
if (attr & A_OWNER) {
if (i + 2 > argc) return 0;
entry->uid= strtoul(argv[i++], nil, 10);
entry->gid= strtoul(argv[i++], nil, 10);
}
if (attr & A_SIZETIME) {
if (i + 2 > argc) return 0;
entry->size= strtoul(argv[i++], nil, 10);
entry->mtime= strtoul(argv[i++], nil, 10);
}
if (attr & A_DEV) {
if (i + 1 > argc) return 0;
entry->rdev= strtoul(argv[i++], nil, 0x10);
}
if (attr & A_LINK) {
if (i + 1 > argc) return 0;
entry->link= argv[i++];
}
entry->linked= entry->lastlink= 0;
if (i < argc) {
/* It has a fake inode number, so it is a hard link. */
static struct links { /* List of hard links. */
struct links *next;
unsigned long fake_ino;
char *path;
} *links[1024];
struct links **plp, *lp;
unsigned long fake_ino;
fake_ino= strtoul(argv[i++], nil, 10);
plp= &links[fake_ino % arraysize(links)];
while ((lp= *plp) != nil && lp->fake_ino != fake_ino)
plp= &lp->next;
if (lp == nil) {
/* New link. */
*plp= lp= allocate(nil, sizeof(*lp));
lp->next= nil;
lp->fake_ino= fake_ino;
lp->path= copystr(entry->path);
} else {
/* Linked to. */
entry->link= lp->path;
entry->linked= 1;
}
if (i < argc) {
if (strcmp(argv[i++], "last") != 0) return 0;
/* Last hard link of a file. */
forget(lp->path);
*plp= lp->next;
deallocate(lp);
entry->lastlink= 1;
}
}
if (i != argc) return 0;
return 1;
}
void state_syntax(off_t line)
{
fprintf(stderr, "remsync: %s: syntax error on line %lu\n",
state_file, (unsigned long) line);
exit(1);
}
entry_t *readstate(void)
/* Read one entry from the state file. */
{
static entry_t entry;
static pathname_t path;
static size_t *trunc;
static size_t trunc_len;
static int base_indent;
char *line;
char **argv;
size_t argc;
static off_t lineno;
int indent, depth;
recurse:
keep= KEEP_STATE;
if (feof(statefp) || (line= read1line(statefp)) == nil) {
checkstate();
return nil;
}
lineno++;
/* How far is this entry indented? */
indent= 0;
while (*line != 0) {
if (*line == ' ') indent++;
else
if (*line == '\t') indent= (indent + 8) & ~7;
else
break;
line++;
}
if (indent > 0 && base_indent == 0) base_indent= indent;
depth= (base_indent == 0 ? 0 : indent / base_indent) + 1;
if (entry.ignore && depth > entry.depth) {
/* If the old directory is ignored, then so are its entries. */
goto recurse;
}
entry.depth= depth;
splitline(line, &argv, &argc);
if (argc < 2) state_syntax(lineno);
if (trunc == nil) {
/* The root of the tree, initialize path. */
if (argv[0][0] != '/') state_syntax(lineno);
path_init(&path);
path_add(&path, "/");
trunc= allocate(nil, (trunc_len= 16) * sizeof(trunc[0]));
/* The root has depth 0. */
entry.depth= 0;
trunc[0]= 0;
} else {
if (entry.depth > trunc_len) {
trunc= allocate(trunc,
(trunc_len*= 2) * sizeof(trunc[0]));
}
path_trunc(&path, trunc[entry.depth - 1]);
path_add(&path, argv[0]);
trunc[entry.depth]= path_length(&path);
}
entry.path= path_name(&path);
entry.name= argv[0];
entry.link= nil;
if ((entry.ignore= strcmp(argv[1], "ignore") == 0)) {
return &entry;
}
if (!getattributes(&entry, argc - 1, argv + 1)) state_syntax(lineno);
return &entry;
}
void checkdiff(void)
{
if (ferror(difffp)) fatal(diff_file);
}
enum { DELETE, REPLACE, COPY, SIMILAR, EQUAL, ADD }
compare(entry_t *remote, entry_t *local)
/* Compare the local and remote entries and tell what need to be done. */
{
int cmp;
/* Surplus entries? */
if (local == nil) return DELETE;
if (remote == nil) return ADD;
/* Extra directory entries? */
if (remote->depth > local->depth) return DELETE;
if (local->depth > remote->depth) return ADD;
/* Compare names. */
cmp= strcmp(remote->name, local->name);
if (cmp < 0) return DELETE;
if (cmp > 0) return ADD;
/* The files have the same name. Ignore one, ignore the other. */
if (remote->ignore || local->ignore) {
remote->ignore= local->ignore= 1;
return EQUAL;
}
/* Reasons for replacement? */
if (remote->type != local->type) return REPLACE;
/* Should be hard linked to the same file. */
if (remote->linked || local->linked) {
if (!remote->linked || !local->linked) return REPLACE;
if (strcmp(remote->link, local->link) != 0) return REPLACE;
}
switch (remote->type) {
case F_FILE:
if (uflag) {
if (remote->mtime < local->mtime) return COPY;
} else {
if (remote->size != local->size
|| remote->mtime != local->mtime)
return COPY;
}
goto check_modes;
case F_BLK:
case F_CHR:
if (remote->rdev != local->rdev) return REPLACE;
goto check_modes;
case F_DIR:
case F_PIPE:
check_modes:
if (remote->mode != local->mode
|| remote->uid != local->uid
|| remote->gid != local->gid) return SIMILAR;
break;
case F_LINK:
if (strcmp(remote->link, local->link) != 0) return REPLACE;
break;
}
return EQUAL;
}
void delete(entry_t *old)
/* Emit an instruction to remove an entry. */
{
if (old->ignore) return;
if (uflag) return;
fprintf(difffp, "rm ");
checkdiff();
if (!print_name(difffp, old->path)) checkdiff();
if (putc('\n', difffp) == EOF) checkdiff();
if (vflag) fprintf(stderr, "rm %s\n", old->path);
}
void change_modes(entry_t *old, entry_t *new)
/* Emit an instruction to change the attributes of an entry. */
{
if (new->ignore) return;
fprintf(difffp, "chmod ");
checkdiff();
if (!print_name(difffp, new->path)) checkdiff();
fprintf(difffp, " %03o %u %u\n",
(unsigned) new->mode,
(unsigned) new->uid, (unsigned) new->gid);
checkdiff();
if (vflag && old->mode != new->mode) {
fprintf(stderr, "chmod %s %03o %u %u\n",
new->path,
(unsigned) new->mode,
(unsigned) new->uid, (unsigned) new->gid);
}
}
int cat(int f, off_t size)
/* Include the contents of a file in the differences file. */
{
ssize_t n;
unsigned char buf[1024 << sizeof(int)];
unsigned char *p;
int c;
if (Dflag) return 1; /* Debug: Don't need the file contents. */
while ((n= read(f, buf, sizeof(buf))) > 0) {
p= buf;
do {
if (size == 0) {
/* File suddenly larger. */
errno= EINVAL;
return 0;
}
c= *p++;
if (putc(c, difffp) == EOF) checkdiff();
size--;
} while (--n != 0);
}
if (size > 0) {
int err= errno;
/* File somehow shrunk, pad it out. */
do {
if (putc(0, difffp) == EOF) checkdiff();
} while (--size != 0);
errno= n == 0 ? EINVAL : err;
n= -1;
}
return n == 0;
}
void add(entry_t *old, entry_t *new)
/* Emit an instruction to add an entry. */
{
pathname_t file;
int f;
if (new->ignore) return;
if (new->linked) {
/* This file is to be a hard link to an existing file. */
fprintf(difffp, "ln ");
checkdiff();
if (!print_name(difffp, new->link)) checkdiff();
if (fputc(' ', difffp) == EOF) checkdiff();
if (!print_name(difffp, new->path)) checkdiff();
if (fputc('\n', difffp) == EOF) checkdiff();
if (vflag) {
fprintf(stderr, "ln %s %s\n", new->link, new->path);
}
return;
}
/* Add some other type of file. */
fprintf(difffp, "add ");
checkdiff();
if (!print_name(difffp, new->path)) checkdiff();
switch (new->type) {
case F_DIR:
fprintf(difffp, " d%03o %u %u\n",
(unsigned) new->mode,
(unsigned) new->uid, (unsigned) new->gid);
if (vflag) fprintf(stderr, "mkdir %s\n", new->path);
break;
case F_FILE:
path_init(&file);
path_add(&file, tree);
path_add(&file, new->path);
if ((f= open(path_name(&file), O_RDONLY)) < 0) {
report(path_name(&file));
path_drop(&file);
fprintf(difffp, " ignore\n");
break;
}
fprintf(difffp, " %03o %u %u %lu %lu\n",
(unsigned) new->mode,
(unsigned) new->uid, (unsigned) new->gid,
(unsigned long) new->size,
(unsigned long) new->mtime);
checkdiff();
if (!cat(f, new->size)) {
int err= errno;
report(path_name(&file));
fprintf(difffp, "old ");
checkdiff();
print_name(difffp, err == EINVAL
? "File changed when copied" : strerror(err));
fputc('\n', difffp);
checkdiff();
} else {
if (vflag) {
fprintf(stderr, "%s %s\n",
old == nil ? "add" :
old->mtime > new->mtime ?
"restore" : "update",
new->path);
}
}
close(f);
path_drop(&file);
break;
case F_BLK:
case F_CHR:
fprintf(difffp, " %c%03o %u %u %lx\n",
new->type == F_BLK ? 'b' : 'c',
(unsigned) new->mode,
(unsigned) new->uid, (unsigned) new->gid,
(unsigned long) new->rdev);
if (vflag) fprintf(stderr, "mknod %s\n", new->path);
break;
case F_PIPE:
fprintf(difffp, " p%03o %u %u\n",
(unsigned) new->mode,
(unsigned) new->uid, (unsigned) new->gid);
if (vflag) fprintf(stderr, "mkfifo %s\n", new->path);
break;
case F_LINK:
fprintf(difffp, " -> ");
checkdiff();
(void) print_name(difffp, new->link);
checkdiff();
fputc('\n', difffp);
if (vflag) {
fprintf(stderr, "ln -s %s %s\n", new->link, new->path);
}
break;
}
checkdiff();
}
void mkdifferences(void)
{
entry_t *remote;
entry_t *local;
remote= readstate();
local= traverse();
while (remote != nil || local != nil) {
switch (compare(remote, local)) {
case DELETE:
/* Remove the remote file. */
delete(remote);
remote->ignore= 1;
remote= readstate();
break;
case REPLACE:
/* Replace the remote file with the local one. */
if (remote->type == F_FILE && local->type == F_FILE
&& !local->linked) {
/* Don't overwrite, remove first. */
delete(remote);
}
/*FALL THROUGH*/
case COPY:
/* Overwrite the remote file with the local one. */
add(remote, local);
remote->ignore= 1;
goto skip2;
case SIMILAR:
/* About the same, but the attributes need changing. */
change_modes(remote, local);
goto skip2;
case EQUAL:
skip2:
/* Skip two files. */
remote= readstate();
local= traverse();
break;
case ADD:
/* Add the local file. */
add(nil, local);
local= traverse();
break;
}
}
fprintf(difffp, "end\n");
fflush(difffp);
checkdiff();
}
void apply_remove(pathname_t *pp)
/* Remove an obsolete file. */
{
struct stat st;
if (lstat(path_name(pp), &st) < 0) {
if (errno != ENOENT) report(path_name(pp));
return;
}
if (S_ISDIR(st.st_mode)) {
/* Recursively delete directories. */
size_t len;
namelist_t *entries;
if ((entries= collect(path_name(pp))) == nil && errno != 0) {
report(path_name(pp));
return;
}
len= path_length(pp);
while (entries != nil) {
path_add(pp, pop_name(&entries));
apply_remove(pp);
path_trunc(pp, len);
}
if (rmdir(path_name(pp)) < 0) {
report(path_name(pp));
return;
}
if (vflag) fprintf(stderr, "rmdir %s\n", path_name(pp));
} else {
/* Some other type of file. */
if (unlink(path_name(pp)) < 0) {
report(path_name(pp));
return;
}
if (vflag) fprintf(stderr, "rm %s\n", path_name(pp));
}
}
void apply_mkold(const char *file, const char *err)
/* Make a file very old. (An error occurred when it was added.) */
{
struct utimbuf utb;
utb.actime= utb.modtime= 0;
if (utime(file, &utb) < 0) {
report(file);
return;
}
fprintf(stderr, "made %s look old", file);
if (err != nil)
fprintf(stderr, " due to a remote problem: %s\n", err);
else
fprintf(stderr, "\n");
}
void apply_chmod(const char *file, mode_t mode, uid_t uid, gid_t gid, int talk)
/* Change mode and ownership. */
{
struct stat st;
if (lstat(file, &st) < 0) {
report(file);
return;
}
if ((st.st_mode & 07777) != mode) {
if (chmod(file, mode) < 0) {
report(file);
return;
}
if (vflag && talk) {
fprintf(stderr, "chmod %03o %s\n",
(unsigned) mode, file);
}
}
if (st.st_uid != uid || st.st_gid != gid) {
if (chown(file, uid, gid) < 0) {
if (errno != EPERM) report(file);
return;
}
if (vflag && talk) {
fprintf(stderr, "chown %u:%u %s\n",
(unsigned) uid, (unsigned) gid, file);
}
}
}
void apply_add(pathname_t *pp, entry_t *entry)
/* Add or replace a file. */
{
const char *file;
off_t size;
int f;
unsigned char buf[1024 << sizeof(int)];
unsigned char *p;
int c;
int dirty;
struct stat st;
struct utimbuf utb;
if (entry->ignore) return;
if (lstat(path_name(pp), &st) >= 0 && (entry->type != F_FILE
|| !S_ISREG(st.st_mode))) {
apply_remove(pp);
}
file= path_name(pp);
switch (entry->type) {
case F_DIR:
if (mkdir(file, entry->mode) < 0) {
report(file);
return;
}
if (vflag) fprintf(stderr, "mkdir %s\n", file);
break;
case F_FILE:
size= entry->size;
f= -1;
st.st_mode= 0;
if (lstat(file, &st) < 0 || S_ISREG(st.st_mode)) {
f= open(file, O_WRONLY | O_CREAT | O_TRUNC,
entry->mode);
if (f < 0) {
(void) chmod(file, entry->mode | 0200);
f= open(file, O_WRONLY | O_CREAT | O_TRUNC,
entry->mode);
}
if (f < 0) {
(void) unlink(file);
f= open(file, O_WRONLY | O_CREAT | O_TRUNC,
entry->mode);
}
if (f < 0) report(file);
}
dirty= (f >= 0);
p= buf;
while (size > 0 && (c= getc(difffp)) != EOF) {
size--;
*p++= c;
if (p == arraylimit(buf) || size == 0) {
if (f >= 0 && write(f, buf, p - buf) < 0) {
report(file);
close(f);
f= -1;
}
p= buf;
}
}
if (size > 0) {
if (ferror(difffp)) report(diff_file);
if (feof(difffp)) {
fprintf(stderr, "remspec: %s: premature EOF\n",
diff_file);
}
if (dirty) apply_mkold(file, nil);
exit(1);
}
if (f < 0) {
if (dirty) apply_mkold(file, nil);
return;
}
close(f);
if (vflag) {
fprintf(stderr, st.st_mode == 0 ? "add %s\n"
: entry->mtime >= st.st_mtime
? "update %s\n" : "restore %s\n", file);
}
utb.actime= time(nil);
utb.modtime= entry->mtime;
if (utime(file, &utb) < 0) report(file);
break;
case F_BLK:
if (mknod(file, S_IFBLK | entry->mode, entry->rdev) < 0) {
report(file);
return;
}
if (vflag) {
fprintf(stderr, "mknod %s b %d %d\n", file,
major(entry->rdev), minor(entry->rdev));
}
break;
case F_CHR:
if (mknod(file, S_IFCHR | entry->mode, entry->rdev) < 0) {
report(file);
return;
}
if (vflag) {
fprintf(stderr, "mknod %s c %d %d\n", file,
major(entry->rdev), minor(entry->rdev));
}
break;
case F_PIPE:
if (mknod(file, S_IFIFO | entry->mode, 0) < 0) {
report(file);
return;
}
if (vflag) fprintf(stderr, "mknod %s p\n", file);
break;
case F_LINK:
if (symlink(entry->link, file) < 0) {
report(file);
return;
}
if (vflag) fprintf(stderr, "ln -s %s %s\n", entry->link, file);
return;
}
apply_chmod(file, entry->mode, entry->uid, entry->gid, 0);
}
void apply_link(const char *file, pathname_t *pp)
/* Hard link *pp to file. */
{
struct stat st1, st2;
if (lstat(file, &st1) < 0) {
report(file);
return;
}
if (lstat(path_name(pp), &st2) >= 0) {
if (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev)
return;
apply_remove(pp);
if (lstat(path_name(pp), &st2) >= 0) return;
}
if (link(file, path_name(pp)) < 0) {
fprintf(stderr, "remsync: ln %s %s: %s\n", file, path_name(pp),
strerror(errno));
excode= 1;
return;
}
if (vflag) fprintf(stderr, "ln %s %s\n", file, path_name(pp));
}
void diff_syntax(const char *line)
{
fprintf(stderr, "remsync: %s: syntax error on this line: %s\n",
diff_file, line);
exit(1);
}
void apply_differences(void)
/* Update a tree to a list of differences derived from a remote tree. */
{
char *line;
char **argv;
size_t argc;
pathname_t path, link;
size_t trunc;
path_init(&path);
path_init(&link);
path_add(&path, tree);
path_add(&link, tree);
trunc= path_length(&path);
while (!feof(difffp) && (line= read1line(difffp)) != nil) {
splitline(line, &argv, &argc);
if (argc == 0) diff_syntax(line);
path_trunc(&path, trunc);
if (strcmp(argv[0], "add") == 0) {
entry_t entry;
if (argc < 3) diff_syntax(line);
path_add(&path, argv[1]);
entry.ignore= (strcmp(argv[2], "ignore") == 0);
if (!entry.ignore && !getattributes(&entry,
argc - 2, argv + 2))
diff_syntax(line);
apply_add(&path, &entry);
} else
if (strcmp(argv[0], "rm") == 0) {
if (argc != 2) diff_syntax(line);
path_add(&path, argv[1]);
apply_remove(&path);
} else
if (strcmp(argv[0], "ln") == 0) {
if (argc != 3) diff_syntax(line);
path_trunc(&link, trunc);
path_add(&link, argv[1]);
path_add(&path, argv[2]);
apply_link(path_name(&link), &path);
} else
if (strcmp(argv[0], "chmod") == 0) {
if (argc != 5) diff_syntax(line);
path_add(&path, argv[1]);
apply_chmod(path_name(&path),
strtoul(argv[2], nil, 010),
strtoul(argv[3], nil, 10),
strtoul(argv[4], nil, 10),
1);
} else
if (strcmp(argv[0], "old") == 0) {
if (argc != 3) diff_syntax(line);
path_add(&path, argv[1]);
apply_mkold(path_name(&path), argv[2]);
} else
if (strcmp(argv[0], "end") == 0) {
if (argc != 1) diff_syntax(line);
break;
} else {
diff_syntax(line);
}
}
checkdiff();
}
void usage(void)
{
fprintf(stderr, "Usage: remsync -sxv tree [state-file]\n");
fprintf(stderr, " remsync -duxvD tree [state-file [diff-file]]\n");
fprintf(stderr, " remsync [-xv] tree [diff-file]\n");
exit(1);
}
int main(int argc, char **argv)
{
int i;
for (i= 1; i < argc && argv[i][0] == '-'; i++) {
char *p= argv[i] + 1;
if (p[0] == '-' && p[1] == 0) { i++; break; }
while (*p != 0) {
switch (*p++) {
case 's': sflag= 1; break;
case 'd': dflag= 1; break;
case 'u': uflag= 1; break;
case 'x': xflag= 1; break;
case 'D': Dflag= 1; break;
case 'v': vflag= 1; break;
default: usage();
}
}
}
if (sflag && dflag) usage();
if (sflag && uflag) usage();
if (!sflag && !dflag && uflag) usage();
if (!dflag && Dflag) usage();
if (i == argc) usage();
tree= argv[i++];
if (sflag) {
/* Make a state file. */
state_file= i < argc ? argv[i++] : "-";
if (i != argc) usage();
statefp= stdout;
if (strcmp(state_file, "-") != 0) {
if ((statefp= fopen(state_file, "w")) == nil)
fatal(state_file);
}
mkstatefile();
} else
if (dflag) {
/* Make a file of differences. */
state_file= i < argc ? argv[i++] : "-";
diff_file= i < argc ? argv[i++] : "-";
if (i != argc) usage();
statefp= stdin;
if (strcmp(state_file, "-") != 0) {
if ((statefp= fopen(state_file, "r")) == nil)
fatal(state_file);
}
difffp= stdout;
if (strcmp(diff_file, "-") != 0) {
if ((difffp= fopen(diff_file, "w")) == nil)
fatal(diff_file);
}
mkdifferences();
} else {
/* Apply a file of differences. */
diff_file= i < argc ? argv[i++] : "-";
if (i != argc) usage();
difffp= stdin;
if (strcmp(diff_file, "-") != 0) {
if ((difffp= fopen(diff_file, "r")) == nil)
fatal(diff_file);
}
apply_differences();
}
exit(excode);
}
|