File: bpc_sysCalls.c

package info (click to toggle)
backuppc-rsync 3.1.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,772 kB
  • sloc: ansic: 46,855; sh: 3,369; perl: 1,932; makefile: 166; python: 83
file content (2210 lines) | stat: -rw-r--r-- 79,278 bytes parent folder | download | duplicates (3)
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
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
/*
 * 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);
        } else {
            if ( LogLevel >= 6 ) bpc_logMsgf("bpc_unlink: updating inode in new (inode = %lu, nlinks = %lu)\n", (unsigned long)file->inode, (unsigned long)file->nlinks);
            bpc_attribCache_setInode(&acNew, file->inode, file);
        }
    } else {
        /*
         * If this file is older than this backup, then move it
         * to old.  Otherwise just remove it.
         */
        if ( !file->isTemp && file->inode < Stats.Inode0 && acOldUsed ) {
            bpc_attrib_file *fileOld = bpc_attribCache_getFile(&acOld, (char*)fileName, 0, 0);

            if ( !fileOld ) {
                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, 0);
                bpc_poolRefDeltaUpdate(&DeltaOld, file->compress, &file->digest, 1);
                bpc_poolRefDeltaUpdate(&DeltaNew, file->compress, &file->digest, -1);
            }
        } else if ( !file->isTemp ) {
            if ( file->digest.len > 0 ) {
                bpc_poolRefDeltaUpdate(&DeltaNew, file->compress, &file->digest, -1);
            }
        }
    }
    if ( deleteInode ) {
        if ( LogLevel >= 6 ) bpc_logMsgf("bpc_unlink: deleting inode in new (inode = %lu)\n",
                                            (unsigned long)file->inode);
        bpc_attribCache_deleteInode(&acNew, file->inode);
    }
    bpc_attribCache_deleteFile(&acNew, (char*)fileName);
    return 0;
}

int bpc_lstat(const char *fileName, struct stat *buf)
{
    bpc_attrib_file *file;
    dev_t rdev = 0;

    /*
     * must be in order of BPC_FTYPE_* definitions
     */
    static uint fmode[] = {
        S_IFREG,                /* BPC_FTYPE_FILE */
        S_IFREG,                /* BPC_FTYPE_HARDLINK */
        S_IFLNK,                /* BPC_FTYPE_SYMLINK */
        S_IFCHR,                /* BPC_FTYPE_CHARDEV */
        S_IFBLK,                /* BPC_FTYPE_BLOCKDEV */
        S_IFDIR,                /* BPC_FTYPE_DIR */
        S_IFIFO,                /* BPC_FTYPE_FIFO */
        S_IFREG,                /* BPC_FTYPE_UNKNOWN */
        S_IFSOCK,               /* BPC_FTYPE_SOCKET */
        S_IFREG,                /* BPC_FTYPE_UNKNOWN */
        S_IFREG,                /* BPC_FTYPE_DELETED */
    };

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lstat(%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)
         * This entry won't have a digest, so we won't be able to open the file.
         */
        if ( LogLevel >= 3 ) bpc_logMsgf("bpc_lstat(%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_DELETED || file->type == BPC_FTYPE_UNKNOWN || file->type >= BPC_FTYPE_INVALID ) {
        errno = ENOENT;
        return -1;
    }
    if ( file->type == BPC_FTYPE_CHARDEV || file->type == BPC_FTYPE_BLOCKDEV ) {
        char data[BPC_MAXPATHLEN];
        int minor = 0, major = 0;
        int nRead = bpc_fileReadAll(&acNew, (char*)fileName, data, sizeof(data) - 1);

        rdev = 1;
        if ( nRead >= 0 ) {
            data[nRead] = '\0';
            if ( sscanf(data, "%d,%d", &major, &minor) == 2 ) {
                rdev = MAKEDEV(major, minor);
            }
        }
    }
    buf->st_dev = 1;
    buf->st_ino = file->inode;
    buf->st_mode = file->mode;
    buf->st_nlink = file->nlinks == 0 ? 1 : file->nlinks;
    buf->st_uid = file->uid;
    buf->st_gid = file->gid;
    buf->st_rdev = rdev;
    buf->st_atime = file->mtime;
    buf->st_mtime = file->mtime;
    buf->st_ctime = file->mtime;
    buf->st_size  = file->size;
    buf->st_blocks = (file->size + 1023) / 1024;
    buf->st_blksize = 1024;

    if ( file->type < sizeof(fmode) / sizeof(fmode[0]) ) {
        buf->st_mode |= fmode[file->type];
    }

    return 0;
}

int bpc_fstat(int filedes, struct stat *buf)
{
    int ret;

    if ( filedes < 0 || filedes >= MAX_FD || !Fd[filedes].used ) {
        errno = EBADF;
        return -1;
    }
    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_fstat(%d (%s))\n", filedes, Fd[filedes].fileName->s);
    ret = bpc_lstat(Fd[filedes].fileName->s, buf);

    /*
     * TODO: needed??  Based on the current write file update the size...
     */
    return ret;
}

int bpc_stat(const char *fileName, struct stat *buf)
{
    bpc_attrib_file *file;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_stat(%s)\n", fileName);
    if ( (file = bpc_attribCache_getFile(&acNew, (char*)fileName, 0, 0)) && file->type == BPC_FTYPE_SYMLINK ) {
        char targetName[BPC_MAXPATHLEN];
        int nRead = bpc_fileReadAll(&acNew, (char*)fileName, targetName, sizeof(targetName) - 1);
        /*
         * TODO: combine fileName and targetName if targetName is relative
         * TODO: what happens if $targetName is a symlink?
         */
        if ( nRead <= 0 ) {
            errno = ENOENT;
            return -1;
        }
        targetName[nRead] = '\0';
        return bpc_lstat(targetName, buf);
    } else {
        return bpc_lstat(fileName, buf);
    }
}

