gdir.c 7.56 KB
Newer Older
1 2 3 4 5 6
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * gdir.c: Simplified wrapper around the DIRENT functions.
 *
 * Copyright 2001 Hans Breuer
7
 * Copyright 2004 Tor Lillqvist
8 9 10 11
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * 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 15 16 17 18 19
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 24
 */

#include "config.h"

Owen Taylor's avatar
Owen Taylor committed
25
#include <errno.h>
26
#include <string.h>
27
#include <stdio.h>
28
#include <sys/stat.h>
Owen Taylor's avatar
Owen Taylor committed
29

30
#ifdef HAVE_DIRENT_H
31
#include <sys/types.h>
32 33 34 35 36
#include <dirent.h>
#endif

#include "gdir.h"

Matthias Clasen's avatar
Matthias Clasen committed
37 38
#include "gconvert.h"
#include "gfileutils.h"
39
#include "gstrfuncs.h"
Matthias Clasen's avatar
Matthias Clasen committed
40
#include "gtestutils.h"
41
#include "glibintl.h"
42

43 44 45 46 47
#if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
#include "../build/win32/dirent/dirent.h"
#include "../build/win32/dirent/wdirent.c"
#endif

48 49
#include "glib-private.h" /* g_dir_open_with_errno, g_dir_new_from_dirp */

50 51 52 53 54 55
/**
 * GDir:
 *
 * An opaque structure representing an opened directory.
 */

56 57
struct _GDir
{
58
#ifdef G_OS_WIN32
59 60 61
  _WDIR *wdirp;
#else
  DIR *dirp;
62 63 64 65
#endif
#ifdef G_OS_WIN32
  gchar utf8_buf[FILENAME_MAX*4];
#endif
66 67
};

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/*< private >
 * g_dir_open_with_errno:
 * @path: the path to the directory you are interested in.
 * @flags: Currently must be set to 0. Reserved for future use.
 *
 * Opens a directory for reading.
 *
 * This function is equivalent to g_dir_open() except in the error case,
 * errno will be set accordingly.
 *
 * This is useful if you want to construct your own error message.
 *
 * Returns: a newly allocated #GDir on success, or %NULL on failure,
 *   with errno set accordingly.
 *
 * Since: 2.38
 */
GDir *
g_dir_open_with_errno (const gchar *path,
                       guint        flags)
{
  GDir dir;
#ifdef G_OS_WIN32
  gint saved_errno;
  wchar_t *wpath;
#endif

  g_return_val_if_fail (path != NULL, NULL);

#ifdef G_OS_WIN32
  wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);

  g_return_val_if_fail (wpath != NULL, NULL);

  dir.wdirp = _wopendir (wpath);
  saved_errno = errno;
  g_free (wpath);
  errno = saved_errno;

  if (dir.wdirp == NULL)
    return NULL;
#else
  dir.dirp = opendir (path);

  if (dir.dirp == NULL)
    return NULL;
#endif

  return g_memdup (&dir, sizeof dir);
}

119 120
/**
 * g_dir_open:
121 122
 * @path: the path to the directory you are interested in. On Unix
 *         in the on-disk encoding. On Windows in UTF-8
123 124 125
 * @flags: Currently must be set to 0. Reserved for future use.
 * @error: return location for a #GError, or %NULL.
 *         If non-%NULL, an error will be set if and only if
Matthias Clasen's avatar
Matthias Clasen committed
126
 *         g_dir_open() fails.
127
 *
128
 * Opens a directory for reading. The names of the files in the
129 130
 * directory can then be retrieved using g_dir_read_name().  Note
 * that the ordering is not defined.
131
 *
132
 * Returns: a newly allocated #GDir on success, %NULL on failure.
133 134
 *   If non-%NULL, you must free the result with g_dir_close()
 *   when you are finished with it.
135 136 137 138 139 140
 **/
GDir *
g_dir_open (const gchar  *path,
            guint         flags,
            GError      **error)
{
141
  gint saved_errno;
142 143
  GDir *dir;

144
  dir = g_dir_open_with_errno (path, flags);
145

146 147 148
  if (dir == NULL)
    {
      gchar *utf8_path;
149

150
      saved_errno = errno;
151

152
      utf8_path = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);
153

154
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (saved_errno),
155
                   _("Error opening directory “%s”: %s"), utf8_path, g_strerror (saved_errno));
156 157
      g_free (utf8_path);
    }
158

159
  return dir;
