* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <errno.h>
-#include <string.h> /* strcmp */
+#include <string.h>
+#include <stdio.h>
+#include <sys/stat.h>
#ifdef HAVE_DIRENT_H
#include <sys/types.h>
#include <dirent.h>
#endif
-#include "galias.h"
-#include "glib.h"
#include "gdir.h"
+#include "gconvert.h"
+#include "gfileutils.h"
+#include "gstrfuncs.h"
+#include "gtestutils.h"
#include "glibintl.h"
+#if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
+#include "../build/win32/dirent/dirent.h"
+#include "../build/win32/dirent/wdirent.c"
+#endif
+
+#include "glib-private.h" /* g_dir_open_with_errno, g_dir_new_from_dirp */
+
+/**
+ * GDir:
+ *
+ * An opaque structure representing an opened directory.
+ */
+
struct _GDir
{
- union {
- DIR *dirp;
#ifdef G_OS_WIN32
- _WDIR *wdirp;
+ _WDIR *wdirp;
+#else
+ DIR *dirp;
#endif
- } u;
#ifdef G_OS_WIN32
gchar utf8_buf[FILENAME_MAX*4];
#endif
};
+/*< 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);
+}
+
/**
* g_dir_open:
* @path: the path to the directory you are interested in. On Unix
* @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
- * g_dir_open_fails.
+ * g_dir_open() fails.
*
* Opens a directory for reading. The names of the files in the
- * directory can then be retrieved using g_dir_read_name().
+ * directory can then be retrieved using g_dir_read_name(). Note
+ * that the ordering is not defined.
*
- * Return value: a newly allocated #GDir on success, %NULL on failure.
+ * Returns: a newly allocated #GDir on success, %NULL on failure.
* If non-%NULL, you must free the result with g_dir_close()
* when you are finished with it.
**/
guint flags,
GError **error)
{
+ gint saved_errno;
GDir *dir;
-#ifndef G_OS_WIN32
- gchar *utf8_path;
-#endif
-
- g_return_val_if_fail (path != NULL, NULL);
-
-#ifdef G_OS_WIN32
- if (G_WIN32_HAVE_WIDECHAR_API ())
- {
- wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
-
- if (wpath == NULL)
- return NULL;
- dir = g_new (GDir, 1);
+ dir = g_dir_open_with_errno (path, flags);
- dir->u.wdirp = _wopendir (wpath);
- g_free (wpath);
-
- if (dir->u.wdirp)
- return dir;
- }
- else
+ if (dir == NULL)
{
- gchar *cp_path = g_locale_from_utf8 (path, -1, NULL, NULL, error);
+ gchar *utf8_path;
- if (cp_path == NULL)
- return NULL;
+ saved_errno = errno;
- dir = g_new (GDir, 1);
+ utf8_path = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);
- dir->u.dirp = opendir (cp_path);
-
- g_free (cp_path);
-
- if (dir->u.dirp)
- return dir;
+ g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (saved_errno),
+ _("Error opening directory '%s': %s"), utf8_path, g_strerror (saved_errno));
+ g_free (utf8_path);
}
- /* error case */
-
- g_set_error (error,
- G_FILE_ERROR,
- g_file_error_from_errno (errno),
- _("Error opening directory '%s': %s"),
- path, g_strerror (errno));
-
- g_free (dir);
-
- return NULL;
-#else
- dir = g_new (GDir, 1);
-
- dir->u.dirp = opendir (path);
-
- if (dir->u.dirp)
- return dir;
-
- /* error case */
- utf8_path = g_filename_to_utf8 (path, -1,
- NULL, NULL, NULL);
- g_set_error (error,
- G_FILE_ERROR,
- g_file_error_from_errno (errno),
- _("Error opening directory '%s': %s"),
- utf8_path, g_strerror (errno));
-
- g_free (utf8_path);
- g_free (dir);
-
- return NULL;
-#endif
+ return dir;
}
-#ifdef G_OS_WIN32
+#if defined (G_OS_WIN32) && !defined (_WIN64)
/* The above function actually is called g_dir_open_utf8, and it's
* that what applications compiled with this GLib version will
}
#endif
+/*< 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
+}
+
/**
* g_dir_read_name:
* @dir: a #GDir* created by g_dir_open()
*
- * Retrieves the name of the next entry in the directory. The '.' and
- * '..' entries are omitted. On Windows, the returned name is in
- * UTF-8. On Unix, it is in the on-disk encoding.
+ * 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
+ * factors.
+ *
+ * %NULL may also be returned in case of errors. On Unix, you can
+ * check `errno` to find out if %NULL was returned because of an error.
*
- * Return value: The entry's name or %NULL if there are no
+ * 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.
+ *
+ * Returns: The entry's name or %NULL if there are no
* more entries. The return value is owned by GLib and
* must not be modified or freed.
**/
-G_CONST_RETURN gchar*
+const gchar *
g_dir_read_name (GDir *dir)
{
+#ifdef G_OS_WIN32
+ gchar *utf8_name;
+ struct _wdirent *wentry;
+#else
struct dirent *entry;
+#endif
g_return_val_if_fail (dir != NULL, NULL);
#ifdef G_OS_WIN32
- if (G_WIN32_HAVE_WIDECHAR_API ())
+ while (1)
{
- gchar *utf8_name;
- struct _wdirent *wentry;
+ wentry = _wreaddir (dir->wdirp);
+ while (wentry
+ && (0 == wcscmp (wentry->d_name, L".") ||
+ 0 == wcscmp (wentry->d_name, L"..")))
+ wentry = _wreaddir (dir->wdirp);
- while (1)
- {
- wentry = _wreaddir (dir->u.wdirp);
- while (wentry
- && (0 == wcscmp (wentry->d_name, L".") ||
- 0 == wcscmp (wentry->d_name, L"..")))
- wentry = _wreaddir (dir->u.wdirp);
-
- 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;
- }
- }
- else
- {
- while (1)
- {
- gchar *utf8_name;
-
- entry = readdir (dir->u.dirp);
- while (entry
- && (0 == strcmp (entry->d_name, ".") ||
- 0 == strcmp (entry->d_name, "..")))
- entry = readdir (dir->u.dirp);
-
- if (entry == NULL)
- return NULL;
-
- utf8_name = g_locale_to_utf8 (entry->d_name, -1, NULL, NULL, NULL);
-
- if (utf8_name != NULL)
- {
- strcpy (dir->utf8_buf, utf8_name);
- g_free (utf8_name);
-
- return dir->utf8_buf;
- }
- }
+ 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;
}
#else
- entry = readdir (dir->u.dirp);
+ entry = readdir (dir->dirp);
while (entry
&& (0 == strcmp (entry->d_name, ".") ||
0 == strcmp (entry->d_name, "..")))
- entry = readdir (dir->u.dirp);
+ entry = readdir (dir->dirp);
if (entry)
return entry->d_name;
#endif
}
-#ifdef G_OS_WIN32
+#if defined (G_OS_WIN32) && !defined (_WIN64)
/* Ditto for g_dir_read_name */
/* Binary compatibility version. Not for newly compiled code. */
-G_CONST_RETURN gchar*
+const gchar *
g_dir_read_name (GDir *dir)
{
while (1)
g_return_if_fail (dir != NULL);
#ifdef G_OS_WIN32
- if (G_WIN32_HAVE_WIDECHAR_API ())
- {
- _wrewinddir (dir->u.wdirp);
- return;
- }
+ _wrewinddir (dir->wdirp);
+#else
+ rewinddir (dir->dirp);
#endif
-
- rewinddir (dir->u.dirp);
}
/**
g_return_if_fail (dir != NULL);
#ifdef G_OS_WIN32
- if (G_WIN32_HAVE_WIDECHAR_API ())
- {
- _wclosedir (dir->u.wdirp);
- g_free (dir);
- return;
- }
+ _wclosedir (dir->wdirp);
+#else
+ closedir (dir->dirp);
#endif
-
- closedir (dir->u.dirp);
g_free (dir);
}