From c2151710dfb6f6ebe0134477198265e67142101b Mon Sep 17 00:00:00 2001 From: Felipe Barriga Richards Date: Thu, 16 Feb 2017 12:17:35 -0300 Subject: [PATCH] support for hidding sidecar files. --- TODO.md | 1 - fuse_xattrs.c | 49 ++++++++++++++++++------- fuse_xattrs_config.h.in | 3 -- passthrough.c | 81 +++++++++++++++++++++++++++++++++++++++-- utils.c | 18 +++++++++ utils.h | 3 ++ xattrs_config.h | 1 + 7 files changed, 134 insertions(+), 22 deletions(-) diff --git a/TODO.md b/TODO.md index 13be7cb..934bc41 100644 --- a/TODO.md +++ b/TODO.md @@ -5,7 +5,6 @@ TODO - Handle permission issues with .xattr files - Add options to: - Change extension name of sidecar files - - Hide sidecar files - Code Quality - Valgrind support - C unit tests diff --git a/fuse_xattrs.c b/fuse_xattrs.c index 610ad4a..48e136b 100644 --- a/fuse_xattrs.c +++ b/fuse_xattrs.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -35,6 +36,10 @@ static int xmp_setxattr(const char *path, const char *name, const char *value, size_t size, int flags) { + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } + if (get_namespace(name) != USER) { debug_print("Only user namespace is supported. name=%s\n", name); return -ENOTSUP; @@ -66,6 +71,10 @@ static int xmp_setxattr(const char *path, const char *name, const char *value, s static int xmp_getxattr(const char *path, const char *name, char *value, size_t size) { + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } + if (get_namespace(name) != USER) { debug_print("Only user namespace is supported. name=%s\n", name); return -ENOTSUP; @@ -85,6 +94,10 @@ static int xmp_getxattr(const char *path, const char *name, char *value, size_t static int xmp_listxattr(const char *path, char *list, size_t size) { + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } + if (size > XATTR_LIST_MAX) { debug_print("The size of the list of attribute names for this file exceeds the system-imposed limit.\n"); return -E2BIG; @@ -100,6 +113,10 @@ static int xmp_listxattr(const char *path, char *list, size_t size) static int xmp_removexattr(const char *path, const char *name) { + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } + if (get_namespace(name) != USER) { debug_print("Only user namespace is supported. name=%s\n", name); return -ENOTSUP; @@ -151,20 +168,6 @@ static struct fuse_operations xmp_oper = { }; -enum { - KEY_HELP, - KEY_VERSION, -}; - - -static struct fuse_opt xattrs_opts[] = { - FUSE_OPT_KEY("-V", KEY_VERSION), - FUSE_OPT_KEY("--version", KEY_VERSION), - FUSE_OPT_KEY("-h", KEY_HELP), - FUSE_OPT_KEY("--help", KEY_HELP), - FUSE_OPT_END -}; - int is_directory(const char *path) { struct stat statbuf; if (stat(path, &statbuf) != 0) { @@ -222,6 +225,23 @@ const char *sanitized_source_directory(const char *path) { return absolute_path; } +enum { + KEY_HELP, + KEY_VERSION, +}; + +#define FUSE_XATTRS_OPT(t, p, v) { t, offsetof(struct xattrs_config, p), v } + +static struct fuse_opt xattrs_opts[] = { + FUSE_XATTRS_OPT("show_sidecar", show_sidecar, 1), + + FUSE_OPT_KEY("-V", KEY_VERSION), + FUSE_OPT_KEY("--version", KEY_VERSION), + FUSE_OPT_KEY("-h", KEY_HELP), + FUSE_OPT_KEY("--help", KEY_HELP), + FUSE_OPT_END +}; + static int xattrs_opt_proc(void *data, const char *arg, int key, struct fuse_args *outargs) { (void) data; @@ -243,6 +263,7 @@ static int xattrs_opt_proc(void *data, const char *arg, int key, " -V --version print version\n" "\n" "FUSE XATTRS options:\n" + " -o show_sidecar don't hide sidecar files\n" "\n", outargs->argv[0]); fuse_opt_add_arg(outargs, "-ho"); diff --git a/fuse_xattrs_config.h.in b/fuse_xattrs_config.h.in index bb27a56..68825ae 100644 --- a/fuse_xattrs_config.h.in +++ b/fuse_xattrs_config.h.in @@ -16,7 +16,4 @@ #define XATTR_SIZE_MAX @XATTR_SIZE_MAX@ #define XATTR_LIST_MAX @XATTR_LIST_MAX@ - - - #endif //CMAKE_FUSE_XATTRS_CONFIG_H diff --git a/passthrough.c b/passthrough.c index 9ef527a..4b1ab7e 100644 --- a/passthrough.c +++ b/passthrough.c @@ -22,14 +22,16 @@ #include #include -#include "fuse_xattrs_config.h" - #include "xattrs_config.h" #include "utils.h" int xmp_getattr(const char *path, struct stat *stbuf) { int res; + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } + char *_path = prepend_source_directory(xattrs_config.source_dir, path); res = lstat(_path, stbuf); free(_path); @@ -42,6 +44,9 @@ int xmp_getattr(const char *path, struct stat *stbuf) { int xmp_access(const char *path, int mask) { int res; + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } char *_path = prepend_source_directory(xattrs_config.source_dir, path); res = access(_path, mask); @@ -55,6 +60,9 @@ int xmp_access(const char *path, int mask) { int xmp_readlink(const char *path, char *buf, size_t size) { int res; + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } char *_path = prepend_source_directory(xattrs_config.source_dir, path); res = readlink(_path, buf, size - 1); @@ -84,6 +92,10 @@ int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler, return -errno; while ((de = readdir(dp)) != NULL) { + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(de->d_name) == 1) { + continue; + } + struct stat st; memset(&st, 0, sizeof(st)); st.st_ino = de->d_ino; @@ -98,6 +110,10 @@ int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler, int xmp_mknod(const char *path, mode_t mode, dev_t rdev) { int res; + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } + char *_path = prepend_source_directory(xattrs_config.source_dir, path); /* On Linux this could just be 'mknod(path, mode, rdev)' but this @@ -120,6 +136,9 @@ int xmp_mknod(const char *path, mode_t mode, dev_t rdev) { int xmp_mkdir(const char *path, mode_t mode) { int res; + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } char *_path = prepend_source_directory(xattrs_config.source_dir, path); res = mkdir(_path, mode); @@ -133,6 +152,9 @@ int xmp_mkdir(const char *path, mode_t mode) { int xmp_unlink(const char *path) { int res; + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } char *_path = prepend_source_directory(xattrs_config.source_dir, path); res = unlink(_path); @@ -146,6 +168,9 @@ int xmp_unlink(const char *path) { int xmp_rmdir(const char *path) { int res; + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } char *_path = prepend_source_directory(xattrs_config.source_dir, path); res = rmdir(_path); @@ -159,6 +184,11 @@ int xmp_rmdir(const char *path) { int xmp_symlink(const char *from, const char *to) { int res; + if (xattrs_config.show_sidecar == 0) { + if (filename_is_sidecar(from) == 1 || filename_is_sidecar(to)) { + return -ENOENT; + } + } char *_to = prepend_source_directory(xattrs_config.source_dir, to); res = symlink(from, _to); @@ -172,6 +202,11 @@ int xmp_symlink(const char *from, const char *to) { int xmp_rename(const char *from, const char *to) { int res; + if (xattrs_config.show_sidecar == 0) { + if (filename_is_sidecar(from) == 1 || filename_is_sidecar(to)) { + return -ENOENT; + } + } char *_from = prepend_source_directory(xattrs_config.source_dir, from); char *_to = prepend_source_directory(xattrs_config.source_dir, to); @@ -187,6 +222,11 @@ int xmp_rename(const char *from, const char *to) { int xmp_link(const char *from, const char *to) { int res; + if (xattrs_config.show_sidecar == 0) { + if (filename_is_sidecar(from) == 1 || filename_is_sidecar(to)) { + return -ENOENT; + } + } char *_from = prepend_source_directory(xattrs_config.source_dir, from); char *_to = prepend_source_directory(xattrs_config.source_dir, to); @@ -202,6 +242,9 @@ int xmp_link(const char *from, const char *to) { int xmp_chmod(const char *path, mode_t mode) { int res; + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } char *_path = prepend_source_directory(xattrs_config.source_dir, path); res = chmod(_path, mode); @@ -215,6 +258,9 @@ int xmp_chmod(const char *path, mode_t mode) { int xmp_chown(const char *path, uid_t uid, gid_t gid) { int res; + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } char *_path = prepend_source_directory(xattrs_config.source_dir, path); res = lchown(_path, uid, gid); @@ -228,6 +274,9 @@ int xmp_chown(const char *path, uid_t uid, gid_t gid) { int xmp_truncate(const char *path, off_t size) { int res; + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } char *_path = prepend_source_directory(xattrs_config.source_dir, path); res = truncate(_path, size); @@ -243,6 +292,10 @@ int xmp_truncate(const char *path, off_t size) { int xmp_utimens(const char *path, const struct timespec ts[2], struct fuse_file_info *fi) { + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } + (void) fi; int res; @@ -259,6 +312,9 @@ struct fuse_file_info *fi) int xmp_open(const char *path, struct fuse_file_info *fi) { int res; + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } char *_path = prepend_source_directory(xattrs_config.source_dir, path); res = open(_path, fi->flags); @@ -272,7 +328,12 @@ int xmp_open(const char *path, struct fuse_file_info *fi) { } int xmp_read(const char *path, char *buf, size_t size, off_t offset, - struct fuse_file_info *fi) { + struct fuse_file_info *fi) +{ + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } + int fd; int res; @@ -293,7 +354,12 @@ int xmp_read(const char *path, char *buf, size_t size, off_t offset, } int xmp_write(const char *path, const char *buf, size_t size, - off_t offset, struct fuse_file_info *fi) { + off_t offset, struct fuse_file_info *fi) +{ + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } + int fd; int res; @@ -315,6 +381,9 @@ int xmp_write(const char *path, const char *buf, size_t size, int xmp_statfs(const char *path, struct statvfs *stbuf) { int res; + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } char *_path = prepend_source_directory(xattrs_config.source_dir, path); res = statvfs(_path, stbuf); @@ -350,6 +419,10 @@ int xmp_fsync(const char *path, int isdatasync, int xmp_fallocate(const char *path, int mode, off_t offset, off_t length, struct fuse_file_info *fi) { + if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) { + return -ENOENT; + } + int fd; int res; diff --git a/utils.c b/utils.c index dbcaa29..10593e6 100644 --- a/utils.c +++ b/utils.c @@ -44,6 +44,24 @@ char *sanitize_value(const char *value, size_t value_size) return sanitized; } + +const size_t BINARY_SIDECAR_EXT_SIZE = strlen(BINARY_SIDECAR_EXT); + +const int filename_is_sidecar(const char *string) { + if(string == NULL) + return 0; + + size_t size = strlen(string); + if (size <= BINARY_SIDECAR_EXT_SIZE) + return 0; + + if (memcmp(string+size-BINARY_SIDECAR_EXT_SIZE, BINARY_SIDECAR_EXT, BINARY_SIDECAR_EXT_SIZE) == 0) { + return 1; + } + + return 0; +} + enum namespace get_namespace(const char *name) { if (strncmp(name, "user.", strlen("user.")) == 0) { return USER; diff --git a/utils.h b/utils.h index 2a592e9..b3b899b 100644 --- a/utils.h +++ b/utils.h @@ -40,4 +40,7 @@ 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); +const size_t BINARY_SIDECAR_EXT_SIZE; +const int filename_is_sidecar(const char *string); + #endif //FUSE_XATTRS_UTILS_H diff --git a/xattrs_config.h b/xattrs_config.h index b75f766..3803902 100644 --- a/xattrs_config.h +++ b/xattrs_config.h @@ -11,6 +11,7 @@ #define FUSE_XATTRS_CONFIG_H struct xattrs_config { + int show_sidecar; const char *source_dir; } xattrs_config; -- 2.39.2