use larger string heap
[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 #include "stringmem.h"
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     strfree(sanitized_value);
70 #endif
71
72     int rtval = binary_storage_write_key(_path, name, value, size, flags);
73     strfree(_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     strfree(_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     strfree(_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     strfree(_path);
151
152     return rtval;
153 }
154
155 extern int xmp_utimens(const char *path, const struct timespec ts[2]);
156
157 static struct fuse_operations xmp_oper = {
158         .getattr     = xmp_getattr,
159         .access      = xmp_access,
160         .readlink    = xmp_readlink,
161         .readdir     = xmp_readdir,
162         .mknod       = xmp_mknod,
163         .mkdir       = xmp_mkdir,
164         .symlink     = xmp_symlink,
165         .unlink      = xmp_unlink,
166         .rmdir       = xmp_rmdir,
167         .rename      = xmp_rename,
168         .link        = xmp_link,
169         .chmod       = xmp_chmod,
170         .chown       = xmp_chown,
171         .truncate    = xmp_truncate,
172 #ifdef HAVE_UTIMENSAT
173         .utimens     = xmp_utimens,
174 #endif
175         .open        = xmp_open,
176         .read        = xmp_read,
177         .write       = xmp_write,
178         .statfs      = xmp_statfs,
179         .release     = xmp_release,
180         .fsync       = xmp_fsync,
181 #ifdef HAVE_POSIX_FALLOCATE
182         .fallocate   = xmp_fallocate,
183 #endif
184 #ifdef __APPLE__
185         .setxattr    = xmp_setxattr_apple,
186         .getxattr    = xmp_getxattr_apple,
187 #else
188         .setxattr    = xmp_setxattr,
189         .getxattr    = xmp_getxattr,
190 #endif
191         .listxattr   = xmp_listxattr,
192         .removexattr = xmp_removexattr,
193 };
194
195 /**
196  * Check if the path is valid. If it's a relative path,
197  * prepend the working path. Ensure it ends with /
198  * @param path relative or absolute path to eval.
199  * @return new string with absolute path
200  */
201 const char *sanitized_source_directory(const char *path) {
202     char *absolute_path = stralloc( PATH_MAX );
203     if ( ( strlen( path ) == 0 ) ||
204          ( realpath( path, absolute_path ) == 0 ) ||
205          ( is_directory( absolute_path ) == -1 ) ) {
206         return NULL; // Path badness
207     }
208
209     // Allocate new memory for the string
210     return strdup( absolute_path );
211 }
212
213 enum {
214     KEY_HELP,
215     KEY_VERSION,
216 };
217
218 #define FUSE_XATTRS_OPT(t, p, v) { t, offsetof(struct xattrs_config, p), v }
219
220 static struct fuse_opt xattrs_opts[] = {
221         FUSE_XATTRS_OPT("show_sidecar",         show_sidecar,       1),
222         FUSE_XATTRS_OPT("enable_namespaces",    enable_namespaces,  1),
223         FUSE_XATTRS_OPT("sroot=%s", sidecar_dir, 0 ),
224
225         FUSE_OPT_KEY("-V",                 KEY_VERSION),
226         FUSE_OPT_KEY("--version",          KEY_VERSION),
227         FUSE_OPT_KEY("-h",                 KEY_HELP),
228         FUSE_OPT_KEY("--help",             KEY_HELP),
229         FUSE_OPT_END
230 };
231
232 static int xattrs_opt_proc(void *data, const char *arg, int key,
233                            struct fuse_args *outargs) {
234     (void) data;
235     switch (key) {
236         case FUSE_OPT_KEY_NONOPT:
237             if (!xattrs_config.source_dir) {
238                 xattrs_config.source_dir = sanitized_source_directory(arg);
239                 xattrs_config.source_dir_size = strlen(xattrs_config.source_dir);
240                 return 0;
241             }
242             break;
243
244         case KEY_HELP:
245             fprintf(stderr,
246                     "usage: %s source_dir mountpoint [options]\n"
247                     "\n"
248                     "general options:\n"
249                     "    -o opt,[opt...]  mount options\n"
250                     "    -h   --help      print help\n"
251                     "    -V   --version   print version\n"
252                     "\n"
253                     "FUSE XATTRS options:\n"
254                     "    -o show_sidecar  don't hide sidecar files\n"
255                     "    -o enable_namespaces  enable namespaces checks\n"
256                     "    -o sroot=path  use other sidecar root path\n"
257                     "\n", outargs->argv[0]);
258
259             fuse_opt_add_arg(outargs, "-ho");
260             fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL);
261             exit(1);
262
263         case KEY_VERSION:
264             printf("FUSE_XATTRS version %d.%d\n", FUSE_XATTRS_VERSION_MAJOR, FUSE_XATTRS_VERSION_MINOR);
265             fuse_opt_add_arg(outargs, "--version");
266             fuse_main(outargs->argc, outargs->argv, &xmp_oper, NULL);
267             exit(0);
268     }
269     return 1;
270 }
271
272
273
274 int main(int argc, char *argv[]) {
275     struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
276     if (fuse_opt_parse(&args, &xattrs_config, xattrs_opts, xattrs_opt_proc) == -1) {
277         exit(1);
278     }
279
280     if ( xattrs_config.sidecar_dir ) {
281         xattrs_config.sidecar_dir =
282             sanitized_source_directory( xattrs_config.sidecar_dir );
283         xattrs_config.sidecar_dir_size = strlen( xattrs_config.sidecar_dir );
284     }
285     
286     if (!xattrs_config.source_dir) {
287         fprintf(stderr, "missing source directory\n");
288         fprintf(stderr, "see `%s -h' for usage\n", argv[0]);
289         exit(1);
290     }
291
292     umask(0);
293
294     // disable multi-threading
295     fuse_opt_add_arg(&args, "-s");
296     return fuse_main(args.argc, args.argv, &xmp_oper, NULL);
297 }