gio/ docs/reference/gio Merged gio-standalone into glib.
[platform/upstream/glib.git] / gio / glocalfileenumerator.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include <glib.h>
26 #include <glocalfileenumerator.h>
27 #include <glocalfileinfo.h>
28 #include "glibintl.h"
29
30   /* TODO:
31    *  It would be nice to use the dirent->d_type to check file type without
32    *  needing to stat each files on linux and other systems that support it.
33    *  (question: does that following symlink or not?)
34    */
35   
36
37 struct _GLocalFileEnumerator
38 {
39   GFileEnumerator parent;
40
41   GFileAttributeMatcher *matcher;
42   GDir *dir;
43   char *filename;
44   char *attributes;
45   GFileQueryInfoFlags flags;
46
47   gboolean got_parent_info;
48   GLocalParentFileInfo parent_info;
49   
50   gboolean follow_symlinks;
51 };
52
53 G_DEFINE_TYPE (GLocalFileEnumerator, g_local_file_enumerator, G_TYPE_FILE_ENUMERATOR);
54
55 static GFileInfo *g_local_file_enumerator_next_file (GFileEnumerator  *enumerator,
56                                                      GCancellable     *cancellable,
57                                                      GError          **error);
58 static gboolean   g_local_file_enumerator_close     (GFileEnumerator  *enumerator,
59                                                      GCancellable     *cancellable,
60                                                      GError          **error);
61
62
63 static void
64 g_local_file_enumerator_finalize (GObject *object)
65 {
66   GLocalFileEnumerator *local;
67
68   local = G_LOCAL_FILE_ENUMERATOR (object);
69
70   g_free (local->filename);
71   g_file_attribute_matcher_unref (local->matcher);
72   if (local->dir)
73     {
74       g_dir_close (local->dir);
75       local->dir = NULL;
76     }
77   
78   if (G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize)
79     (*G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize) (object);
80 }
81
82
83 static void
84 g_local_file_enumerator_class_init (GLocalFileEnumeratorClass *klass)
85 {
86   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
87   GFileEnumeratorClass *enumerator_class = G_FILE_ENUMERATOR_CLASS (klass);
88   
89   gobject_class->finalize = g_local_file_enumerator_finalize;
90
91   enumerator_class->next_file = g_local_file_enumerator_next_file;
92   enumerator_class->close = g_local_file_enumerator_close;
93 }
94
95 static void
96 g_local_file_enumerator_init (GLocalFileEnumerator *local)
97 {
98 }
99
100 static void
101 convert_file_to_io_error (GError **error,
102                           GError *file_error)
103 {
104   int new_code;
105
106   if (file_error == NULL)
107     return;
108   
109   new_code = G_IO_ERROR_FAILED;
110   
111   if (file_error->domain == G_FILE_ERROR) {
112     switch (file_error->code) {
113     case G_FILE_ERROR_NOENT:
114       new_code = G_IO_ERROR_NOT_FOUND;
115       break;
116     case G_FILE_ERROR_ACCES:
117       new_code = G_IO_ERROR_PERMISSION_DENIED;
118       break;
119     case G_FILE_ERROR_NOTDIR:
120       new_code = G_IO_ERROR_NOT_DIRECTORY;
121       break;
122     default:
123       break;
124     }
125   }
126   
127   g_set_error (error, G_IO_ERROR,
128                new_code,
129                "%s", file_error->message);
130 }
131
132 GFileEnumerator *
133 g_local_file_enumerator_new (const char *filename,
134                              const char *attributes,
135                              GFileQueryInfoFlags flags,
136                              GCancellable *cancellable,
137                              GError **error)
138 {
139   GLocalFileEnumerator *local;
140   GDir *dir;
141   GError *dir_error;
142   int new_code;
143
144   dir_error = NULL;
145   dir = g_dir_open (filename, 0, error != NULL ? &dir_error : NULL);
146   if (dir == NULL) {
147     convert_file_to_io_error (error, dir_error);
148     g_error_free (dir_error);
149     return NULL;
150   }
151   
152   local = g_object_new (G_TYPE_LOCAL_FILE_ENUMERATOR, NULL);
153
154   local->dir = dir;
155   local->filename = g_strdup (filename);
156   local->matcher = g_file_attribute_matcher_new (attributes);
157   local->flags = flags;
158   
159   return G_FILE_ENUMERATOR (local);
160 }
161
162 static GFileInfo *
163 g_local_file_enumerator_next_file (GFileEnumerator *enumerator,
164                                    GCancellable     *cancellable,
165                                    GError **error)
166 {
167   GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
168   const char *filename;
169   char *path;
170   GFileInfo *info;
171   GError *my_error = NULL;
172
173   if (!local->got_parent_info)
174     {
175       _g_local_file_info_get_parent_info (local->filename, local->matcher, &local->parent_info);
176       local->got_parent_info = TRUE;
177     }
178   
179  next_file:
180   
181   filename = g_dir_read_name (local->dir);
182   if (filename == NULL)
183     return NULL;
184
185   path = g_build_filename (local->filename, filename, NULL);
186   info = _g_local_file_info_get (filename, path,
187                                  local->matcher,
188                                  local->flags,
189                                  &local->parent_info,
190                                  &my_error); 
191   g_free (path);
192   
193   if (info == NULL)
194     {
195       /* Failed to get info */
196       /* If the file does not exist there might have been a race where
197        * the file was removed between the readdir and the stat, so we
198        * ignore the file. */
199       if (my_error->domain == G_IO_ERROR &&
200           my_error->code == G_IO_ERROR_NOT_FOUND)
201         {
202           g_error_free (my_error);
203           goto next_file;
204         }
205       else
206         g_propagate_error (error, my_error);
207     }
208
209   return info;
210 }
211
212 static gboolean
213 g_local_file_enumerator_close (GFileEnumerator *enumerator,
214                                GCancellable     *cancellable,
215                                GError          **error)
216 {
217   GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
218
219   if (local->dir)
220     {
221       g_dir_close (local->dir);
222       local->dir = NULL;
223     }
224
225   return TRUE;
226 }
227
228