+FUSE interface skeleton.
[captive.git] / src / client / fuse / main.c
1 /* $Id$
2  * client FUSE interface for libcaptive
3  * Copyright (C) 2005 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
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
8  * 
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.
13  * 
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
17  */
18
19
20 #include "config.h"
21
22 #include <fuse.h>
23 #include <popt.h>
24 #include <locale.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <captive/client-vfs.h>
29 #include <captive/macros.h>
30
31 #include "main.h"       /* self */
32 #include "op_statfs.h"
33 #include "op_fsync.h"
34 #include "op_fsyncdir.h"
35
36
37 CaptiveVfsObject *capfuse_captive_vfs_object;
38
39 static const struct poptOption popt_table[]={
40         CAPTIVE_POPT_INCLUDE,
41         POPT_AUTOHELP
42         POPT_TABLEEND
43         };
44
45 static const struct fuse_operations capfuse_operations={
46         statfs:     op_statfs,
47         fsync:      op_fsync,
48         fsyncdir:   op_fsyncdir,
49 #if 0
50         getattr:    op_getattr,
51         mknod:      op_mknod,
52         mkdir:      op_mkdir,
53         unlink:     op_unlink,
54         rmdir:      op_rmdir,
55         rename:     op_rename,
56         chmod:      op_chmod,
57         truncate:   op_truncate,
58         utime:      op_utime,
59         open:       op_open,
60         read:       op_read,
61         write:      op_write,
62         flush:      op_flush,
63         release:    op_release,
64         opendir:    op_opendir,
65         readdir:    op_readdir,
66         releasedir: op_releasedir,
67 #endif
68         };
69
70 /* argv[0] expected as the program name. */
71 /* Only options and mountpoint expected here. */
72 static void capfuse_run(int argc,const char **argv)
73 {
74 char *capfuse_mountpoint;
75 int capfuse_multithreaded,capfuse_fd;
76 struct fuse *capfuse_fuse;
77
78         if (!(capfuse_fuse=fuse_setup(
79                                 argc,   /* argc */
80                                 (/*de-const; broken fuset_setup()*/char **)argv,        /* argv */
81                                 &capfuse_operations,    /* op */
82                                 sizeof(capfuse_operations),     /* op_size */
83                         &capfuse_mountpoint,    /* mountpoint */
84                         &capfuse_multithreaded, /* multithreaded */
85                         &capfuse_fd)))  /* fd */
86                 g_error(_("FUSE fuse_setup() failed"));
87         if (fuse_loop(capfuse_fuse))
88                 g_error(_("FUSE fuse_loop() error: %m"));
89         fuse_teardown(capfuse_fuse,capfuse_fd,capfuse_mountpoint);
90 }
91
92 int main(int argc,char **argv)
93 {
94 poptContext context;
95 int errint;
96 int rest_argc;
97 const char **rest_argv,**csp;
98 struct captive_options options;
99 const char **capfuse_argv;
100
101         g_log_set_always_fatal(~(0
102                         |G_LOG_LEVEL_MESSAGE
103                         |G_LOG_LEVEL_INFO
104                         |G_LOG_LEVEL_DEBUG
105                         ));
106
107         /* Prevent output block buffering if redirecting stdout to file. */
108         setvbuf(stdout,(char *)NULL,_IONBF,0);
109         setvbuf(stderr,(char *)NULL,_IONBF,0);
110
111         /* Initialize the i18n stuff */
112         setlocale(LC_ALL,"");
113         bindtextdomain(PACKAGE,LOCALEDIR);
114         textdomain(PACKAGE);
115
116         /* Initialize GObject subsystem of GLib. */
117         g_type_init();
118
119         captive_options_init(&options);
120         captive_options=&options;       /* for parsing by 'CAPTIVE_POPT_INCLUDE' */
121
122         context=poptGetContext(
123                         PACKAGE,        /* name */
124                         argc,(/*en-const*/const char **)argv,   /* argc,argv */
125                         popt_table,     /* options */
126                         POPT_CONTEXT_POSIXMEHARDER);    /* flags; && !POPT_CONTEXT_KEEP_FIRST */
127         if (context==NULL) {
128                 g_error(_("Error parsing command-line arguments"));
129                 return EXIT_FAILURE;
130                 }
131         errint=poptReadDefaultConfig(context,
132                         TRUE);  /* useEnv */
133         if (errint!=0)
134                 g_warning(_("Error reading default popt configuration"));
135         errint=poptGetNextOpt(context);
136         if (errint!=-1) {
137                 g_error(_("Error parsing (dash-prefixed) command-line argument"));
138                 return EXIT_FAILURE;
139                 }
140         rest_argv=poptGetArgs(context);
141         for (csp=rest_argv,rest_argc=0;csp && *csp;csp++)
142                 rest_argc++;
143
144         captive_options=NULL;   /* already parsed by 'CAPTIVE_POPT_INCLUDE' */
145
146         /* image_iochannel */
147         if (rest_argc<1) {
148                 g_error(_("File/device disk image pathname command-line argument required"));
149                 return EXIT_FAILURE;
150                 }
151         g_assert(options.image_iochannel==NULL);
152         if (!(options.image_iochannel=g_io_channel_new_file(
153                         rest_argv[0],   /* filename */
154                         (options.rwmode==CAPTIVE_OPTION_RWMODE_RW ? "r+" : "r"),        /* mode */
155                         NULL))) {       /* error */
156                 g_error(_("image_iochannel open failed"));
157                 return EXIT_FAILURE;
158                 }
159
160         if (options.filesystem.type==CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY) {
161                 g_error(_("'--filesystem' option required ('ntfs.sys' pathname suggested)"));
162                 return EXIT_FAILURE;
163                 }
164         if (!options.load_module) {
165                 g_warning(_("'--load-module' option required ('ntoskrnl.exe' pathname suggested)"));
166                 return EXIT_FAILURE;
167                 }
168
169         if (GNOME_VFS_OK!=captive_vfs_new(&capfuse_captive_vfs_object,&options)) {
170                 g_error(_("captive_vfs_new() failed"));
171                 return EXIT_FAILURE;
172                 }
173         captive_options_free(&options);
174         rest_argc--;
175         rest_argv++;
176
177         /* Simulate argv[0] there as it got cut by popt. */
178         captive_newn_alloca(capfuse_argv,1+rest_argc+1);
179         capfuse_argv[0]=argv[0];
180         memcpy(capfuse_argv+1,rest_argv,sizeof(*rest_argv)*(rest_argc+1));
181
182         /* FIXFUSE: fuse_main()/fuse_main_real() would be enough for Captive fuse but
183          * the public interface of fuse_main() is too broken.
184          */
185         capfuse_run(1+rest_argc,capfuse_argv);
186
187         /* 'rest_argv' gets cleared by 'poptFreeContext(context);' below */
188         poptFreeContext(context);
189
190         if (capfuse_captive_vfs_object) {
191                 g_object_unref(capfuse_captive_vfs_object);
192                 capfuse_captive_vfs_object=NULL;
193                 }
194
195         return EXIT_SUCCESS;
196 }