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
25 #include <osxfuse/fuse.h>
30 #include <sys/xattr.h>
31 #include <sys/param.h>
33 #include "fuse_xattrs_config.h"
35 #include "xattrs_config.h"
37 #include "passthrough.h"
39 #include "binary_storage.h"
41 static int xmp_setxattr(const char *path, const char *name, const char *value, size_t size, int flags)
43 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
47 if (xattrs_config.enable_namespaces == 1 && get_namespace(name) != USER) {
48 debug_print("Only user namespace is supported. name=%s\n", name);
51 if (strlen(name) > XATTR_NAME_MAX) {
52 debug_print("attribute name must be equal or smaller than %d bytes\n", XATTR_NAME_MAX);
55 if (size > XATTR_SIZE_MAX) {
56 debug_print("attribute value cannot be bigger than %d bytes\n", XATTR_SIZE_MAX);
60 char *_path = prepend_source_directory(path);
63 char *sanitized_value = sanitize_value(value, size);
64 debug_print("path=%s name=%s value=%s size=%zu XATTR_CREATE=%d XATTR_REPLACE=%d\n",
65 _path, name, sanitized_value, size, flags & XATTR_CREATE, flags & XATTR_REPLACE);
67 free(sanitized_value);
70 int rtval = binary_storage_write_key(_path, name, value, size, flags);
76 static int xmp_getxattr(const char *path, const char *name, char *value, size_t size)
78 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
82 if (xattrs_config.enable_namespaces == 1 && get_namespace(name) != USER) {
83 debug_print("Only user namespace is supported. name=%s\n", name);
86 if (strlen(name) > XATTR_NAME_MAX) {
87 debug_print("attribute name must be equal or smaller than %d bytes\n", XATTR_NAME_MAX);
91 char *_path = prepend_source_directory(path);
92 debug_print("path=%s name=%s size=%zu\n", _path, name, size);
93 int rtval = binary_storage_read_key(_path, name, value, size);
100 static int xmp_setxattr_apple(const char *path, const char *name, const char *value, size_t size, int flags, u_int32_t position) {
101 assert(position == 0);
102 return xmp_setxattr(path, name, value, size, flags);
105 static int xmp_getxattr_apple(const char *path, const char *name, char *value, size_t size, u_int32_t position) {
106 assert(position == 0);
107 return xmp_getxattr(path, name, value, size);
111 static int xmp_listxattr(const char *path, char *list, size_t size)
113 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
117 if (size > XATTR_LIST_MAX) {
118 debug_print("The size of the list of attribute names for this file exceeds the system-imposed limit.\n");
122 char *_path = prepend_source_directory(path);
123 debug_print("path=%s size=%zu\n", _path, size);
124 int rtval = binary_storage_list_keys(_path, list, size);
130 static int xmp_removexattr(const char *path, const char *name)
132 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
136 if (xattrs_config.enable_namespaces == 1 && get_namespace(name) != USER) {
137 debug_print("Only user namespace is supported. name=%s\n", name);
140 if (strlen(name) > XATTR_NAME_MAX) {
141 debug_print("attribute name must be equal or smaller than %d bytes\n", XATTR_NAME_MAX);
145 char *_path = prepend_source_directory(path);
146 debug_print("path=%s name=%s\n", _path, name);
147 int rtval = binary_storage_remove_key(_path, name);
153 static struct fuse_operations xmp_oper = {
154 .getattr = xmp_getattr,
155 .access = xmp_access,
156 .readlink = xmp_readlink,
157 .readdir = xmp_readdir,
160 .symlink = xmp_symlink,
161 .unlink = xmp_unlink,
163 .rename = xmp_rename,
167 .truncate = xmp_truncate,
168 #ifdef HAVE_UTIMENSAT
169 .utimens = xmp_utimens,
174 .statfs = xmp_statfs,
175 .release = xmp_release,
177 #ifdef HAVE_POSIX_FALLOCATE
178 .fallocate = xmp_fallocate,
181 .setxattr = xmp_setxattr_apple,
182 .getxattr = xmp_getxattr_apple,
184 .setxattr = xmp_setxattr,
185 .getxattr = xmp_getxattr,
187 .listxattr = xmp_listxattr,
188 .removexattr = xmp_removexattr,
192 * Check if the path is valid. If it's a relative path,
193 * prepend the working path.
194 * @param path relative or absolute path to eval.
195 * @return new string with absolute path
197 const char *sanitized_source_directory(const char *path) {
199 if (strlen(path) == 0) {
203 /* absolute path, we don't do anything */
204 if (path[0] == '/') {
205 if (is_directory(path) == -1) {
208 absolute_path = strdup(path);
209 return absolute_path;
212 static char cwd[MAXPATHLEN];
213 char *pwd = getcwd(cwd, sizeof(cwd));
214 size_t len = strlen(pwd) + 1 + strlen(path) + 1;
215 int has_trailing_backslash = (path[strlen(path)-1] == '/');
216 if (!has_trailing_backslash)
219 absolute_path = (char*) malloc(sizeof(char) * len);
220 memset(absolute_path, '\0', len);
221 sprintf(absolute_path, "%s/%s", pwd, path);
223 if(!has_trailing_backslash)
224 absolute_path[len-2] = '/';
226 if (is_directory(absolute_path) == -1) {
231 return absolute_path;
239 #define FUSE_XATTRS_OPT(t, p, v) { t, offsetof(struct xattrs_config, p), v }
241 static struct fuse_opt xattrs_opts[] = {
242 FUSE_XATTRS_OPT("show_sidecar", show_sidecar, 1),
243 FUSE_XATTRS_OPT("enable_namespaces", enable_namespaces, 1),
245 FUSE_OPT_KEY("-V", KEY_VERSION),
246 FUSE_OPT_KEY("--version", KEY_VERSION),
247 FUSE_OPT_KEY("-h", KEY_HELP),
248 FUSE_OPT_KEY("--help", KEY_HELP),
252 static int xattrs_opt_proc(void *data, const char *arg, int key,
253 struct fuse_args *outargs) {
256 case FUSE_OPT_KEY_NONOPT:
257 if (!xattrs_config.source_dir) {
258 xattrs_config.source_dir = sanitized_source_directory(arg);
259 xattrs_config.source_dir_size = strlen(xattrs_config.source_dir);
266 "usage: %s source_dir mountpoint [options]\n"
269 " -o opt,[opt...] mount options\n"
270 " -h --help print help\n"
271 " -V --version print version\n"
273 "FUSE XATTRS options:\n"
274 " -o show_sidecar don't hide sidecar files\n"
275 " -o enable_namespaces enable namespaces checks\n"
276 "\n", outargs->argv[0]);
278 fuse_opt_add_arg(outargs, "-ho");
279 fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL);
283 printf("FUSE_XATTRS version %d.%d\n", FUSE_XATTRS_VERSION_MAJOR, FUSE_XATTRS_VERSION_MINOR);
284 fuse_opt_add_arg(outargs, "--version");
285 fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL);
293 int main(int argc, char *argv[]) {
294 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
295 if (fuse_opt_parse(&args, &xattrs_config, xattrs_opts, xattrs_opt_proc) == -1) {
299 if (!xattrs_config.source_dir) {
300 fprintf(stderr, "missing source directory\n");
301 fprintf(stderr, "see `%s -h' for usage\n", argv[0]);
307 // disable multi-threading
308 fuse_opt_add_arg(&args, "-s");
309 return fuse_main(args.argc, args.argv, &xmp_oper, NULL);