5b5a25e0dcfb1bc06234e85a70d196149377dd5c
[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                 return captivefs_vfs;
103                 }
104         G_UNLOCK(libcaptive);
105
106         if (!g_thread_supported())
107                 g_thread_init(NULL);    /* g_thread_init() fails on second initialization */
108
109         /* Initialize GObject subsystem of GLib. */
110         g_type_init();
111
112         captive_new(captivefs_vfs);
113         captivefs_vfs->captive_vfs_object=NULL;
114         captivefs_vfs->parent_pid=getpid();
115         captivefs_vfs->global_ctx=global_ctx;
116         *global_ctx=captivefs_vfs;
117         captivefs_vfs->inits=1;
118         captivefs_vfs->mounts=0;
119
120         captive_options_init(&captivefs_vfs->options);
121
122         if ((cgs=lu_opt_getchar(cfg,"MOUNT","captive_options"))) {
123                 captive_options_string=(/* de-const */gchar *)captive_strdup_alloca(cgs);
124                 /* Convert ';' to ' ' to prevent its parsing by LUFS to keep its ordering
125                  * to let the options be overridable by user (such as 'ro').
126                  */
127                 for (gs=captive_options_string;(gs=strchr(gs,';'));gs++)
128                         *gs=' ';
129                 if (!captive_options_parse(
130                                 &captivefs_vfs->options,        /* options */
131                                 captive_options_string))        /* captive_args */
132                         goto fail_free_options;
133                 }
134
135         if (captivefs_vfs->options.debug_messages)
136                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_init");
137
138         /* image_iochannel */
139         if ((cgs=lu_opt_getchar(cfg,"MOUNT","image"))) {
140                 g_assert(captivefs_vfs->options.image_iochannel==NULL);
141                 if (!(captivefs_vfs->options.image_iochannel=g_io_channel_new_file(
142                                 cgs,    /* filename */
143                                 (captivefs_vfs->options.rwmode==CAPTIVE_OPTION_RWMODE_RW ? "r+" : "r"), /* mode */
144                                 NULL))) {       /* error */
145                         g_warning(_("%s: image_iochannel open failed"),"captivefs_init");
146                         goto fail_free_options;
147                         }
148                 }
149
150         return captivefs_vfs;
151
152 fail_free_options:
153         captive_options_free(&captivefs_vfs->options);
154 /* fail: */
155         return NULL;
156 }
157
158
159 /* Cleanup
160  * Check the global context count and free it if necessary.
161  * Deallocate memory and free all resources.
162  */
163 void captivefs_free(struct captivefs_vfs *captivefs_vfs)
164 {
165         g_return_if_fail(captivefs_vfs_validate(captivefs_vfs));
166         g_return_if_fail(captivefs_vfs->inits>0);
167
168         if (captivefs_vfs->options.debug_messages)
169                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_free");
170         
171         if (--captivefs_vfs->inits>0) {
172                 g_assert(captivefs_vfs_validate(captivefs_vfs));
173                 return;
174                 }
175         g_assert(captivefs_vfs->mounts==0);
176         G_LOCK(libcaptive);
177         *captivefs_vfs->global_ctx=NULL;
178         G_UNLOCK(libcaptive);
179
180         g_assert(G_OBJECT(captivefs_vfs->captive_vfs_object)->ref_count==1);
181
182         G_LOCK(libcaptive);
183         g_object_unref(captivefs_vfs->captive_vfs_object);
184         G_UNLOCK(libcaptive);
185
186         g_io_channel_unref(captivefs_vfs->options.image_iochannel);
187         captive_options_free(&captivefs_vfs->options);
188         g_free(captivefs_vfs);
189 }
190
191
192 /* Mount the file system.
193  * Called when a mount operation is performed.
194  * Initialize specific connections, login, etc.
195  *
196  * Notes:
197  *     By default, LUFS may attempt multiple connections at once.  If your
198  * filesystem doesn't support this, you need to specificy -c 1 on the
199  * lufsmount command line or connections=1 in the mount options.
200  *     See ftpfs for an example of how to read configuration options
201  * from a configuration file if you want to, for example, be able to set
202  * default values.
203  */
204 int captivefs_mount(struct captivefs_vfs *captivefs_vfs)
205 {
206         /* We may be called from the parent. */
207         g_return_val_if_fail(captivefs_vfs!=NULL,FALSE);
208         captivefs_vfs_validate(captivefs_vfs);  /* It may return FALSE. */
209
210         if (captivefs_vfs->options.debug_messages)
211                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_mount");
212
213         captivefs_vfs->mounts++;
214
215         captivefs_vfs_validate(captivefs_vfs);  /* It may return FALSE. */
216
217         return 1;       /* NEVER return 0 */
218 }
219
220
221 /* Unmount the  file system
222  * Called when the file system is unmounted.
223  */
224 void captivefs_umount(struct captivefs_vfs *captivefs_vfs)
225 {
226         g_return_if_fail(captivefs_vfs_validate(captivefs_vfs));
227         g_return_if_fail(captivefs_vfs->mounts>0);
228
229         if (captivefs_vfs->options.debug_messages)
230                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_umount");
231
232         captivefs_vfs->mounts--;
233
234         g_assert(captivefs_vfs_validate(captivefs_vfs));
235 }
236
237
238 #ifdef HAVE_STRUCT_LUFS_SBATTR
239 int captivefs_statfs(struct captivefs_vfs *captivefs_vfs,struct lufs_sbattr *sbattr)
240 {
241 CaptiveVfsVolumeInfo captive_volume_info;
242 GnomeVFSResult errvfsresult;
243
244         g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
245         g_return_val_if_fail(sbattr!=NULL,-1);
246
247         if (captivefs_vfs->options.debug_messages)
248                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_statfs");
249
250         G_LOCK(libcaptive);
251         errvfsresult=captive_vfs_volume_info_get(captivefs_vfs->captive_vfs_object,&captive_volume_info);
252         G_UNLOCK(libcaptive);
253         if (errvfsresult!=GNOME_VFS_OK)
254                 return -1;
255
256         sbattr->sb_bytes=captive_volume_info.bytes;
257         sbattr->sb_bytes_free=captive_volume_info.bytes_free;
258         sbattr->sb_bytes_available=captive_volume_info.bytes_available;
259         /* 'sbattr->sb_files' not known - left unchanged. */
260         /* 'sbattr->sb_ffree' not known - left unchanged. */
261
262         return 0;
263 }
264 #endif /* HAVE_STRUCT_LUFS_SBATTR */