X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=utils.c;h=a6a8b756f8b33cbb4dc37142e5abbb2db4fed459;hb=9d14bf7e1b9e691428703acdbcebedb6ed2880e4;hp=2f935ab83c02b4b326dcdcf491bdc47eeb570ece;hpb=99a69b6850cc3f8d03ae08c24bc9b89d2c72260c;p=rrq%2Ffuse_xattrs.git diff --git a/utils.c b/utils.c index 2f935ab..a6a8b75 100644 --- a/utils.c +++ b/utils.c @@ -15,18 +15,11 @@ #include "utils.h" #include "fuse_xattrs_config.h" #include "xattrs_config.h" +#include "stringmem.h" /* 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; + return strjoin( xattrs_config.source_dir, b, 0 ); } int is_directory(const char *path) { @@ -59,15 +52,15 @@ int is_regular_file(const char *path) { 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