2 * lufs interface module vfs objects implementation for libcaptive
3 * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
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
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.
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
22 #include "captivefs-vfs.h" /* self */
23 #include <glib/gmessages.h>
24 #include "captivefs-misc.h"
27 #include <captive/client-vfs.h>
28 #include <captive/options.h>
29 #include <captive/macros.h>
32 #include <lufs/proto.h>
35 gboolean captivefs_vfs_validate(struct captivefs_vfs *captivefs_vfs)
37 g_return_val_if_fail(captivefs_vfs!=NULL,FALSE);
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;
46 if (captivefs_vfs->parent_pid==getpid()) {
51 errvfsresult=captive_vfs_new(&captivefs_vfs->captive_vfs_object,&captivefs_vfs->options);
53 if (errvfsresult!=GNOME_VFS_OK) {
55 g_return_val_if_reached(FALSE);
58 if (!CAPTIVE_VFS_IS_OBJECT(captivefs_vfs->captive_vfs_object)) {
60 g_return_val_if_reached(FALSE);
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.
80 struct captivefs_vfs *captivefs_init
81 (struct list_head *cfg,struct dir_cache *cache,const struct credentials *cred,struct captivefs_vfs **global_ctx)
83 struct captivefs_vfs *captivefs_vfs;
85 gchar *gs,*captive_options_string;
87 g_return_val_if_fail(global_ctx!=NULL,NULL);
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.
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;
106 G_UNLOCK(libcaptive);
108 if (!g_thread_supported())
109 g_thread_init(NULL); /* g_thread_init() fails on second initialization */
111 /* Initialize GObject subsystem of GLib. */
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;
123 captive_options_init(&captivefs_vfs->options);
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').
130 for (gs=captive_options_string;(gs=strchr(gs,';'));gs++)
132 if (!captive_options_parse(
133 &captivefs_vfs->options, /* options */
134 captive_options_string)) /* captive_args */
135 goto fail_free_options;
137 if (lu_opt_getchar(cfg,"MOUNT","private"))
138 captivefs_vfs->private=TRUE;
140 if (captivefs_vfs->options.debug_messages)
141 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_init");
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(
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;
155 return captivefs_vfs;
158 captive_options_free(&captivefs_vfs->options);
165 * Check the global context count and free it if necessary.
166 * Deallocate memory and free all resources.
168 void captivefs_free(struct captivefs_vfs *captivefs_vfs)
170 g_return_if_fail(captivefs_vfs_validate(captivefs_vfs));
171 g_return_if_fail(captivefs_vfs->inits>0);
173 if (captivefs_vfs->options.debug_messages)
174 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_free");
176 if (--captivefs_vfs->inits>0) {
177 g_assert(captivefs_vfs_validate(captivefs_vfs));
180 g_assert(captivefs_vfs->mounts==0);
182 *captivefs_vfs->global_ctx=NULL;
183 G_UNLOCK(libcaptive);
185 g_assert(G_OBJECT(captivefs_vfs->captive_vfs_object)->ref_count==1);
188 g_object_unref(captivefs_vfs->captive_vfs_object);
189 G_UNLOCK(libcaptive);
191 /* Do not: g_io_channel_unref(captivefs_vfs->options.image_iochannel);
192 * as it will be unreferenced by captive_options_free().
194 captive_options_free(&captivefs_vfs->options);
195 g_free(captivefs_vfs);
199 /* Mount the file system.
200 * Called when a mount operation is performed.
201 * Initialize specific connections, login, etc.
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
211 int captivefs_mount(struct captivefs_vfs *captivefs_vfs)
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. */
217 if (captivefs_vfs->options.debug_messages)
218 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_mount");
220 captivefs_vfs->mounts++;
222 captivefs_vfs_validate(captivefs_vfs); /* It may return FALSE. */
224 return 1; /* NEVER return 0 */
228 /* Unmount the file system
229 * Called when the file system is unmounted.
231 void captivefs_umount(struct captivefs_vfs *captivefs_vfs)
233 g_return_if_fail(captivefs_vfs_validate(captivefs_vfs));
234 g_return_if_fail(captivefs_vfs->mounts>0);
236 if (captivefs_vfs->options.debug_messages)
237 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_umount");
239 captivefs_vfs->mounts--;
241 g_assert(captivefs_vfs_validate(captivefs_vfs));
245 #ifdef HAVE_STRUCT_LUFS_SBATTR
246 int captivefs_statfs(struct captivefs_vfs *captivefs_vfs,struct lufs_sbattr *sbattr)
248 CaptiveVfsVolumeInfo captive_volume_info;
249 GnomeVFSResult errvfsresult;
251 g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
252 g_return_val_if_fail(sbattr!=NULL,-1);
254 if (captivefs_vfs->options.debug_messages)
255 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_statfs");
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)
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. */
271 #endif /* HAVE_STRUCT_LUFS_SBATTR */