int bpc_file_checksum(char *fileName, char *sum, int checksum_len)
{
    bpc_attrib_file *file = bpc_attribCache_getFile(&acNew, (char*)fileName, 0, 0); 
    bpc_strBuf *poolPath;
    STRUCT_STAT st;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_file_checksum(%s)\n", fileName);
    if ( !file || file->digest.len < checksum_len ) return -2;
    /*
     * check the pool file actually exists before returning the digest.
     */
    poolPath = bpc_strBuf_new();
    bpc_digest_md52path(poolPath, file->compress, &file->digest);
    if ( stat(poolPath->s, &st) ) {
        bpc_strBuf_free(poolPath);
        return -1;
    }
    if ( st.st_mode & S_IXOTH ) {
        /*
         * pool file is marked for deletion - safely unmark it since we are using it
         */
        if ( bpc_poolWrite_unmarkPendingDelete(poolPath->s) ) {
            bpc_logErrf("bpc_file_checksum(%s): couldn't unmark pool file %s - returning no match\n",
                                        fileName, poolPath->s);
            bpc_strBuf_free(poolPath);
            return -1;
        }
    }
    bpc_strBuf_free(poolPath);
    memcpy(sum, file->digest.digest, checksum_len);
    return 0;
}

/*
 * the link contents (ie: target) are kept in the original client
 * charset (ie: not converted to utf8).
 */
int bpc_symlink(const char *fileName, const char *symName)
{
    bpc_attrib_file *file;
    FdInfo *fd;
    int ret = 0;
    char logText[2 * BPC_MAXPATHLEN + 32];

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_symlink(%s, %s)\n", fileName, symName);
    /*
     * it's an error if symname exists
     */
    if ( (file = bpc_attribCache_getFile(&acNew, (char*)symName, 0, 0)) ) {
        errno = EEXIST;
        return -1;
    }

    /*
     * add delete attribute to old if nothing is in old
     */
    if ( acOldUsed && !bpc_attribCache_getFile(&acOld, (char*)symName, 0, 0) ) {
        file = bpc_attribCache_getFile(&acOld, (char*)symName, 1, 0);
        file->type = BPC_FTYPE_DELETED;
        bpc_attribCache_setFile(&acOld, (char*)symName, file, 0);
    }

    if ( !(fd = bpc_fileOpen(&acNew, (char*)symName, O_WRONLY | O_CREAT | O_TRUNC)) ) {
        bpc_logErrf("bpc_symlink: open/create of %s failed\n", symName);
        Stats.ErrorCnt++;
        return -1;
    }
    if ( bpc_fileWrite(&acNew, fd, (char*)fileName, strlen(fileName)) ) {
        bpc_logErrf("bpc_symlink: write failed\n");
        ret = -1;
        Stats.ErrorCnt++;
    }
    snprintf(logText, sizeof(logText), "%s -> %s", symName, fileName);
    if ( bpc_fileClose(&acNew, fd, BPC_FTYPE_SYMLINK, 0777, logText) ) {
        bpc_logErrf("bpc_symlink: close failed\n");
        ret = -1;
        Stats.ErrorCnt++;
    }
    return ret;
}

int bpc_link(const char *targetName, const char *linkName)
{
    bpc_attrib_file *file;
    bpc_strBuf *poolPath;
    STRUCT_STAT st;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_link(%s, %s)\n", targetName, linkName);

    if ( bpc_attribCache_getFile(&acNew, (char*)linkName, 0, 0) ) {
        errno = EEXIST;
        return -1;
    }

    /*
     * check if the target exists.  hardlinks to directories are not supported.
     */
    if ( !(file = bpc_attribCache_getFile(&acNew, (char*)targetName, 0, 0))
                    || file->type == BPC_FTYPE_DIR ) {
        errno = ENOENT;
        return -1;
    }

    /*
     * reference counts are unchanged in each of these cases (first link and additional link)
     */
    if ( file->nlinks == 0 ) {
        /*
         * first save the original target file (since it a regular file with no links)
         */
        if ( acOldUsed && !bpc_attribCache_getFile(&acOld, (char*)targetName, 0, 0) ) {
            bpc_attribCache_setFile(&acOld, (char*)targetName, file, 0);
            bpc_poolRefDeltaUpdate(&DeltaOld, file->compress, &file->digest, 1);
        }
        /*
         * promote the target to a hardlink; both files are identical
         */
        file->nlinks = 2;
        bpc_attribCache_setFile(&acNew, (char*)targetName, file, 0);
        bpc_attribCache_setFile(&acNew, (char*)linkName, file, 0);
    } else {
        /*
         * save the inode away since the link count is going to increase
         */
        if ( acOldUsed && !bpc_attribCache_getInode(&acOld, file->inode, 0) ) {
            bpc_attribCache_setInode(&acOld, file->inode, file);
            bpc_poolRefDeltaUpdate(&DeltaOld, file->compress, &file->digest, 1);
        }
        /*
         * reference count is unchanged since the inode already points at the pool file
         */
        file->nlinks++;
        bpc_attribCache_setFile(&acNew, (char*)linkName, file, 0);
    }

    Stats.ExistFileCnt++;
    Stats.ExistFileSize += file->size;
    poolPath = bpc_strBuf_new();
    bpc_digest_md52path(poolPath, file->compress, &file->digest);
    if ( !stat(poolPath->s, &st) ) Stats.ExistFileCompSize += st.st_size;
    bpc_strBuf_free(poolPath);

    if ( acOldUsed && !bpc_attribCache_getFile(&acOld, (char*)linkName, 0, 0) ) {
        file = bpc_attribCache_getFile(&acOld, (char*)linkName, 1, 0);
        file->type = BPC_FTYPE_DELETED;
        bpc_attribCache_setFile(&acOld, (char*)linkName, file, 0);
    }
    fprintf(stderr, "IOdone: new %s => %s\n", linkName, targetName);
    return 0;
}

/*
 * check and set the number of links on a file
 */
