Added. Added. Added. Added.
[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 #include "gioalias.h"
31
32   /* TODO:
33    *  It would be nice to use the dirent->d_type to check file type without
34    *  needing to stat each files on linux and other systems that support it.
35    *  (question: does that following symlink or not?)
36    */
37   
38
39 struct _GLocalFileEnumerator
40 {
41   GFileEnumerator parent;
42
43   GFileAttributeMatcher *matcher;
44   GDir *dir;
45   char *filename;
46   char *attributes;
47   GFileQueryInfoFlags flags;
48
49   gboolean got_parent_info;
50   GLocalParentFileInfo parent_info;
51   
52   gboolean follow_symlinks;
53 };
54
55 G_DEFINE_TYPE (GLocalFileEnumerator, g_local_file_enumerator, G_TYPE_FILE_ENUMERATOR);
56
57 static GFileInfo *g_local_file_enumerator_next_file (GFileEnumerator  *enumerator,
58                                                      GCancellable     *cancellable,
59                                                      GError          **error);
60 static gboolean   g_local_file_enumerator_close     (GFileEnumerator  *enumerator,
61                                                      GCancellable     *cancellable,
62                                                      GError          **error);
63
64
65 static void
66 g_local_file_enumerator_finalize (GObject *object)
67 {
68   GLocalFileEnumerator *local;
69
70   local = G_LOCAL_FILE_ENUMERATOR (object);
71
72   g_free (local->filename);
73   g_file_attribute_matcher_unref (local->matcher);
74   if (local->dir)
75     {
76       g_dir_close (local->dir);
77       local->dir = NULL;
78     }
79   
80   if (G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize)
81     (*G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize) (object);
82 }
83
84
85 static void
86 g_local_file_enumerator_class_init (GLocalFileEnumeratorClass *klass)
87 {
88   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
89   GFileEnumeratorClass *enumerator_class = G_FILE_ENUMERATOR_CLASS (klass);
90   
91   gobject_class->finalize = g_local_file_enumerator_finalize;
92
93   enumerator_class->next_file = g_local_file_enumerator_next_file;
94   enumerator_class->close = g_local_file_enumerator_close;
95 }
96
97 static void
98 g_local_file_enumerator_init (GLocalFileEnumerator *local)
99 {
100 }
101
102 static void
103 convert_file_to_io_error (GError **error,
104                           GError *file_error)
105 {
106   int new_code;
107
108   if (file_error == NULL)
109     return;
110   
111   new_code = G_IO_ERROR_FAILED;
112   
113   if (file_error->domain == G_FILE_ERROR) {
114     switch (file_error->code) {
115     case G_FILE_ERROR_NOENT:
116       new_code = G_IO_ERROR_NOT_FOUND;
117       break;
118     case G_FILE_ERROR_ACCES:
119       new_code = G_IO_ERROR_PERMISSION_DENIED;
120       break;
121     case G_FILE_ERROR_NOTDIR:
122       new_code = G_IO_ERROR_NOT_DIRECTORY;
123       break;
124     default:
125       break;
126     }
127   }
128   
129   g_set_error (error, G_IO_ERROR,
130                new_code,
131                "%s", file_error->message);
132 }
133
134 GFileEnumerator *
135 g_local_file_enumerator_new (const char *filename,
136                              const char *attributes,
137                              GFileQueryInfoFlags flags,
138                              GCancellable *cancellable,
139                              GError **error)
140 {
141   GLocalFileEnumerator *local;
142   GDir *dir;
143   GError *dir_error;
144   int new_code;
145
146   dir_error = NULL;
147   dir = g_dir_open (filename, 0, error != NULL ? &dir_error : NULL);
148   if (dir == NULL) {
149     convert_file_to_io_error (error, dir_error);
150     g_error_free (dir_error);
151     return NULL;
152   }
153   
154   local = g_object_new (G_TYPE_LOCAL_FILE_ENUMERATOR, NULL);
155
156   local->dir = dir;
157   local->filename = g_strdup (filename);
158   local->matcher = g_file_attribute_matcher_new (attributes);
159   local->flags = flags;
160   
161   return G_FILE_ENUMERATOR (local);
162 }
163
164 static GFileInfo *
165 g_local_file_enumerator_next_file (GFileEnumerator *enumerator,
166                                    GCancellable     *cancellable,
167                                    GError **error)
168 {
169   GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
170   const char *filename;
171   char *path;
172   GFileInfo *info;
173   GError *my_error = NULL;
174
175   if (!local->got_parent_info)
176     {
177       _g_local_file_info_get_parent_info (local->filename, local->matcher, &local->parent_info);
178       local->got_parent_info = TRUE;
179     }
180   
181  next_file:
182   
183   filename = g_dir_read_name (local->dir);
184   if (filename == NULL)
185     return NULL;
186
187   path = g_build_filename (local->filename, filename, NULL);
188   info = _g_local_file_info_get (filename, path,
189                                  local->matcher,
190                                  local->flags,
191                                  &local->parent_info,
192                                  &my_error); 
193   g_free (path);
194   
195   if (info == NULL)
196     {
197       /* Failed to get info */
198       /* If the file does not exist there might have been a race where
199        * the file was removed between the readdir and the stat, so we
200        * ignore the file. */
201       if (my_error->domain == G_IO_ERROR &&
202           my_error->code == G_IO_ERROR_NOT_FOUND)
203         {
204           g_error_free (my_error);
205           goto next_file;
206         }
207       else
208         g_propagate_error (error, my_error);
209     }
210
211   return info;
212 }
213
214 static gboolean
215 g_local_file_enumerator_close (GFileEnumerator *enumerator,
216                                GCancellable     *cancellable,
217                                GError          **error)
218 {
219   GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
220
221   if (local->dir)
222     {
223       g_dir_close (local->dir);
224       local->dir = NULL;
225     }
226
227   return TRUE;
228 }
229
230