[geary] Provide and use an async make_directory_with_parents in Db.Database.



commit 4ac7c10f3898332533d68b6a6f08bed5d97b0a22
Author: Michael James Gratton <mike vee net>
Date:   Mon May 21 21:08:00 2018 +1000

    Provide and use an async make_directory_with_parents in Db.Database.

 src/engine/db/db-database.vala  |   12 +-----------
 src/engine/util/util-files.vala |   29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 11 deletions(-)
---
diff --git a/src/engine/db/db-database.vala b/src/engine/db/db-database.vala
index 3d3aeb2..90f8f5c 100644
--- a/src/engine/db/db-database.vala
+++ b/src/engine/db/db-database.vala
@@ -114,17 +114,7 @@ public class Geary.Db.Database : Geary.Db.Context {
         this.prepare_cb = prepare_cb;
 
         if (this.file != null && (flags & DatabaseFlags.CREATE_DIRECTORY) != 0) {
-            GLib.File db_dir = this.file.get_parent();
-            try {
-                yield db_dir.query_info_async(
-                    GLib.FileAttribute.STANDARD_TYPE,
-                    GLib.FileQueryInfoFlags.NONE,
-                    GLib.Priority.DEFAULT,
-                    cancellable
-                );
-            } catch (GLib.IOError.NOT_FOUND err) {
-                db_dir.make_directory_with_parents(cancellable);
-            }
+            yield Geary.Files.make_directory_with_parents(this.file.get_parent());
         }
 
         if (threadsafe()) {
diff --git a/src/engine/util/util-files.vala b/src/engine/util/util-files.vala
index 5f0827e..2869cf3 100644
--- a/src/engine/util/util-files.vala
+++ b/src/engine/util/util-files.vala
@@ -94,6 +94,35 @@ public async FileType query_file_type_async(File file, bool follow_symlinks, Can
     return info.get_file_type();
 }
 
+/**
+ * Ensure a directory exists, asynchronously.
+ *
+ * Returns true if the directory ws created. A {@link GLib.Error} is
+ * thrown if the directory cannot be created, but not if it already
+ * exists.
+ */
+public async bool make_directory_with_parents(File dir,
+                                              Cancellable? cancellable = null)
+    throws Error {
+    bool ret = false;
+    GLib.IOError? create_err = null;
+    yield Nonblocking.Concurrent.global.schedule_async(() => {
+            try {
+                dir.make_directory_with_parents(cancellable);
+            } catch (GLib.IOError err) {
+                create_err = err;
+            }
+        });
+
+    if (create_err == null) {
+        ret = true;
+    } else if (!(create_err is GLib.IOError.EXISTS)) {
+        throw create_err;
+    }
+
+    return ret;
+}
+
 public uint hash(File file) {
     return file.hash();
 }


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