gst/gstregistryxml.c: Copy g_mkdir_with_parent() from glib, because we don't require...
authorDavid Schleef <ds@schleef.org>
Thu, 15 Sep 2005 00:35:11 +0000 (00:35 +0000)
committerDavid Schleef <ds@schleef.org>
Thu, 15 Sep 2005 00:35:11 +0000 (00:35 +0000)
Original commit message from CVS:
* gst/gstregistryxml.c: Copy g_mkdir_with_parent() from glib,
because we don't require glib-2.8.

ChangeLog
gst/gstregistryxml.c

index 55b3418..44bc4cf 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2005-09-14  David Schleef  <ds@schleef.org>
 
+       * gst/gstregistryxml.c: Copy g_mkdir_with_parent() from glib,
+         because we don't require glib-2.8.
+
+2005-09-14  David Schleef  <ds@schleef.org>
+
        * gst/gstregistryxml.c: Added.  Essentially moved out of the
          registries directory.
 
index c031c05..bb2c94a 100644 (file)
@@ -130,34 +130,68 @@ get_time (const char *path, gboolean * is_dir)
 }
 #endif
 
-static gboolean
-make_dir (gchar * filename)
+/* The following function was copied from GLIB-2.8.  When the glib
+ * requirement gets bumped to an appropriate level, this can be
+ * removed.  */
+/* gfileutils.c - File utility functions
+ *
+ *  Copyright 2000 Red Hat, Inc.
+ *
+ *  LGPL
+ */
+static int
+private_g_mkdir_with_parents (const gchar * pathname, int mode)
 {
-  struct stat dirstat;
-  gchar *dirname;
+  gchar *fn, *p;
 
-  if (strrchr (filename, '/') == NULL)
-    return FALSE;
+  if (pathname == NULL || *pathname == '\0') {
+    errno = EINVAL;
+    return -1;
+  }
 
-  dirname = g_strndup (filename, strrchr (filename, '/') - filename);
+  fn = g_strdup (pathname);
 
-  if (stat (dirname, &dirstat) == -1 && errno == ENOENT) {
-    if (g_mkdir (dirname, 0755) != 0) {
-      if (make_dir (dirname) != TRUE) {
-        g_free (dirname);
-        return FALSE;
-      } else {
-        if (g_mkdir (dirname, 0755) != 0) {
-          return FALSE;
-        }
+  if (g_path_is_absolute (fn))
+    p = (gchar *) g_path_skip_root (fn);
+  else
+    p = fn;
+
+  do {
+    while (*p && !G_IS_DIR_SEPARATOR (*p))
+      p++;
+
+    if (!*p)
+      p = NULL;
+    else
+      *p = '\0';
+
+    if (!g_file_test (fn, G_FILE_TEST_EXISTS)) {
+      if (g_mkdir (fn, mode) == -1) {
+        int errno_save = errno;
+
+        g_free (fn);
+        errno = errno_save;
+        return -1;
       }
+    } else if (!g_file_test (fn, G_FILE_TEST_IS_DIR)) {
+      g_free (fn);
+      errno = ENOTDIR;
+      return -1;
+    }
+    if (p) {
+      *p++ = G_DIR_SEPARATOR;
+      while (*p && G_IS_DIR_SEPARATOR (*p))
+        p++;
     }
   }
+  while (p);
 
-  g_free (dirname);
-  return TRUE;
+  g_free (fn);
+
+  return 0;
 }
 
+
 #if 0
 static void
 gst_registry_xml_get_perms_func (GstRegistry * registry)
@@ -989,7 +1023,7 @@ gst_registry_xml_write_cache (GstRegistry * registry, const char *location)
 
     /* oops, I bet the directory doesn't exist */
     dir = g_path_get_dirname (location);
-    g_mkdir_with_parents (dir, 0777);
+    private_g_mkdir_with_parents (dir, 0777);
     g_free (dir);
 
     registry->cache_file = fopen (tmp_location, "w");