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>
33 #include "main.h" /* self */
34 #include "op_statfs.h"
36 #include "op_fsyncdir.h"
37 #include "op_opendir.h"
38 #include "op_readdir.h"
39 #include "op_releasedir.h"
42 #include "op_release.h"
43 #include "op_getattr.h"
45 #include "op_unlink.h"
49 #include "op_truncate.h"
51 #include "op_rename.h"
56 /* FIXME: Dupe with libcaptive/client/options.c */
57 #define DEFAULT_SYSLOG_FACILITY LOG_DAEMON
58 /* Each element must be preceded by a comma (',')! */
59 #define LIBFUSE_ADDONS ",default_permissions,kernel_cache"
62 CaptiveVfsObject *capfuse_captive_vfs_object;
64 static const struct poptOption popt_table[]={
70 static const struct fuse_operations capfuse_operations={
73 fsyncdir: op_fsyncdir,
76 releasedir: op_releasedir,
86 truncate: op_truncate,
92 /* argv[0] expected as the program name. */
93 /* Only options and mountpoint expected here. */
94 static void capfuse_run(int argc,const char **argv)
96 char *capfuse_mountpoint;
97 int capfuse_multithreaded,capfuse_fd;
98 struct fuse *capfuse_fuse;
100 if (!(capfuse_fuse=fuse_setup(
102 (/*de-const; broken fuset_setup()*/char **)argv, /* argv */
103 &capfuse_operations, /* op */
104 sizeof(capfuse_operations), /* op_size */
105 &capfuse_mountpoint, /* mountpoint */
106 &capfuse_multithreaded, /* multithreaded */
107 &capfuse_fd))) /* fd */
108 g_error(_("FUSE fuse_setup() failed"));
109 if (fuse_loop(capfuse_fuse)) {
110 /* Do not: g_error(_("FUSE fuse_loop() error"));
111 * as it is caused on each umount(8).
115 fuse_teardown(capfuse_fuse,capfuse_fd,capfuse_mountpoint);
118 int main(int argc,char **argv)
123 const char **rest_argv,**csp;
124 struct captive_options options;
125 const char **capfuse_argv;
126 const char *program_name=argv[0];
127 const char *sandbox_server_argv0=G_STRINGIFY(LIBEXECDIR) "/captive-sandbox-server";
128 const char *image_filename;
130 g_log_set_always_fatal(~(0
136 captive_standalone_init();
138 captive_options_init(&options);
139 captive_options=&options; /* for parsing by 'CAPTIVE_POPT_INCLUDE' */
141 g_assert(!options.sandbox_server_argv);
142 g_assert(!options.sandbox_server_ior);
143 /* captive_options_free(&options) will: g_free(options.sandbox_server_argv); */
144 /* Allocation is so terrible to be compatible with: captive_options_copy() */
145 options.sandbox_server_argv=g_malloc(2*sizeof(*options.sandbox_server_argv)+strlen(sandbox_server_argv0)+1);
146 options.sandbox_server_argv[0]=(char *)(options.sandbox_server_argv+2);
147 options.sandbox_server_argv[1]=NULL;
148 strcpy(options.sandbox_server_argv[0],sandbox_server_argv0);
149 options.sandbox=TRUE;
151 g_assert(!options.bug_pathname);
152 options.bug_pathname=g_strdup(G_STRINGIFY(VARLIBCAPTIVEDIR) "/bug-%FT%T.captivebug.xml.gz");
154 options.syslog_facility=DEFAULT_SYSLOG_FACILITY;
156 context=poptGetContext(
158 argc,(/*en-const*/const char **)argv, /* argc,argv */
159 popt_table, /* options */
160 POPT_CONTEXT_POSIXMEHARDER); /* flags; && !POPT_CONTEXT_KEEP_FIRST */
162 g_error(_("Error parsing command-line arguments"));
165 errint=poptReadDefaultConfig(context,
168 g_warning(_("Error reading default popt configuration"));
169 errint=poptGetNextOpt(context);
171 g_error(_("Error parsing (dash-prefixed) command-line argument"));
174 rest_argv=poptGetArgs(context);
175 for (csp=rest_argv,rest_argc=0;csp && *csp;csp++)
178 captive_options=NULL; /* already parsed by 'CAPTIVE_POPT_INCLUDE' */
180 /* Override the (default) Captive options with mount(8) supplied "-o" argument. */
181 /* rest_argv[0] is the device now. */
182 /* rest_argv[1] is the mountpoint now. */
183 if (rest_argc>=4 && !strcmp(rest_argv[2],"-o")) {
184 const char *cs=rest_argv[3];
187 if ((!strncmp(cs,"ro",2) || !strncmp(cs,"rw",2)) && (cs[2]==',' || !cs[2])) {
188 if (!strncmp(cs,"ro",2))
189 options.rwmode=CAPTIVE_OPTION_RWMODE_RO;
190 if (!strncmp(cs,"rw",2))
191 options.rwmode=CAPTIVE_OPTION_RWMODE_RW;
193 if ((cs=strchr(cs,',')))
198 /* image_iochannel */
200 g_error(_("File/device disk image pathname command-line argument required"));
203 g_assert(options.image_iochannel==NULL);
204 image_filename=rest_argv[0];
207 if (!(options.image_iochannel=g_io_channel_new_file(
208 image_filename, /* filename */
209 (options.rwmode==CAPTIVE_OPTION_RWMODE_RW ? "r+" : "r"), /* mode */
210 NULL))) { /* error */
211 g_error(_("image_iochannel failed open of: %s"),image_filename);
215 if (options.filesystem.type==CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY) {
216 const char *self_prefix="mount.captive-";
217 size_t self_prefix_len=strlen(self_prefix);
220 if ((fsname=strrchr(program_name,'/')))
224 if (strncmp(fsname,self_prefix,self_prefix_len))
225 g_error(_("Cannot detected default filesystem name from my basename: %s"),fsname);
226 fsname+=self_prefix_len;
227 if (!captive_options_module_load(&options.filesystem,
228 captive_printf_alloca("%s/%s.sys",G_STRINGIFY(VARLIBCAPTIVEDIR),fsname)))
229 g_error(_("'--filesystem' option requires valid pathname ('ntfs.sys' suggested)"));
230 g_assert(options.filesystem.type!=CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY);
232 if (!options.load_module) {
233 struct captive_options_module *options_module;
235 captive_new(options_module);
236 if (!captive_options_module_load(options_module,G_STRINGIFY(VARLIBCAPTIVEDIR) "/ntoskrnl.exe"))
237 g_error(_("'--load-module' option requires valid pathname ('ntoskrnl.exe' suggested)"));
239 options.load_module=g_list_append(options.load_module,options_module);
242 if (GNOME_VFS_OK!=captive_vfs_new(&capfuse_captive_vfs_object,&options)) {
243 g_error(_("captive_vfs_new() failed"));
246 captive_options_free(&options);
248 /* Simulate argv[0] there as it got cut by popt. */
249 captive_newn_alloca(capfuse_argv,1+rest_argc+1+2);
250 capfuse_argv[0]=argv[0];
251 memcpy(capfuse_argv+1,rest_argv,sizeof(*rest_argv)*(rest_argc+1));
253 for (i=1;capfuse_argv[i];i++) {
254 if (strcmp(capfuse_argv[i],"-o"))
256 capfuse_argv[i+1]=captive_printf_alloca("fsname=%s" LIBFUSE_ADDONS ",%s",image_filename,capfuse_argv[i+1]);
259 if (!capfuse_argv[i]) {
260 capfuse_argv[i ]="-o";
261 capfuse_argv[i+1]=captive_printf_alloca("fsname=%s" LIBFUSE_ADDONS,image_filename);
262 capfuse_argv[i+2]=NULL;
265 /* FIXFUSE: fuse_main()/fuse_main_real() would be enough for Captive fuse but
266 * the public interface of fuse_main() is too broken.
268 capfuse_run(1+rest_argc,capfuse_argv);
270 /* 'rest_argv' gets cleared by 'poptFreeContext(context);' below */
271 poptFreeContext(context);
273 if (capfuse_captive_vfs_object) {
274 g_object_unref(capfuse_captive_vfs_object);
275 capfuse_captive_vfs_object=NULL;