diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2017-04-30 17:28:32 +0200 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2017-04-30 17:28:32 +0200 |
commit | c071ac260a24561c889643b7f8df37950a597137 (patch) | |
tree | 378d5cd29701c9ff095d8caac0e834f19a059987 /src/blogc-make/ctx.c | |
parent | f3b6c03f21e45edd2220cb6fc996fe4b5fa11bb8 (diff) | |
download | blogc-c071ac260a24561c889643b7f8df37950a597137.tar.gz blogc-c071ac260a24561c889643b7f8df37950a597137.tar.bz2 blogc-c071ac260a24561c889643b7f8df37950a597137.zip |
make: copy rule should support directories, recursively
now any directories listed in the [copy] section of blogcfile will have
its files copied recursively to the output dir. this is useful for big
chunks of generated files, e.g. doxygen documentation.
Diffstat (limited to 'src/blogc-make/ctx.c')
-rw-r--r-- | src/blogc-make/ctx.c | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/src/blogc-make/ctx.c b/src/blogc-make/ctx.c index e062474..ee050a4 100644 --- a/src/blogc-make/ctx.c +++ b/src/blogc-make/ctx.c @@ -7,10 +7,13 @@ */ #include <sys/stat.h> +#include <sys/types.h> +#include <dirent.h> #include <libgen.h> #include <time.h> #include <stdlib.h> #include <stdbool.h> +#include <string.h> #include "../common/error.h" #include "../common/file.h" #include "../common/utils.h" @@ -50,6 +53,48 @@ bm_filectx_new(bm_ctx_t *ctx, const char *filename) } +bc_slist_t* +bm_filectx_new_r(bc_slist_t *l, bm_ctx_t *ctx, const char *filename) +{ + if (ctx == NULL || filename == NULL) + return NULL; + + char *f = filename[0] == '/' ? bc_strdup(filename) : + bc_strdup_printf("%s/%s", ctx->root_dir, filename); + + struct stat buf; + if (0 != stat(f, &buf)) { + free(f); + return l; + } + + if (S_ISDIR(buf.st_mode)) { + DIR *dir = opendir(f); + if (dir == NULL) { + free(f); + return l; + } + + struct dirent *e; + while (NULL != (e = readdir(dir))) { + if ((0 == strcmp(e->d_name, ".")) || (0 == strcmp(e->d_name, ".."))) + continue; + char *tmp = bc_strdup_printf("%s/%s", filename, e->d_name); + l = bm_filectx_new_r(l, ctx, tmp); + free(tmp); + } + + closedir(dir); + free(f); + return l; + } + + l = bc_slist_append(l, bm_filectx_new(ctx, filename)); + free(f); + return l; +} + + bool bm_filectx_changed(bm_filectx_t *ctx, time_t *tv_sec, long *tv_nsec) { @@ -208,8 +253,8 @@ bm_ctx_new(bm_ctx_t *base, const char *settings_file, const char *argv0, rv->copy_fctx = NULL; if (settings->copy != NULL) { for (size_t i = 0; settings->copy[i] != NULL; i++) { - rv->copy_fctx = bc_slist_append(rv->copy_fctx, - bm_filectx_new(rv, settings->copy[i])); + rv->copy_fctx = bm_filectx_new_r(rv->copy_fctx, rv, + settings->copy[i]); } } |