2 * glib GIOChannel interface over gnome-vfs GnomeVFSURI for libcaptive
3 * Copyright (C) 2002 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 "captive/macros.h"
23 #include <libgnomevfs/gnome-vfs-method.h>
24 #include <libgnomevfs/gnome-vfs-ops.h>
25 #include <glib/gmessages.h>
26 #include "captive/client.h"
27 #include "gnome-vfs-module.h"
30 /* FIXME: fill 'err' */
32 struct captive_gnomevfs_giognomevfs {
34 GnomeVFSHandle *gnomevfshandle;
37 G_LOCK_DEFINE_STATIC(giochannel_funcs);
38 static GIOFuncs giochannel_funcs;
41 static gboolean validate_giognomevfs(struct captive_gnomevfs_giognomevfs *giognomevfs)
43 g_return_val_if_fail(giognomevfs!=NULL,FALSE);
44 g_return_val_if_fail(giognomevfs->gnomevfshandle!=NULL,FALSE);
50 static GIOStatus captive_gnomevfs_giognomevfs_io_read
51 (GIOChannel *channel,gchar *buf,gsize count,gsize *bytes_read,GError **err)
53 struct captive_gnomevfs_giognomevfs *giognomevfs=(struct captive_gnomevfs_giognomevfs *)channel;
54 GnomeVFSResult errvfsresult;
55 GnomeVFSFileSize bytes_read_local;
57 g_return_val_if_fail(validate_giognomevfs(giognomevfs),G_IO_STATUS_ERROR);
58 g_return_val_if_fail(buf!=NULL,G_IO_STATUS_ERROR);
59 g_return_val_if_fail(bytes_read!=NULL,G_IO_STATUS_ERROR);
61 errvfsresult=gnome_vfs_read(
62 giognomevfs->gnomevfshandle, /* handle */
65 &bytes_read_local); /* bytes_read */
66 g_return_val_if_fail((errvfsresult==GNOME_VFS_OK || errvfsresult==GNOME_VFS_ERROR_EOF),G_IO_STATUS_ERROR);
68 *bytes_read=bytes_read_local;
69 return (errvfsresult==GNOME_VFS_ERROR_EOF ? G_IO_STATUS_EOF : G_IO_STATUS_NORMAL);
73 static GIOStatus captive_gnomevfs_giognomevfs_io_write
74 (GIOChannel *channel,const gchar *buf,gsize count,gsize *bytes_written,GError **err)
76 struct captive_gnomevfs_giognomevfs *giognomevfs=(struct captive_gnomevfs_giognomevfs *)channel;
77 GnomeVFSResult errvfsresult;
78 GnomeVFSFileSize bytes_written_local;
80 g_return_val_if_fail(validate_giognomevfs(giognomevfs),G_IO_STATUS_ERROR);
81 g_return_val_if_fail(buf!=NULL,G_IO_STATUS_ERROR);
82 g_return_val_if_fail(bytes_written!=NULL,G_IO_STATUS_ERROR);
84 errvfsresult=gnome_vfs_write(
85 giognomevfs->gnomevfshandle, /* handle */
88 &bytes_written_local); /* bytes_written */
89 g_return_val_if_fail(errvfsresult==GNOME_VFS_OK,G_IO_STATUS_ERROR);
91 *bytes_written=bytes_written_local;
92 return G_IO_STATUS_NORMAL;
96 static GIOStatus captive_gnomevfs_giognomevfs_io_seek(GIOChannel *channel,gint64 offset,GSeekType type,GError **err)
98 struct captive_gnomevfs_giognomevfs *giognomevfs=(struct captive_gnomevfs_giognomevfs *)channel;
99 GnomeVFSSeekPosition whence;
100 GnomeVFSResult errvfsresult;
102 g_return_val_if_fail(validate_giognomevfs(giognomevfs),G_IO_STATUS_ERROR);
105 case G_SEEK_CUR: whence=GNOME_VFS_SEEK_CURRENT; break;
106 case G_SEEK_SET: whence=GNOME_VFS_SEEK_START; break;
107 case G_SEEK_END: whence=GNOME_VFS_SEEK_END; break;
108 default: g_return_val_if_reached(G_IO_STATUS_ERROR);
110 errvfsresult=gnome_vfs_seek(giognomevfs->gnomevfshandle,whence,offset);
111 g_return_val_if_fail(errvfsresult==GNOME_VFS_OK,G_IO_STATUS_ERROR);
113 return G_IO_STATUS_NORMAL;
117 static GIOStatus captive_gnomevfs_giognomevfs_io_close(GIOChannel *channel,GError **err)
119 struct captive_gnomevfs_giognomevfs *giognomevfs=(struct captive_gnomevfs_giognomevfs *)channel;
121 g_return_val_if_fail(validate_giognomevfs(giognomevfs),G_IO_STATUS_ERROR);
123 gnome_vfs_close(giognomevfs->gnomevfshandle);
125 return G_IO_STATUS_NORMAL;
129 static GSource* captive_gnomevfs_giognomevfs_io_create_watch(GIOChannel *channel,GIOCondition condition)
131 struct captive_gnomevfs_giognomevfs *giognomevfs=(struct captive_gnomevfs_giognomevfs *)channel;
133 g_return_val_if_fail(validate_giognomevfs(giognomevfs),NULL);
135 g_return_val_if_reached(NULL); /* FIXME: NOT IMPLEMENTED YET */
139 static void captive_gnomevfs_giognomevfs_io_free(GIOChannel *channel)
141 struct captive_gnomevfs_giognomevfs *giognomevfs=(struct captive_gnomevfs_giognomevfs *)channel;
143 g_return_if_fail(validate_giognomevfs(giognomevfs));
149 static GIOStatus captive_gnomevfs_giognomevfs_io_set_flags(GIOChannel *channel,GIOFlags flags,GError **err)
151 struct captive_gnomevfs_giognomevfs *giognomevfs=(struct captive_gnomevfs_giognomevfs *)channel;
153 g_return_val_if_fail(validate_giognomevfs(giognomevfs),G_IO_STATUS_ERROR);
155 /* no G_IO_FLAG_APPEND and no G_IO_FLAG_NONBLOCK */
156 g_return_val_if_fail((flags&G_IO_FLAG_SET_MASK)==0,G_IO_STATUS_ERROR);
158 return G_IO_STATUS_NORMAL;
162 static GIOFlags captive_gnomevfs_giognomevfs_io_get_flags(GIOChannel *channel)
164 struct captive_gnomevfs_giognomevfs *giognomevfs=(struct captive_gnomevfs_giognomevfs *)channel;
166 g_return_val_if_fail(validate_giognomevfs(giognomevfs),0);
168 return 0; /* | !G_IO_FLAG_APPEND | !G_IO_FLAG_NONBLOCK */
172 GnomeVFSResult captive_gnomevfs_giognomevfs_new(struct captive_gnomevfs_giognomevfs **giognomevfsp,
173 const gchar *captive_args,GnomeVFSURI *uri)
175 GnomeVFSHandle *handle;
176 struct captive_gnomevfs_giognomevfs *giognomevfs;
177 GnomeVFSResult errvfsresult;
180 g_return_val_if_fail(giognomevfsp!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
181 g_return_val_if_fail(uri!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
183 /* Parse 'captive_args' to get at least 'captive_option_rwmode' option value (phase 1). */
185 errbool=captive_init(
186 captive_args, /* captive_args */
187 NULL); /* image_iochannel */
188 G_UNLOCK(libcaptive);
189 g_assert(errbool==FALSE);
191 errvfsresult=gnome_vfs_open_uri(&handle,uri,
193 | GNOME_VFS_OPEN_RANDOM /* mandatory; otherwise we will get truncation if GNOME_VFS_OPEN_WRITE */
194 | GNOME_VFS_OPEN_READ
195 | (captive_option_rwmode==CAPTIVE_OPTION_RWMODE_RW ? GNOME_VFS_OPEN_WRITE : 0));
196 g_return_val_if_fail(errvfsresult==GNOME_VFS_OK,errvfsresult);
198 G_LOCK(giochannel_funcs);
199 giochannel_funcs.io_read =captive_gnomevfs_giognomevfs_io_read;
200 giochannel_funcs.io_write =captive_gnomevfs_giognomevfs_io_write;
201 giochannel_funcs.io_seek =captive_gnomevfs_giognomevfs_io_seek;
202 giochannel_funcs.io_close =captive_gnomevfs_giognomevfs_io_close;
203 giochannel_funcs.io_create_watch=captive_gnomevfs_giognomevfs_io_create_watch;
204 giochannel_funcs.io_free =captive_gnomevfs_giognomevfs_io_free;
205 giochannel_funcs.io_set_flags =captive_gnomevfs_giognomevfs_io_set_flags;
206 giochannel_funcs.io_get_flags =captive_gnomevfs_giognomevfs_io_get_flags;
207 G_UNLOCK(giochannel_funcs);
209 captive_new(giognomevfs);
210 g_assert(G_STRUCT_OFFSET(struct captive_gnomevfs_giognomevfs,iochannel)==0); /* safely re-type-able */
211 g_io_channel_init(&giognomevfs->iochannel);
212 giognomevfs->iochannel.funcs=&giochannel_funcs;
213 giognomevfs->iochannel.is_seekable=TRUE;
214 giognomevfs->iochannel.is_readable=TRUE;
215 giognomevfs->iochannel.is_writeable=(captive_option_rwmode==CAPTIVE_OPTION_RWMODE_RW);
216 giognomevfs->gnomevfshandle=handle;
218 /* Initialization for the real this time (phase 2). */
220 errbool=captive_init(
221 NULL, /* captive_args */
222 (GIOChannel *)giognomevfs); /* image_iochannel */
223 G_UNLOCK(libcaptive);
224 g_assert(errbool==TRUE);
226 *giognomevfsp=giognomevfs;