[gvfs] Create directory and empty tree when opening nonexistant tree for write



commit 4e0d8b97655f7c78b7da642351bc919f1f5b2f5c
Author: Alexander Larsson <alexl redhat com>
Date:   Thu Jun 18 18:44:27 2009 +0200

    Create directory and empty tree when opening nonexistant tree for write

 metadata/metatree.c |   25 ++++++++++++++++++++++++-
 1 files changed, 24 insertions(+), 1 deletions(-)
---
diff --git a/metadata/metatree.c b/metadata/metatree.c
index 1ec73ed..b7a1d31 100644
--- a/metadata/metatree.c
+++ b/metadata/metatree.c
@@ -9,6 +9,7 @@
 #include <time.h>
 
 #include "metatree.h"
+#include "metabuilder.h"
 #include <glib.h>
 #include <glib/gstdio.h>
 #include <errno.h>
@@ -234,11 +235,33 @@ meta_tree_init (MetaTree *tree)
   int fd;
   void *data;
   guint32 *attributes;
+  gboolean retried;
   int i;
 
+  retried = FALSE;
+ retry:
   fd = open (tree->filename, O_RDONLY);
   if (fd == -1)
-    return FALSE;
+    {
+      if (tree->for_write && !retried)
+	{
+	  MetaBuilder *builder;
+	  char *dir;
+
+	  dir = g_path_get_dirname (tree->filename);
+	  g_mkdir_with_parents (dir, 0700);
+
+	  builder = meta_builder_new ();
+	  retried = TRUE;
+	  if (meta_builder_write (builder, tree->filename))
+	    {
+	      meta_builder_free (builder);
+	      goto retry;
+	    }
+	  meta_builder_free (builder);
+	}
+      return FALSE;
+    }
 
   if (fstat (fd, &statbuf) != 0 ||
       statbuf.st_size < sizeof (MetaFileHeader))



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]