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