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 return captivefs_vfs;
104 G_UNLOCK(libcaptive);
106 if (!g_thread_supported())
107 g_thread_init(NULL); /* g_thread_init() fails on second initialization */
109 /* Initialize GObject subsystem of GLib. */
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;
120 captive_options_init(&captivefs_vfs->options);
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').
127 for (gs=captive_options_string;(gs=strchr(gs,';'));gs++)
129 if (!captive_options_parse(
130 &captivefs_vfs->options, /* options */
131 captive_options_string)) /* captive_args */
132 goto fail_free_options;
135 if (captivefs_vfs->options.debug_messages)
136 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_init");
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(
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;
150 return captivefs_vfs;
153 captive_options_free(&captivefs_vfs->options);
160 * Check the global context count and free it if necessary.
161 * Deallocate memory and free all resources.
163 void captivefs_free(struct captivefs_vfs *captivefs_vfs)
165 g_return_if_fail(captivefs_vfs_validate(captivefs_vfs));
166 g_return_if_fail(captivefs_vfs->inits>0);
168 if (captivefs_vfs->options.debug_messages)
169 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_free");
171 if (--captivefs_vfs->inits>0) {
172 g_assert(captivefs_vfs_validate(captivefs_vfs));
175 g_assert(captivefs_vfs->mounts==0);
177 *captivefs_vfs->global_ctx=NULL;
178 G_UNLOCK(libcaptive);
180 g_assert(G_OBJECT(captivefs_vfs->captive_vfs_object)->ref_count==1);
183 g_object_unref(captivefs_vfs->captive_vfs_object);
184 G_UNLOCK(libcaptive);
186 g_io_channel_unref(captivefs_vfs->options.image_iochannel);
187 captive_options_free(&captivefs_vfs->options);
188 g_free(captivefs_vfs);
192 /* Mount the file system.
193 * Called when a mount operation is performed.
194 * Initialize specific connections, login, etc.
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
204 int captivefs_mount(struct captivefs_vfs *captivefs_vfs)
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. */
210 if (captivefs_vfs->options.debug_messages)
211 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_mount");
213 captivefs_vfs->mounts++;
215 captivefs_vfs_validate(captivefs_vfs); /* It may return FALSE. */
217 return 1; /* NEVER return 0 */
221 /* Unmount the file system
222 * Called when the file system is unmounted.
224 void captivefs_umount(struct captivefs_vfs *captivefs_vfs)
226 g_return_if_fail(captivefs_vfs_validate(captivefs_vfs));
227 g_return_if_fail(captivefs_vfs->mounts>0);
229 if (captivefs_vfs->options.debug_messages)
230 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_umount");
232 captivefs_vfs->mounts--;
234 g_assert(captivefs_vfs_validate(captivefs_vfs));
238 #ifdef HAVE_STRUCT_LUFS_SBATTR
239 int captivefs_statfs(struct captivefs_vfs *captivefs_vfs,struct lufs_sbattr *sbattr)
241 CaptiveVfsVolumeInfo captive_volume_info;
242 GnomeVFSResult errvfsresult;
244 g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
245 g_return_val_if_fail(sbattr!=NULL,-1);
247 if (captivefs_vfs->options.debug_messages)
248 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_statfs");
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)
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. */
264 #endif /* HAVE_STRUCT_LUFS_SBATTR */