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
|
/*
* Directory and file system operations
*
* 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.h"
/*
* Create all the directories in the given path. Path must be non-const. Trailing '/' characters are removed.
*/
int bpc_path_create(char *path)
{
char *p = path;
STRUCT_STAT st;
int levels = 0;
if ( BPC_LogLevel >= 6 ) bpc_logMsgf("bpc_path_create(%s)\n", path);
/*
* check if it exists already
*/
if ( !stat(path, &st) && S_ISDIR(st.st_mode) ) return 0;
/*
* We walk up until we find the deepest level directory that exists.
* First remove trailing slashes.
*/
p = path + strlen(path);
while ( p > path && p[-1] == '/' ) p--;
if ( *p == '/' ) *p = '\0';
while ( p > path ) {
while ( p > path && p[-1] != '/' ) p--;
while ( p > path && p[-1] == '/' ) p--;
if ( *p == '/' ) {
*p = '\0';
levels++;
if ( !stat(path, &st) && S_ISDIR(st.st_mode) ) break;
}
}
if ( BPC_LogLevel >= 9 ) bpc_logMsgf("bpc_path_create: found that %s exists (%d levels up)\n", path, levels);
/*
* We have removed levels '/' characters from path. Replace each one and create the directory.
*/
while ( levels-- > 0 ) {
p = path + strlen(path);
*p = '/';
if ( mkdir(path, ACCESSPERMS) < 0 && errno != EEXIST) {
bpc_logErrf("bpc_path_create: can't create %s (errno %d)\n", path, errno);
return -1;
}
if ( BPC_LogLevel >= 9 ) bpc_logMsgf("bpc_path_create: created %s\n", path);
}
return 0;
}
/*
* Remove all the files below path (if a directory) and path itself. Deduct reference counts
* for every attrib file removed.
*
* Note that inodes are *not* updated, even in cases where nlinks > 0.
*/
int bpc_path_remove(bpc_deltaCount_info *deltaInfo, char *path, int compress)
{
char filePath[BPC_MAXPATHLEN];
STRUCT_STAT st;
DIR *dir;
struct dirent *dp;
int errorCnt = 0;
size_t dirListSize = 0, dirListLen = 0;
char *dirList = NULL, *dirListP;
if ( BPC_LogLevel >= 6 ) bpc_logMsgf("bpc_path_remove(%s)\n", path);
if ( !(dir = opendir(path)) ) {
unlink(path);
return errorCnt;
}
while ( (dp = readdir(dir)) ) {
if ( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") ) continue;
snprintf(filePath, sizeof(filePath), "%s/%s", path, dp->d_name);
if ( BPC_LogLevel >= 8 ) bpc_logMsgf("bpc_path_remove: removing %s\n", filePath);
if ( stat(filePath, &st) ) {
/*
* hmmm. stat failed - just try to remove it
*/
unlink(filePath);
continue;
}
if ( S_ISDIR(st.st_mode) ) {
/*
* To avoid recursing with dir still open (consuming an open fd), remember all the dirs
* and recurse after we close dir.
*/
if ( !dirList ) {
dirListSize = 4096;
if ( !(dirList = malloc(dirListSize)) ) {
bpc_logErrf("bpc_path_remove: can't allocate %u bytes\n", (unsigned)dirListSize);
return ++errorCnt;
}
}
if ( dirListLen + strlen(dp->d_name) + 1 >= dirListSize ) {
dirListSize = dirListSize * 2 + strlen(dp->d_name);
if ( !(dirList = realloc(dirList, dirListSize)) ) {
bpc_logErrf("bpc_path_remove: can't reallocate %u bytes\n", (unsigned)dirListSize);
return ++errorCnt;
}
}
strcpy(dirList + dirListLen, dp->d_name);
dirListLen += strlen(dp->d_name) + 1;
} else {
/*
* if this is an attrib file, we need to read it and deduct the reference counts.
*/
if ( !strncmp(dp->d_name, "attrib", 6) ) {
bpc_attrib_dir dir;
bpc_attrib_dirInit(&dir, compress);
if ( bpc_attrib_dirRead(&dir, NULL, filePath, 0) ) {
bpc_logErrf("bpc_path_remove: can't read attrib file %s\n", filePath);
errorCnt++;
}
if ( BPC_LogLevel >= 9 ) bpc_logMsgf("bpc_path_remove: adjusting ref counts from attrib file %s\n", filePath);
if ( !unlink(filePath) ) {
/*
* Only reduce the ref counts if we succeeded in removing the attrib file
*/
bpc_attrib_dirRefCount(deltaInfo, &dir, -1);
}
bpc_attrib_dirDestroy(&dir);
} else {
if ( unlink(filePath) ) errorCnt++;
}
}
}
closedir(dir);
/*
* Now visit the subdirs we have saved above.
*/
if ( dirList ) {
for ( dirListP = dirList ; dirListP < dirList + dirListLen ; dirListP += strlen(dirListP) + 1 ) {
snprintf(filePath, sizeof(filePath), "%s/%s", path, dirListP);
errorCnt += bpc_path_remove(deltaInfo, filePath, compress);
}
free(dirList);
}
if ( rmdir(path) ) errorCnt++;
return errorCnt;
}
/*
* Reference count all the files below the directory path, based on the attrib
* files in and below path.
*/
int bpc_path_refCountAllInodeMax(bpc_deltaCount_info *deltaInfo, char *path, int compress, int incr, unsigned int *inodeMax)
{
char filePath[BPC_MAXPATHLEN];
STRUCT_STAT st;
DIR *dir;
struct dirent *dp;
int errorCnt = 0;
size_t dirListSize = 0, dirListLen = 0;
char *dirList = NULL, *dirListP;
if ( BPC_LogLevel >= 6 ) bpc_logMsgf("bpc_path_refCountAll(%s)\n", path);
if ( !(dir = opendir(path)) ) {
return errorCnt;
}
while ( (dp = readdir(dir)) ) {
if ( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") ) continue;
snprintf(filePath, sizeof(filePath), "%s/%s", path, dp->d_name);
if ( BPC_LogLevel >= 8 ) bpc_logMsgf("bpc_path_refCountAll: got %s\n", filePath);
if ( stat(filePath, &st) ) continue;
if ( S_ISDIR(st.st_mode) ) {
/*
* To avoid recursing with dir still open (consuming an open fd), remember all the dirs
* and recurse after we close dir.
*/
if ( !dirList ) {
dirListSize = 4096;
if ( !(dirList = malloc(dirListSize)) ) {
bpc_logErrf("bpc_path_refCountAll: can't allocate %u bytes\n", (unsigned)dirListSize);
return ++errorCnt;
}
}
if ( dirListLen + strlen(dp->d_name) + 1 >= dirListSize ) {
dirListSize = dirListSize * 2 + strlen(dp->d_name);
if ( !(dirList = realloc(dirList, dirListSize)) ) {
bpc_logErrf("bpc_path_refCountAll: can't reallocate %u bytes\n", (unsigned)dirListSize);
return ++errorCnt;
}
}
strcpy(dirList + dirListLen, dp->d_name);
dirListLen += strlen(dp->d_name) + 1;
} else {
/*
* if this is an attrib file, we need to read it and deduct the reference counts.
*/
if ( !strncmp(dp->d_name, "attrib", 6) ) {
bpc_attrib_dir dir;
bpc_attrib_dirInit(&dir, compress);
if ( bpc_attrib_dirRead(&dir, path, dp->d_name, 0) ) {
bpc_logErrf("bpc_path_refCountAll: can't read attrib file %s\n", filePath);
errorCnt++;
} else {
if ( BPC_LogLevel >= 9 ) bpc_logMsgf("bpc_path_refCountAll: adjusting ref counts from attrib file %s\n", filePath);
bpc_attrib_dirRefCountInodeMax(deltaInfo, &dir, incr, inodeMax);
}
bpc_attrib_dirDestroy(&dir);
}
}
}
closedir(dir);
/*
* Now visit the subdirs we have saved above.
*/
if ( dirList ) {
for ( dirListP = dirList ; dirListP < dirList + dirListLen ; dirListP += strlen(dirListP) + 1 ) {
snprintf(filePath, sizeof(filePath), "%s/%s", path, dirListP);
errorCnt += bpc_path_refCountAllInodeMax(deltaInfo, filePath, compress, incr, inodeMax);
}
free(dirList);
}
return errorCnt;
}
/*
* Reference count all the files below the directory path, based on the attrib
* files in and below path.
*/
int bpc_path_refCountAll(bpc_deltaCount_info *deltaInfo, char *path, int compress, int incr)
{
return bpc_path_refCountAllInodeMax(deltaInfo, path, compress, incr, NULL);
}
/*
* Add an exclusive lock to the byte range in the given file.
* Blocks until the lock becomes available.
*/
int bpc_lockRangeFd(int fd, OFF_T offset, OFF_T len, int block)
{
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = offset;
lock.l_len = len;
lock.l_pid = 0;
return fcntl(fd, block ? F_SETLKW : F_SETLK, &lock);
}
int bpc_unlockRangeFd(int fd, OFF_T offset, OFF_T len)
{
struct flock lock;
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = offset;
lock.l_len = len;
lock.l_pid = 0;
return fcntl(fd, F_SETLK, &lock);
}
int bpc_lockRangeFile(char *lockFile, OFF_T offset, OFF_T len, int block)
{
int fd;
if ( (fd = open(lockFile, O_CREAT | O_RDWR, 0660)) < 0 ) {
bpc_logErrf("bpc_lockRangeFile: can't open/create lock file %s\n", lockFile);
return fd;
}
if ( bpc_lockRangeFd(fd, offset, len, block) ) {
close(fd);
if ( block ) {
bpc_logErrf("bpc_lockRangeFile: lock(%s) failed (errno = %d)\n", lockFile, errno);
}
return -1;
}
return fd;
}
void bpc_unlockRangeFile(int lockFd)
{
if ( lockFd >= 0 ) close(lockFd);
}
|