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