[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / glib / gdir.c
index 0c304f6..e6cf5e6 100644 (file)
@@ -17,9 +17,7 @@
  * 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"
@@ -47,6 +45,8 @@
 #include "../build/win32/dirent/wdirent.c"
 #endif
 
+#include "glib-private.h" /* g_dir_open_with_errno, g_dir_new_from_dirp */
+
 /**
  * GDir:
  *
@@ -65,6 +65,57 @@ struct _GDir
 #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
@@ -78,7 +129,7 @@ struct _GDir
  * 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.
  **/
@@ -87,67 +138,25 @@ g_dir_open (const gchar  *path,
             guint         flags,
             GError      **error)
 {
+  gint saved_errno;
   GDir *dir;
-  int errsv;
-#ifdef G_OS_WIN32
-  wchar_t *wpath;
-#else
-  gchar *utf8_path;
-#endif
-
-  g_return_val_if_fail (path != NULL, NULL);
-
-#ifdef G_OS_WIN32
-  wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
 
-  if (wpath == NULL)
-    return NULL;
-
-  dir = g_new (GDir, 1);
-
-  dir->wdirp = _wopendir (wpath);
-  g_free (wpath);
+  dir = g_dir_open_with_errno (path, flags);
 
-  if (dir->wdirp)
-    return dir;
-
-  /* error case */
-  errsv = errno;
-
-  g_set_error (error,
-              G_FILE_ERROR,
-              g_file_error_from_errno (errsv),
-              _("Error opening directory '%s': %s"),
-              path, g_strerror (errsv));
-  
-  g_free (dir);
-      
-  return NULL;
-#else
-  dir = g_new (GDir, 1);
-
-  dir->dirp = opendir (path);
-
-  if (dir->dirp)
-    return dir;
-
-  /* error case */
-  errsv = errno;
+  if (dir == NULL)
+    {
+      gchar *utf8_path;
 
-  utf8_path = g_filename_to_utf8 (path, -1,
-                                 NULL, NULL, NULL);
+      saved_errno = errno;
 
-  g_set_error (error,
-               G_FILE_ERROR,
-               g_file_error_from_errno (errsv),
-               _("Error opening directory '%s': %s"),
-              utf8_path, g_strerror (errsv));
+      utf8_path = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);
 
-  g_free (utf8_path);
-  g_free (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);
+    }
 
-  return NULL;
-#endif
+  return dir;
 }
 
 #if defined (G_OS_WIN32) && !defined (_WIN64)
@@ -180,6 +189,40 @@ g_dir_open (const gchar  *path,
 }
 #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()
@@ -190,8 +233,7 @@ g_dir_open (const gchar  *path,
  * factors.
  *
  * %NULL may also be returned in case of errors. On Unix, you can
- * check <literal>errno</literal> to find out if %NULL was returned
- * because of an error.
+ * check `errno` to find out if %NULL was returned because of an error.
  *
  * On Unix, the '.' and '..' entries are omitted, and the returned
  * name is in the on-disk encoding.
@@ -199,7 +241,7 @@ g_dir_open (const gchar  *path,
  * On Windows, as is true of all GLib functions which operate on
  * filenames, the returned name is in UTF-8.
  *
- * Return value: The entry's name or %NULL if there are no
+ * 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.
  **/