source: trunk/third/gnome-vfs/libgnomevfs/gnome-vfs-handle.c @ 15858

Revision 15858, 5.0 KB checked in by ghudson, 24 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r15857, which included commits to RCS files with non-trunk default branches.
Line 
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#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
28#include "gnome-vfs.h"
29#include "gnome-vfs-private.h"
30
31
32struct GnomeVFSHandle {
33        /* URI of the file being accessed through the handle.  */
34        GnomeVFSURI *uri;
35
36        /* Method-specific handle.  */
37        GnomeVFSMethodHandle *method_handle;
38
39        /* Open mode.  */
40        GnomeVFSOpenMode open_mode;
41};
42
43#define CHECK_IF_OPEN(handle)                   \
44G_STMT_START{                                   \
45        if (handle->uri == NULL)                \
46                return GNOME_VFS_ERROR_NOT_OPEN;        \
47}G_STMT_END
48
49#define CHECK_IF_SUPPORTED(handle, what)                \
50G_STMT_START{                                           \
51        if (!VFS_METHOD_HAS_FUNC(handle->uri->method, what))            \
52                return GNOME_VFS_ERROR_NOT_SUPPORTED;   \
53}G_STMT_END
54
55#define INVOKE(result, handle, what, params)            \
56G_STMT_START{                                           \
57        CHECK_IF_OPEN (handle);                         \
58        CHECK_IF_SUPPORTED (handle, what);              \
59        (result) = handle->uri->method->what params;    \
60}G_STMT_END
61
62#define INVOKE_AND_RETURN(handle, what, params)         \
63G_STMT_START{                                           \
64        GnomeVFSResult __result;                        \
65                                                        \
66        INVOKE (__result, handle, what, params);        \
67        return __result;                                \
68}G_STMT_END
69
70
71GnomeVFSHandle *
72gnome_vfs_handle_new (GnomeVFSURI *uri,
73                      GnomeVFSMethodHandle *method_handle,
74                      GnomeVFSOpenMode open_mode)
75{
76        GnomeVFSHandle *new;
77
78        g_return_val_if_fail (uri != NULL, NULL);
79        g_return_val_if_fail (method_handle != NULL, NULL);
80
81        new = g_new (GnomeVFSHandle, 1);
82
83        new->uri = gnome_vfs_uri_ref (uri);
84        new->method_handle = method_handle;
85        new->open_mode = open_mode;
86
87        if ((open_mode & GNOME_VFS_OPEN_RANDOM) &&
88            !VFS_METHOD_HAS_FUNC (uri->method, seek)) {
89                GnomeVFSMethodHandle *handle;
90                handle = gnome_vfs_seek_emulate (new->uri, method_handle,
91                                                 open_mode);
92                if (handle) /* Successfully wrapped */
93                        new->method_handle = handle;
94        }
95
96        return new;
97}
98
99void
100gnome_vfs_handle_destroy (GnomeVFSHandle *handle)
101{
102        g_return_if_fail (handle != NULL);
103
104        gnome_vfs_uri_unref (handle->uri);
105
106        g_free (handle);
107}
108
109
110GnomeVFSOpenMode
111gnome_vfs_handle_get_open_mode (GnomeVFSHandle *handle)
112{
113        g_return_val_if_fail (handle != NULL, (GnomeVFSOpenMode) 0);
114
115        return handle->open_mode;
116}
117
118
119/* Actions.  */
120
121GnomeVFSResult
122gnome_vfs_handle_do_close (GnomeVFSHandle *handle,
123                           GnomeVFSContext *context)
124{
125        GnomeVFSResult result;
126
127        INVOKE (result, handle, close, (handle->uri->method, handle->method_handle, context));
128
129        /* Even if close has failed, we shut down the handle. */
130        gnome_vfs_handle_destroy (handle);
131
132        return result;
133}
134
135GnomeVFSResult
136gnome_vfs_handle_do_read (GnomeVFSHandle *handle,
137                          gpointer buffer,
138                          GnomeVFSFileSize num_bytes,
139                          GnomeVFSFileSize *bytes_read,
140                          GnomeVFSContext *context)
141{
142        INVOKE_AND_RETURN (handle, read, (handle->uri->method, handle->method_handle,
143                                          buffer, num_bytes, bytes_read,
144                                          context));
145}
146
147GnomeVFSResult
148gnome_vfs_handle_do_write (GnomeVFSHandle *handle,
149                           gconstpointer buffer,
150                           GnomeVFSFileSize num_bytes,
151                           GnomeVFSFileSize *bytes_written,
152                           GnomeVFSContext *context)
153{
154        INVOKE_AND_RETURN (handle, write, (handle->uri->method, handle->method_handle,
155                                           buffer, num_bytes, bytes_written,
156                                           context));
157}
158
159
160GnomeVFSResult
161gnome_vfs_handle_do_seek (GnomeVFSHandle *handle,
162                          GnomeVFSSeekPosition whence,
163                          GnomeVFSFileSize offset,
164                          GnomeVFSContext *context)
165{
166        INVOKE_AND_RETURN (handle, seek, (handle->uri->method, handle->method_handle,
167                                          whence, offset, context));
168}
169
170GnomeVFSResult
171gnome_vfs_handle_do_tell (GnomeVFSHandle *handle,
172                          GnomeVFSFileSize *offset_return)
173{
174        INVOKE_AND_RETURN (handle, tell, (handle->uri->method, handle->method_handle,
175                                          offset_return));
176}
177
178
179GnomeVFSResult
180gnome_vfs_handle_do_get_file_info (GnomeVFSHandle *handle,
181                                   GnomeVFSFileInfo *info,
182                                   GnomeVFSFileInfoOptions options,
183                                   GnomeVFSContext *context)
184{
185        INVOKE_AND_RETURN (handle, get_file_info_from_handle,
186                           (handle->uri->method, handle->method_handle, info, options,
187                            context));
188}
189
190GnomeVFSResult gnome_vfs_handle_do_truncate     (GnomeVFSHandle *handle,
191                                                 GnomeVFSFileSize length,
192                                                 GnomeVFSContext *context)
193{
194        INVOKE_AND_RETURN (handle, truncate_handle, (handle->uri->method, handle->method_handle, length, context));
195}
Note: See TracBrowser for help on using the repository browser.