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