utils.c
utils.h
const.h
-)
+ fuse_xattrs_config.h)
add_executable(fuse_xattrs ${SOURCE_FILES})
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:
FEATURES
--------
-- Add option to mirror a specific directory instead of /
- binary_storage:
- crc32
- make it endian-independent:
/* 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"
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)
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)
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)
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 = {
.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);
}
--- /dev/null
+//
+// 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
#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;
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;
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;
(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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
(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;
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;
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;
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;
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;
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;
#!/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
popd
fusermount -zu test/mount
+rm -d test/source
rm -d test/mount
exit ${RESULT}
#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)
{
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