[glib: 13/24] gfile: Add API to create a new temporary file asynchronously




commit fa243915297592bb09c3362571a8aed23190b110
Author: Marco Trevisan (Treviño) <mail 3v1n0 net>
Date:   Wed Jun 1 19:40:25 2022 +0200

    gfile: Add API to create a new temporary file asynchronously
    
    Make possible to create a new gfile with a temporary name in async
    way, using the same API of g_file_new_tmp().

 docs/reference/gio/gio-sections-common.txt |   2 +
 gio/gfile.c                                | 144 +++++++++++++++++++++++++++++
 gio/gfile.h                                |  10 ++
 gio/tests/file.c                           | 101 ++++++++++++++++++++
 4 files changed, 257 insertions(+)
---
diff --git a/docs/reference/gio/gio-sections-common.txt b/docs/reference/gio/gio-sections-common.txt
index b7c9e933c6..7d81f159af 100644
--- a/docs/reference/gio/gio-sections-common.txt
+++ b/docs/reference/gio/gio-sections-common.txt
@@ -84,6 +84,8 @@ g_file_new_for_uri
 g_file_new_for_commandline_arg
 g_file_new_for_commandline_arg_and_cwd
 g_file_new_tmp
+g_file_new_tmp_async
+g_file_new_tmp_finish
 g_file_parse_name
 g_file_new_build_filename
 g_file_dup
diff --git a/gio/gfile.c b/gio/gfile.c
index 52113a91c3..e08df28533 100644
--- a/gio/gfile.c
+++ b/gio/gfile.c
@@ -96,6 +96,7 @@
  * - g_file_new_for_uri() if you have a URI.
  * - g_file_new_for_commandline_arg() for a command line argument.
  * - g_file_new_tmp() to create a temporary file from a template.
+ * - g_file_new_tmp_async() to asynchronously create a temporary file.
  * - g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().
  * - g_file_new_build_filename() to create a file from path elements.
  *
