394376639e12075a09eacf045442277b83194410
[captive.git] / src / client / lufs / captivefs-vfs.c
1 /* $Id$
2  * lufs interface module vfs objects implementation for libcaptive
3  * Copyright (C) 2003 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 "captivefs-vfs.h"      /* self */
23 #include <glib/gmessages.h>
24 #include "captivefs-misc.h"
25 #include <unistd.h>
26
27 #include <captive/client-vfs.h>
28 #include <captive/options.h>
29 #include <captive/macros.h>
30
31 #include <lufs/fs.h>
32
33
34 gboolean captivefs_vfs_validate(struct captivefs_vfs *captivefs_vfs)
35 {
36         g_return_val_if_fail(captivefs_vfs!=NULL,FALSE);
37
38         G_LOCK(libcaptive);
39         if (!captivefs_vfs->captive_vfs_object) {
40 GnomeVFSResult errvfsresult;
41
42                 if (captivefs_vfs->parent_pid==getpid()) {
43                         G_UNLOCK(libcaptive);
44                         return FALSE;
45                         }
46
47                 errvfsresult=captive_vfs_new(&captivefs_vfs->captive_vfs_object,&captivefs_vfs->options);
48
49                 if (errvfsresult!=GNOME_VFS_OK) {
50                         G_UNLOCK(libcaptive);
51                         g_return_val_if_reached(FALSE);
52                         }
53                 }
54         if (!CAPTIVE_VFS_IS_OBJECT(captivefs_vfs->captive_vfs_object)) {
55                 G_UNLOCK(libcaptive);
56                 g_return_val_if_reached(FALSE);
57                 }
58         G_UNLOCK(libcaptive);
59
60         return TRUE;
61 }
62
63
64 /* Initialization
65  * Here we allocate a structure to hold all the file system local info 
66  * (localfs_local). This structure will then be passed as a parameter to 
67  * the other functions.
68  * global_ctx holds info about another structure that can be shared between all
69  * instances of the filesystem. If the pointer is NULL, then this is the
70  * first instance and the structure should be allocated.
71  * ! You must implement  locking/usage-count/deallocation logic when using
72  *   a global context. (locking is omited here)
73  * ! Most filesystems don't need a global context so you can safely ignore the
74  *   global_ctx parameter.  
75  */
76 struct captivefs_vfs *captivefs_init
77                 (struct list_head *cfg,struct dir_cache *cache,const struct credentials *cred,void **global_ctx)
78 {
79 struct captivefs_vfs *captivefs_vfs;
80 const struct poptOption *cpopt;
81 GPtrArray *arg_array;
82 const gchar *cgs;
83 poptContext context;
84 int errint;
85
86         /* Do not: g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_init");
87          * as we do not yet have 'VFS_DEBUG_MESSAGES(captive_vfs_object)' set.
88          * Generally we make all g_log() conditional here as we do not want to mess
89          * with overriden GLog handlers of libcaptive itself.
90          */
91
92         arg_array=g_ptr_array_new();
93         if (!(cgs=lu_opt_getchar(cfg,"MOUNT","fs")))
94                 cgs="captivefs";
95         g_ptr_array_add(arg_array,(/* de-const */ char *)cgs);
96         for (cpopt=captive_popt;cpopt->argInfo || cpopt->longName;cpopt++) {
97 const gchar *dashdashname,*value;
98
99                 if (!cpopt->longName)
100                         continue;
101                 dashdashname=captive_printf_alloca("--%s",cpopt->longName);
102                 if (1
103                                 && !(value=lu_opt_getchar(cfg,"MOUNT",(/* de-const */ char *)dashdashname))
104                                 && !(value=lu_opt_getchar(cfg,"MOUNT",(/* de-const */ char *)cpopt->longName)))
105                         continue;
106                 g_ptr_array_add(arg_array,(/* de-const */ char *)captive_printf_alloca("%s=%s",dashdashname,value));
107                 }
108         g_ptr_array_add(arg_array,NULL);
109         g_assert(arg_array->len>=2);
110         g_assert(arg_array->pdata[arg_array->len-2]!=NULL);
111         g_assert(arg_array->pdata[arg_array->len-1]==NULL);
112
113         /* Initialize GObject subsystem of GLib. */
114         g_type_init();
115
116         captive_new(captivefs_vfs);
117         captivefs_vfs->captive_vfs_object=NULL;
118         captivefs_vfs->parent_pid=getpid();
119
120         captive_options_init(&captivefs_vfs->options);
121         captive_options=&captivefs_vfs->options;        /* for parsing by 'CAPTIVE_POPT_INCLUDE' */
122
123         context=poptGetContext(
124                         PACKAGE,        /* name */
125                         arg_array->len-1,       /* argc */
126                         (const char **)arg_array->pdata,        /* argv */
127                         captive_popt,   /* options */
128                         POPT_CONTEXT_POSIXMEHARDER);    /* flags; && !POPT_CONTEXT_KEEP_FIRST */
129         g_ptr_array_free(arg_array,
130                         FALSE); /* free_seg */
131         if (context==NULL) {
132                 g_warning("%s: argument recognization args_error","captivefs_init");
133                 goto fail_free_options;
134                 }
135         errint=poptReadDefaultConfig(context,
136                         TRUE);  /* useEnv */
137         if (errint!=0) {
138                 g_warning("%s: argument recognization args_error","captivefs_init");
139                 goto fail_free_options;
140                 }
141         errint=poptGetNextOpt(context);
142         if (errint!=-1) {
143                 g_warning("%s: some non-callbacked argument reached","captivefs_init");
144                 goto fail_free_options;
145                 }
146         cgs=poptPeekArg(context);
147         if (cgs) {
148                 g_warning("%s: some non-option argument reached","captivefs_init");
149                 goto fail_free_options;
150                 }
151
152         captive_options=NULL;   /* already parsed by 'CAPTIVE_POPT_INCLUDE' */
153
154         if (captivefs_vfs->options.debug_messages)
155                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_init");
156
157         /* image_iochannel */
158         if ((cgs=lu_opt_getchar(cfg,"MOUNT","image"))) {
159                 g_assert(captivefs_vfs->options.image_iochannel==NULL);
160                 if (!(captivefs_vfs->options.image_iochannel=g_io_channel_new_file(
161                                 cgs,    /* filename */
162                                 (captivefs_vfs->options.rwmode==CAPTIVE_OPTION_RWMODE_RW ? "r+" : "r"), /* mode */
163                                 NULL))) {       /* error */
164                         g_warning(_("%s: image_iochannel open failed"),"captivefs_init");
165                         goto fail_free_options;
166                         }
167                 }
168
169         return captivefs_vfs;
170
171 fail_free_options:
172         captive_options_free(&captivefs_vfs->options);
173 /* fail: */
174         return NULL;
175 }
176
177
178 /* Cleanup
179  * Check the global context count and free it if necessary.
180  * Deallocate memory and free all resources.
181  */
182 void captivefs_free(struct captivefs_vfs *captivefs_vfs)
183 {
184         g_return_if_fail(captivefs_vfs_validate(captivefs_vfs));
185
186         if (captivefs_vfs->options.debug_messages)
187                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_free");
188
189         g_assert(G_OBJECT(captivefs_vfs->captive_vfs_object)->ref_count==1);
190
191         G_LOCK(libcaptive);
192         g_object_unref(captivefs_vfs->captive_vfs_object);
193         G_UNLOCK(libcaptive);
194
195         g_io_channel_unref(captivefs_vfs->options.image_iochannel);
196         captive_options_free(&captivefs_vfs->options);
197         g_free(captivefs_vfs);
198 }
199
200
201 /* Mount the file system.
202  * Called when a mount operation is performed.
203  * Initialize specific connections, login, etc.
204  *
205  * Notes:
206  *     By default, LUFS may attempt multiple connections at once.  If your
207  * filesystem doesn't support this, you need to specificy -c 1 on the
208  * lufsmount command line or connections=1 in the mount options.
209  *     See ftpfs for an example of how to read configuration options
210  * from a configuration file if you want to, for example, be able to set
211  * default values.
212  */
213 int captivefs_mount(struct captivefs_vfs *captivefs_vfs)
214 {
215         /* We may be called from the parent. */
216         g_return_val_if_fail(captivefs_vfs!=NULL,FALSE);
217         captivefs_vfs_validate(captivefs_vfs);  /* It may return FALSE. */
218
219         if (captivefs_vfs->options.debug_messages)
220                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_mount");
221
222         return 1;       /* NEVER return 0 */
223 }
224
225
226 /* Unmount the  file system
227  * Called when the file system is unmounted.
228  */
229 void captivefs_umount(struct captivefs_vfs *captivefs_vfs)
230 {
231         g_return_if_fail(captivefs_vfs_validate(captivefs_vfs));
232
233         if (captivefs_vfs->options.debug_messages)
234                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_umount");
235 }