3d3a5a493abed3208166a44fc4167bc1ea694a4d
[rrq/fuse_xattrs.git] / fuse_xattrs.c
1 /*
2   fuse_xattrs - Add xattrs support using sidecar files
3
4   Copyright (C) 2016-2017  Felipe Barriga Richards <felipe {at} felipebarriga.cl>
5
6   Based on passthrough.c (libfuse example)
7
8   This program can be distributed under the terms of the GNU GPL.
9   See the file COPYING.
10 */
11
12 #define FUSE_USE_VERSION 30
13
14 /* For pread()/pwrite()/utimensat() */
15 #define _XOPEN_SOURCE 700
16
17 #include <errno.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stddef.h>
22 #include <assert.h>
23
24 #ifdef __APPLE__
25     #include <osxfuse/fuse.h>
26 #else
27     #include <fuse.h>
28 #endif
29
30 #include <sys/xattr.h>
31 #include <sys/param.h>
32
33 #include "fuse_xattrs_config.h"
34
35 #include "xattrs_config.h"
36 #include "utils.h"
37 #include "passthrough.h"
38
39 #include "binary_storage.h"
40
41 static int xmp_setxattr(const char *path, const char *name, const char *value, size_t size, int flags)
42 {
43     if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
44         return -ENOENT;
45     }
46
47     if (xattrs_config.enable_namespaces == 1 && get_namespace(name) != USER) {
48         debug_print("Only user namespace is supported. name=%s\n", name);
49         return -ENOTSUP;
50     }
51     if (strlen(name) > XATTR_NAME_MAX) {
52         debug_print("attribute name must be equal or smaller than %d bytes\n", XATTR_NAME_MAX);
53         return -ERANGE;
54     }
55     if (size > XATTR_SIZE_MAX) {
56         debug_print("attribute value cannot be bigger than %d bytes\n", XATTR_SIZE_MAX);
57         return -ENOSPC;
58     }
59
60     char *_path = prepend_source_directory(path);
61
62 #ifdef DEBUG
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);
66
67     free(sanitized_value);
68 #endif
69
70     int rtval = binary_storage_write_key(_path, name, value, size, flags);
71     free(_path);
72
73     return rtval;
74 }
75
76 static int xmp_getxattr(const char *path, const char *name, char *value, size_t size)
77 {
78     if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
79         return -ENOENT;
80     }
81
82     if (xattrs_config.enable_namespaces == 1 && get_namespace(name) != USER) {
83         debug_print("Only user namespace is supported. name=%s\n", name);
84         return -ENOTSUP;
85     }
86     if (strlen(name) > XATTR_NAME_MAX) {
87         debug_print("attribute name must be equal or smaller than %d bytes\n", XATTR_NAME_MAX);
88         return -ERANGE;
89     }
90
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);
94     free(_path);
95
96     return rtval;
97 }
98
99 #ifdef __APPLE__
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);
103     }
104
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);
108     }
109 #endif
110
111 static int xmp_listxattr(const char *path, char *list, size_t size)
112 {
113     if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
114         return -ENOENT;
115     }
116
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");
119         return -E2BIG;
120     }
121
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);
125     free(_path);
126
127     return rtval;
128 }
129
130 static int xmp_removexattr(const char *path, const char *name)
131 {
132     if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
133         return -ENOENT;
134     }
135
136     if (xattrs_config.enable_namespaces == 1 && get_namespace(name) != USER) {
137         debug_print("Only user namespace is supported. name=%s\n", name);
138         return -ENOTSUP;
139     }
140     if (strlen(name) > XATTR_NAME_MAX) {
141         debug_print("attribute name must be equal or smaller than %d bytes\n", XATTR_NAME_MAX);
142         return -ERANGE;
143     }
144
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);
148     free(_path);
149
150     return rtval;
151 }
152
153 static struct fuse_operations xmp_oper = {
154         .getattr     = xmp_getattr,
155         .access      = xmp_access,
156         .readlink    = xmp_readlink,
157         .readdir     = xmp_readdir,
158         .mknod       = xmp_mknod,
159         .mkdir       = xmp_mkdir,
160         .symlink     = xmp_symlink,
161         .unlink      = xmp_unlink,
162         .rmdir       = xmp_rmdir,
163         .rename      = xmp_rename,
164         .link        = xmp_link,
165         .chmod       = xmp_chmod,
166         .chown       = xmp_chown,
167         .truncate    = xmp_truncate,
168 #ifdef HAVE_UTIMENSAT
169         .utimens     = xmp_utimens,
170 #endif
171         .open        = xmp_open,
172         .read        = xmp_read,
173         .write       = xmp_write,
174         .statfs      = xmp_statfs,
175         .release     = xmp_release,
176         .fsync       = xmp_fsync,
177 #ifdef HAVE_POSIX_FALLOCATE
178         .fallocate   = xmp_fallocate,
179 #endif
180 #ifdef __APPLE__
181         .setxattr    = xmp_setxattr_apple,
182         .getxattr    = xmp_getxattr_apple,
183 #else
184         .setxattr    = xmp_setxattr,
185         .getxattr    = xmp_getxattr,
186 #endif
187         .listxattr   = xmp_listxattr,
188         .removexattr = xmp_removexattr,
189 };
190
191 /**
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
196  */
197 const char *sanitized_source_directory(const char *path) {
198     char *absolute_path;
199     if (strlen(path) == 0) {
200         return NULL;
201     }
202
203     /* absolute path, we don't do anything */
204     if (path[0] == '/') {
205         if (is_directory(path) == -1) {
206             return NULL;
207         }
208         absolute_path = strdup(path);
209         return absolute_path;
210     }
211
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)
217         len++;
218
219     absolute_path = (char*) malloc(sizeof(char) * len);
220     memset(absolute_path, '\0', len);
221     sprintf(absolute_path, "%s/%s", pwd, path);
222
223     if(!has_trailing_backslash)
224         absolute_path[len-2] = '/';
225
226     if (is_directory(absolute_path) == -1) {
227         free(absolute_path);
228         return NULL;
229     }
230
231     return absolute_path;
232 }
233
234 enum {
235     KEY_HELP,
236     KEY_VERSION,
237 };
238
239 #define FUSE_XATTRS_OPT(t, p, v) { t, offsetof(struct xattrs_config, p), v }
240
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),
244
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),
249         FUSE_OPT_END
250 };
251
252 static int xattrs_opt_proc(void *data, const char *arg, int key,
253                            struct fuse_args *outargs) {
254     (void) data;
255     switch (key) {
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);
260                 return 0;
261             }
262             break;
263
264         case KEY_HELP:
265             fprintf(stderr,
266                     "usage: %s source_dir mountpoint [options]\n"
267                             "\n"
268                             "general options:\n"
269                             "    -o opt,[opt...]  mount options\n"
270                             "    -h   --help      print help\n"
271                             "    -V   --version   print version\n"
272                             "\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]);
277
278             fuse_opt_add_arg(outargs, "-ho");
279             fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL);
280             exit(1);
281
282         case KEY_VERSION:
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);
286             exit(0);
287     }
288     return 1;
289 }
290
291
292
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) {
296         exit(1);
297     }
298
299     if (!xattrs_config.source_dir) {
300         fprintf(stderr, "missing source directory\n");
301         fprintf(stderr, "see `%s -h' for usage\n", argv[0]);
302         exit(1);
303     }
304
305     umask(0);
306
307     // disable multi-threading
308     fuse_opt_add_arg(&args, "-s");
309     return fuse_main(args.argc, args.argv, &xmp_oper, NULL);
310 }