File Coverage

bpc_attrib.c
Criterion Covered Total %
statement 0 662 0.0
branch 0 396 0.0
condition n/a
subroutine n/a
pod n/a
total 0 1058 0.0


line stmt bran cond sub pod time code
1             /*
2             * Routines for read/writing/managing file attributes
3             *
4             * Copyright (C) 2013 Craig Barratt.
5             *
6             * This program is free software; you can redistribute it and/or modify
7             * it under the terms of the GNU General Public License as published by
8             * the Free Software Foundation; either version 3 of the License, or
9             * (at your option) any later version.
10             *
11             * This program is distributed in the hope that it will be useful,
12             * but WITHOUT ANY WARRANTY; without even the implied warranty of
13             * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14             * GNU General Public License for more details.
15             *
16             * You should have received a copy of the GNU General Public License along
17             * with this program; if not, visit the http://fsf.org website.
18             */
19              
20             #include "backuppc.h"
21              
22             /*
23             * Type of attribute file. This is saved as a magic number at the
24             * start of the file. This type is for V3 and earlier.
25             */
26             #define BPC_ATTRIB_TYPE_UNIX (0x17555555)
27              
28             /*
29             * super set of UNIX, including extended attribs and digest, for 4.x+
30             */
31             #define BPC_ATTRIB_TYPE_XATTR (0x17565353)
32              
33             /*
34             * starting in 4.x, attrib files in the pc backup tree are
35             * just digests that give the location of the real attrib file
36             * in the pool. attrib files written in the pc backup tree
37             * start with this magic number, followed by the digest.
38             */
39             #define BPC_ATTRIB_TYPE_DIGEST (0x17585451)
40              
41             static char *FileType2Text[] = {
42             "file",
43             "hardlink",
44             "symlink",
45             "chardev",
46             "blockdev",
47             "dir",
48             "fifo",
49             "?",
50             "socket",
51             "?",
52             "deleted",
53             };
54              
55             /*
56             * Ensure by default we use old-style "attrib" files for <= 4.0.0alpha3 compatibility.
57             * Newer versions of BackupPC turn this off to use new attribute file naming.
58             */
59             static int WriteOldStyleAttribFile = 1;
60             static int KeepOldAttribFiles = 1;
61              
62 0           void bpc_attrib_backwardCompat(int writeOldStyleAttribFile, int keepOldAttribFiles)
63             {
64 0 0         if ( writeOldStyleAttribFile >= 0 ) WriteOldStyleAttribFile = writeOldStyleAttribFile;
65 0 0         if ( keepOldAttribFiles >= 0 ) KeepOldAttribFiles = keepOldAttribFiles;
66 0 0         if ( BPC_LogLevel >= 5 ) {
67 0           bpc_logMsgf("bpc_attrib_backwardCompat: WriteOldStyleAttribFile = %d, KeepOldAttribFiles = %d\n",
68             WriteOldStyleAttribFile, KeepOldAttribFiles);
69             }
70 0           }
71              
72              
73             #define CONV_BUF_TO_UINT32(buf) ((buf)[0] << 24 | (buf)[1] << 16 | (buf)[2] << 8 | (buf)[3])
74              
75             #define CONV_UINT32_TO_BUF(buf, val) { *(buf)++ = ((val) >> 24) & 0xff; \
76             *(buf)++ = ((val) >> 16) & 0xff; \
77             *(buf)++ = ((val) >> 8) & 0xff; \
78             *(buf)++ = ((val) >> 0) & 0xff; }
79              
80             /*
81             * Note on xattr keys: they are treated as opaque strings of bytes, and the convention
82             * is to include the '\0' byte termination in the keyLen (ie: it is strlen(key) + 1).
83             */
84 0           bpc_attrib_xattr *bpc_attrib_xattrGet(bpc_attrib_file *file, void *key, int keyLen, int allocate_if_missing)
85             {
86 0           return (bpc_attrib_xattr*)bpc_hashtable_find(&file->xattrHT, key, keyLen, allocate_if_missing);
87             }
88              
89 0           void bpc_attrib_xattrDestroy(bpc_attrib_xattr *xattr)
90             {
91 0 0         if ( xattr->key.key ) free(xattr->key.key);
92 0 0         if ( xattr->value ) free(xattr->value);
93 0           }
94              
95 0           int bpc_attrib_xattrDelete(bpc_attrib_file *file, void *key, int keyLen)
96             {
97 0           bpc_attrib_xattr *xattr = bpc_hashtable_find(&file->xattrHT, key, keyLen, 0);
98              
99 0 0         if ( !xattr ) return -1;
100 0           bpc_attrib_xattrDestroy(xattr);
101 0           bpc_hashtable_nodeDelete(&file->xattrHT, xattr);
102 0           return 0;
103             }
104              
105 0           static void bpc_attrib_xattrDeleteNode(bpc_attrib_xattr *xattr, bpc_attrib_file *file)
106             {
107 0           bpc_attrib_xattrDestroy(xattr);
108 0           bpc_hashtable_nodeDelete(&file->xattrHT, xattr);
109 0           }
110              
111 0           int bpc_attrib_xattrDeleteAll(bpc_attrib_file *file)
112             {
113 0           bpc_hashtable_iterate(&file->xattrHT, (void*)bpc_attrib_xattrDeleteNode, file);
114 0           return 0;
115             }
116              
117             /*
118             * returns >0 if the new value is the same as the current value.
119             * returns 0 if the new value was set correctly
120             * returns <0 on error.
121             */
122 0           int bpc_attrib_xattrSetValue(bpc_attrib_file *file, void *key, int keyLen, void *value, uint32 valueLen)
123             {
124             bpc_attrib_xattr *xattr;
125             char keyCopy[BPC_MAXPATHLEN];
126              
127 0 0         if ( ((char*)key)[keyLen - 1] != 0x0 ) {
128 0 0         if ( keyLen >= BPC_MAXPATHLEN - 8 ) {
129 0           bpc_logMsgf("bpc_attrib_xattrSetValue: BOTCH: key not 0x0 terminated; too long to repair (keyLen %u)\n", keyLen);
130 0           return -1;
131             } else {
132 0           memcpy(keyCopy, key, keyLen);
133 0           keyCopy[keyLen++] = 0x0;
134 0           key = keyCopy;
135 0           bpc_logMsgf("bpc_attrib_xattrSetValue: BOTCH: appended 0x0 to xattr name '%s' (keyLen now %u)\n", (char*)key, keyLen);
136             }
137             }
138 0           xattr = bpc_attrib_xattrGet(file, key, keyLen, 1);
139              
140 0 0         if ( !xattr->value ) {
141             /*
142             * new entry
143             */
144 0 0         if ( !(xattr->key.key = malloc(keyLen)) ) {
145 0           bpc_logErrf("bpc_attrib_xattrSetValue: can't allocate %d bytes for key\n", keyLen);
146 0           return -1;
147             }
148 0           memcpy(xattr->key.key, key, keyLen);
149 0           xattr->key.keyLen = keyLen;
150             } else {
151             /*
152             * existing entry - no need to recopy key. If value array isn't big enough then create another.
153             */
154 0 0         if ( valueLen > xattr->valueLen ) {
155 0           free(xattr->value);
156 0           xattr->value = NULL;
157 0 0         } else if ( valueLen == xattr->valueLen && !memcmp(xattr->value, value, valueLen) ) {
    0          
158             /*
159             * same value - no change
160             */
161 0           return 1;
162             }
163             }
164 0 0         if ( !xattr->value && !(xattr->value = malloc(valueLen)) ) {
    0          
165 0           bpc_logErrf("bpc_attrib_xattrSetValue: can't allocate %d bytes for value\n", valueLen);
166 0           return -1;
167             }
168 0           memcpy(xattr->value, value, valueLen);
169 0           xattr->valueLen = valueLen;
170 0           return 0;
171             }
172              
173 0           void bpc_attrib_xattrCopy(bpc_attrib_xattr *xattrSrc, bpc_attrib_file *fileDest)
174             {
175             bpc_attrib_xattr *xattr;
176 0 0         uchar *key = (uchar*)malloc(xattrSrc->key.keyLen > 0 ? xattrSrc->key.keyLen : 1);
177 0 0         uchar *value = (uchar*)malloc(xattrSrc->valueLen > 0 ? xattrSrc->valueLen : 1);
178              
179 0 0         if ( !key || !value ) {
    0          
180 0           bpc_logErrf("bpc_attrib_xattrCopy: can't allocate %d,%d bytes\n", xattrSrc->key.keyLen + 1, xattrSrc->valueLen + 1);
181 0           return;
182             }
183              
184 0           memcpy(key, xattrSrc->key.key, xattrSrc->key.keyLen);
185 0           memcpy(value, xattrSrc->value, xattrSrc->valueLen);
186              
187 0           xattr = bpc_attrib_xattrGet(fileDest, key, xattrSrc->key.keyLen, 1);
188              
189 0 0         if ( xattr->value ) {
190             /*
191             * Shouldn't be present, but if so clear it out and write the new key
192             */
193 0           bpc_attrib_xattrDestroy(xattr);
194 0           xattr->key.key = key;
195 0           xattr->key.keyLen = xattrSrc->key.keyLen;
196             }
197 0           xattr->value = value;
198 0           xattr->valueLen = xattrSrc->valueLen;
199             }
200              
201 0           int bpc_attrib_xattrCount(bpc_attrib_file *file)
202             {
203 0           return bpc_hashtable_entryCount(&file->xattrHT);
204             }
205              
206             typedef struct {
207             char *list;
208             ssize_t idx;
209             ssize_t listLen;
210             int ignoreRsyncACLs;
211             } xattrList_info;
212              
213 0           static void bpc_attrib_xattrListKey(bpc_attrib_xattr *xattr, xattrList_info *info)
214             {
215 0 0         if ( info->idx < 0 ) return;
216              
217 0 0         if ( info->ignoreRsyncACLs ) {
218             static struct {
219             char *str;
220             unsigned int len;
221             } ignoreKeys[] = {
222             { "user.rsync.%aacl", sizeof("user.rsync.%aacl"), }, /* note: sizeof() includes the \0 terminator */
223             { "user.rsync.%dacl", sizeof("user.rsync.%dacl"), },
224             };
225             uint i;
226              
227 0 0         for ( i = 0 ; i < sizeof(ignoreKeys) / sizeof(ignoreKeys[0]) ; i++ ) {
228 0 0         if ( xattr->key.keyLen == ignoreKeys[i].len
229 0 0         && !memcmp(xattr->key.key, ignoreKeys[i].str, xattr->key.keyLen) ) {
230 0           return;
231             }
232             }
233             }
234 0 0         if ( info->list ) {
235 0 0         if ( info->idx + (signed)xattr->key.keyLen > info->listLen ) {
236 0           info->idx = -1;
237 0           return;
238             }
239             /*
240             * confirm that keyLen includes the \0 terminating byte
241             */
242 0           memcpy(info->list + info->idx, xattr->key.key, xattr->key.keyLen);
243 0 0         if ( xattr->key.keyLen >= 1 && info->list[info->idx + xattr->key.keyLen - 1] != 0x0 ) {
    0          
244 0           info->list[info->idx + xattr->key.keyLen - 1] = 0x0;
245 0           bpc_logMsgf("bpc_attrib_xattrListKey: BOTCH: truncated xattr name '%s' to match keyLen %u\n", info->list + info->idx, xattr->key.keyLen);
246             }
247 0 0         if ( BPC_LogLevel >= 6 ) bpc_logMsgf("bpc_attrib_xattrListKey: adding %s\n", info->list + info->idx);
248 0           info->idx += xattr->key.keyLen;
249             } else {
250 0           info->idx += xattr->key.keyLen;
251             }
252             }
253              
254             /*
255             * Concatenate all the xattr keys, (which the caller has ensured are \0 terminated),
256             * into a single string. Return the number of bytes in the output string.
257             * Returns -1 if listLen is too short to fit all the keys.
258             * If list is NULL, instead returns the number of bytes required to store all the keys.
259             */
260 0           size_t bpc_attrib_xattrList(bpc_attrib_file *file, char *list, size_t listLen, int ignoreRsyncACLs)
261             {
262             xattrList_info info;
263              
264 0           info.list = list;
265 0           info.idx = 0;
266 0           info.listLen = listLen;
267 0           info.ignoreRsyncACLs = ignoreRsyncACLs;
268              
269 0           bpc_hashtable_iterate(&file->xattrHT, (void*)bpc_attrib_xattrListKey, &info);
270 0           return info.idx;
271             }
272              
273 0           void bpc_attrib_fileDestroy(bpc_attrib_file *file)
274             {
275 0 0         if ( file->name) free(file->name);
276 0           bpc_hashtable_iterate(&file->xattrHT, (void*)bpc_attrib_xattrDestroy, NULL);
277 0           bpc_hashtable_destroy(&file->xattrHT);
278 0           }
279              
280             /*
281             * Return the attributes for the given file.
282             * If allocate_if_missing == 0 and not present, then NULL is returned.
283             * If allocate_if_missing != 0 and not present, then an empty struct is returned with the key filled in,
284             * and file->name is NULL.
285             */
286 0           bpc_attrib_file *bpc_attrib_fileGet(bpc_attrib_dir *dir, char *fileName, int allocate_if_missing)
287             {
288 0           return bpc_hashtable_find(&dir->filesHT, (uchar*)fileName, strlen(fileName), allocate_if_missing);
289             }
290              
291             /*
292             * Initialize an empty file structure (ie: one returned by bpc_attrib_fileGet() that is empty)
293             */
294 0           void bpc_attrib_fileInit(bpc_attrib_file *file, char *fileName, int xattrNumEntries)
295             {
296 0           int fileNameLen = strlen(fileName);
297              
298 0 0         if ( file->name ) bpc_attrib_fileDestroy(file);
299 0           file->name = (char*)malloc(fileNameLen + 1);
300 0 0         if ( !file->name ) {
301 0           bpc_logErrf("bpc_attrib_fileInit: can't allocate %d bytes for file name\n", fileNameLen + 1);
302 0           return;
303             }
304 0           memcpy(file->name, fileName, fileNameLen + 1);
305 0           file->isTemp = 0;
306 0           file->key.key = file->name;
307 0           bpc_hashtable_create(&file->xattrHT, 16 + xattrNumEntries, sizeof(bpc_attrib_xattr));
308             }
309              
310             /*
311             * Copy all the attributes from fileSrc to fileDest. fileDest should already have a
312             * valid allocated fileName and allocated xattr hash. The fileDest xattr hash is
313             * emptied before the copy, meaning it is over written.
314             *
315             * If overwriteEmptyDigest == 0, an empty digest in fileSrc will not overwrite fileDest.
316             */
317 0           void bpc_attrib_fileCopyOpt(bpc_attrib_file *fileDest, bpc_attrib_file *fileSrc, int overwriteEmptyDigest)
318             {
319 0 0         if ( fileDest == fileSrc ) return;
320              
321 0           fileDest->type = fileSrc->type;
322 0           fileDest->compress = fileSrc->compress;
323 0           fileDest->mode = fileSrc->mode;
324 0           fileDest->isTemp = fileSrc->isTemp;
325 0           fileDest->uid = fileSrc->uid;
326 0           fileDest->gid = fileSrc->gid;
327 0           fileDest->nlinks = fileSrc->nlinks;
328 0           fileDest->mtime = fileSrc->mtime;
329 0           fileDest->size = fileSrc->size;
330 0           fileDest->inode = fileSrc->inode;
331 0           fileDest->backupNum = fileSrc->backupNum;
332 0 0         if ( fileSrc->digest.len > 0 || overwriteEmptyDigest ) {
    0          
333 0           fileDest->digest = fileSrc->digest;
334             }
335 0           bpc_hashtable_iterate(&fileDest->xattrHT, (void*)bpc_attrib_xattrDestroy, NULL);
336 0           bpc_hashtable_erase(&fileDest->xattrHT);
337 0           bpc_hashtable_iterate(&fileSrc->xattrHT, (void*)bpc_attrib_xattrCopy, fileDest);
338             }
339              
340             /*
341             * Copy all the attributes from fileSrc to fileDest. fileDest should already have a
342             * valid allocated fileName and allocated xattr hash. The fileDest xattr hash is
343             * emptied before the copy, meaning it is over written.
344             */
345 0           void bpc_attrib_fileCopy(bpc_attrib_file *fileDest, bpc_attrib_file *fileSrc)
346             {
347 0 0         if ( fileDest == fileSrc ) return;
348              
349 0           bpc_attrib_fileCopyOpt(fileDest, fileSrc, 1);
350             }
351              
352             /*
353             * Check if two file attribute structures are the same. Returns 0 if they are the same.
354             */
355 0           int bpc_attrib_fileCompare(bpc_attrib_file *file0, bpc_attrib_file *file1)
356             {
357 0           uint idx = 0;
358              
359 0 0         if ( file0->type != file1->type
360 0 0         || file0->compress != file1->compress
361 0 0         || file0->mode != file1->mode
362 0 0         || file0->uid != file1->uid
363 0 0         || file0->gid != file1->gid
364 0 0         || file0->nlinks != file1->nlinks
365 0 0         || file0->mtime != file1->mtime
366 0 0         || file0->size != file1->size
367 0 0         || file0->inode != file1->inode
368 0 0         || file0->digest.len != file1->digest.len
369 0 0         || memcmp(file0->digest.digest, file1->digest.digest, file0->digest.len)
370 0 0         || bpc_attrib_xattrCount(file0) != bpc_attrib_xattrCount(file1) ) {
371 0 0         if ( BPC_LogLevel >= 9 ) bpc_logMsgf("bpc_attrib_fileCompare: %s %s differ\n", file0->name, file1->name);
372 0           return 1;
373             }
374             while ( 1 ) {
375 0           bpc_attrib_xattr *xattr0 = bpc_hashtable_nextEntry(&file0->xattrHT, &idx), *xattr1;
376 0 0         if ( !xattr0 ) return 0;
377 0 0         if ( !(xattr1 = bpc_attrib_xattrGet(file1, xattr0->key.key, xattr0->key.keyLen, 0)) ) return 1;
378 0 0         if ( xattr0->valueLen != xattr1->valueLen || memcmp(xattr0->value, xattr1->value, xattr0->valueLen) ) return 1;
    0          
379 0           }
380             }
381              
382 0           void bpc_attrib_fileDeleteName(bpc_attrib_dir *dir, char *fileName)
383             {
384 0           bpc_attrib_file *file = bpc_hashtable_find(&dir->filesHT, (uchar*)fileName, strlen(fileName), 0);
385              
386 0 0         if ( !file ) return;
387 0           bpc_attrib_fileDestroy(file);
388 0           bpc_hashtable_nodeDelete(&dir->filesHT, file);
389             }
390              
391 0           int bpc_attrib_fileIterate(bpc_attrib_dir *dir, bpc_attrib_file **file, uint *idx)
392             {
393 0           *file = bpc_hashtable_nextEntry(&dir->filesHT, idx);
394 0 0         if ( !*file ) return -1;
395 0           return 0;
396             }
397              
398 0           int bpc_attrib_fileCount(bpc_attrib_dir *dir)
399             {
400 0           return bpc_hashtable_entryCount(&dir->filesHT);
401             }
402              
403 0           char *bpc_attrib_fileType2Text(int type)
404             {
405 0 0         if ( type < 0 || type >= (int)(sizeof(FileType2Text) / sizeof(FileType2Text[0])) ) return "?";
    0          
406 0           return FileType2Text[type];
407             }
408              
409 0           void bpc_attrib_dirInit(bpc_attrib_dir *dir, int compressLevel)
410             {
411 0           dir->digest.len = 0;
412 0           dir->compress = compressLevel;
413 0           bpc_hashtable_create(&dir->filesHT, 512, sizeof(bpc_attrib_file));
414 0           }
415              
416 0           void bpc_attrib_dirDestroy(bpc_attrib_dir *dir)
417             {
418 0           bpc_hashtable_iterate(&dir->filesHT, (void*)bpc_attrib_fileDestroy, NULL);
419 0           bpc_hashtable_destroy(&dir->filesHT);
420 0           }
421              
422             typedef struct {
423             bpc_deltaCount_info *deltaInfo;
424             int incr;
425             unsigned int *inodeMax;
426             } fileRefCnt_info;
427              
428 0           static void bpc_attrib_fileRefCount(bpc_attrib_file *file, fileRefCnt_info *info)
429             {
430 0 0         if ( file->digest.len > 0 ) {
431             char hexStr[BPC_DIGEST_LEN_MAX * 2 + 1];
432 0           bpc_digest_digest2str(&file->digest, hexStr);
433 0 0         if ( BPC_LogLevel >= 7 ) bpc_logMsgf("bpc_attrib_fileRefCount: file %s digest %s delta %d\n", file->name, hexStr, info->incr);
434 0           bpc_poolRefDeltaUpdate(info->deltaInfo, file->compress, &file->digest, info->incr);
435             }
436 0 0         if ( info->inodeMax && file->inode > *info->inodeMax ) {
    0          
437 0           *info->inodeMax = file->inode;
438             }
439 0           }
440              
441             /*
442             * call refDeltaUpdate with incr (typically +/-1) for every entry in the directory,
443             * as well as the dir itself.
444             */
445 0           void bpc_attrib_dirRefCountInodeMax(bpc_deltaCount_info *deltaInfo, bpc_attrib_dir *dir, int incr, unsigned int *inodeMax)
446             {
447             fileRefCnt_info info;
448              
449 0           info.deltaInfo = deltaInfo;
450 0           info.incr = incr;
451 0           info.inodeMax = inodeMax;
452 0           bpc_hashtable_iterate(&dir->filesHT, (void*)bpc_attrib_fileRefCount, &info);
453 0 0         if ( dir->digest.len > 0 ) {
454             char hexStr[BPC_DIGEST_LEN_MAX * 2 + 1];
455 0           bpc_digest_digest2str(&dir->digest, hexStr);
456 0 0         if ( BPC_LogLevel >= 7 ) bpc_logMsgf("bpc_attrib_dirRefCount: attrib digest %s delta = %d\n", hexStr, incr);
457 0           bpc_poolRefDeltaUpdate(deltaInfo, dir->compress, &dir->digest, incr);
458             } else {
459 0 0         if ( BPC_LogLevel >= 7 ) bpc_logMsgf("bpc_attrib_dirRefCount: no attrib digest -> no delta\n");
460             }
461 0           }
462              
463             /*
464             * call refDeltaUpdate with incr (typically +/-1) for every entry in the directory,
465             * as well as the dir itself.
466             */
467 0           void bpc_attrib_dirRefCount(bpc_deltaCount_info *deltaInfo, bpc_attrib_dir *dir, int incr)
468             {
469 0           bpc_attrib_dirRefCountInodeMax(deltaInfo, dir, incr, NULL);
470 0           }
471              
472             typedef struct {
473             char *entries;
474             ssize_t entryIdx;
475             ssize_t entrySize;
476             } dirEntry_info;
477              
478 0           static void bpc_attrib_getDirEntry(bpc_attrib_file *file, dirEntry_info *info)
479             {
480 0           ssize_t len = strlen(file->name) + 1;
481              
482 0 0         if ( info->entryIdx < 0 ) return;
483 0 0         if ( info->entries ) {
484 0 0         if ( info->entryIdx + len > info->entrySize ) {
485 0           info->entryIdx = -1;
486 0           return;
487             }
488 0           memcpy(info->entries + info->entryIdx, file->name, len);
489             }
490 0           info->entryIdx += len;
491             }
492              
493 0           ssize_t bpc_attrib_getEntries(bpc_attrib_dir *dir, char *entries, ssize_t entrySize)
494             {
495             dirEntry_info info;
496              
497 0           info.entries = entries;
498 0           info.entryIdx = 0;
499 0           info.entrySize = entrySize;
500              
501 0           bpc_hashtable_iterate(&dir->filesHT, (void*)bpc_attrib_getDirEntry, &info);
502 0           return info.entryIdx;
503             }
504              
505 0           void bpc_attrib_attribFilePath(char *path, char *dir, char *attribFileName)
506             {
507 0 0         if ( !dir ) {
508 0           snprintf(path, BPC_MAXPATHLEN, "%s", attribFileName);
509             } else {
510 0 0         snprintf(path, BPC_MAXPATHLEN, "%s/%s", dir, attribFileName ? attribFileName : "attrib");
511             }
512 0           }
513              
514 0           bpc_digest *bpc_attrib_dirDigestGet(bpc_attrib_dir *dir)
515             {
516 0           return &dir->digest;
517             }
518              
519 0           static int read_more_data(bpc_fileZIO_fd *fd, uchar *buf, size_t bufSize, size_t *nRead, uchar **bufPP, char *attribPath)
520             {
521             int thisRead;
522             /*
523             * move the remaining part of the buffer down, and read more data
524             */
525 0           *nRead = (buf + *nRead) - *bufPP;
526 0           memmove(buf, *bufPP, *nRead);
527 0           *bufPP = buf;
528 0           thisRead = bpc_fileZIO_read(fd, buf + *nRead, bufSize - *nRead);
529 0 0         if ( thisRead < 0 ) {
530 0           bpc_logErrf("bpc_attrib_dirRead: can't read more bytes from %s\n", attribPath);
531 0           return -1;
532             }
533 0           *nRead += thisRead;
534 0           return 0;
535             }
536              
537             /*
538             * Read variable-length unsigned integer in 7 bit chunks, LSB first.
539             */
540 0           static int64 getVarInt(uchar **bufPP, uchar *bufEnd)
541             {
542 0           int64 result = 0;
543 0           uchar *bufP = *bufPP;
544 0           int i = 0;
545              
546 0 0         while ( bufP < bufEnd ) {
547 0           uchar c = *bufP++;
548 0           result |= ((int64)(c & 0x7f)) << i;
549 0 0         if ( !(c & 0x80) ) {
550 0           *bufPP = bufP;
551 0           return result;
552             }
553 0           i += 7;
554             }
555             /*
556             * we ran out of data... make sure bufP is greater than bufEnd, since
557             * returning it to be equal (ie: bufP) will be incorrectly interpreted as
558             * meaning the integer correctly ended right at the end of the buffer.
559             */
560 0           *bufPP = bufEnd + 1;
561 0           return result;
562             }
563              
564             /*
565             * V3 variable length integer read, MSB first, which is compatible with perl pack("w")
566             */
567 0           static int64 getVarInt_v3(uchar **bufPP, uchar *bufEnd)
568             {
569 0           int64 result = 0;
570 0           uchar *bufP = *bufPP;
571              
572 0 0         while ( bufP < bufEnd ) {
573 0           uchar c = *bufP++;
574 0           result = (result << 7) | (c & 0x7f);
575 0 0         if ( !(c & 0x80) ) {
576 0           *bufPP = bufP;
577 0           return result;
578             }
579             }
580             /*
581             * we ran out of data... make sure bufP is greater than bufEnd, since
582             * returning it to be equal (ie: bufP) will be incorrectly interpreted as
583             * meaning the integer correctly ended right at the end of the buffer.
584             */
585 0           *bufPP = bufEnd + 1;
586 0           return result;
587             }
588              
589             /*
590             * Write variable-length unsigned integer in 7 bit chunks, LSB first
591             */
592 0           static void setVarInt(uchar **bufPP, uchar *bufEnd, int64 value)
593             {
594 0           uchar *bufP = *bufPP;
595 0           int maxBytes = (sizeof(value) * 8 + 6) / 7;
596              
597             do {
598 0           uchar c = value & 0x7f;
599 0           value >>= 7;
600 0           maxBytes--;
601 0 0         if ( value && maxBytes > 0 ) c |= 0x80;
    0          
602 0 0         if ( bufP < bufEnd ) {
603 0           *bufP++ = c;
604             } else {
605 0           bufP++;
606             }
607 0 0         } while ( value && maxBytes > 0 );
    0          
608 0           *bufPP = bufP;
609 0           }
610              
611             /*
612             * Unpack the data in buf[] into the file structure, after the file name and xattr entry
613             * count have been extracted. Returns next unused buffer location.
614             *
615             * If there isn't enough data to extract a complete file structure, the return value
616             * will be greater than bufEnd. You should gather more data and re-call the function.
617             */
618 0           uchar *bpc_attrib_buf2file(bpc_attrib_file *file, uchar *buf, uchar *bufEnd, int xattrNumEntries)
619             {
620 0           uchar *bufP = buf;
621             int i;
622              
623 0           file->type = getVarInt(&bufP, bufEnd);
624 0           file->mtime = getVarInt(&bufP, bufEnd);
625 0           file->mode = getVarInt(&bufP, bufEnd);
626 0           file->uid = getVarInt(&bufP, bufEnd);
627 0           file->gid = getVarInt(&bufP, bufEnd);
628 0           file->size = getVarInt(&bufP, bufEnd);
629 0           file->inode = getVarInt(&bufP, bufEnd);
630 0           file->compress = getVarInt(&bufP, bufEnd);
631 0           file->nlinks = getVarInt(&bufP, bufEnd);
632 0           file->digest.len = getVarInt(&bufP, bufEnd);
633 0           file->isTemp = 0;
634              
635 0 0         if ( file->digest.len > 0 && bufP + file->digest.len <= bufEnd ) {
    0          
636 0           memcpy(file->digest.digest, bufP, file->digest.len);
637             }
638 0           bufP += file->digest.len;
639              
640 0 0         for ( i = 0 ; i < xattrNumEntries ; i++ ) {
641 0           uint keyLen = getVarInt(&bufP, bufEnd);
642 0           uint valueLen = getVarInt(&bufP, bufEnd);
643              
644 0 0         if ( bufP + keyLen + valueLen <= bufEnd ) {
645 0           bpc_attrib_xattrSetValue(file, bufP, keyLen, bufP + keyLen, valueLen);
646             }
647 0           bufP += keyLen + valueLen;
648             }
649 0           return bufP;
650             }
651              
652             /*
653             * Extract an entire packed file structure, starting with the fileName length varint.
654             * Returns next unused buffer location. It is assumed the file structure is already
655             * initialized and has a valid fileName allocated, so we don't allocate it here.
656             *
657             * If there isn't enough data to extract a complete file structure, the return value
658             * will be greater than bufEnd. You should gather more data and re-call the function.
659             * On certain errors, returns NULL;
660             */
661 0           uchar *bpc_attrib_buf2fileFull(bpc_attrib_file *file, uchar *bufP, uchar *bufEnd)
662             {
663             uint fileNameLen, xattrNumEntries;
664              
665 0           fileNameLen = getVarInt(&bufP, bufEnd);
666 0 0         if ( fileNameLen > BPC_MAXPATHLEN - 1 ) {
667 0           bpc_logErrf("bpc_attrib_buf2fileFull: got unreasonable file name length %d\n", fileNameLen);
668 0           return NULL;
669             }
670 0           bufP += fileNameLen;
671 0           bpc_attrib_xattrDeleteAll(file);
672 0           xattrNumEntries = getVarInt(&bufP, bufEnd);
673 0 0         if ( BPC_LogLevel >= 6 ) bpc_logMsgf("bpc_attrib_buf2fileFull: xattrNumEntries = %d\n", xattrNumEntries);
674 0           bufP = bpc_attrib_buf2file(file, bufP, bufEnd, xattrNumEntries);
675 0           return bufP;
676             }
677              
678             /*
679             * Read the attribute file at dirPath/attribFilePath and populate dir
680             */
681 0           int bpc_attrib_dirRead(bpc_attrib_dir *dir, char *dirPath, char *attribFilePath, int backupNum)
682             {
683             char attribPath[BPC_MAXPATHLEN];
684             bpc_fileZIO_fd fd;
685             size_t nRead;
686 0           uint32 magic = 0;
687             uchar buf[8 * 65536], *bufP;
688             STRUCT_STAT st;
689             char *p, *attribFileName;
690              
691 0           bpc_attrib_attribFilePath(attribPath, dirPath, attribFilePath);
692 0           dir->digest.len = 0;
693              
694             /*
695             * attribFileName points to the last portion of attribFilePath, or the whole
696             * string if it doesn't contain '/'
697             */
698 0 0         if ( (attribFileName = strrchr(attribFilePath, '/')) ) {
699 0           attribFileName++;
700             } else {
701 0           attribFileName = attribFilePath;
702             }
703              
704 0 0         if ( BPC_LogLevel >= 6 ) bpc_logMsgf("bpc_attrib_dirRead(%s); dirPath = %s, attribFilePath = %s, attribFileName = %s\n",
705             attribPath, dirPath, attribFilePath, attribFileName);
706              
707 0 0         if ( (p = strchr(attribFileName, '_')) && !stat(attribPath, &st) && S_ISREG(st.st_mode) ) {
    0          
    0          
708             /*
709             * Explicit path name to new-style attrib file, and it exists; extract digest
710             */
711 0 0         if ( !strcmp(p + 1, "0") ) return 0;
712 0           bpc_digest_str2digest(&dir->digest, p + 1);
713 0 0         if ( BPC_LogLevel >= 6 ) {
714             char str[256];
715 0           bpc_digest_digest2str(&dir->digest, str);
716 0           bpc_logMsgf("bpc_attrib_dirRead: called with attrib file %s: digest = %s, len = %d\n",
717             attribPath, str, dir->digest.len);
718             }
719             /*
720             * Write new type attrib files (since we found a new-style one)
721             */
722 0           WriteOldStyleAttribFile = 0;
723 0           magic = BPC_ATTRIB_TYPE_XATTR;
724 0 0         } else if ( stat(attribPath, &st) || !S_ISREG(st.st_mode) || strchr(attribFileName, '_') ) {
    0          
    0          
725             DIR *dirOs;
726             struct dirent *dp;
727 0           int attribFileNameLen = strlen(attribFileName);
728             char attribDirPath[BPC_MAXPATHLEN];
729             /*
730             * Starting in 0.50, the attrib files are zero length with the digest encoded in
731             * the file name, so there is no file just called "attrib". Look in the directory
732             * to find it.
733             */
734 0           strcpy(attribDirPath, attribPath);
735 0 0         if ( (p = strrchr(attribDirPath, '/')) ) {
736 0           *p = '\0';
737             } else {
738 0           strcpy(attribDirPath, ".");
739             }
740 0 0         if ( !(dirOs = opendir(attribDirPath)) ) {
741             /*
742             * This is a benign error - just return as though there is an empty attrib file
743             */
744 0 0         if ( BPC_LogLevel >= 8 ) bpc_logMsgf("bpc_attrib_dirRead: can't opendir %s (note: this is ok)\n", attribDirPath);
745 0           return 0;
746             }
747 0 0         while ( (dp = readdir(dirOs)) ) {
748 0 0         if ( strncmp(dp->d_name, attribFileName, attribFileNameLen) ) continue;
749 0           p = dp->d_name + attribFileNameLen;
750 0 0         if ( p[0] != '_' ) continue;
751 0           p++;
752 0 0         if ( !strcmp(p, "0") ) {
753             /*
754             * An empty attrib file is legit; just return with no entries
755             */
756 0 0         if ( BPC_LogLevel >= 6 ) {
757 0           bpc_logMsgf("bpc_attrib_dirRead: Got empty attrib file %s\n", dp->d_name);
758             }
759 0           closedir(dirOs);
760 0           return 0;
761             }
762 0           bpc_digest_str2digest(&dir->digest, p);
763 0 0         if ( BPC_LogLevel >= 6 ) {
764             char str[256];
765 0           bpc_digest_digest2str(&dir->digest, str);
766 0           bpc_logMsgf("bpc_attrib_dirRead: Got attrib file %s: digest = %s, len = %d\n",
767 0           dp->d_name, str, dir->digest.len);
768             }
769             /*
770             * Write new type attrib files (since we found a new-style one)
771             */
772 0           WriteOldStyleAttribFile = 0;
773 0           break;
774             }
775 0           closedir(dirOs);
776 0 0         if ( dir->digest.len == 0 ) return 0;
777 0           magic = BPC_ATTRIB_TYPE_XATTR;
778             } else {
779 0 0         if ( bpc_fileZIO_open(&fd, attribPath, 0, dir->compress) ) {
780 0           bpc_logErrf("bpc_attrib_dirRead: can't open %s\n", attribPath);
781 0           return -1;
782             }
783 0           nRead = bpc_fileZIO_read(&fd, buf, sizeof(buf));
784 0 0         if ( nRead == 0 ) {
785             /*
786             * an empty file is legit - this means an empty directory (ie: zero attrib entries).
787             * indicate this with an empty digest and empty hash of entries.
788             */
789 0           bpc_fileZIO_close(&fd);
790 0 0         if ( !strcmp(attribFileName, "attrib") ) {
791             char attribPathTemp[BPC_MAXPATHLEN + 16];
792            
793 0           strcpy(attribPathTemp, attribPath);
794 0           strcat(attribPathTemp, "_0");
795 0 0         if ( rename(attribPath, attribPathTemp) ) {
796 0           bpc_logErrf("bpc_attrib_dirRead: rename of empty attrib file from %s to %s failed\n", attribPath, attribPathTemp);
797 0           return -1;
798 0 0         } else if ( BPC_LogLevel >= 6 ) {
799 0           bpc_logMsgf("bpc_attrib_dirRead: renamed empty attrib file %s -> %s\n", attribPath, attribPathTemp);
800             }
801             }
802 0           return 0;
803             }
804 0 0         if ( nRead < 4 ) {
805 0           bpc_logErrf("bpc_attrib_dirRead: can't read at least 4 bytes from %s\n", attribPath);
806 0           bpc_fileZIO_close(&fd);
807 0           return -1;
808             }
809 0           magic = CONV_BUF_TO_UINT32(buf);
810             }
811              
812 0 0         if ( magic == BPC_ATTRIB_TYPE_DIGEST ) {
813             char attribPathNew[BPC_MAXPATHLEN];
814             int fdNum;
815 0           size_t digestLen = nRead - 4, attribPathLen = strlen(attribPath);
816              
817 0 0         if ( nRead < 20 ) {
818 0           bpc_logErrf("bpc_attrib_dirRead: can't read at least 20 bytes from %s\n", attribPath);
819 0           return -1;
820             }
821 0           bpc_fileZIO_close(&fd);
822 0 0         if ( digestLen > sizeof(dir->digest.digest) ) digestLen = sizeof(dir->digest.digest);
823 0           memcpy(dir->digest.digest, buf + 4, digestLen);
824 0           dir->digest.len = digestLen;
825              
826 0 0         if ( !KeepOldAttribFiles ) {
827             /*
828             * replace the attrib file with a new-style attrib file where the digest is encoded
829             * in a zero length file name
830             */
831 0 0         if ( attribPathLen + dir->digest.len * 2 + 2 >= sizeof(attribPathNew) ) {
832 0           bpc_logErrf("bpc_attrib_dirRead: new digest path too long (%d, %d, %d)\n",
833             attribPathLen, dir->digest.len, sizeof(attribPathNew));
834 0           return -1;
835             }
836 0           strcpy(attribPathNew, attribPath);
837 0           attribPathNew[attribPathLen++] = '_';
838 0           bpc_digest_digest2str(&dir->digest, attribPathNew + attribPathLen);
839 0 0         if ( (fdNum = open(attribPathNew, O_WRONLY | O_CREAT | O_TRUNC, 0660)) < 0 ) {
840 0           bpc_logErrf("bpc_attrib_dirRead: can't open/create empty attrib file %s\n", attribPathNew);
841 0           return -1;
842             }
843 0           close(fdNum);
844 0           unlink(attribPath);
845 0 0         if ( BPC_LogLevel >= 4 ) bpc_logMsgf("bpc_attrib_dirRead: replaced %s with %s\n",
846             attribPath, attribPathNew);
847             }
848             }
849              
850 0 0         if ( dir->digest.len > 0 ) {
851             /*
852             * Handle V4+ case - open the pool file directly
853             * For V3, digest.len == 0 since we opened the attrib file above (it is stored hardlinked in the backup
854             * directory; there is no digest)
855             */
856 0           bpc_digest_md52path(attribPath, dir->compress, &dir->digest);
857 0 0         if ( bpc_fileZIO_open(&fd, attribPath, 0, dir->compress) ) {
858 0           bpc_logErrf("bpc_attrib_dirRead: can't open %s\n", attribPath);
859 0           return -1;
860             }
861 0           nRead = bpc_fileZIO_read(&fd, buf, sizeof(buf));
862 0 0         if ( nRead < 4 ) {
863 0           bpc_logErrf("bpc_attrib_dirRead: can't read at least 4 bytes from %s\n", attribPath);
864 0           bpc_fileZIO_close(&fd);
865 0           return -1;
866             }
867 0           magic = CONV_BUF_TO_UINT32(buf);
868             }
869 0           bufP = buf + 4;
870              
871 0 0         if ( magic == BPC_ATTRIB_TYPE_XATTR ) {
872 0           int retry = 0;
873 0 0         while ( bufP < buf + nRead ) {
874             uint fileNameLen, xattrNumEntries;
875             char *fileName;
876             bpc_attrib_file *file;
877 0           uchar *bufPsave = bufP;
878              
879 0 0         if ( nRead == sizeof(buf) && bufP > buf + nRead - 2 * BPC_MAXPATHLEN
    0          
880 0 0         && read_more_data(&fd, buf, sizeof(buf), &nRead, &bufP, attribPath) ) {
881 0           bpc_fileZIO_close(&fd);
882 0           return -1;
883             }
884              
885 0           fileNameLen = getVarInt(&bufP, buf + nRead);
886 0 0         if ( fileNameLen > BPC_MAXPATHLEN - 1 ) {
887 0           bpc_logErrf("bpc_attrib_dirRead: got unreasonable file name length %d\n", fileNameLen);
888 0           bpc_fileZIO_close(&fd);
889 0           return -1;
890             }
891              
892             /*
893             * Save the fileName, but it's not NULL terminated yet.
894             * After we consume the next varint, we can safely NULL-terminate
895             * the fileName, which allows us to look up or create the file entry.
896             */
897 0           fileName = (char*)bufP;
898 0           bufP += fileNameLen;
899 0           xattrNumEntries = getVarInt(&bufP, buf + nRead);
900 0           fileName[fileNameLen] = '\0';
901              
902 0           file = bpc_attrib_fileGet(dir, fileName, 1);
903 0           bpc_attrib_fileInit(file, fileName, xattrNumEntries);
904 0           file->backupNum = backupNum;
905              
906 0           bufP = bpc_attrib_buf2file(file, bufP, buf + nRead, xattrNumEntries);
907 0 0         if ( bufP > buf + nRead ) {
908             /*
909             * Need to get more data and try again. We have allocated file->name,
910             * and perhaps partially filled the xattr structure, which will be ok
911             * on the retry since the same structure will be used.
912             */
913 0 0         if ( retry ) {
914 0           bpc_logErrf("bpc_attrib_dirRead: BOTCH: couldn't complete file conversion on retry (%ld,%ld,%ld)\n",
915             bufP - buf, bufPsave - buf, nRead);
916 0           bpc_fileZIO_close(&fd);
917 0           return -1;
918             }
919 0 0         if ( BPC_LogLevel >= 7 ) bpc_logMsgf("bpc_attrib_dirRead: retrying file conversion\n");
920 0           bufP = bufPsave;
921 0 0         if ( read_more_data(&fd, buf, sizeof(buf), &nRead, &bufP, attribPath) ) {
922 0           bpc_fileZIO_close(&fd);
923 0           return -1;
924             }
925 0           retry = 1;
926             } else {
927 0           retry = 0;
928             }
929 0 0         if ( !retry && BPC_LogLevel >= 8 ) bpc_logMsgf("bpc_attrib_dirRead(%s): Got file %s: type = %d, mode = 0%o, uid/gid = %d/%d, size = %d\n",
    0          
930 0           attribPath, file->name, file->type, file->mode, file->uid, file->gid, file->size);
931             }
932 0 0         } else if ( magic == BPC_ATTRIB_TYPE_UNIX ) {
933 0 0         while ( bufP < buf + nRead ) {
934             uint fileNameLen;
935             char *fileName;
936             bpc_attrib_file *file;
937             int64 sizeDiv4GB;
938             uint type;
939              
940 0 0         if ( nRead == sizeof(buf) && bufP > buf + nRead - 2 * BPC_MAXPATHLEN
    0          
941 0 0         && read_more_data(&fd, buf, sizeof(buf), &nRead, &bufP, attribPath) ) {
942 0           bpc_fileZIO_close(&fd);
943 0           return -1;
944             }
945              
946 0           fileNameLen = getVarInt_v3(&bufP, buf + nRead);
947 0 0         if ( fileNameLen > 2 * BPC_MAXPATHLEN - 16 ) {
948 0           bpc_logErrf("bpc_attrib_dirRead: got unreasonable file name length %d\n", fileNameLen);
949 0           bpc_fileZIO_close(&fd);
950 0           return -1;
951             }
952              
953             /*
954             * Save the fileName, but it's not NULL terminated yet.
955             * After we get the next data, we can safely NULL-terminate the fileName.
956             */
957 0           fileName = (char*)bufP;
958 0           bufP += fileNameLen;
959 0           type = getVarInt_v3(&bufP, buf + nRead);
960 0           fileName[fileNameLen] = '\0';
961              
962 0           file = bpc_attrib_fileGet(dir, fileName, 1);
963 0           bpc_attrib_fileInit(file, fileName, 0);
964              
965 0           file->type = type;
966 0           file->mode = getVarInt_v3(&bufP, buf + nRead);
967 0           file->uid = getVarInt_v3(&bufP, buf + nRead);
968 0           file->gid = getVarInt_v3(&bufP, buf + nRead);
969 0           sizeDiv4GB = getVarInt_v3(&bufP, buf + nRead);
970 0           file->size = (sizeDiv4GB << 32) + getVarInt_v3(&bufP, buf + nRead);
971 0           file->mtime = CONV_BUF_TO_UINT32(bufP); bufP += 4;
972 0           file->compress = dir->compress;
973 0           file->backupNum = backupNum;
974              
975 0 0         if ( BPC_LogLevel >= 8 ) bpc_logMsgf("bpc_attrib_dirRead(%s): Got v3 file %s: type = %d, mode = 0%o, uid/gid = %d/%d, size = %d\n",
976 0           attribPath, file->name, file->type, file->mode, file->uid, file->gid, file->size);
977             }
978             } else {
979 0           bpc_logErrf("Unexpected magic number 0x%x read from %s\n", magic, attribPath);
980 0           return -1;
981             }
982             /* TODO: make sure we are at EOF? */
983 0           bpc_fileZIO_close(&fd);
984 0           return 0;
985             }
986              
987             typedef struct {
988             uchar *bufP;
989             uchar *bufEnd;
990             uint numEntries;
991             } buf_info;
992              
993             typedef struct {
994             bpc_poolWrite_info fd;
995             uchar buf[4 * 65536];
996             uchar *bufP;
997             } write_info;
998              
999 0           static void write_file_flush(write_info *info)
1000             {
1001 0 0         if ( info->bufP > info->buf ) {
1002 0 0         if ( BPC_LogLevel >= 7 ) bpc_logMsgf("write_file_flush: writing %lu bytes to pool\n", (unsigned long)(info->bufP - info->buf));
1003 0           bpc_poolWrite_write(&info->fd, info->buf, info->bufP - info->buf);
1004             }
1005 0           info->bufP = info->buf;
1006 0           }
1007              
1008 0           static void bpc_attrib_xattrWrite(bpc_attrib_xattr *xattr, buf_info *info)
1009             {
1010 0           setVarInt(&info->bufP, info->bufEnd, xattr->key.keyLen);
1011 0           setVarInt(&info->bufP, info->bufEnd, xattr->valueLen);
1012              
1013 0 0         if ( xattr->key.keyLen >= 1 && info->bufP + xattr->key.keyLen <= info->bufEnd ) {
    0          
1014 0           memcpy(info->bufP, xattr->key.key, xattr->key.keyLen);
1015 0 0         if ( info->bufP[xattr->key.keyLen - 1] != 0x0 ) {
1016 0           info->bufP[xattr->key.keyLen - 1] = 0x0;
1017 0           bpc_logMsgf("bpc_attrib_xattrWrite: BOTCH: truncated xattr name '%s' to match keyLen %u\n", info->bufP, xattr->key.keyLen);
1018             }
1019             }
1020 0           info->bufP += xattr->key.keyLen;
1021              
1022 0 0         if ( info->bufP + xattr->valueLen <= info->bufEnd ) {
1023 0           memcpy(info->bufP, xattr->value, xattr->valueLen);
1024             }
1025 0           info->bufP += xattr->valueLen;
1026 0           info->numEntries++;
1027 0           }
1028              
1029             /*
1030             * Write a file structure to the memory buffer. Returns the next unused buffer
1031             * pointer. If the buffer is exhausted, no data is written past the buffer end,
1032             * Therefore, if the return value is greater than bufEnd, then the conversion
1033             * failed to fit. The routine can be called again with at least (bufP - buf)
1034             * bytes allocated.
1035             */
1036 0           uchar *bpc_attrib_file2buf(bpc_attrib_file *file, uchar *buf, uchar *bufEnd)
1037             {
1038 0           uchar *bufP = buf;
1039 0           size_t fileNameLen = strlen(file->name);
1040 0           uint xattrEntryCnt = bpc_hashtable_entryCount(&file->xattrHT);
1041             buf_info info;
1042              
1043 0           setVarInt(&bufP, bufEnd, fileNameLen);
1044 0 0         if ( bufP + fileNameLen < bufEnd ) {
1045 0           memcpy(bufP, file->name, fileNameLen);
1046             }
1047 0           bufP += fileNameLen;
1048              
1049 0           setVarInt(&bufP, bufEnd, xattrEntryCnt);
1050 0           setVarInt(&bufP, bufEnd, file->type);
1051 0           setVarInt(&bufP, bufEnd, file->mtime);
1052 0           setVarInt(&bufP, bufEnd, file->mode);
1053 0           setVarInt(&bufP, bufEnd, file->uid);
1054 0           setVarInt(&bufP, bufEnd, file->gid);
1055 0           setVarInt(&bufP, bufEnd, file->size);
1056 0           setVarInt(&bufP, bufEnd, file->inode);
1057 0           setVarInt(&bufP, bufEnd, file->compress);
1058 0           setVarInt(&bufP, bufEnd, file->nlinks);
1059 0           setVarInt(&bufP, bufEnd, file->digest.len);
1060              
1061 0 0         if ( bufP + file->digest.len <= bufEnd ) {
1062 0           memcpy(bufP, file->digest.digest, file->digest.len);
1063             }
1064 0           bufP += file->digest.len;
1065              
1066 0           info.bufEnd = bufEnd;
1067 0           info.bufP = bufP;
1068 0           info.numEntries = 0;
1069 0           bpc_hashtable_iterate(&file->xattrHT, (void*)bpc_attrib_xattrWrite, &info);
1070 0 0         if ( info.numEntries != xattrEntryCnt ) {
1071 0           bpc_logErrf("bpc_attrib_file2buf: BOTCH: wrote %u xattr entries vs %u; attrib file corrupted\n", info.numEntries, xattrEntryCnt);
1072             }
1073 0           return info.bufP;
1074             }
1075              
1076 0           static void bpc_attrib_fileWrite(bpc_attrib_file *file, write_info *info)
1077             {
1078             uchar *bufP;
1079              
1080 0 0         if ( file->isTemp ) {
1081 0 0         if ( BPC_LogLevel >= 6 ) bpc_logMsgf("Skipping temp file %s: type = %d, mode = 0%o, uid/gid = %lu/%lu, size = %lu, inode = %lu, nlinks = %d, digest = 0x%02x%02x%02x..., bufUsed = %lu\n",
1082 0           file->name, file->type, file->mode,
1083 0           (unsigned long)file->uid, (unsigned long)file->gid,
1084 0           (unsigned long)file->size, (unsigned long)file->inode, file->nlinks,
1085 0           file->digest.digest[0], file->digest.digest[1], file->digest.digest[2],
1086 0           (unsigned long)(info->bufP - info->buf));
1087 0           return;
1088             }
1089 0           bufP = bpc_attrib_file2buf(file, info->bufP, info->buf + sizeof(info->buf));
1090 0 0         if ( BPC_LogLevel >= 6 ) bpc_logMsgf("Wrote file %s: type = %d, mode = 0%o, uid/gid = %lu/%lu, size = %lu, inode = %lu, nlinks = %d, digest = 0x%02x%02x%02x..., bufUsed = %lu\n",
1091 0           file->name, file->type, file->mode,
1092 0           (unsigned long)file->uid, (unsigned long)file->gid,
1093 0           (unsigned long)file->size, (unsigned long)file->inode, file->nlinks,
1094 0           file->digest.digest[0], file->digest.digest[1], file->digest.digest[2],
1095 0           (unsigned long)(info->bufP - info->buf));
1096              
1097 0 0         if ( bufP <= info->buf + sizeof(info->buf) ) {
1098             /*
1099             * it fit into the buffer
1100             */
1101 0           info->bufP = bufP;
1102 0           return;
1103             }
1104             /*
1105             * we overflowed the buffer - flush and try again
1106             */
1107 0           write_file_flush(info);
1108 0           bufP = bpc_attrib_file2buf(file, info->bufP, info->buf + sizeof(info->buf));
1109 0 0         if ( bufP <= info->buf + sizeof(info->buf) ) {
1110 0           info->bufP = bufP;
1111 0           return;
1112             }
1113 0           bpc_logErrf("bpc_attrib_fileWrite: BOTCH: can't fit file into buffer (%ld, %ld)\n", bufP - info->buf, sizeof(info->buf));
1114             }
1115              
1116             /*
1117             * Pre 0.50 attribute writing. Writes a small file that contains the file hash of the attrib file
1118             */
1119 0           static int bpc_attrib_dirWriteOld(bpc_deltaCount_info *deltaInfo, bpc_attrib_dir *dir,
1120             char *dirPath, char *attribFileName, bpc_digest *oldDigest)
1121             {
1122             char attribPath[BPC_MAXPATHLEN], attribPathTemp[BPC_MAXPATHLEN];
1123             bpc_fileZIO_fd fd;
1124             bpc_digest digest;
1125             int status;
1126             OFF_T poolFileSize;
1127             int errorCnt;
1128             static write_info info;
1129             char *p;
1130              
1131 0           bpc_attrib_attribFilePath(attribPath, dirPath, attribFileName);
1132 0 0         if ( BPC_LogLevel >= 6 ) bpc_logMsgf("bpc_attrib_dirWriteOld(%s)\n", attribPath);
1133 0           snprintf(attribPathTemp, BPC_MAXPATHLEN, "%s.%d", attribPath, getpid());
1134 0 0         if ( (p = strrchr(attribPathTemp, '/')) ) {
1135 0           *p = '\0';
1136 0 0         if ( bpc_path_create(attribPathTemp) ) return -1;
1137 0           *p = '/';
1138             }
1139              
1140 0 0         if ( bpc_hashtable_entryCount(&dir->filesHT) == 0 ) {
1141             int fdNum;
1142             /*
1143             * Empty directory - we just generate an empty attrib file, which we don't pool
1144             */
1145 0 0         if ( (fdNum = open(attribPathTemp, O_WRONLY | O_CREAT | O_TRUNC, 0660)) < 0 ) {
1146 0           bpc_logErrf("bpc_attrib_dirWrite: can't open/create raw %s for writing\n", attribPathTemp);
1147 0           return -1;
1148             }
1149 0           close(fdNum);
1150 0 0         if ( rename(attribPathTemp, attribPath) ) {
1151 0           bpc_logErrf("bpc_attrib_dirWrite: rename from %s to %s failed\n", attribPathTemp, attribPath);
1152 0           return -1;
1153             }
1154 0 0         if ( oldDigest ) bpc_poolRefDeltaUpdate(deltaInfo, dir->compress, oldDigest, -1);
1155 0           dir->digest.len = 0;
1156 0           return 0;
1157             }
1158              
1159 0           info.bufP = info.buf;
1160 0           CONV_UINT32_TO_BUF(info.bufP, BPC_ATTRIB_TYPE_XATTR);
1161              
1162 0           bpc_poolWrite_open(&info.fd, dir->compress, NULL);
1163 0           bpc_hashtable_iterate(&dir->filesHT, (void*)bpc_attrib_fileWrite, &info);
1164 0           write_file_flush(&info);
1165 0           bpc_poolWrite_close(&info.fd, &status, &digest, &poolFileSize, &errorCnt);
1166              
1167 0 0         if ( errorCnt ) return -1;
1168              
1169             /*
1170             * Now write the small atttib file, which just contains a magic number and the digest
1171             */
1172 0 0         if ( bpc_fileZIO_open(&fd, attribPathTemp, 1, dir->compress) ) {
1173 0           bpc_logErrf("bpc_attrib_dirWrite: can't open/create %s for writing\n", attribPathTemp);
1174 0           return -1;
1175             }
1176 0           info.bufP = info.buf;
1177 0           CONV_UINT32_TO_BUF(info.bufP, BPC_ATTRIB_TYPE_DIGEST);
1178 0 0         if ( digest.len > 0 ) {
1179 0           memcpy(info.bufP, digest.digest, digest.len);
1180 0           info.bufP += digest.len;
1181             }
1182 0 0         if ( bpc_fileZIO_write(&fd, info.buf, info.bufP - info.buf) < 0 ) {
1183 0           bpc_logErrf("bpc_attrib_dirWrite: can't write to %s\n", attribPathTemp);
1184 0           bpc_fileZIO_close(&fd);
1185 0           return -1;
1186             }
1187 0           bpc_fileZIO_close(&fd);
1188 0 0         if ( rename(attribPathTemp, attribPath) ) {
1189 0           bpc_logErrf("bpc_attrib_dirWrite: rename from %s to %s failed\n", attribPathTemp, attribPath);
1190 0           return -1;
1191             }
1192 0 0         if ( BPC_LogLevel >= 8 ) bpc_logMsgf("bpc_attrib_dirWrite: new attrib digest = 0x%02x%02x%02x..., oldDigest = 0x%02x%02x...\n",
    0          
    0          
1193 0           digest.digest[0], digest.digest[1], digest.digest[2],
1194 0           oldDigest ? oldDigest->digest[0] : 0x0, oldDigest ? oldDigest->digest[1] : 0x0);
1195              
1196 0 0         if ( oldDigest ) bpc_poolRefDeltaUpdate(deltaInfo, dir->compress, oldDigest, -1);
1197 0           bpc_poolRefDeltaUpdate(deltaInfo, dir->compress, &digest, 1);
1198              
1199             /*
1200             * update with the new digest
1201             */
1202 0           memcpy(&dir->digest, &digest, sizeof(digest));
1203              
1204 0           return 0;
1205             }
1206              
1207 0           int bpc_attrib_dirWrite(bpc_deltaCount_info *deltaInfo, bpc_attrib_dir *dir, char *dirPath, char *attribFileName, bpc_digest *oldDigest)
1208             {
1209             char attribPath[BPC_MAXPATHLEN], attribPathTemp[BPC_MAXPATHLEN], *baseAttribFileName;
1210             bpc_digest digest;
1211             int status;
1212             OFF_T poolFileSize;
1213             int errorCnt;
1214             static write_info info;
1215             char *p;
1216             int fdNum;
1217             size_t attribPathLen, baseAttribFileNameLen;
1218              
1219 0 0         if ( WriteOldStyleAttribFile ) return bpc_attrib_dirWriteOld(deltaInfo, dir, dirPath, attribFileName, oldDigest);
1220              
1221             /*
1222             * baseAttribFileName points to the last portion of attribFileName, or the whole
1223             * string if it doesn't contain '/'
1224             */
1225 0 0         if ( (baseAttribFileName = strrchr(attribFileName, '/')) ) {
1226 0           baseAttribFileName++;
1227             } else {
1228 0           baseAttribFileName = attribFileName;
1229             }
1230 0           baseAttribFileNameLen = strlen(baseAttribFileName);
1231              
1232 0           bpc_attrib_attribFilePath(attribPath, dirPath, attribFileName);
1233 0 0         if ( BPC_LogLevel >= 6 ) bpc_logMsgf("bpc_attrib_dirWrite(%s): dirPath = %s, attribFileName = %s, baseAttribFileName = %s\n",
1234             attribPath, dirPath, attribFileName, baseAttribFileName);
1235 0           snprintf(attribPathTemp, BPC_MAXPATHLEN, "%s.%d", attribPath, getpid());
1236 0 0         if ( (p = strrchr(attribPathTemp, '/')) ) {
1237 0           *p = '\0';
1238 0 0         if ( bpc_path_create(attribPathTemp) ) return -1;
1239 0           *p = '/';
1240             }
1241 0           attribPathLen = strlen(attribPath);
1242              
1243 0 0         if ( bpc_hashtable_entryCount(&dir->filesHT) > 0 ) {
1244             /*
1245             * Write the attribute file to the pool
1246             */
1247 0           info.bufP = info.buf;
1248 0           CONV_UINT32_TO_BUF(info.bufP, BPC_ATTRIB_TYPE_XATTR);
1249              
1250 0           bpc_poolWrite_open(&info.fd, dir->compress, NULL);
1251 0           bpc_hashtable_iterate(&dir->filesHT, (void*)bpc_attrib_fileWrite, &info);
1252 0           write_file_flush(&info);
1253 0           bpc_poolWrite_close(&info.fd, &status, &digest, &poolFileSize, &errorCnt);
1254              
1255 0 0         if ( errorCnt ) return -1;
1256              
1257             /*
1258             * Starting in 0.50, the attrib file is always empty (so it takes no extra blocks)
1259             * and we simply include the digest in the file name by appending the hex digits.
1260             *
1261             * An empty attrib file is simply "attrib_0", and a legacy attrib file (pre <0.50)
1262             * is "attrib".
1263             */
1264 0 0         if ( attribPathLen + digest.len * 2 + 2 >= sizeof(attribPath) ) {
1265 0           bpc_logErrf("bpc_attrib_dirWrite: path too long (%d, %d, %d)\n", strlen(attribPath), digest.len, sizeof(attribPath));
1266 0           return -1;
1267             }
1268 0           attribPath[attribPathLen++] = '_';
1269 0           bpc_digest_digest2str(&digest, attribPath + attribPathLen);
1270             } else {
1271 0           digest.len = 0;
1272 0           attribPath[attribPathLen++] = '_';
1273 0           strcpy(attribPath + attribPathLen, "0");
1274             }
1275            
1276             /*
1277             * Now create an empty attrib file
1278             */
1279 0 0         if ( (fdNum = open(attribPathTemp, O_WRONLY | O_CREAT | O_TRUNC, 0660)) < 0 ) {
1280 0           bpc_logErrf("bpc_attrib_dirWrite: can't open/create raw %s for writing\n", attribPathTemp);
1281 0           return -1;
1282             }
1283 0           close(fdNum);
1284 0 0         if ( rename(attribPathTemp, attribPath) ) {
1285 0           bpc_logErrf("bpc_attrib_dirWrite: rename from %s to %s failed\n", attribPathTemp, attribPath);
1286 0           return -1;
1287             }
1288 0 0         if ( BPC_LogLevel >= 5 ) bpc_logMsgf("bpc_attrib_dirWrite: created new attrib file %s\n", attribPath);
1289              
1290 0 0         if ( BPC_LogLevel >= 8 ) bpc_logMsgf("bpc_attrib_dirWrite: new attrib digest = 0x%02x%02x%02x..., oldDigest = 0x%02x%02x...\n",
    0          
    0          
1291 0           digest.digest[0], digest.digest[1], digest.digest[2],
1292 0           oldDigest ? oldDigest->digest[0] : 0x0, oldDigest ? oldDigest->digest[1] : 0x0);
1293 0           bpc_poolRefDeltaUpdate(deltaInfo, dir->compress, &digest, 1);
1294              
1295 0 0         if ( oldDigest ) {
1296 0 0         if ( !bpc_digest_compare(&digest, oldDigest) ) {
1297 0 0         if ( BPC_LogLevel >= 2 ) bpc_logMsgf("bpc_attrib_dirWrite: old attrib has same digest; no changes to ref counts\n");
1298 0           return 0;
1299             }
1300 0 0         if ( attribPathLen + oldDigest->len * 2 + 2 >= sizeof(attribPath) ) {
1301 0           bpc_logErrf("bpc_attrib_dirWrite: oldDigest path too long (%d, %d, %d)\n", strlen(attribPath), oldDigest->len, sizeof(attribPath));
1302 0           return -1;
1303             }
1304 0           strcpy(attribPathTemp, attribPath);
1305 0 0         if ( oldDigest->len > 0 ) {
1306 0           bpc_poolRefDeltaUpdate(deltaInfo, dir->compress, oldDigest, -1);
1307 0           bpc_digest_digest2str(oldDigest, attribPathTemp + attribPathLen);
1308             } else {
1309 0           strcpy(attribPathTemp + attribPathLen, "0");
1310             }
1311 0 0         if ( !unlink(attribPathTemp) ) {
1312 0 0         if ( BPC_LogLevel >= 5 ) bpc_logMsgf("bpc_attrib_dirWrite: removed old attrib file %s\n", attribPathTemp);
1313             } else {
1314             DIR *dirOs;
1315             struct dirent *dp;
1316             char deletePath[BPC_MAXPATHLEN];
1317              
1318             /*
1319             * Scan the directory and remove any other attribute files that have the
1320             * same root.
1321             */
1322 0 0         if ( !(p = strrchr(attribPath, '/')) ) {
1323 0           bpc_logErrf("bpc_attrib_dirWrite: can't find a '/' in %s\n", attribPath);
1324 0           return -1;
1325             }
1326 0           *p++ = '\0';
1327 0 0         if ( !(dirOs = opendir(attribPath)) ) {
1328 0           bpc_logErrf("bpc_attrib_dirWrite: can't opendir %s\n", attribPath);
1329 0           return -1;
1330             }
1331 0 0         while ( (dp = readdir(dirOs)) ) {
1332 0 0         if ( strncmp(dp->d_name, baseAttribFileName, baseAttribFileNameLen) || !strcmp(dp->d_name, p) ) continue;
    0          
1333 0           snprintf(deletePath, sizeof(deletePath), "%s/%s", attribPath, dp->d_name);
1334 0           unlink(deletePath);
1335 0 0         if ( BPC_LogLevel >= 5 ) bpc_logMsgf("bpc_attrib_dirWrite: removed other old attrib file %s\n", deletePath);
1336             }
1337 0           closedir(dirOs);
1338             }
1339             }
1340              
1341             /*
1342             * update with the new digest
1343             */
1344 0           memcpy(&dir->digest, &digest, sizeof(digest));
1345              
1346 0           return 0;
1347             }