X-Git-Url: http://git.wpitchoune.net/gitweb/?p=ppastats.git;a=blobdiff_plain;f=src%2Fpio.c;h=701823debca23e3d59b1f93a34b8ff6e6e99f046;hp=35202a53500525105ac8757dae6e4e947f528a84;hb=eb26e5d2404df23c3151a55a6d887abb421ef601;hpb=2a5acf4e9e701b60e600b86366687c267fb64d9d diff --git a/src/pio.c b/src/pio.c index 35202a5..701823d 100644 --- a/src/pio.c +++ b/src/pio.c @@ -18,15 +18,16 @@ */ #include +#include #include #include #include #include #include +#include #include - /* Directory separator is \ when cross-compiling for MS Windows systems */ #if defined(__MINGW32__) @@ -234,6 +235,8 @@ file_copy(const char *src, const char *dst) FILE *fsrc, *fdst; int ret = 0; + log_fct("copy %s to %s", src, dst); + fsrc = fopen(src, "r"); if (fsrc) { @@ -283,10 +286,15 @@ char *path_append(const char *dir, const char *path) void mkdirs(const char *dirs, mode_t mode) { - char *c = (char *)dirs; - char *dir = malloc(strlen(dirs) + 1); + char *c, *dir; + int i; + + log_fct("mkdirs %s", dirs); - int i = 0; + c = (char *)dirs; + dir = malloc(strlen(dirs) + 1); + + i = 0; while (*c) { if ((*c == DIRSEP || *c == '\0') && c != dirs) { strncpy(dir, dirs, i); @@ -328,3 +336,60 @@ file_copy_print_error(int code, const char *src, const char *dst) printf("File copy error: unknown error %d.\n", code); } } + +int dir_rcopy(const char *src, const char *dst) +{ + int ret; + char **paths; + FTS *ftsp; + FTSENT *p, *chp; + int fts_options = FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR; + char *p_dst, *n_dst; + + log_fct_enter(); + + log_fct("copy dir %s to %s", src, dst); + + paths = malloc(2 * sizeof(char *)); + paths[0] = strdup(src); + paths[1] = NULL; + + ftsp = fts_open(paths, fts_options, NULL); + if (!ftsp) + return 1; + + chp = fts_children(ftsp, 0); + if (!chp) + return 0; + + n_dst = dir_normalize(dst); + + while ((p = fts_read(ftsp)) != NULL) { + switch (p->fts_info) { + case FTS_D: + p_dst = path_append(n_dst, + p->fts_path + strlen(src) + 1); + mkdirs(p_dst, 0777); + free(p_dst); + break; + case FTS_F: + p_dst = path_append(n_dst, + p->fts_path + strlen(src) + 1); + file_copy(p->fts_path, p_dst); + free(p_dst); + break; + default: + break; + } + } + fts_close(ftsp); + + free(n_dst); + free(paths); + + ret = 0; + + log_fct_exit(); + + return ret; +}