160 161
}

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
/*< private >
 * g_dir_new_from_dirp:
 * @dirp: a #DIR* created by opendir() or fdopendir()
 *
 * Creates a #GDir object from the DIR object that is created using
 * opendir() or fdopendir().  The created #GDir assumes ownership of the
 * passed-in #DIR pointer.
 *
 * @dirp must not be %NULL.
 *
 * This function never fails.
 *
 * Returns: a newly allocated #GDir, which should be closed using
 *     g_dir_close().
 *
 * Since: 2.38
 **/
GDir *
g_dir_new_from_dirp (gpointer dirp)
{
#ifdef G_OS_UNIX
  GDir *dir;

  g_return_val_if_fail (dirp != NULL, NULL);

  dir = g_new (GDir, 1);
  dir->dirp = dirp;

  return dir;
#else
  g_assert_not_reached ();
#endif
}

196 197 198 199
/**
 * g_dir_read_name:
 * @dir: a #GDir* created by g_dir_open()
 *
200 201 202
 * Retrieves the name of another entry in the directory, or %NULL.
 * The order of entries returned from this function is not defined,
 * and may vary by file system or other operating-system dependent
203 204 205
 * factors.
 *
 * %NULL may also be returned in case of errors. On Unix, you can
206
 * check `errno` to find out if %NULL was returned because of an error.
207 208 209 210 211 212
 *
 * On Unix, the '.' and '..' entries are omitted, and the returned
 * name is in the on-disk encoding.
 *
 * On Windows, as is true of all GLib functions which operate on
 * filenames, the returned name is in UTF-8.
213
 *
214
 * Returns: (type filename): The entry's name or %NULL if there are no
215 216
 *   more entries. The return value is owned by GLib and
 *   must not be modified or freed.
217
 **/
218
const gchar *
219
g_dir_read_name (GDir *dir)
220
{
221 222 223 224
#ifdef G_OS_WIN32
  gchar *utf8_name;
  struct _wdirent *wentry;
#else
225
  struct dirent *entry;
226
#endif
227 228 229

  g_return_val_if_fail (dir != NULL, NULL);

230
#ifdef G_OS_WIN32
231
  while (1)
232
    {
233 234 235 236 237
      wentry = _wreaddir (dir->wdirp);
      while (wentry 
	     && (0 == wcscmp (wentry->d_name, L".") ||
		 0 == wcscmp (wentry->d_name, L"..")))
	wentry = _wreaddir (dir->wdirp);
238

239 240 241 242 243 244 245 246 247 248 249 250
      if (wentry == NULL)
	return NULL;

      utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);

      if (utf8_name == NULL)
	continue;		/* Huh, impossible? Skip it anyway */

      strcpy (dir->utf8_buf, utf8_name);
      g_free (utf8_name);

      return dir->utf8_buf;
251 252
    }
#else
253
  entry = readdir (dir->dirp);
254
  while (entry 
255 256
         && (0 == strcmp (entry->d_name, ".") ||
             0 == strcmp (entry->d_name, "..")))
257
    entry = readdir (dir->dirp);
258

259 260 261 262
  if (entry)
    return entry->d_name;
  else
    return NULL;
263 264 265
#endif
}

266 267 268 269 270 271 272 273 274 275 276 277
/**
 * g_dir_rewind:
 * @dir: a #GDir* created by g_dir_open()
 *
 * Resets the given directory. The next call to g_dir_read_name()
 * will return the first entry again.
 **/
void
g_dir_rewind (GDir *dir)
{
  g_return_if_fail (dir != NULL);
  
278
#ifdef G_OS_WIN32
279 280 281
  _wrewinddir (dir->wdirp);
#else
  rewinddir (dir->dirp);
282
#endif
283 284 285 286 287 288 289 290
}

/**
 * g_dir_close:
 * @dir: a #GDir* created by g_dir_open()
 *
 * Closes the directory and deallocates all related resources.
 **/
291
void
292 293
g_dir_close (GDir *dir)
{
Owen Taylor's avatar
Owen Taylor committed
294
  g_return_if_fail (dir != NULL);
295

296
#ifdef G_OS_WIN32
297 298 299
  _wclosedir (dir->wdirp);
#else
  closedir (dir->dirp);
300
#endif
301 302
  g_free (dir);
}
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

#ifdef G_OS_WIN32

/* Binary compatibility versions. Not for newly compiled code. */

_GLIB_EXTERN GDir        *g_dir_open_utf8      (const gchar  *path,
                                                guint         flags,
                                                GError      **error);
_GLIB_EXTERN const gchar *g_dir_read_name_utf8 (GDir         *dir);

GDir *
g_dir_open_utf8 (const gchar  *path,
                 guint         flags,
                 GError      **error)
{
  return g_dir_open (path, flags, error);
}

const gchar *
g_dir_read_name_utf8 (GDir *dir)
{
  return g_dir_read_name (dir);
}

#endif