2 fuse_xattrs - Add xattrs support using sidecar files
4 Copyright (C) 2016-2017 Felipe Barriga Richards <felipe {at} felipebarriga.cl>
6 Based on passthrough.c (libfuse example)
8 This program can be distributed under the terms of the GNU GPL.
12 #define FUSE_USE_VERSION 30
14 /* For pread()/pwrite()/utimensat() */
15 #define _XOPEN_SOURCE 700
17 /* For get_current_dir_name */
27 #include <sys/xattr.h>
29 #include "fuse_xattrs_config.h"
31 #include "xattrs_config.h"
33 #include "passthrough.h"
35 #include "binary_storage.h"
37 static int xmp_setxattr(const char *path, const char *name, const char *value, size_t size, int flags)
39 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
43 if (get_namespace(name) != USER) {
44 debug_print("Only user namespace is supported. name=%s\n", name);
47 if (strlen(name) > XATTR_NAME_MAX) {
48 debug_print("attribute name must be equal or smaller than %d bytes\n", XATTR_NAME_MAX);
51 if (size > XATTR_SIZE_MAX) {
52 debug_print("attribute value cannot be bigger than %d bytes\n", XATTR_SIZE_MAX);
56 char *_path = prepend_source_directory(path);
59 char *sanitized_value = sanitize_value(value, size);
60 debug_print("path=%s name=%s value=%s size=%zu XATTR_CREATE=%d XATTR_REPLACE=%d\n",
61 _path, name, sanitized_value, size, flags & XATTR_CREATE, flags & XATTR_REPLACE);
63 free(sanitized_value);
66 int rtval = binary_storage_write_key(_path, name, value, size, flags);
72 static int xmp_getxattr(const char *path, const char *name, char *value, size_t size)
74 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
78 if (get_namespace(name) != USER) {
79 debug_print("Only user namespace is supported. name=%s\n", name);
82 if (strlen(name) > XATTR_NAME_MAX) {
83 debug_print("attribute name must be equal or smaller than %d bytes\n", XATTR_NAME_MAX);
87 char *_path = prepend_source_directory(path);
88 debug_print("path=%s name=%s size=%zu\n", _path, name, size);
89 int rtval = binary_storage_read_key(_path, name, value, size);
95 static int xmp_listxattr(const char *path, char *list, size_t size)
97 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
101 if (size > XATTR_LIST_MAX) {
102 debug_print("The size of the list of attribute names for this file exceeds the system-imposed limit.\n");
106 char *_path = prepend_source_directory(path);
107 debug_print("path=%s size=%zu\n", _path, size);
108 int rtval = binary_storage_list_keys(_path, list, size);
114 static int xmp_removexattr(const char *path, const char *name)
116 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
120 if (get_namespace(name) != USER) {
121 debug_print("Only user namespace is supported. name=%s\n", name);
124 if (strlen(name) > XATTR_NAME_MAX) {
125 debug_print("attribute name must be equal or smaller than %d bytes\n", XATTR_NAME_MAX);
129 char *_path = prepend_source_directory(path);
130 debug_print("path=%s name=%s\n", _path, name);
131 int rtval = binary_storage_remove_key(_path, name);
137 static struct fuse_operations xmp_oper = {
138 .getattr = xmp_getattr,
139 .access = xmp_access,
140 .readlink = xmp_readlink,
141 .readdir = xmp_readdir,
144 .symlink = xmp_symlink,
145 .unlink = xmp_unlink,
147 .rename = xmp_rename,
151 .truncate = xmp_truncate,
152 #ifdef HAVE_UTIMENSAT
153 .utimens = xmp_utimens,
158 .statfs = xmp_statfs,
159 .release = xmp_release,
161 #ifdef HAVE_POSIX_FALLOCATE
162 .fallocate = xmp_fallocate,
164 .setxattr = xmp_setxattr,
165 .getxattr = xmp_getxattr,
166 .listxattr = xmp_listxattr,
167 .removexattr = xmp_removexattr,
171 * Check if the path is valid. If it's a relative path,
172 * prepend the working path.
173 * @param path relative or absolute path to eval.
174 * @return new string with absolute path
176 const char *sanitized_source_directory(const char *path) {
178 if (strlen(path) == 0) {
182 /* absolute path, we don't do anything */
183 if (path[0] == '/') {
184 if (is_directory(path) == -1) {
187 absolute_path = strdup(path);
188 return absolute_path;
191 char *pwd = get_current_dir_name();
192 size_t len = strlen(pwd) + 1 + strlen(path) + 1;
193 int has_trailing_backslash = (path[strlen(path)-1] == '/');
194 if (!has_trailing_backslash)
197 absolute_path = (char*) malloc(sizeof(char) * len);
198 memset(absolute_path, '\0', len);
199 sprintf(absolute_path, "%s/%s", pwd, path);
201 if(!has_trailing_backslash)
202 absolute_path[len-2] = '/';
204 if (is_directory(absolute_path) == -1) {
209 return absolute_path;
217 #define FUSE_XATTRS_OPT(t, p, v) { t, offsetof(struct xattrs_config, p), v }
219 static struct fuse_opt xattrs_opts[] = {
220 FUSE_XATTRS_OPT("show_sidecar", show_sidecar, 1),
222 FUSE_OPT_KEY("-V", KEY_VERSION),
223 FUSE_OPT_KEY("--version", KEY_VERSION),
224 FUSE_OPT_KEY("-h", KEY_HELP),
225 FUSE_OPT_KEY("--help", KEY_HELP),
229 static int xattrs_opt_proc(void *data, const char *arg, int key,
230 struct fuse_args *outargs) {
233 case FUSE_OPT_KEY_NONOPT:
234 if (!xattrs_config.source_dir) {
235 xattrs_config.source_dir = sanitized_source_directory(arg);
236 xattrs_config.source_dir_size = strlen(xattrs_config.source_dir);
243 "usage: %s source_dir mountpoint [options]\n"
246 " -o opt,[opt...] mount options\n"
247 " -h --help print help\n"
248 " -V --version print version\n"
250 "FUSE XATTRS options:\n"
251 " -o show_sidecar don't hide sidecar files\n"
252 "\n", outargs->argv[0]);
254 fuse_opt_add_arg(outargs, "-ho");
255 fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL);
259 printf("FUSE_XATTRS version %d.%d\n", FUSE_XATTRS_VERSION_MAJOR, FUSE_XATTRS_VERSION_MINOR);
260 fuse_opt_add_arg(outargs, "--version");
261 fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL);
269 int main(int argc, char *argv[]) {
270 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
271 if (fuse_opt_parse(&args, &xattrs_config, xattrs_opts, xattrs_opt_proc) == -1) {
275 if (!xattrs_config.source_dir) {
276 fprintf(stderr, "missing source directory\n");
277 fprintf(stderr, "see `%s -h' for usage\n", argv[0]);
283 // disable multi-threading
284 fuse_opt_add_arg(&args, "-s");
285 return fuse_main(args.argc, args.argv, &xmp_oper, NULL);