GDir: add some glib-private APIs
[platform/upstream/glib.git] / glib / gdir.c
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 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, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include "config.h"
26
27 #include <errno.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <sys/stat.h>
31
32 #ifdef HAVE_DIRENT_H
33 #include <sys/types.h>
34 #include <dirent.h>
35 #endif
36
37 #include "gdir.h"
38
39 #include "gconvert.h"
40 #include "gfileutils.h"
41 #include "gstrfuncs.h"
42 #include "gtestutils.h"
43 #include "glibintl.h"
44
45 #if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
46 #include "../build/win32/dirent/dirent.h"
47 #include "../build/win32/dirent/wdirent.c"
48 #endif
49
50 #include "glib-private.h" /* g_dir_open_with_errno, g_dir_new_from_dirp */
51
52 /**
53  * GDir:
54  *
55  * An opaque structure representing an opened directory.
56  */
57
58 struct _GDir
59 {
60 #ifdef G_OS_WIN32
61   _WDIR *wdirp;
62 #else
63   DIR *dirp;
64 #endif
65 #ifdef G_OS_WIN32
66   gchar utf8_buf[FILENAME_MAX*4];
67 #endif
68 };
69
70 /*< private >
71  * g_dir_open_with_errno:
72  * @path: the path to the directory you are interested in.
73  * @flags: Currently must be set to 0. Reserved for future use.
74  *
75  * Opens a directory for reading.
76  *
77  * This function is equivalent to g_dir_open() except in the error case,
78  * errno will be set accordingly.
79  *
80  * This is useful if you want to construct your own error message.
81  *
82  * Returns: a newly allocated #GDir on success, or %NULL on failure,
83  *   with errno set accordingly.
84  *
85  * Since: 2.38
86  */
87 GDir *
88 g_dir_open_with_errno (const gchar *path,
89                        guint        flags)
90 {
91   GDir dir;
92 #ifdef G_OS_WIN32
93   gint saved_errno;
94   wchar_t *wpath;
95 #endif
96
97   g_return_val_if_fail (path != NULL, NULL);
98
99 #ifdef G_OS_WIN32
100   wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
101
102   g_return_val_if_fail (wpath != NULL, NULL);
103
104   dir.wdirp = _wopendir (wpath);
105   saved_errno = errno;
106   g_free (wpath);
107   errno = saved_errno;
108
109   if (dir.wdirp == NULL)
110     return NULL;
111 #else
112   dir.dirp = opendir (path);
113
114   if (dir.dirp == NULL)
115     return NULL;
116 #endif
117
118   return g_memdup (&dir, sizeof dir);
119 }
120
121 /**
122  * g_dir_open:
123  * @path: the path to the directory you are interested in. On Unix
124  *         in the on-disk encoding. On Windows in UTF-8
125  * @flags: Currently must be set to 0. Reserved for future use.
126  * @error: return location for a #GError, or %NULL.
127  *         If non-%NULL, an error will be set if and only if
128  *         g_dir_open() fails.
129  *
130  * Opens a directory for reading. The names of the files in the
131  * directory can then be retrieved using g_dir_read_name().  Note
132  * that the ordering is not defined.
133  *
134  * Return value: a newly allocated #GDir on success, %NULL on failure.
135  *   If non-%NULL, you must free the result with g_dir_close()
136  *   when you are finished with it.
137  **/
138 GDir *
139 g_dir_open (const gchar  *path,
140             guint         flags,
141             GError      **error)
142 {
143   gint saved_errno;
144   GDir *dir;
145
146   dir = g_dir_open_with_errno (path, flags);
147
148   if (dir == NULL)
149     {
150       gchar *utf8_path;
151
152       saved_errno = errno;
153
154       utf8_path = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);
155
156       g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (saved_errno),
157                    _("Error opening directory '%s': %s"), utf8_path, g_strerror (saved_errno));
158       g_free (utf8_path);
159     }
160
161   return dir;
162 }
163
164 #if defined (G_OS_WIN32) && !defined (_WIN64)
165
166 /* The above function actually is called g_dir_open_utf8, and it's
167  * that what applications compiled with this GLib version will
168  * use.
169  */
170
171 #undef g_dir_open
172
173 /* Binary compatibility version. Not for newly compiled code. */
174
175 GDir *
176 g_dir_open (const gchar  *path,
177             guint         flags,
178             GError      **error)
179 {
180   gchar *utf8_path = g_locale_to_utf8 (path, -1, NULL, NULL, error);
181   GDir *retval;
182
183   if (utf8_path == NULL)
184     return NULL;
185
186   retval = g_dir_open_utf8 (utf8_path, flags, error);
187
188   g_free (utf8_path);
189
190   return retval;
191 }
192 #endif
193
194 /*< private >
195  * g_dir_new_from_dirp:
196  * @dirp: a #DIR* created by opendir() or fdopendir()
197  *
198  * Creates a #GDir object from the DIR object that is created using
199  * opendir() or fdopendir().  The created #GDir assumes ownership of the
200  * passed-in #DIR pointer.
201  *
202  * @dirp must not be %NULL.
203  *
204  * This function never fails.
205  *
206  * Returns: a newly allocated #GDir, which should be closed using
207  *     g_dir_close().
208  *
209  * Since: 2.38
210  **/
211 GDir *
212 g_dir_new_from_dirp (gpointer dirp)
213 {
214 #ifdef G_OS_UNIX
215   GDir *dir;
216
217   g_return_val_if_fail (dirp != NULL, NULL);
218
219   dir = g_new (GDir, 1);
220   dir->dirp = dirp;
221
222   return dir;
223 #else
224   g_assert_not_reached ();
225 #endif
226 }
227
228 /**
229  * g_dir_read_name:
230  * @dir: a #GDir* created by g_dir_open()
231  *
232  * Retrieves the name of another entry in the directory, or %NULL.
233  * The order of entries returned from this function is not defined,
234  * and may vary by file system or other operating-system dependent
235  * factors.
236  *
237  * %NULL may also be returned in case of errors. On Unix, you can
238  * check <literal>errno</literal> to find out if %NULL was returned
239  * because of an error.
240  *
241  * On Unix, the '.' and '..' entries are omitted, and the returned
242  * name is in the on-disk encoding.
243  *
244  * On Windows, as is true of all GLib functions which operate on
245  * filenames, the returned name is in UTF-8.
246  *
247  * Return value: The entry's name or %NULL if there are no
248  *   more entries. The return value is owned by GLib and
249  *   must not be modified or freed.
250  **/
251 const gchar *
252 g_dir_read_name (GDir *dir)
253 {
254 #ifdef G_OS_WIN32
255   gchar *utf8_name;
256   struct _wdirent *wentry;
257 #else
258   struct dirent *entry;
259 #endif
260
261   g_return_val_if_fail (dir != NULL, NULL);
262
263 #ifdef G_OS_WIN32
264   while (1)
265     {
266       wentry = _wreaddir (dir->wdirp);
267       while (wentry 
268              && (0 == wcscmp (wentry->d_name, L".") ||
269                  0 == wcscmp (wentry->d_name, L"..")))
270         wentry = _wreaddir (dir->wdirp);
271
272       if (wentry == NULL)
273         return NULL;
274
275       utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
276
277       if (utf8_name == NULL)
278         continue;               /* Huh, impossible? Skip it anyway */
279
280       strcpy (dir->utf8_buf, utf8_name);
281       g_free (utf8_name);
282
283       return dir->utf8_buf;
284     }
285 #else
286   entry = readdir (dir->dirp);
287   while (entry 
288          && (0 == strcmp (entry->d_name, ".") ||
289              0 == strcmp (entry->d_name, "..")))
290     entry = readdir (dir->dirp);
291
292   if (entry)
293     return entry->d_name;
294   else
295     return NULL;
296 #endif
297 }
298
299 #if defined (G_OS_WIN32) && !defined (_WIN64)
300
301 /* Ditto for g_dir_read_name */
302
303 #undef g_dir_read_name
304
305 /* Binary compatibility version. Not for newly compiled code. */
306
307 const gchar *
308 g_dir_read_name (GDir *dir)
309 {
310   while (1)
311     {
312       const gchar *utf8_name = g_dir_read_name_utf8 (dir);
313       gchar *retval;
314       
315       if (utf8_name == NULL)
316         return NULL;
317
318       retval = g_locale_from_utf8 (utf8_name, -1, NULL, NULL, NULL);
319
320       if (retval != NULL)
321         {
322           strcpy (dir->utf8_buf, retval);
323           g_free (retval);
324
325           return dir->utf8_buf;
326         }
327     }
328 }
329
330 #endif
331
332 /**
333  * g_dir_rewind:
334  * @dir: a #GDir* created by g_dir_open()
335  *
336  * Resets the given directory. The next call to g_dir_read_name()
337  * will return the first entry again.
338  **/
339 void
340 g_dir_rewind (GDir *dir)
341 {
342   g_return_if_fail (dir != NULL);
343   
344 #ifdef G_OS_WIN32
345   _wrewinddir (dir->wdirp);
346 #else
347   rewinddir (dir->dirp);
348 #endif
349 }
350
351 /**
352  * g_dir_close:
353  * @dir: a #GDir* created by g_dir_open()
354  *
355  * Closes the directory and deallocates all related resources.
356  **/
357 void
358 g_dir_close (GDir *dir)
359 {
360   g_return_if_fail (dir != NULL);
361
362 #ifdef G_OS_WIN32
363   _wclosedir (dir->wdirp);
364 #else
365   closedir (dir->dirp);
366 #endif
367   g_free (dir);
368 }