forced setting of HAVE_UTIMENSAT=1
[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 struct xattrs_config xattrs_config;
42
43 static int xmp_setxattr(const char *path, const char *name, const char *value, size_t size, int flags)
44 {
45     if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
46         return -ENOENT;
47     }
48
49     if (xattrs_config.enable_namespaces == 1 && get_namespace(name) != USER) {
50         debug_print("Only user namespace is supported. name=%s\n", name);
51         return -ENOTSUP;
52     }
53     if (strlen(name) > XATTR_NAME_MAX) {
54         debug_print("attribute name must be equal or smaller than %d bytes\n", XATTR_NAME_MAX);
55         return -ERANGE;
56     }
57     if (size > XATTR_SIZE_MAX) {
58         debug_print("attribute value cannot be bigger than %d bytes\n", XATTR_SIZE_MAX);
59         return -ENOSPC;
60     }
61
62     char *_path = prepend_source_directory(path);
63
64 #ifdef DEBUG
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);
68
69     free(sanitized_value);
70 #endif
71
72     int rtval = binary_storage_write_key(_path, name, value, size, flags);
73     free(_path);
74
75     return rtval;
76 }
77
78 static int xmp_getxattr(const char *path, const char *name, char *value, size_t size)
79 {
80     if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
81         return -ENOENT;
82     }
83
84     if (xattrs_config.enable_namespaces == 1 && get_namespace(name) != USER) {
85         debug_print("Only user namespace is supported. name=%s\n", name);
86         return -ENOTSUP;
87     }
88     if (strlen(name) > XATTR_NAME_MAX) {
89         debug_print("attribute name must be equal or smaller than %d bytes\n", XATTR_NAME_MAX);
90         return -ERANGE;
91     }
92
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);
96     free(_path);
97
98     return rtval;
99 }
100
101 #ifdef __APPLE__
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);
105     }
106
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);
110     }
111 #endif
112
113 static int xmp_listxattr(const char *path, char *list, size_t size)
114 {
115     if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
116         return -ENOENT;
117     }
118
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");
121         return -E2BIG;
122     }
123
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);
127     free(_path);
128
129     return rtval;
130 }
131
132 static int xmp_removexattr(const char *path, const char *name)
133 {
134     if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
135         return -ENOENT;
136     }
137
138     if (xattrs_config.enable_namespaces == 1 && get_namespace(name) != USER) {
139         debug_print("Only user namespace is supported. name=%s\n", name);
140         return -ENOTSUP;
141     }
142     if (strlen(name) > XATTR_NAME_MAX) {
143         debug_print("attribute name must be equal or smaller than %d bytes\n", XATTR_NAME_MAX);
144         return -ERANGE;
145     }
146
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);
150     free(_path);
151
152     return rtval;
153 }
154
155 static struct fuse_operations xmp_oper = {
156         .getattr     = xmp_getattr,
157         .access      = xmp_access,
158         .readlink    = xmp_readlink,
159         .readdir     = xmp_readdir,
160         .mknod       = xmp_mknod,
161         .mkdir       = xmp_mkdir,
162         .symlink     = xmp_symlink,
163         .unlink      = xmp_unlink,
164         .rmdir       = xmp_rmdir,
165         .rename      = xmp_rename,
166         .link        = xmp_link,
167         .chmod       = xmp_chmod,
168         .chown       = xmp_chown,
169         .truncate    = xmp_truncate,
170 #ifdef HAVE_UTIMENSAT
171         .utimens     = xmp_utimens,
172 #endif
173         .open        = xmp_open,
174         .read        = xmp_read,
175         .write       = xmp_write,
176         .statfs      = xmp_statfs,
177         .release     = xmp_release,
178         .fsync       = xmp_fsync,
179 #ifdef HAVE_POSIX_FALLOCATE
180         .fallocate   = xmp_fallocate,
181 #endif
182 #ifdef __APPLE__
183         .setxattr    = xmp_setxattr_apple,
184         .getxattr    = xmp_getxattr_apple,
185 #else
186         .setxattr    = xmp_setxattr,
187         .getxattr    = xmp_getxattr,
188 #endif
189         .listxattr   = xmp_listxattr,
190         .removexattr = xmp_removexattr,
191 };
192
193 /**
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
198  */
199 const char *sanitized_source_directory(const char *path) {
200     char *absolute_path;
201     if (strlen(path) == 0) {
202         return NULL;
203     }
204
205     /* absolute path, we don't do anything */
206     if (path[0] == '/') {
207         if (is_directory(path) == -1) {
208             return NULL;
209         }
210         absolute_path = strdup(path);
211         return absolute_path;
212     }
213
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)
219         len++;
220
221     absolute_path = (char*) malloc(sizeof(char) * len);
222     memset(absolute_path, '\0', len);
223     sprintf(absolute_path, "%s/%s", pwd, path);
224
225     if(!has_trailing_backslash)
226         absolute_path[len-2] = '/';
227
228     if (is_directory(absolute_path) == -1) {
229         free(absolute_path);
230         return NULL;
231     }
232
233     return absolute_path;
234 }
235
236 enum {
237     KEY_HELP,
238     KEY_VERSION,
239 };
240
241 #define FUSE_XATTRS_OPT(t, p, v) { t, offsetof(struct xattrs_config, p), v }
242
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),
246
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),
251         FUSE_OPT_END
252 };
253
254 static int xattrs_opt_proc(void *data, const char *arg, int key,
255                            struct fuse_args *outargs) {
256     (void) data;
257     switch (key) {
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);
262                 return 0;
263             }
264             break;
265
266         case KEY_HELP:
267             fprintf(stderr,
268                     "usage: %s source_dir mountpoint [options]\n"
269                             "\n"
270                             "general options:\n"
271                             "    -o opt,[opt...]  mount options\n"
272                             "    -h   --help      print help\n"
273                             "    -V   --version   print version\n"
274                             "\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]);
279
280             fuse_opt_add_arg(outargs, "-ho");
281             fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL);
282             exit(1);
283
284         case KEY_VERSION:
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);
288             exit(0);
289     }
290     return 1;
291 }
292
293
294
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) {
298         exit(1);
299     }
300
301     if (!xattrs_config.source_dir) {
302         fprintf(stderr, "missing source directory\n");
303         fprintf(stderr, "see `%s -h' for usage\n", argv[0]);
304         exit(1);
305     }
306
307     umask(0);
308
309     // disable multi-threading
310     fuse_opt_add_arg(&args, "-s");
311     return fuse_main(args.argc, args.argv, &xmp_oper, NULL);
312 }