mirror of
https://github.com/amadvance/snapraid.git
synced 2026-12-03 05:10:16 -05:00
Moves handle_ancestor() to mkancestor() to be a generic function.
This commit is contained in:
parent
e92f712f49
commit
a1e7786dd5
5 changed files with 56 additions and 49 deletions
6
check.c
6
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");
|
||||
|
|
|
|||
42
handle.c
42
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;
|
||||
}
|
||||
|
|
|
|||
5
handle.h
5
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.
|
||||
|
|
|
|||
43
util.c
43
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 */
|
||||
|
||||
|
|
|
|||
9
util.h
9
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 */
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue