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

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