From 08981f18d48f86a9186865a6959f876fa7193cce Mon Sep 17 00:00:00 2001
From: Benedikt Meurer <benny@xfce.org>
Date: Mon, 25 Jul 2005 20:08:37 +0000
Subject: [PATCH] 2005-07-25	Benedikt Meurer <benny@xfce.org>

	* thunar-vfs/thunar-vfs-sysdep.{c,h}: Add _thunar_vfs_sysdep_readdir()
	  as a portable, thread-safe readdir replacement.
	* thunar-vfs/thunar-vfs-job-listdir.c(thunar_vfs_job_listdir_execute):
	  Use _thunar_vfs_sysdep_readdir().
	* thunar-vfs/Makefile.am: Add the sysdep component to the build
	  framework.




(Old svn revision: 16412)
---
 ChangeLog                           |   9 +++
 thunar-vfs/Makefile.am              |   2 +
 thunar-vfs/thunar-vfs-job-listdir.c |  12 +--
 thunar-vfs/thunar-vfs-sysdep.c      | 115 ++++++++++++++++++++++++++++
 thunar-vfs/thunar-vfs-sysdep.h      |  35 +++++++++
 5 files changed, 164 insertions(+), 9 deletions(-)
 create mode 100644 thunar-vfs/thunar-vfs-sysdep.c
 create mode 100644 thunar-vfs/thunar-vfs-sysdep.h

diff --git a/ChangeLog b/ChangeLog
index d23c3e973..5bc5cd9c1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2005-07-25	Benedikt Meurer <benny@xfce.org>
+
+	* thunar-vfs/thunar-vfs-sysdep.{c,h}: Add _thunar_vfs_sysdep_readdir()
+	  as a portable, thread-safe readdir replacement.
+	* thunar-vfs/thunar-vfs-job-listdir.c(thunar_vfs_job_listdir_execute):
+	  Use _thunar_vfs_sysdep_readdir().
+	* thunar-vfs/Makefile.am: Add the sysdep component to the build
+	  framework.
+
 2005-07-24	Benedikt Meurer <benny@xfce.org>
 
 	* thunar/thunar-desktop-view.c,
diff --git a/thunar-vfs/Makefile.am b/thunar-vfs/Makefile.am
index 9762e8cb8..2002252f4 100644
--- a/thunar-vfs/Makefile.am
+++ b/thunar-vfs/Makefile.am
@@ -40,6 +40,8 @@ libthunar_vfs_la_SOURCES =						\
 	thunar-vfs-mime-parser.h					\
 	thunar-vfs-mime-parser.c					\
 	thunar-vfs-monitor.c						\
+	thunar-vfs-sysdep.c						\
+	thunar-vfs-sysdep.h						\
 	thunar-vfs-trash.c						\
 	thunar-vfs-user.c						\
 	thunar-vfs-util.c						\
diff --git a/thunar-vfs/thunar-vfs-job-listdir.c b/thunar-vfs/thunar-vfs-job-listdir.c
index 346a38d63..0a5615d28 100644
--- a/thunar-vfs/thunar-vfs-job-listdir.c
+++ b/thunar-vfs/thunar-vfs-job-listdir.c
@@ -43,6 +43,7 @@
 #endif
 
 #include <thunar-vfs/thunar-vfs-job-listdir.h>
+#include <thunar-vfs/thunar-vfs-sysdep.h>
 
 
 
@@ -183,20 +184,13 @@ thunar_vfs_job_listdir_execute (ThunarVfsJob *job)
        */
       for (;;)
         {
-          if (G_UNLIKELY (readdir_r (dp, &d_buffer, &d) < 0))
-            {
-              error = g_error_new_literal (G_FILE_ERROR, g_file_error_from_errno (errno), g_strerror (errno));
-              break;
-            }
+          if (G_UNLIKELY (!_thunar_vfs_sysdep_readdir (dp, &d_buffer, &d, &error)))
+            break;
 
           /* check for end of directory */
           if (G_UNLIKELY (d == NULL))
               break;
 
-          /* ignore "." and ".." entries */
-          if (G_UNLIKELY (d->d_name[0] == '.' && (d->d_name[1] == '\0' || (d->d_name[1] == '.' && d->d_name[2] == '\0'))))
-            continue;
-
           names = g_slist_insert_sorted (names, g_string_chunk_insert (names_chunk, d->d_name), (GCompareFunc) strcmp);
         }
 
