bpc_dirOps.c

filter: Statementstmt 0.0% 0 / 139 Branchbran 0.0% 0 / 122 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 261 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« bpc_attribCache.c ↑ Summary bpc_fileDigest.c »
1 /*
2 * Directory and file system operations
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 * Create all the directories in the given path. Path must be non-const. Trailing '/' characters are removed.
24 */
25 0 int bpc_path_create(char *path)
26 {
27 0 char *p = path;
28 STRUCT_STAT st;
29 0 int levels = 0;
30  
31 0 ▶ if ( BPC_LogLevel >= 6 ) bpc_logMsgf("bpc_path_create(%s)\n", path);
Branch: 1 branch
BranchTrueFalseTotal
if ( BPC_LogLevel >= 6 ) bpc_logMsgf("bpc_path_create(%s)\n", path); 0 0 0
32 /*
33 * check if it exists already
34 */
35 0 ▶ if ( !stat(path, &st) && S_ISDIR(st.st_mode) ) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if ( !stat(path, &st) && S_ISDIR(st.st_mode) ) return 0; 0 0 0
if ( !stat(path, &st) && S_ISDIR(st.st_mode) ) return 0; 0 0 0
36  
37 /*
38 * We walk up until we find the deepest level directory that exists.
39 * First remove trailing slashes.
40 */
41 0 p = path + strlen(path);
42 0 ▶ while ( p > path && p[-1] == '/' ) p--;
Branch: 2 branches
BranchTrueFalseTotal
while ( p > path && p[-1] == '/' ) p--; 0 0 0
while ( p > path && p[-1] == '/' ) p--; 0 0 0
43 0 ▶ if ( *p == '/' ) *p = '\0';
Branch: 1 branch
BranchTrueFalseTotal
if ( *p == '/' ) *p = '\0'; 0 0 0
44 0 ▶ while ( p > path ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( p > path ) { 0 0 0
45 0 ▶ while ( p > path && p[-1] != '/' ) p--;
Branch: 2 branches
BranchTrueFalseTotal
while ( p > path && p[-1] != '/' ) p--; 0 0 0
while ( p > path && p[-1] != '/' ) p--; 0 0 0
46 0 ▶ while ( p > path && p[-1] == '/' ) p--;
Branch: 2 branches
BranchTrueFalseTotal
while ( p > path && p[-1] == '/' ) p--; 0 0 0
while ( p > path && p[-1] == '/' ) p--; 0 0 0
47 0 ▶ if ( *p == '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( *p == '/' ) { 0 0 0
48 0 *p = '\0';
49 0 levels++;
50 0 ▶ if ( !stat(path, &st) && S_ISDIR(st.st_mode) ) break;
Branch: 2 branches
BranchTrueFalseTotal
if ( !stat(path, &st) && S_ISDIR(st.st_mode) ) break; 0 0 0
if ( !stat(path, &st) && S_ISDIR(st.st_mode) ) break; 0 0 0
51 }
52 }
53 0 ▶ if ( BPC_LogLevel >= 9 ) bpc_logMsgf("bpc_path_create: found that %s exists (%d levels up)\n", path, levels);
Branch: 1 branch
BranchTrueFalseTotal
if ( BPC_LogLevel >= 9 ) bpc_logMsgf("bpc_path_create: found that %s exists (%d levels up)\n", path, levels); 0 0 0
54  
55 /*
56 * We have removed levels '/' characters from path. Replace each one and create the directory.
57 */
58 0 ▶ while ( levels-- > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( levels-- > 0 ) { 0 0 0
59 0 p = path + strlen(path);
60 0 *p = '/';
61 0 ▶ if ( mkdir(path, ACCESSPERMS) < 0 && errno != EEXIST) {
Branch: 2 branches
BranchTrueFalseTotal
if ( mkdir(path, ACCESSPERMS) < 0 && errno != EEXIST) { 0 0 0
if ( mkdir(path, ACCESSPERMS) < 0 && errno != EEXIST) { 0 0 0
62 0 bpc_logErrf("bpc_path_create: can't create %s (errno %d)\n", path, errno);
63 0 return -1;
64 }
65 0 ▶ if ( BPC_LogLevel >= 9 ) bpc_logMsgf("bpc_path_create: created %s\n", path);
Branch: 1 branch
BranchTrueFalseTotal
if ( BPC_LogLevel >= 9 ) bpc_logMsgf("bpc_path_create: created %s\n", path); 0 0 0
66 }
67 0 return 0;
68 }
69  
70 /*
71 * Remove all the files below path (if a directory) and path itself. Deduct reference counts
72 * for every attrib file removed.
73 *
74 * Note that inodes are *not* updated, even in cases where nlinks > 0.
75 */
76 0 int bpc_path_remove(bpc_deltaCount_info *deltaInfo, char *path, int compress)
77 {
78 char filePath[BPC_MAXPATHLEN];
79 STRUCT_STAT st;
80 DIR *dir;
81 struct dirent *dp;
82 0 int errorCnt = 0;
83 0 size_t dirListSize = 0, dirListLen = 0;
84 0 char *dirList = NULL, *dirListP;
85  
86 0 ▶ if ( BPC_LogLevel >= 6 ) bpc_logMsgf("bpc_path_remove(%s)\n", path);
Branch: 1 branch
BranchTrueFalseTotal
if ( BPC_LogLevel >= 6 ) bpc_logMsgf("bpc_path_remove(%s)\n", path); 0 0 0
87 0 ▶ if ( !(dir = opendir(path)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !(dir = opendir(path)) ) { 0 0 0
88 0 unlink(path);
89 0 return errorCnt;
90 }
91 0 ▶ while ( (dp = readdir(dir)) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( (dp = readdir(dir)) ) { 0 0 0
92 0 ▶ if ( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") ) continue;
Branch: 2 branches
BranchTrueFalseTotal
if ( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") ) continue; 0 0 0
if ( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") ) continue; 0 0 0
93 0 snprintf(filePath, sizeof(filePath), "%s/%s", path, dp->d_name);
94 0 ▶ if ( BPC_LogLevel >= 8 ) bpc_logMsgf("bpc_path_remove: removing %s\n", filePath);
Branch: 1 branch
BranchTrueFalseTotal
if ( BPC_LogLevel >= 8 ) bpc_logMsgf("bpc_path_remove: removing %s\n", filePath); 0 0 0
95 0 ▶ if ( stat(filePath, &st) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( stat(filePath, &st) ) { 0 0 0
96 /*
97 * hmmm. stat failed - just try to remove it
98 */
99 0 unlink(filePath);
100 0 continue;
101 }
102 0 ▶ if ( S_ISDIR(st.st_mode) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( S_ISDIR(st.st_mode) ) { 0 0 0
103 /*
104 * To avoid recursing with dir still open (consuming an open fd), remember all the dirs
105 * and recurse after we close dir.
106 */
107 0 ▶ if ( !dirList ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !dirList ) { 0 0 0
108 0 dirListSize = 4096;
109 0 ▶ if ( !(dirList = malloc(dirListSize)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !(dirList = malloc(dirListSize)) ) { 0 0 0
110 0 bpc_logErrf("bpc_path_remove: can't allocate %u bytes\n", (unsigned)dirListSize);
111 0 return ++errorCnt;
112 }
113 }
114 0 ▶ if ( dirListLen + strlen(dp->d_name) + 1 >= dirListSize ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( dirListLen + strlen(dp->d_name) + 1 >= dirListSize ) { 0 0 0
115 0 dirListSize = dirListSize * 2 + strlen(dp->d_name);
116 0 ▶ if ( !(dirList = realloc(dirList, dirListSize)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !(dirList = realloc(dirList, dirListSize)) ) { 0 0 0
117 0 bpc_logErrf("bpc_path_remove: can't reallocate %u bytes\n", (unsigned)dirListSize);
118 0 return ++errorCnt;
119 }
120 }
121 0 strcpy(dirList + dirListLen, dp->d_name);
122 0 dirListLen += strlen(dp->d_name) + 1;
123 } else {
124 /*
125 * if this is an attrib file, we need to read it and deduct the reference counts.
126 */
127 0 ▶ if ( !strncmp(dp->d_name, "attrib", 6) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !strncmp(dp->d_name, "attrib", 6) ) { 0 0 0
128 bpc_attrib_dir dir;
129  
130 0 bpc_attrib_dirInit(&dir, compress);
131 0 ▶ if ( bpc_attrib_dirRead(&dir, NULL, filePath, 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( bpc_attrib_dirRead(&dir, NULL, filePath, 0) ) { 0 0 0
132 0 bpc_logErrf("bpc_path_remove: can't read attrib file %s\n", filePath);
133 0 errorCnt++;
134 }
135 0 ▶ if ( BPC_LogLevel >= 9 ) bpc_logMsgf("bpc_path_remove: adjusting ref counts from attrib file %s\n", filePath);
Branch: 1 branch
BranchTrueFalseTotal
if ( BPC_LogLevel >= 9 ) bpc_logMsgf("bpc_path_remove: adjusting ref counts from attrib file %s\n", filePath); 0 0 0
136 0 ▶ if ( !unlink(filePath) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !unlink(filePath) ) { 0 0 0
137 /*
138 * Only reduce the ref counts if we succeeded in removing the attrib file
139 */
140 0 bpc_attrib_dirRefCount(deltaInfo, &dir, -1);
141 }
142 0 bpc_attrib_dirDestroy(&dir);
143 } else {
144 0 ▶ if ( unlink(filePath) ) errorCnt++;
Branch: 1 branch
BranchTrueFalseTotal
if ( unlink(filePath) ) errorCnt++; 0 0 0
145 }
146 }
147 }
148 0 closedir(dir);
149 /*
150 * Now visit the subdirs we have saved above.
151 */
152 0 ▶ if ( dirList ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( dirList ) { 0 0 0
153 0 ▶ for ( dirListP = dirList ; dirListP < dirList + dirListLen ; dirListP += strlen(dirListP) + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( dirListP = dirList ; dirListP < dirList + dirListLen ; dirListP += strlen(dirListP) + 1 ) { 0 0 0
154 0 snprintf(filePath, sizeof(filePath), "%s/%s", path, dirListP);
155 0 errorCnt += bpc_path_remove(deltaInfo, filePath, compress);
156 }
157 0 free(dirList);
158 }
159 0 ▶ if ( rmdir(path) ) errorCnt++;
Branch: 1 branch
BranchTrueFalseTotal
if ( rmdir(path) ) errorCnt++; 0 0 0
160 0 return errorCnt;
161 }
162  
163 /*
164 * Reference count all the files below the directory path, based on the attrib
165 * files in and below path.
166 */
167 0 int bpc_path_refCountAllInodeMax(bpc_deltaCount_info *deltaInfo, char *path, int compress, int incr, unsigned int *inodeMax)
168 {
169 char filePath[BPC_MAXPATHLEN];
170 STRUCT_STAT st;
171 DIR *dir;
172 struct dirent *dp;
173 0 int errorCnt = 0;
174 0 size_t dirListSize = 0, dirListLen = 0;
175 0 char *dirList = NULL, *dirListP;
176  
177 0 ▶ if ( BPC_LogLevel >= 6 ) bpc_logMsgf("bpc_path_refCountAll(%s)\n", path);
Branch: 1 branch
BranchTrueFalseTotal
if ( BPC_LogLevel >= 6 ) bpc_logMsgf("bpc_path_refCountAll(%s)\n", path); 0 0 0
178 0 ▶ if ( !(dir = opendir(path)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !(dir = opendir(path)) ) { 0 0 0
179 0 return errorCnt;
180 }
181 0 ▶ while ( (dp = readdir(dir)) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( (dp = readdir(dir)) ) { 0 0 0
182 0 ▶ if ( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") ) continue;
Branch: 2 branches
BranchTrueFalseTotal
if ( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") ) continue; 0 0 0
if ( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") ) continue; 0 0 0
183 0 snprintf(filePath, sizeof(filePath), "%s/%s", path, dp->d_name);
184 0 ▶ if ( BPC_LogLevel >= 8 ) bpc_logMsgf("bpc_path_refCountAll: got %s\n", filePath);
Branch: 1 branch
BranchTrueFalseTotal
if ( BPC_LogLevel >= 8 ) bpc_logMsgf("bpc_path_refCountAll: got %s\n", filePath); 0 0 0
185 0 ▶ if ( stat(filePath, &st) ) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ( stat(filePath, &st) ) continue; 0 0 0
186 0 ▶ if ( S_ISDIR(st.st_mode) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( S_ISDIR(st.st_mode) ) { 0 0 0
187 /*
188 * To avoid recursing with dir still open (consuming an open fd), remember all the dirs
189 * and recurse after we close dir.
190 */
191 0 ▶ if ( !dirList ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !dirList ) { 0 0 0
192 0 dirListSize = 4096;
193 0 ▶ if ( !(dirList = malloc(dirListSize)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !(dirList = malloc(dirListSize)) ) { 0 0 0
194 0 bpc_logErrf("bpc_path_refCountAll: can't allocate %u bytes\n", (unsigned)dirListSize);
195 0 return ++errorCnt;
196 }
197 }
198 0 ▶ if ( dirListLen + strlen(dp->d_name) + 1 >= dirListSize ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( dirListLen + strlen(dp->d_name) + 1 >= dirListSize ) { 0 0 0
199 0 dirListSize = dirListSize * 2 + strlen(dp->d_name);
200 0 ▶ if ( !(dirList = realloc(dirList, dirListSize)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !(dirList = realloc(dirList, dirListSize)) ) { 0 0 0
201 0 bpc_logErrf("bpc_path_refCountAll: can't reallocate %u bytes\n", (unsigned)dirListSize);
202 0 return ++errorCnt;
203 }
204 }
205 0 strcpy(dirList + dirListLen, dp->d_name);
206 0 dirListLen += strlen(dp->d_name) + 1;
207 } else {
208 /*
209 * if this is an attrib file, we need to read it and deduct the reference counts.
210 */
211 0 ▶ if ( !strncmp(dp->d_name, "attrib", 6) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !strncmp(dp->d_name, "attrib", 6) ) { 0 0 0
212 bpc_attrib_dir dir;
213  
214 0 bpc_attrib_dirInit(&dir, compress);
215 0 ▶ if ( bpc_attrib_dirRead(&dir, path, dp->d_name, 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( bpc_attrib_dirRead(&dir, path, dp->d_name, 0) ) { 0 0 0
216 0 bpc_logErrf("bpc_path_refCountAll: can't read attrib file %s\n", filePath);
217 0 errorCnt++;
218 } else {
219 0 ▶ if ( BPC_LogLevel >= 9 ) bpc_logMsgf("bpc_path_refCountAll: adjusting ref counts from attrib file %s\n", filePath);
Branch: 1 branch
BranchTrueFalseTotal
if ( BPC_LogLevel >= 9 ) bpc_logMsgf("bpc_path_refCountAll: adjusting ref counts from attrib file %s\n", filePath); 0 0 0
220 0 bpc_attrib_dirRefCountInodeMax(deltaInfo, &dir, incr, inodeMax);
221 }
222 0 bpc_attrib_dirDestroy(&dir);
223 }
224 }
225 }
226 0 closedir(dir);
227 /*
228 * Now visit the subdirs we have saved above.
229 */
230 0 ▶ if ( dirList ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( dirList ) { 0 0 0
231 0 ▶ for ( dirListP = dirList ; dirListP < dirList + dirListLen ; dirListP += strlen(dirListP) + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( dirListP = dirList ; dirListP < dirList + dirListLen ; dirListP += strlen(dirListP) + 1 ) { 0 0 0
232 0 snprintf(filePath, sizeof(filePath), "%s/%s", path, dirListP);
233 0 errorCnt += bpc_path_refCountAllInodeMax(deltaInfo, filePath, compress, incr, inodeMax);
234 }
235 0 free(dirList);
236 }
237 0 return errorCnt;
238 }
239  
240 /*
241 * Reference count all the files below the directory path, based on the attrib
242 * files in and below path.
243 */
244 0 int bpc_path_refCountAll(bpc_deltaCount_info *deltaInfo, char *path, int compress, int incr)
245 {
246 0 return bpc_path_refCountAllInodeMax(deltaInfo, path, compress, incr, NULL);
247 }
248  
249 /*
250 * Add an exclusive lock to the byte range in the given file.
251 * Blocks until the lock becomes available.
252 */
253 0 int bpc_lockRangeFd(int fd, OFF_T offset, OFF_T len, int block)
254 {
255 struct flock lock;
256  
257 0 lock.l_type = F_WRLCK;
258 0 lock.l_whence = SEEK_SET;
259 0 lock.l_start = offset;
260 0 lock.l_len = len;
261 0 lock.l_pid = 0;
262  
263 0 ▶ return fcntl(fd, block ? F_SETLKW : F_SETLK, &lock);
Branch: 1 branch
BranchTrueFalseTotal
return fcntl(fd, block ? F_SETLKW : F_SETLK, &lock); 0 0 0
264 }
265  
266 0 int bpc_unlockRangeFd(int fd, OFF_T offset, OFF_T len)
267 {
268 struct flock lock;
269  
270 0 lock.l_type = F_UNLCK;
271 0 lock.l_whence = SEEK_SET;
272 0 lock.l_start = offset;
273 0 lock.l_len = len;
274 0 lock.l_pid = 0;
275  
276 0 return fcntl(fd, F_SETLK, &lock);
277 }
278  
279 0 int bpc_lockRangeFile(char *lockFile, OFF_T offset, OFF_T len, int block)
280 {
281 int fd;
282  
283 0 ▶ if ( (fd = open(lockFile, O_CREAT | O_RDWR, 0660)) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (fd = open(lockFile, O_CREAT | O_RDWR, 0660)) < 0 ) { 0 0 0
284 0 bpc_logErrf("bpc_lockRangeFile: can't open/create lock file %s\n", lockFile);
285 0 return fd;
286 }
287 0 ▶ if ( bpc_lockRangeFd(fd, offset, len, block) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( bpc_lockRangeFd(fd, offset, len, block) ) { 0 0 0
288 0 close(fd);
289 0 ▶ if ( block ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( block ) { 0 0 0
290 0 bpc_logErrf("bpc_lockRangeFile: lock(%s) failed (errno = %d)\n", lockFile, errno);
291 }
292 0 return -1;
293 }
294 0 return fd;
295 }
296  
297 0 void bpc_unlockRangeFile(int lockFd)
298 {
299 0 ▶ if ( lockFd >= 0 ) close(lockFd);
Branch: 1 branch
BranchTrueFalseTotal
if ( lockFd >= 0 ) close(lockFd); 0 0 0
300 0 }
« bpc_attribCache.c ↑ Summary bpc_fileDigest.c »