2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
17 static char *progname
;
19 static char *xstrdup(const char *s
)
23 fprintf(stderr
, "%s: failed to allocate memory\n", progname
);
29 static void *xrealloc(void *oldptr
, size_t size
)
31 void *ptr
= realloc(oldptr
, size
);
33 fprintf(stderr
, "%s: failed to allocate memory\n", progname
);
39 static void add_arg(char **cmdp
, const char *opt
)
41 size_t optlen
= strlen(opt
);
42 size_t cmdlen
= *cmdp
? strlen(*cmdp
) : 0;
43 char *cmd
= xrealloc(*cmdp
, cmdlen
+ optlen
* 4 + 4);
64 static char *add_option(const char *opt
, char *options
)
66 int oldlen
= options
? strlen(options
) : 0;
68 options
= xrealloc(options
, oldlen
+ 1 + strlen(opt
) + 1);
78 int main(int argc
, char *argv
[])
82 const char *mountpoint
;
92 basename
= strrchr(argv
[0], '/');
98 if (strncmp(basename
, "mount.fuse.", 11) == 0)
100 if (strncmp(basename
, "mount.fuseblk.", 14) == 0)
101 type
= basename
+ 14;
103 if (type
&& !type
[0])
108 "usage: %s %s destination [-t type] [-o opt[,opts...]]\n",
109 progname
, type
? "source" : "type#[source]");
117 mountpoint
= argv
[2];
119 for (i
= 3; i
< argc
; i
++) {
120 if (strcmp(argv
[i
], "-v") == 0) {
122 } else if (strcmp(argv
[i
], "-t") == 0) {
127 "%s: missing argument to option '-t'\n",
132 if (strncmp(type
, "fuse.", 5) == 0)
134 else if (strncmp(type
, "fuseblk.", 8) == 0)
139 "%s: empty type given as argument to option '-t'\n",
143 } else if (strcmp(argv
[i
], "-o") == 0) {
150 opts
= xstrdup(argv
[i
]);
151 opt
= strtok(opts
, ",");
155 const char *ignore_opts
[] = { "",
163 if (strncmp(opt
, "setuid=", 7) == 0) {
164 setuid
= xstrdup(opt
+ 7);
167 for (j
= 0; ignore_opts
[j
]; j
++)
168 if (strcmp(opt
, ignore_opts
[j
]) == 0)
172 if (strcmp(opt
, "nodev") == 0)
174 else if (strcmp(opt
, "nosuid") == 0)
177 options
= add_option(opt
, options
);
179 opt
= strtok(NULL
, ",");
185 options
= add_option("dev", options
);
187 options
= add_option("suid", options
);
191 type
= xstrdup(source
);
192 source
= strchr(type
, '#');
196 fprintf(stderr
, "%s: empty filesystem type\n",
201 fprintf(stderr
, "%s: empty source\n", progname
);
206 add_arg(&command
, type
);
208 add_arg(&command
, source
);
209 add_arg(&command
, mountpoint
);
211 add_arg(&command
, "-o");
212 add_arg(&command
, options
);
215 if (setuid
&& setuid
[0]) {
216 char *sucommand
= command
;
218 add_arg(&command
, "su");
219 add_arg(&command
, "-");
220 add_arg(&command
, setuid
);
221 add_arg(&command
, "-c");
222 add_arg(&command
, sucommand
);
223 } else if (!getenv("HOME")) {
224 /* Hack to make filesystems work in the boot environment */
225 setenv("HOME", "/root", 0);
228 execl("/bin/sh", "/bin/sh", "-c", command
, NULL
);
229 fprintf(stderr
, "%s: failed to execute /bin/sh: %s\n", progname
,