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
59 CaptiveVfsObject *capfuse_captive_vfs_object;
61 static const struct poptOption popt_table[]={
67 static const struct fuse_operations capfuse_operations={
70 fsyncdir: op_fsyncdir,
73 releasedir: op_releasedir,
83 truncate: op_truncate,
89 /* argv[0] expected as the program name. */
90 /* Only options and mountpoint expected here. */
91 static void capfuse_run(int argc,const char **argv)
93 char *capfuse_mountpoint;
94 int capfuse_multithreaded,capfuse_fd;
95 struct fuse *capfuse_fuse;
97 if (!(capfuse_fuse=fuse_setup(
99 (/*de-const; broken fuset_setup()*/char **)argv, /* argv */
100 &capfuse_operations, /* op */
101 sizeof(capfuse_operations), /* op_size */
102 &capfuse_mountpoint, /* mountpoint */
103 &capfuse_multithreaded, /* multithreaded */
104 &capfuse_fd))) /* fd */
105 g_error(_("FUSE fuse_setup() failed"));
106 if (fuse_loop(capfuse_fuse)) {
107 /* Do not: g_error(_("FUSE fuse_loop() error"));
108 * as it is caused on each umount(8).
112 fuse_teardown(capfuse_fuse,capfuse_fd,capfuse_mountpoint);
115 int main(int argc,char **argv)
120 const char **rest_argv,**csp;
121 struct captive_options options;
122 const char **capfuse_argv;
123 const char *program_name=argv[0];
124 const char *sandbox_server_argv0=G_STRINGIFY(SBINDIR) "/captive-sandbox-server";
126 g_log_set_always_fatal(~(0
132 /* Prevent output block buffering if redirecting stdout to file. */
133 setvbuf(stdout,(char *)NULL,_IONBF,0);
134 setvbuf(stderr,(char *)NULL,_IONBF,0);
136 /* Initialize the i18n stuff */
137 setlocale(LC_ALL,"");
138 bindtextdomain(PACKAGE,LOCALEDIR);
141 /* Initialize GObject subsystem of GLib. */
144 struct captive_options {
145 struct captive_options_module filesystem;
146 enum captive_option_rwmode rwmode;
147 enum captive_option_media media;
148 gboolean debug_messages;
149 GIOChannel *image_iochannel;
150 GList *load_module; /* of 'struct captive_options_module *' */
152 char **sandbox_server_argv;
153 gchar *sandbox_server_ior;
155 int syslog_facility; /* LOG_*; -1 if not used */
158 captive_options_init(&options);
159 captive_options=&options; /* for parsing by 'CAPTIVE_POPT_INCLUDE' */
161 g_assert(!options.sandbox_server_argv);
162 g_assert(!options.sandbox_server_ior);
163 /* captive_options_free(&options) will: g_free(options.sandbox_server_argv); */
164 /* Allocation is so terrible to be compatible with: captive_options_copy() */
165 options.sandbox_server_argv=g_malloc(2*sizeof(*options.sandbox_server_argv)+strlen(sandbox_server_argv0)+1);
166 options.sandbox_server_argv[0]=(char *)(options.sandbox_server_argv+2);
167 options.sandbox_server_argv[1]=NULL;
168 strcpy(options.sandbox_server_argv[0],sandbox_server_argv0);
169 options.sandbox=TRUE;
171 g_assert(!options.bug_pathname);
172 options.bug_pathname=g_strdup(G_STRINGIFY(VARLIBCAPTIVEDIR) "/bug-%FT%T.captivebug.xml.gz");
174 options.syslog_facility=DEFAULT_SYSLOG_FACILITY;
176 context=poptGetContext(
178 argc,(/*en-const*/const char **)argv, /* argc,argv */
179 popt_table, /* options */
180 POPT_CONTEXT_POSIXMEHARDER); /* flags; && !POPT_CONTEXT_KEEP_FIRST */
182 g_error(_("Error parsing command-line arguments"));
185 errint=poptReadDefaultConfig(context,
188 g_warning(_("Error reading default popt configuration"));
189 errint=poptGetNextOpt(context);
191 g_error(_("Error parsing (dash-prefixed) command-line argument"));
194 rest_argv=poptGetArgs(context);
195 for (csp=rest_argv,rest_argc=0;csp && *csp;csp++)
198 captive_options=NULL; /* already parsed by 'CAPTIVE_POPT_INCLUDE' */
200 /* image_iochannel */
202 g_error(_("File/device disk image pathname command-line argument required"));
205 g_assert(options.image_iochannel==NULL);
206 if (!(options.image_iochannel=g_io_channel_new_file(
207 rest_argv[0], /* filename */
208 (options.rwmode==CAPTIVE_OPTION_RWMODE_RW ? "r+" : "r"), /* mode */
209 NULL))) { /* error */
210 g_error(_("image_iochannel failed open of: %s"),rest_argv[0]);
214 if (options.filesystem.type==CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY) {
215 const char *self_prefix="mount.captive-";
216 size_t self_prefix_len=strlen(self_prefix);
219 if ((fsname=strrchr(program_name,'/')))
223 if (strncmp(fsname,self_prefix,self_prefix_len))
224 g_error(_("Cannot detected default filesystem name from my basename: %s"),fsname);
225 fsname+=self_prefix_len;
226 if (!captive_options_module_load(&options.filesystem,
227 captive_printf_alloca("%s/%s.sys",G_STRINGIFY(VARLIBCAPTIVEDIR),fsname)))
228 g_error(_("'--filesystem' option requires valid pathname ('ntfs.sys' suggested)"));
229 g_assert(options.filesystem.type!=CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY);
231 if (!options.load_module) {
232 struct captive_options_module *options_module;
234 captive_new(options_module);
235 if (!captive_options_module_load(options_module,G_STRINGIFY(VARLIBCAPTIVEDIR) "/ntoskrnl.exe"))
236 g_error(_("'--load-module' option requires valid pathname ('ntoskrnl.exe' suggested)"));
238 options.load_module=g_list_append(options.load_module,options_module);
241 if (GNOME_VFS_OK!=captive_vfs_new(&capfuse_captive_vfs_object,&options)) {
242 g_error(_("captive_vfs_new() failed"));
245 captive_options_free(&options);
249 /* Simulate argv[0] there as it got cut by popt. */
250 captive_newn_alloca(capfuse_argv,1+rest_argc+1);
251 capfuse_argv[0]=argv[0];
252 memcpy(capfuse_argv+1,rest_argv,sizeof(*rest_argv)*(rest_argc+1));
254 /* FIXFUSE: fuse_main()/fuse_main_real() would be enough for Captive fuse but
255 * the public interface of fuse_main() is too broken.
257 capfuse_run(1+rest_argc,capfuse_argv);
259 /* 'rest_argv' gets cleared by 'poptFreeContext(context);' below */
260 poptFreeContext(context);
262 if (capfuse_captive_vfs_object) {
263 g_object_unref(capfuse_captive_vfs_object);
264 capfuse_captive_vfs_object=NULL;