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.
4 Copyright (C) 1999 Free Software Foundation
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.
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.
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.
21 Author: Ettore Perazzoli <ettore@gnu.org>
25 #include "gnome-vfs-handle.h"
26 #include "gnome-vfs-handle-private.h"
27 #include "gnome-vfs-method.h"
29 #include <glib/gmessages.h>
31 struct GnomeVFSHandle {
32 /* URI of the file being accessed through the handle. */
35 /* Method-specific handle. */
36 GnomeVFSMethodHandle *method_handle;
39 GnomeVFSOpenMode open_mode;
42 #define CHECK_IF_OPEN(handle) \
44 if (handle->uri == NULL) \
45 return GNOME_VFS_ERROR_NOT_OPEN; \
48 #define CHECK_IF_SUPPORTED(handle, what) \
50 if (!VFS_METHOD_HAS_FUNC(handle->uri->method, what)) \
51 return GNOME_VFS_ERROR_NOT_SUPPORTED; \
54 #define INVOKE(result, handle, what, params) \
56 CHECK_IF_OPEN (handle); \
57 CHECK_IF_SUPPORTED (handle, what); \
58 (result) = handle->uri->method->what params; \
61 #define INVOKE_AND_RETURN(handle, what, params) \
63 GnomeVFSResult __result; \
65 INVOKE (__result, handle, what, params); \
71 _gnome_vfs_handle_new (GnomeVFSURI *uri,
72 GnomeVFSMethodHandle *method_handle,
73 GnomeVFSOpenMode open_mode)
77 g_return_val_if_fail (uri != NULL, NULL);
78 g_return_val_if_fail (method_handle != NULL, NULL);
80 new = g_new (GnomeVFSHandle, 1);
82 new->uri = gnome_vfs_uri_ref (uri);
83 new->method_handle = method_handle;
84 new->open_mode = open_mode;
90 _gnome_vfs_handle_destroy (GnomeVFSHandle *handle)
92 g_return_if_fail (handle != NULL);
94 gnome_vfs_uri_unref (handle->uri);
101 _gnome_vfs_handle_get_open_mode (GnomeVFSHandle *handle)
103 g_return_val_if_fail (handle != NULL, (GnomeVFSOpenMode) 0);
105 return handle->open_mode;
112 _gnome_vfs_handle_do_close (GnomeVFSHandle *handle,
113 GnomeVFSContext *context)
115 GnomeVFSResult result;
117 INVOKE (result, handle, close, (handle->uri->method, handle->method_handle, context));
119 /* Even if close has failed, we shut down the handle. */
120 _gnome_vfs_handle_destroy (handle);
126 _gnome_vfs_handle_do_read (GnomeVFSHandle *handle,
128 GnomeVFSFileSize num_bytes,
129 GnomeVFSFileSize *bytes_read,
130 GnomeVFSContext *context)
132 INVOKE_AND_RETURN (handle, read, (handle->uri->method, handle->method_handle,
133 buffer, num_bytes, bytes_read,
138 _gnome_vfs_handle_do_write (GnomeVFSHandle *handle,
139 gconstpointer buffer,
140 GnomeVFSFileSize num_bytes,
141 GnomeVFSFileSize *bytes_written,
142 GnomeVFSContext *context)
144 INVOKE_AND_RETURN (handle, write, (handle->uri->method, handle->method_handle,
145 buffer, num_bytes, bytes_written,
151 _gnome_vfs_handle_do_seek (GnomeVFSHandle *handle,
152 GnomeVFSSeekPosition whence,
153 GnomeVFSFileSize offset,
154 GnomeVFSContext *context)
156 INVOKE_AND_RETURN (handle, seek, (handle->uri->method, handle->method_handle,
157 whence, offset, context));
161 _gnome_vfs_handle_do_tell (GnomeVFSHandle *handle,
162 GnomeVFSFileSize *offset_return)
164 INVOKE_AND_RETURN (handle, tell, (handle->uri->method, handle->method_handle,
170 _gnome_vfs_handle_do_get_file_info (GnomeVFSHandle *handle,
171 GnomeVFSFileInfo *info,
172 GnomeVFSFileInfoOptions options,
173 GnomeVFSContext *context)
175 INVOKE_AND_RETURN (handle, get_file_info_from_handle,
176 (handle->uri->method, handle->method_handle, info, options,
180 GnomeVFSResult _gnome_vfs_handle_do_truncate (GnomeVFSHandle *handle,
181 GnomeVFSFileSize length,
182 GnomeVFSContext *context)
184 INVOKE_AND_RETURN (handle, truncate_handle, (handle->uri->method, handle->method_handle, length, context));
188 _gnome_vfs_handle_do_file_control (GnomeVFSHandle *handle,
189 const char *operation,
190 gpointer operation_data,
191 GnomeVFSContext *context)
193 INVOKE_AND_RETURN (handle, file_control, (handle->uri->method, handle->method_handle, operation, operation_data, context));