int bpc_nlinkSet(const char *targetName, uint32 nlinks)
{
    bpc_attrib_file *file;

    /*
     * check if the target exists.
     */
    if ( !(file = bpc_attribCache_getFile(&acNew, (char*)targetName, 0, 0)) ) {
        errno = ENOENT;
        return -1;
    }

    if ( file->nlinks == nlinks ) {
        if ( LogLevel >= 4 ) bpc_logMsgf("bpc_nlinkSet(%s, %u) -> no change\n", targetName, nlinks);
        return 0;
    }
    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_nlinkSet(%s, %u) -> was %u\n", targetName, nlinks, file->nlinks);

    file->nlinks = nlinks;;
    bpc_attribCache_setFile(&acNew, (char*)targetName, file, 0);
    return 0;
}

#ifdef HAVE_LUTIMES

int bpc_lutimes(const char *fileName, struct timeval *t)
{
    bpc_attrib_file *file;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lutimes(%s)\n", fileName);
    if ( !(file = bpc_attribCache_getFile(&acNew, (char*)fileName, 0, 0)) ) {
        if ( !am_generator ) {
            /*
             * ignore receive lutimes setting
             */
            return 0;
        }
        errno = ENOENT;
        return -1;
    }
    if ( file->mtime == t[1].tv_sec ) return 0;

    if ( file->inode < Stats.Inode0 && acOldUsed && !file->isTemp && !bpc_attribCache_getFile(&acOld, (char*)fileName, 0, 0) ) {
        if ( bpc_attribCache_setFile(&acOld, (char*)fileName, file, 1) > 0 ) {
            bpc_poolRefDeltaUpdate(&DeltaOld, file->compress, &file->digest, 1);
        }
    }
    file->mtime = t[1].tv_sec;
    bpc_attribCache_setFile(&acNew, (char*)fileName, file, 0);
    return 0;
}

#endif

#ifdef HAVE_UTIMES

int bpc_utimes(const char *fileName, struct timeval *t)
{
    bpc_attrib_file *file;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_utimes(%s)\n", fileName);

    if ( (file = bpc_attribCache_getFile(&acNew, (char*)fileName, 0, 0)) && file->type == BPC_FTYPE_SYMLINK ) {
        char targetName[BPC_MAXPATHLEN];
        int nRead = bpc_fileReadAll(&acNew, (char*)fileName, targetName, sizeof(targetName) - 1);
        /*
         * TODO: combine fileName and targetName if targetName is relative
         * TODO: what happens if $targetName is a symlink?
         */
        if ( nRead <= 0 ) {
            errno = ENOENT;
            return -1;
        }
        targetName[nRead] = '\0';
        return bpc_lutimes(targetName, t);
    } else {
        return bpc_lutimes(fileName, t);
    }
}

#endif

int bpc_lutime(const char *fileName, time_t mtime)
{
    bpc_attrib_file *file;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lutime(%s)\n", fileName);

    if ( !(file = bpc_attribCache_getFile(&acNew, (char*)fileName, 0, 0)) ) {
        if ( !am_generator ) {
            /*
             * ignore receive lutime setting
             */
            return 0;
        }
        errno = ENOENT;
        return -1;
    }
    if ( file->mtime == mtime ) return 0;

    if ( file->inode < Stats.Inode0 && acOldUsed && !file->isTemp && !bpc_attribCache_getFile(&acOld, (char*)fileName, 0, 0) ) {
        if ( bpc_attribCache_setFile(&acOld, (char*)fileName, file, 1) > 0 ) {
            bpc_poolRefDeltaUpdate(&DeltaOld, file->compress, &file->digest, 1);
        }
    }
    file->mtime = mtime;
    bpc_attribCache_setFile(&acNew, (char*)fileName, file, 0);
    return 0;
}

int bpc_utime(const char *fileName, time_t mtime)
{
    bpc_attrib_file *file;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_utime(%s)\n", fileName);

    if ( (file = bpc_attribCache_getFile(&acNew, (char*)fileName, 0, 0)) && file->type == BPC_FTYPE_SYMLINK ) {
        char targetName[BPC_MAXPATHLEN];
        int nRead = bpc_fileReadAll(&acNew, (char*)fileName, targetName, sizeof(targetName) - 1);
        /*
         * TODO: combine fileName and targetName if targetName is relative
         * TODO: what happens if $targetName is a symlink?
         */
        if ( nRead <= 0 ) {
            errno = ENOENT;
            return -1;
        }
        targetName[nRead] = '\0';
        return bpc_lutime(targetName, mtime);
    } else {
        return bpc_lutime(fileName, mtime);
    }
}

int bpc_lchown(const char *fileName, uid_t uid, gid_t gid)
{
    bpc_attrib_file *file;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lchown(%s, %lu, %lu)\n",
                                    fileName, (unsigned long)uid, (unsigned long)gid);

    if ( !(file = bpc_attribCache_getFile(&acNew, (char*)fileName, 0, 0)) ) {
        if ( !am_generator ) {
            /*
             * ignore receive lchown setting
             */
            return 0;
        }
        errno = ENOENT;
        return -1;
    }
    if ( file->uid == uid && file->gid == gid ) return 0;

    if ( file->inode < Stats.Inode0 && acOldUsed && !file->isTemp && !bpc_attribCache_getFile(&acOld, (char*)fileName, 0, 0) ) {
        if ( bpc_attribCache_setFile(&acOld, (char*)fileName, file, 1) > 0 ) {
            bpc_poolRefDeltaUpdate(&DeltaOld, file->compress, &file->digest, 1);
        }
    }
    file->uid = uid;
    file->gid = gid;
    bpc_attribCache_setFile(&acNew, (char*)fileName, file, 0);
    return 0;
}

