188442ab6428a6a5dd33f7a65cf5e046e9a623e1
[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 #include <lufs/proto.h>
33
34
35 gboolean captivefs_vfs_validate(struct captivefs_vfs *captivefs_vfs)
36 {
37         g_return_val_if_fail(captivefs_vfs!=NULL,FALSE);
38
39         G_LOCK(libcaptive);
40         g_assert(captivefs_vfs->inits>0);
41         g_assert(captivefs_vfs->mounts>=0);
42         g_assert(captivefs_vfs->mounts<=captivefs_vfs->inits);
43         if (!captivefs_vfs->captive_vfs_object) {
44 GnomeVFSResult errvfsresult;
45
46                 if (captivefs_vfs->parent_pid==getpid()) {
47                         G_UNLOCK(libcaptive);
48                         return FALSE;
49                         }
50
51                 errvfsresult=captive_vfs_new(&captivefs_vfs->captive_vfs_object,&captivefs_vfs->options);
52
53                 if (errvfsresult!=GNOME_VFS_OK) {
54                         G_UNLOCK(libcaptive);
55                         g_return_val_if_reached(FALSE);
56                         }
57                 }
58         if (!CAPTIVE_VFS_IS_OBJECT(captivefs_vfs->captive_vfs_object)) {
59                 G_UNLOCK(libcaptive);
60                 g_return_val_if_reached(FALSE);
61                 }
62         G_UNLOCK(libcaptive);
63
64         return TRUE;
65 }
66
67
68 /* Initialization
69  * Here we allocate a structure to hold all the file system local info 
70  * (localfs_local). This structure will then be passed as a parameter to 
71  * the other functions.
72  * global_ctx holds info about another structure that can be shared between all
73  * instances of the filesystem. If the pointer is NULL, then this is the
74  * first instance and the structure should be allocated.
75  * ! You must implement  locking/usage-count/deallocation logic when using
76  *   a global context. (locking is omited here)
77  * ! Most filesystems don't need a global context so you can safely ignore the
78  *   global_ctx parameter.  
79  */
80 struct captivefs_vfs *captivefs_init
81                 (struct list_head *cfg,struct dir_cache *cache,const struct credentials *cred,struct captivefs_vfs **global_ctx)
82 {
83 struct captivefs_vfs *captivefs_vfs;
84 const gchar *cgs;
85 gchar *gs,*captive_options_string;
86
87         g_return_val_if_fail(global_ctx!=NULL,NULL);
88
89         /* Do not: g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_init");
90          * as we do not yet have 'VFS_DEBUG_MESSAGES(captive_vfs_object)' set.
91          * Generally we make all g_log() conditional here as we do not want to mess
92          * with overriden GLog handlers of libcaptive itself.
93          */
94
95         G_LOCK(libcaptive);
96         if ((captivefs_vfs=*global_ctx)) {
97                 g_assert(captivefs_vfs->inits>0);
98                 /* We do not support multiple LUFS threads if they could not be cross-locked. */
99                 g_return_val_if_fail(g_thread_supported(),NULL);
100                 captivefs_vfs->inits++;
101                 G_UNLOCK(libcaptive);
102                 if (captivefs_vfs->options.debug_messages)
103                         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_init");
104                 return captivefs_vfs;
105                 }
106         G_UNLOCK(libcaptive);
107
108         if (!g_thread_supported())
109                 g_thread_init(NULL);    /* g_thread_init() fails on second initialization */
110
111         /* Initialize GObject subsystem of GLib. */
112         g_type_init();
113
114         captive_new(captivefs_vfs);
115         captivefs_vfs->captive_vfs_object=NULL;
116         captivefs_vfs->private=FALSE;
117         captivefs_vfs->parent_pid=getpid();
118         captivefs_vfs->global_ctx=global_ctx;
119         *global_ctx=captivefs_vfs;
120         captivefs_vfs->inits=1;
121         captivefs_vfs->mounts=0;
122
123         captive_options_init(&captivefs_vfs->options);
124
125         if ((cgs=lu_opt_getchar(cfg,"MOUNT","captive_options"))) {
126                 captive_options_string=(/* de-const */gchar *)captive_strdup_alloca(cgs);
127                 /* Convert ';' to ' ' to prevent its parsing by LUFS to keep its ordering
128                  * to let the options be overridable by user (such as 'ro').
129                  */
130                 for (gs=captive_options_string;(gs=strchr(gs,';'));gs++)
131                         *gs=' ';
132                 if (!captive_options_parse(
133                                 &captivefs_vfs->options,        /* options */
134                                 captive_options_string))        /* captive_args */
135                         goto fail_free_options;
136                 }
137         if (lu_opt_getchar(cfg,"MOUNT","private"))
138                 captivefs_vfs->private=TRUE;
139
140         if (captivefs_vfs->options.debug_messages)
141                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_init");
142
143         /* image_iochannel */
144         if ((cgs=lu_opt_getchar(cfg,"MOUNT","image"))) {
145                 g_assert(captivefs_vfs->options.image_iochannel==NULL);
146                 if (!(captivefs_vfs->options.image_iochannel=g_io_channel_new_file(
147                                 cgs,    /* filename */
148                                 (captivefs_vfs->options.rwmode==CAPTIVE_OPTION_RWMODE_RW ? "r+" : "r"), /* mode */
149                                 NULL))) {       /* error */
150                         g_warning(_("%s: image_iochannel open failed"),"captivefs_init");
151                         goto fail_free_options;
152                         }
153                 }
154
155         return captivefs_vfs;
156
157 fail_free_options:
158         captive_options_free(&captivefs_vfs->options);
159 /* fail: */
160         return NULL;
161 }
162
163
164 /* Cleanup
165  * Check the global context count and free it if necessary.
166  * Deallocate memory and free all resources.
167  */
168 void captivefs_free(struct captivefs_vfs *captivefs_vfs)
169 {
170         g_return_if_fail(captivefs_vfs_validate(captivefs_vfs));
171         g_return_if_fail(captivefs_vfs->inits>0);
172
173         if (captivefs_vfs->options.debug_messages)
174                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_free");
175         
176         if (--captivefs_vfs->inits>0) {
177                 g_assert(captivefs_vfs_validate(captivefs_vfs));
178                 return;
179                 }
180         g_assert(captivefs_vfs->mounts==0);
181         G_LOCK(libcaptive);
182         *captivefs_vfs->global_ctx=NULL;
183         G_UNLOCK(libcaptive);
184
185         g_assert(G_OBJECT(captivefs_vfs->captive_vfs_object)->ref_count==1);
186
187         G_LOCK(libcaptive);
188         g_object_unref(captivefs_vfs->captive_vfs_object);
189         G_UNLOCK(libcaptive);
190
191         /* Do not: g_io_channel_unref(captivefs_vfs->options.image_iochannel);
192          * as it will be unreferenced by captive_options_free().
193          */
194         captive_options_free(&captivefs_vfs->options);
195         g_free(captivefs_vfs);
196 }
197
198
199 /* Mount the file system.
200  * Called when a mount operation is performed.
201  * Initialize specific connections, login, etc.
202  *
203  * Notes:
204  *     By default, LUFS may attempt multiple connections at once.  If your
205  * filesystem doesn't support this, you need to specificy -c 1 on the
206  * lufsmount command line or connections=1 in the mount options.
207  *     See ftpfs for an example of how to read configuration options
208  * from a configuration file if you want to, for example, be able to set
209  * default values.
210  */
211 int captivefs_mount(struct captivefs_vfs *captivefs_vfs)
212 {
213         /* We may be called from the parent. */
214         g_return_val_if_fail(captivefs_vfs!=NULL,FALSE);
215         captivefs_vfs_validate(captivefs_vfs);  /* It may return FALSE. */
216
217         if (captivefs_vfs->options.debug_messages)
218                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_mount");
219
220         captivefs_vfs->mounts++;
221
222         captivefs_vfs_validate(captivefs_vfs);  /* It may return FALSE. */
223
224         return 1;       /* NEVER return 0 */
225 }
226
227
228 /* Unmount the  file system
229  * Called when the file system is unmounted.
230  */
231 void captivefs_umount(struct captivefs_vfs *captivefs_vfs)
232 {
233         g_return_if_fail(captivefs_vfs_validate(captivefs_vfs));
234         g_return_if_fail(captivefs_vfs->mounts>0);
235
236         if (captivefs_vfs->options.debug_messages)
237                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_umount");
238
239         captivefs_vfs->mounts--;
240
241         g_assert(captivefs_vfs_validate(captivefs_vfs));
242 }
243
244
245 #ifdef HAVE_STRUCT_LUFS_SBATTR
246 int captivefs_statfs(struct captivefs_vfs *captivefs_vfs,struct lufs_sbattr *sbattr)
247 {
248 CaptiveVfsVolumeInfo captive_volume_info;
249 GnomeVFSResult errvfsresult;
250
251         g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
252         g_return_val_if_fail(sbattr!=NULL,-1);
253
254         if (captivefs_vfs->options.debug_messages)
255                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_statfs");
256
257         G_LOCK(libcaptive);
258         errvfsresult=captive_vfs_volume_info_get(captivefs_vfs->captive_vfs_object,&captive_volume_info);
259         G_UNLOCK(libcaptive);
260         if (errvfsresult!=GNOME_VFS_OK)
261                 return -1;
262
263         sbattr->sb_bytes=captive_volume_info.bytes;
264         sbattr->sb_bytes_free=captive_volume_info.bytes_free;
265         sbattr->sb_bytes_available=captive_volume_info.bytes_available;
266         /* 'sbattr->sb_files' not known - left unchanged. */
267         /* 'sbattr->sb_ffree' not known - left unchanged. */
268
269         return 0;
270 }
271 #endif /* HAVE_STRUCT_LUFS_SBATTR */