-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclone.c
More file actions
1595 lines (1325 loc) · 48.9 KB
/
clone.c
File metadata and controls
1595 lines (1325 loc) · 48.9 KB
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
// clone.c
// clone
//
// Created by Dr. Rolf Jansen on 2013-01-13.
// Copyright (c) 2013-2022. 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER
// 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.
/*
ToDo:
- for backups of remote read-only stores, fetch a copy of the remote file tree.
- add a command line option for forcing uid:gid+flags.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <pwd.h>
#include <dirent.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/acl.h>
#include "utils.h"
static const char *version = "Version 1.0.9b (r"STRINGIFY(SCMREV)")";
// Device and file system informations
int *gSourceFSType;
dev_t gSourceDev;
int *gDestinFSType;
dev_t gDestinDev;
int gVerbosityLevel = 1;
int gErrorCount = 0;
// Mode Flags
bool gReadNoCache = false;
bool gWriteNoCache = false;
bool gIncremental = false;
bool gSynchronize = false;
bool gHardLinking = false;
llong gWriterLast = 0;
llong gTotalItems = 0;
double gTotalSize = 0.0;
Node **gHLinkINodes = NULL;
Node **gExcludeList = NULL;
// Thread synchronization
bool gRunning = true;
bool gQItemWaitFlag = false;
bool gChunkWaitFlag = false;
bool gReaderWaitFlag = false;
bool gWriterWaitFlag = false;
bool gLevelWaitFlag = false;
pthread_t reader_thread;
pthread_t writer_thread;
pthread_attr_t thread_attrib;
pthread_mutex_t qitem_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t qitem_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t chunk_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t chunk_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t reader_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t reader_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t writer_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t writer_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t level_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t level_cond = PTHREAD_COND_INITIALIZER;
static inline void pthread_cond_wait_flag(pthread_cond_t *cond, pthread_mutex_t *mutex, bool *flag)
{
if (*flag)
pthread_cond_signal(cond);
else
*flag = true;
pthread_cond_wait(cond, mutex);
*flag = false;
}
// The chunk dispenser works very much like a towel dispenser featuring
// an integrated recycling unit. It dispenses empty chunks, informs the
// chunk currently being worked on, and it recycles finished chunks.
// Actually, the recycling procedure is as simple as setting the state
// flag to empty. This won't work with towels, since towel users won't
// accept dirty towels only being marked clean :-)
#define chunkBlckSize 1048576
#define maxChunkCount 100
enum {empty, ready, final};
#pragma pack(16)
typedef struct CopyChunk
{
int state;
int rc;
size_t size;
char buffer[chunkBlckSize];
} CopyChunk;
CopyChunk gChunkDispenser[maxChunkCount];
#pragma pack()
int gBaseChunkSN = 0;
int gHighChunkSN = maxChunkCount;
int gNextChunkSN = 0;
CopyChunk *dispenseEmptyChunk(void)
{
CopyChunk *chunk;
pthread_mutex_lock(&chunk_mutex);
while (gNextChunkSN == gHighChunkSN)
pthread_cond_wait_flag(&chunk_cond, &chunk_mutex, &gChunkWaitFlag);
chunk = &gChunkDispenser[gNextChunkSN];
chunk->state = empty;
if (++gNextChunkSN >= maxChunkCount)
{
gNextChunkSN = 0;
gHighChunkSN = gBaseChunkSN;
}
pthread_mutex_unlock(&chunk_mutex);
return chunk;
}
CopyChunk *informCurrentChunk(void)
{
CopyChunk *chunk;
pthread_mutex_lock(&chunk_mutex);
chunk = &gChunkDispenser[gBaseChunkSN];
if (chunk->state == empty)
chunk = NULL;
pthread_mutex_unlock(&chunk_mutex);
return chunk;
}
void recycleFinishedChunk(CopyChunk *chunk)
{
if (chunk)
{
int chunkSN = (int)(((intptr_t)chunk - (intptr_t)gChunkDispenser)/sizeof(CopyChunk));
pthread_mutex_lock(&chunk_mutex);
if (chunkSN >= gBaseChunkSN)
{
chunk->state = empty;
gBaseChunkSN = chunkSN + 1;
if (gBaseChunkSN >= maxChunkCount)
{
gBaseChunkSN %= maxChunkCount;
gHighChunkSN = maxChunkCount;
}
else if (gHighChunkSN != maxChunkCount)
gHighChunkSN = gBaseChunkSN;
if (gChunkWaitFlag && gNextChunkSN < gHighChunkSN)
pthread_cond_signal(&chunk_cond);
}
pthread_mutex_unlock(&chunk_mutex);
}
}
#define maxQItemCount 100
#pragma pack(16)
enum {virgin, nascent, reading, writing, skipping};
typedef struct
{
int stage;
llong fid;
int sfd, dfd;
char *src, *dst;
struct stat st;
CopyChunk *chunk;
} QItem;
QItem gQItemDispenser[maxQItemCount];
#pragma pack()
int gBaseQItemSN = 0;
int gHighQItemSN = maxQItemCount;
int gNextQItemSN = 0;
QItem *dispenseNewQItem(void)
{
QItem *qitem;
pthread_mutex_lock(&qitem_mutex);
while (gNextQItemSN == gHighQItemSN)
pthread_cond_wait_flag(&qitem_cond, &qitem_mutex, &gQItemWaitFlag);
qitem = &gQItemDispenser[gNextQItemSN];
if (++gNextQItemSN >= maxQItemCount)
{
gNextQItemSN = 0;
gHighQItemSN = gBaseQItemSN;
}
pthread_mutex_unlock(&qitem_mutex);
return qitem;
}
QItem *informReadingQItem(void)
{
QItem *qitem;
pthread_mutex_lock(&qitem_mutex);
qitem = &gQItemDispenser[gBaseQItemSN];
if (qitem->stage != nascent)
qitem = NULL;
pthread_mutex_unlock(&qitem_mutex);
return qitem;
}
QItem *informWritingQItem(void)
{
QItem *qitem;
pthread_mutex_lock(&qitem_mutex);
qitem = &gQItemDispenser[gBaseQItemSN];
if (qitem->stage != writing && qitem->stage != skipping)
qitem = NULL;
pthread_mutex_unlock(&qitem_mutex);
return qitem;
}
void recycleFinishedQItem(QItem *qitem)
{
if (qitem)
{
int qitemSN = (int)(((intptr_t)qitem - (intptr_t)gQItemDispenser)/sizeof(QItem));
pthread_mutex_lock(&qitem_mutex);
if (qitemSN >= gBaseQItemSN)
{
qitem->stage = virgin;
gBaseQItemSN = qitemSN + 1;
if (gBaseQItemSN >= maxQItemCount)
{
gBaseQItemSN %= maxQItemCount;
gHighQItemSN = maxQItemCount;
}
else if (gHighQItemSN != maxQItemCount)
gHighQItemSN = gBaseQItemSN;
if (gQItemWaitFlag && gNextQItemSN < gHighQItemSN)
pthread_cond_signal(&qitem_cond);
}
pthread_mutex_unlock(&qitem_mutex);
}
}
static inline void feedback(const char action, const char *path)
{
switch (gVerbosityLevel)
{
case 0:
return;
default:
case 1:
case 2:
putc(action, stdout);
fflush(stdout);
break;
case 3:
printf("%c %s\n", action, path);
fflush(stdout);
break;
}
}
#pragma mark ••• Passing the Attributes •••
void setAttributes(int sfd, int dfd, const char *src, const char *dst, struct stat *st)
{
ExtMetaData xmd;
getMetaData(sfd, src, st, &xmd);
if (sfd != -1)
close(sfd);
setMetaData(dfd, dst, &xmd);
if (dfd != -1)
close(dfd);
lchown(dst, st->st_uid, st->st_gid);
lchmod(dst, st->st_mode&ALLPERMS);
setTimesFlags(dst, st);
if (gVerbosityLevel >= 2 && !S_ISDIR(st->st_mode))
feedback('.', dst);
}
#pragma mark ••• Threaded Copying •••
int atomCopy(char *src, char *dst, struct stat *st);
void *reader(void *threadArg)
{
static char errorString[errStrLen];
while (gRunning)
{
QItem *qitem;
pthread_mutex_lock(&reader_mutex);
while (!(qitem = informReadingQItem())) // wait for new items that are ready for reading
pthread_cond_wait_flag(&reader_cond, &reader_mutex, &gReaderWaitFlag);
qitem->stage = reading; // ready for reading now
pthread_mutex_unlock(&reader_mutex);
int in = open(qitem->src, O_RDONLY);
if (in != -1)
{
qitem->sfd = in;
if (gReadNoCache)
fnocache(in);
int state;
size_t size;
CopyChunk *chunk;
do
{
chunk = dispenseEmptyChunk();
if ((size = read(in, chunk->buffer, chunkBlckSize)) != -1)
{
chunk->rc = NO_ERROR;
chunk->size = size;
chunk->state = state = (size < chunkBlckSize) ? final : ready;
}
else
{
// Nothing good can be done here. Only drop a, message, and set the chunk size to 0.
// The writer has to resolve the issue. Perhaps it is already waiting for the next chunk,
// and therefore, the process may not be stopped here.
chunk->rc = SRC_ERROR;
chunk->size = 0;
chunk->state = state = final;
gErrorCount++;
strerror_r(abs(chunk->rc), errorString, errStrLen);
printf("\nRead error on file %s: %s.\n", qitem->src, errorString);
}
pthread_mutex_lock(&writer_mutex);
if (qitem->stage == reading)
{
qitem->chunk = chunk;
qitem->stage = writing; // ready for writing now
}
if (gWriterWaitFlag)
pthread_cond_signal(&writer_cond);
pthread_mutex_unlock(&writer_mutex);
} while (state != final);
}
else // (in == -1)
{
// Most probably, the file has been deleted after it was scheduled for copying.
// Drop a message, and tell the writer to skip this one.
gErrorCount++;
strerror_r(errno, errorString, errStrLen);
printf("\nFile %s could not be opened for reading: %s.\n", qitem->src, errorString);
pthread_mutex_lock(&writer_mutex);
close(qitem->sfd); qitem->sfd = -1;
qitem->stage = skipping; // do not write anything, only clean-up
if (gWriterWaitFlag)
pthread_cond_signal(&writer_cond);
pthread_mutex_unlock(&writer_mutex);
}
}
return NULL;
}
void *writer(void *threadArg)
{
static char errorString[errStrLen];
while (gRunning)
{
QItem *qitem;
llong fid;
pthread_mutex_lock(&writer_mutex);
while (!(qitem = informWritingQItem())) // wait for items that are ready for writing
pthread_cond_wait_flag(&writer_cond, &writer_mutex, &gWriterWaitFlag);
fid = qitem->fid;
pthread_mutex_unlock(&writer_mutex);
if (qitem->stage == skipping) // check for an invalidated qitem
goto cleanup; // skip writing
CopyChunk *chunk = qitem->chunk;
int out = open(qitem->dst, O_WRONLY|O_CREAT|O_TRUNC|O_EXLOCK, qitem->st.st_mode&ALLPERMS);
if (out != -1)
{
qitem->dfd = out;
if (gWriteNoCache)
fnocache(out);
int state, rc = NO_ERROR;
size_t fsize = 0;
do
{
state = chunk->state;
if (chunk->rc != NO_ERROR)
rc = chunk->rc;
else if (chunk->size == 0 || write(out, chunk->buffer, chunk->size) != -1)
{
fsize += chunk->size;
recycleFinishedChunk(chunk);
if (state != final)
{
pthread_mutex_lock(&writer_mutex);
while (!(chunk = informCurrentChunk()))
pthread_cond_wait_flag(&writer_cond, &writer_mutex, &gWriterWaitFlag);
pthread_mutex_unlock(&writer_mutex);
}
}
else
{
gErrorCount++;
recycleFinishedChunk(chunk);
while (state != final)
{
pthread_mutex_lock(&writer_mutex);
while (!(chunk = informCurrentChunk()))
pthread_cond_wait_flag(&writer_cond, &writer_mutex, &gWriterWaitFlag);
pthread_mutex_unlock(&writer_mutex);
state = chunk->state;
recycleFinishedChunk(chunk);
}
strerror_r(abs(rc = DST_ERROR), errorString, errStrLen);
printf("\nWrite error on file %s: %s.\n", qitem->dst, errorString);
}
} while (state != final && rc == NO_ERROR);
if (rc == NO_ERROR)
{
gTotalItems++;
gTotalSize += fsize;
if (fsize != qitem->st.st_size && // if the file size changed then most probably
lstat(qitem->src, &qitem->st) != NO_ERROR) // other things changed too, so lstat() again.
rc = SRC_ERROR;
}
else // (rc != NO_ERROR)
{
// 1. clean up
close(qitem->dfd); qitem->dfd = -1;
close(qitem->sfd); qitem->sfd = -1;
while (state != final)
{
recycleFinishedChunk(chunk);
pthread_mutex_lock(&writer_mutex);
while (!(chunk = informCurrentChunk()))
pthread_cond_wait_flag(&writer_cond, &writer_mutex, &gWriterWaitFlag);
state = chunk->state;
pthread_mutex_unlock(&writer_mutex);
}
// 2. try again using the atomic file copy routine
rc = atomCopy(qitem->src, qitem->dst, &qitem->st);
}
// Error check again -- the atomic file copy may have resolved a previous issue
if (rc == NO_ERROR)
setAttributes(qitem->sfd, qitem->dfd, qitem->src, qitem->dst, &qitem->st);
else // (rc != NO_ERROR)
{
gErrorCount++;
strerror_r(abs(rc), errorString, errStrLen);
printf("\nFile %s could not be copied to %s: %s.\n", qitem->src, qitem->dst, errorString);
}
}
else // (out == -1)
{
close(qitem->sfd); qitem->sfd = -1;
// Drop a message, remove the file from the queue, and clean-up without further notice.
gErrorCount++;
strerror_r(errno, errorString, errStrLen);
printf("\nFile %s could not be opened for writing: %s.\n", qitem->dst, errorString);
int state;
do
{
state = chunk->state;
recycleFinishedChunk(chunk);
if (state != final)
{
pthread_mutex_lock(&writer_mutex);
while (!(chunk = informCurrentChunk()))
pthread_cond_wait_flag(&writer_cond, &writer_mutex, &gWriterWaitFlag);
pthread_mutex_unlock(&writer_mutex);
}
} while (state != final);
}
cleanup:
deallocate_batch(false, VPR(qitem->dst), VPR(qitem->src), NULL);
recycleFinishedQItem(qitem);
pthread_mutex_lock(&reader_mutex);
if (gReaderWaitFlag)
pthread_cond_signal(&reader_cond);
pthread_mutex_unlock(&reader_mutex);
pthread_mutex_lock(&level_mutex);
gWriterLast = fid;
if (gLevelWaitFlag)
pthread_cond_signal(&level_cond);
pthread_mutex_unlock(&level_mutex);
}
return NULL;
}
void fileCopyScheduler(llong fid, char *src, char *dst, struct stat *st)
{
QItem *qitem = dispenseNewQItem();
pthread_mutex_lock(&reader_mutex);
qitem->stage = nascent;
qitem->fid = fid;
qitem->sfd = -1;
qitem->dfd = -1;
qitem->src = src;
qitem->dst = dst;
qitem->st = *st;
if (gReaderWaitFlag)
pthread_cond_signal(&reader_cond);
pthread_mutex_unlock(&reader_mutex);
}
#pragma mark ••• Atomic Copying •••
int atomCopy(char *src, char *dst, struct stat *st)
{
int in, out;
if ((in = open(src, O_RDONLY)) != -1)
if ((out = open(dst, O_WRONLY|O_CREAT|O_TRUNC|O_EXLOCK, st->st_mode&ALLPERMS)) != -1)
{
if (gReadNoCache)
fnocache(in);
if (gWriteNoCache)
fnocache(out);
int rc = NO_ERROR;
size_t size, filesize = 0;
char *buffer = allocate(chunkBlckSize, false);
do
{
if ((size = read(in, buffer, chunkBlckSize)) == -1)
rc = SRC_ERROR;
else if (size != 0 && write(out, buffer, size) == -1)
rc = DST_ERROR;
else
filesize += size;
} while (rc == NO_ERROR && size == chunkBlckSize);
deallocate(VPR(buffer), false);
close(out);
close(in);
gTotalItems++;
gTotalSize += filesize;
if (rc == NO_ERROR && filesize != st->st_size && // if the filesize changed then most probably
lstat(src, st) != NO_ERROR) // other things changed too, so lstat() again.
rc = SRC_ERROR;
return rc;
}
else // !out
{
close(in);
return DST_ERROR;
}
else // !in
return SRC_ERROR;
}
int hlnkCopy(char *src, char *dst, size_t dl, struct stat *st)
{
int rc;
if (!gHardLinking)
{
Node *ino = findINode(gHLinkINodes, st->st_ino);
if (ino && ino->value.pl.i == st->st_dev)
{
chflags(ino->name, 0);
rc = (link(ino->name, dst) == NO_ERROR) ? 0 : DST_ERROR;
gTotalItems++;
}
else
{
storeINode(gHLinkINodes, st->st_ino, dst, dl, st->st_dev);
rc = atomCopy(src, dst, st);
}
}
else
{
chflags(src, 0);
rc = (link(src, dst) == NO_ERROR) ? 0 : DST_ERROR;
gTotalItems++;
}
if (rc == NO_ERROR)
setAttributes(-1, -1, src, dst, st);
deallocate_batch(false, VPR(dst), VPR(src), NULL);
return rc;
}
int fileEmpty(char *src, char *dst, struct stat *st)
{
int rc;
int out = open(dst, O_WRONLY|O_CREAT|O_TRUNC|O_EXLOCK, st->st_mode&ALLPERMS);
if (out != -1)
{
setAttributes(-1, out, src, dst, st);
gTotalItems++;
rc = NO_ERROR;
}
else
rc = DST_ERROR;
deallocate_batch(false, VPR(dst), VPR(src), NULL);
return rc;
}
int slnkCopy(char *src, char *dst, struct stat *st)
{
int rc = NO_ERROR;
char *revpath = NULL;
size_t revlen, maxlen = 1024;
// 1. reveal the path pointed to by the link
revpath = allocate(maxlen, false);
while ((revlen = readlink(src, revpath, maxlen)) >= maxlen)
{
deallocate(VPR(revpath), false);
if ((maxlen += maxlen) <= 1048576)
revpath = allocate(maxlen += maxlen, false);
else
{
revpath = NULL; // if the length of the path to be revealed does'nt fit yet
rc = -1; // into 1 MB, then there is something screwed in our system
goto cleanup;
}
}
if (revlen == -1)
{
rc = SRC_ERROR;
goto cleanup;
}
else
revpath[revlen] = '\0';
// 2. create a new symlink at dst pointing to the revealed path
if (symlink(revpath, dst) == NO_ERROR)
{
setAttributes(-1, -1, src, dst, st);
gTotalItems++;
}
else
rc = DST_ERROR;
cleanup:
deallocate_batch(false, VPR(revpath), VPR(dst), VPR(src), NULL);
return rc;
}
#pragma mark ••• Directory Traversal •••
int dtType2stFmt(int d_type)
{
switch (d_type)
{
default:
case DT_UNKNOWN: // 0 - The type is unknown.
return 0;
case DT_FIFO: // 1 - A named pipe or FIFO.
return DT_FIFO;
case DT_CHR: // 2 - A character device.
return DT_CHR;
case DT_DIR: // 4 - A directory.
return S_IFDIR;
case DT_BLK: // 6 - A block device.
return S_IFBLK;
case DT_REG: // 8 - A regular file.
return S_IFREG;
case DT_LNK: // 10 - A symbolic link.
return S_IFLNK;
case DT_SOCK: // 12 - A local-domain socket.
return S_IFSOCK;
case DT_WHT: // 14 - A whiteout file. (somehow deleted, but not eventually yet)
return S_IFWHT;
}
}
int deleteDirectory(char *path, size_t pl);
int deleteDirEntity(char *path, size_t pl, llong st_mode)
{
static char errorString[errStrLen];
const char *ftype;
int err, rc = NO_ERROR;
switch (st_mode & S_IFMT)
{
case S_IFDIR: // A directory.
*(short *)&path[pl++] = *(short *)"/";
chflags(path, 0);
if (err = deleteDirectory(path, pl))
rc = err;
else if (rmdir(path) != NO_ERROR)
{
rc = errno;
gErrorCount++;
strerror_r(rc, errorString, errStrLen);
printf("\nDirectory %s could not be deleted: %s.\n", path, errorString);
}
else
feedback('-', path);
break;
case S_IFIFO: // A named pipe or FIFO.
case S_IFREG: // A regular file.
case S_IFLNK: // A symbolic link.
case S_IFSOCK: // A local-domain socket.
case S_IFWHT: // A whiteout file. (somehow deleted, but not eventually yet)
lchflags(path, 0);
if (unlink(path) != NO_ERROR)
{
rc = errno;
gErrorCount++;
strerror_r(rc, errorString, errStrLen);
printf("\nFile %s could not be deleted: %s.\n", path, errorString);
}
break;
case S_IFCHR: // A character device.
ftype = "a character device";
goto special;
case S_IFBLK: // A block device.
ftype = "a block device";
goto special;
default:
ftype = "of unknown type";
special:
printf("\n%s is %s, it could not be deleted.\n", path, ftype);
break;
}
return rc;
}
int deleteDirectory(char *path, size_t pl)
{
int rc = NO_ERROR;
DIR *dp;
struct dirent bep, *ep;
if (dp = opendir(path))
{
struct stat st;
while (readdir_r(dp, &bep, &ep) == 0 && ep)
if ( ep->d_name[0] != '.' || (ep->d_name[1] != '\0' &&
(ep->d_name[1] != '.' || ep->d_name[2] != '\0')))
{
// next path
size_t npl = pl + ep->d_namlen;
char *npath = strcpy(allocate(npl+2, false), path); strcpy(npath+pl, ep->d_name);
if (ep->d_type != DT_UNKNOWN)
rc = deleteDirEntity(npath, npl, dtType2stFmt(ep->d_type));
else if (lstat(npath, &st) != -1)
rc = deleteDirEntity(npath, npl, st.st_mode);
else
rc = DST_ERROR;
deallocate(VPR(npath), false);
}
closedir(dp);
}
return rc;
}
int deleteEntityTree(Node *syncNode, const char *path, size_t pl)
{
int rcL, rcR, rc = rcL = rcR = NO_ERROR;
if (syncNode->L)
rcL = deleteEntityTree(syncNode->L, path, pl);
if (syncNode->R)
rcR = deleteEntityTree(syncNode->R, path, pl);
size_t npl = pl + strlen(syncNode->name);
char *npath = strcpy(allocate(npl+2, false), path); strcpy(npath+pl, syncNode->name);
rc = deleteDirEntity(npath, npl, syncNode->value.pl.i);
deallocate_batch(false, VPR(npath), VPR(syncNode->name), VPR(syncNode), NULL);
return (rcL != NO_ERROR)
? rcL
: (rcR != NO_ERROR)
? rcR
: rc;
}
char *pickextension(char *name, int *naml)
{
int l;
for (l = *naml-1; l > 0 && name[l] != '.'; l--);
if (0 < l && l < *naml-1)
{
*naml -= l;
return name+l;
}
else
{
*naml = 0;
return NULL;
}
}
void clone(const char *src, size_t sl, const char *dst, size_t dl, struct stat *st)
{
static char errorString[errStrLen];
static llong fileID = 0;
struct stat sstat, dstat;
DIR *sdp, *ddp;
struct dirent *sep, *dep, bep;
// In incremental or synchronize mode, store the inventory of
// the destination directory into the key-name/value store.
Node *syncNode, **syncEntities = NULL;
if ((gIncremental || gSynchronize) && (ddp = opendir(dst)))
{
Value value = {{.i = 0}, Simple, NULL};
syncEntities = createTable(1024);
while (readdir_r(ddp, &bep, &dep) == NO_ERROR && dep)
if ( dep->d_name[0] != '.' || (dep->d_name[1] != '\0' &&
(dep->d_name[1] != '.' || dep->d_name[2] != '\0')))
{
size_t fpl = dl+dep->d_namlen;
char fullp[OSP(fpl+1)]; strcpy(fullp, dst); strcpy(fullp+dl, dep->d_name);
int extl = (int)dep->d_namlen;
char *fext = pickextension(dep->d_name, &extl);
if (!gExcludeList
|| !findFSName(gExcludeList, dep->d_name, dep->d_namlen)
&& !findFSName(gExcludeList, fullp, fpl)
&& !findFSName(gExcludeList, fext, extl))
if (dep->d_type == DT_DIR || dep->d_type == DT_REG || dep->d_type == DT_LNK)
{
value.pl.i = dtType2stFmt(dep->d_type);
storeFSName(syncEntities, dep->d_name, dep->d_namlen, &value);
}
else if (dep->d_type == DT_UNKNOWN) // need to call lstat()
{
if (lstat(fullp, &dstat) != -1 &&
((dstat.st_mode &= S_IFMT) == S_IFDIR || dstat.st_mode == S_IFREG || dstat.st_mode == S_IFLNK))
{
value.pl.i = dstat.st_mode;
storeFSName(syncEntities, dep->d_name, dep->d_namlen, &value);
}
}
}
closedir(ddp);
}
if (sdp = opendir(src))
{
int rc;
llong lastID = 0;
llong fCount = 0;
const char *ftype;
while (readdir_r(sdp, &bep, &sep) == NO_ERROR && sep)
if ( sep->d_name[0] != '.' || (sep->d_name[1] != '\0' &&
(sep->d_name[1] != '.' || sep->d_name[2] != '\0')))
{
int extl = (int)sep->d_namlen;
char *fext = pickextension(sep->d_name, &extl);
if (gExcludeList
&& (findFSName(gExcludeList, sep->d_name, sep->d_namlen)
|| findFSName(gExcludeList, fext, extl)))
{
if (syncEntities)
removeFSName(syncEntities, sep->d_name, sep->d_namlen);
continue;
}
// next source path
size_t nsl = sl + sep->d_namlen; // next source length
char *nsrc = strcpy(allocate(nsl+2, false), src); strcpy(nsrc+sl, sep->d_name);
if (gExcludeList && findFSName(gExcludeList, nsrc, nsl)