int bpc_rename(const char *oldName, const char *newName)
{
    bpc_attrib_file *file, *fileNew;
    int oldIsTemp, fileAttrChanged = 0;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_rename(%s, %s)\n", oldName, newName);

    if ( !(file = bpc_attribCache_getFile(&acNew, (char*)oldName, 0, 0)) ) {
        if ( LogLevel >= 4 ) bpc_logMsgf("bpc_rename: %s doesn't exist\n", oldName);
        errno = ENOENT;
        return -1;
    }
    if ( !am_generator ) {
        /*
         * We don't do renames on the receiver, since it's too hard to keep attribute updates on
         * both the generator and receiver sycnhronized.  Send the attributes to the generator so
         * it can do the rename.
         */
        static xbuf rename_msg = EMPTY_XBUF;
        char *bufP, *bufPnew;
        uint32 oldLen = strlen(oldName) + 1, newLen = strlen(newName) + 1;

        if ( !rename_msg.size ) {
            alloc_xbuf(&rename_msg, 4096);
        }
        if ( rename_msg.size < 3 * sizeof(uint32) + oldLen + newLen + 1024 ) {
            realloc_xbuf(&rename_msg, 3 * sizeof(uint32) + oldLen + newLen + 1024);
        }
        bufP = rename_msg.buf;
        SIVAL(bufP, 0, oldLen);        bufP += sizeof(uint32);
        SIVAL(bufP, 0, newLen);        bufP += sizeof(uint32);
        SIVAL(bufP, 0, file->isTemp);  bufP += sizeof(uint32);
        memcpy(bufP, oldName, oldLen); bufP += oldLen;
        memcpy(bufP, newName, newLen); bufP += newLen;
        bufPnew = (char*)bpc_attrib_file2buf(file, (uchar*)bufP, (uchar*)rename_msg.buf + rename_msg.size);
        if ( bufPnew > rename_msg.buf + rename_msg.size ) {
            ssize_t used = bufP - rename_msg.buf;
            realloc_xbuf(&rename_msg, (bufPnew - rename_msg.buf) + 4096);
            bufPnew = (char*)bpc_attrib_file2buf(file, (uchar*)rename_msg.buf + used, (uchar*)rename_msg.buf + rename_msg.size);
        }
        if ( LogLevel >= 6 ) bpc_logMsgf("Sending rename request (len=%d)\n", bufPnew - rename_msg.buf);
        send_msg(MSG_RENAME, rename_msg.buf, bufPnew - rename_msg.buf, 0);
        bpc_attribCache_setFile(&acNew, (char*)newName, file, 0);
        bpc_attribCache_deleteFile(&acNew, (char*)oldName);
        return 0;
    }

    oldIsTemp = file->isTemp;
    file->isTemp = 0;
    if ( (fileNew = bpc_attribCache_getFile(&acNew, (char*)newName, 0, 0)) ) {
        /*
         * If fileNew is a temporary file, just delete it
         */
        if ( fileNew->isTemp ) {
            bpc_attribCache_deleteFile(&acNew, (char*)newName);
            fileNew = NULL;
        } else {
            /*
             * If newName exists, and has different attributes, then we unlink the file.
             */
            if ( fileNew->nlinks > 0 ) {
                /*
                 * We are updating a file with hardlinks.  unlink() will break the existing hardlink.
                 * Give the oldName a new inode number, so that the hardlink will be re-established later
                 * if the files are still meant to be linked.
                 */
                file->inode      = Stats.InodeCurr;
                Stats.InodeCurr += 2;
            }
            if ( (fileAttrChanged = bpc_attrib_fileCompare(file, fileNew)) ) {
                if ( bpc_unlink(newName) ) return -1;
            }
        }
    }
    if ( file->type == BPC_FTYPE_DIR ) {
        bpc_strBuf *path = bpc_strBuf_new(), *pathOld = bpc_strBuf_new();

        bpc_attribCache_getFullMangledPath(&acNew, path, (char*)newName, file->backupNum);
        bpc_attribCache_getFullMangledPath(&acNew, pathOld, (char*)oldName, file->backupNum);

        if ( rename(pathOld->s, path->s) ) {
            bpc_logErrf("bpc_rename: directory rename %s -> %s failed\n", pathOld->s, path->s);
            errno = EACCES;
            bpc_strBuf_free(path);
            bpc_strBuf_free(pathOld);
            return -1;
        }
        bpc_strBuf_free(path);
        bpc_strBuf_free(pathOld);
    }
    if ( acOldUsed ) {
        if ( !oldIsTemp && !bpc_attribCache_getFile(&acOld, (char*)oldName, 0, 0) ) {
            if ( bpc_attribCache_setFile(&acOld, (char*)oldName, file, 1) > 0 ) {
                bpc_poolRefDeltaUpdate(&DeltaOld, file->compress, &file->digest, 1);
            }
        }
        if ( !fileNew && !bpc_attribCache_getFile(&acOld, (char*)newName, 0, 0) ) {
            bpc_attrib_file *fileOld = bpc_attribCache_getFile(&acOld, (char*)newName, 1, 0);
            fileOld->type = BPC_FTYPE_DELETED;
            bpc_attribCache_setFile(&acOld, (char*)newName, fileOld, 0);
        }
    }
    if ( !fileNew || fileAttrChanged ) {
	if ( oldIsTemp ) {
	    bpc_poolRefDeltaUpdate(&DeltaNew, file->compress, &file->digest, 1);
	}
        bpc_attribCache_setFile(&acNew, (char*)newName, file, 0);
    }
    bpc_attribCache_deleteFile(&acNew, (char*)oldName);
    fprintf(stderr, "IOrename: %lu %s%s\n", (unsigned long)strlen(oldName), oldName, newName);
    return 0;
}


int bpc_rename_request(char *oldName, char *newName, uint32 isTemp, char *bufP, char *bufEnd)
{
    bpc_attrib_file *file = bpc_attribCache_getFile(&acNew, (char*)oldName, 1, 0);
    if ( (bufP = (char*)bpc_attrib_buf2fileFull(file, (uchar*)bufP, (uchar*)bufEnd)) != bufEnd ) {
        bpc_logErrf("bpc_rename_request(%s,%s) got to %p vs end = %p\n", oldName, newName, bufP, bufEnd);
    }
    file->isTemp = isTemp;
    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_rename_request: name = %s, xattr cnt = %d\n", file->name, bpc_hashtable_entryCount(&file->xattrHT));
    return bpc_rename(oldName, newName);
}

