This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
/
gfile.c
5883 lines (5149 loc) · 176 KB
/
gfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2006-2007 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Alexander Larsson <alexl@redhat.com>
*/
#include "config.h"
#include <string.h>
#include <sys/types.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#include "gfile.h"
#include "gvfs.h"
#include "gioscheduler.h"
#include "glocalfile.h"
#include "gsimpleasyncresult.h"
#include "gfileattribute-priv.h"
#include "gpollfilemonitor.h"
#include "gappinfo.h"
#include "gfileinputstream.h"
#include "gfileoutputstream.h"
#include "gcancellable.h"
#include "gasyncresult.h"
#include "gioerror.h"
#include "glibintl.h"
#include "gioalias.h"
/**
* SECTION:gfile
* @short_description: File and Directory Handling
* @include: gio/gio.h
* @see_also: #GFileInfo, #GFileEnumerator
*
* #GFile is a high level abstraction for manipulating files on a
* virtual file system. #GFile<!-- -->s are lightweight, immutable
* objects that do no I/O upon creation. It is necessary to understand that
* #GFile objects do not represent files, merely an identifier for a file. All
* file content I/O is implemented as streaming operations (see #GInputStream and
* #GOutputStream).
*
* To construct a #GFile, you can use:
* g_file_new_for_path() if you have a path.
* g_file_new_for_uri() if you have a URI.
* g_file_new_for_commandline_arg() for a command line argument.
* g_file_parse_name() from a utf8 string gotten from g_file_get_parse_name().
*
* One way to think of a #GFile is as an abstraction of a pathname. For normal
* files the system pathname is what is stored internally, but as #GFile<!-- -->s
* are extensible it could also be something else that corresponds to a pathname
* in a userspace implementation of a filesystem.
*
* #GFile<!-- -->s make up hierarchies of directories and files that correspond to the
* files on a filesystem. You can move through the file system with #GFile using
* g_file_get_parent() to get an identifier for the parent directory, g_file_get_child()
* to get a child within a directory, g_file_resolve_relative_path() to resolve a relative
* path between two #GFile<!-- -->s. There can be multiple hierarchies, so you may not
* end up at the same root if you repeatedly call g_file_get_parent() on two different
* files.
*
* All #GFile<!-- -->s have a basename (get with g_file_get_basename()). These names
* are byte strings that are used to identify the file on the filesystem (relative to
* its parent directory) and there is no guarantees that they have any particular charset
* encoding or even make any sense at all. If you want to use filenames in a user
* interface you should use the display name that you can get by requesting the
* %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
* This is guaranteed to be in utf8 and can be used in a user interface. But always
* store the real basename or the #GFile to use to actually access the file, because
* there is no way to go from a display name to the actual name.
*
* Using #GFile as an identifier has the same weaknesses as using a path in that
* there may be multiple aliases for the same file. For instance, hard or
* soft links may cause two different #GFile<!-- -->s to refer to the same file.
* Other possible causes for aliases are: case insensitive filesystems, short
* and long names on Fat/NTFS, or bind mounts in Linux. If you want to check if
* two #GFile<!-- -->s point to the same file you can query for the
* %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
* canonicalization of pathnames passed in, so that trivial differences in the
* path string used at creation (duplicated slashes, slash at end of path, "."
* or ".." path segments, etc) does not create different #GFile<!-- -->s.
*
* Many #GFile operations have both synchronous and asynchronous versions
* to suit your application. Asynchronous versions of synchronous functions
* simply have _async() appended to their function names. The asynchronous
* I/O functions call a #GAsyncReadyCallback which is then used to finalize
* the operation, producing a GAsyncResult which is then passed to the
* function's matching _finish() operation.
*
* Some #GFile operations do not have synchronous analogs, as they may
* take a very long time to finish, and blocking may leave an application
* unusable. Notable cases include:
* g_file_mount_mountable() to mount a mountable file.
* g_file_unmount_mountable() to unmount a mountable file.
* g_file_eject_mountable() to eject a mountable file.
*
* <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
* One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
* short. Entity tags are somewhat like a more abstract version of the
* traditional mtime, and can be used to quickly determine if the file has
* been modified from the version on the file system. See the HTTP 1.1
* <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
* for HTTP Etag headers, which are a very similar concept.
* </para>
**/
static void g_file_base_init (gpointer g_class);
static void g_file_class_init (gpointer g_class,
gpointer class_data);
static void g_file_real_query_info_async (GFile *file,
const char *attributes,
GFileQueryInfoFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileInfo * g_file_real_query_info_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_query_filesystem_info_async (GFile *file,
const char *attributes,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_enumerate_children_async (GFile *file,
const char *attributes,
GFileQueryInfoFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_read_async (GFile *file,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileInputStream * g_file_real_read_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_append_to_async (GFile *file,
GFileCreateFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_create_async (GFile *file,
GFileCreateFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileOutputStream *g_file_real_create_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_replace_async (GFile *file,
const char *etag,
gboolean make_backup,
GFileCreateFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileOutputStream *g_file_real_replace_finish (GFile *file,
GAsyncResult *res,
GError **error);
static gboolean g_file_real_set_attributes_from_info (GFile *file,
GFileInfo *info,
GFileQueryInfoFlags flags,
GCancellable *cancellable,
GError **error);
static void g_file_real_set_display_name_async (GFile *file,
const char *display_name,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFile * g_file_real_set_display_name_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_set_attributes_async (GFile *file,
GFileInfo *info,
GFileQueryInfoFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static gboolean g_file_real_set_attributes_finish (GFile *file,
GAsyncResult *res,
GFileInfo **info,
GError **error);
static void g_file_real_find_enclosing_mount_async (GFile *file,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_copy_async (GFile *source,
GFile *destination,
GFileCopyFlags flags,
int io_priority,
GCancellable *cancellable,
GFileProgressCallback progress_callback,
gpointer progress_callback_data,
GAsyncReadyCallback callback,
gpointer user_data);
static gboolean g_file_real_copy_finish (GFile *file,
GAsyncResult *res,
GError **error);
GType
g_file_get_type (void)
{
static volatile gsize g_define_type_id__volatile = 0;
if (g_once_init_enter (&g_define_type_id__volatile))
{
const GTypeInfo file_info =
{
sizeof (GFileIface), /* class_size */
g_file_base_init, /* base_init */
NULL, /* base_finalize */
g_file_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
0,
0, /* n_preallocs */
NULL
};
GType g_define_type_id =
g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
&file_info, 0);
g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
}
return g_define_type_id__volatile;
}
static void
g_file_class_init (gpointer g_class,
gpointer class_data)
{
GFileIface *iface = g_class;
iface->enumerate_children_async = g_file_real_enumerate_children_async;
iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
iface->set_display_name_async = g_file_real_set_display_name_async;
iface->set_display_name_finish = g_file_real_set_display_name_finish;
iface->query_info_async = g_file_real_query_info_async;
iface->query_info_finish = g_file_real_query_info_finish;
iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
iface->set_attributes_async = g_file_real_set_attributes_async;
iface->set_attributes_finish = g_file_real_set_attributes_finish;
iface->read_async = g_file_real_read_async;
iface->read_finish = g_file_real_read_finish;
iface->append_to_async = g_file_real_append_to_async;
iface->append_to_finish = g_file_real_append_to_finish;
iface->create_async = g_file_real_create_async;
iface->create_finish = g_file_real_create_finish;
iface->replace_async = g_file_real_replace_async;
iface->replace_finish = g_file_real_replace_finish;
iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
iface->copy_async = g_file_real_copy_async;
iface->copy_finish = g_file_real_copy_finish;
}
static void
g_file_base_init (gpointer g_class)
{
}
/**
* g_file_is_native:
* @file: input #GFile.
*
* Checks to see if a file is native to the platform.
*
* A native file s one expressed in the platform-native filename format,
* e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
* as it might be on a locally mounted remote filesystem.
*
* On some systems non-native files may be available using
* the native filesystem via a userspace filesystem (FUSE), in
* these cases this call will return %FALSE, but g_file_get_path()
* will still return a native path.
*
* This call does no blocking i/o.
*
* Returns: %TRUE if file is native.
**/
gboolean
g_file_is_native (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), FALSE);
iface = G_FILE_GET_IFACE (file);
return (* iface->is_native) (file);
}
/**
* g_file_has_uri_scheme:
* @file: input #GFile.
* @uri_scheme: a string containing a URI scheme.
*
* Checks to see if a #GFile has a given URI scheme.
*
* This call does no blocking i/o.
*
* Returns: %TRUE if #GFile's backend supports the
* given URI scheme, %FALSE if URI scheme is %NULL,
* not supported, or #GFile is invalid.
**/
gboolean
g_file_has_uri_scheme (GFile *file,
const char *uri_scheme)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), FALSE);
g_return_val_if_fail (uri_scheme != NULL, FALSE);
iface = G_FILE_GET_IFACE (file);
return (* iface->has_uri_scheme) (file, uri_scheme);
}
/**
* g_file_get_uri_scheme:
* @file: input #GFile.
*
* Gets the URI scheme for a #GFile.
* RFC 3986 decodes the scheme as:
* <programlisting>
* URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
* </programlisting>
* Common schemes include "file", "http", "ftp", etc.
*
* This call does no blocking i/o.
*
* Returns: a string containing the URI scheme for the given
* #GFile. The returned string should be freed with g_free()
* when no longer needed.
**/
char *
g_file_get_uri_scheme (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->get_uri_scheme) (file);
}
/**
* g_file_get_basename:
* @file: input #GFile.
*
* Gets the base name (the last component of the path) for a given #GFile.
*
* If called for the top level of a system (such as the filesystem root
* or a uri like sftp://host/) it will return a single directory separator
* (and on Windows, possibly a drive letter).
*
* The base name is a byte string (*not* UTF-8). It has no defined encoding
* or rules other than it may not contain zero bytes. If you want to use
* filenames in a user interface you should use the display name that you
* can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
* attribute with g_file_query_info().
*
* This call does no blocking i/o.
*
* Returns: string containing the #GFile's base name, or %NULL
* if given #GFile is invalid. The returned string should be
* freed with g_free() when no longer needed.
**/
char *
g_file_get_basename (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->get_basename) (file);
}
/**
* g_file_get_path:
* @file: input #GFile.
*
* Gets the local pathname for #GFile, if one exists.
*
* This call does no blocking i/o.
*
* Returns: string containing the #GFile's path, or %NULL if
* no such path exists. The returned string should be
* freed with g_free() when no longer needed.
**/
char *
g_file_get_path (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->get_path) (file);
}
/**
* g_file_get_uri:
* @file: input #GFile.
*
* Gets the URI for the @file.
*
* This call does no blocking i/o.
*
* Returns: a string containing the #GFile's URI.
* The returned string should be freed with g_free() when no longer needed.
**/
char *
g_file_get_uri (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->get_uri) (file);
}
/**
* g_file_get_parse_name:
* @file: input #GFile.
*
* Gets the parse name of the @file.
* A parse name is a UTF-8 string that describes the
* file such that one can get the #GFile back using
* g_file_parse_name().
*
* This is generally used to show the #GFile as a nice
* full-pathname kind of string in a user interface,
* like in a location entry.
*
* For local files with names that can safely be converted
* to UTF8 the pathname is used, otherwise the IRI is used
* (a form of URI that allows UTF8 characters unescaped).
*
* This call does no blocking i/o.
*
* Returns: a string containing the #GFile's parse name. The returned
* string should be freed with g_free() when no longer needed.
**/
char *
g_file_get_parse_name (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->get_parse_name) (file);
}
/**
* g_file_dup:
* @file: input #GFile.
*
* Duplicates a #GFile handle. This operation does not duplicate
* the actual file or directory represented by the #GFile; see
* g_file_copy() if attempting to copy a file.
*
* This call does no blocking i/o.
*
* Returns: a new #GFile that is a duplicate of the given #GFile.
**/
GFile *
g_file_dup (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->dup) (file);
}
/**
* g_file_hash:
* @file: #gconstpointer to a #GFile.
*
* Creates a hash value for a #GFile.
*
* This call does no blocking i/o.
*
* Returns: 0 if @file is not a valid #GFile, otherwise an
* integer that can be used as hash value for the #GFile.
* This function is intended for easily hashing a #GFile to
* add to a #GHashTable or similar data structure.
**/
guint
g_file_hash (gconstpointer file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), 0);
iface = G_FILE_GET_IFACE (file);
return (* iface->hash) ((GFile *)file);
}
/**
* g_file_equal:
* @file1: the first #GFile.
* @file2: the second #GFile.
*
* Checks equality of two given #GFile<!-- -->s. Note that two
* #GFile<!-- -->s that differ can still refer to the same
* file on the filesystem due to various forms of filename
* aliasing.
*
* This call does no blocking i/o.
*
* Returns: %TRUE if @file1 and @file2 are equal.
* %FALSE if either is not a #GFile.
**/
gboolean
g_file_equal (GFile *file1,
GFile *file2)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file1), FALSE);
g_return_val_if_fail (G_IS_FILE (file2), FALSE);
if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
return FALSE;
iface = G_FILE_GET_IFACE (file1);
return (* iface->equal) (file1, file2);
}
/**
* g_file_get_parent:
* @file: input #GFile.
*
* Gets the parent directory for the @file.
* If the @file represents the root directory of the
* file system, then %NULL will be returned.
*
* This call does no blocking i/o.
*
* Returns: a #GFile structure to the parent of the given
* #GFile or %NULL if there is no parent.
* Free the returned object with g_object_unref().
**/
GFile *
g_file_get_parent (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->get_parent) (file);
}
/**
* g_file_get_child:
* @file: input #GFile.
* @name: string containing the child's basename.
*
* Gets a child of @file with basename equal to @name.
*
* Note that the file with that specific name might not exist, but
* you can still have a #GFile that points to it. You can use this
* for instance to create that file.
*
* This call does no blocking i/o.
*
* Returns: a #GFile to a child specified by @name.
* Free the returned object with g_object_unref().
**/
GFile *
g_file_get_child (GFile *file,
const char *name)
{
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (name != NULL, NULL);
return g_file_resolve_relative_path (file, name);
}
/**
* g_file_get_child_for_display_name:
* @file: input #GFile.
* @display_name: string to a possible child.
* @error: #GError.
*
* Gets the child of @file for a given @display_name (i.e. a UTF8
* version of the name). If this function fails, it returns %NULL and @error will be
* set. This is very useful when constructing a GFile for a new file
* and the user entered the filename in the user interface, for instance
* when you select a directory and type a filename in the file selector.
*
* This call does no blocking i/o.
*
* Returns: a #GFile to the specified child, or
* %NULL if the display name couldn't be converted.
* Free the returned object with g_object_unref().
**/
GFile *
g_file_get_child_for_display_name (GFile *file,
const char *display_name,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (display_name != NULL, NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->get_child_for_display_name) (file, display_name, error);
}
/**
* g_file_has_prefix:
* @file: input #GFile.
* @prefix: input #GFile.
*
* Checks whether @file has the prefix specified by @prefix. In other word,
* if the names of inital elements of @file<!-- -->s pathname match @prefix.
*
* This call does no i/o, as it works purely on names. As such it can
* sometimes return %FALSE even if @file is inside a @prefix (from a
* filesystem point of view), because the prefix of @file is an alias
* of @prefix.
*
* Returns: %TRUE if the @files's parent, grandparent, etc is @prefix.
* %FALSE otherwise.
**/
gboolean
g_file_has_prefix (GFile *file,
GFile *prefix)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), FALSE);
g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
return FALSE;
iface = G_FILE_GET_IFACE (file);
/* The vtable function differs in arg order since we're
using the old contains_file call */
return (* iface->prefix_matches) (prefix, file);
}
/**
* g_file_get_relative_path:
* @parent: input #GFile.
* @descendant: input #GFile.
*
* Gets the path for @descendant relative to @parent.
*
* This call does no blocking i/o.
*
* Returns: string with the relative path from @descendant
* to @parent, or %NULL if @descendant doesn't have @parent as prefix.
* The returned string should be freed with g_free() when no longer needed.
**/
char *
g_file_get_relative_path (GFile *parent,
GFile *descendant)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (parent), NULL);
g_return_val_if_fail (G_IS_FILE (descendant), NULL);
if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
return NULL;
iface = G_FILE_GET_IFACE (parent);
return (* iface->get_relative_path) (parent, descendant);
}
/**
* g_file_resolve_relative_path:
* @file: input #GFile.
* @relative_path: a given relative path string.
*
* Resolves a relative path for @file to an absolute path.
*
* This call does no blocking i/o.
*
* Returns: #GFile to the resolved path. %NULL if @relative_path
* is %NULL or if @file is invalid.
* Free the returned object with g_object_unref().
**/
GFile *
g_file_resolve_relative_path (GFile *file,
const char *relative_path)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (relative_path != NULL, NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->resolve_relative_path) (file, relative_path);
}
/**
* g_file_enumerate_children:
* @file: input #GFile.
* @attributes: an attribute query string.
* @flags: a set of #GFileQueryInfoFlags.
* @cancellable: optional #GCancellable object, %NULL to ignore.
* @error: #GError for error reporting.
*
* Gets the requested information about the files in a directory. The result
* is a #GFileEnumerator object that will give out #GFileInfo objects for
* all the files in the directory.
*
* The @attribute value is a string that specifies the file attributes that
* should be gathered. It is not an error if it's not possible to read a particular
* requested attribute from a file - it just won't be set. @attribute should
* be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
* means all attributes, and a wildcard like "standard::*" means all attributes in the standard
* namespace. An example attribute query be "standard::*,owner::user".
* The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
*
* If @cancellable is not %NULL, then the operation can be cancelled by
* triggering the cancellable object from another thread. If the operation
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
*
* If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
* If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
* Other errors are possible too.
*
* Returns: A #GFileEnumerator if successful, %NULL on error.
* Free the returned object with g_object_unref().
**/
GFileEnumerator *
g_file_enumerate_children (GFile *file,
const char *attributes,
GFileQueryInfoFlags flags,
GCancellable *cancellable,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
if (iface->enumerate_children == NULL)
{
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
_("Operation not supported"));
return NULL;
}
return (* iface->enumerate_children) (file, attributes, flags,
cancellable, error);
}
/**
* g_file_enumerate_children_async:
* @file: input #GFile.
* @attributes: an attribute query string.
* @flags: a set of #GFileQueryInfoFlags.
* @io_priority: the <link linkend="io-priority">I/O priority</link>
* of the request.
* @cancellable: optional #GCancellable object, %NULL to ignore.
* @callback: a #GAsyncReadyCallback to call when the request is satisfied
* @user_data: the data to pass to callback function
*
* Asynchronously gets the requested information about the files in a directory. The result
* is a #GFileEnumerator object that will give out #GFileInfo objects for
* all the files in the directory.
*
* For more details, see g_file_enumerate_children() which is
* the synchronous version of this call.
*
* When the operation is finished, @callback will be called. You can then call
* g_file_enumerate_children_finish() to get the result of the operation.
**/
void
g_file_enumerate_children_async (GFile *file,
const char *attributes,
GFileQueryInfoFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GFileIface *iface;
g_return_if_fail (G_IS_FILE (file));
iface = G_FILE_GET_IFACE (file);
(* iface->enumerate_children_async) (file,
attributes,
flags,
io_priority,
cancellable,
callback,
user_data);
}
/**
* g_file_enumerate_children_finish:
* @file: input #GFile.
* @res: a #GAsyncResult.
* @error: a #GError.
*
* Finishes an async enumerate children operation.
* See g_file_enumerate_children_async().
*
* Returns: a #GFileEnumerator or %NULL if an error occurred.
* Free the returned object with g_object_unref().
**/
GFileEnumerator *
g_file_enumerate_children_finish (GFile *file,
GAsyncResult *res,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
if (G_IS_SIMPLE_ASYNC_RESULT (res))
{
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
if (g_simple_async_result_propagate_error (simple, error))
return NULL;
}
iface = G_FILE_GET_IFACE (file);
return (* iface->enumerate_children_finish) (file, res, error);
}
/**
* g_file_query_exists:
* @file: input #GFile.
* @cancellable: optional #GCancellable object, %NULL to ignore.
*
* Utility function to check if a particular file exists. This is
* implemented using g_file_query_info() and as such does blocking I/O.
*
* Note that in many cases it is racy to first check for file existence
* and then execute something based on the outcome of that, because the
* file might have been created or removed in between the operations. The
* general approach to handling that is to not check, but just do the
* operation and handle the errors as they come.
*
* As an example of race-free checking, take the case of reading a file, and
* if it doesn't exist, creating it. There are two racy versions: read it, and
* on error create it; and: check if it exists, if not create it. These
* can both result in two processes creating the file (with perhaps a partially
* written file as the result). The correct approach is to always try to create
* the file with g_file_create() which will either atomically create the file
* or fail with a G_IO_ERROR_EXISTS error.
*
* However, in many cases an existence check is useful in a user
* interface, for instance to make a menu item sensitive/insensitive, so that
* you don't have to fool users that something is possible and then just show
* and error dialog. If you do this, you should make sure to also handle the
* errors that can happen due to races when you execute the operation.
*
* Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
*/
gboolean
g_file_query_exists (GFile *file,
GCancellable *cancellable)
{
GFileInfo *info;
g_return_val_if_fail (G_IS_FILE(file), FALSE);
info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
G_FILE_QUERY_INFO_NONE, cancellable, NULL);
if (info != NULL)
{
g_object_unref (info);
return TRUE;
}
return FALSE;
}
/**
* g_file_query_file_type:
* @file: input #GFile.
* @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info().
* @cancellable: optional #GCancellable object, %NULL to ignore.
*
* Utility function to inspect the #GFileType of a file. This is
* implemented using g_file_query_info() and as such does blocking I/O.
*
* The primary use case of this method is to check if a file is a regular file,
* directory, or symlink.
*
* Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN if the file
* does not exist
*
* Since: 2.18
*/
GFileType
g_file_query_file_type (GFile *file,
GFileQueryInfoFlags flags,
GCancellable *cancellable)
{
GFileInfo *info;
GFileType file_type;
g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
cancellable, NULL);
if (info != NULL)
{
file_type = g_file_info_get_file_type (info);
g_object_unref (info);
}
else
file_type = G_FILE_TYPE_UNKNOWN;
return file_type;
}
/**
* g_file_query_info:
* @file: input #GFile.