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

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