int bpc_mknod(const char *fileName, mode_t mode, dev_t dev)
{
    bpc_attrib_file *file;
    int type;
    int ret = 0;
    FdInfo *fd;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_mknod(%s, 0%o, %lu)\n",
                            fileName, mode, (unsigned long)dev);

    if ( (file = bpc_attribCache_getFile(&acNew, (char*)fileName, 0, 0)) ) {
        errno = EEXIST;
        return -1;
    }
    if ( acOldUsed && !bpc_attribCache_getFile(&acOld, (char*)fileName, 0, 0) ) {
        file = bpc_attribCache_getFile(&acOld, (char*)fileName, 1, 0);
        file->type = BPC_FTYPE_DELETED;
        bpc_attribCache_setFile(&acOld, (char*)fileName, file, 0);
    }
    type = BPC_FTYPE_FILE;
    if ( (mode & S_IFMT) == S_IFIFO )  type = BPC_FTYPE_FIFO;
    if ( (mode & S_IFMT) == S_IFBLK )  type = BPC_FTYPE_BLOCKDEV;
    if ( (mode & S_IFMT) == S_IFCHR )  type = BPC_FTYPE_CHARDEV;
    if ( (mode & S_IFMT) == S_IFSOCK ) type = BPC_FTYPE_SOCKET;

    if ( type == BPC_FTYPE_BLOCKDEV || type == BPC_FTYPE_CHARDEV ) {
        bpc_strBuf *data = bpc_strBuf_new();

        if ( !(fd = bpc_fileOpen(&acNew, (char*)fileName, O_WRONLY | O_CREAT | O_TRUNC)) ) {
            bpc_logErrf("bpc_mknod: open/create of %s failed\n", fileName);
            Stats.ErrorCnt++;
            return -1;
        }
        bpc_strBuf_snprintf(data, 0, "%lu,%lu", (unsigned long)major(dev), (unsigned long)minor(dev));
        if ( bpc_fileWrite(&acNew, fd, data->s, strlen(data->s)) ) {
            bpc_logErrf("bpc_mknod: write failed\n");
            ret = -1;
            Stats.ErrorCnt++;
        }
        bpc_strBuf_free(data);
        if ( bpc_fileClose(&acNew, fd, type, mode & ~S_IFMT, (char*)fileName) ) {
            bpc_logErrf("bpc_mknod: close failed\n");
            ret = -1;
            Stats.ErrorCnt++;
        }
    } else {
        /*
         * empty file - just write attributes
         */
        file = bpc_attribCache_getFile(&acNew, (char*)fileName, 1, 0);
        file->type  = type;
        file->mode  = mode & ~S_IFMT;
        file->inode = Stats.InodeCurr;
        file->size  = 0;
        Stats.InodeCurr  += 2;
        bpc_attribCache_setFile(&acNew, (char*)fileName, file, 0);
        fprintf(stderr, "IOdone: new %s\n", fileName);
    }
    return ret;
}

int bpc_open(const char *fileName, int flags, mode_t mode)
{
    bpc_attrib_file *file;
    FdInfo *fd;

    /*
     * handle a special case of opening a directory.  If a directory
     * is being replaced by a file, the generator has removed the
     * directory already, but we (ie: the receiver) don't know
     * that yet.
     */
    if ( !am_generator && (file = bpc_attribCache_getFile(&acNew, (char*)fileName, 0, 0))
                       && file->type == BPC_FTYPE_DIR ) {
        if ( acOldUsed ) bpc_attribCache_setFile(&acOld, (char*)fileName, file, 1);
        bpc_attribCache_deleteFile(&acNew, (char*)fileName);
        if ( LogLevel >= 4 ) bpc_logMsgf("bpc_open(%s, 0x%x, 0%o) opening directory -> -1\n", fileName, flags, mode);
        return -1;
    }

    if ( !(fd = bpc_fileOpen(&acNew, (char*)fileName, flags)) ) {
        if ( LogLevel >= 4 ) bpc_logMsgf("bpc_open(%s, 0x%x, 0%o) -> -1\n", fileName, flags, mode);
        return -1;
    }
    fd->mode = mode;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_open(%s, 0x%x, 0%o) -> %d\n", fileName, flags, mode, fd->fdNum);

    return fd->fdNum;
}

int bpc_close(int fdNum)
{
    if ( fdNum < 0 || fdNum >= MAX_FD || !Fd[fdNum].used ) {
        errno = EBADF;
        return -1;
    }
    
    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_close(%d (%s))\n", fdNum, Fd[fdNum].fileName->s);

    return bpc_fileClose(&acNew, &Fd[fdNum], -1, -1, NULL);
}

off_t bpc_lseek(int fdNum, off_t offset, int whence)
{
    FdInfo *fd;

    if ( fdNum < 0 || fdNum >= MAX_FD || !Fd[fdNum].used ) {
        errno = EBADF;
        return -1;
    }
    fd = &Fd[fdNum];

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lseek(%d (%s), %lu, %d)\n", fdNum, fd->fileName->s, offset, whence);

    if ( fd->tmpFd < 0 ) {
        off_t newPosn = -1;

        if ( whence == SEEK_SET )      newPosn = offset;
        else if ( whence == SEEK_CUR ) newPosn = fd->posn + offset;
        else if ( whence == SEEK_END ) newPosn = fd->fileSize + offset;
        if ( newPosn < 0 ) {
            errno = EINVAL;
            return -1;
        }
        if ( (size_t)newPosn < fd->bufferSize ) {
            fd->posn = newPosn;
            return fd->posn;
        }
        /*
         * We need to seek off the end of our in-memory buffer.
         * Switch to a file instead.
         */
        if ( bpc_fileSwitchToDisk(&acNew, fd) ) return -1;
    }
    return lseek(fd->tmpFd, offset, whence);
}

off_t bpc_ftruncate(int fdNum, off_t length)
{
    FdInfo *fd;

    if ( fdNum < 0 || fdNum >= MAX_FD || !Fd[fdNum].used ) {
        errno = EBADF;
        return -1;
    }
    fd = &Fd[fdNum];

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_ftruncate(%d (%s), %lu)\n", fdNum, fd->fileName->s, length);

    if ( fd->tmpFd < 0 ) {
        if ( length < 0 ) {
            errno = EINVAL;
            return -1;
        }
        if ( length == fd->fileSize ) {
            return 0;
        }
        if ( (size_t)length < fd->bufferSize ) {
            fd->dirty = 1;
            fd->fileSize = length;
            if ( fd->posn > fd->fileSize ) fd->posn = fd->fileSize;
            return 0;
        }
        /*
         * We need to make the file larger than the in-memory buffer.
         * Switch to a file instead.
         */
        if ( bpc_fileSwitchToDisk(&acNew, fd) ) return -1;
    }
    fd->dirty = 1;
    return ftruncate(fd->tmpFd, length);
}

