BackupPC-XS
view release on metacpan or search on metacpan
bpc_dirOps.c view on Meta::CPAN
/*
* 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;
( run in 0.711 second using v1.01-cache-2.11-cpan-39bf76dae61 )