diff --git a/thunar-vfs/thunar-vfs-sysdep.c b/thunar-vfs/thunar-vfs-sysdep.c
new file mode 100644
index 000000000..46e89164a
--- /dev/null
+++ b/thunar-vfs/thunar-vfs-sysdep.c
@@ -0,0 +1,115 @@
+/* $Id$ */
+/*-
+ * Copyright (c) 2005 Benedikt Meurer <benny@xfce.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library 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.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
+#ifdef HAVE_DIRENT_H
+#include <dirent.h>
+#endif
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+#ifdef HAVE_MEMORY_H
+#include <memory.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#include <thunar-vfs/thunar-vfs-sysdep.h>
+
+
+
+/**
+ * _thunar_vfs_sysdep_readdir:
+ * @dirp    : the %DIR pointer.
+ * @entry   : the buffer to store the dir entry to.
+ * @result  : result pointer.
+ * @error   : return location for errors or %NULL.
+ *
+ * This function provides a thread-safe way to read the
+ * next directory entry from a %DIR pointer. The caller
+ * must provide a buffer where the result can be stored
+ * to (the @entry parameter). If the operation succeeds,
+ * %TRUE will be returned, and @result will be set to
+ * point to %NULL or @result, depending on whether the
+ * end of the directory was reached.
+ *
+ * This function will never return directory entries
+ * for '.' and '..', as they aren't used throughout
+ * Thunar.
+ *
+ * Return value: %FALSE if an error occurred, else %TRUE.
+ **/
+gboolean
+_thunar_vfs_sysdep_readdir (gpointer        dirp,
+                            struct dirent  *entry,
+                            struct dirent **result,
+                            GError        **error)
+{
+#ifndef HAVE_READDIR_R
+  G_LOCK_DEFINE_STATIC (readdir);
+#endif
+
+  g_return_val_if_fail (dirp != NULL, FALSE);
+  g_return_val_if_fail (entry != NULL, FALSE);
+  g_return_val_if_fail (result != NULL, FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  for (;;)
+    {
+#ifdef HAVE_READDIR_R
+      if (G_UNLIKELY (readdir_r (dirp, entry, result) < 0))
+        {
+          g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
+                       g_strerror (errno));
+          return FALSE;
+        }
+#else
+      G_LOCK (readdir);
+      *result = readdir (dirp);
+      if (G_LIKELY (*result != NULL))
+        *result = memcpy (entry, *result, sizeof (*entry));
+      G_UNLOCK (readdir);
+#endif
+
+      /* ignore "." and ".." entries */
+      if (G_LIKELY (*result != NULL))
+        {
+          const gchar *name = entry->d_name;
+          if (G_UNLIKELY (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))))
+            continue;
+        }
+
+      /* we got our next entry */
+      break;
+    }
+
+  return TRUE;
+}
+
+
+
diff --git a/thunar-vfs/thunar-vfs-sysdep.h b/thunar-vfs/thunar-vfs-sysdep.h
new file mode 100644
index 000000000..68534ce05
--- /dev/null
+++ b/thunar-vfs/thunar-vfs-sysdep.h
@@ -0,0 +1,35 @@
+/* $Id$ */
+/*-
+ * Copyright (c) 2005 Benedikt Meurer <benny@xfce.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library 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.
+ */
+
+#ifndef __THUNAR_VFS_SYSDEP_H__
+#define __THUNAR_VFS_SYSDEP_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS;
+
+gboolean _thunar_vfs_sysdep_readdir (gpointer        dirp,
+                                     struct dirent  *entry,
+                                     struct dirent **result,
+                                     GError        **error);
+
+G_END_DECLS;
+
+#endif /* !__THUNAR_VFS_SYSDEP_H__ */
-- 
GitLab