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>
32 #include "main.h" /* self */
33 #include "op_statfs.h"
35 #include "op_fsyncdir.h"
36 #include "op_opendir.h"
37 #include "op_readdir.h"
38 #include "op_releasedir.h"
41 #include "op_release.h"
42 #include "op_getattr.h"
44 #include "op_unlink.h"
48 #include "op_truncate.h"
50 #include "op_rename.h"
55 /* FIXME: Dupe with libcaptive/client/options.c */
56 #define DEFAULT_SYSLOG_FACILITY LOG_DAEMON
57 /* Each element must be preceded by a comma (',')! */
58 #define LIBFUSE_ADDONS ",default_permissions,kernel_cache"
61 CaptiveVfsObject *capfuse_captive_vfs_object;
63 static const struct poptOption popt_table[]={
69 static const struct fuse_operations capfuse_operations={
72 fsyncdir: op_fsyncdir,
75 releasedir: op_releasedir,
85 truncate: op_truncate,
91 /* argv[0] expected as the program name. */
92 /* Only options and mountpoint expected here. */
93 static void capfuse_run(int argc,const char **argv)
95 char *capfuse_mountpoint;
96 int capfuse_multithreaded,capfuse_fd;
97 struct fuse *capfuse_fuse;
99 if (!(capfuse_fuse=fuse_setup(
101 (/*de-const; broken fuset_setup()*/char **)argv, /* argv */
102 &capfuse_operations, /* op */
103 sizeof(capfuse_operations), /* op_size */
104 &capfuse_mountpoint, /* mountpoint */
105 &capfuse_multithreaded, /* multithreaded */
106 &capfuse_fd))) /* fd */
107 g_error(_("FUSE fuse_setup() failed"));
108 if (fuse_loop(capfuse_fuse)) {
109 /* Do not: g_error(_("FUSE fuse_loop() error"));
110 * as it is caused on each umount(8).
114 fuse_teardown(capfuse_fuse,capfuse_fd,capfuse_mountpoint);
117 int main(int argc,char **argv)
122 const char **rest_argv,**csp;
123 struct captive_options options;
124 const char **capfuse_argv;
125 const char *program_name=argv[0];
126 const char *sandbox_server_argv0=G_STRINGIFY(LIBEXECDIR) "/captive-sandbox-server";
127 const char *image_filename;
129 g_log_set_always_fatal(~(0
135 /* Prevent output block buffering if redirecting stdout to file. */
136 setvbuf(stdout,(char *)NULL,_IONBF,0);
137 setvbuf(stderr,(char *)NULL,_IONBF,0);
139 /* Initialize the i18n stuff */
140 setlocale(LC_ALL,"");
141 bindtextdomain(PACKAGE,LOCALEDIR);
144 /* Initialize GObject subsystem of GLib. */
147 captive_options_init(&options);
148 captive_options=&options; /* for parsing by 'CAPTIVE_POPT_INCLUDE' */
150 g_assert(!options.sandbox_server_argv);
151 g_assert(!options.sandbox_server_ior);
152 /* captive_options_free(&options) will: g_free(options.sandbox_server_argv); */
153 /* Allocation is so terrible to be compatible with: captive_options_copy() */
154 options.sandbox_server_argv=g_malloc(2*sizeof(*options.sandbox_server_argv)+strlen(sandbox_server_argv0)+1);
155 options.sandbox_server_argv[0]=(char *)(options.sandbox_server_argv+2);
156 options.sandbox_server_argv[1]=NULL;
157 strcpy(options.sandbox_server_argv[0],sandbox_server_argv0);
158 options.sandbox=TRUE;
160 g_assert(!options.bug_pathname);
161 options.bug_pathname=g_strdup(G_STRINGIFY(VARLIBCAPTIVEDIR) "/bug-%FT%T.captivebug.xml.gz");
163 options.syslog_facility=DEFAULT_SYSLOG_FACILITY;
165 context=poptGetContext(
167 argc,(/*en-const*/const char **)argv, /* argc,argv */
168 popt_table, /* options */
169 POPT_CONTEXT_POSIXMEHARDER); /* flags; && !POPT_CONTEXT_KEEP_FIRST */
171 g_error(_("Error parsing command-line arguments"));
174 errint=poptReadDefaultConfig(context,
177 g_warning(_("Error reading default popt configuration"));
178 errint=poptGetNextOpt(context);
180 g_error(_("Error parsing (dash-prefixed) command-line argument"));
183 rest_argv=poptGetArgs(context);
184 for (csp=rest_argv,rest_argc=0;csp && *csp;csp++)
187 captive_options=NULL; /* already parsed by 'CAPTIVE_POPT_INCLUDE' */
189 /* Override the (default) Captive options with mount(8) supplied "-o" argument. */
190 /* rest_argv[0] is the device now. */
191 /* rest_argv[1] is the mountpoint now. */
192 if (rest_argc>=4 && !strcmp(rest_argv[2],"-o")) {
193 const char *cs=rest_argv[3];
196 if ((!strncmp(cs,"ro",2) || !strncmp(cs,"rw",2)) && (cs[2]==',' || !cs[2])) {
197 if (!strncmp(cs,"ro",2))
198 options.rwmode=CAPTIVE_OPTION_RWMODE_RO;
199 if (!strncmp(cs,"rw",2))
200 options.rwmode=CAPTIVE_OPTION_RWMODE_RW;
202 if ((cs=strchr(cs,',')))
207 /* image_iochannel */
209 g_error(_("File/device disk image pathname command-line argument required"));
212 g_assert(options.image_iochannel==NULL);
213 image_filename=rest_argv[0];
216 if (!(options.image_iochannel=g_io_channel_new_file(
217 image_filename, /* filename */
218 (options.rwmode==CAPTIVE_OPTION_RWMODE_RW ? "r+" : "r"), /* mode */
219 NULL))) { /* error */
220 g_error(_("image_iochannel failed open of: %s"),image_filename);
224 if (options.filesystem.type==CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY) {
225 const char *self_prefix="mount.captive-";
226 size_t self_prefix_len=strlen(self_prefix);
229 if ((fsname=strrchr(program_name,'/')))
233 if (strncmp(fsname,self_prefix,self_prefix_len))
234 g_error(_("Cannot detected default filesystem name from my basename: %s"),fsname);
235 fsname+=self_prefix_len;
236 if (!captive_options_module_load(&options.filesystem,
237 captive_printf_alloca("%s/%s.sys",G_STRINGIFY(VARLIBCAPTIVEDIR),fsname)))
238 g_error(_("'--filesystem' option requires valid pathname ('ntfs.sys' suggested)"));
239 g_assert(options.filesystem.type!=CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY);
241 if (!options.load_module) {
242 struct captive_options_module *options_module;
244 captive_new(options_module);
245 if (!captive_options_module_load(options_module,G_STRINGIFY(VARLIBCAPTIVEDIR) "/ntoskrnl.exe"))
246 g_error(_("'--load-module' option requires valid pathname ('ntoskrnl.exe' suggested)"));
248 options.load_module=g_list_append(options.load_module,options_module);
251 if (GNOME_VFS_OK!=captive_vfs_new(&capfuse_captive_vfs_object,&options)) {
252 g_error(_("captive_vfs_new() failed"));
255 captive_options_free(&options);
257 /* Simulate argv[0] there as it got cut by popt. */
258 captive_newn_alloca(capfuse_argv,1+rest_argc+1+2);
259 capfuse_argv[0]=argv[0];
260 memcpy(capfuse_argv+1,rest_argv,sizeof(*rest_argv)*(rest_argc+1));
262 for (i=1;capfuse_argv[i];i++) {
263 if (strcmp(capfuse_argv[i],"-o"))
265 capfuse_argv[i+1]=captive_printf_alloca("fsname=%s" LIBFUSE_ADDONS ",%s",image_filename,capfuse_argv[i+1]);
268 if (!capfuse_argv[i]) {
269 capfuse_argv[i ]="-o";
270 capfuse_argv[i+1]=captive_printf_alloca("fsname=%s" LIBFUSE_ADDONS,image_filename);
271 capfuse_argv[i+2]=NULL;
274 /* FIXFUSE: fuse_main()/fuse_main_real() would be enough for Captive fuse but
275 * the public interface of fuse_main() is too broken.
277 capfuse_run(1+rest_argc,capfuse_argv);
279 /* 'rest_argv' gets cleared by 'poptFreeContext(context);' below */
280 poptFreeContext(context);
282 if (capfuse_captive_vfs_object) {
283 g_object_unref(capfuse_captive_vfs_object);
284 capfuse_captive_vfs_object=NULL;