ssize_t bpc_read(int fdNum, void *buf, size_t readSize)
{
    FdInfo *fd;

    if ( fdNum < 0 || fdNum >= MAX_FD || !Fd[fdNum].used ) {
        errno = EBADF;
        return -1;
    }
    fd = &Fd[fdNum];

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_read(%d (%s), buf, %lu) tmpFd = %d\n",
                                    fdNum, fd->fileName->s, readSize, fd->tmpFd);

    if ( fd->tmpFd < 0 ) {
        if ( fd->posn >= fd->fileSize || (size_t)fd->posn >= fd->bufferSize ) {
            bpc_logMsgf("bpc_read: read past EOF; readSize = %lu, posn = %lu, fileSize = %lu, bufferSize = %lu\n",
                                                 readSize, fd->posn, fd->fileSize, fd->bufferSize);
             return 0;
        }
        if ( readSize > (size_t)fd->fileSize - fd->posn ) readSize = fd->fileSize - fd->posn;
	if ( readSize > (size_t)fd->bufferSize - fd->posn ) readSize = fd->bufferSize - fd->posn;
        memcpy(buf, fd->buffer + fd->posn, readSize);
        fd->posn += readSize;
        return readSize;
    } else {
        return read(fd->tmpFd, buf, readSize);
    }
}

ssize_t bpc_write(int fdNum, const void *buf, size_t writeSize)
{
    FdInfo *fd;

    if ( fdNum < 0 || fdNum >= MAX_FD || !Fd[fdNum].used ) {
        errno = EBADF;
        return -1;
    }
    fd = &Fd[fdNum];

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_write(%d (%s), buf, %lu)\n",
                                    fdNum, fd->fileName->s, writeSize);

    if ( bpc_fileWrite(&acNew, fd, (char*)buf, writeSize) ) return -1;
    return writeSize;
}

ssize_t bpc_readlink(const char *fileName, char *buffer, size_t bufferSize)
{
    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_readlink(%s, buf, %lu)\n",
                                    fileName, bufferSize);

    return bpc_fileReadAll(&acNew, (char*)fileName, buffer, bufferSize);
}

int bpc_access(const char *fileName, int mode)
{
    bpc_attrib_file *file = bpc_attribCache_getFile(&acNew, (char*)fileName, 0, 0);

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_access(%s, %d) -> %d\n",
                                      fileName, mode, file ? 0 : -1);
    if ( !file ) {
        errno = ENOENT;
        return -1;
    } else {
        return 0;
    }
}

void bpc_tmpNameFlagSet(const char *tmpName)
{
    bpc_attrib_file *file = bpc_attribCache_getFile(&acNew, (char*)tmpName, 0, 0);

    if ( file && !file->isTemp ) {
        file->isTemp = 1;
        if ( file->digest.len > 0 ) {
            bpc_poolRefDeltaUpdate(&DeltaNew, file->compress, &file->digest, -1);
        }
    }
}

int bpc_chdir(const char *dirName)
{
    bpc_attrib_file *file;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_chdir(%s)\n", dirName);

    if ( !(file = bpc_attribCache_getFile(&acNew, (char*)dirName, 0, 0)) || file->type != BPC_FTYPE_DIR ) {
        errno = ENOENT;
        return -1;
    }
    bpc_attribCache_setCurrentDirectory(&acNew, (char*)dirName);
    if ( acOldUsed ) bpc_attribCache_setCurrentDirectory(&acOld, (char*)dirName);
    return 0;
}

int bpc_mkdir(const char *dirName, mode_t mode)
{
    bpc_strBuf *path = bpc_strBuf_new();
    bpc_attrib_file *file;
    int ret;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_mkdir(%s, 0%o)\n", dirName, mode);

    bpc_attribCache_getFullMangledPath(&acNew, path, (char*)dirName, -1);
    if ( bpc_attribCache_getFile(&acNew, (char*)dirName, 0, 0) ) {
        errno = EEXIST;
        bpc_strBuf_free(path);
        return -1;
    }

    if ( acOldUsed && !bpc_attribCache_getFile(&acOld, (char*)dirName, 0, 0) ) {
        file = bpc_attribCache_getFile(&acOld, (char*)dirName, 1, 0);
        file->type = BPC_FTYPE_DELETED;
        bpc_attribCache_setFile(&acOld, (char*)dirName, file, 0);
    }
    if ( (ret = bpc_path_create(path->s)) ) {
        bpc_strBuf_free(path);
        return ret;
    }
    file = bpc_attribCache_getFile(&acNew, (char*)dirName, 1, 0);
    file->type  = BPC_FTYPE_DIR;
    file->mode  = mode;
    file->inode = Stats.InodeCurr;
    if ( !(preserve_times & PRESERVE_DIR_TIMES) ) {
        /*
         * Normally dir mtimes are set later. But if --omit-dir-times was
         * specified, then we need to set to a reasonable value, ie now.
         */
        file->mtime = time(NULL);
    }
    Stats.InodeCurr += 2;
    bpc_attribCache_setFile(&acNew, (char*)dirName, file, 0);
    if ( !*dirName ) {
        fprintf(stderr, "IOdone: new .\n");
    } else {
        fprintf(stderr, "IOdone: new %s\n", dirName);
    }
    bpc_strBuf_free(path);
    return 0;
}

