source: trunk/third/gnome-vfs/libgnomevfs/gnome-vfs-ops.c @ 15858

Revision 15858, 18.7 KB checked in by ghudson, 24 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r15857, which included commits to RCS files with non-trunk default branches.
Line 
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
3   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#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
28#include "gnome-vfs.h"
29#include "gnome-vfs-private.h"
30
31/**
32 * gnome_vfs_open:
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
36 *
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.
39 *
40 * Return value: An integer representing the result of the operation
41 **/
42GnomeVFSResult
43gnome_vfs_open (GnomeVFSHandle **handle,
44                const gchar *text_uri,
45                GnomeVFSOpenMode open_mode)
46{
47        GnomeVFSURI *uri;
48        GnomeVFSResult result;
49
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);
52
53        uri = gnome_vfs_uri_new (text_uri);
54        if (uri == NULL)
55                return GNOME_VFS_ERROR_INVALID_URI;
56
57        result = gnome_vfs_open_uri (handle, uri, open_mode);
58
59        gnome_vfs_uri_unref (uri);
60
61        return result;
62}
63
64/**
65 * gnome_vfs_open_uri:
66 * @handle: A pointer to a pointer to a GnomeVFSHandle object
67 * @uri: URI to open
68 * @open_mode: Open mode
69 *
70 * Open @uri according to mode @open_mode.  On return, @*handle will then
71 * contain a pointer to a handle for the open file.
72 *
73 * Return value: An integer representing the result of the operation
74 **/
75GnomeVFSResult
76gnome_vfs_open_uri (GnomeVFSHandle **handle,
77                    GnomeVFSURI *uri,
78                    GnomeVFSOpenMode open_mode)
79{
80        return gnome_vfs_open_uri_cancellable (handle, uri, open_mode, NULL);
81}
82
83/**
84 * gnome_vfs_create:
85 * @handle: A pointer to a pointer to a GnomeVFSHandle object
86 * @text_uri: String representing the URI to create
87 * @open_mode: Open mode
88 * @exclusive: Whether the file should be created in "exclusive" mode:
89 * i.e. if this flag is nonzero, operation will fail if a file with the
90 * same name already exists.
91 * @perm: Bitmap representing the permissions for the newly created file
92 * (Unix style).
93 *
94 * Create @uri according to mode @open_mode.  On return, @*handle will then
95 * contain a pointer to a handle for the open file.
96 *
97 * Return value: An integer representing the result of the operation
98 **/
99GnomeVFSResult
100gnome_vfs_create (GnomeVFSHandle **handle,
101                  const gchar *text_uri,
102                  GnomeVFSOpenMode open_mode,
103                  gboolean exclusive,
104                  guint perm)
105{
106        GnomeVFSURI *uri;
107        GnomeVFSResult result;
108
109        g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
110        g_return_val_if_fail (text_uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
111
112        uri = gnome_vfs_uri_new (text_uri);
113        if (uri == NULL)
114                return GNOME_VFS_ERROR_INVALID_URI;
115
116        result = gnome_vfs_create_uri (handle, uri, open_mode, exclusive, perm);
117
118        gnome_vfs_uri_unref (uri);
119
120        return result;
121}
122
123/**
124 * gnome_vfs_create_uri:
125 * @handle: A pointer to a pointer to a GnomeVFSHandle object
126 * @uri: URI for the file to create
127 * @open_mode: Open mode
128 * @exclusive: Whether the file should be created in "exclusive" mode:
129 * i.e. if this flag is nonzero, operation will fail if a file with the
130 * same name already exists.
131 * @perm: Bitmap representing the permissions for the newly created file
132 * (Unix style).
133 *
134 * Create @uri according to mode @open_mode.  On return, @*handle will then
135 * contain a pointer to a handle for the open file.
136 *
137 * Return value: An integer representing the result of the operation
138 **/
139GnomeVFSResult
140gnome_vfs_create_uri (GnomeVFSHandle **handle,
141                      GnomeVFSURI *uri,
142                      GnomeVFSOpenMode open_mode,
143                      gboolean exclusive,
144                      guint perm)
145{
146        return gnome_vfs_create_uri_cancellable (handle, uri, open_mode,
147                                                 exclusive, perm, NULL);
148}
149
150/**
151 * gnome_vfs_close:
152 * @handle: A pointer to a GnomeVFSHandle object
153 *
154 * Close file associated with @handle.
155 *
156 * Return value: An integer representing the result of the operation.
157 **/
158GnomeVFSResult
159gnome_vfs_close (GnomeVFSHandle *handle)
160{
161        return gnome_vfs_close_cancellable (handle, NULL);
162}
163
164/**
165 * gnome_vfs_read:
166 * @handle: Handle of the file to read data from
167 * @buffer: Pointer to a buffer that must be at least @bytes bytes large
168 * @bytes: Number of bytes to read
169 * @bytes_read: Pointer to a variable that will hold the number of bytes
170 * effectively read on return.
171 *
172 * Read @bytes from @handle.  As with Unix system calls, the number of
173 * bytes read can effectively be less than @bytes on return and will be
174 * stored in @*bytes_read.
175 *
176 * Return value: An integer representing the result of the operation
177 **/
178GnomeVFSResult
179gnome_vfs_read (GnomeVFSHandle *handle,
180                gpointer buffer,
181                GnomeVFSFileSize bytes,
182                GnomeVFSFileSize *bytes_read)
183{
184        return gnome_vfs_read_cancellable (handle, buffer, bytes, bytes_read,
185                                           NULL);
186}
187
188/**
189 * gnome_vfs_write:
190 * @handle: Handle of the file to write data to
191 * @buffer: Pointer to the buffer containing the data to be written
192 * @bytes: Number of bytes to write
193 * @bytes_write: Pointer to a variable that will hold the number of bytes
194 * effectively written on return.
195 *
196 * Write @bytes into the file opened through @handle.  As with Unix system
197 * calls, the number of bytes written can effectively be less than @bytes on
198 * return and will be stored in @*bytes_written.
199 *
200 * Return value: An integer representing the result of the operation
201 **/
202GnomeVFSResult
203gnome_vfs_write (GnomeVFSHandle *handle,
204                 gconstpointer buffer,
205                 GnomeVFSFileSize bytes,
206                 GnomeVFSFileSize *bytes_written)
207{
208        return gnome_vfs_write_cancellable (handle, buffer, bytes,
209                                            bytes_written, NULL);
210}
211
212/**
213 * gnome_vfs_seek:
214 * @handle: Handle for which the current position must be changed
215 * @whence: Integer value representing the starting position
216 * @offset: Number of bytes to skip from the position specified by @whence
217 * (a positive value means to move forward; a negative one to move backwards)
218 *
219 * Set the current position for reading/writing through @handle.
220 *
221 * Return value:
222 **/
223GnomeVFSResult
224gnome_vfs_seek (GnomeVFSHandle *handle,
225                GnomeVFSSeekPosition whence,
226                GnomeVFSFileOffset offset)
227{
228        return gnome_vfs_seek_cancellable (handle, whence, offset, NULL);
229}
230
231/**
232 * gnome_vfs_tell:
233 * @handle: Handle for which the current position must be retrieved
234 * @offset_return: Pointer to a variable that will contain the current position
235 * on return
236 *
237 * Return the current position on @handle.
238 *
239 * Return value: An integer representing the result of the operation
240 **/
241GnomeVFSResult
242gnome_vfs_tell (GnomeVFSHandle *handle,
243                GnomeVFSFileSize *offset_return)
244{
245        g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
246
247        return gnome_vfs_handle_do_tell (handle, offset_return);
248}
249
250/**
251 * gnome_vfs_get_file_info:
252 * @text_uri: URI of the file for which information will be retrieved
253 * @info: Pointer to a GnomeVFSFileInfo object that will hold the information
254 * for the file on return
255 * @options: Options for retrieving file information
256 * to retrieve for the file
257 *
258 * Retrieve information about @text_uri.  The information will be stored in
259 * @*info.
260 *
261 * Return value: An integer representing the result of the operation
262 **/
263GnomeVFSResult
264gnome_vfs_get_file_info (const gchar *text_uri,
265                         GnomeVFSFileInfo *info,
266                         GnomeVFSFileInfoOptions options)
267{
268        GnomeVFSURI *uri;
269        GnomeVFSResult result;
270
271        uri = gnome_vfs_uri_new (text_uri);
272
273        if (uri == NULL)
274                return GNOME_VFS_ERROR_NOT_SUPPORTED;
275       
276        result = gnome_vfs_get_file_info_uri(uri, info, options);
277        gnome_vfs_uri_unref (uri);
278
279        return result;
280}
281
282/**
283 * gnome_vfs_get_file_info_uri:
284 * @uri: URI of the file for which information will be retrieved
285 * @info: Pointer to a GnomeVFSFileInfo object that will hold the information
286 * for the file on return
287 * @options: Options for retrieving file information
288 * to retrieve for the file
289 *
290 * Retrieve information about @text_uri.  The information will be stored in
291 * @info.
292 *
293 * Return value: An integer representing the result of the operation
294 **/
295GnomeVFSResult
296gnome_vfs_get_file_info_uri (GnomeVFSURI *uri,
297                             GnomeVFSFileInfo *info,
298                             GnomeVFSFileInfoOptions options)
299{
300        return gnome_vfs_get_file_info_uri_cancellable (uri,
301                                                        info,
302                                                        options,
303                                                        NULL);
304}
305
306/**
307 * gnome_vfs_get_file_info_from_handle:
308 * @handle: Handle of the file for which information must be retrieved
309 * @info: Pointer to a GnomeVFSFileInfo object that will hold the information
310 * for the file on return
311 * @options: Options for retrieving file information
312 * to retrieve for the file
313 *
314 * Retrieve information about an open file.  The information will be stored in
315 * @*info.
316 *
317 * Return value: An integer representing the result of the operation
318 **/
319GnomeVFSResult
320gnome_vfs_get_file_info_from_handle (GnomeVFSHandle *handle,
321                                     GnomeVFSFileInfo *info,
322                                     GnomeVFSFileInfoOptions options)
323{
324        return gnome_vfs_get_file_info_from_handle_cancellable (handle, info,
325                                                                options,
326                                                                NULL);
327}
328
329GnomeVFSResult
330gnome_vfs_truncate (const char *text_uri, GnomeVFSFileSize length)
331{
332        GnomeVFSURI *uri;
333        GnomeVFSResult result;
334
335        uri = gnome_vfs_uri_new (text_uri);
336
337        if (uri == NULL)
338                return GNOME_VFS_ERROR_NOT_SUPPORTED;
339
340        result = gnome_vfs_truncate_uri(uri, length);
341        gnome_vfs_uri_unref (uri);
342
343        return result;
344}
345
346
347GnomeVFSResult
348gnome_vfs_truncate_uri (GnomeVFSURI *uri, GnomeVFSFileSize length)
349{
350        return gnome_vfs_truncate_uri_cancellable(uri, length, NULL);
351}
352
353GnomeVFSResult
354gnome_vfs_truncate_handle (GnomeVFSHandle *handle, GnomeVFSFileSize length)
355{
356        return gnome_vfs_truncate_handle_cancellable(handle, length, NULL);
357}
358
359/**
360 * gnome_vfs_make_directory_for_uri:
361 * @uri: URI of the directory to be created
362 * @perm: Unix-style permissions for the newly created directory
363 *
364 * Create @uri as a directory.
365 *
366 * Return value: An integer representing the result of the operation
367 **/
368GnomeVFSResult
369gnome_vfs_make_directory_for_uri (GnomeVFSURI *uri,
370                                  guint perm)
371{
372        return gnome_vfs_make_directory_for_uri_cancellable (uri, perm, NULL);
373}
374
375/**
376 * gnome_vfs_make_directory:
377 * @text_uri: URI of the directory to be created
378 * @perm: Unix-style permissions for the newly created directory
379 *
380 * Create @text_uri as a directory.
381 *
382 * Return value: An integer representing the result of the operation
383 **/
384GnomeVFSResult
385gnome_vfs_make_directory (const gchar *text_uri,
386                          guint perm)
387{
388        GnomeVFSResult result;
389        GnomeVFSURI *uri;
390
391        g_return_val_if_fail (text_uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
392
393        uri = gnome_vfs_uri_new (text_uri);
394        if (uri == NULL)
395                return GNOME_VFS_ERROR_INVALID_URI;
396
397        result = gnome_vfs_make_directory_for_uri (uri, perm);
398
399        gnome_vfs_uri_unref (uri);
400
401        return result;
402}
403
404/**
405 * gnome_vfs_remove_directory_from_uri:
406 * @uri: URI of the directory to be removed
407 *
408 * Remove @uri.  @uri must be an empty directory.
409 *
410 * Return value: An integer representing the result of the operation
411 **/
412GnomeVFSResult
413gnome_vfs_remove_directory_from_uri (GnomeVFSURI *uri)
414{
415        return gnome_vfs_remove_directory_from_uri_cancellable (uri, NULL);
416}
417
418/**
419 * gnome_vfs_remove_directory_from:
420 * @text_uri: URI of the directory to be removed
421 *
422 * Remove @text_uri.  @uri must be an empty directory.
423 *
424 * Return value: An integer representing the result of the operation
425 **/
426GnomeVFSResult
427gnome_vfs_remove_directory (const gchar *text_uri)
428{
429        GnomeVFSResult result;
430        GnomeVFSURI *uri;
431
432        g_return_val_if_fail (text_uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
433
434        uri = gnome_vfs_uri_new (text_uri);
435        if (uri == NULL)
436                return GNOME_VFS_ERROR_INVALID_URI;
437
438        result = gnome_vfs_remove_directory_from_uri (uri);
439
440        gnome_vfs_uri_unref (uri);
441
442        return result;
443}
444
445/**
446 * gnome_vfs_unlink_from_uri:
447 * @uri: URI of the file to be unlinked
448 *
449 * Unlink @uri.
450 *
451 * Return value: An integer representing the result of the operation
452 **/
453GnomeVFSResult
454gnome_vfs_unlink_from_uri (GnomeVFSURI *uri)
455{
456        return gnome_vfs_unlink_from_uri_cancellable (uri, NULL);
457}
458
459/**
460 * gnome_vfs_create_symbolic_link:
461 * @uri: URI to create a link at
462 * @target_reference: URI "reference" to point the link to (URI or relative path)
463 *
464 * Creates a symbolic link, or eventually, a URI link (as necessary)
465 * at @uri pointing to @target_reference
466 *
467 * Return value: An integer representing the result of the operation
468 **/
469GnomeVFSResult
470gnome_vfs_create_symbolic_link (GnomeVFSURI *uri, const gchar *target_reference)
471{
472        return gnome_vfs_create_symbolic_link_cancellable (uri, target_reference, NULL);
473}
474
475/**
476 * gnome_vfs_unlink_from_uri:
477 * @text_uri: URI of the file to be unlinked
478 *
479 * Unlink @text_uri.
480 *
481 * Return value: An integer representing the result of the operation
482 **/
483GnomeVFSResult
484gnome_vfs_unlink (const gchar *text_uri)
485{
486        GnomeVFSResult result;
487        GnomeVFSURI *uri;
488
489        g_return_val_if_fail (text_uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
490
491        uri = gnome_vfs_uri_new (text_uri);
492        if (uri == NULL)
493                return GNOME_VFS_ERROR_INVALID_URI;
494
495        result = gnome_vfs_unlink_from_uri (uri);
496
497        gnome_vfs_uri_unref (uri);
498
499        return result;
500}
501
502/**
503 * gnome_vfs_move_uri:
504 * @old_uri: Source URI
505 * @new_uri: Destination URI
506 *
507 * Move a file from URI @old_uri to @new_uri.  This will only work if @old_uri
508 * and @new_uri are on the same file system.  Otherwise, it is necessary
509 * to use the more general %gnome_vfs_xfer_uri() function.
510 *
511 * Return value: An integer representing the result of the operation.
512 **/
513GnomeVFSResult
514gnome_vfs_move_uri (GnomeVFSURI *old_uri,
515                    GnomeVFSURI *new_uri,
516                    gboolean force_replace)
517{
518        return gnome_vfs_move_uri_cancellable (old_uri, new_uri,
519                                               force_replace, NULL);
520}
521
522/**
523 * gnome_vfs_move:
524 * @old_text_uri: Source URI
525 * @new_text_uri: Destination URI
526 *
527 * Move a file from URI @old_text_uri to @new_text_uri.  This will only work
528 * if @old_text_uri and @new_text_uri are on the same file system.  Otherwise,
529 * it is necessary to use the more general %gnome_vfs_xfer_uri() function.
530 *
531 * Return value: An integer representing the result of the operation.
532 **/
533GnomeVFSResult
534gnome_vfs_move (const gchar *old_text_uri,
535                const gchar *new_text_uri,
536                gboolean force_replace)
537{
538        GnomeVFSURI *old_uri, *new_uri;
539        GnomeVFSResult retval;
540
541        g_return_val_if_fail (old_text_uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
542        g_return_val_if_fail (new_text_uri != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
543
544        old_uri = gnome_vfs_uri_new (old_text_uri);
545        if (old_uri == NULL)
546                return GNOME_VFS_ERROR_INVALID_URI;
547
548        new_uri = gnome_vfs_uri_new (new_text_uri);
549        if (new_uri == NULL) {
550                gnome_vfs_uri_unref (old_uri);
551                return GNOME_VFS_ERROR_INVALID_URI;
552        }
553
554        retval = gnome_vfs_move_uri (old_uri, new_uri, force_replace);
555
556        gnome_vfs_uri_unref (old_uri);
557        gnome_vfs_uri_unref (new_uri);
558
559        return retval;
560}
561
562/**
563 * gnome_vfs_check_same_fs_uris:
564 * @a: A URI
565 * @b: Another URI
566 * @same_fs_return: Pointer to a boolean variable which will be set to %TRUE
567 * if @a and @b are on the same file system on return.
568 *
569 * Check if @a and @b are on the same file system.
570 *
571 * Return value: An integer representing the result of the operation.
572 **/
573GnomeVFSResult
574gnome_vfs_check_same_fs_uris (GnomeVFSURI *a,
575                              GnomeVFSURI *b,
576                              gboolean *same_fs_return)
577{
578        return gnome_vfs_check_same_fs_uris_cancellable (a, b, same_fs_return,
579                                                         NULL);
580}
581
582/**
583 * gnome_vfs_check_same_fs:
584 * @a: A URI
585 * @b: Another URI
586 * @same_fs_return: Pointer to a boolean variable which will be set to %TRUE
587 * if @a and @b are on the same file system on return.
588 *
589 * Check if @a and @b are on the same file system.
590 *
591 * Return value: An integer representing the result of the operation.
592 **/
593GnomeVFSResult
594gnome_vfs_check_same_fs (const gchar *a,
595                         const gchar *b,
596                         gboolean *same_fs_return)
597{
598        GnomeVFSURI *a_uri, *b_uri;
599        GnomeVFSResult retval;
600
601        g_return_val_if_fail (a != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
602        g_return_val_if_fail (b != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
603        g_return_val_if_fail (same_fs_return != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS);
604
605        *same_fs_return = FALSE;
606
607        a_uri = gnome_vfs_uri_new (a);
608        if (a_uri == NULL)
609                return GNOME_VFS_ERROR_INVALID_URI;
610
611        b_uri = gnome_vfs_uri_new (b);
612        if (b_uri == NULL) {
613                gnome_vfs_uri_unref (a_uri);
614                return GNOME_VFS_ERROR_INVALID_URI;
615        }
616
617        retval = gnome_vfs_check_same_fs_uris (a_uri, b_uri, same_fs_return);
618
619        gnome_vfs_uri_unref (a_uri);
620        gnome_vfs_uri_unref (b_uri);
621
622        return retval;
623}
624
625/**
626 * gnome_vfs_set_file_info_uri:
627 * @uri: A URI
628 * @info: Information that must be set for the file
629 * @mask: Bit mask representing which fields of @info need to be set
630 *
631 * Set file information for @uri; only the information for which the
632 * corresponding bit in @mask is set is actually modified.
633 *
634 * Return value: An integer representing the result of the operation.
635 **/
636GnomeVFSResult
637gnome_vfs_set_file_info_uri (GnomeVFSURI *uri,
638                             GnomeVFSFileInfo *info,
639                             GnomeVFSSetFileInfoMask mask)
640{
641        return gnome_vfs_set_file_info_cancellable (uri, info, mask, NULL);
642}
643
644/**
645 * gnome_vfs_set_file_info:
646 * @text_uri: A URI
647 * @info: Information that must be set for the file
648 * @mask: Bit mask representing which fields of @info need to be set
649 *
650 * Set file information for @uri; only the information for which the
651 * corresponding bit in @mask is set is actually modified.
652 *
653 * Return value: An integer representing the result of the operation.
654 **/
655GnomeVFSResult
656gnome_vfs_set_file_info (const gchar *text_uri,
657                         GnomeVFSFileInfo *info,
658                         GnomeVFSSetFileInfoMask mask)
659{
660        GnomeVFSURI *uri;
661        GnomeVFSResult result;
662
663        uri = gnome_vfs_uri_new (text_uri);
664        if (uri == NULL)
665                return GNOME_VFS_ERROR_INVALID_URI;
666
667        result = gnome_vfs_set_file_info_uri (uri, info, mask);
668
669        gnome_vfs_uri_unref (uri);
670
671        return result;
672}
673
674/**
675 * gnome_vfs_uri_exists:
676 * @uri: A URI
677 *
678 * Check if the URI points to an existing entity.
679 *
680 * Return value: TRUE if URI exists.
681 **/
682gboolean
683gnome_vfs_uri_exists (GnomeVFSURI *uri)
684{
685        GnomeVFSFileInfo *info;
686        GnomeVFSResult result;
687
688        info = gnome_vfs_file_info_new ();
689        result = gnome_vfs_get_file_info_uri (uri, info, GNOME_VFS_FILE_INFO_DEFAULT);
690        gnome_vfs_file_info_unref (info);
691
692        return result == GNOME_VFS_OK;
693}
Note: See TracBrowser for help on using the repository browser.