features: Now it will add xattrs support to an specific directory (instead of the...
authorFelipe Barriga Richards <spam@felipebarriga.cl>
Thu, 16 Feb 2017 01:29:34 +0000 (22:29 -0300)
committerFelipe Barriga Richards <spam@felipebarriga.cl>
Thu, 16 Feb 2017 17:10:22 +0000 (14:10 -0300)
CMakeLists.txt
README.md
TODO.md
fuse_xattrs.c
fuse_xattrs_config.h [new file with mode: 0644]
passthrough.c
run_tests.sh
utils.c
utils.h

index 1343d3ff514a9dc9c981888274ee4e8304f847cf..4b4f27fb78b51aa135162de3e6348bc84557e034 100644 (file)
@@ -45,7 +45,7 @@ set(SOURCE_FILES
         utils.c
         utils.h
         const.h
-)
+        fuse_xattrs_config.h)
 
 add_executable(fuse_xattrs ${SOURCE_FILES})
 
index f24f0185b7fd1b485a0a10e72d6e60b844f63d5e..98866e3b3fd3769ca14ac662cf0d43ec5b028cb7 100644 (file)
--- 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 b951fb0b82af2bc79cde79de86197a8b50c5e8d8..13be7cb459b4da58f1d91d797a8036d4eb73e53b 100644 (file)
--- 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:
index b94182ef836136d65e15259a3a7b8bf5db817189..84007f7fe7990482c012a89f11d9172d19f6ab2c 100644 (file)
 /* For pread()/pwrite()/utimensat() */
 #define _XOPEN_SOURCE 700
 
+/* For get_current_dir_name */
+#define _GNU_SOURCE
+
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include <fuse.h>
 #include <sys/xattr.h>
 
 #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 (file)
index 0000000..e504b01
--- /dev/null
@@ -0,0 +1,12 @@
+//
+// Copyright (C) 2017  Felipe Barriga Richards <felipe {at} felipebarriga.cl>
+//
+
+#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
index 97c178e3a7d80c05579397d396de536a3c447dce..95058833a371376d61204cd234965f87c3a8d5cd 100644 (file)
 #include <fuse.h>
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 #include <unistd.h>
 #include <dirent.h>
 #include <errno.h>
 
+#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;
 
index a260b52fdc18019a7ba9b44b5ca42104b66f293e..a3dcb365972f9e1ea686dae32d21ce07e5bc0716 100755 (executable)
@@ -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 ccb84b58f71357e68f518b88fe29d0b29c43af60..1a05447a43f7e01211dec229a9cbf343a6e25958 100644 (file)
--- a/utils.c
+++ b/utils.c
 #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 6f8d0d9ebee7a53f8ceda279c3f63347bef6086b..2a592e9956010a6adb561d612c69e8b7bdf0bbb5 100644 (file)
--- 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