$OpenBSD: patch-glib_gfileutils_c,v 1.4 2019/03/10 09:00:29 ajacoutot Exp $

Try to create the complete path right away and fall back to creating all path
elements one by one.

Ignore ENOENT errors up until the last element to help programs using unveil(2).

Index: glib/gfileutils.c
--- glib/gfileutils.c.orig
+++ glib/gfileutils.c
@@ -226,6 +226,20 @@ g_mkdir_with_parents (const gchar *pathname,
       return -1;
     }
 
+  /* try to create the full path first */
+  if (g_mkdir (pathname, mode) == 0)
+    return 0;
+  else if (errno == EEXIST)
+    {
+      if (!g_file_test (pathname, G_FILE_TEST_IS_DIR))
+        {
+          errno = ENOTDIR;
+          return -1;
+        }
+      return 0;
+    }
+
+  /* walk the full path and try creating each element */
   fn = g_strdup (pathname);
 
   if (g_path_is_absolute (fn))
@@ -245,7 +259,7 @@ g_mkdir_with_parents (const gchar *pathname,
       
       if (!g_file_test (fn, G_FILE_TEST_EXISTS))
 	{
-	  if (g_mkdir (fn, mode) == -1 && errno != EEXIST)
+	  if (g_mkdir (fn, mode) == -1 && errno != EEXIST && (p ? (errno != ENOENT) : (-1)))
 	    {
 	      int errno_save = errno;
 	      g_free (fn);
