From: Felipe Barriga Richards Date: Thu, 16 Feb 2017 19:23:45 +0000 (-0300) Subject: optimization: prepend_source_directory X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=af0aaaa2a3edc0634ebb0a32750df759ab3eb515;p=rrq%2Ffuse_xattrs.git optimization: prepend_source_directory --- diff --git a/fuse_xattrs.c b/fuse_xattrs.c index 48e136b..b0461a0 100644 --- a/fuse_xattrs.c +++ b/fuse_xattrs.c @@ -53,7 +53,7 @@ static int xmp_setxattr(const char *path, const char *name, const char *value, s return -ENOSPC; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); #ifdef DEBUG char *sanitized_value = sanitize_value(value, size); @@ -84,7 +84,7 @@ static int xmp_getxattr(const char *path, const char *name, char *value, size_t return -ERANGE; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); debug_print("path=%s name=%s size=%zu\n", _path, name, size); int rtval = binary_storage_read_key(_path, name, value, size); free(_path); @@ -103,7 +103,7 @@ static int xmp_listxattr(const char *path, char *list, size_t size) return -E2BIG; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); debug_print("path=%s size=%zu\n", _path, size); int rtval = binary_storage_list_keys(_path, list, size); free(_path); @@ -126,7 +126,7 @@ static int xmp_removexattr(const char *path, const char *name) return -ERANGE; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); debug_print("path=%s name=%s\n", _path, name); int rtval = binary_storage_remove_key(_path, name); free(_path); @@ -249,6 +249,7 @@ static int xattrs_opt_proc(void *data, const char *arg, int key, case FUSE_OPT_KEY_NONOPT: if (!xattrs_config.source_dir) { xattrs_config.source_dir = sanitized_source_directory(arg); + xattrs_config.source_dir_size = strlen(xattrs_config.source_dir); return 0; } break; diff --git a/passthrough.c b/passthrough.c index 4b1ab7e..3f55490 100644 --- a/passthrough.c +++ b/passthrough.c @@ -32,7 +32,7 @@ int xmp_getattr(const char *path, struct stat *stbuf) { return -ENOENT; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); res = lstat(_path, stbuf); free(_path); @@ -48,7 +48,7 @@ int xmp_access(const char *path, int mask) { return -ENOENT; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); res = access(_path, mask); free(_path); @@ -64,7 +64,7 @@ int xmp_readlink(const char *path, char *buf, size_t size) { return -ENOENT; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); res = readlink(_path, buf, size - 1); free(_path); @@ -84,7 +84,7 @@ int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler, (void) offset; (void) fi; - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); dp = opendir(_path); free(_path); @@ -114,7 +114,7 @@ int xmp_mknod(const char *path, mode_t mode, dev_t rdev) { return -ENOENT; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); /* On Linux this could just be 'mknod(path, mode, rdev)' but this is more portable */ @@ -140,7 +140,7 @@ int xmp_mkdir(const char *path, mode_t mode) { return -ENOENT; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); res = mkdir(_path, mode); free(_path); @@ -156,7 +156,7 @@ int xmp_unlink(const char *path) { return -ENOENT; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); res = unlink(_path); free(_path); @@ -172,7 +172,7 @@ int xmp_rmdir(const char *path) { return -ENOENT; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); res = rmdir(_path); free(_path); @@ -190,7 +190,7 @@ int xmp_symlink(const char *from, const char *to) { } } - char *_to = prepend_source_directory(xattrs_config.source_dir, to); + char *_to = prepend_source_directory(to); res = symlink(from, _to); free(_to); @@ -208,8 +208,8 @@ int xmp_rename(const char *from, const char *to) { } } - char *_from = prepend_source_directory(xattrs_config.source_dir, from); - char *_to = prepend_source_directory(xattrs_config.source_dir, to); + char *_from = prepend_source_directory(from); + char *_to = prepend_source_directory(to); res = rename(_from, _to); free(_from); free(_to); @@ -228,8 +228,8 @@ int xmp_link(const char *from, const char *to) { } } - char *_from = prepend_source_directory(xattrs_config.source_dir, from); - char *_to = prepend_source_directory(xattrs_config.source_dir, to); + char *_from = prepend_source_directory(from); + char *_to = prepend_source_directory(to); res = link(_from, _to); free(_from); free(_to); @@ -246,7 +246,7 @@ int xmp_chmod(const char *path, mode_t mode) { return -ENOENT; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); res = chmod(_path, mode); free(_path); @@ -262,7 +262,7 @@ int xmp_chown(const char *path, uid_t uid, gid_t gid) { return -ENOENT; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); res = lchown(_path, uid, gid); free(_path); @@ -278,7 +278,7 @@ int xmp_truncate(const char *path, off_t size) { return -ENOENT; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); res = truncate(_path, size); free(_path); @@ -299,7 +299,7 @@ struct fuse_file_info *fi) (void) fi; int res; - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); /* don't use utime/utimes since they follow symlinks */ res = utimensat(0, _path, ts, AT_SYMLINK_NOFOLLOW); free(_path); @@ -316,7 +316,7 @@ int xmp_open(const char *path, struct fuse_file_info *fi) { return -ENOENT; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); res = open(_path, fi->flags); free(_path); @@ -338,7 +338,7 @@ int xmp_read(const char *path, char *buf, size_t size, off_t offset, int res; (void) fi; - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); fd = open(_path, O_RDONLY); free(_path); @@ -364,7 +364,7 @@ int xmp_write(const char *path, const char *buf, size_t size, int res; (void) fi; - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); fd = open(_path, O_WRONLY); free(_path); @@ -385,7 +385,7 @@ int xmp_statfs(const char *path, struct statvfs *stbuf) { return -ENOENT; } - char *_path = prepend_source_directory(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); res = statvfs(_path, stbuf); free(_path); @@ -431,7 +431,7 @@ int xmp_fallocate(const char *path, int mode, if (mode) return -EOPNOTSUPP; - char *_path = concat(xattrs_config.source_dir, path); + char *_path = prepend_source_directory(path); fd = open(_path, O_WRONLY); free(_path); diff --git a/utils.c b/utils.c index f245e7e..71fe009 100644 --- a/utils.c +++ b/utils.c @@ -13,11 +13,17 @@ #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; } diff --git a/utils.h b/utils.h index b3b899b..1c20826 100644 --- a/utils.h +++ b/utils.h @@ -38,7 +38,7 @@ enum namespace { enum namespace get_namespace(const char *name); char *get_sidecar_path(const char *path); char *sanitize_value(const char *value, size_t value_size); -char *prepend_source_directory(const char *a, const char *b); +char *prepend_source_directory(const char *b); const size_t BINARY_SIDECAR_EXT_SIZE; const int filename_is_sidecar(const char *string); diff --git a/xattrs_config.h b/xattrs_config.h index 3803902..b3a2917 100644 --- a/xattrs_config.h +++ b/xattrs_config.h @@ -11,8 +11,10 @@ #define FUSE_XATTRS_CONFIG_H struct xattrs_config { - int show_sidecar; + const int show_sidecar; const char *source_dir; + size_t source_dir_size; + } xattrs_config;