2 * client FUSE interface for libcaptive
3 * Copyright (C) 2005 Jan Kratochvil <project-captive@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <captive/client-vfs.h>
30 #include <captive/macros.h>
31 #include <captive/client.h>
32 #include <captive/captivemodid.h>
34 #include "main.h" /* self */
35 #include "op_statfs.h"
37 #include "op_fsyncdir.h"
38 #include "op_opendir.h"
39 #include "op_readdir.h"
40 #include "op_releasedir.h"
43 #include "op_release.h"
44 #include "op_getattr.h"
46 #include "op_unlink.h"
50 #include "op_truncate.h"
52 #include "op_rename.h"
57 /* FIXME: Dupe with libcaptive/client/options.c */
58 #define DEFAULT_SYSLOG_FACILITY LOG_DAEMON
59 /* Each element must be preceded by a comma (',')! */
60 #define LIBFUSE_ADDONS ",default_permissions,kernel_cache"
63 CaptiveVfsObject *capfuse_captive_vfs_object;
65 static const struct poptOption popt_table[]={
71 static const struct fuse_operations capfuse_operations={
74 fsyncdir: op_fsyncdir,
77 releasedir: op_releasedir,
87 truncate: op_truncate,
93 /* argv[0] expected as the program name. */
94 /* Only options and mountpoint expected here. */
95 static void capfuse_run(int argc,const char **argv)
97 char *capfuse_mountpoint;
98 int capfuse_multithreaded,capfuse_fd;
99 struct fuse *capfuse_fuse;
101 if (!(capfuse_fuse=fuse_setup(
103 (/*de-const; broken fuset_setup()*/char **)argv, /* argv */
104 &capfuse_operations, /* op */
105 sizeof(capfuse_operations), /* op_size */
106 &capfuse_mountpoint, /* mountpoint */
107 &capfuse_multithreaded, /* multithreaded */
108 &capfuse_fd))) /* fd */
109 g_error(_("FUSE fuse_setup() failed - you may need Linux kernel 2.6.14 or higher with its 'fuse.ko' enabled"));
110 if (fuse_loop(capfuse_fuse)) {
111 /* Do not: g_error(_("FUSE fuse_loop() error"));
112 * as it is caused on each umount(8).
116 fuse_teardown(capfuse_fuse,capfuse_fd,capfuse_mountpoint);
119 int main(int argc,char **argv)
121 poptContext context=NULL; /* Valid if: opt_o; '= NULL' to shut up GCC. */
123 const char **rest_argv,**csp;
124 struct captive_options options;
126 const char **capfuse_argv;
127 const char *program_name=argv[0];
128 const char *sandbox_server_argv0=G_STRINGIFY(LIBEXECDIR) "/captive-sandbox-server";
129 const char *image_filename=NULL;
130 gboolean opt_n=FALSE;
131 gboolean opt_v=FALSE;
132 const char *opt_o=NULL;
133 const char *mountpoint=NULL;
135 int opt_o_argc; /* Valid if: opt_o */
136 const char **opt_o_argv=NULL; /* Valid if: opt_o */
141 for (sp=argv;*sp;sp++)
142 fprintf(stderr,"argv: \"%s\"\n",*sp);
146 g_log_set_always_fatal(~(0
152 captive_standalone_init();
154 /* poptGetNextOpt() below requires valid: captive_options */
155 captive_options_init(&options);
156 captive_options=&options; /* for parsing by 'CAPTIVE_POPT_INCLUDE' */
159 if (*argv_sp && (*argv_sp)[0]!='-')
160 image_filename=*argv_sp++;
161 if (*argv_sp && (*argv_sp)[0]!='-')
162 mountpoint=*argv_sp++;
163 if (*argv_sp && !strcmp(*argv_sp,"-n")) {
167 if (*argv_sp && !strcmp(*argv_sp,"-v")) {
171 if (argv_sp[0] && argv_sp[1] && !strcmp(argv_sp[0],"-o")) {
176 /* Lethal path but still give chance for "--help" etc. */
177 /* Do not: POPT_CONTEXT_POSIXMEHARDER
178 * as mount(8) puts there first un-pre-dashed "ro"/"rw" etc. */
179 /* poptGetNextOpt() requires valid: captive_options */
180 if ((context=poptGetContext(
182 argc,(/*en-const*/const char **)argv, /* argc,argv */
183 popt_table, /* options */
184 0))) /* flags; && !POPT_CONTEXT_KEEP_FIRST */
185 while (0<=poptGetNextOpt(context));
186 g_error(_("Excessive argument: %s"),*argv_sp);
190 char *opt_o_spaced=(char */* de-const; length is not changed */)captive_strdup_alloca(opt_o);
194 for (s=opt_o_spaced;*s;s++) {
195 /* Be compatible with: poptParseArgvString()
196 * which also does not differentiate '"' and "'" */
197 if (*s=='\\' && s[1]) {
216 if (poptParseArgvString(opt_o_spaced,&opt_o_argc,&opt_o_argv))
217 g_error(_("Error splitting arguments of the space-reparsed '-o': %s"),opt_o_spaced);
218 /* Pre-dash "ro"/"rw" to let libcaptive to be aware of the mode. */
219 for (csp=opt_o_argv;*csp;csp++) {
220 /* Unsupported options not valid for fusermount(8) and causing:
221 * fusermount: mount failed: Invalid argument */
223 || !strcmp(*csp,"defaults")
224 || !strcmp(*csp, "auto")
225 || !strcmp(*csp,"noauto")) {
226 memmove(csp,csp+1,(opt_o_argv+opt_o_argc+1-(csp+1))*sizeof(*csp));
231 if (!strcmp(*csp,"ro"))
233 if (!strcmp(*csp,"rw"))
238 g_assert(!options.sandbox_server_argv);
239 g_assert(!options.sandbox_server_ior);
240 /* captive_options_free(&options) will: g_free(options.sandbox_server_argv); */
241 /* Allocation is so terrible to be compatible with: captive_options_copy() */
242 options.sandbox_server_argv=g_malloc(2*sizeof(*options.sandbox_server_argv)+strlen(sandbox_server_argv0)+1);
243 options.sandbox_server_argv[0]=(char *)(options.sandbox_server_argv+2);
244 options.sandbox_server_argv[1]=NULL;
245 strcpy(options.sandbox_server_argv[0],sandbox_server_argv0);
246 options.sandbox=TRUE;
248 g_assert(!options.bug_pathname);
249 options.bug_pathname=g_strdup(G_STRINGIFY(VARLIBCAPTIVEDIR) "/bug-%FT%T.captivebug.xml.gz");
251 options.syslog_facility=DEFAULT_SYSLOG_FACILITY;
254 /* Do not: POPT_CONTEXT_POSIXMEHARDER
255 * as mount(8) puts there first un-pre-dashed "ro"/"rw" etc. */
256 if (!(context=poptGetContext(
258 opt_o_argc,opt_o_argv, /* argc,argv */
259 popt_table, /* options */
260 POPT_CONTEXT_KEEP_FIRST))) /* flags */
261 g_error(_("Error parsing command-line arguments from pre-parsed '-o': %s"),opt_o);
262 if (poptReadDefaultConfig(context,
264 g_warning(_("Error reading default popt configuration"));
265 while (0<=poptGetNextOpt(context));
266 rest_argv=poptGetArgs(context);
271 for (csp=rest_argv;csp && *csp;csp++)
274 if (!image_filename || !mountpoint)
275 g_error(_("File/device disk image pathname and mountpoint command-line arguments required"));
277 /* image_iochannel */
278 g_assert(options.image_iochannel==NULL);
279 if (!(options.image_iochannel=g_io_channel_new_file(
280 image_filename, /* filename */
281 (options.rwmode==CAPTIVE_OPTION_RWMODE_RW ? "r+" : "r"), /* mode */
282 NULL))) { /* error */
283 g_error(_("image_iochannel failed open of: %s"),image_filename);
287 if (!options.captivemodid)
288 options.captivemodid=captive_captivemodid_load_default(FALSE);
290 if (options.filesystem.type==CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY) {
291 const char *self_prefix="mount.captive-";
292 size_t self_prefix_len=strlen(self_prefix);
295 if ((fsname=strrchr(program_name,'/')))
299 if (strncmp(fsname,self_prefix,self_prefix_len))
300 g_error(_("Cannot detected default filesystem name from my basename: %s"),fsname);
301 fsname+=self_prefix_len;
302 if (!captive_options_module_load(&options.filesystem,
303 captive_printf_alloca("%s/%s.sys",G_STRINGIFY(VARLIBCAPTIVEDIR),fsname)))
304 g_error(_("'--filesystem' option requires valid pathname ('ntfs.sys' suggested)"));
305 g_assert(options.filesystem.type!=CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY);
307 if (!options.load_module) {
308 struct captive_options_module *options_module;
310 captive_new(options_module);
311 if (!captive_options_module_load(options_module,G_STRINGIFY(VARLIBCAPTIVEDIR) "/ntoskrnl.exe"))
312 g_error(_("'--load-module' option requires valid pathname ('ntoskrnl.exe' suggested)"));
314 options.load_module=g_list_append(options.load_module,options_module);
317 /* It is still required for: captive_options_module_load() */
318 captive_options=NULL; /* already parsed by 'CAPTIVE_POPT_INCLUDE' */
320 if (GNOME_VFS_OK!=captive_vfs_new(&capfuse_captive_vfs_object,&options)) {
321 g_error(_("captive_vfs_new() failed"));
324 captive_options_free(&options);
326 /* Simulate argv[0] there as it got cut by popt. */
327 capfuse_argc=4+2*rest_argc;
328 captive_newn_alloca(capfuse_argv,capfuse_argc+1);
333 *csp++=captive_printf_alloca("fsname=%s" LIBFUSE_ADDONS ",%s",
334 image_filename,(options.rwmode==CAPTIVE_OPTION_RWMODE_RO ? "ro" : "rw"));
344 for (csp=capfuse_argv;*csp;csp++)
345 fprintf(stderr,"capfuse_argv: \"%s\"\n",*csp);
349 /* FIXFUSE: fuse_main()/fuse_main_real() would be enough for Captive fuse but
350 * the public interface of fuse_main() is too broken.
352 capfuse_run(capfuse_argc,capfuse_argv);
354 /* 'rest_argv' gets cleared by 'poptFreeContext(context);' below */
356 poptFreeContext(context);
358 if (capfuse_captive_vfs_object) {
359 g_object_unref(capfuse_captive_vfs_object);
360 capfuse_captive_vfs_object=NULL;