X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=utils.c;h=2f935ab83c02b4b326dcdcf491bdc47eeb570ece;hb=2aaa49414e781633db18c263167f5ce27d9d1e22;hp=f245e7e3e84849b0a62d57a9c5dd09ff872749bb;hpb=2f9db7e8ce45c8a7eb3a0e3bc126d0480661b09e;p=rrq%2Ffuse_xattrs.git diff --git a/utils.c b/utils.c index f245e7e..2f935ab 100644 --- a/utils.c +++ b/utils.c @@ -10,18 +10,53 @@ #include #include #include +#include #include "utils.h" #include "fuse_xattrs_config.h" +#include "xattrs_config.h" -char *prepend_source_directory(const char *a, const char *b) { - size_t len = strlen(a) + strlen(b) + 1; - char *dst = (char*) malloc(sizeof(char) * len); - sprintf(dst, "%s%s", a, b); +/* TODO: re-use memory to avoid calling malloc every time */ +char *prepend_source_directory(const char *b) { + const size_t b_size = strlen(b); + const size_t dst_len = xattrs_config.source_dir_size + b_size + 1; + char *dst = (char*) malloc(sizeof(char) * dst_len); + + memcpy(dst, xattrs_config.source_dir, xattrs_config.source_dir_size); + memcpy(dst+xattrs_config.source_dir_size, b, b_size + 1); // include '\0' + //sprintf(dst, "%s%s", a, b); return dst; } +int is_directory(const char *path) { + struct stat statbuf; + if (stat(path, &statbuf) != 0) { + fprintf(stderr, "cannot get source directory status: %s\n", path); + return -1; + } + + if (!S_ISDIR(statbuf.st_mode)) { + fprintf(stderr, "source directory must be a directory: %s\n", path); + return -1; + } + + return 1; +} + +int is_regular_file(const char *path) { + struct stat statbuf; + if (stat(path, &statbuf) != 0) { + return -1; + } + + if (!S_ISREG(statbuf.st_mode)) { + return -1; + } + + return 1; +} + char *get_sidecar_path(const char *path) { const size_t path_len = strlen(path);