-
Notifications
You must be signed in to change notification settings - Fork 17
/
bpc_sysCalls.c
2210 lines (1994 loc) · 77.4 KB
/
bpc_sysCalls.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
/*
* Emulate system calls for BackupPC.
*
* Copyright (C) 2013 Craig Barratt.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, visit the http://fsf.org website.
*/
#include "backuppc/backuppc.h"
#include "ifuncs.h"
#include "lib/sysxattrs.h"
#define MAX_FD (64)
#define MAX_BUF_SZ (8 << 20) /* 8MB */
/*
* A freelist of unused MAX_BUF_SZ sized-buffers.
* We use the first sizeof(void*) bytes of the buffer as a single-linked
* list, with a NULL at the end
*/
static void *DataBufferFreeList = (void*)NULL;
extern int am_generator;
extern int always_checksum;
extern int preserve_hard_links;
extern int protocol_version;
extern int preserve_times;
static bpc_attribCache_info acNew;
static bpc_attribCache_info acOld;
static int acOldUsed;
static bpc_deltaCount_info DeltaNew;
static bpc_deltaCount_info DeltaOld;
static int LogLevel;
static int DoneInit = 0;
static int CompressLevel;
typedef struct _bpc_sysCall_stats {
ino_t Inode0, InodeCurr;
int64 ErrorCnt;
int64 ExistFileCnt, ExistFileSize, ExistFileCompSize;
int64 NewFileCnt, NewFileSize, NewFileCompSize;
int64 TotalFileCnt, TotalFileSize;
} bpc_sysCall_stats;
static bpc_sysCall_stats Stats;
static void logMsgCB(UNUSED(int errFlag), char *mesg, size_t mesgLen)
{
fwrite(mesg, 1, mesgLen, stderr);
fflush(stderr);
//rwrite(errFlag ? FERROR : FINFO, mesg, mesgLen, 0);
}
void bpc_sysCall_init(
char *topDir, /* backuppc top-level data dir path */
char *hostName, /* host name */
char *shareNameUM, /* unmangled share name */
int newBkupNum, /* new backup number */
int newCompress, /* compression level for new backup */
int prevBkupNum, /* prior backup number (or -1) */
int prevCompress, /* comperssion level for prior backup */
char *mergeBkupInfo, /* which backups to merge together on read */
ino_t inode0, /* starting inode number for this backup */
int attrib_new, /* flag to turn on new-style attrib file names */
int logLevel /* logging level */
)
{
bpc_strBuf *hostDir = bpc_strBuf_new();
extern int BPC_HardLinkMax;
extern int BPC_PoolV3Enabled;
bpc_logMsgCBSet(logMsgCB);
bpc_lib_conf_init(topDir, BPC_HardLinkMax, BPC_PoolV3Enabled, logLevel);
bpc_attribCache_init(&acNew, hostName, newBkupNum, shareNameUM, newCompress);
bpc_strBuf_snprintf(hostDir, 0, "%s/pc/%s/%d", topDir, hostName, newBkupNum);
bpc_poolRefDeltaFileInit(&DeltaNew, hostDir->s);
bpc_attribCache_setDeltaInfo(&acNew, &DeltaNew);
CompressLevel = newCompress;
if ( prevBkupNum >= 0 ) {
bpc_attribCache_init(&acOld, hostName, prevBkupNum, shareNameUM, prevCompress);
bpc_strBuf_snprintf(hostDir, 0, "%s/pc/%s/%d", topDir, hostName, prevBkupNum);
bpc_poolRefDeltaFileInit(&DeltaOld, hostDir->s);
bpc_attribCache_setDeltaInfo(&acOld, &DeltaOld);
acOldUsed = 1;
} else {
acOldUsed = 0;
}
bpc_strBuf_free(hostDir);
Stats.InodeCurr = Stats.Inode0 = inode0;
LogLevel = logLevel;
/*
* If attrib_new, write new-style attrib files (<= 4.0.0beta3 uses old-style), which
* are 0-length files with the digest encoded in the file name (eg: attrib_md5HexDigest).
* We can still read the old-style files, but we upgrade them as we go.
*/
bpc_attrib_backwardCompat(!attrib_new, !attrib_new);
if ( mergeBkupInfo && *mergeBkupInfo ) {
/*
* Count number of backups to merge: 1 + number of commas.
*/
int i, bkupCnt = 1;
char *p = mergeBkupInfo;
bpc_backup_info *bkupList;
while ( (p = strchr(p + 1, ',')) ) {
bkupCnt++;
}
p = mergeBkupInfo;
if ( !(bkupList = calloc(bkupCnt, sizeof(bpc_backup_info))) ) {
bpc_logErrf("bpc_sysCall_init: can't allocate backup list (%d)\n", bkupCnt);
return;
}
for ( i = 0 ; i < bkupCnt ; i++ ) {
if ( sscanf(p, "%d/%d/%d", &bkupList[i].num, &bkupList[i].compress, &bkupList[i].version) != 3 ) {
bpc_logErrf("bpc_sysCall_init: can't parse bkup info string %s\n", p);
return;
}
if ( !(p = strchr(p, ',')) ) break;
p++;
}
bpc_attribCache_setMergeList(&acNew, bkupList, bkupCnt);
}
DoneInit = 1;
}
void bpc_am_generator(int generator, int pid)
{
if ( generator ) {
Stats.InodeCurr = 2 * (Stats.InodeCurr / 2) + 0;
bpc_logMsgf("xferPids %d,%d\n", getpid(), pid);
} else {
Stats.InodeCurr = 2 * (Stats.InodeCurr / 2) + 1;
bpc_attribCache_readOnly(&acNew, 1);
if ( acOldUsed ) bpc_attribCache_readOnly(&acOld, 1);
}
bpc_lib_setTmpFileUnique(generator);
}
int bpc_sysCall_cleanup(void)
{
fflush(stdout);
fflush(stderr);
if ( LogLevel >= 4 ) bpc_logMsgf("bpc_sysCall_cleanup: doneInit = %d\n", DoneInit);
if ( !DoneInit ) return 0;
if ( am_generator ) {
if ( preserve_hard_links ) hard_link_bpc_update_link_count();
bpc_attribCache_flush(&acNew, 1, NULL);
if ( acOldUsed ) bpc_attribCache_flush(&acOld, 1, NULL);
}
if ( LogLevel >= 6 ) {
fprintf(stderr, "RefCnt Deltas for new backup\n");
bpc_poolRefDeltaPrint(&DeltaNew);
if ( acOldUsed ) {
fprintf(stderr, "RefCnt Deltas for prev backup\n");
bpc_poolRefDeltaPrint(&DeltaOld);
}
}
Stats.ErrorCnt += bpc_poolRefDeltaFileFlush(&DeltaNew);
if ( acOldUsed ) {
Stats.ErrorCnt += bpc_poolRefDeltaFileFlush(&DeltaOld);
}
fprintf(stderr, "%s: %llu errors, %llu filesExist, %llu sizeExist, %llu sizeExistComp, %llu filesTotal, %llu sizeTotal, %llu filesNew, %llu sizeNew, %llu sizeNewComp, %llu inode\n",
am_generator ? "DoneGen" : "Done",
(unsigned long long)Stats.ErrorCnt,
(unsigned long long)Stats.ExistFileCnt,
(unsigned long long)Stats.ExistFileSize,
(unsigned long long)Stats.ExistFileCompSize,
(unsigned long long)Stats.TotalFileCnt,
(unsigned long long)Stats.TotalFileSize,
(unsigned long long)Stats.NewFileCnt,
(unsigned long long)Stats.NewFileSize,
(unsigned long long)Stats.NewFileCompSize,
(unsigned long long)Stats.InodeCurr);
fflush(stdout);
fflush(stderr);
return Stats.ErrorCnt;
}
void bpc_progress_fileDone(void)
{
static int fileCnt = 0, fileCntNext = 1;
fileCnt++;
if ( fileCnt < fileCntNext ) return;
fileCntNext = fileCnt + 20;
fprintf(stderr, "__bpc_progress_fileCnt__ %d\n", fileCnt);
}
void bpc_sysCall_statusFileSize(unsigned long fileSize)
{
Stats.TotalFileCnt++;
Stats.TotalFileSize += fileSize;
bpc_progress_fileDone();
}
void bpc_sysCall_setInode0Debug(int inode0, char *hostName, char *shareNameUM, int prevBkupNum, int prevCompress)
{
Stats.Inode0 = inode0;
if ( prevBkupNum >= 0 ) {
bpc_attribCache_init(&acOld, hostName, prevBkupNum, shareNameUM, prevCompress);
acOldUsed = 1;
}
}
/*
* File handling
*/
typedef struct {
int used;
int fdNum;
int flags;
int dirty;
int mode;
int fdUnusedNext;
off_t posn;
off_t fileSize;
int tmpFd;
bpc_strBuf *fileName;
bpc_strBuf *tmpFileName;
char *buffer;
size_t bufferSize;
bpc_digest digest;
} FdInfo;
static FdInfo Fd[MAX_FD];
/*
* We keep a simple integer index free list. This is the head of the free list.
* Fd[i].fdUnusedNext points to the next entry on the free list.
*/
static int FdUnused = -1;
/*
* Find a spare file descriptor. The integer file descriptors we return
* here have no relation to real file descriptors. They are simply handles
* that allow us to index into the $io->{fileDesc} array. So long as the
* caller uses the returned file descriptor only to call these functions
* (ie: not to directly make IO system calls) then we are ok.
*/
static int bpc_fileDescriptorNew(void)
{
int i;
if ( FdUnused < 0 ) {
/*
* initialize the free list
*/
FdUnused = 3;
for ( i = FdUnused ; i < MAX_FD ; i++ ) {
Fd[i].used = 0;
Fd[i].fdNum = i;
Fd[i].tmpFd = -1;
Fd[i].fdUnusedNext = i + 1;
}
Fd[MAX_FD-1].fdUnusedNext = -1;
}
i = FdUnused;
if ( !Fd[i].fileName ) {
Fd[i].fileName = bpc_strBuf_new();
Fd[i].tmpFileName = bpc_strBuf_new();
}
if ( i >= 0 ) {
FdUnused = Fd[i].fdUnusedNext;
return i;
} else {
bpc_logErrf("bpc_fileDescriptorNew: out of file descriptors\n");
Stats.ErrorCnt++;
return -1;
}
}
static void bpc_fileDescFree(FdInfo *fd)
{
if ( LogLevel >= 4 ) bpc_logMsgf("bpc_fileDescFree: fdNum = %d, tmpFd = %d, tmpFileName = %s\n", fd->fdNum, fd->tmpFd, fd->tmpFd >= 0 ? fd->tmpFileName->s : "NULL");
if ( fd->tmpFd >= 0 ) {
close(fd->tmpFd);
unlink(fd->tmpFileName->s);
fd->tmpFd = -1;
}
if ( fd->buffer ) {
*(void**)fd->buffer = DataBufferFreeList;
DataBufferFreeList = fd->buffer;
}
fd->used = 0;
fd->buffer = NULL;
fd->fdUnusedNext = FdUnused;
FdUnused = fd->fdNum;
}
static int TmpFileCnt = 0;
static int bpc_fileWriteBuffer(int fdNum, char *buffer, size_t nBytes)
{
while ( nBytes > 0 ) {
ssize_t nWrite = write(fdNum, buffer, nBytes);
if ( nWrite < 0 ) {
if ( errno == EINTR ) continue;
return -1;
}
buffer += nWrite;
nBytes -= nWrite;
}
return 0;
}
/*
* Create a temporary output file and write the existing data buffer there
*/
static int bpc_fileSwitchToDisk(bpc_attribCache_info *ac, FdInfo *fd)
{
bpc_strBuf_snprintf(fd->tmpFileName, 0, "%s/rsyncTmp.%d.%d.%d", ac->backupTopDir->s, getpid(), am_generator, TmpFileCnt++);
if ( (fd->tmpFd = open(fd->tmpFileName->s, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0 ) {
bpc_logErrf("bpc_fileSwitchToDisk: can't open/create %s for writing\n", fd->tmpFileName->s);
Stats.ErrorCnt++;
return -1;
}
if ( fd->fileSize > 0 && bpc_fileWriteBuffer(fd->tmpFd, fd->buffer, fd->fileSize) ) return -1;
if ( lseek(fd->tmpFd, fd->posn, SEEK_SET) != fd->posn ) {
bpc_logErrf("bpc_fileSwitchToDisk: unable to seek %s to %lu\n", fd->tmpFileName->s, (unsigned long)fd->posn);
Stats.ErrorCnt++;
return -1;
}
return 0;
}
/*
* Open a file and cache the file handle and information. Returns a pointer
* to an FdInfo structure, or NULL on error.
*
* The file is either stored in memory (fh->buffer) or a regular
* uncompressed unbuffered read-write file with handle fh->tmpFd.
*
* The current seek position is fh->posn.
*
* If you call fileClose() first, then fileOpen() with $write <= 0, you are
* guaranteed to get just the read-only BackupPC::FileZIO handle fh->fhRead
* or the in-memory fh->buffer.
*
* The following flags determine behavior:
* O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_TRUNC, O_APPEND
*/
static FdInfo *bpc_fileOpen(bpc_attribCache_info *ac, char *fileName, int flags)
{
bpc_attrib_file *file;
int fdNum;
FdInfo *fd;
file = bpc_attribCache_getFile(&acNew, fileName, 0, 0);
if ( !file && !(flags & O_CREAT) ) return NULL;
if ( (fdNum = bpc_fileDescriptorNew()) < 0 ) return NULL;
fd = &Fd[fdNum];
fd->used = 1;
fd->posn = 0;
fd->fdNum = fdNum;
fd->flags = flags;
fd->dirty = 0;
fd->bufferSize = MAX_BUF_SZ;
if ( DataBufferFreeList ) {
fd->buffer = DataBufferFreeList;
DataBufferFreeList = *(void**)DataBufferFreeList;
} else {
fd->buffer = malloc(fd->bufferSize * sizeof(fd->buffer[0]));
}
fd->fileSize = 0;
if ( !fd->buffer ) {
bpc_logErrf("bpc_fileOpen: fatal error: can't allocate %lu bytes for data buffer\n", MAX_BUF_SZ);
bpc_fileDescFree(fd);
return NULL;
}
bpc_strBuf_strcpy(fd->fileName, 0, fileName);
if ( file && !(flags & O_TRUNC) && !(file->isTemp && file->size == 0) ) {
bpc_strBuf *fullPath = bpc_strBuf_new();
bpc_fileZIO_fd fdz;
/*
* need to read existing file
*/
if ( file->digest.len > 0 || file->size == 0 ) {
/*
* all non-empty V4+ files have digests, so use the digest to look in the pool
*/
if ( file->digest.len > 0 ) {
bpc_digest_md52path(fullPath, file->compress, &file->digest);
} else {
bpc_strBuf_strcpy(fullPath, 0, "/dev/null");
}
if ( bpc_fileZIO_open(&fdz, fullPath->s, 0, file->compress) ) {
bpc_logErrf("bpc_fileOpen: can't open pool file %s (from %s, %d, %d)\n", fullPath->s, fd->fileName->s, file->compress, file->digest.len);
Stats.ErrorCnt++;
bpc_fileDescFree(fd);
bpc_strBuf_free(fullPath);
return NULL;
}
} else {
/*
* either we failed to read the digest (eg, missing inode), or it's a V3 file - look in the backup directory
*/
bpc_attribCache_getFullMangledPath(&acNew, fullPath, (char*)fileName, file->backupNum);
if ( bpc_fileZIO_open(&fdz, fullPath->s, 0, file->compress) ) {
bpc_logErrf("bpc_fileOpen: can't open file %s (from %s, %d, %d, %d)\n",
fullPath->s, fd->fileName->s, file->compress, file->digest.len, file->nlinks);
Stats.ErrorCnt++;
bpc_fileDescFree(fd);
bpc_strBuf_free(fullPath);
return NULL;
}
}
bpc_strBuf_free(fullPath);
fd->fileSize = bpc_fileZIO_read(&fdz, (uchar*)fd->buffer, fd->bufferSize);
if ( fd->fileSize >= (off_t)fd->bufferSize ) {
off_t nRead;
/*
* buffer is full - write to flat disk and then copy the rest of the file
*/
fd->posn = fd->fileSize;
if ( bpc_fileSwitchToDisk(ac, fd) ) {
bpc_fileDescFree(fd);
return NULL;
}
while ( (nRead = bpc_fileZIO_read(&fdz, (uchar*)fd->buffer, fd->bufferSize)) > 0 ) {
if ( bpc_fileWriteBuffer(fd->tmpFd, fd->buffer, nRead) ) {
bpc_logErrf("bpc_fileOpen: bpc_fileWriteBuffer(%d, ..., %lu) failed\n",
fd->tmpFd, nRead);
bpc_fileDescFree(fd);
return NULL;
}
fd->fileSize += nRead;
}
}
bpc_fileZIO_close(&fdz);
}
if ( fd->tmpFd >= 0 ) {
if ( !(flags & O_APPEND) && lseek(fd->tmpFd, 0, SEEK_SET) != 0 ) {
bpc_logErrf("bpc_fileOpen: can't seek to start of file %s\n", fd->tmpFileName->s);
Stats.ErrorCnt++;
bpc_fileDescFree(fd);
return NULL;
}
} else {
if ( (flags & O_APPEND) ) fd->posn = fd->fileSize;
}
return fd;
}
static int bpc_fileWrite(bpc_attribCache_info *ac, FdInfo *fd, char *buf, size_t bufLen)
{
if ( fd->tmpFd < 0 ) {
if ( (size_t)fd->posn + bufLen <= fd->bufferSize ) {
if ( fd->posn + (off_t)bufLen > fd->fileSize || memcmp(fd->buffer + fd->posn, buf, bufLen) ) {
fd->dirty = 1;
memcpy(fd->buffer + fd->posn, buf, bufLen);
}
fd->posn += bufLen;
if ( fd->fileSize < fd->posn ) fd->fileSize = fd->posn;
return 0;
}
/*
* We would overflow the buffer, so write to a file instead
*/
if ( bpc_fileSwitchToDisk(ac, fd) ) return -1;
}
fd->dirty = 1;
return bpc_fileWriteBuffer(fd->tmpFd, buf, bufLen);
}
static int bpc_fileClose(bpc_attribCache_info *ac, FdInfo *fd, int newType, int newMode, char *fileNameLog)
{
bpc_attrib_file *file;
off_t fileSize = 0;
static bpc_poolWrite_info pwInfo;
bpc_digest digest;
int match;
off_t poolFileSize;
int errorCnt;
file = bpc_attribCache_getFile(&acNew, fd->fileName->s, 0, 0);
if ( file && newType < 0 ) newType = file->type;
if ( newType < 0 ) newType = BPC_FTYPE_FILE;
if ( file && file->size != fd->fileSize && ((fd->flags & O_WRONLY) || (fd->flags & O_RDWR)) ) {
fd->dirty = 1;
}
if ( !fd->dirty ) {
if ( (fd->flags & O_WRONLY) || (fd->flags & O_RDWR) ) {
fprintf(stderr, "IOdone: same %s\n", fd->fileName->s);
}
bpc_fileDescFree(fd);
return 0;
}
if ( fd->tmpFd < 0 ) {
fileSize = fd->fileSize;
bpc_poolWrite_open(&pwInfo, ac->compress, NULL);
bpc_poolWrite_write(&pwInfo, (uchar*)fd->buffer, fileSize);
bpc_poolWrite_close(&pwInfo, &match, &digest, &poolFileSize, &errorCnt);
} else {
off_t nRead;
lseek(fd->tmpFd, 0, SEEK_SET);
bpc_poolWrite_open(&pwInfo, ac->compress, NULL);
while ( (nRead = read(fd->tmpFd, fd->buffer, fd->bufferSize)) > 0 ) {
bpc_poolWrite_write(&pwInfo, (uchar*)fd->buffer, nRead);
fileSize += nRead;
}
bpc_poolWrite_close(&pwInfo, &match, &digest, &poolFileSize, &errorCnt);
}
Stats.ErrorCnt += errorCnt;
if ( match ) {
Stats.ExistFileCnt++;
if ( newType == BPC_FTYPE_FILE || newType == BPC_FTYPE_SYMLINK ) {
Stats.ExistFileSize += fileSize;
Stats.ExistFileCompSize += poolFileSize;
}
} else {
Stats.NewFileCnt++;
if ( newType == BPC_FTYPE_FILE || newType == BPC_FTYPE_SYMLINK ) {
Stats.NewFileSize += fileSize;
Stats.NewFileCompSize += poolFileSize;
}
}
if ( file && file->digest.len == digest.len && !memcmp(file->digest.digest, digest.digest, digest.len)
&& file->size == fileSize ) {
/*
* File is unchanged
*/
fprintf(stderr, "IOdone: same %s\n", fd->fileName->s);
bpc_fileDescFree(fd);
return 0;
}
if ( file && !file->isTemp ) {
if ( acOldUsed && file->inode < Stats.Inode0 && !bpc_attribCache_getFile(&acOld, fd->fileName->s, 0, 0) ) {
if ( file->nlinks > 0 ) {
/*
* Only write the inode if it doesn't exist in old;
* in that case increase the pool reference count
*/
if ( bpc_attribCache_setFile(&acOld, fd->fileName->s, file, 1) > 0 ) {
bpc_poolRefDeltaUpdate(&DeltaOld, file->compress, &file->digest, 1);
}
} else {
bpc_attribCache_setFile(&acOld, fd->fileName->s, file, 0);
}
} else {
/*
* The current file is new to this backup and will be replaced below, so reduce
* the ref count of the existing (old) file.
*/
bpc_poolRefDeltaUpdate(&DeltaNew, file->compress, &file->digest, -1);
}
} else if ( acOldUsed && (!file || !file->isTemp) && !bpc_attribCache_getFile(&acOld, fd->fileName->s, 0, 0) ) {
bpc_attrib_file *oldFile = bpc_attribCache_getFile(&acOld, fd->fileName->s, 1, 0);
oldFile->type = BPC_FTYPE_DELETED;
bpc_attribCache_setFile(&acOld, fd->fileName->s, oldFile, 0);
}
if ( !file ) {
file = bpc_attribCache_getFile(&acNew, fd->fileName->s, 1, 0);
file->inode = Stats.InodeCurr;
Stats.InodeCurr += 2;
file->nlinks = 0;
file->mode = fd->mode;
}
file->compress = ac->compress;
file->digest = digest;
if ( newType >= 0 ) file->type = newType;
if ( newMode >= 0 ) file->mode = newMode;
if ( file->type == BPC_FTYPE_FILE || file->type == BPC_FTYPE_SYMLINK ) {
file->size = fileSize;
} else {
file->size = 0;
}
if ( !file->isTemp ) bpc_poolRefDeltaUpdate(&DeltaNew, file->compress, &file->digest, 1);
bpc_attribCache_setFile(&acNew, fd->fileName->s, file, 0);
fprintf(stderr, "IOdone: %s %s\n", match ? "pool" : "new", fileNameLog ? fileNameLog : fd->fileName->s);
bpc_fileDescFree(fd);
return 0;
}
/*
* Read an entire file into the buffer, up until the buffer is full. Returns the number
* of bytes read, or -1 on error.
*/
static off_t bpc_fileReadAll(bpc_attribCache_info *ac, char *fileName, char *buffer, size_t bufferSize)
{
bpc_strBuf *fullPath;
bpc_attrib_file *file;
bpc_fileZIO_fd fd;
off_t nRead;
if ( !(file = bpc_attribCache_getFile(ac, fileName, 0, 0)) ) return -1;
fullPath = bpc_strBuf_new();
if ( file->digest.len > 0 ) {
/*
* V4+ pool file
*/
bpc_digest_md52path(fullPath, file->compress, &file->digest);
} else {
/*
* V3 look in the backup directory
*/
bpc_attribCache_getFullMangledPath(&acNew, fullPath, (char*)fileName, file->backupNum);
}
if ( bpc_fileZIO_open(&fd, fullPath->s, 0, file->compress) ) {
bpc_logErrf("bpc_fileReadAll: can't open %s (from %s)\n", fullPath->s, fileName);
Stats.ErrorCnt++;
bpc_strBuf_free(fullPath);
return -1;
}
nRead = bpc_fileZIO_read(&fd, (uchar*)buffer, bufferSize);
bpc_fileZIO_close(&fd);
bpc_strBuf_free(fullPath);
return nRead;
}
/*
* Directory handling
*/
typedef struct {
struct dirent dirent;
char *entries;
ssize_t entrySize;
ssize_t entryIdx;
} my_DIR;
char *bpc_mktemp(char *template)
{
char *p = template + strlen(template);
int i, xCnt = 0;
while ( p > template && p[-1] == 'X' ) {
p--;
xCnt++;
}
if ( xCnt == 0 ) return NULL;
for ( i = 0 ; i < (1 << (4 * xCnt)) ; i++ ) {
sprintf(p, "%0*x", xCnt, i);
if ( bpc_attribCache_getFile(&acNew, template, 0, 0)
|| (acOldUsed && bpc_attribCache_getFile(&acOld, template, 0, 0)) ) {
continue;
}
if ( LogLevel >= 7 ) bpc_logMsgf("bpc_mktemp: returning %s\n", template);
return template;
}
if ( LogLevel >= 7 ) bpc_logMsgf("bpc_mktemp: returning NULL\n");
return NULL;
}
int bpc_mkstemp(char *template, char *origFileName)
{
char *p = template + strlen(template);
bpc_attrib_file *file, *fileOrig = NULL;
int i, xCnt = 0;
FdInfo *fd;
while ( p > template && p[-1] == 'X' ) {
p--;
xCnt++;
}
if ( xCnt == 0 ) return -1;
for ( i = 0 ; i < (1 << (4 * xCnt)) ; i++ ) {
sprintf(p, "%0*x", xCnt, i);
if ( bpc_attribCache_getFile(&acNew, template, 0, 0)
|| (acOldUsed && bpc_attribCache_getFile(&acOld, template, 0, 0)) ) {
continue;
}
file = bpc_attribCache_getFile(&acNew, template, 1, 0);
if ( origFileName
&& (fileOrig = bpc_attribCache_getFile(&acNew, origFileName, 0, 0))
&& fileOrig->type == BPC_FTYPE_FILE ) {
/*
* We have been told that this temp file is an update of origFileName.
* If it exists, we copy all the attributes, including the digest,
* which is a cheap way to make the temp file look just like
* the orig file.
*/
if ( LogLevel >= 4 ) bpc_logMsgf("bpc_mkstemp: copying attribs from %s to %s\n", origFileName, template);
bpc_attrib_fileCopy(file, fileOrig);
/*
* Make sure the pool file exists; otherwise zero out the digest
*/
if ( file->digest.len > 0 ) {
bpc_strBuf *poolPath = bpc_strBuf_new();
STRUCT_STAT st;
bpc_digest_md52path(poolPath, file->compress, &file->digest);
if ( stat(poolPath->s, &st) ) {
if ( LogLevel >= 4 ) bpc_logMsgf("bpc_mkstemp: %s doesn't exist; ignoring digest for %s\n",
poolPath->s, template);
file->digest.len = 0;
file->size = 0;
}
bpc_strBuf_free(poolPath);
}
file->nlinks = 0;
} else {
/*
* No orig file, so create new attributes
*/
file->type = BPC_FTYPE_FILE;
file->mode = 0600;
file->compress = CompressLevel;
file->inode = Stats.InodeCurr;
Stats.InodeCurr += 2;
}
file->isTemp = 1;
bpc_attribCache_setFile(&acNew, template, file, 0);
if ( !(fd = bpc_fileOpen(&acNew, template, O_RDWR | O_CREAT)) ) {
if ( LogLevel >= 4 ) bpc_logMsgf("bpc_mkstemp: bpc_fileOpen(%s,...) failed, size = %d, digestLen = %d\n",
template, file->size, file->digest.len);
return -1;
}
if ( LogLevel >= 4 ) bpc_logMsgf("bpc_mkstemp: returning %s, fd = %d (size = %d, digestLen = %d)\n",
template, fd->fdNum, file->size, file->digest.len);
return fd->fdNum;
}
if ( LogLevel >= 4 ) bpc_logMsgf("bpc_mkstemp: returning -1\n");
return -1;
}
/*
* Confirm that fileName exists, has the indicated size and MD5 file_sum. If so, mimic
* mkstemp above by creating a temporary file copy, but don't open it.
*
* The is used to implement an optimization in receiver.c: if we are receiving file deltas,
* we check if the deltas show the file is identical. That avoids opening the file
* and copying it to the temp file (which involves a lot of processing given the
* compression overhead, and disk IO for large files).
*/
int bpc_sysCall_checkFileMatch(char *fileName, char *tmpName, struct file_struct *rsyncFile,
char *file_sum, off_t fileSize)
{
bpc_attrib_file *fileOrig, *file;
bpc_strBuf *poolPath;
if ( !(fileOrig = bpc_attribCache_getFile(&acNew, fileName, 0, 0)) ) {
/*
* Hmmm. The file doesn't exist, but we got deltas suggesting the file is
* unchanged. So that means the generator found a matching pool file.
* Let's try the same thing.
*/
if ( bpc_sysCall_poolFileCheck(fileName, rsyncFile)
|| !(fileOrig = bpc_attribCache_getFile(&acNew, fileName, 0, 0)) ) {
if ( fileSize > 0 ) {
bpc_logErrf("bpc_sysCall_checkFileMatch(%s): file doesn't exist\n", fileName);
}
return -1;
}
}
if ( fileOrig->size != fileSize || fileOrig->digest.len < MD5_DIGEST_LEN || memcmp(file_sum, fileOrig->digest.digest, MD5_DIGEST_LEN) ) {
if ( LogLevel >= 4 ) bpc_logMsgf("bpc_sysCall_checkFileMatch(%s): size/digest don't match (%lu/%lu, %d, 0x%02x%02x.../0x%02x%02x...\n",
fileName, (unsigned long)fileOrig->size, (unsigned long)fileSize,
fileOrig->digest.len,
fileOrig->digest.digest[0], fileOrig->digest.digest[1],
((unsigned)file_sum[0]) & 0xff, ((unsigned)file_sum[1]) & 0xff);
return -1;
}
/*
* make sure the pool file exists
*/
poolPath = bpc_strBuf_new();
bpc_digest_md52path(poolPath, CompressLevel, &fileOrig->digest);
if ( fileSize != 0 ) {
STRUCT_STAT st;
if ( stat(poolPath->s, &st) ) {
bpc_logErrf("bpc_sysCall_checkFileMatch(%s): got good match, but pool file %s doesn't exist - rewriting\n",
fileName, poolPath->s);
bpc_strBuf_free(poolPath);
return -1;
}
if ( st.st_mode & S_IXOTH ) {
/*
* pool file is marked for deletion - safely unmark it since we are going to use it
*/
if ( bpc_poolWrite_unmarkPendingDelete(poolPath->s) ) {
bpc_logErrf("bpc_sysCall_checkFileMatch(%s): couldn't unmark pool file %s - rewriting\n",
fileName, poolPath->s);
bpc_strBuf_free(poolPath);
return -1;
}
}
}
bpc_strBuf_free(poolPath);
/*
* Now mimic bpc_mkstemp() above
*/
if ( !get_tmpname(tmpName, fileName, 0) || !bpc_mktemp(tmpName) ) {
bpc_logErrf("bpc_sysCall_checkFileMatch(%s): tmp name failed\n", fileName);
return -1;
}
file = bpc_attribCache_getFile(&acNew, tmpName, 1, 0);
bpc_attrib_fileCopy(file, fileOrig);
file->nlinks = 0;
file->isTemp = 1;
bpc_attribCache_setFile(&acNew, tmpName, file, 0);
if ( LogLevel >= 4 ) bpc_logMsgf("bpc_sysCall_checkFileMatch(%s): good match, made copy in %s\n", fileName, tmpName);
fprintf(stderr, "IOdone: same %s\n", tmpName);
return 0;
}
/*
* This is called by the generator-side to see if there is a matching pool
* file that can be used for the block checksums. This needs to match
* mks_temp above to make sure the same basis file is used by each side.
*
* If there is a match, we create a temp file entry, which will be replaced
* when the receiver does the rename.
*/
int bpc_sysCall_poolFileCheck(char *fileName, struct file_struct *rsyncFile)
{
bpc_digest digest;
bpc_strBuf *poolPath;
unsigned int ext;
int foundPoolFile = 0;
if ( protocol_version < 30 || !always_checksum ) return -1;
poolPath = bpc_strBuf_new();
digest.len = MD5_DIGEST_LEN;
memcpy(digest.digest, F_SUM(rsyncFile), MD5_DIGEST_LEN);
/*
* find the first non-empty pool file in the chain
*/
if ( F_LENGTH(rsyncFile) > 0 ) {
for ( ext = 0 ; !foundPoolFile ; ext++ ) {
STRUCT_STAT st;
bpc_digest_append_ext(&digest, ext);
bpc_digest_md52path(poolPath, CompressLevel, &digest);
if ( stat(poolPath->s, &st) ) break;
if ( st.st_size == 0 ) continue;
if ( st.st_mode & S_IXOTH ) {
/*
* pool file is marked for deletion - safely unmark it since we going to use it
*/
if ( bpc_poolWrite_unmarkPendingDelete(poolPath->s) ) {
if ( LogLevel >= 4 ) bpc_logMsgf("bpc_sysCall_poolFileCheck(%s): couldn't unmark potential match %s\n", fileName, poolPath->s);
continue;
}
}
foundPoolFile = 1;
}
} else {
/*
* For an empty file there is no corresponding pool file, so just say it matches
*/
foundPoolFile = 1;
bpc_strBuf_strcpy(poolPath, 0, "");
}
if ( foundPoolFile ) {
bpc_attrib_file *file = bpc_attribCache_getFile(&acNew, fileName, 1, 0);
if ( LogLevel >= 4 ) bpc_logMsgf("bpc_sysCall_poolFileCheck(%s): potential match %s (len = %lu)\n", fileName, poolPath->s, (unsigned long)F_LENGTH(rsyncFile));
file->type = BPC_FTYPE_FILE;
file->size = F_LENGTH(rsyncFile);
file->mode = 0600;
file->inode = Stats.InodeCurr;
file->compress = CompressLevel;
Stats.InodeCurr += 2;
file->digest = digest;
file->isTemp = 1;
bpc_strBuf_free(poolPath);
return 0;
} else {
if ( LogLevel >= 4 ) bpc_logMsgf("bpc_sysCall_poolFileCheck(%s): no pool file at %s\n", fileName, poolPath->s);
bpc_strBuf_free(poolPath);
return -1;
}
}
/*
* Print file transfer status for retry and fail
*/
void bpc_sysCall_printfileStatus(char *fileName, char *status)
{
fprintf(stderr, "IOdone: %s %s\n", status, fileName);
}
/*
* TODO: do target if symlink?
*/
int bpc_lchmod(const char *fileName, mode_t mode)
{
bpc_attrib_file *file;
if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lchmod(%s, 0%o)\n", fileName, mode);
if ( !(file = bpc_attribCache_getFile(&acNew, (char*)fileName, 0, 0)) ) {
if ( !am_generator ) {
/*
* ignore receive lchmod setting
*/
return 0;
}
errno = ENOENT;
return -1;
}
if ( file->mode == mode ) return 0;
if ( acOldUsed && !file->isTemp && file->inode < Stats.Inode0 && !bpc_attribCache_getFile(&acOld, (char*)fileName, 0, 0) ) {
if ( bpc_attribCache_setFile(&acOld, (char*)fileName, file, 1) ) {
bpc_poolRefDeltaUpdate(&DeltaOld, file->compress, &file->digest, 1);
}
}
file->mode = mode;
bpc_attribCache_setFile(&acNew, (char*)fileName, file, 0);
return 0;
}
int bpc_fchmod(int filedes, mode_t mode)
{
if ( filedes < 0 || filedes >= MAX_FD || !Fd[filedes].used ) {
errno = EBADF;
return -1;
}
if ( LogLevel >= 4 ) bpc_logMsgf("bpc_fchmod(%d (%s), 0%o)\n",
filedes, Fd[filedes].fileName->s, mode);
return bpc_lchmod(Fd[filedes].fileName->s, mode);
}
int bpc_unlink(const char *fileName)
{
bpc_attrib_file *file;
int deleteInode = 0;
if ( LogLevel >= 4 ) bpc_logMsgf("bpc_unlink(%s)\n", fileName);
if ( !(file = bpc_attribCache_getFile(&acNew, (char*)fileName, 0, 0)) ) {
/*
* See if the file is there, but it's the inode that is missing
*/
if ( !(file = bpc_attribCache_getFile(&acNew, (char*)fileName, 0, 1)) ) {
errno = ENOENT;
return -1;
}
/*
* Hmmm, this shouldn't happen (file entry is present, but inode wasn't found)
*/
if ( LogLevel >= 3 ) bpc_logMsgf("bpc_unlink(%s): type %d, size %lu, nlinks %u; inode %u missing; setting nlinks to 0\n",
fileName, file->type, file->size, file->nlinks, file->inode);
file->nlinks = 0;
}
if ( file->type == BPC_FTYPE_DIR ) {
errno = EISDIR;
return -1;
}
if ( file->nlinks > 0 ) {
if ( acOldUsed && !file->isTemp && !bpc_attribCache_getInode(&acOld, file->inode, 0) ) {
/*
* copy the inode to old
*/
if ( LogLevel >= 6 ) bpc_logMsgf("bpc_unlink: setting inode in old (inode = %lu, nlinks = %lu)\n",
(unsigned long)file->inode, (unsigned long)file->nlinks);
bpc_attribCache_setInode(&acOld, file->inode, file);
bpc_poolRefDeltaUpdate(&DeltaOld, file->compress, &file->digest, 1);
}
/*
* If this file is older than this backup, then move it to old
* (don't update the inode, since we know it exists after we just
* copied it).
*/
if ( file && file->inode < Stats.Inode0 && acOldUsed && !file->isTemp && !bpc_attribCache_getFile(&acOld, (char*)fileName, 0, 0) ) {
if ( LogLevel >= 6 ) bpc_logMsgf("bpc_unlink: setting %s in old (inode = %lu, nlinks = %lu)\n",
fileName, (unsigned long)file->inode, (unsigned long)file->nlinks);
bpc_attribCache_setFile(&acOld, (char*)fileName, file, 1);
}
/*
* Now reduce the number of links and update the inode
* ref count is handled above
*/
file->nlinks--;
if ( file->nlinks <= 0 ) {
deleteInode = 1;
bpc_poolRefDeltaUpdate(&DeltaNew, file->compress, &file->digest, -1);