1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* gnome-vfs-private-ops.c - Private synchronous operations for the GNOME
5 Copyright (C) 1999 Free Software Foundation
7 The Gnome Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The Gnome Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the Gnome Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Author: Ettore Perazzoli <ettore@gnu.org> */
24 /* This file provides private versions of the ops for internal use. These are
25 meant to be used within the GNOME VFS and its modules: they are not for
26 public consumption through the external API. */
29 #include "gnome-vfs-cancellable-ops.h"
30 #include "gnome-vfs-method.h"
31 #include "gnome-vfs-handle-private.h"
33 #include <glib/gmessages.h>
34 #include <glib/gutils.h>
38 gnome_vfs_open_uri_cancellable (GnomeVFSHandle **handle,
40 GnomeVFSOpenMode open_mode,
41 GnomeVFSContext *context)
43 GnomeVFSMethodHandle *method_handle;
44 GnomeVFSResult result;
46 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
47 g_return_val_if_fail (uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
48 g_return_val_if_fail (uri->method != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
50 if (gnome_vfs_context_check_cancellation (context))
51 return GNOME_VFS_ERROR_CANCELLED;
53 if (!VFS_METHOD_HAS_FUNC(uri->method, open))
54 return GNOME_VFS_ERROR_NOT_SUPPORTED;
56 result = uri->method->open (uri->method, &method_handle, uri, open_mode,
59 if (result != GNOME_VFS_OK)
62 *handle = _gnome_vfs_handle_new (uri, method_handle, open_mode);
68 gnome_vfs_create_uri_cancellable (GnomeVFSHandle **handle,
70 GnomeVFSOpenMode open_mode,
73 GnomeVFSContext *context)
75 GnomeVFSMethodHandle *method_handle;
76 GnomeVFSResult result;
78 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
79 g_return_val_if_fail (uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
81 if (gnome_vfs_context_check_cancellation (context))
82 return GNOME_VFS_ERROR_CANCELLED;
84 if (!VFS_METHOD_HAS_FUNC(uri->method, create))
85 return GNOME_VFS_ERROR_NOT_SUPPORTED;
87 result = uri->method->create (uri->method, &method_handle, uri, open_mode,
88 exclusive, perm, context);
89 if (result != GNOME_VFS_OK)
92 *handle = _gnome_vfs_handle_new (uri, method_handle, open_mode);
98 gnome_vfs_close_cancellable (GnomeVFSHandle *handle,
99 GnomeVFSContext *context)
101 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
103 if (gnome_vfs_context_check_cancellation (context))
104 return GNOME_VFS_ERROR_CANCELLED;
106 return _gnome_vfs_handle_do_close (handle, context);
110 gnome_vfs_read_cancellable (GnomeVFSHandle *handle,
112 GnomeVFSFileSize bytes,
113 GnomeVFSFileSize *bytes_read,
114 GnomeVFSContext *context)
116 GnomeVFSFileSize dummy_bytes_read;
118 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
120 if (gnome_vfs_context_check_cancellation (context))
121 return GNOME_VFS_ERROR_CANCELLED;
123 if (bytes_read == NULL) {
124 bytes_read = &dummy_bytes_read;
127 return _gnome_vfs_handle_do_read (handle, buffer, bytes, bytes_read,
132 gnome_vfs_write_cancellable (GnomeVFSHandle *handle,
133 gconstpointer buffer,
134 GnomeVFSFileSize bytes,
135 GnomeVFSFileSize *bytes_written,
136 GnomeVFSContext *context)
138 GnomeVFSFileSize dummy_bytes_written;
140 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
142 if (gnome_vfs_context_check_cancellation (context))
143 return GNOME_VFS_ERROR_CANCELLED;
145 if (bytes_written == NULL) {
146 bytes_written = &dummy_bytes_written;
149 return _gnome_vfs_handle_do_write (handle, buffer, bytes,
150 bytes_written, context);
154 gnome_vfs_seek_cancellable (GnomeVFSHandle *handle,
155 GnomeVFSSeekPosition whence,
156 GnomeVFSFileOffset offset,
157 GnomeVFSContext *context)
159 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
161 if (gnome_vfs_context_check_cancellation (context))
162 return GNOME_VFS_ERROR_CANCELLED;
164 return _gnome_vfs_handle_do_seek (handle, whence, offset, context);
168 gnome_vfs_get_file_info_uri_cancellable (GnomeVFSURI *uri,
169 GnomeVFSFileInfo *info,
170 GnomeVFSFileInfoOptions options,
171 GnomeVFSContext *context)
173 GnomeVFSResult result;
175 g_return_val_if_fail (uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
177 if (gnome_vfs_context_check_cancellation (context))
178 return GNOME_VFS_ERROR_CANCELLED;
180 if (!VFS_METHOD_HAS_FUNC(uri->method, get_file_info))
181 return GNOME_VFS_ERROR_NOT_SUPPORTED;
183 result = uri->method->get_file_info (uri->method, uri, info, options,
190 gnome_vfs_get_file_info_from_handle_cancellable (GnomeVFSHandle *handle,
191 GnomeVFSFileInfo *info,
192 GnomeVFSFileInfoOptions options,
193 GnomeVFSContext *context)
196 GnomeVFSResult result;
198 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
200 if (gnome_vfs_context_check_cancellation (context))
201 return GNOME_VFS_ERROR_CANCELLED;
204 result = _gnome_vfs_handle_do_get_file_info (handle, info,
212 gnome_vfs_truncate_uri_cancellable (GnomeVFSURI *uri,
213 GnomeVFSFileSize length,
214 GnomeVFSContext *context)
216 g_return_val_if_fail (uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
218 if (gnome_vfs_context_check_cancellation (context))
219 return GNOME_VFS_ERROR_CANCELLED;
221 if (!VFS_METHOD_HAS_FUNC(uri->method, truncate))
222 return GNOME_VFS_ERROR_NOT_SUPPORTED;
224 return uri->method->truncate(uri->method, uri, length, context);
228 gnome_vfs_truncate_handle_cancellable (GnomeVFSHandle *handle,
229 GnomeVFSFileSize length,
230 GnomeVFSContext *context)
232 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
234 if (gnome_vfs_context_check_cancellation (context))
235 return GNOME_VFS_ERROR_CANCELLED;
237 return _gnome_vfs_handle_do_truncate (handle, length, context);
241 gnome_vfs_make_directory_for_uri_cancellable (GnomeVFSURI *uri,
243 GnomeVFSContext *context)
245 GnomeVFSResult result;
247 g_return_val_if_fail (uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
249 if (gnome_vfs_context_check_cancellation (context))
250 return GNOME_VFS_ERROR_CANCELLED;
252 if (!VFS_METHOD_HAS_FUNC(uri->method, make_directory))
253 return GNOME_VFS_ERROR_NOT_SUPPORTED;
255 result = uri->method->make_directory (uri->method, uri, perm, context);
260 gnome_vfs_find_directory_cancellable (GnomeVFSURI *near_uri,
261 GnomeVFSFindDirectoryKind kind,
262 GnomeVFSURI **result_uri,
263 gboolean create_if_needed,
264 gboolean find_if_needed,
266 GnomeVFSContext *context)
268 GnomeVFSResult result;
270 g_return_val_if_fail (result_uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
272 if (gnome_vfs_context_check_cancellation (context))
273 return GNOME_VFS_ERROR_CANCELLED;
275 if (near_uri != NULL) {
276 gnome_vfs_uri_ref (near_uri);
278 /* assume file: method and the home directory */
279 near_uri = gnome_vfs_uri_new (g_get_home_dir());
282 g_assert (near_uri != NULL);
284 if (!VFS_METHOD_HAS_FUNC(near_uri->method, find_directory)) {
285 gnome_vfs_uri_unref (near_uri);
286 return GNOME_VFS_ERROR_NOT_SUPPORTED;
289 result = near_uri->method->find_directory (near_uri->method, near_uri, kind,
290 result_uri, create_if_needed, find_if_needed, permissions, context);
292 gnome_vfs_uri_unref (near_uri);
297 gnome_vfs_remove_directory_from_uri_cancellable (GnomeVFSURI *uri,
298 GnomeVFSContext *context)
300 GnomeVFSResult result;
302 g_return_val_if_fail (uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
304 if (gnome_vfs_context_check_cancellation (context)) {
305 return GNOME_VFS_ERROR_CANCELLED;
308 if (!VFS_METHOD_HAS_FUNC(uri->method, remove_directory)) {
309 return GNOME_VFS_ERROR_NOT_SUPPORTED;
312 result = uri->method->remove_directory (uri->method, uri, context);
317 gnome_vfs_unlink_from_uri_cancellable (GnomeVFSURI *uri,
318 GnomeVFSContext *context)
320 g_return_val_if_fail (uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
322 if (gnome_vfs_context_check_cancellation (context)) {
323 return GNOME_VFS_ERROR_CANCELLED;
326 if (!VFS_METHOD_HAS_FUNC(uri->method, unlink)) {
327 return GNOME_VFS_ERROR_NOT_SUPPORTED;
330 return uri->method->unlink (uri->method, uri, context);
334 gnome_vfs_create_symbolic_link_cancellable (GnomeVFSURI *uri,
335 const char *target_reference,
336 GnomeVFSContext *context)
338 g_return_val_if_fail (uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
340 if (gnome_vfs_context_check_cancellation (context)) {
341 return GNOME_VFS_ERROR_CANCELLED;
344 if (!VFS_METHOD_HAS_FUNC(uri->method, create_symbolic_link)) {
345 return GNOME_VFS_ERROR_NOT_SUPPORTED;
348 return uri->method->create_symbolic_link (uri->method, uri, target_reference, context);
352 check_same_fs_in_uri (GnomeVFSURI *a,
355 if (a->method != b->method) {
359 if (strcmp (a->method_string, b->method_string) != 0) {
367 gnome_vfs_move_uri_cancellable (GnomeVFSURI *old,
369 gboolean force_replace,
370 GnomeVFSContext *context)
372 g_return_val_if_fail (old != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
373 g_return_val_if_fail (new != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
375 if (gnome_vfs_context_check_cancellation (context))
376 return GNOME_VFS_ERROR_CANCELLED;
378 if (! check_same_fs_in_uri (old, new))
379 return GNOME_VFS_ERROR_NOT_SAME_FILE_SYSTEM;
381 if (gnome_vfs_uri_equal (old, new)) {
385 if (!VFS_METHOD_HAS_FUNC(old->method, move))
386 return GNOME_VFS_ERROR_NOT_SUPPORTED;
388 return old->method->move (old->method, old, new, force_replace, context);
392 gnome_vfs_check_same_fs_uris_cancellable (GnomeVFSURI *a,
394 gboolean *same_fs_return,
395 GnomeVFSContext *context)
397 g_return_val_if_fail (a != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
398 g_return_val_if_fail (b != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
399 g_return_val_if_fail (same_fs_return != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
401 if (gnome_vfs_context_check_cancellation (context))
402 return GNOME_VFS_ERROR_CANCELLED;
404 if (! check_same_fs_in_uri (a, b)) {
405 *same_fs_return = FALSE;
409 if (!VFS_METHOD_HAS_FUNC(a->method, check_same_fs)) {
410 *same_fs_return = FALSE;
414 return a->method->check_same_fs (a->method, a, b, same_fs_return, context);
418 gnome_vfs_set_file_info_cancellable (GnomeVFSURI *a,
419 const GnomeVFSFileInfo *info,
420 GnomeVFSSetFileInfoMask mask,
421 GnomeVFSContext *context)
423 g_return_val_if_fail (a != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
424 g_return_val_if_fail (info != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
426 if (gnome_vfs_context_check_cancellation (context))
427 return GNOME_VFS_ERROR_CANCELLED;
429 if (!VFS_METHOD_HAS_FUNC(a->method, set_file_info))
430 return GNOME_VFS_ERROR_NOT_SUPPORTED;
432 return a->method->set_file_info (a->method, a, info, mask, context);
436 gnome_vfs_file_control_cancellable (GnomeVFSHandle *handle,
437 const char *operation,
438 gpointer operation_data,
439 GnomeVFSContext *context)
441 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
442 g_return_val_if_fail (operation != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
444 if (gnome_vfs_context_check_cancellation (context))
445 return GNOME_VFS_ERROR_CANCELLED;
447 return _gnome_vfs_handle_do_file_control (handle, operation, operation_data, context);