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