forked from GNOME/rhythmbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrb-file-helpers.c
1679 lines (1477 loc) · 40.4 KB
/
rb-file-helpers.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
/*
* Copyright (C) 2002 Jorn Baayen
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* The Rhythmbox authors hereby grant permission for non-GPL compatible
* GStreamer plugins to be used and distributed together with GStreamer
* and Rhythmbox. This permission is above and beyond the permissions granted
* by the GPL license by which Rhythmbox is covered. If you modify this code
* you may extend this exception to your version of the code, but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/**
* SECTION:rb-file-helpers
* @short_description: An assortment of file and URI helper functions
*
* This is a variety of functions for dealing with files and URIs, including
* locating installed files, finding user cache and config directories,
* and dealing with file naming restrictions for various filesystems.
*/
#include "config.h"
#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <libpeas/peas.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "rb-file-helpers.h"
#include "rb-debug.h"
#include "rb-util.h"
typedef struct _RBUriHandleRecursivelyAsyncData RBUriHandleRecursivelyAsyncData;
static void _uri_handle_recursively_free (RBUriHandleRecursivelyAsyncData *data);
static void _uri_handle_recursively_next_dir (RBUriHandleRecursivelyAsyncData *data);
static void _uri_handle_recursively_next_files (RBUriHandleRecursivelyAsyncData *data);
static GHashTable *files = NULL;
static char *dot_dir = NULL;
static char *user_data_dir = NULL;
static char *user_cache_dir = NULL;
static char *uninstalled_paths[] = {
SHARE_UNINSTALLED_DIR "/",
SHARE_UNINSTALLED_DIR "/ui/",
SHARE_UNINSTALLED_BUILDDIR "/",
SHARE_UNINSTALLED_BUILDDIR "/ui/",
SHARE_DIR "/",
NULL
};
static char *installed_paths[] = {
SHARE_DIR "/",
NULL
};
static char **search_paths;
static const char *recurse_attributes =
G_FILE_ATTRIBUTE_STANDARD_NAME ","
G_FILE_ATTRIBUTE_STANDARD_TYPE ","
G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN ","
G_FILE_ATTRIBUTE_ID_FILE ","
G_FILE_ATTRIBUTE_ACCESS_CAN_READ ","
G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK;
/**
* rb_locale_dir:
*
* Returns the locale directory identified at build configuration time.
*
* Return value: locale dir
*/
const char *
rb_locale_dir (void)
{
return GNOMELOCALEDIR;
}
/**
* rb_file:
* @filename: name of file to search for
*
* Searches for an installed file, returning the full path name
* if found, NULL otherwise.
*
* Return value: Full file name, if found. Must not be freed.
*/
const char *
rb_file (const char *filename)
{
char *ret;
int i;
g_assert (files != NULL);
ret = g_hash_table_lookup (files, filename);
if (ret != NULL)
return ret;
for (i = 0; search_paths[i] != NULL; i++) {
ret = g_strconcat (search_paths[i], filename, NULL);
if (g_file_test (ret, G_FILE_TEST_EXISTS) == TRUE) {
g_hash_table_insert (files, g_strdup (filename), ret);
return (const char *) ret;
}
g_free (ret);
}
return NULL;
}
/**
* rb_user_data_dir:
*
* This will create the rhythmbox user data directory, using the XDG Base
* Directory specification. If none of the XDG environment variables are
* set, this will be ~/.local/share/rhythmbox.
*
* Returns: string holding the path to the rhythmbox user data directory, or
* NULL if the directory does not exist and cannot be created.
*/
const char *
rb_user_data_dir (void)
{
if (user_data_dir == NULL) {
user_data_dir = g_build_filename (g_get_user_data_dir (),
"rhythmbox",
NULL);
if (g_mkdir_with_parents (user_data_dir, 0700) == -1)
rb_debug ("unable to create Rhythmbox's user data dir, %s", user_data_dir);
}
return user_data_dir;
}
/**
* rb_user_cache_dir:
*
* This will create the rhythmbox user cache directory, using the XDG
* Base Directory specification. If none of the XDG environment
* variables are set, this will be ~/.cache/rhythmbox.
*
* Returns: string holding the path to the rhythmbox user cache directory, or
* NULL if the directory does not exist and could not be created.
*/
const char *
rb_user_cache_dir (void)
{
if (user_cache_dir == NULL) {
user_cache_dir = g_build_filename (g_get_user_cache_dir (),
"rhythmbox",
NULL);
if (g_mkdir_with_parents (user_cache_dir, 0700) == -1)
rb_debug ("unable to create Rhythmbox's user cache dir, %s", user_cache_dir);
}
return user_cache_dir;
}
/**
* rb_music_dir:
*
* Returns the default directory for the user's music library.
* This will usually be the 'Music' directory under the home directory.
*
* Return value: user's music directory. must not be freed.
*/
const char *
rb_music_dir (void)
{
const char *dir;
dir = g_get_user_special_dir (G_USER_DIRECTORY_MUSIC);
if (dir == NULL) {
dir = getenv ("HOME");
if (dir == NULL) {
dir = "/tmp";
}
}
rb_debug ("user music dir: %s", dir);
return dir;
}
/**
* rb_find_user_data_file:
* @name: name of file to find
*
* Determines the full path to use for user-specific files, such as rhythmdb.xml,
* within the user data directory (see @rb_user_data_dir).
*
* Returns: allocated string containing the location of the file to use, even if
* an error occurred.
*/
char *
rb_find_user_data_file (const char *name)
{
return g_build_filename (rb_user_data_dir (), name, NULL);
}
/**
* rb_find_user_cache_file:
* @name: name of file to find
*
* Determines the full path to use for user-specific cached files
* within the user cache directory.
*
* Returns: allocated string containing the location of the file to use, even if
* an error occurred.
*/
char *
rb_find_user_cache_file (const char *name)
{
return g_build_filename (rb_user_cache_dir (), name, NULL);
}
/**
* rb_find_plugin_data_file:
* @plugin: the plugin object
* @name: name of the file to find
*
* Locates a file under the plugin's data directory.
*
* Returns: allocated string containing the location of the file
*/
char *
rb_find_plugin_data_file (GObject *object, const char *name)
{
PeasPluginInfo *info;
char *ret = NULL;
const char *plugin_name = "<unknown>";
g_object_get (object, "plugin-info", &info, NULL);
if (info != NULL) {
char *tmp;
tmp = g_build_filename (peas_plugin_info_get_data_dir (info), name, NULL);
if (g_file_test (tmp, G_FILE_TEST_EXISTS)) {
ret = tmp;
} else {
g_free (tmp);
}
plugin_name = peas_plugin_info_get_name (info);
}
if (ret == NULL) {
const char *f;
f = rb_file (name);
if (f != NULL) {
ret = g_strdup (f);
}
}
if (ret == NULL) {
rb_debug ("didn't find file '%s' for plugin '%s'", name, plugin_name);
} else {
rb_debug ("found '%s' when searching for file '%s' for plugin '%s'",
ret, name, plugin_name);
}
/* ensure it's an absolute path */
if (ret != NULL && ret[0] != '/') {
char *pwd = g_get_current_dir ();
char *path = g_strconcat (pwd, G_DIR_SEPARATOR_S, ret, NULL);
g_free (ret);
g_free (pwd);
ret = path;
}
return ret;
}
/**
* rb_find_plugin_resource:
* @plugin: the plugin object
* @name: name of the file to find
*
* Constructs a resource path for a plugin data file.
*
* Returns: allocated string containing the resource path
*/
char *
rb_find_plugin_resource (GObject *object, const char *name)
{
PeasPluginInfo *info;
const char *plugin_name;
g_object_get (object, "plugin-info", &info, NULL);
if (info == NULL)
return NULL;
plugin_name = peas_plugin_info_get_module_name (info);
return g_strdup_printf ("/org/gnome/Rhythmbox/%s/%s", plugin_name, name);
}
/**
* rb_file_helpers_init:
* @uninstalled: if %TRUE, search in source and build directories
* as well as installed locations
*
* Sets up file search paths for @rb_file. Must be called on startup.
*/
void
rb_file_helpers_init (gboolean uninstalled)
{
if (uninstalled)
search_paths = uninstalled_paths;
else
search_paths = installed_paths;
files = g_hash_table_new_full (g_str_hash,
g_str_equal,
(GDestroyNotify) g_free,
(GDestroyNotify) g_free);
}
/**
* rb_file_helpers_shutdown:
*
* Frees various data allocated by file helper functions.
* Should be called on shutdown.
*/
void
rb_file_helpers_shutdown (void)
{
g_hash_table_destroy (files);
g_free (dot_dir);
g_free (user_data_dir);
g_free (user_cache_dir);
}
#define MAX_LINK_LEVEL 5
/* not sure this is really useful */
/**
* rb_uri_resolve_symlink:
* @uri: the URI to process
* @error: returns error information
*
* Attempts to resolve symlinks in @uri and return a canonical URI for the file
* it identifies.
*
* Return value: resolved URI, or NULL on error
*/
char *
rb_uri_resolve_symlink (const char *uri, GError **error)
{
GFile *file;
GFile *rfile;
char *result = NULL;
file = g_file_new_for_uri (uri);
rfile = rb_file_resolve_symlink (file, error);
g_object_unref (file);
if (rfile != NULL) {
result = g_file_get_uri (rfile);
g_object_unref (rfile);
}
return result;
}
/**
* rb_file_resolve_symlink:
* @file: the file to process
* @error: returns error information
*
* Attempts to resolve symlinks leading to @file and return a canonical location.
*
* Return value: (transfer full): a #GFile representing the canonical location, or NULL on error
*/
GFile *
rb_file_resolve_symlink (GFile *file, GError **error)
{
GFileInfo *file_info = NULL;
int link_count = 0;
GFile *result = NULL;
GFile *current;
char *furi;
char *ruri;
const char *attr = G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET;
GError *l_error = NULL;
current = g_object_ref (file);
while (link_count < MAX_LINK_LEVEL) {
GFile *parent;
GFile *new_file;
const char *target;
/* look for a symlink target */
file_info = g_file_query_info (current,
attr,
G_FILE_QUERY_INFO_NONE,
NULL, &l_error);
if (l_error != NULL) {
/* argh */
ruri = g_file_get_uri (current);
rb_debug ("error querying %s: %s", ruri, l_error->message);
g_free (ruri);
result = NULL;
break;
} else if (g_file_info_has_attribute (file_info, attr) == FALSE) {
/* no symlink, so return the path */
result = g_object_ref (current);
if (link_count > 0) {
furi = g_file_get_uri (file);
ruri = g_file_get_uri (result);
rb_debug ("resolved symlinks: %s -> %s", furi, ruri);
g_free (furi);
g_free (ruri);
}
break;
}
/* resolve it and try again */
new_file = NULL;
parent = g_file_get_parent (current);
if (parent == NULL) {
/* dang */
break;
}
target = g_file_info_get_attribute_byte_string (file_info, attr);
new_file = g_file_resolve_relative_path (parent, target);
g_object_unref (parent);
g_object_unref (file_info);
file_info = NULL;
g_object_unref (current);
current = new_file;
if (current == NULL) {
/* dang */
break;
}
link_count++;
}
g_clear_object (¤t);
g_clear_object (&file_info);
if (result == NULL && error == NULL) {
furi = g_file_get_uri (file);
rb_debug ("too many symlinks while resolving %s", furi);
g_free (furi);
l_error = g_error_new (G_IO_ERROR,
G_IO_ERROR_TOO_MANY_LINKS,
_("Too many symlinks"));
}
if (l_error != NULL) {
g_propagate_error (error, l_error);
}
return result;
}
/**
* rb_uri_is_directory:
* @uri: the URI to check
*
* Checks if @uri identifies a directory.
*
* Return value: %TRUE if @uri is a directory
*/
gboolean
rb_uri_is_directory (const char *uri)
{
GFile *f;
GFileInfo *fi;
GFileType ftype;
f = g_file_new_for_uri (uri);
fi = g_file_query_info (f, G_FILE_ATTRIBUTE_STANDARD_TYPE, 0, NULL, NULL);
g_object_unref (f);
if (fi == NULL) {
/* ? */
return FALSE;
}
ftype = g_file_info_get_attribute_uint32 (fi, G_FILE_ATTRIBUTE_STANDARD_TYPE);
g_object_unref (fi);
return (ftype == G_FILE_TYPE_DIRECTORY);
}
/**
* rb_uri_exists:
* @uri: a URI to check
*
* Checks if a URI identifies a resource that exists
*
* Return value: %TRUE if @uri exists
*/
gboolean
rb_uri_exists (const char *uri)
{
GFile *f;
gboolean exists;
f = g_file_new_for_uri (uri);
exists = g_file_query_exists (f, NULL);
g_object_unref (f);
return exists;
}
static gboolean
get_uri_perm (const char *uri, const char *perm_attribute)
{
GFile *f;
GFileInfo *info;
GError *error = NULL;
gboolean result;
f = g_file_new_for_uri (uri);
info = g_file_query_info (f, perm_attribute, 0, NULL, &error);
if (error != NULL) {
result = FALSE;
g_error_free (error);
} else {
result = g_file_info_get_attribute_boolean (info, perm_attribute);
}
if (info != NULL) {
g_object_unref (info);
}
g_object_unref (f);
return result;
}
/**
* rb_uri_is_readable:
* @uri: a URI to check
*
* Checks if the user can read the resource identified by @uri
*
* Return value: %TRUE if @uri is readable
*/
gboolean
rb_uri_is_readable (const char *uri)
{
return get_uri_perm (uri, G_FILE_ATTRIBUTE_ACCESS_CAN_READ);
}
/**
* rb_uri_is_writable:
* @uri: a URI to check
*
* Checks if the user can write to the resource identified by @uri
*
* Return value: %TRUE if @uri is writable
*/
gboolean
rb_uri_is_writable (const char *uri)
{
return get_uri_perm (uri, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
}
/**
* rb_uri_is_local:
* @uri: a URI to check
*
* Checks if @uri identifies a local resource. Currently this just
* checks that it uses the 'file' URI scheme.
*
* Return value: %TRUE if @uri is local
*/
gboolean
rb_uri_is_local (const char *uri)
{
return g_str_has_prefix (uri, "file://");
}
/**
* rb_uri_is_hidden:
* @uri: a URI to check
*
* Checks if @uri is hidden, according to the Unix filename convention.
* If the filename component of @uri begins with a dot, the file is considered
* hidden.
*
* Return value: %TRUE if @uri is hidden
*/
gboolean
rb_uri_is_hidden (const char *uri)
{
return g_utf8_strrchr (uri, -1, '/')[1] == '.';
}
/**
* rb_uri_could_be_podcast:
* @uri: a URI to check
* @is_opml: returns whether the URI identifies an OPML document
*
* Checks if @uri identifies a resource that is probably a podcast
* (RSS or Atom feed). This does not perform any IO, it just guesses
* based on the URI itself.
*
* Return value: %TRUE if @uri may be a podcast
*/
gboolean
rb_uri_could_be_podcast (const char *uri, gboolean *is_opml)
{
const char *query_string;
if (is_opml != NULL)
*is_opml = FALSE;
/* feed:// URIs are always podcasts */
if (g_str_has_prefix (uri, "feed:")) {
rb_debug ("'%s' must be a podcast", uri);
return TRUE;
}
/* Check the scheme is a possible one first */
if (g_str_has_prefix (uri, "http") == FALSE &&
g_str_has_prefix (uri, "itpc:") == FALSE &&
g_str_has_prefix (uri, "itms:") == FALSE &&
g_str_has_prefix (uri, "itmss:") == FALSE) {
rb_debug ("'%s' can't be a Podcast or OPML file, not the right scheme", uri);
return FALSE;
}
/* Now, check whether the iTunes Music Store link
* is a podcast */
if (g_str_has_prefix (uri, "itms:") != FALSE
&& strstr (uri, "phobos.apple.com") != NULL
&& strstr (uri, "viewPodcast") != NULL)
return TRUE;
/* Check for new itmss stype iTunes Music Store link
* to a podcast */
if (g_str_has_prefix (uri, "itmss:") != FALSE
&& strstr (uri, "podcast") != NULL)
return TRUE;
query_string = strchr (uri, '?');
if (query_string == NULL) {
query_string = uri + strlen (uri);
}
/* FIXME hacks */
if (strstr (uri, "rss") != NULL ||
strstr (uri, "atom") != NULL ||
strstr (uri, "feed") != NULL) {
rb_debug ("'%s' should be Podcast file, HACK", uri);
return TRUE;
} else if (strstr (uri, "opml") != NULL) {
rb_debug ("'%s' should be an OPML file, HACK", uri);
if (is_opml != NULL)
*is_opml = TRUE;
return TRUE;
}
if (strncmp (query_string - 4, ".rss", 4) == 0 ||
strncmp (query_string - 4, ".xml", 4) == 0 ||
strncmp (query_string - 5, ".atom", 5) == 0 ||
strncmp (uri, "itpc", 4) == 0 ||
(strstr (uri, "phobos.apple.com/") != NULL && strstr (uri, "viewPodcast") != NULL) ||
strstr (uri, "itunes.com/podcast") != NULL) {
rb_debug ("'%s' should be Podcast file", uri);
return TRUE;
} else if (strncmp (query_string - 5, ".opml", 5) == 0) {
rb_debug ("'%s' should be an OPML file", uri);
if (is_opml != NULL)
*is_opml = TRUE;
return TRUE;
}
return FALSE;
}
/**
* rb_uri_make_hidden:
* @uri: a URI to construct a hidden version of
*
* Constructs a URI that is similar to @uri but which identifies
* a hidden file. This can be used for temporary files that should not
* be visible to the user while they are in use.
*
* Return value: hidden URI, must be freed by the caller.
*/
char *
rb_uri_make_hidden (const char *uri)
{
GFile *file;
GFile *parent;
char *shortname;
char *dotted;
char *ret = NULL;
if (rb_uri_is_hidden (uri))
return g_strdup (uri);
file = g_file_new_for_uri (uri);
shortname = g_file_get_basename (file);
if (shortname == NULL) {
g_object_unref (file);
return NULL;
}
parent = g_file_get_parent (file);
if (parent == NULL) {
g_object_unref (file);
g_free (shortname);
return NULL;
}
g_object_unref (file);
dotted = g_strdup_printf (".%s", shortname);
g_free (shortname);
file = g_file_get_child (parent, dotted);
g_object_unref (parent);
g_free (dotted);
if (file != NULL) {
ret = g_file_get_uri (file);
g_object_unref (file);
}
return ret;
}
static gboolean
_should_process (GFileInfo *info)
{
/* check that the file is non-hidden and readable */
if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ)) {
if (g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ) == FALSE) {
return FALSE;
}
}
if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN)) {
if (g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN)) {
return FALSE;
}
}
return TRUE;
}
static gboolean
_uri_handle_file (GFile *dir, GFileInfo *fileinfo, GHashTable *handled, RBUriRecurseFunc func, gpointer user_data, GFile **descend)
{
const char *file_id;
gboolean is_dir;
gboolean ret;
GFileType file_type;
GFile *child;
*descend = NULL;
if (_should_process (fileinfo) == FALSE) {
rb_debug ("ignoring %s", g_file_info_get_name (fileinfo));
return TRUE;
}
/* already handled? */
file_id = g_file_info_get_attribute_string (fileinfo, G_FILE_ATTRIBUTE_ID_FILE);
if (file_id == NULL) {
/* have to hope for the best, I guess */
} else if (g_hash_table_lookup (handled, file_id) != NULL) {
return TRUE;
} else {
g_hash_table_insert (handled, g_strdup (file_id), GINT_TO_POINTER (1));
}
/* type? */
file_type = g_file_info_get_attribute_uint32 (fileinfo, G_FILE_ATTRIBUTE_STANDARD_TYPE);
switch (file_type) {
case G_FILE_TYPE_DIRECTORY:
case G_FILE_TYPE_MOUNTABLE:
is_dir = TRUE;
break;
default:
is_dir = FALSE;
break;
}
child = g_file_get_child (dir, g_file_info_get_name (fileinfo));
ret = (func) (child, fileinfo, user_data);
if (is_dir && ret) {
*descend = child;
} else {
g_object_unref (child);
}
return ret;
}
static void
_uri_handle_recurse (GFile *dir,
GCancellable *cancel,
GHashTable *handled,
RBUriRecurseFunc func,
gpointer user_data)
{
GFileEnumerator *files;
GFileInfo *info;
GError *error = NULL;
GFile *descend;
files = g_file_enumerate_children (dir, recurse_attributes, G_FILE_QUERY_INFO_NONE, cancel, &error);
if (error != NULL) {
char *where;
/* handle the case where we're given a single file to process */
if (error->code == G_IO_ERROR_NOT_DIRECTORY) {
g_clear_error (&error);
info = g_file_query_info (dir, recurse_attributes, G_FILE_QUERY_INFO_NONE, cancel, &error);
if (error == NULL) {
if (_should_process (info)) {
(func) (dir, info, user_data);
}
g_object_unref (info);
return;
}
}
where = g_file_get_uri (dir);
rb_debug ("error enumerating %s: %s", where, error->message);
g_free (where);
g_error_free (error);
return;
}
while (1) {
info = g_file_enumerator_next_file (files, cancel, &error);
if (error != NULL) {
rb_debug ("error enumerating files: %s", error->message);
break;
} else if (info == NULL) {
break;
}
if (_uri_handle_file (dir, info, handled, func, user_data, &descend) == FALSE)
break;
if (descend) {
_uri_handle_recurse (descend, cancel, handled, func, user_data);
g_object_unref (descend);
}
}
g_object_unref (files);
}
/**
* rb_uri_handle_recursively:
* @uri: URI to visit
* @cancel: an optional #GCancellable to allow cancellation
* @func: (scope call): Callback function
* @user_data: Data for callback function
*
* Calls @func for each file found under the directory identified by @uri.
* If @uri identifies a file, calls @func for that instead.
*/
void
rb_uri_handle_recursively (const char *uri,
GCancellable *cancel,
RBUriRecurseFunc func,
gpointer user_data)
{
GFile *file;
GHashTable *handled;
file = g_file_new_for_uri (uri);
handled = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
_uri_handle_recurse (file, cancel, handled, func, user_data);
g_hash_table_destroy (handled);
g_object_unref (file);
}
struct _RBUriHandleRecursivelyAsyncData {
GCancellable *cancel;
RBUriRecurseFunc func;
gpointer user_data;
GDestroyNotify data_destroy;
GHashTable *handled;
GQueue *dirs_left;
GFile *current;
GFileEnumerator *enumerator;
};
static void
_uri_handle_recursively_free (RBUriHandleRecursivelyAsyncData *data)
{
if (data->data_destroy)
data->data_destroy (data->user_data);
g_clear_object (&data->current);
g_clear_object (&data->enumerator);
g_clear_object (&data->cancel);
g_hash_table_destroy (data->handled);
g_queue_free_full (data->dirs_left, g_object_unref);
g_free (data);
}
static void
_uri_handle_recursively_process_files (GObject *src, GAsyncResult *result, gpointer ptr)
{
GList *files;
GList *l;
GFile *descend;
GError *error = NULL;
RBUriHandleRecursivelyAsyncData *data = ptr;
files = g_file_enumerator_next_files_finish (G_FILE_ENUMERATOR (src), result, &error);
if (error != NULL) {
rb_debug ("error enumerating files: %s", error->message);
_uri_handle_recursively_next_dir (data);
g_clear_error (&error);
return;
}
if (files == NULL) {
_uri_handle_recursively_next_dir (data);
return;
}
rb_debug ("got %d file(s)", g_list_length (files));
for (l = files; l != NULL; l = l->next) {
descend = NULL;
if (_uri_handle_file (data->current, l->data, data->handled, data->func, data->user_data, &descend) == FALSE) {
rb_debug ("callback returned false");
g_cancellable_cancel (data->cancel);
break;
} else if (descend) {
char *uri = g_file_get_uri (descend);
rb_debug ("adding dir %s to processing list", uri);
g_free (uri);
g_queue_push_tail (data->dirs_left, descend);
}
}
g_list_free_full (files, g_object_unref);
_uri_handle_recursively_next_files (data);
}
static void
_uri_handle_recursively_next_files (RBUriHandleRecursivelyAsyncData *data)
{
g_file_enumerator_next_files_async (data->enumerator,
16, /* or something */
G_PRIORITY_DEFAULT,
data->cancel,
_uri_handle_recursively_process_files,
data);
}
static void
_uri_handle_recursively_enum_files (GObject *src, GAsyncResult *result, gpointer ptr)
{
GError *error = NULL;
RBUriHandleRecursivelyAsyncData *data = ptr;
data->enumerator = g_file_enumerate_children_finish (G_FILE (src), result, &error);
if (error != NULL) {
if (error->code == G_IO_ERROR_NOT_DIRECTORY) {
GFileInfo *info;
g_clear_error (&error);
info = g_file_query_info (G_FILE (src), recurse_attributes, G_FILE_QUERY_INFO_NONE, data->cancel, &error);
if (error == NULL) {
if (_should_process (info)) {
(data->func) (G_FILE (src), info, data->user_data);
}
g_object_unref (info);
}
} else {
rb_debug ("error enumerating folder: %s", error->message);
}