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-cancellable-ops.c
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
3    Virtual File System.
4
5    Copyright (C) 1999 Free Software Foundation
6
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.
11
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.
16
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.
21
22    Author: Ettore Perazzoli <ettore@gnu.org> */
23
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.  */
27
28 #include <config.h>
29 #include "gnome-vfs-cancellable-ops.h"
30 #include "gnome-vfs-method.h"
31 #include "gnome-vfs-handle-private.h"
32
33 #include <glib/gmessages.h>
34 #include <glib/gutils.h>
35 #include <string.h>
36
37 GnomeVFSResult
38 gnome_vfs_open_uri_cancellable (GnomeVFSHandle **handle,
39                                 GnomeVFSURI *uri,
40                                 GnomeVFSOpenMode open_mode,
41                                 GnomeVFSContext *context)
42 {
43         GnomeVFSMethodHandle *method_handle;
44         GnomeVFSResult result;
45
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);
49
50         if (gnome_vfs_context_check_cancellation (context))
51                 return GNOME_VFS_ERROR_CANCELLED;
52
53         if (!VFS_METHOD_HAS_FUNC(uri->method, open))
54                 return GNOME_VFS_ERROR_NOT_SUPPORTED;
55
56         result = uri->method->open (uri->method, &method_handle, uri, open_mode,
57                                     context);
58
59         if (result != GNOME_VFS_OK)
60                 return result;
61
62         *handle = _gnome_vfs_handle_new (uri, method_handle, open_mode);
63         
64         return GNOME_VFS_OK;
65 }
66
67 GnomeVFSResult
68 gnome_vfs_create_uri_cancellable (GnomeVFSHandle **handle,
69                                   GnomeVFSURI *uri,
70                                   GnomeVFSOpenMode open_mode,
71                                   gboolean exclusive,
72                                   guint perm,
73                                   GnomeVFSContext *context)
74 {
75         GnomeVFSMethodHandle *method_handle;
76         GnomeVFSResult result;
77
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);
80
81         if (gnome_vfs_context_check_cancellation (context))
82                 return GNOME_VFS_ERROR_CANCELLED;
83
84         if (!VFS_METHOD_HAS_FUNC(uri->method, create))
85                 return GNOME_VFS_ERROR_NOT_SUPPORTED;
86
87         result = uri->method->create (uri->method, &method_handle, uri, open_mode,
88                                       exclusive, perm, context);
89         if (result != GNOME_VFS_OK)
90                 return result;
91
92         *handle = _gnome_vfs_handle_new (uri, method_handle, open_mode);
93
94         return GNOME_VFS_OK;
95 }
96
97 GnomeVFSResult
98 gnome_vfs_close_cancellable (GnomeVFSHandle *handle,
99                              GnomeVFSContext *context)
100 {
101         g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
102
103         if (gnome_vfs_context_check_cancellation (context))
104                 return GNOME_VFS_ERROR_CANCELLED;
105
106         return _gnome_vfs_handle_do_close (handle, context);
107 }
108
109 GnomeVFSResult
110 gnome_vfs_read_cancellable (GnomeVFSHandle *handle,
111                             gpointer buffer,
112                             GnomeVFSFileSize bytes,
113                             GnomeVFSFileSize *bytes_read,
114                             GnomeVFSContext *context)
115 {
116         GnomeVFSFileSize dummy_bytes_read;
117
118         g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
119
120         if (gnome_vfs_context_check_cancellation (context))
121                 return GNOME_VFS_ERROR_CANCELLED;
122
123         if (bytes_read == NULL) {
124                 bytes_read = &dummy_bytes_read;
125         }
126
127         return _gnome_vfs_handle_do_read (handle, buffer, bytes, bytes_read,
128                                          context);
129 }
130
131 GnomeVFSResult
132 gnome_vfs_write_cancellable (GnomeVFSHandle *handle,
133                              gconstpointer buffer,
134                              GnomeVFSFileSize bytes,
135                              GnomeVFSFileSize *bytes_written,
136                              GnomeVFSContext *context)
137 {
138         GnomeVFSFileSize dummy_bytes_written;
139
140         g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
141
142         if (gnome_vfs_context_check_cancellation (context))
143                 return GNOME_VFS_ERROR_CANCELLED;
144
145         if (bytes_written == NULL) {
146                 bytes_written = &dummy_bytes_written;
147         }
148
149         return _gnome_vfs_handle_do_write (handle, buffer, bytes,
150                                           bytes_written, context);
151 }
152
153 GnomeVFSResult
154 gnome_vfs_seek_cancellable (GnomeVFSHandle *handle,
155                             GnomeVFSSeekPosition whence,
156                             GnomeVFSFileOffset offset,
157                             GnomeVFSContext *context)
158 {
159         g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
160
161         if (gnome_vfs_context_check_cancellation (context))
162                 return GNOME_VFS_ERROR_CANCELLED;
163
164         return _gnome_vfs_handle_do_seek (handle, whence, offset, context);
165 }
166
167 GnomeVFSResult
168 gnome_vfs_get_file_info_uri_cancellable (GnomeVFSURI *uri,
169                                          GnomeVFSFileInfo *info,
170                                          GnomeVFSFileInfoOptions options,
171                                          GnomeVFSContext *context)
172 {
173         GnomeVFSResult result;
174
175         g_return_val_if_fail (uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
176         
177         if (gnome_vfs_context_check_cancellation (context))
178                 return GNOME_VFS_ERROR_CANCELLED;
179
180         if (!VFS_METHOD_HAS_FUNC(uri->method, get_file_info))
181                 return GNOME_VFS_ERROR_NOT_SUPPORTED;
182
183         result = uri->method->get_file_info (uri->method, uri, info, options,
184                                              context);
185
186         return result;
187 }
188
189 GnomeVFSResult
190 gnome_vfs_get_file_info_from_handle_cancellable (GnomeVFSHandle *handle,
191                                                  GnomeVFSFileInfo *info,
192                                                  GnomeVFSFileInfoOptions options,
193                                                  GnomeVFSContext *context)
194
195 {
196         GnomeVFSResult result;
197
198         g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
199
200         if (gnome_vfs_context_check_cancellation (context))
201                 return GNOME_VFS_ERROR_CANCELLED;
202
203
204         result =  _gnome_vfs_handle_do_get_file_info (handle, info,
205                                                      options,
206                                                      context);
207
208         return result;
209 }
210
211 GnomeVFSResult
212 gnome_vfs_truncate_uri_cancellable (GnomeVFSURI *uri,
213                                     GnomeVFSFileSize length,
214                                     GnomeVFSContext *context)
215 {
216         g_return_val_if_fail (uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
217
218         if (gnome_vfs_context_check_cancellation (context))
219                 return GNOME_VFS_ERROR_CANCELLED;
220
221         if (!VFS_METHOD_HAS_FUNC(uri->method, truncate))
222                 return GNOME_VFS_ERROR_NOT_SUPPORTED;
223
224         return uri->method->truncate(uri->method, uri, length, context);
225 }
226
227 GnomeVFSResult
228 gnome_vfs_truncate_handle_cancellable (GnomeVFSHandle *handle,
229                                        GnomeVFSFileSize length,
230                                        GnomeVFSContext *context)
231 {
232         g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
233
234         if (gnome_vfs_context_check_cancellation (context))
235                 return GNOME_VFS_ERROR_CANCELLED;
236
237         return _gnome_vfs_handle_do_truncate (handle, length, context);
238 }
239
240 GnomeVFSResult
241 gnome_vfs_make_directory_for_uri_cancellable (GnomeVFSURI *uri,
242                                               guint perm,
243                                               GnomeVFSContext *context)
244 {
245         GnomeVFSResult result;
246
247         g_return_val_if_fail (uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
248
249         if (gnome_vfs_context_check_cancellation (context))
250                 return GNOME_VFS_ERROR_CANCELLED;
251
252         if (!VFS_METHOD_HAS_FUNC(uri->method, make_directory))
253                 return GNOME_VFS_ERROR_NOT_SUPPORTED;
254
255         result = uri->method->make_directory (uri->method, uri, perm, context);
256         return result;
257 }
258
259 GnomeVFSResult
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,
265                                       guint permissions,
266                                       GnomeVFSContext *context)
267 {
268         GnomeVFSResult result;
269
270         g_return_val_if_fail (result_uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
271
272         if (gnome_vfs_context_check_cancellation (context))
273                 return GNOME_VFS_ERROR_CANCELLED;
274
275         if (near_uri != NULL) {
276                 gnome_vfs_uri_ref (near_uri);
277         } else {
278                 /* assume file: method and the home directory */
279                 near_uri = gnome_vfs_uri_new (g_get_home_dir());
280         }
281
282         g_assert (near_uri != NULL);
283                 
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;
287         }
288
289         result = near_uri->method->find_directory (near_uri->method, near_uri, kind,
290                 result_uri, create_if_needed, find_if_needed, permissions, context);
291
292         gnome_vfs_uri_unref (near_uri);
293         return result;
294 }
295
296 GnomeVFSResult
297 gnome_vfs_remove_directory_from_uri_cancellable (GnomeVFSURI *uri,
298                                                  GnomeVFSContext *context)
299 {
300         GnomeVFSResult result;
301
302         g_return_val_if_fail (uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
303
304         if (gnome_vfs_context_check_cancellation (context)) {
305                 return GNOME_VFS_ERROR_CANCELLED;
306         }
307
308         if (!VFS_METHOD_HAS_FUNC(uri->method, remove_directory)) {
309                 return GNOME_VFS_ERROR_NOT_SUPPORTED;
310         }
311
312         result = uri->method->remove_directory (uri->method, uri, context);
313         return result;
314 }
315
316 GnomeVFSResult
317 gnome_vfs_unlink_from_uri_cancellable (GnomeVFSURI *uri,
318                                        GnomeVFSContext *context)
319 {
320         g_return_val_if_fail (uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
321
322         if (gnome_vfs_context_check_cancellation (context)) {
323                 return GNOME_VFS_ERROR_CANCELLED;
324         }
325
326         if (!VFS_METHOD_HAS_FUNC(uri->method, unlink)) {
327                 return GNOME_VFS_ERROR_NOT_SUPPORTED;
328         }
329
330         return uri->method->unlink (uri->method, uri, context);
331 }
332
333 GnomeVFSResult
334 gnome_vfs_create_symbolic_link_cancellable (GnomeVFSURI *uri,
335                                             const char *target_reference,
336                                             GnomeVFSContext *context)
337 {
338         g_return_val_if_fail (uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
339         
340         if (gnome_vfs_context_check_cancellation (context)) {
341                 return GNOME_VFS_ERROR_CANCELLED;
342         }
343
344         if (!VFS_METHOD_HAS_FUNC(uri->method, create_symbolic_link)) {
345                 return GNOME_VFS_ERROR_NOT_SUPPORTED;
346         }
347
348         return uri->method->create_symbolic_link (uri->method, uri, target_reference, context);
349 }
350
351 static gboolean
352 check_same_fs_in_uri (GnomeVFSURI *a,
353                       GnomeVFSURI *b)
354 {
355         if (a->method != b->method) {
356                 return FALSE;
357         }
358         
359         if (strcmp (a->method_string, b->method_string) != 0) {
360                 return FALSE;
361         }
362
363         return TRUE;
364 }
365
366 GnomeVFSResult
367 gnome_vfs_move_uri_cancellable (GnomeVFSURI *old,
368                                 GnomeVFSURI *new,
369                                 gboolean force_replace,
370                                 GnomeVFSContext *context)
371 {
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);
374
375         if (gnome_vfs_context_check_cancellation (context))
376                 return GNOME_VFS_ERROR_CANCELLED;
377
378         if (! check_same_fs_in_uri (old, new))
379                 return GNOME_VFS_ERROR_NOT_SAME_FILE_SYSTEM;
380
381         if (gnome_vfs_uri_equal (old, new)) {
382                 return GNOME_VFS_OK;
383         }
384
385         if (!VFS_METHOD_HAS_FUNC(old->method, move))
386                 return GNOME_VFS_ERROR_NOT_SUPPORTED;
387
388         return old->method->move (old->method, old, new, force_replace, context);
389 }
390
391 GnomeVFSResult
392 gnome_vfs_check_same_fs_uris_cancellable (GnomeVFSURI *a,
393                                           GnomeVFSURI *b,
394                                           gboolean *same_fs_return,
395                                           GnomeVFSContext *context)
396 {
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);
400
401         if (gnome_vfs_context_check_cancellation (context))
402                 return GNOME_VFS_ERROR_CANCELLED;
403
404         if (! check_same_fs_in_uri (a, b)) {
405                 *same_fs_return = FALSE;
406                 return GNOME_VFS_OK;
407         }
408
409         if (!VFS_METHOD_HAS_FUNC(a->method, check_same_fs)) {
410                 *same_fs_return = FALSE;
411                 return GNOME_VFS_OK;
412         }
413
414         return a->method->check_same_fs (a->method, a, b, same_fs_return, context);
415 }
416
417 GnomeVFSResult
418 gnome_vfs_set_file_info_cancellable (GnomeVFSURI *a,
419                                      const GnomeVFSFileInfo *info,
420                                      GnomeVFSSetFileInfoMask mask,
421                                      GnomeVFSContext *context)
422 {
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);
425
426         if (gnome_vfs_context_check_cancellation (context))
427                 return GNOME_VFS_ERROR_CANCELLED;
428
429         if (!VFS_METHOD_HAS_FUNC(a->method, set_file_info))
430                 return GNOME_VFS_ERROR_NOT_SUPPORTED;
431
432         return a->method->set_file_info (a->method, a, info, mask, context);
433 }
434
435 GnomeVFSResult
436 gnome_vfs_file_control_cancellable (GnomeVFSHandle *handle,
437                                     const char *operation,
438                                     gpointer operation_data,
439                                     GnomeVFSContext *context)
440 {
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);
443
444         if (gnome_vfs_context_check_cancellation (context))
445                 return GNOME_VFS_ERROR_CANCELLED;
446
447         return _gnome_vfs_handle_do_file_control (handle, operation, operation_data, context);
448 }
449