ftp://ftp.redhat.com/pub/redhat/linux/rawhide/SRPMS/SRPMS/gnome-vfs2-2.3.8-1.src.rpm
[gnome-vfs-httpcaptive.git] / libgnomevfs / gnome-vfs-handle.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* gnome-vfs-handle.c - Handle object for GNOME VFS files.
3
4    Copyright (C) 1999 Free Software Foundation
5
6    The Gnome Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The Gnome Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the Gnome Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.
20
21    Author: Ettore Perazzoli <ettore@gnu.org>
22 */
23
24 #include <config.h>
25 #include "gnome-vfs-handle.h"
26 #include "gnome-vfs-handle-private.h"
27 #include "gnome-vfs-method.h"
28
29 #include <glib/gmessages.h>
30
31 struct GnomeVFSHandle {
32         /* URI of the file being accessed through the handle.  */
33         GnomeVFSURI *uri;
34
35         /* Method-specific handle.  */
36         GnomeVFSMethodHandle *method_handle;
37
38         /* Open mode.  */
39         GnomeVFSOpenMode open_mode;
40 };
41
42 #define CHECK_IF_OPEN(handle)                   \
43 G_STMT_START{                                   \
44         if (handle->uri == NULL)                \
45                 return GNOME_VFS_ERROR_NOT_OPEN;        \
46 }G_STMT_END
47
48 #define CHECK_IF_SUPPORTED(handle, what)                \
49 G_STMT_START{                                           \
50         if (!VFS_METHOD_HAS_FUNC(handle->uri->method, what))            \
51                 return GNOME_VFS_ERROR_NOT_SUPPORTED;   \
52 }G_STMT_END
53
54 #define INVOKE(result, handle, what, params)            \
55 G_STMT_START{                                           \
56         CHECK_IF_OPEN (handle);                         \
57         CHECK_IF_SUPPORTED (handle, what);              \
58         (result) = handle->uri->method->what params;    \
59 }G_STMT_END
60
61 #define INVOKE_AND_RETURN(handle, what, params)         \
62 G_STMT_START{                                           \
63         GnomeVFSResult __result;                        \
64                                                         \
65         INVOKE (__result, handle, what, params);        \
66         return __result;                                \
67 }G_STMT_END
68
69 \f
70 GnomeVFSHandle *
71 _gnome_vfs_handle_new (GnomeVFSURI *uri,
72                       GnomeVFSMethodHandle *method_handle,
73                       GnomeVFSOpenMode open_mode)
74 {
75         GnomeVFSHandle *new;
76
77         g_return_val_if_fail (uri != NULL, NULL);
78         g_return_val_if_fail (method_handle != NULL, NULL);
79
80         new = g_new (GnomeVFSHandle, 1);
81
82         new->uri = gnome_vfs_uri_ref (uri);
83         new->method_handle = method_handle;
84         new->open_mode = open_mode;
85
86         return new;
87 }
88
89 void
90 _gnome_vfs_handle_destroy (GnomeVFSHandle *handle)
91 {
92         g_return_if_fail (handle != NULL);
93
94         gnome_vfs_uri_unref (handle->uri);
95
96         g_free (handle);
97 }
98
99 \f
100 GnomeVFSOpenMode
101 _gnome_vfs_handle_get_open_mode (GnomeVFSHandle *handle)
102 {
103         g_return_val_if_fail (handle != NULL, (GnomeVFSOpenMode) 0);
104
105         return handle->open_mode;
106 }
107
108 \f
109 /* Actions.  */
110
111 GnomeVFSResult
112 _gnome_vfs_handle_do_close (GnomeVFSHandle *handle,
113                            GnomeVFSContext *context)
114 {
115         GnomeVFSResult result;
116
117         INVOKE (result, handle, close, (handle->uri->method, handle->method_handle, context));
118
119         /* Even if close has failed, we shut down the handle. */
120         _gnome_vfs_handle_destroy (handle);
121
122         return result;
123 }
124
125 GnomeVFSResult
126 _gnome_vfs_handle_do_read (GnomeVFSHandle *handle,
127                           gpointer buffer,
128                           GnomeVFSFileSize num_bytes,
129                           GnomeVFSFileSize *bytes_read,
130                           GnomeVFSContext *context)
131 {
132         INVOKE_AND_RETURN (handle, read, (handle->uri->method, handle->method_handle,
133                                           buffer, num_bytes, bytes_read,
134                                           context));
135 }
136
137 GnomeVFSResult
138 _gnome_vfs_handle_do_write (GnomeVFSHandle *handle,
139                            gconstpointer buffer,
140                            GnomeVFSFileSize num_bytes,
141                            GnomeVFSFileSize *bytes_written,
142                            GnomeVFSContext *context)
143 {
144         INVOKE_AND_RETURN (handle, write, (handle->uri->method, handle->method_handle,
145                                            buffer, num_bytes, bytes_written,
146                                            context));
147 }
148
149 \f
150 GnomeVFSResult
151 _gnome_vfs_handle_do_seek (GnomeVFSHandle *handle,
152                           GnomeVFSSeekPosition whence,
153                           GnomeVFSFileSize offset,
154                           GnomeVFSContext *context)
155 {
156         INVOKE_AND_RETURN (handle, seek, (handle->uri->method, handle->method_handle,
157                                           whence, offset, context));
158 }
159
160 GnomeVFSResult
161 _gnome_vfs_handle_do_tell (GnomeVFSHandle *handle,
162                           GnomeVFSFileSize *offset_return)
163 {
164         INVOKE_AND_RETURN (handle, tell, (handle->uri->method, handle->method_handle,
165                                           offset_return));
166 }
167
168 \f
169 GnomeVFSResult
170 _gnome_vfs_handle_do_get_file_info (GnomeVFSHandle *handle,
171                                    GnomeVFSFileInfo *info,
172                                    GnomeVFSFileInfoOptions options,
173                                    GnomeVFSContext *context)
174 {
175         INVOKE_AND_RETURN (handle, get_file_info_from_handle,
176                            (handle->uri->method, handle->method_handle, info, options,
177                             context));
178 }
179
180 GnomeVFSResult _gnome_vfs_handle_do_truncate     (GnomeVFSHandle *handle,
181                                                  GnomeVFSFileSize length,
182                                                  GnomeVFSContext *context)
183 {
184         INVOKE_AND_RETURN (handle, truncate_handle, (handle->uri->method, handle->method_handle, length, context));
185 }
186
187 GnomeVFSResult
188 _gnome_vfs_handle_do_file_control  (GnomeVFSHandle          *handle,
189                                    const char              *operation,
190                                    gpointer                 operation_data,
191                                    GnomeVFSContext         *context)
192 {
193         INVOKE_AND_RETURN (handle, file_control, (handle->uri->method, handle->method_handle, operation, operation_data, context));
194 }