1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* gnome-vfs-ops.c - Synchronous operations for the GNOME Virtual File
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> */
25 #include "gnome-vfs-ops.h"
26 #include "gnome-vfs-monitor-private.h"
27 #include "gnome-vfs-cancellable-ops.h"
28 #include "gnome-vfs-handle-private.h"
29 #include <glib/gmessages.h>
33 * @handle: A pointer to a pointer to a GnomeVFSHandle object
34 * @text_uri: String representing the URI to open
35 * @open_mode: Open mode
37 * Open @text_uri according to mode @open_mode. On return, @handle will then
38 * contain a pointer to a handle for the open file.
40 * Return value: An integer representing the result of the operation
43 gnome_vfs_open (GnomeVFSHandle **handle,
44 const gchar *text_uri,
45 GnomeVFSOpenMode open_mode)
48 GnomeVFSResult result;
50 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
51 g_return_val_if_fail (text_uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
53 uri = gnome_vfs_uri_new (text_uri);
55 return GNOME_VFS_ERROR_INVALID_URI;
57 result = gnome_vfs_open_uri (handle, uri, open_mode);
59 gnome_vfs_uri_unref (uri);
66 * @handle: A pointer to a pointer to a GnomeVFSHandle object
68 * @open_mode: Open mode
70 * Open @uri according to mode @open_mode. On return, @handle will then
71 * contain a pointer to a handle for the open file.
73 * Return value: An integer representing the result of the operation
76 gnome_vfs_open_uri (GnomeVFSHandle **handle,
78 GnomeVFSOpenMode open_mode)
80 return gnome_vfs_open_uri_cancellable (handle, uri, open_mode, NULL);
85 * @handle: A pointer to a pointer to a GnomeVFSHandle object
86 * @text_uri: String representing the URI to create
87 * @open_mode: mode to leave the file opened in after creation (or %GNOME_VFS_OPEN_MODE_NONE
88 * to leave the file closed after creation)
89 * @exclusive: Whether the file should be created in "exclusive" mode:
90 * i.e. if this flag is nonzero, operation will fail if a file with the
91 * same name already exists.
92 * @perm: Bitmap representing the permissions for the newly created file
95 * Create @uri according to mode @open_mode. On return, @handle will then
96 * contain a pointer to a handle for the open file.
98 * Return value: An integer representing the result of the operation
101 gnome_vfs_create (GnomeVFSHandle **handle,
102 const gchar *text_uri,
103 GnomeVFSOpenMode open_mode,
108 GnomeVFSResult result;
110 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
111 g_return_val_if_fail (text_uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
113 uri = gnome_vfs_uri_new (text_uri);
115 return GNOME_VFS_ERROR_INVALID_URI;
117 result = gnome_vfs_create_uri (handle, uri, open_mode, exclusive, perm);
119 gnome_vfs_uri_unref (uri);
125 * gnome_vfs_create_uri:
126 * @handle: A pointer to a pointer to a GnomeVFSHandle object
127 * @uri: URI for the file to create
128 * @open_mode: Open mode
129 * @exclusive: Whether the file should be created in "exclusive" mode:
130 * i.e. if this flag is nonzero, operation will fail if a file with the
131 * same name already exists.
132 * @perm: Bitmap representing the permissions for the newly created file
135 * Create @uri according to mode @open_mode. On return, @handle will then
136 * contain a pointer to a handle for the open file.
138 * Return value: An integer representing the result of the operation
141 gnome_vfs_create_uri (GnomeVFSHandle **handle,
143 GnomeVFSOpenMode open_mode,
147 return gnome_vfs_create_uri_cancellable (handle, uri, open_mode,
148 exclusive, perm, NULL);
153 * @handle: A pointer to a GnomeVFSHandle object
155 * Close file associated with @handle.
157 * Return value: An integer representing the result of the operation.
160 gnome_vfs_close (GnomeVFSHandle *handle)
162 return gnome_vfs_close_cancellable (handle, NULL);
167 * @handle: Handle of the file to read data from
168 * @buffer: Pointer to a buffer that must be at least @bytes bytes large
169 * @bytes: Number of bytes to read
170 * @bytes_read: Pointer to a variable that will hold the number of bytes
171 * effectively read on return.
173 * Read @bytes from @handle. As with Unix system calls, the number of
174 * bytes read can effectively be less than @bytes on return and will be
175 * stored in @bytes_read.
177 * Return value: An integer representing the result of the operation
180 gnome_vfs_read (GnomeVFSHandle *handle,
182 GnomeVFSFileSize bytes,
183 GnomeVFSFileSize *bytes_read)
185 return gnome_vfs_read_cancellable (handle, buffer, bytes, bytes_read,
191 * @handle: Handle of the file to write data to
192 * @buffer: Pointer to the buffer containing the data to be written
193 * @bytes: Number of bytes to write
194 * @bytes_written: Pointer to a variable that will hold the number of bytes
195 * effectively written on return.
197 * Write @bytes into the file opened through @handle. As with Unix system
198 * calls, the number of bytes written can effectively be less than @bytes on
199 * return and will be stored in @bytes_written.
201 * Return value: An integer representing the result of the operation
204 gnome_vfs_write (GnomeVFSHandle *handle,
205 gconstpointer buffer,
206 GnomeVFSFileSize bytes,
207 GnomeVFSFileSize *bytes_written)
209 return gnome_vfs_write_cancellable (handle, buffer, bytes,
210 bytes_written, NULL);
215 * @handle: Handle for which the current position must be changed
216 * @whence: Integer value representing the starting position
217 * @offset: Number of bytes to skip from the position specified by @whence
218 * (a positive value means to move forward; a negative one to move backwards)
220 * Set the current position for reading/writing through @handle.
225 gnome_vfs_seek (GnomeVFSHandle *handle,
226 GnomeVFSSeekPosition whence,
227 GnomeVFSFileOffset offset)
229 return gnome_vfs_seek_cancellable (handle, whence, offset, NULL);
234 * @handle: Handle for which the current position must be retrieved
235 * @offset_return: Pointer to a variable that will contain the current position
238 * Return the current position on @handle. This is the point in the file
239 * pointed to by handle that reads and writes will occur on.
241 * Return value: An integer representing the result of the operation
244 gnome_vfs_tell (GnomeVFSHandle *handle,
245 GnomeVFSFileSize *offset_return)
247 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
249 return _gnome_vfs_handle_do_tell (handle, offset_return);
253 * gnome_vfs_get_file_info:
254 * @text_uri: URI of the file for which information will be retrieved
255 * @info: Pointer to a GnomeVFSFileInfo object that will hold the information
256 * for the file on return
257 * @options: Options for retrieving file information
258 * to retrieve for the file
260 * Retrieve information about @text_uri. The information will be stored in
263 * Return value: An integer representing the result of the operation
266 gnome_vfs_get_file_info (const gchar *text_uri,
267 GnomeVFSFileInfo *info,
268 GnomeVFSFileInfoOptions options)
271 GnomeVFSResult result;
273 uri = gnome_vfs_uri_new (text_uri);
276 return GNOME_VFS_ERROR_NOT_SUPPORTED;
278 result = gnome_vfs_get_file_info_uri(uri, info, options);
279 gnome_vfs_uri_unref (uri);
285 * gnome_vfs_get_file_info_uri:
286 * @uri: URI of the file for which information will be retrieved
287 * @info: Pointer to a GnomeVFSFileInfo object that will hold the information
288 * for the file on return
289 * @options: Options for retrieving file information
290 * to retrieve for the file
292 * Retrieve information about @text_uri. The information will be stored in
295 * Return value: An integer representing the result of the operation
298 gnome_vfs_get_file_info_uri (GnomeVFSURI *uri,
299 GnomeVFSFileInfo *info,
300 GnomeVFSFileInfoOptions options)
302 return gnome_vfs_get_file_info_uri_cancellable (uri,
309 * gnome_vfs_get_file_info_from_handle:
310 * @handle: Handle of the file for which information must be retrieved
311 * @info: Pointer to a GnomeVFSFileInfo object that will hold the information
312 * for the file on return
313 * @options: Options for retrieving file information
314 * to retrieve for the file
316 * Retrieve information about an open file. The information will be stored in
319 * Return value: An integer representing the result of the operation
322 gnome_vfs_get_file_info_from_handle (GnomeVFSHandle *handle,
323 GnomeVFSFileInfo *info,
324 GnomeVFSFileInfoOptions options)
326 return gnome_vfs_get_file_info_from_handle_cancellable (handle, info,
332 * gnome_vfs_truncate:
333 * @text_uri: URI of the file to be truncated
334 * @length: length of the new file at @text_uri
336 * Truncate the file at @text_uri to @length bytes.
338 * Return value: An integer representing the result of the operation
341 gnome_vfs_truncate (const char *text_uri, GnomeVFSFileSize length)
344 GnomeVFSResult result;
346 uri = gnome_vfs_uri_new (text_uri);
349 return GNOME_VFS_ERROR_NOT_SUPPORTED;
351 result = gnome_vfs_truncate_uri(uri, length);
352 gnome_vfs_uri_unref (uri);
359 * gnome_vfs_truncate_uri:
360 * @uri: URI of the file to be truncated
361 * @length: length of the new file at @uri
363 * Truncate the file at @uri to be only @length bytes. Data past @length
364 * bytes will be discarded.
366 * Return value: An integer representing the result of the operation
369 gnome_vfs_truncate_uri (GnomeVFSURI *uri, GnomeVFSFileSize length)
371 return gnome_vfs_truncate_uri_cancellable(uri, length, NULL);
375 * gnome_vfs_truncate_handle:
376 * @handle: a handle to the file to be truncated
377 * @length: length of the new file the handle is open to
379 * Truncate the file pointed to be @handle to be only @length bytes.
380 * Data past @length bytes will be discarded.
382 * Return value: An integer representing the result of the operation
385 gnome_vfs_truncate_handle (GnomeVFSHandle *handle, GnomeVFSFileSize length)
387 return gnome_vfs_truncate_handle_cancellable(handle, length, NULL);
391 * gnome_vfs_make_directory_for_uri:
392 * @uri: URI of the directory to be created
393 * @perm: Unix-style permissions for the newly created directory
395 * Create a directory at @uri. Only succeeds if a file or directory
396 * does not already exist at @uri.
398 * Return value: An integer representing the result of the operation
401 gnome_vfs_make_directory_for_uri (GnomeVFSURI *uri,
404 return gnome_vfs_make_directory_for_uri_cancellable (uri, perm, NULL);
408 * gnome_vfs_make_directory:
409 * @text_uri: URI of the directory to be created
410 * @perm: Unix-style permissions for the newly created directory
412 * Create @text_uri as a directory.
414 * Return value: An integer representing the result of the operation
417 gnome_vfs_make_directory (const gchar *text_uri,
420 GnomeVFSResult result;
423 g_return_val_if_fail (text_uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
425 uri = gnome_vfs_uri_new (text_uri);
427 return GNOME_VFS_ERROR_INVALID_URI;
429 result = gnome_vfs_make_directory_for_uri (uri, perm);
431 gnome_vfs_uri_unref (uri);
437 * gnome_vfs_remove_directory_from_uri:
438 * @uri: URI of the directory to be removed
440 * Remove @uri. @uri must be an empty directory.
442 * Return value: An integer representing the result of the operation
445 gnome_vfs_remove_directory_from_uri (GnomeVFSURI *uri)
447 return gnome_vfs_remove_directory_from_uri_cancellable (uri, NULL);
451 * gnome_vfs_remove_directory:
452 * @text_uri: URI of the directory to be removed
454 * Remove @text_uri. @text_uri must be an empty directory.
456 * Return value: An integer representing the result of the operation
459 gnome_vfs_remove_directory (const gchar *text_uri)
461 GnomeVFSResult result;
464 g_return_val_if_fail (text_uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
466 uri = gnome_vfs_uri_new (text_uri);
468 return GNOME_VFS_ERROR_INVALID_URI;
470 result = gnome_vfs_remove_directory_from_uri (uri);
472 gnome_vfs_uri_unref (uri);
478 * gnome_vfs_unlink_from_uri:
479 * @uri: URI of the file to be unlinked
481 * Unlink @uri (i.e. delete the file).
483 * Return value: An integer representing the result of the operation
486 gnome_vfs_unlink_from_uri (GnomeVFSURI *uri)
488 return gnome_vfs_unlink_from_uri_cancellable (uri, NULL);
492 * gnome_vfs_create_symbolic_link:
493 * @uri: URI to create a link at
494 * @target_reference: URI "reference" to point the link to (URI or relative path)
496 * Creates a symbolic link, or eventually, a URI link (as necessary)
497 * at @uri pointing to @target_reference
499 * Return value: An integer representing the result of the operation
502 gnome_vfs_create_symbolic_link (GnomeVFSURI *uri, const gchar *target_reference)
504 return gnome_vfs_create_symbolic_link_cancellable (uri, target_reference, NULL);
509 * @text_uri: URI of the file to be unlinked
511 * Unlink @text_uri (i.e. delete the file).
513 * Return value: An integer representing the result of the operation
516 gnome_vfs_unlink (const gchar *text_uri)
518 GnomeVFSResult result;
521 g_return_val_if_fail (text_uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
523 uri = gnome_vfs_uri_new (text_uri);
525 return GNOME_VFS_ERROR_INVALID_URI;
527 result = gnome_vfs_unlink_from_uri (uri);
529 gnome_vfs_uri_unref (uri);
535 * gnome_vfs_move_uri:
536 * @old_uri: Source URI
537 * @new_uri: Destination URI
538 * @force_replace: If %TRUE, move target to @new_uri even if there
539 * is already a file at @new_uri. If there is a file, it will be discarded.
541 * Move a file from URI @old_uri to @new_uri. This will only work if @old_uri
542 * and @new_uri are on the same file system. Otherwise, it is necessary
543 * to use the more general %gnome_vfs_xfer_uri() function.
545 * Return value: An integer representing the result of the operation.
548 gnome_vfs_move_uri (GnomeVFSURI *old_uri,
549 GnomeVFSURI *new_uri,
550 gboolean force_replace)
552 return gnome_vfs_move_uri_cancellable (old_uri, new_uri,
553 force_replace, NULL);
558 * @old_text_uri: Source URI
559 * @new_text_uri: Destination URI
560 * @force_replace: if %TRUE, perform the operation even if it unlinks an existing
561 * file at @new_text_uri
563 * Move a file from URI @old_text_uri to @new_text_uri. This will only work
564 * if @old_text_uri and @new_text_uri are on the same file system. Otherwise,
565 * it is necessary to use the more general %gnome_vfs_xfer_uri() function.
567 * Return value: An integer representing the result of the operation.
570 gnome_vfs_move (const gchar *old_text_uri,
571 const gchar *new_text_uri,
572 gboolean force_replace)
574 GnomeVFSURI *old_uri, *new_uri;
575 GnomeVFSResult retval;
577 g_return_val_if_fail (old_text_uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
578 g_return_val_if_fail (new_text_uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
580 old_uri = gnome_vfs_uri_new (old_text_uri);
582 return GNOME_VFS_ERROR_INVALID_URI;
584 new_uri = gnome_vfs_uri_new (new_text_uri);
585 if (new_uri == NULL) {
586 gnome_vfs_uri_unref (old_uri);
587 return GNOME_VFS_ERROR_INVALID_URI;
590 retval = gnome_vfs_move_uri (old_uri, new_uri, force_replace);
592 gnome_vfs_uri_unref (old_uri);
593 gnome_vfs_uri_unref (new_uri);
599 * gnome_vfs_check_same_fs_uris:
601 * @target_uri: Another URI
602 * @same_fs_return: Pointer to a boolean variable which will be set to %TRUE
603 * if @source_uri and @target_uri are on the same file system on return.
605 * Check if @source_uri and @target_uri are on the same file system.
607 * Return value: An integer representing the result of the operation.
610 gnome_vfs_check_same_fs_uris (GnomeVFSURI *source_uri,
611 GnomeVFSURI *target_uri,
612 gboolean *same_fs_return)
614 return gnome_vfs_check_same_fs_uris_cancellable (source_uri,
621 * gnome_vfs_check_same_fs:
623 * @target: Another URI
624 * @same_fs_return: Pointer to a boolean variable which will be set to %TRUE
626 * Return %TRUE if @source and @target are on the same file system.
628 * Return value: An integer representing the result of the operation.
631 gnome_vfs_check_same_fs (const gchar *source,
633 gboolean *same_fs_return)
635 GnomeVFSURI *a_uri, *b_uri;
636 GnomeVFSResult retval;
638 g_return_val_if_fail (source != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
639 g_return_val_if_fail (target != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
640 g_return_val_if_fail (same_fs_return != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
642 *same_fs_return = FALSE;
644 a_uri = gnome_vfs_uri_new (source);
646 return GNOME_VFS_ERROR_INVALID_URI;
648 b_uri = gnome_vfs_uri_new (target);
650 gnome_vfs_uri_unref (a_uri);
651 return GNOME_VFS_ERROR_INVALID_URI;
654 retval = gnome_vfs_check_same_fs_uris (a_uri, b_uri, same_fs_return);
656 gnome_vfs_uri_unref (a_uri);
657 gnome_vfs_uri_unref (b_uri);
663 * gnome_vfs_set_file_info_uri:
665 * @info: Information that must be set for the file
666 * @mask: Bit mask representing which fields of @info need to be set
668 * Set file information for @uri; only the information for which the
669 * corresponding bit in @mask is set is actually modified.
671 * Return value: An integer representing the result of the operation.
674 gnome_vfs_set_file_info_uri (GnomeVFSURI *uri,
675 GnomeVFSFileInfo *info,
676 GnomeVFSSetFileInfoMask mask)
678 return gnome_vfs_set_file_info_cancellable (uri, info, mask, NULL);
682 * gnome_vfs_set_file_info:
684 * @info: Information that must be set for the file
685 * @mask: Bit mask representing which fields of @info need to be set
687 * Set file information for @uri; only the information for which the
688 * corresponding bit in @mask is set is actually modified.
690 * Return value: An integer representing the result of the operation.
693 gnome_vfs_set_file_info (const gchar *text_uri,
694 GnomeVFSFileInfo *info,
695 GnomeVFSSetFileInfoMask mask)
698 GnomeVFSResult result;
700 uri = gnome_vfs_uri_new (text_uri);
702 return GNOME_VFS_ERROR_INVALID_URI;
704 result = gnome_vfs_set_file_info_uri (uri, info, mask);
706 gnome_vfs_uri_unref (uri);
712 * gnome_vfs_uri_exists:
715 * Check if the URI points to an existing entity.
717 * Return value: TRUE if URI exists.
720 gnome_vfs_uri_exists (GnomeVFSURI *uri)
722 GnomeVFSFileInfo *info;
723 GnomeVFSResult result;
725 info = gnome_vfs_file_info_new ();
726 result = gnome_vfs_get_file_info_uri (uri, info, GNOME_VFS_FILE_INFO_DEFAULT);
727 gnome_vfs_file_info_unref (info);
729 return result == GNOME_VFS_OK;
733 * gnome_vfs_monitor_add:
734 * @handle: after the call, @handle will be a pointer to an operation handle
735 * @text_uri: URI to monitor
736 * @monitor_type: add a directory or file monitor
737 * @callback: function to call when the monitor is tripped
738 * @user_data: data to pass to @callback
740 * Watch the file or directory at @text_uri for changes (or the creation/deletion of the file)
741 * and call @callback when there is a change. If a directory monitor is added, @callback is
742 * notified when any file in the directory changes.
744 * Return value: an integer representing the success of the operation
747 gnome_vfs_monitor_add (GnomeVFSMonitorHandle **handle,
748 const gchar *text_uri,
749 GnomeVFSMonitorType monitor_type,
750 GnomeVFSMonitorCallback callback,
753 GnomeVFSURI *uri = gnome_vfs_uri_new (text_uri);
754 GnomeVFSResult result;
757 return GNOME_VFS_ERROR_INVALID_URI;
760 if (!VFS_METHOD_HAS_FUNC(uri->method, monitor_add)) {
761 gnome_vfs_uri_unref (uri);
762 return GNOME_VFS_ERROR_NOT_SUPPORTED;
765 result = _gnome_vfs_monitor_do_add (uri->method, handle, uri,
766 monitor_type, callback,
769 gnome_vfs_uri_unref (uri);
775 * gnome_vfs_monitor_cancel:
776 * @handle: handle of the monitor to cancel
778 * Cancel the monitor pointed to be @handle.
780 * Return value: an integer representing the success of the operation
783 gnome_vfs_monitor_cancel (GnomeVFSMonitorHandle *handle)
785 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
787 return _gnome_vfs_monitor_do_cancel (handle);
791 * gnome_vfs_file_control:
792 * @handle: handle of the file to affect
793 * @operation: The operation to execute
794 * @operation_data: The data needed to execute the operation
796 * Execute a backend dependent operation specified by the string @operation.
797 * This is typically used for specialized vfs backends that need additional
798 * operations that gnome-vfs doesn't have. Compare it to the unix call ioctl().
799 * The format of @operation_data depends on the operation. Operation that are
800 * backend specific are normally namespaced by their module name.
802 * Return value: an integer representing the success of the operation
805 gnome_vfs_file_control (GnomeVFSHandle *handle,
806 const char *operation,
807 gpointer operation_data)
809 return gnome_vfs_file_control_cancellable (handle, operation, operation_data, NULL);