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 struct xattrs_config xattrs_config;
43 static int xmp_setxattr(const char *path, const char *name, const char *value, size_t size, int flags)
45 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
49 if (xattrs_config.enable_namespaces == 1 && get_namespace(name) != USER) {
50 debug_print("Only user namespace is supported. name=%s\n", name);
53 if (strlen(name) > XATTR_NAME_MAX) {
54 debug_print("attribute name must be equal or smaller than %d bytes\n", XATTR_NAME_MAX);
57 if (size > XATTR_SIZE_MAX) {
58 debug_print("attribute value cannot be bigger than %d bytes\n", XATTR_SIZE_MAX);
62 char *_path = prepend_source_directory(path);
65 char *sanitized_value = sanitize_value(value, size);
66 debug_print("path=%s name=%s value=%s size=%zu XATTR_CREATE=%d XATTR_REPLACE=%d\n",
67 _path, name, sanitized_value, size, flags & XATTR_CREATE, flags & XATTR_REPLACE);
69 free(sanitized_value);
72 int rtval = binary_storage_write_key(_path, name, value, size, flags);
78 static int xmp_getxattr(const char *path, const char *name, char *value, size_t size)
80 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
84 if (xattrs_config.enable_namespaces == 1 && get_namespace(name) != USER) {
85 debug_print("Only user namespace is supported. name=%s\n", name);
88 if (strlen(name) > XATTR_NAME_MAX) {
89 debug_print("attribute name must be equal or smaller than %d bytes\n", XATTR_NAME_MAX);
93 char *_path = prepend_source_directory(path);
94 debug_print("path=%s name=%s size=%zu\n", _path, name, size);
95 int rtval = binary_storage_read_key(_path, name, value, size);
102 static int xmp_setxattr_apple(const char *path, const char *name, const char *value, size_t size, int flags, u_int32_t position) {
103 assert(position == 0);
104 return xmp_setxattr(path, name, value, size, flags);
107 static int xmp_getxattr_apple(const char *path, const char *name, char *value, size_t size, u_int32_t position) {
108 assert(position == 0);
109 return xmp_getxattr(path, name, value, size);
113 static int xmp_listxattr(const char *path, char *list, size_t size)
115 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
119 if (size > XATTR_LIST_MAX) {
120 debug_print("The size of the list of attribute names for this file exceeds the system-imposed limit.\n");
124 char *_path = prepend_source_directory(path);
125 debug_print("path=%s size=%zu\n", _path, size);
126 int rtval = binary_storage_list_keys(_path, list, size);
132 static int xmp_removexattr(const char *path, const char *name)
134 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
138 if (xattrs_config.enable_namespaces == 1 && get_namespace(name) != USER) {
139 debug_print("Only user namespace is supported. name=%s\n", name);
142 if (strlen(name) > XATTR_NAME_MAX) {
143 debug_print("attribute name must be equal or smaller than %d bytes\n", XATTR_NAME_MAX);
147 char *_path = prepend_source_directory(path);
148 debug_print("path=%s name=%s\n", _path, name);
149 int rtval = binary_storage_remove_key(_path, name);
155 static struct fuse_operations xmp_oper = {
156 .getattr = xmp_getattr,
157 .access = xmp_access,
158 .readlink = xmp_readlink,
159 .readdir = xmp_readdir,
162 .symlink = xmp_symlink,
163 .unlink = xmp_unlink,
165 .rename = xmp_rename,
169 .truncate = xmp_truncate,
170 #ifdef HAVE_UTIMENSAT
171 .utimens = xmp_utimens,
176 .statfs = xmp_statfs,
177 .release = xmp_release,
179 #ifdef HAVE_POSIX_FALLOCATE
180 .fallocate = xmp_fallocate,
183 .setxattr = xmp_setxattr_apple,
184 .getxattr = xmp_getxattr_apple,
186 .setxattr = xmp_setxattr,
187 .getxattr = xmp_getxattr,
189 .listxattr = xmp_listxattr,
190 .removexattr = xmp_removexattr,
194 * Check if the path is valid. If it's a relative path,
195 * prepend the working path.
196 * @param path relative or absolute path to eval.
197 * @return new string with absolute path
199 const char *sanitized_source_directory(const char *path) {
201 if (strlen(path) == 0) {
205 /* absolute path, we don't do anything */
206 if (path[0] == '/') {
207 if (is_directory(path) == -1) {
210 absolute_path = strdup(path);
211 return absolute_path;
214 static char cwd[MAXPATHLEN];
215 char *pwd = getcwd(cwd, sizeof(cwd));
216 size_t len = strlen(pwd) + 1 + strlen(path) + 1;
217 int has_trailing_backslash = (path[strlen(path)-1] == '/');
218 if (!has_trailing_backslash)
221 absolute_path = (char*) malloc(sizeof(char) * len);
222 memset(absolute_path, '\0', len);
223 sprintf(absolute_path, "%s/%s", pwd, path);
225 if(!has_trailing_backslash)
226 absolute_path[len-2] = '/';
228 if (is_directory(absolute_path) == -1) {
233 return absolute_path;
241 #define FUSE_XATTRS_OPT(t, p, v) { t, offsetof(struct xattrs_config, p), v }
243 static struct fuse_opt xattrs_opts[] = {
244 FUSE_XATTRS_OPT("show_sidecar", show_sidecar, 1),
245 FUSE_XATTRS_OPT("enable_namespaces", enable_namespaces, 1),
247 FUSE_OPT_KEY("-V", KEY_VERSION),
248 FUSE_OPT_KEY("--version", KEY_VERSION),
249 FUSE_OPT_KEY("-h", KEY_HELP),
250 FUSE_OPT_KEY("--help", KEY_HELP),
254 static int xattrs_opt_proc(void *data, const char *arg, int key,
255 struct fuse_args *outargs) {
258 case FUSE_OPT_KEY_NONOPT:
259 if (!xattrs_config.source_dir) {
260 xattrs_config.source_dir = sanitized_source_directory(arg);
261 xattrs_config.source_dir_size = strlen(xattrs_config.source_dir);
268 "usage: %s source_dir mountpoint [options]\n"
271 " -o opt,[opt...] mount options\n"
272 " -h --help print help\n"
273 " -V --version print version\n"
275 "FUSE XATTRS options:\n"
276 " -o show_sidecar don't hide sidecar files\n"
277 " -o enable_namespaces enable namespaces checks\n"
278 "\n", outargs->argv[0]);
280 fuse_opt_add_arg(outargs, "-ho");
281 fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL);
285 printf("FUSE_XATTRS version %d.%d\n", FUSE_XATTRS_VERSION_MAJOR, FUSE_XATTRS_VERSION_MINOR);
286 fuse_opt_add_arg(outargs, "--version");
287 fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL);
295 int main(int argc, char *argv[]) {
296 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
297 if (fuse_opt_parse(&args, &xattrs_config, xattrs_opts, xattrs_opt_proc) == -1) {
301 if (!xattrs_config.source_dir) {
302 fprintf(stderr, "missing source directory\n");
303 fprintf(stderr, "see `%s -h' for usage\n", argv[0]);
309 // disable multi-threading
310 fuse_opt_add_arg(&args, "-s");
311 return fuse_main(args.argc, args.argv, &xmp_oper, NULL);