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(xattrs_config.source_dir, 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(xattrs_config.source_dir, 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(xattrs_config.source_dir, 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(xattrs_config.source_dir, 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 int is_directory(const char *path) {
173 if (stat(path, &statbuf) != 0) {
174 fprintf(stderr, "cannot get source directory status: %s\n", path);
178 if (!S_ISDIR(statbuf.st_mode)) {
179 fprintf(stderr, "source directory must be a directory: %s\n", path);
187 * Check if the path is valid. If it's a relative path,
188 * prepend the working path.
189 * @param path relative or absolute path to eval.
190 * @return new string with absolute path
192 const char *sanitized_source_directory(const char *path) {
194 if (strlen(path) == 0) {
198 /* absolute path, we don't do anything */
199 if (path[0] == '/') {
200 if (is_directory(path) == -1) {
203 absolute_path = strdup(path);
204 return absolute_path;
207 char *pwd = get_current_dir_name();
208 size_t len = strlen(pwd) + 1 + strlen(path) + 1;
209 int has_trailing_backslash = (path[strlen(path)-1] == '/');
210 if (!has_trailing_backslash)
213 absolute_path = (char*) malloc(sizeof(char) * len);
214 memset(absolute_path, '\0', len);
215 sprintf(absolute_path, "%s/%s", pwd, path);
217 if(!has_trailing_backslash)
218 absolute_path[len-2] = '/';
220 if (is_directory(absolute_path) == -1) {
225 return absolute_path;
233 #define FUSE_XATTRS_OPT(t, p, v) { t, offsetof(struct xattrs_config, p), v }
235 static struct fuse_opt xattrs_opts[] = {
236 FUSE_XATTRS_OPT("show_sidecar", show_sidecar, 1),
238 FUSE_OPT_KEY("-V", KEY_VERSION),
239 FUSE_OPT_KEY("--version", KEY_VERSION),
240 FUSE_OPT_KEY("-h", KEY_HELP),
241 FUSE_OPT_KEY("--help", KEY_HELP),
245 static int xattrs_opt_proc(void *data, const char *arg, int key,
246 struct fuse_args *outargs) {
249 case FUSE_OPT_KEY_NONOPT:
250 if (!xattrs_config.source_dir) {
251 xattrs_config.source_dir = sanitized_source_directory(arg);
258 "usage: %s source_dir mountpoint [options]\n"
261 " -o opt,[opt...] mount options\n"
262 " -h --help print help\n"
263 " -V --version print version\n"
265 "FUSE XATTRS options:\n"
266 " -o show_sidecar don't hide sidecar files\n"
267 "\n", outargs->argv[0]);
269 fuse_opt_add_arg(outargs, "-ho");
270 fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL);
274 printf("FUSE_XATTRS version %d.%d\n", FUSE_XATTRS_VERSION_MAJOR, FUSE_XATTRS_VERSION_MINOR);
275 fuse_opt_add_arg(outargs, "--version");
276 fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL);
284 int main(int argc, char *argv[]) {
285 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
286 if (fuse_opt_parse(&args, &xattrs_config, xattrs_opts, xattrs_opt_proc) == -1) {
290 if (!xattrs_config.source_dir) {
291 fprintf(stderr, "missing source directory\n");
292 fprintf(stderr, "see `%s -h' for usage\n", argv[0]);
297 return fuse_main(args.argc, args.argv, &xmp_oper, NULL);