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"
38 #include "stringmem.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 strfree(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 extern int xmp_utimens(const char *path, const struct timespec ts[2]);
157 static struct fuse_operations xmp_oper = {
158 .getattr = xmp_getattr,
159 .access = xmp_access,
160 .readlink = xmp_readlink,
161 .readdir = xmp_readdir,
164 .symlink = xmp_symlink,
165 .unlink = xmp_unlink,
167 .rename = xmp_rename,
171 .truncate = xmp_truncate,
172 #ifdef HAVE_UTIMENSAT
173 .utimens = xmp_utimens,
178 .statfs = xmp_statfs,
179 .release = xmp_release,
181 #ifdef HAVE_POSIX_FALLOCATE
182 .fallocate = xmp_fallocate,
185 .setxattr = xmp_setxattr_apple,
186 .getxattr = xmp_getxattr_apple,
188 .setxattr = xmp_setxattr,
189 .getxattr = xmp_getxattr,
191 .listxattr = xmp_listxattr,
192 .removexattr = xmp_removexattr,
196 * Check if the path is valid. If it's a relative path,
197 * prepend the working path.
198 * @param path relative or absolute path to eval.
199 * @return new string with absolute path
201 const char *sanitized_source_directory(const char *path) {
203 if (strlen(path) == 0) {
207 /* absolute path, we don't do anything */
208 if (path[0] == '/') {
209 if (is_directory(path) == -1) {
212 absolute_path = strdup(path);
213 return absolute_path;
216 static char cwd[MAXPATHLEN];
217 char *pwd = getcwd(cwd, sizeof(cwd));
218 size_t len = strlen(pwd) + 1 + strlen(path) + 1;
219 int has_trailing_backslash = (path[strlen(path)-1] == '/');
220 if (!has_trailing_backslash)
223 absolute_path = (char*) malloc(sizeof(char) * len);
224 memset(absolute_path, '\0', len);
225 sprintf(absolute_path, "%s/%s", pwd, path);
227 if(!has_trailing_backslash)
228 absolute_path[len-2] = '/';
230 if (is_directory(absolute_path) == -1) {
231 strfree(absolute_path);
235 return absolute_path;
243 #define FUSE_XATTRS_OPT(t, p, v) { t, offsetof(struct xattrs_config, p), v }
245 static struct fuse_opt xattrs_opts[] = {
246 FUSE_XATTRS_OPT("show_sidecar", show_sidecar, 1),
247 FUSE_XATTRS_OPT("enable_namespaces", enable_namespaces, 1),
248 FUSE_XATTRS_OPT("sroot=%s", sidecar_dir, 0 ),
250 FUSE_OPT_KEY("-V", KEY_VERSION),
251 FUSE_OPT_KEY("--version", KEY_VERSION),
252 FUSE_OPT_KEY("-h", KEY_HELP),
253 FUSE_OPT_KEY("--help", KEY_HELP),
257 static int xattrs_opt_proc(void *data, const char *arg, int key,
258 struct fuse_args *outargs) {
261 case FUSE_OPT_KEY_NONOPT:
262 if (!xattrs_config.source_dir) {
263 xattrs_config.source_dir = sanitized_source_directory(arg);
264 xattrs_config.source_dir_size = strlen(xattrs_config.source_dir);
271 "usage: %s source_dir mountpoint [options]\n"
274 " -o opt,[opt...] mount options\n"
275 " -h --help print help\n"
276 " -V --version print version\n"
278 "FUSE XATTRS options:\n"
279 " -o show_sidecar don't hide sidecar files\n"
280 " -o enable_namespaces enable namespaces checks\n"
281 "\n", outargs->argv[0]);
283 fuse_opt_add_arg(outargs, "-ho");
284 fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL);
288 printf("FUSE_XATTRS version %d.%d\n", FUSE_XATTRS_VERSION_MAJOR, FUSE_XATTRS_VERSION_MINOR);
289 fuse_opt_add_arg(outargs, "--version");
290 fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL);
298 int main(int argc, char *argv[]) {
299 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
300 if (fuse_opt_parse(&args, &xattrs_config, xattrs_opts, xattrs_opt_proc) == -1) {
304 if ( xattrs_config.sidecar_dir ) {
305 xattrs_config.sidecar_dir =
306 sanitized_source_directory( xattrs_config.sidecar_dir );
310 if (!xattrs_config.source_dir) {
311 fprintf(stderr, "missing source directory\n");
312 fprintf(stderr, "see `%s -h' for usage\n", argv[0]);
318 // disable multi-threading
319 fuse_opt_add_arg(&args, "-s");
320 return fuse_main(args.argc, args.argv, &xmp_oper, NULL);