diff --git a/check.c b/check.c index 243237d5..11ffac50 100644 --- a/check.c +++ b/check.c @@ -993,7 +993,7 @@ static int state_check_process(struct snapraid_state* state, int check, int fix, int f; /* create the ancestor directories */ - ret = handle_ancestor(path); + ret = mkancestor(path); if (ret != 0) { fprintf(stderr, "WARNING! Without a working data disk, it isn't possible to fix errors on it.\n"); printf("Stopping\n"); @@ -1113,7 +1113,7 @@ static int state_check_process(struct snapraid_state* state, int check, int fix, if (fix && failed) { /* create the ancestor directories */ - ret = handle_ancestor(path); + ret = mkancestor(path); if (ret != 0) { fprintf(stderr, "WARNING! Without a working data disk, it isn't possible to fix errors on it.\n"); printf("Stopping\n"); @@ -1205,7 +1205,7 @@ static int state_check_process(struct snapraid_state* state, int check, int fix, if (fix && failed) { /* create the ancestor directories */ - ret = handle_ancestor(path); + ret = mkancestor(path); if (ret != 0) { fprintf(stderr, "WARNING! Without a working data disk, it isn't possible to fix errors on it.\n"); printf("Stopping\n"); diff --git a/handle.c b/handle.c index 46bbf9ee..bbe196a7 100644 --- a/handle.c +++ b/handle.c @@ -24,46 +24,6 @@ /****************************************************************************/ /* handle */ -int handle_ancestor(const char* file) -{ - char dir[PATH_MAX]; - char* c; - - pathcpy(dir, sizeof(dir), file); - - c = strrchr(dir, '/'); - if (!c) { - /* no ancestor */ - return 0; - } - - /* clear the file */ - *c = 0; - - if (*dir == 0) { - /* nothing more to do */ - return 0; - } - - if (access(dir, F_OK) == 0) { - /* the directory/file exists */ - return 0; - } - - /* recursively create them all */ - if (handle_ancestor(dir) != 0) { - return -1; - } - - /* create it */ - if (mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) { - fprintf(stderr, "Error creating directory '%s'. %s.\n", dir, strerror(errno)); - return -1; - } - - return 0; -} - int handle_create(struct snapraid_handle* handle, struct snapraid_file* file) { int ret; @@ -75,7 +35,7 @@ int handle_create(struct snapraid_handle* handle, struct snapraid_file* file) pathprint(handle->path, sizeof(handle->path), "%s%s", handle->disk->dir, file->sub); - ret = handle_ancestor(handle->path); + ret = mkancestor(handle->path); if (ret != 0) { return -1; } diff --git a/handle.h b/handle.h index 499580b1..3856d5a8 100644 --- a/handle.h +++ b/handle.h @@ -32,11 +32,6 @@ struct snapraid_handle { data_off_t valid_size; /**< Size of the valid data. */ }; -/** - * Creates all the required directories if missing. - */ -int handle_ancestor(const char* file); - /** * Creates a file. * The file is created if missing, and opened with write access. diff --git a/util.c b/util.c index c33202c5..59f35f53 100644 --- a/util.c +++ b/util.c @@ -654,6 +654,49 @@ int pathcmp(const char* a, const char* b) #endif } +/****************************************************************************/ +/* filesystem */ + +int mkancestor(const char* file) +{ + char dir[PATH_MAX]; + char* c; + + pathcpy(dir, sizeof(dir), file); + + c = strrchr(dir, '/'); + if (!c) { + /* no ancestor */ + return 0; + } + + /* clear the file */ + *c = 0; + + if (*dir == 0) { + /* nothing more to do */ + return 0; + } + + if (access(dir, F_OK) == 0) { + /* the directory/file exists */ + return 0; + } + + /* recursively create them all */ + if (mkancestor(dir) != 0) { + return -1; + } + + /* create it */ + if (mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) { + fprintf(stderr, "Error creating directory '%s'. %s.\n", dir, strerror(errno)); + return -1; + } + + return 0; +} + /****************************************************************************/ /* mem */ diff --git a/util.h b/util.h index f6bdf490..987c967f 100644 --- a/util.h +++ b/util.h @@ -340,6 +340,15 @@ void pathslash(char* dst, size_t size); */ int pathcmp(const char* a, const char* b); +/****************************************************************************/ +/* filesystem */ + +/** + * Creates all the ancestor directories if missing. + * The file name, after the last /, is ignored. + */ +int mkancestor(const char* file); + /****************************************************************************/ /* memory */