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