Moves handle_ancestor() to mkancestor() to be a generic function.

This commit is contained in:
Andrea Mazzoleni 2013-02-20 13:29:58 +01:00
parent e92f712f49
commit a1e7786dd5
5 changed files with 56 additions and 49 deletions

View file

@ -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");

View file

@ -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;
}

View file

@ -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
View file

@ -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
View file

@ -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 */