From 5c2c6d3286e691f8a5b61bc2d83b9cc07bb18e9d Mon Sep 17 00:00:00 2001 From: Felipe Barriga Richards Date: Wed, 15 Feb 2017 22:29:34 -0300 Subject: [PATCH] features: Now it will add xattrs support to an specific directory (instead of the full root). --- CMakeLists.txt | 2 +- README.md | 2 +- TODO.md | 1 - fuse_xattrs.c | 165 +++++++++++++++++++++++++++++++++++++++---- fuse_xattrs_config.h | 12 ++++ passthrough.c | 111 +++++++++++++++++++++++------ run_tests.sh | 4 +- utils.c | 7 ++ utils.h | 1 + 9 files changed, 267 insertions(+), 38 deletions(-) create mode 100644 fuse_xattrs_config.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1343d3f..4b4f27f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,7 +45,7 @@ set(SOURCE_FILES utils.c utils.h const.h -) + fuse_xattrs_config.h) add_executable(fuse_xattrs ${SOURCE_FILES}) diff --git a/README.md b/README.md index f24f018..98866e3 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ http://github.com/fbarriga/fuse_xattrs Once fuse_xattrs is installed (see next section) running it is very simple: - fuse_xattrs mountpoint + fuse_xattrs source_directory mountpoint To unmount the filesystem: diff --git a/TODO.md b/TODO.md index b951fb0..13be7cb 100644 --- a/TODO.md +++ b/TODO.md @@ -20,7 +20,6 @@ BUGS FEATURES -------- -- Add option to mirror a specific directory instead of / - binary_storage: - crc32 - make it endian-independent: diff --git a/fuse_xattrs.c b/fuse_xattrs.c index b94182e..84007f7 100644 --- a/fuse_xattrs.c +++ b/fuse_xattrs.c @@ -14,15 +14,20 @@ /* For pread()/pwrite()/utimensat() */ #define _XOPEN_SOURCE 700 +/* For get_current_dir_name */ +#define _GNU_SOURCE + #include #include #include +#include #include #include #include "utils.h" #include "passthrough.h" +#include "fuse_xattrs_config.h" #include "binary_storage.h" #include "const.h" @@ -42,14 +47,20 @@ 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); + #ifdef DEBUG char *sanitized_value = sanitize_value(value, size); debug_print("path=%s name=%s value=%s size=%zu XATTR_CREATE=%d XATTR_REPLACE=%d\n", - path, name, sanitized_value, size, flags & XATTR_CREATE, flags & XATTR_REPLACE); + _path, name, sanitized_value, size, flags & XATTR_CREATE, flags & XATTR_REPLACE); free(sanitized_value); #endif - return binary_storage_write_key(path, name, value, size, flags); + + int rtval = binary_storage_write_key(_path, name, value, size, flags); + free(_path); + + return rtval; } static int xmp_getxattr(const char *path, const char *name, char *value, size_t size) @@ -63,8 +74,12 @@ static int xmp_getxattr(const char *path, const char *name, char *value, size_t return -ERANGE; } - debug_print("path=%s name=%s size=%zu\n", path, name, size); - return binary_storage_read_key(path, name, value, size); + char *_path = prepend_source_directory(xattrs_config.source_dir, 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); + + return rtval; } static int xmp_listxattr(const char *path, char *list, size_t size) @@ -74,8 +89,12 @@ static int xmp_listxattr(const char *path, char *list, size_t size) return -E2BIG; } - debug_print("path=%s size=%zu\n", path, size); - return binary_storage_list_keys(path, list, size); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + debug_print("path=%s size=%zu\n", _path, size); + int rtval = binary_storage_list_keys(_path, list, size); + free(_path); + + return rtval; } static int xmp_removexattr(const char *path, const char *name) @@ -89,8 +108,12 @@ static int xmp_removexattr(const char *path, const char *name) return -ERANGE; } - debug_print("path=%s name=%s\n", path, name); - return binary_storage_remove_key(path, name); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + debug_print("path=%s name=%s\n", _path, name); + int rtval = binary_storage_remove_key(_path, name); + free(_path); + + return rtval; } static struct fuse_operations xmp_oper = { @@ -126,9 +149,127 @@ static struct fuse_operations xmp_oper = { .removexattr = xmp_removexattr, }; -int main(int argc, char *argv[]) -{ - // TODO: parse options... + +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) { + 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; +} + +/** + * Check if the path is valid. If it's a relative path, + * prepend the working path. + * @param path relative or absolute path to eval. + * @return new string with absolute path + */ +const char *sanitized_source_directory(const char *path) { + char *absolute_path; + if (strlen(path) == 0) { + return NULL; + } + + /* absolute path, we don't do anything */ + if (path[0] == '/') { + if (is_directory(path) == -1) { + return NULL; + } + absolute_path = strdup(path); + return absolute_path; + } + + char *pwd = get_current_dir_name(); + size_t len = strlen(pwd) + 1 + strlen(path) + 1; + int has_trailing_backslash = (path[strlen(path)-1] == '/'); + if (!has_trailing_backslash) + len++; + + absolute_path = (char*) malloc(sizeof(char) * len); + memset(absolute_path, '\0', len); + sprintf(absolute_path, "%s/%s", pwd, path); + + if(!has_trailing_backslash) + absolute_path[len-2] = '/'; + + if (is_directory(absolute_path) == -1) { + free(absolute_path); + return NULL; + } + + return absolute_path; +} + +static int xattrs_opt_proc(void *data, const char *arg, int key, + struct fuse_args *outargs) { + (void) data; + switch (key) { + case FUSE_OPT_KEY_NONOPT: + if (!xattrs_config.source_dir) { + xattrs_config.source_dir = sanitized_source_directory(arg); + return 0; + } + break; + + case KEY_HELP: + fprintf(stderr, + "usage: %s source_dir mountpoint [options]\n" + "\n" + "general options:\n" + " -o opt,[opt...] mount options\n" + " -h --help print help\n" + " -V --version print version\n" + "\n" + "FUSE XATTRS options:\n" + "\n", outargs->argv[0]); + + fuse_opt_add_arg(outargs, "-ho"); + fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL); + exit(1); + + case KEY_VERSION: + fuse_opt_add_arg(outargs, "--version"); + fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL); + exit(0); + } + return 1; +} + + + +int main(int argc, char *argv[]) { + struct fuse_args args = FUSE_ARGS_INIT(argc, argv); + if (fuse_opt_parse(&args, &xattrs_config, xattrs_opts, xattrs_opt_proc) == -1) { + exit(1); + } + + if (!xattrs_config.source_dir) { + fprintf(stderr, "missing source directory\n"); + fprintf(stderr, "see `%s -h' for usage\n", argv[0]); + exit(1); + } + umask(0); - return fuse_main(argc, argv, &xmp_oper, NULL); + return fuse_main(args.argc, args.argv, &xmp_oper, NULL); } diff --git a/fuse_xattrs_config.h b/fuse_xattrs_config.h new file mode 100644 index 0000000..e504b01 --- /dev/null +++ b/fuse_xattrs_config.h @@ -0,0 +1,12 @@ +// +// Copyright (C) 2017 Felipe Barriga Richards +// + +#ifndef FUSE_XATTRS_FUSE_XATTRS_CONFIG_H +#define FUSE_XATTRS_FUSE_XATTRS_CONFIG_H + +struct xattrs_config { + const char *source_dir; +} xattrs_config; + +#endif //FUSE_XATTRS_FUSE_XATTRS_CONFIG_H diff --git a/passthrough.c b/passthrough.c index 97c178e..9505883 100644 --- a/passthrough.c +++ b/passthrough.c @@ -17,14 +17,21 @@ #include #include #include +#include #include #include #include +#include "fuse_xattrs_config.h" +#include "utils.h" + int xmp_getattr(const char *path, struct stat *stbuf) { int res; - res = lstat(path, stbuf); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + res = lstat(_path, stbuf); + free(_path); + if (res == -1) return -errno; @@ -34,7 +41,10 @@ int xmp_getattr(const char *path, struct stat *stbuf) { int xmp_access(const char *path, int mask) { int res; - res = access(path, mask); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + res = access(_path, mask); + free(_path); + if (res == -1) return -errno; @@ -44,7 +54,10 @@ int xmp_access(const char *path, int mask) { int xmp_readlink(const char *path, char *buf, size_t size) { int res; - res = readlink(path, buf, size - 1); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + res = readlink(_path, buf, size - 1); + free(_path); + if (res == -1) return -errno; @@ -61,7 +74,10 @@ int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler, (void) offset; (void) fi; - dp = opendir(path); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + dp = opendir(_path); + free(_path); + if (dp == NULL) return -errno; @@ -80,17 +96,20 @@ 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; + char *_path = prepend_source_directory(xattrs_config.source_dir, path); /* On Linux this could just be 'mknod(path, mode, rdev)' but this is more portable */ if (S_ISREG(mode)) { - res = open(path, O_CREAT | O_EXCL | O_WRONLY, mode); + res = open(_path, O_CREAT | O_EXCL | O_WRONLY, mode); if (res >= 0) res = close(res); } else if (S_ISFIFO(mode)) - res = mkfifo(path, mode); + res = mkfifo(_path, mode); else - res = mknod(path, mode, rdev); + res = mknod(_path, mode, rdev); + + free(_path); if (res == -1) return -errno; @@ -100,7 +119,10 @@ int xmp_mknod(const char *path, mode_t mode, dev_t rdev) { int xmp_mkdir(const char *path, mode_t mode) { int res; - res = mkdir(path, mode); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + res = mkdir(_path, mode); + free(_path); + if (res == -1) return -errno; @@ -110,7 +132,10 @@ int xmp_mkdir(const char *path, mode_t mode) { int xmp_unlink(const char *path) { int res; - res = unlink(path); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + res = unlink(_path); + free(_path); + if (res == -1) return -errno; @@ -120,7 +145,10 @@ int xmp_unlink(const char *path) { int xmp_rmdir(const char *path) { int res; - res = rmdir(path); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + res = rmdir(_path); + free(_path); + if (res == -1) return -errno; @@ -130,7 +158,10 @@ int xmp_rmdir(const char *path) { int xmp_symlink(const char *from, const char *to) { int res; - res = symlink(from, to); + char *_to = prepend_source_directory(xattrs_config.source_dir, to); + res = symlink(from, _to); + free(_to); + if (res == -1) return -errno; @@ -140,7 +171,12 @@ int xmp_symlink(const char *from, const char *to) { int xmp_rename(const char *from, const char *to) { int res; - res = rename(from, to); + char *_from = prepend_source_directory(xattrs_config.source_dir, from); + char *_to = prepend_source_directory(xattrs_config.source_dir, to); + res = rename(_from, _to); + free(_from); + free(_to); + if (res == -1) return -errno; @@ -150,7 +186,12 @@ int xmp_rename(const char *from, const char *to) { int xmp_link(const char *from, const char *to) { int res; - res = link(from, to); + char *_from = prepend_source_directory(xattrs_config.source_dir, from); + char *_to = prepend_source_directory(xattrs_config.source_dir, to); + res = link(_from, _to); + free(_from); + free(_to); + if (res == -1) return -errno; @@ -160,7 +201,10 @@ int xmp_link(const char *from, const char *to) { int xmp_chmod(const char *path, mode_t mode) { int res; - res = chmod(path, mode); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + res = chmod(_path, mode); + free(_path); + if (res == -1) return -errno; @@ -170,7 +214,10 @@ int xmp_chmod(const char *path, mode_t mode) { int xmp_chown(const char *path, uid_t uid, gid_t gid) { int res; - res = lchown(path, uid, gid); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + res = lchown(_path, uid, gid); + free(_path); + if (res == -1) return -errno; @@ -180,7 +227,10 @@ int xmp_chown(const char *path, uid_t uid, gid_t gid) { int xmp_truncate(const char *path, off_t size) { int res; - res = truncate(path, size); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + res = truncate(_path, size); + free(_path); + if (res == -1) return -errno; @@ -194,8 +244,10 @@ struct fuse_file_info *fi) (void) fi; int res; + char *_path = prepend_source_directory(xattrs_config.source_dir, path); /* don't use utime/utimes since they follow symlinks */ - res = utimensat(0, path, ts, AT_SYMLINK_NOFOLLOW); + res = utimensat(0, _path, ts, AT_SYMLINK_NOFOLLOW); + free(_path); if (res == -1) return -errno; @@ -206,7 +258,10 @@ struct fuse_file_info *fi) int xmp_open(const char *path, struct fuse_file_info *fi) { int res; - res = open(path, fi->flags); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + res = open(_path, fi->flags); + free(_path); + if (res == -1) return -errno; @@ -220,7 +275,10 @@ int xmp_read(const char *path, char *buf, size_t size, off_t offset, int res; (void) fi; - fd = open(path, O_RDONLY); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + fd = open(_path, O_RDONLY); + free(_path); + if (fd == -1) return -errno; @@ -238,7 +296,10 @@ int xmp_write(const char *path, const char *buf, size_t size, int res; (void) fi; - fd = open(path, O_WRONLY); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + fd = open(_path, O_WRONLY); + free(_path); + if (fd == -1) return -errno; @@ -253,7 +314,10 @@ int xmp_write(const char *path, const char *buf, size_t size, int xmp_statfs(const char *path, struct statvfs *stbuf) { int res; - res = statvfs(path, stbuf); + char *_path = prepend_source_directory(xattrs_config.source_dir, path); + res = statvfs(_path, stbuf); + free(_path); + if (res == -1) return -errno; @@ -292,7 +356,10 @@ int xmp_fallocate(const char *path, int mode, if (mode) return -EOPNOTSUPP; - fd = open(path, O_WRONLY); + char *_path = concat(xattrs_config.source_dir, path); + fd = open(_path, O_WRONLY); + free(_path); + if (fd == -1) return -errno; diff --git a/run_tests.sh b/run_tests.sh index a260b52..a3dcb36 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -1,7 +1,8 @@ #!/usr/bin/env bash mkdir -p test/mount -./fuse_xattrs -o nonempty test/mount/ +mkdir -p test/source +./fuse_xattrs -o nonempty test/source/ test/mount/ pushd test @@ -13,6 +14,7 @@ set -e popd fusermount -zu test/mount +rm -d test/source rm -d test/mount exit ${RESULT} diff --git a/utils.c b/utils.c index ccb84b5..1a05447 100644 --- a/utils.c +++ b/utils.c @@ -14,6 +14,13 @@ #include "utils.h" #include "const.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); + + return dst; +} char *get_sidecar_path(const char *path) { diff --git a/utils.h b/utils.h index 6f8d0d9..2a592e9 100644 --- a/utils.h +++ b/utils.h @@ -38,5 +38,6 @@ 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); #endif //FUSE_XATTRS_UTILS_H -- 2.39.2