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 | |
---|
27 | #include "gnome-vfs.h" |
---|
28 | #include "gnome-vfs-private.h" |
---|
29 | |
---|
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 | |
---|
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 | if ((open_mode & GNOME_VFS_OPEN_RANDOM) && |
---|
87 | !VFS_METHOD_HAS_FUNC (uri->method, seek)) { |
---|
88 | GnomeVFSMethodHandle *handle; |
---|
89 | handle = gnome_vfs_seek_emulate (new->uri, method_handle, |
---|
90 | open_mode); |
---|
91 | if (handle) /* Successfully wrapped */ |
---|
92 | new->method_handle = handle; |
---|
93 | } |
---|
94 | |
---|
95 | return new; |
---|
96 | } |
---|
97 | |
---|
98 | void |
---|
99 | gnome_vfs_handle_destroy (GnomeVFSHandle *handle) |
---|
100 | { |
---|
101 | g_return_if_fail (handle != NULL); |
---|
102 | |
---|
103 | gnome_vfs_uri_unref (handle->uri); |
---|
104 | |
---|
105 | g_free (handle); |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | GnomeVFSOpenMode |
---|
110 | gnome_vfs_handle_get_open_mode (GnomeVFSHandle *handle) |
---|
111 | { |
---|
112 | g_return_val_if_fail (handle != NULL, (GnomeVFSOpenMode) 0); |
---|
113 | |
---|
114 | return handle->open_mode; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | /* Actions. */ |
---|
119 | |
---|
120 | GnomeVFSResult |
---|
121 | gnome_vfs_handle_do_close (GnomeVFSHandle *handle, |
---|
122 | GnomeVFSContext *context) |
---|
123 | { |
---|
124 | GnomeVFSResult result; |
---|
125 | |
---|
126 | INVOKE (result, handle, close, (handle->uri->method, handle->method_handle, context)); |
---|
127 | |
---|
128 | /* Even if close has failed, we shut down the handle. */ |
---|
129 | gnome_vfs_handle_destroy (handle); |
---|
130 | |
---|
131 | return result; |
---|
132 | } |
---|
133 | |
---|
134 | GnomeVFSResult |
---|
135 | gnome_vfs_handle_do_read (GnomeVFSHandle *handle, |
---|
136 | gpointer buffer, |
---|
137 | GnomeVFSFileSize num_bytes, |
---|
138 | GnomeVFSFileSize *bytes_read, |
---|
139 | GnomeVFSContext *context) |
---|
140 | { |
---|
141 | INVOKE_AND_RETURN (handle, read, (handle->uri->method, handle->method_handle, |
---|
142 | buffer, num_bytes, bytes_read, |
---|
143 | context)); |
---|
144 | } |
---|
145 | |
---|
146 | GnomeVFSResult |
---|
147 | gnome_vfs_handle_do_write (GnomeVFSHandle *handle, |
---|
148 | gconstpointer buffer, |
---|
149 | GnomeVFSFileSize num_bytes, |
---|
150 | GnomeVFSFileSize *bytes_written, |
---|
151 | GnomeVFSContext *context) |
---|
152 | { |
---|
153 | INVOKE_AND_RETURN (handle, write, (handle->uri->method, handle->method_handle, |
---|
154 | buffer, num_bytes, bytes_written, |
---|
155 | context)); |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | GnomeVFSResult |
---|
160 | gnome_vfs_handle_do_seek (GnomeVFSHandle *handle, |
---|
161 | GnomeVFSSeekPosition whence, |
---|
162 | GnomeVFSFileSize offset, |
---|
163 | GnomeVFSContext *context) |
---|
164 | { |
---|
165 | INVOKE_AND_RETURN (handle, seek, (handle->uri->method, handle->method_handle, |
---|
166 | whence, offset, context)); |
---|
167 | } |
---|
168 | |
---|
169 | GnomeVFSResult |
---|
170 | gnome_vfs_handle_do_tell (GnomeVFSHandle *handle, |
---|
171 | GnomeVFSFileSize *offset_return) |
---|
172 | { |
---|
173 | INVOKE_AND_RETURN (handle, tell, (handle->uri->method, handle->method_handle, |
---|
174 | offset_return)); |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | GnomeVFSResult |
---|
179 | gnome_vfs_handle_do_get_file_info (GnomeVFSHandle *handle, |
---|
180 | GnomeVFSFileInfo *info, |
---|
181 | GnomeVFSFileInfoOptions options, |
---|
182 | GnomeVFSContext *context) |
---|
183 | { |
---|
184 | INVOKE_AND_RETURN (handle, get_file_info_from_handle, |
---|
185 | (handle->uri->method, handle->method_handle, info, options, |
---|
186 | context)); |
---|
187 | } |
---|
188 | |
---|
189 | GnomeVFSResult gnome_vfs_handle_do_truncate (GnomeVFSHandle *handle, |
---|
190 | GnomeVFSFileSize length, |
---|
191 | GnomeVFSContext *context) |
---|
192 | { |
---|
193 | INVOKE_AND_RETURN (handle, truncate_handle, (handle->uri->method, handle->method_handle, length, context)); |
---|
194 | } |
---|