X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=utils.c;h=a6a8b756f8b33cbb4dc37142e5abbb2db4fed459;hb=HEAD;hp=f245e7e3e84849b0a62d57a9c5dd09ff872749bb;hpb=2f9db7e8ce45c8a7eb3a0e3bc126d0480661b09e;p=rrq%2Ffuse_xattrs.git diff --git a/utils.c b/utils.c index f245e7e..a6a8b75 100644 --- a/utils.c +++ b/utils.c @@ -10,29 +10,57 @@ #include #include #include +#include #include "utils.h" #include "fuse_xattrs_config.h" +#include "xattrs_config.h" +#include "stringmem.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) { + return strjoin( xattrs_config.source_dir, b, 0 ); +} + +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; + } - return dst; + if (!S_ISREG(statbuf.st_mode)) { + return -1; + } + + return 1; } char *get_sidecar_path(const char *path) { - const size_t path_len = strlen(path); - const size_t sidecar_ext_len = strlen(BINARY_SIDECAR_EXT); // this can be optimized - const size_t sidecar_path_len = path_len + sidecar_ext_len + 1; - char *sidecar_path = (char *) malloc(sidecar_path_len); - memset(sidecar_path, '\0', sidecar_path_len); - memcpy(sidecar_path, path, path_len); - memcpy(sidecar_path + path_len, BINARY_SIDECAR_EXT, sidecar_ext_len); - - return sidecar_path; +#define CFG xattrs_config + if ( strncmp( path, CFG.source_dir, CFG.source_dir_size ) == 0 ) { + const char *p = path + CFG.source_dir_size; + if ( CFG.sidecar_dir ) { + return strjoin( CFG.sidecar_dir, p, BINARY_SIDECAR_EXT, 0 ); + } + } + return strjoin( path, BINARY_SIDECAR_EXT, 0 ); +#undef CFG } // TODO: make it work for binary data