int bpc_rmdir(const char *dirName)
{
    bpc_strBuf *path = bpc_strBuf_new();
    bpc_attrib_file *file;
    STRUCT_STAT st;
    int statOk, cnt;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_rmdir(%s)\n", dirName);

    file = bpc_attribCache_getFile(&acNew, (char*)dirName, 0, 0);
    bpc_attribCache_getFullMangledPath(&acNew, path, (char*)dirName, file->backupNum);

    statOk = !stat(path->s, &st);

    if ( (!file || file->type != BPC_FTYPE_DIR) && (!statOk || !S_ISDIR(st.st_mode)) ) {
        errno = ENOENT;
        bpc_strBuf_free(path);
        return -1;
    }
    if ( (cnt = bpc_attribCache_getDirEntryCnt(&acNew, (char*)dirName)) > 0 ) {
        errno = ENOTEMPTY;
        bpc_strBuf_free(path);
        return -1;
    }

    /*
     * Remove the directory (and update reference counts).  We need
     * to first flush the attrib cache below this directory.
     * If this directory is older than this backup, then move the
     * attributes to old.
     *
     * TODO: is dirName in the right charset?
     */
    bpc_attribCache_flush(&acNew, 0, (char*)dirName);
    if ( statOk ) bpc_path_remove(&DeltaNew, path->s, acNew.compress);
    if ( file && file->inode < Stats.Inode0 && acOldUsed && !bpc_attribCache_getFile(&acOld, (char*)dirName, 0, 0) ) {
        bpc_attribCache_setFile(&acOld, (char*)dirName, file, 0);
    }
    bpc_attribCache_deleteFile(&acNew, (char*)dirName);
    bpc_strBuf_free(path);
    return 0;
}

DIR *bpc_opendir(const char *path)
{
    my_DIR *d;
    ssize_t entrySize;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_opendir(%s)\n", path);

    /*
     * Get the total number of bytes needed to store all the file names and inode numbers
     */
    if ( (entrySize = bpc_attribCache_getDirEntries(&acNew, (char*)path, NULL, 0)) < 0 ) return NULL;

    if ( !(d = calloc(1, sizeof(my_DIR))) ) return NULL;
    if ( !(d->entries = malloc(entrySize)) ) {
        free(d);
        return NULL;
    }

    /*
     * Now populate entries with all the file names, each NULL terminated, followed by the inode number.
     */
    d->entrySize = entrySize;
    if ( bpc_attribCache_getDirEntries(&acNew, (char*)path, d->entries, d->entrySize) != d->entrySize ) {
        free(d);
        free(d->entries);
        return NULL;
    }
    d->entryIdx = 0;
    return (DIR*)d;
}

struct dirent *bpc_readdir(DIR *dir)
{
    my_DIR *d = (my_DIR*)dir;

    if ( d->entryIdx >= d->entrySize ) {
        if ( LogLevel >= 4 ) bpc_logMsgf("bpc_readdir -> NULL\n");
        return NULL;
    }

    strncpy(d->dirent.d_name, d->entries + d->entryIdx, sizeof(d->dirent.d_name));
    d->dirent.d_name[sizeof(d->dirent.d_name)-1] = '\0';
    d->entryIdx += strlen(d->entries + d->entryIdx) + 1;
    memcpy(&d->dirent.d_ino, d->entries + d->entryIdx, sizeof(ino_t));
    d->entryIdx += sizeof(ino_t);
    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_readdir -> %s\n", d->dirent.d_name);
    return &d->dirent;
}

int bpc_closedir(DIR *dir)
{
    my_DIR *d = (my_DIR*)dir;

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_closedir()\n");

    if ( d->entries) free(d->entries);
    free(d);
    return 0;
}

/* 
 * xattr handling
 */
ssize_t bpc_lgetxattr(const char *path, const char *name, void *value, size_t size)
{
    bpc_attrib_file *file = bpc_attribCache_getFile(&acNew, (char*)path, 0, 0);
    bpc_attrib_xattr *xattr;

    if ( !file ) {
        if ( !am_generator ) {
            errno = ENOATTR;
            if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lgetxattr(%s, %s, %lu) -> no file, but return no xattr found\n", path, name, size);
            return -1;
        }
        errno = ENOENT;
        if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lgetxattr(%s, %s, %lu) -> file not found\n", path, name, size);
        return -1;
    }
    if ( !(xattr = bpc_attrib_xattrGet(file, (char*)name, strlen(name) + 1, 0)) ) {
        errno = ENOATTR;
        if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lgetxattr(%s, %s, %lu) -> xattr not found\n", path, name, size);
        return -1;
    }
    if ( !value || size == 0 ) return xattr->valueLen;
    if ( xattr->valueLen <= size ) {
        memcpy(value, xattr->value, xattr->valueLen);
        if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lgetxattr(%s, %s, %lu) -> returning value len %u\n", path, name, size, xattr->valueLen);
        return xattr->valueLen;
    } else {
        errno = ERANGE;
        if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lgetxattr(%s, %s, %lu) -> return buffer too small (len %u)\n", path, name, size, xattr->valueLen);
        return -1;
    }
}

ssize_t bpc_fgetxattr(int filedes, const char *name, void *value, size_t size)
{
    if ( filedes < 0 || filedes >= MAX_FD || !Fd[filedes].used ) {
        errno = EBADF;
        return -1;
    }

    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_fgetxattr(%d (%s), %s)\n", filedes, Fd[filedes].fileName->s, name);

    return bpc_lgetxattr(Fd[filedes].fileName->s, name, value, size);
}

