2 afuse - An automounter using FUSE
3 Copyright (C) 2008 Jacob Bower <jacob.bower@ic.ac.uk>
5 Portions of this program derive from examples provided with
8 This program can be distributed under the terms of the GNU GPL.
13 Feb '08, Jeremy Maitin-Shepard <jbms@cmu.edu>
14 * Added timeout-based unmounting.
15 * Forced immediate unmounting by removing root directory of
17 * Minor performance tweaks.
18 * Better handling of filesystems which are unexpectedly
26 // For pread()/pwrite()
27 #define _XOPEN_SOURCE 500
44 #include <sys/types.h>
50 #include <sys/xattr.h>
57 #include "variable_pairing_heap.h"
59 #define TMP_DIR_TEMPLATE "/tmp/afuse-XXXXXX"
60 char *mount_point_directory;
61 dev_t mount_point_dev;
63 // Data structure filled in when parsing command line args
64 struct user_options_t {
65 char *mount_command_template;
66 char *unmount_command_template;
68 uint64_t auto_unmount_delay;
69 } user_options = {NULL, NULL, false, UINT64_MAX};
71 typedef struct _mount_list_t {
72 struct _mount_list_t *next;
73 struct _mount_list_t *prev;
80 PH_NEW_LINK(struct _mount_list_t) auto_unmount_ph_node;
81 /* This is the sort key for the auto_unmount_ph heap. It will
82 equal UINT64_MAX if this node is not in the heap. */
83 int64_t auto_unmount_time;
86 PH_DECLARE_TYPE(auto_unmount_ph, mount_list_t)
87 PH_DEFINE_TYPE(auto_unmount_ph, mount_list_t, auto_unmount_ph_node, auto_unmount_time)
89 #define BLOCK_SIGALRM \
90 sigset_t block_sigalrm_oldset, block_sigalrm_set; \
91 sigemptyset(&block_sigalrm_set); \
92 sigaddset(&block_sigalrm_set, SIGALRM); \
93 sigprocmask(SIG_BLOCK, &block_sigalrm_set, &block_sigalrm_oldset)
95 #define UNBLOCK_SIGALRM \
96 sigprocmask(SIG_SETMASK, &block_sigalrm_oldset, NULL)
98 auto_unmount_ph_t auto_unmount_ph;
99 int64_t auto_unmount_next_timeout = INT64_MAX;
101 static int64_t from_timeval(const struct timeval *tv)
103 return (int64_t)tv->tv_sec * 1000000 + ((int64_t)tv->tv_usec);
106 static void to_timeval(struct timeval *tv, int64_t usec)
108 tv->tv_sec = usec / 1000000;
109 tv->tv_usec = usec % 1000000;
112 static int get_retval(int res)
119 static int check_mount(mount_list_t *mount)
123 if (lstat(mount->mount_point, &buf) == -1)
125 if (buf.st_dev == mount_point_dev)
130 static void update_auto_unmount(mount_list_t *mount)
132 if (user_options.auto_unmount_delay == UINT64_MAX)
135 /* Get the current time */
137 mount_list_t *min_mount;
138 int64_t cur_time, next_time;
139 gettimeofday(&tv, NULL);
140 cur_time = from_timeval(&tv);
144 /* Always remove first */
145 if (mount->auto_unmount_time != INT64_MAX)
146 auto_unmount_ph_remove(&auto_unmount_ph, mount);
148 if (!mount->fd_list && !mount->dir_list)
150 mount->auto_unmount_time = cur_time + user_options.auto_unmount_delay;
151 auto_unmount_ph_insert(&auto_unmount_ph, mount);
154 mount->auto_unmount_time = INT64_MAX;
157 min_mount = auto_unmount_ph_min(&auto_unmount_ph);
158 next_time = min_mount ? min_mount->auto_unmount_time : INT64_MAX;
160 if (next_time != auto_unmount_next_timeout)
162 struct itimerval itv;
163 auto_unmount_next_timeout = next_time;
165 if (next_time != INT64_MAX)
167 if (next_time > cur_time)
168 to_timeval(&itv.it_value, next_time - cur_time);
169 else /* Timer is set to expire immediately --- set it to 1 instead*/
170 to_timeval(&itv.it_value, 1);
173 /* Disable the timer */
174 to_timeval(&itv.it_value, 0);
176 to_timeval(&itv.it_interval, 0);
177 if (setitimer(ITIMER_REAL, &itv, NULL) != 0) {
178 perror("Error setting timer");
183 static void handle_auto_unmount_timer(int x)
185 /* Get the current time */
189 gettimeofday(&tv, NULL);
190 cur_time = from_timeval(&tv);
192 while ((mount = auto_unmount_ph_min(&auto_unmount_ph)) != NULL &&
193 mount->auto_unmount_time <= cur_time)
198 update_auto_unmount(NULL);
201 mount_list_t *mount_list = NULL;
203 mount_list_t *find_mount(const char *root_name)
205 mount_list_t *current_mount = mount_list;
207 while(current_mount) {
208 if( strcmp(root_name, current_mount->root_name) == 0)
209 return current_mount;
211 current_mount = current_mount->next;
217 int is_mount(const char *root_name)
219 return find_mount(root_name) ? 1 : 0;
222 mount_list_t *add_mount(const char *root_name, char *mount_point)
224 mount_list_t *new_mount;
226 new_mount = (mount_list_t *)my_malloc( sizeof(mount_list_t) );
227 new_mount->root_name = my_strdup(root_name);
228 new_mount->mount_point = mount_point;
230 new_mount->next = mount_list;
231 new_mount->prev = NULL;
232 new_mount->fd_list = NULL;
233 new_mount->dir_list = NULL;
234 new_mount->auto_unmount_time = INT64_MAX;
236 mount_list->prev = new_mount;
238 mount_list = new_mount;
240 update_auto_unmount(new_mount);
245 void remove_mount(mount_list_t *current_mount)
247 if (current_mount->auto_unmount_time != INT64_MAX)
248 auto_unmount_ph_remove(&auto_unmount_ph, current_mount);
250 free(current_mount->root_name);
251 free(current_mount->mount_point);
252 if(current_mount->prev)
253 current_mount->prev->next = current_mount->next;
255 mount_list = current_mount->next;
256 if(current_mount->next)
257 current_mount->next->prev = current_mount->prev;
259 update_auto_unmount(NULL);
262 char *make_mount_point(const char *root_name)
267 // Create the mount point
268 dir_tmp = my_malloc(strlen(mount_point_directory) + 2 + strlen(root_name));
269 strcpy(dir_tmp, mount_point_directory);
270 strcat(dir_tmp, "/");
271 strcat(dir_tmp, root_name);
273 if(mkdir(dir_tmp, 0700) == -1 && errno != EEXIST) {
274 fprintf(stderr, "Cannot create directory: %s (%s)\n",
275 dir_tmp, strerror(errno));
283 // Note: this method strips out quotes and applies them itself as should be appropriate
284 bool run_template(const char *template, const char *mount_point, const char *root_name)
298 for(i = 0; template[i]; i++)
299 if(template[i] == '%') {
300 switch(template[i + 1])
303 len += strlen(mount_point);
307 len += strlen(root_name);
315 } else if(template[i] == ' ' && !quote) {
318 } else if(template[i] == '"')
320 else if(template[i] == '\\' && template[i + 1])
325 buf = my_malloc(len + 1);
326 args = my_malloc((nargs + 1) * sizeof(*args));
332 for(i = 0; template[i]; i++)
333 if(template[i] == '%') {
334 switch(template[i + 1])
337 strcpy(p, mount_point);
338 p += strlen(mount_point);
342 strcpy(p, root_name);
343 p += strlen(root_name);
351 } else if(template[i] == ' ' && !quote) {
354 } else if(template[i] == '"')
356 else if(template[i] == '\\' && template[i + 1])
357 *p++ = template[++i];
366 fprintf(stderr, "Failed to fork (%s)\n", strerror(errno));
372 execvp(args[0], args);
375 pid = waitpid(pid, &status, 0);
377 fprintf(stderr, "Failed to waitpid (%s)\n", strerror(errno));
382 if(!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
383 fprintf(stderr, "Failed to invoke command: %s\n", args[0]);
393 mount_list_t *do_mount(const char *root_name)
398 fprintf(stderr, "Mounting: %s\n", root_name);
400 if( !(mount_point = make_mount_point(root_name)) ) {
401 fprintf(stderr, "Failed to create mount point directory: %s/%s\n",
402 mount_point_directory, root_name);
406 if(!run_template(user_options.mount_command_template,
407 mount_point, root_name)) {
408 // remove the now unused directory
409 if( rmdir(mount_point) == -1 )
410 fprintf(stderr, "Failed to remove mount point dir: %s (%s)",
411 mount_point, strerror(errno));
417 mount = add_mount(root_name, mount_point);
421 int do_umount(mount_list_t *mount)
423 fprintf(stderr, "Unmounting: %s\n", mount->root_name);
425 run_template(user_options.unmount_command_template,
426 mount->mount_point, mount->root_name);
427 /* Still unmount anyway */
429 if( rmdir(mount->mount_point) == -1 )
430 fprintf(stderr, "Failed to remove mount point dir: %s (%s)",
431 mount->mount_point, strerror(errno));
436 void unmount_all(void)
438 fprintf(stderr, "Attempting to unmount all filesystems:\n");
441 fprintf(stderr, "\tUnmounting: %s\n", mount_list->root_name);
443 do_umount(mount_list);
446 fprintf(stderr, "done.\n");
457 if(rmdir(mount_point_directory) == -1)
458 fprintf(stderr, "Failed to remove temporary mount point directory: %s (%s)\n",
459 mount_point_directory, strerror(errno));
462 int max_path_out_len(const char *path_in)
464 return strlen(mount_point_directory) + strlen(path_in) + 2;
467 // returns true if path is the a child directory of a root node
468 // e.g. /a/b is a child, /a is not.
469 int extract_root_name(const char *path, char *root_name)
474 for(i = 1; path[i] && path[i] != '/'; i++)
475 root_name[i - 1] = path[i];
476 root_name[i - 1] = '\0';
478 return strlen(&path[i]);
481 typedef enum {PROC_PATH_FAILED, PROC_PATH_ROOT_DIR, PROC_PATH_PROXY_DIR} proc_result_t;
483 proc_result_t process_path(const char *path_in, char *path_out, char *root_name,
484 int attempt_mount, mount_list_t **out_mount)
490 mount_list_t *mount = NULL;
494 fprintf(stderr, "Path in: %s\n", path_in);
495 is_child = extract_root_name(path_in, root_name);
496 fprintf(stderr, "root_name is: %s\n", root_name);
498 // Mount filesystem if necessary
499 // the combination of is_child and attempt_mount prevent inappropriate
500 // mounting of a filesystem for example if the user tries to mknod
501 // in the afuse root this should cause an error not a mount.
502 // !!FIXME!! this is broken on FUSE < 2.5 (?) because a getattr
503 // on the root node seems to occur with every single access.
504 if( /*(is_child || attempt_mount ) && */
505 strlen(root_name) > 0 &&
506 !(mount = find_mount(root_name)) &&
507 !(mount = do_mount(root_name)))
508 return PROC_PATH_FAILED;
510 if (mount && !check_mount(mount))
513 mount = do_mount(root_name);
515 return PROC_PATH_FAILED;
518 // construct path_out (mount_point_directory + '/' + path_in + '\0')
519 path_out_base = path_out;
520 len = strlen(mount_point_directory);
521 memcpy(path_out, mount_point_directory, len);
524 len = strlen(path_in) - 1;
525 memcpy(path_out, path_in + 1, len);
528 fprintf(stderr, "Path out: %s\n", path_out_base);
532 return strlen(root_name) ? PROC_PATH_PROXY_DIR : PROC_PATH_ROOT_DIR;
535 static int afuse_getattr(const char *path, struct stat *stbuf)
538 char *root_name = alloca( strlen(path) );
539 char *real_path = alloca( max_path_out_len(path) );
544 fprintf(stderr, "> GetAttr\n");
546 switch( process_path(path, real_path, root_name, 0, &mount) )
548 case PROC_PATH_FAILED:
552 case PROC_PATH_ROOT_DIR:
553 fprintf(stderr, "Getattr on: (%s) - %s\n", path, root_name);
554 if(mount || strlen(root_name) == 0) {
555 stbuf->st_mode = S_IFDIR | 0700;
557 stbuf->st_uid = getuid();
558 stbuf->st_gid = getgid();
560 stbuf->st_blksize = 0;
561 stbuf->st_blocks = 0;
570 case PROC_PATH_PROXY_DIR:
571 retval = get_retval(lstat(real_path, stbuf));
575 update_auto_unmount(mount);
580 static int afuse_readlink(const char *path, char *buf, size_t size)
583 char *root_name = alloca( strlen(path) );
584 char *real_path = alloca( max_path_out_len(path) );
589 switch( process_path(path, real_path, root_name, 1, &mount) )
591 case PROC_PATH_FAILED:
594 case PROC_PATH_ROOT_DIR:
597 case PROC_PATH_PROXY_DIR:
598 res = readlink(real_path, buf, size - 1);
609 update_auto_unmount(mount);
614 static int afuse_opendir(const char *path, struct fuse_file_info *fi)
617 char *root_name = alloca( strlen(path) );
619 char *real_path = alloca( max_path_out_len(path) );
623 switch( process_path(path, real_path, root_name, 1, &mount) )
625 case PROC_PATH_FAILED:
628 case PROC_PATH_ROOT_DIR:
631 case PROC_PATH_PROXY_DIR:
632 dp = opendir(real_path);
637 fi->fh = (unsigned long) dp;
639 dir_list_add(&mount->dir_list, dp);
644 update_auto_unmount(mount);
649 static inline DIR *get_dirp(struct fuse_file_info *fi)
651 return (DIR *) (uintptr_t) fi->fh;
654 static int afuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
655 off_t offset, struct fuse_file_info *fi)
657 DIR *dp = get_dirp(fi);
659 char *root_name = alloca( strlen(path) );
660 char *real_path = alloca( max_path_out_len(path) );
661 mount_list_t *mount, *next;
665 switch( process_path(path, real_path, root_name, 1, &mount) )
667 case PROC_PATH_FAILED:
671 case PROC_PATH_ROOT_DIR:
672 filler(buf, ".", NULL, 0);
673 filler(buf, "..", NULL, 0);
674 for(mount = mount_list; mount; mount = next)
677 /* Check for dead mounts. */
678 if (!check_mount(mount))
681 filler(buf, mount->root_name, NULL, 0);
687 case PROC_PATH_PROXY_DIR:
689 while ((de = readdir(dp)) != NULL) {
691 memset(&st, 0, sizeof(st));
692 st.st_ino = de->d_ino;
693 st.st_mode = de->d_type << 12;
694 if (filler(buf, de->d_name, &st, telldir(dp)))
701 update_auto_unmount(mount);
706 static int afuse_releasedir(const char *path, struct fuse_file_info *fi)
708 DIR *dp = get_dirp(fi);
710 char *root_name = alloca( strlen(path) );
711 char *real_path = alloca( max_path_out_len(path) );
716 switch( process_path(path, real_path, root_name, 1, &mount) )
718 case PROC_PATH_FAILED:
722 case PROC_PATH_ROOT_DIR:
726 case PROC_PATH_PROXY_DIR:
728 dir_list_remove(&mount->dir_list, dp);
733 update_auto_unmount(mount);
738 static int afuse_mknod(const char *path, mode_t mode, dev_t rdev)
740 char *root_name = alloca( strlen(path) );
741 char *real_path = alloca( max_path_out_len(path) );
745 fprintf(stderr, "> Mknod\n");
747 switch( process_path(path, real_path, root_name, 0, &mount) )
749 case PROC_PATH_FAILED:
753 case PROC_PATH_ROOT_DIR:
757 case PROC_PATH_PROXY_DIR:
759 retval = get_retval(mkfifo(real_path, mode));
761 retval = get_retval(mknod(real_path, mode, rdev));
765 update_auto_unmount(mount);
770 static int afuse_mkdir(const char *path, mode_t mode)
772 char *root_name = alloca( strlen(path) );
773 char *real_path = alloca( max_path_out_len(path) );
778 switch( process_path(path, real_path, root_name, 0, &mount) )
780 case PROC_PATH_FAILED:
783 case PROC_PATH_ROOT_DIR:
786 case PROC_PATH_PROXY_DIR:
787 retval = get_retval(mkdir(real_path, mode));
791 update_auto_unmount(mount);
796 static int afuse_unlink(const char *path)
798 char *root_name = alloca( strlen(path) );
799 char *real_path = alloca( max_path_out_len(path) );
804 switch( process_path(path, real_path, root_name, 0, &mount) )
806 case PROC_PATH_FAILED:
809 case PROC_PATH_ROOT_DIR:
812 case PROC_PATH_PROXY_DIR:
813 retval = get_retval(unlink(real_path));
817 update_auto_unmount(mount);
822 static int afuse_rmdir(const char *path)
824 char *root_name = alloca( strlen(path) );
825 char *real_path = alloca( max_path_out_len(path) );
830 switch( process_path(path, real_path, root_name, 0, &mount) )
832 case PROC_PATH_FAILED:
835 case PROC_PATH_ROOT_DIR:
838 case PROC_PATH_PROXY_DIR:
839 if (!extract_root_name(path, root_name))
842 if (mount->dir_list || mount->fd_list)
851 retval = get_retval(rmdir(real_path));
855 update_auto_unmount(mount);
860 static int afuse_symlink(const char *from, const char *to)
862 char *root_name_to = alloca( strlen(to) );
863 char *real_to_path = alloca( max_path_out_len(to) );
868 switch( process_path(to, real_to_path, root_name_to, 0, &mount) )
870 case PROC_PATH_FAILED:
873 case PROC_PATH_ROOT_DIR:
876 case PROC_PATH_PROXY_DIR:
877 retval = get_retval(symlink(from, real_to_path));
881 update_auto_unmount(mount);
886 static int afuse_rename(const char *from, const char *to)
888 char *root_name_from = alloca( strlen(from) );
889 char *root_name_to = alloca( strlen(to) );
890 char *real_from_path = alloca( max_path_out_len(from) );
891 char *real_to_path = alloca( max_path_out_len(to) );
892 mount_list_t *mount_from, *mount_to = NULL;
896 switch( process_path(from, real_from_path, root_name_from, 0, &mount_from) )
898 case PROC_PATH_FAILED:
902 case PROC_PATH_ROOT_DIR:
906 case PROC_PATH_PROXY_DIR:
907 switch( process_path(to, real_to_path, root_name_to, 0, &mount_to) )
909 case PROC_PATH_FAILED:
913 case PROC_PATH_ROOT_DIR:
917 case PROC_PATH_PROXY_DIR:
918 retval = get_retval(rename(real_from_path, real_to_path));
924 update_auto_unmount(mount_to);
925 if (mount_from && mount_from != mount_to)
926 update_auto_unmount(mount_from);
931 static int afuse_link(const char *from, const char *to)
933 char *root_name_from = alloca( strlen(from) );
934 char *root_name_to = alloca( strlen(to) );
935 char *real_from_path = alloca( max_path_out_len(from) );
936 char *real_to_path = alloca( max_path_out_len(to) );
937 mount_list_t *mount_to = NULL, *mount_from;
941 switch( process_path(from, real_from_path, root_name_from, 0, &mount_from) )
943 case PROC_PATH_FAILED:
946 case PROC_PATH_ROOT_DIR:
949 case PROC_PATH_PROXY_DIR:
950 switch( process_path(to, real_to_path, root_name_to, 0, &mount_to) )
952 case PROC_PATH_FAILED:
955 case PROC_PATH_ROOT_DIR:
958 case PROC_PATH_PROXY_DIR:
959 retval = get_retval(link(real_from_path, real_to_path));
965 update_auto_unmount(mount_to);
966 if (mount_from && mount_from != mount_to)
967 update_auto_unmount(mount_from);
972 static int afuse_chmod(const char *path, mode_t mode)
974 char *root_name = alloca( strlen(path) );
975 char *real_path = alloca( max_path_out_len(path) );
980 switch( process_path(path, real_path, root_name, 0, &mount) )
982 case PROC_PATH_FAILED:
985 case PROC_PATH_ROOT_DIR:
988 case PROC_PATH_PROXY_DIR:
989 retval = get_retval(chmod(real_path, mode));
993 update_auto_unmount(mount);
998 static int afuse_chown(const char *path, uid_t uid, gid_t gid)
1000 char *root_name = alloca( strlen(path) );
1001 char *real_path = alloca( max_path_out_len(path) );
1002 mount_list_t *mount;
1006 switch( process_path(path, real_path, root_name, 0, &mount) )
1008 case PROC_PATH_FAILED:
1011 case PROC_PATH_ROOT_DIR:
1014 case PROC_PATH_PROXY_DIR:
1015 retval = get_retval(lchown(real_path, uid, gid));
1019 update_auto_unmount(mount);
1024 static int afuse_truncate(const char *path, off_t size)
1026 char *root_name = alloca( strlen(path) );
1027 char *real_path = alloca( max_path_out_len(path) );
1028 mount_list_t *mount;
1032 switch( process_path(path, real_path, root_name, 0, &mount) )
1034 case PROC_PATH_FAILED:
1037 case PROC_PATH_ROOT_DIR:
1040 case PROC_PATH_PROXY_DIR:
1041 retval = get_retval(truncate(real_path, size));
1045 update_auto_unmount(mount);
1051 static int afuse_utime(const char *path, struct utimbuf *buf)
1054 char *root_name = alloca( strlen(path) );
1055 char *real_path = alloca( max_path_out_len(path) );
1056 mount_list_t *mount;
1060 switch( process_path(path, real_path, root_name, 0, &mount) )
1062 case PROC_PATH_FAILED:
1065 case PROC_PATH_ROOT_DIR:
1068 case PROC_PATH_PROXY_DIR:
1069 retval = get_retval(utime(real_path, buf));
1073 update_auto_unmount(mount);
1079 static int afuse_open(const char *path, struct fuse_file_info *fi)
1082 char *root_name = alloca( strlen(path) );
1083 mount_list_t *mount;
1084 char *real_path = alloca( max_path_out_len(path) );
1088 switch( process_path(path, real_path, root_name, 1, &mount) )
1090 case PROC_PATH_FAILED:
1093 case PROC_PATH_ROOT_DIR:
1096 case PROC_PATH_PROXY_DIR:
1097 fd = open(real_path, fi->flags);
1105 fd_list_add(&mount->fd_list, fd);
1110 update_auto_unmount(mount);
1115 static int afuse_read(const char *path, char *buf, size_t size, off_t offset,
1116 struct fuse_file_info *fi)
1121 res = pread(fi->fh, buf, size, offset);
1128 static int afuse_write(const char *path, const char *buf, size_t size,
1129 off_t offset, struct fuse_file_info *fi)
1134 res = pwrite(fi->fh, buf, size, offset);
1138 if(user_options.flush_writes)
1145 static int afuse_release(const char *path, struct fuse_file_info *fi)
1147 char *root_name = alloca( strlen(path) );
1148 mount_list_t *mount;
1152 extract_root_name(path, root_name);
1153 mount = find_mount(root_name);
1154 retval = get_retval(close(fi->fh));
1158 fd_list_remove(&mount->fd_list, fi->fh);
1159 update_auto_unmount(mount);
1166 static int afuse_fsync(const char *path, int isdatasync,
1167 struct fuse_file_info *fi)
1172 #ifndef HAVE_FDATASYNC
1176 res = fdatasync(fi->fh);
1179 res = fsync(fi->fh);
1180 return get_retval(res);
1183 #if FUSE_VERSION >= 25
1184 static int afuse_access(const char *path, int mask)
1186 char *root_name = alloca( strlen(path) );
1187 char *real_path = alloca( max_path_out_len(path) );
1188 mount_list_t *mount;
1192 switch( process_path(path, real_path, root_name, 1, &mount) )
1194 case PROC_PATH_FAILED:
1197 case PROC_PATH_ROOT_DIR:
1198 case PROC_PATH_PROXY_DIR:
1199 retval = get_retval(access(real_path, mask));
1203 update_auto_unmount(mount);
1208 static int afuse_ftruncate(const char *path, off_t size,
1209 struct fuse_file_info *fi)
1212 return get_retval(ftruncate(fi->fh, size));
1215 static int afuse_create(const char *path, mode_t mode, struct fuse_file_info *fi)
1218 char *root_name = alloca( strlen(path) );
1219 char *real_path = alloca( max_path_out_len(path) );
1220 mount_list_t *mount;
1224 switch( process_path(path, real_path, root_name, 0, &mount) )
1226 case PROC_PATH_FAILED:
1229 case PROC_PATH_ROOT_DIR:
1232 case PROC_PATH_PROXY_DIR:
1233 fd = open(real_path, fi->flags, mode);
1244 update_auto_unmount(mount);
1249 static int afuse_fgetattr(const char *path, struct stat *stbuf,
1250 struct fuse_file_info *fi)
1254 return get_retval(fstat(fi->fh, stbuf));
1259 #if FUSE_VERSION >= 25
1260 static int afuse_statfs(const char *path, struct statvfs *stbuf)
1262 static int afuse_statfs(const char *path, struct statfs *stbuf)
1265 char *root_name = alloca( strlen(path) );
1266 char *real_path = alloca( max_path_out_len(path) );
1267 mount_list_t *mount;
1271 switch( process_path(path, real_path, root_name, 1, &mount) )
1273 case PROC_PATH_FAILED:
1277 case PROC_PATH_ROOT_DIR:
1278 #if FUSE_VERSION >= 25
1279 stbuf->f_namemax = 0x7fffffff;
1280 stbuf->f_frsize = 512;
1282 stbuf->f_namelen = 0x7fffffff;
1284 stbuf->f_bsize = 1024;
1285 stbuf->f_blocks = 0;
1287 stbuf->f_bavail = 0;
1293 case PROC_PATH_PROXY_DIR:
1294 retval = get_retval(statvfs(real_path, stbuf));
1298 update_auto_unmount(mount);
1303 void afuse_destroy(void *p)
1308 #ifdef HAVE_SETXATTR
1309 /* xattr operations are optional and can safely be left unimplemented */
1310 static int afuse_setxattr(const char *path, const char *name, const char *value,
1311 size_t size, int flags)
1313 char *root_name = alloca( strlen(path) );
1314 char *real_path = alloca( max_path_out_len(path) );
1315 mount_list_t *mount;
1319 switch( process_path(path, real_path, root_name, 0, &mount) )
1321 case PROC_PATH_FAILED:
1324 case PROC_PATH_ROOT_DIR:
1327 case PROC_PATH_PROXY_DIR:
1328 retval = get_retval(lsetxattr(real_path, name, value, size, flags));
1332 update_auto_unmount(mount);
1337 static int afuse_getxattr(const char *path, const char *name, char *value,
1340 char *root_name = alloca( strlen(path) );
1341 char *real_path = alloca( max_path_out_len(path) );
1342 mount_list_t *mount;
1346 switch( process_path(path, real_path, root_name, 1, &mount) )
1348 case PROC_PATH_FAILED:
1351 case PROC_PATH_ROOT_DIR:
1354 case PROC_PATH_PROXY_DIR:
1355 retval = get_retval(lgetxattr(real_path, name, value, size));
1359 update_auto_unmount(mount);
1364 static int afuse_listxattr(const char *path, char *list, size_t size)
1366 char *root_name = alloca( strlen(path) );
1367 char *real_path = alloca( max_path_out_len(path) );
1368 mount_list_t *mount;
1372 switch( process_path(path, real_path, root_name, 1, &mount) )
1374 case PROC_PATH_FAILED:
1377 case PROC_PATH_ROOT_DIR:
1380 case PROC_PATH_PROXY_DIR:
1381 retval = get_retval(llistxattr(real_path, list, size));
1385 update_auto_unmount(mount);
1390 static int afuse_removexattr(const char *path, const char *name)
1392 char *root_name = alloca( strlen(path) );
1393 char *real_path = alloca( max_path_out_len(path) );
1394 mount_list_t *mount;
1398 switch( process_path(path, real_path, root_name, 0, &mount) )
1400 case PROC_PATH_FAILED:
1403 case PROC_PATH_ROOT_DIR:
1406 case PROC_PATH_PROXY_DIR:
1407 retval = get_retval(lremovexattr(real_path, name));
1411 update_auto_unmount(mount);
1415 #endif /* HAVE_SETXATTR */
1417 static struct fuse_operations afuse_oper = {
1418 .getattr = afuse_getattr,
1419 .readlink = afuse_readlink,
1420 .opendir = afuse_opendir,
1421 .readdir = afuse_readdir,
1422 .releasedir = afuse_releasedir,
1423 .mknod = afuse_mknod,
1424 .mkdir = afuse_mkdir,
1425 .symlink = afuse_symlink,
1426 .unlink = afuse_unlink,
1427 .rmdir = afuse_rmdir,
1428 .rename = afuse_rename,
1430 .chmod = afuse_chmod,
1431 .chown = afuse_chown,
1432 .truncate = afuse_truncate,
1433 .utime = afuse_utime,
1436 .write = afuse_write,
1437 .release = afuse_release,
1438 .fsync = afuse_fsync,
1439 .statfs = afuse_statfs,
1440 #if FUSE_VERSION >= 25
1441 .access = afuse_access,
1442 .create = afuse_create,
1443 .ftruncate = afuse_ftruncate,
1444 .fgetattr = afuse_fgetattr,
1446 .destroy = afuse_destroy,
1447 #ifdef HAVE_SETXATTR
1448 .setxattr = afuse_setxattr,
1449 .getxattr = afuse_getxattr,
1450 .listxattr = afuse_listxattr,
1451 .removexattr = afuse_removexattr,
1461 #define AFUSE_OPT(t, p, v) { t, offsetof(struct user_options_t, p), v }
1463 static struct fuse_opt afuse_opts[] = {
1464 AFUSE_OPT("mount_template=%s", mount_command_template, 0),
1465 AFUSE_OPT("unmount_template=%s", unmount_command_template, 0),
1467 AFUSE_OPT("timeout=%Lu", auto_unmount_delay, 0),
1469 FUSE_OPT_KEY("flushwrites", KEY_FLUSHWRITES),
1470 FUSE_OPT_KEY("-h", KEY_HELP),
1471 FUSE_OPT_KEY("--help", KEY_HELP),
1476 static void usage(const char *progname)
1479 "Usage: %s mountpoint [options]\n"
1481 " -o opt,[opt...] mount options\n"
1482 " -h --help print help\n"
1483 " -V --version print FUSE version information\n"
1486 " -o mount_template=CMD template for CMD to execute to mount (*)\n"
1487 " -o unmount_template=CMD template for CMD to execute to unmount (*) (**)\n"
1488 " -o timeout=TIMEOUT automatically unmount after TIMEOUT seconds\n"
1489 " -o flushwrites flushes data to disk for all file writes\n"
1491 " (*) - When executed, %%r and %%m are expanded in templates to the root\n"
1492 " directory name for the new mount point, and the actual directory to\n"
1493 " mount onto respectively to mount onto. Both templates are REQUIRED.\n"
1495 " (**) - The unmount command must perform a lazy unmount operation. E.g. the\n"
1496 " -u -z options to fusermount, or -l for regular mount.\n"
1500 static int afuse_opt_proc(void *data, const char *arg, int key,
1501 struct fuse_args *outargs)
1508 usage(outargs->argv[0]);
1509 fuse_opt_add_arg(outargs, "-ho");
1510 fuse_main(outargs->argc, outargs->argv, &afuse_oper);
1513 case KEY_FLUSHWRITES:
1514 user_options.flush_writes = true;
1522 int main(int argc, char *argv[])
1524 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
1525 char *temp_dir_name = my_malloc(strlen(TMP_DIR_TEMPLATE));
1526 strcpy(temp_dir_name, TMP_DIR_TEMPLATE);
1528 if(fuse_opt_parse(&args, &user_options, afuse_opts, afuse_opt_proc) == -1)
1531 // !!FIXME!! force single-threading for now as data structures are not locked
1532 fuse_opt_add_arg(&args, "-s");
1534 // Adjust user specified timeout from seconds to microseconds as required
1535 user_options.auto_unmount_delay *= 1000000;
1537 auto_unmount_ph_init(&auto_unmount_ph);
1540 * Install the SIGALRM signal handler.
1543 struct sigaction act;
1544 act.sa_handler = &handle_auto_unmount_timer;
1545 sigemptyset(&act.sa_mask);
1547 while (sigaction(SIGALRM, &act, NULL) == -1 && errno == EINTR)
1551 // Check for required parameters
1552 if(!user_options.mount_command_template || !user_options.unmount_command_template) {
1553 fprintf(stderr, "(Un)Mount command templates missing.\n\n");
1555 fuse_opt_add_arg(&args, "-ho");
1556 fuse_main(args.argc, args.argv, &afuse_oper);
1561 if( !(mount_point_directory = mkdtemp(temp_dir_name)) ) {
1562 fprintf(stderr, "Failed to create temporary mount point dir.\n");
1568 if (lstat(mount_point_directory, &buf) == -1) {
1569 fprintf(stderr, "Failed to stat temporary mount point dir.\n");
1572 mount_point_dev = buf.st_dev;
1578 // !!FIXME!! death by signal doesn't unmount fs
1579 return fuse_main(args.argc, args.argv, &afuse_oper);