@@ -4232,6 +4233,8 @@ g_file_make_symbolic_link_async (GFile               *file,
   GFileIface *iface;
 
   g_return_if_fail (G_IS_FILE (file));
+  g_return_if_fail (symlink_value != NULL);
+  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
 
   iface = G_FILE_GET_IFACE (file);
 
@@ -6942,6 +6945,147 @@ g_file_new_tmp (const char     *tmpl,
   return file;
 }
 
+typedef struct {
+  GFile *file;
+  GFileIOStream *iostream;
+} NewTmpAsyncData;
+
+static void
+new_tmp_data_free (NewTmpAsyncData *data)
+{
+  g_clear_object (&data->file);
+  g_clear_object (&data->iostream);
+  g_free (data);
+}
+
+static void
+new_tmp_async_thread (GTask         *task,
+                      gpointer       object,
+                      gpointer       task_data,
+                      GCancellable  *cancellable)
+{
+  GFile *file;
+  const char *tmpl = task_data;
+  GFileIOStream *iostream = NULL;
+  GError *error = NULL;
+  NewTmpAsyncData *return_data;
+
+  if (g_task_return_error_if_cancelled (task))
+    return;
+
+  file = g_file_new_tmp (tmpl, &iostream, &error);
+
+  if (!file)
+    {
+      int error_code = G_IO_ERROR_FAILED;
+
+      if (error->domain == G_IO_ERROR)
+        {
+          g_task_return_error (task, g_steal_pointer (&error));
+          return;
+        }
+
+      if (error->domain == G_FILE_ERROR)
+        error_code = g_io_error_from_file_error (error->code);
+
+      g_task_return_new_error (task, G_IO_ERROR, error_code,
+                               _("Failed to create a temporary directory for "
+                                 "template “%s”: %s"),
+                               tmpl, error->message);
+
+      g_clear_error (&error);
+      return;
+    }
+
+  return_data = g_new0 (NewTmpAsyncData, 1);
+  return_data->file = g_steal_pointer (&file);
+  return_data->iostream = g_steal_pointer (&iostream);
+
+  g_task_return_pointer (task, g_steal_pointer (&return_data),
+                         (GDestroyNotify) new_tmp_data_free);
+}
+
+/**
+ * g_file_new_tmp_async:
+ * @tmpl: (type filename) (nullable): Template for the file
+ *   name, as in g_file_open_tmp(), or %NULL for a default template
+ * @io_priority: the [I/O priority][io-priority] of the request
+ * @cancellable: optional #GCancellable object, %NULL to ignore
+ * @callback: (nullable): a #GAsyncReadyCallback to call when the request is done
+ * @user_data: (nullable): data to pass to @callback
+ *
+ * Asynchronously opens a file in the preferred directory for temporary files
+ *  (as returned by g_get_tmp_dir()) as g_file_new_tmp().
+ *
+ * @tmpl should be a string in the GLib file name encoding
+ * containing a sequence of six 'X' characters, and containing no
+ * directory components. If it is %NULL, a default template is used.
+ *
+ * Since: 2.74
+ */
+void
+g_file_new_tmp_async (const char          *tmpl,
+                      int                  io_priority,
+                      GCancellable        *cancellable,
+                      GAsyncReadyCallback  callback,
+                      gpointer             user_data)
+{
+  GTask *task;
+
+  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
+
+  task = g_task_new (NULL, cancellable, callback, user_data);
+  g_task_set_source_tag (task, g_file_new_tmp_async);
+  g_task_set_task_data (task, g_strdup (tmpl), g_free);
+  g_task_set_priority (task, io_priority);
+  g_task_set_check_cancellable (task, TRUE);
+  g_task_run_in_thread (task, new_tmp_async_thread);
+  g_object_unref (task);
+}
+
+/**
+ * g_file_new_tmp_finish:
+ * @result: a #GAsyncResult
+ * @iostream: (out) (not optional) (not nullable) (transfer full): on return, a #GFileIOStream for the 
created file
+ * @error: a #GError, or %NULL
+ *
+ * Finishes a temporary file creation started by g_file_new_tmp_async().
+ *
+ * Returns: (transfer full): a new #GFile.
+ *   Free the returned object with g_object_unref().
+ *
+ * Since: 2.74
+ */
+GFile *
+g_file_new_tmp_finish (GAsyncResult   *result,
+                       GFileIOStream **iostream,
+                       GError        **error)
+{
+  GFile *file;
+  NewTmpAsyncData *data;
+
+  g_return_val_if_fail (g_task_is_valid (result, NULL), NULL);
+  g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) ==
+                        g_file_new_tmp_async, NULL);
+  g_return_val_if_fail (iostream != NULL, NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  data = g_task_propagate_pointer (G_TASK (result), error);
+
+  if (!data)
+    {
+      *iostream = NULL;
+      return NULL;
+    }
+
+  file = g_steal_pointer (&data->file);
+  *iostream = g_steal_pointer (&data->iostream);
+
+  new_tmp_data_free (data);
+
+  return file;
+}
+
 /**
  * g_file_parse_name:
  * @parse_name: a file name or path to be parsed
diff --git a/gio/gfile.h b/gio/gfile.h
index 4e95c16e47..ef76d94540 100644
--- a/gio/gfile.h
+++ b/gio/gfile.h
@@ -626,6 +626,16 @@ GLIB_AVAILABLE_IN_2_32
 GFile *                 g_file_new_tmp                    (const char                 *tmpl,
                                                            GFileIOStream             **iostream,
                                                            GError                    **error);
+GLIB_AVAILABLE_IN_2_74
+void                    g_file_new_tmp_async              (const char                 *tmpl,
+                                                           int                         io_priority,
+                                                           GCancellable               *cancellable,
+                                                           GAsyncReadyCallback         callback,
+                                                           gpointer                    user_data);
+GLIB_AVAILABLE_IN_2_74
+GFile *                 g_file_new_tmp_finish             (GAsyncResult               *result,
+                                                           GFileIOStream             **iostream,
+                                                           GError                    **error);
 GLIB_AVAILABLE_IN_ALL
 GFile *                 g_file_parse_name                 (const char                 *parse_name);
 GLIB_AVAILABLE_IN_2_56
diff --git a/gio/tests/file.c b/gio/tests/file.c
index ab3bde2a80..919eb60d9f 100644
--- a/gio/tests/file.c
+++ b/gio/tests/file.c
@@ -1976,6 +1976,106 @@ test_replace (gconstpointer test_data)
 #endif
 }
 
+static void
+on_new_tmp_done (GObject      *object,
+                 GAsyncResult *result,
+                 gpointer      user_data)
+{
+  GFile *file;
+  GFile *parent;
+  GFileInfo *info;
+  GFileIOStream *iostream;
+  GError *error = NULL;
+  GMainLoop *loop = user_data;
+  gchar *basename;
+  gchar *parent_path;
+
+  g_assert_null (object);
+
+  file = g_file_new_tmp_finish (result, &iostream, &error);
+  g_assert_no_error (error);
+
+  g_assert_true (g_file_query_exists (file, NULL));
+
+  basename = g_file_get_basename (file);
+  g_assert_true (g_str_has_prefix (basename, "g_file_new_tmp_async_"));
+
+  info = g_file_io_stream_query_info (iostream, G_FILE_ATTRIBUTE_STANDARD_TYPE,
+                                      NULL, &error);
+  g_assert_no_error (error);
+
+  g_assert_cmpuint (g_file_info_get_file_type (info), ==, G_FILE_TYPE_REGULAR);
+  g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
+  g_assert_no_error (error);
+
+  parent = g_file_get_parent (file);
+  parent_path = g_file_get_path (parent);
+
+  g_assert_cmpstr (g_get_tmp_dir (), ==, parent_path);
+
+  g_main_loop_quit (loop);
+
+  g_object_unref (file);
+  g_object_unref (parent);
+  g_object_unref (iostream);
+  g_object_unref (info);
+  g_free (basename);
+  g_free (parent_path);
+}
+
+static void
+on_new_tmp_error (GObject      *object,
+                  GAsyncResult *result,
+                  gpointer      user_data)
+{
+  GFileIOStream *iostream = (GFileIOStream*) &on_new_tmp_error;
+  AsyncErrorData *error_data = user_data;
+
+  g_assert_null (object);
+
+  g_assert_null (g_file_new_tmp_finish (result, &iostream, error_data->error));
+  g_assert_nonnull (error_data->error);
+  g_assert_null (iostream);
+
+  g_main_loop_quit (error_data->loop);
+}
+
+static void
+test_async_new_tmp (void)
+{
+  GMainLoop *loop;
+  GError *error = NULL;
+  GCancellable *cancellable;
+  AsyncErrorData error_data = { .error = &error };
+
+  loop = g_main_loop_new (NULL, TRUE);
+  error_data.loop = loop;
+
+  g_file_new_tmp_async ("g_file_new_tmp_async_XXXXXX",
+                        G_PRIORITY_DEFAULT, NULL,
+                        on_new_tmp_done, loop);
+  g_main_loop_run (loop);
+
+  g_file_new_tmp_async ("g_file_new_tmp_async_invalid_template",
+                        G_PRIORITY_DEFAULT, NULL,
+                        on_new_tmp_error, &error_data);
+  g_main_loop_run (loop);
+  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
+  g_clear_error (&error);
+
+  cancellable = g_cancellable_new ();
+  g_file_new_tmp_async ("g_file_new_tmp_async_cancelled_XXXXXX",
+                        G_PRIORITY_DEFAULT, cancellable,
+                        on_new_tmp_error, &error_data);
+  g_cancellable_cancel (cancellable);
+  g_main_loop_run (loop);
+  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
+  g_clear_object (&cancellable);
+  g_clear_error (&error);
+
+  g_main_loop_unref (loop);
+}
+
 static void
 on_file_deleted (GObject      *object,
                 GAsyncResult *result,
@@ -3295,6 +3395,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/file/replace-symlink/using-etag", test_replace_symlink_using_etag);
   g_test_add_data_func ("/file/replace/write-only", GUINT_TO_POINTER (FALSE), test_replace);
   g_test_add_data_func ("/file/replace/read-write", GUINT_TO_POINTER (TRUE), test_replace);
+  g_test_add_func ("/file/async-new-tmp", test_async_new_tmp);
   g_test_add_func ("/file/async-delete", test_async_delete);
   g_test_add_func ("/file/async-make-symlink", test_async_make_symlink);
   g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);


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