1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * gdir.c: Simplified wrapper around the DIRENT functions.
5  *
6  * Copyright 2001 Hans Breuer
7  * Copyright 2004 Tor Lillqvist
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "config.h"
24 
25 #include <errno.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <sys/stat.h>
29 
30 #ifdef HAVE_DIRENT_H
31 #include <sys/types.h>
32 #include <dirent.h>
33 #endif
34 
35 #include "gdir.h"
36 
37 #include "gconvert.h"
38 #include "gfileutils.h"
39 #include "gstrfuncs.h"
40 #include "gtestutils.h"
41 #include "glibintl.h"
42 
43 #if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
44 #include "dirent/dirent.h"
45 #endif
46 
47 #include "glib-private.h" /* g_dir_open_with_errno, g_dir_new_from_dirp */
48 
49 /**
50  * GDir:
51  *
52  * An opaque structure representing an opened directory.
53  */
54 
55 struct _GDir
56 {
57 #ifdef G_OS_WIN32
58   _WDIR *wdirp;
59 #else
60   DIR *dirp;
61 #endif
62 #ifdef G_OS_WIN32
63   /* maximum encoding of FILENAME_MAX UTF-8 characters, plus a nul terminator
64    * (FILENAME_MAX is not guaranteed to include one) */
65   gchar utf8_buf[FILENAME_MAX*4 + 1];
66 #endif
67 };
68 
69 /*< private >
70  * g_dir_open_with_errno:
71  * @path: the path to the directory you are interested in.
72  * @flags: Currently must be set to 0. Reserved for future use.
73  *
74  * Opens a directory for reading.
75  *
76  * This function is equivalent to g_dir_open() except in the error case,
77  * errno will be set accordingly.
78  *
79  * This is useful if you want to construct your own error message.
80  *
81  * Returns: a newly allocated #GDir on success, or %NULL on failure,
82  *   with errno set accordingly.
83  *
84  * Since: 2.38
85  */
86 GDir *
g_dir_open_with_errno(const gchar * path,guint flags)87 g_dir_open_with_errno (const gchar *path,
88                        guint        flags)
89 {
90   GDir dir;
91 #ifdef G_OS_WIN32
92   gint saved_errno;
93   wchar_t *wpath;
94 #endif
95 
96   g_return_val_if_fail (path != NULL, NULL);
97 
98 #ifdef G_OS_WIN32
99   wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
100 
101   g_return_val_if_fail (wpath != NULL, NULL);
102 
103   dir.wdirp = _wopendir (wpath);
104   saved_errno = errno;
105   g_free (wpath);
106   errno = saved_errno;
107 
108   if (dir.wdirp == NULL)
109     return NULL;
110 #else
111   dir.dirp = opendir (path);
112 
113   if (dir.dirp == NULL)
114     return NULL;
115 #endif
116 
117   return g_memdup2 (&dir, sizeof dir);
118 }
119 
120 /**
121  * g_dir_open:
122  * @path: the path to the directory you are interested in. On Unix
123  *         in the on-disk encoding. On Windows in UTF-8
124  * @flags: Currently must be set to 0. Reserved for future use.
125  * @error: return location for a #GError, or %NULL.
126  *         If non-%NULL, an error will be set if and only if
127  *         g_dir_open() fails.
128  *
129  * Opens a directory for reading. The names of the files in the
130  * directory can then be retrieved using g_dir_read_name().  Note
131  * that the ordering is not defined.
132  *
133  * Returns: a newly allocated #GDir on success, %NULL on failure.
134  *   If non-%NULL, you must free the result with g_dir_close()
135  *   when you are finished with it.
136  **/
137 GDir *
g_dir_open(const gchar * path,guint flags,GError ** error)138 g_dir_open (const gchar  *path,
139             guint         flags,
140             GError      **error)
141 {
142   gint saved_errno;
143   GDir *dir;
144 
145   dir = g_dir_open_with_errno (path, flags);
146 
147   if (dir == NULL)
148     {
149       gchar *utf8_path;
150 
151       saved_errno = errno;
152 
153       utf8_path = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);
154 
155       g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (saved_errno),
156                    _("Error opening directory “%s”: %s"), utf8_path, g_strerror (saved_errno));
157       g_free (utf8_path);
158     }
159 
160   return dir;
161 }
162 
163 /*< private >
164  * g_dir_new_from_dirp:
165  * @dirp: a #DIR* created by opendir() or fdopendir()
166  *
167  * Creates a #GDir object from the DIR object that is created using
168  * opendir() or fdopendir().  The created #GDir assumes ownership of the
169  * passed-in #DIR pointer.
170  *
171  * @dirp must not be %NULL.
172  *
173  * This function never fails.
174  *
175  * Returns: a newly allocated #GDir, which should be closed using
176  *     g_dir_close().
177  *
178  * Since: 2.38
179  **/
180 GDir *
g_dir_new_from_dirp(gpointer dirp)181 g_dir_new_from_dirp (gpointer dirp)
182 {
183 #ifdef G_OS_UNIX
184   GDir *dir;
185 
186   g_return_val_if_fail (dirp != NULL, NULL);
187 
188   dir = g_new (GDir, 1);
189   dir->dirp = dirp;
190 
191   return dir;
192 #else
193   g_assert_not_reached ();
194 
195   return NULL;
196 #endif
197 }
198 
199 /**
200  * g_dir_read_name:
201  * @dir: a #GDir* created by g_dir_open()
202  *
203  * Retrieves the name of another entry in the directory, or %NULL.
204  * The order of entries returned from this function is not defined,
205  * and may vary by file system or other operating-system dependent
206  * factors.
207  *
208  * %NULL may also be returned in case of errors. On Unix, you can
209  * check `errno` to find out if %NULL was returned because of an error.
210  *
211  * On Unix, the '.' and '..' entries are omitted, and the returned
212  * name is in the on-disk encoding.
213  *
214  * On Windows, as is true of all GLib functions which operate on
215  * filenames, the returned name is in UTF-8.
216  *
217  * Returns: (type filename): The entry's name or %NULL if there are no
218  *   more entries. The return value is owned by GLib and
219  *   must not be modified or freed.
220  **/
221 const gchar *
g_dir_read_name(GDir * dir)222 g_dir_read_name (GDir *dir)
223 {
224 #ifdef G_OS_WIN32
225   gchar *utf8_name;
226   struct _wdirent *wentry;
227 #else
228   struct dirent *entry;
229 #endif
230 
231   g_return_val_if_fail (dir != NULL, NULL);
232 
233 #ifdef G_OS_WIN32
234   while (1)
235     {
236       wentry = _wreaddir (dir->wdirp);
237       while (wentry
238 	     && (0 == wcscmp (wentry->d_name, L".") ||
239 		 0 == wcscmp (wentry->d_name, L"..")))
240 	wentry = _wreaddir (dir->wdirp);
241 
242       if (wentry == NULL)
243 	return NULL;
244 
245       utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
246 
247       if (utf8_name == NULL)
248 	continue;		/* Huh, impossible? Skip it anyway */
249 
250       strcpy (dir->utf8_buf, utf8_name);
251       g_free (utf8_name);
252 
253       return dir->utf8_buf;
254     }
255 #else
256   entry = readdir (dir->dirp);
257   while (entry
258          && (0 == strcmp (entry->d_name, ".") ||
259              0 == strcmp (entry->d_name, "..")))
260     entry = readdir (dir->dirp);
261 
262   if (entry)
263     return entry->d_name;
264   else
265     return NULL;
266 #endif
267 }
268 
269 /**
270  * g_dir_rewind:
271  * @dir: a #GDir* created by g_dir_open()
272  *
273  * Resets the given directory. The next call to g_dir_read_name()
274  * will return the first entry again.
275  **/
276 void
g_dir_rewind(GDir * dir)277 g_dir_rewind (GDir *dir)
278 {
279   g_return_if_fail (dir != NULL);
280 
281 #ifdef G_OS_WIN32
282   _wrewinddir (dir->wdirp);
283 #else
284   rewinddir (dir->dirp);
285 #endif
286 }
287 
288 /**
289  * g_dir_close:
290  * @dir: a #GDir* created by g_dir_open()
291  *
292  * Closes the directory and deallocates all related resources.
293  **/
294 void
g_dir_close(GDir * dir)295 g_dir_close (GDir *dir)
296 {
297   g_return_if_fail (dir != NULL);
298 
299 #ifdef G_OS_WIN32
300   _wclosedir (dir->wdirp);
301 #else
302   closedir (dir->dirp);
303 #endif
304   g_free (dir);
305 }
306 
307 #ifdef G_OS_WIN32
308 
309 /* Binary compatibility versions. Not for newly compiled code. */
310 
311 _GLIB_EXTERN GDir        *g_dir_open_utf8      (const gchar  *path,
312                                                 guint         flags,
313                                                 GError      **error);
314 _GLIB_EXTERN const gchar *g_dir_read_name_utf8 (GDir         *dir);
315 
316 GDir *
g_dir_open_utf8(const gchar * path,guint flags,GError ** error)317 g_dir_open_utf8 (const gchar  *path,
318                  guint         flags,
319                  GError      **error)
320 {
321   return g_dir_open (path, flags, error);
322 }
323 
324 const gchar *
g_dir_read_name_utf8(GDir * dir)325 g_dir_read_name_utf8 (GDir *dir)
326 {
327   return g_dir_read_name (dir);
328 }
329 
330 #endif
331