1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include <glocalfileenumerator.h>
27 #include <glocalfileinfo.h>
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?)
37 struct _GLocalFileEnumerator
39 GFileEnumerator parent;
41 GFileAttributeMatcher *matcher;
45 GFileQueryInfoFlags flags;
47 gboolean got_parent_info;
48 GLocalParentFileInfo parent_info;
50 gboolean follow_symlinks;
53 G_DEFINE_TYPE (GLocalFileEnumerator, g_local_file_enumerator, G_TYPE_FILE_ENUMERATOR);
55 static GFileInfo *g_local_file_enumerator_next_file (GFileEnumerator *enumerator,
56 GCancellable *cancellable,
58 static gboolean g_local_file_enumerator_close (GFileEnumerator *enumerator,
59 GCancellable *cancellable,
64 g_local_file_enumerator_finalize (GObject *object)
66 GLocalFileEnumerator *local;
68 local = G_LOCAL_FILE_ENUMERATOR (object);
70 g_free (local->filename);
71 g_file_attribute_matcher_unref (local->matcher);
74 g_dir_close (local->dir);
78 if (G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize)
79 (*G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize) (object);
84 g_local_file_enumerator_class_init (GLocalFileEnumeratorClass *klass)
86 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
87 GFileEnumeratorClass *enumerator_class = G_FILE_ENUMERATOR_CLASS (klass);
89 gobject_class->finalize = g_local_file_enumerator_finalize;
91 enumerator_class->next_file = g_local_file_enumerator_next_file;
92 enumerator_class->close = g_local_file_enumerator_close;
96 g_local_file_enumerator_init (GLocalFileEnumerator *local)
101 convert_file_to_io_error (GError **error,
106 if (file_error == NULL)
109 new_code = G_IO_ERROR_FAILED;
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;
116 case G_FILE_ERROR_ACCES:
117 new_code = G_IO_ERROR_PERMISSION_DENIED;
119 case G_FILE_ERROR_NOTDIR:
120 new_code = G_IO_ERROR_NOT_DIRECTORY;
127 g_set_error (error, G_IO_ERROR,
129 "%s", file_error->message);
133 g_local_file_enumerator_new (const char *filename,
134 const char *attributes,
135 GFileQueryInfoFlags flags,
136 GCancellable *cancellable,
139 GLocalFileEnumerator *local;
145 dir = g_dir_open (filename, 0, error != NULL ? &dir_error : NULL);
147 convert_file_to_io_error (error, dir_error);
148 g_error_free (dir_error);
152 local = g_object_new (G_TYPE_LOCAL_FILE_ENUMERATOR, NULL);
155 local->filename = g_strdup (filename);
156 local->matcher = g_file_attribute_matcher_new (attributes);
157 local->flags = flags;
159 return G_FILE_ENUMERATOR (local);
163 g_local_file_enumerator_next_file (GFileEnumerator *enumerator,
164 GCancellable *cancellable,
167 GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
168 const char *filename;
171 GError *my_error = NULL;
173 if (!local->got_parent_info)
175 _g_local_file_info_get_parent_info (local->filename, local->matcher, &local->parent_info);
176 local->got_parent_info = TRUE;
181 filename = g_dir_read_name (local->dir);
182 if (filename == NULL)
185 path = g_build_filename (local->filename, filename, NULL);
186 info = _g_local_file_info_get (filename, path,
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)
202 g_error_free (my_error);
206 g_propagate_error (error, my_error);
213 g_local_file_enumerator_close (GFileEnumerator *enumerator,
214 GCancellable *cancellable,
217 GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
221 g_dir_close (local->dir);