int bpc_lsetxattr(const char *path, const char *name, const void *value, size_t size, UNUSED(int flags))
{
    bpc_attrib_file *file = bpc_attribCache_getFile(&acNew, (char*)path, 0, 0);
    bpc_attrib_xattr *xattr;
    int ret;

    if ( !am_generator && (!file || !file->isTemp) ) {
        /*
         * We don't set xattrs for non-temp files on the receiver, since it's too hard to keep
         * attribute updates on both the generator and receiver sycnhronized.  Send the xattr
         * setting to the generator so it can do the setting.
         */
        static xbuf rename_msg = EMPTY_XBUF;
        char *bufP;
        uint32 pathLen = strlen(path) + 1, nameLen = strlen(name) + 1;

        if ( !rename_msg.size ) {
            alloc_xbuf(&rename_msg, 4096);
        }
        if ( rename_msg.size < 3 * sizeof(uint32) + pathLen + nameLen + size + 1024 ) {
            realloc_xbuf(&rename_msg, 3 * sizeof(uint32) + pathLen + nameLen + size + 1024);
        }
        bufP = rename_msg.buf;
        SIVAL(bufP, 0, pathLen);       bufP += sizeof(uint32);
        SIVAL(bufP, 0, nameLen);       bufP += sizeof(uint32);
        SIVAL(bufP, 0, size);          bufP += sizeof(uint32);
        memcpy(bufP, path, pathLen);   bufP += pathLen;
        memcpy(bufP, name, nameLen);   bufP += nameLen;
        memcpy(bufP, value, size);     bufP += size;
        if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lsetxattr(%s, %s, %lu) -> sending to gen (len=%d)\n", path, name, size, bufP - rename_msg.buf);
        send_msg(MSG_XATTR_SET, rename_msg.buf, bufP - rename_msg.buf, 0);
        return 0;
    }

    if ( !file ) {
        errno = ENOENT;
        if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lsetxattr(%s, %s, %lu) -> file not found\n", path, name, size);
        return -1;
    }

    /*
     * Check if the attribute is unchanged (we can't just call bpc_attribCache_setFile(), 
     * since it updates in place, meaning we then don't have the original version).
     */
    if ( (xattr = bpc_attrib_xattrGet(file, (char*)name, strlen(name) + 1, 0)) ) {
        if ( xattr->valueLen == size && !memcmp(xattr->value, value, xattr->valueLen) ) {
            if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lsetxattr(%s, %s, %lu) -> xattr unchanged\n", path, name, size);
            return 0;
        }
    }

    /*
     * Save away the attributes in old if not recently set and not present already
     */
    if ( acOldUsed && !file->isTemp && file->inode < Stats.Inode0 && !bpc_attribCache_getFile(&acOld, (char*)path, 0, 0) ) {
        if ( bpc_attribCache_setFile(&acOld, (char*)path, file, 1) > 0 ) {
            bpc_poolRefDeltaUpdate(&DeltaOld, file->compress, &file->digest, 1);
        }
    }

    /*
     * now set the new attribute value
     */
    ret = bpc_attrib_xattrSetValue(file, (char*)name, strlen(name) + 1, (void*)value, size);
    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lsetxattr(%s, %s, %lu, %d) -> return %d\n", path, name, size, ret, file->isTemp);
    bpc_attribCache_setFile(&acNew, (char*)path, file, 0);
    return ret < 0 ? ret : 0;
}

int bpc_lremovexattr(const char *path, const char *name)
{
    bpc_attrib_file *file = bpc_attribCache_getFile(&acNew, (char*)path, 0, 0);
    bpc_attrib_xattr *xattr;
    int ret;

    if ( !am_generator && (!file || !file->isTemp) ) {
        /*
         * We don't remove xattrs for non-temp files on the receiver, since it's too hard to keep
         * attribute updates on both the generator and receiver sycnhronized.  Send the xattr
         * setting to the generator so it can do the setting.
         */
        static xbuf rename_msg = EMPTY_XBUF;
        char *bufP;
        uint32 pathLen = strlen(path) + 1, nameLen = strlen(name) + 1;

        if ( !rename_msg.size ) {
            alloc_xbuf(&rename_msg, 4096);
        }
        if ( rename_msg.size < 3 * sizeof(uint32) + pathLen + nameLen + 1024 ) {
            realloc_xbuf(&rename_msg, 3 * sizeof(uint32) + pathLen + nameLen + 1024);
        }
        bufP = rename_msg.buf;
        SIVAL(bufP, 0, pathLen);       bufP += sizeof(uint32);
        SIVAL(bufP, 0, nameLen);       bufP += sizeof(uint32);
        memcpy(bufP, path, pathLen);   bufP += pathLen;
        memcpy(bufP, name, nameLen);   bufP += nameLen;
        if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lremovexattr(%s, %s) -> sending to gen (len=%d)\n", path, name, bufP - rename_msg.buf);
        send_msg(MSG_XATTR_REMOVE, rename_msg.buf, bufP - rename_msg.buf, 0);
        return 0;
    }

    if ( !file ) {
        errno = ENOENT;
        if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lremovexattr(%s, %s) -> file not found\n", path, name);
        return -1;
    }

    /*
     * Check if the attribute is exists - if not then quietly return.
     */
    if ( !(xattr = bpc_attrib_xattrGet(file, (char*)name, strlen(name) + 1, 0)) ) {
        if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lremovexattr(%s, %s) -> xattr not found\n", path, name);
        errno = ENOATTR;
        return -1;
    }

    /*
     * Save away the attributes in old if not recently set and not present already
     */
    if ( acOldUsed && !file->isTemp && file->inode < Stats.Inode0 && !bpc_attribCache_getFile(&acOld, (char*)path, 0, 0) ) {
        if ( bpc_attribCache_setFile(&acOld, (char*)path, file, 1) > 0 ) {
            bpc_poolRefDeltaUpdate(&DeltaOld, file->compress, &file->digest, 1);
        }
    }

    /*
     * now remove the attribute
     */
    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_lremovexattr(%s, %s) -> xattr removed\n", path, name);
    ret = bpc_attrib_xattrDelete(file, (char*)name, strlen(name) + 1);
    bpc_attribCache_setFile(&acNew, (char*)path, file, 0);
    return ret;
}

ssize_t bpc_llistxattr(const char *path, char *list, size_t size)
{
    bpc_attrib_file *file = bpc_attribCache_getFile(&acNew, (char*)path, 0, 0);

    if ( !file ) {
        if ( !am_generator ) {
            if ( LogLevel >= 4 ) bpc_logMsgf("bpc_llistxattr(%s) -> no file, but return 0\n", path);
            return 0;
        }
        errno = ENOENT;
        if ( LogLevel >= 4 ) bpc_logMsgf("bpc_llistxattr(%s) -> file not found\n", path);
        return -1;
    }
    if ( LogLevel >= 4 ) bpc_logMsgf("bpc_llistxattr(%s):\n", path);
    return bpc_attrib_xattrList(file, list, size, 1);
}