[glib: 14/24] gfile: Add Async API to create a temporary directory and return as GFile
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib: 14/24] gfile: Add Async API to create a temporary directory and return as GFile
- Date: Thu, 23 Jun 2022 11:49:59 +0000 (UTC)
commit 22e951e6ac9cf7464354d1a11c229b9c122603ea
Author: Marco Trevisan (Treviño) <mail 3v1n0 net>
Date: Wed Jun 1 20:32:36 2022 +0200
gfile: Add Async API to create a temporary directory and return as GFile
While it's possible to create a directory synchronously via
g_dir_make_tmp(), there's no such API that performs it asynchronously.
So implement it using GFile, using a thread to perform such task.
docs/reference/gio/gio-sections-common.txt | 2 +
gio/gfile.c | 106 +++++++++++++++++++++++++++++
gio/gfile.h | 9 +++
gio/tests/file.c | 95 ++++++++++++++++++++++++++
4 files changed, 212 insertions(+)
---
diff --git a/docs/reference/gio/gio-sections-common.txt b/docs/reference/gio/gio-sections-common.txt
index 7d81f159af..5f45a66f42 100644
--- a/docs/reference/gio/gio-sections-common.txt
+++ b/docs/reference/gio/gio-sections-common.txt
@@ -86,6 +86,8 @@ g_file_new_for_commandline_arg_and_cwd
g_file_new_tmp
g_file_new_tmp_async
g_file_new_tmp_finish
+g_file_new_tmp_dir_async
+g_file_new_tmp_dir_finish
g_file_parse_name
g_file_new_build_filename
g_file_dup
diff --git a/gio/gfile.c b/gio/gfile.c
index e08df28533..a4d6d11cf5 100644
--- a/gio/gfile.c
+++ b/gio/gfile.c
@@ -97,6 +97,7 @@
* - 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_new_tmp_dir_async() to asynchronously create a temporary directory.
* - 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.
*
@@ -7086,6 +7087,111 @@ g_file_new_tmp_finish (GAsyncResult *result,
return file;
}
+static void
+new_tmp_dir_async_thread (GTask *task,
+ gpointer object,
+ gpointer task_data,
+ GCancellable *cancellable)
+{
+ gchar *path;
+ const char *tmpl = task_data;
+ GError *error = NULL;
+
+ if (g_task_return_error_if_cancelled (task))
+ return;
+
+ path = g_dir_make_tmp (tmpl, &error);
+
+ if (!path)
+ {
+ 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;
+ }
+
+ g_task_return_pointer (task, g_file_new_for_path (path), g_object_unref);
+
+ g_free (path);
+}
+
+/**
+ * g_file_new_tmp_dir_async:
+ * @tmpl: (type filename) (nullable): Template for the file
+ * name, as in g_dir_make_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 creates a directory in the preferred directory for
+ * temporary files (as returned by g_get_tmp_dir()) as g_dir_make_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_dir_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_dir_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_dir_async_thread);
+ g_object_unref (task);
+}
+
+/**
+ * g_file_new_tmp_dir_finish:
+ * @result: a #GAsyncResult
+ * @error: a #GError, or %NULL
+ *
+ * Finishes a temporary directory creation started by
+ * g_file_new_tmp_dir_async().
+ *
+ * Returns: (transfer full): a new #GFile.
+ * Free the returned object with g_object_unref().
+ *
+ * Since: 2.74
+ */
+GFile *
+g_file_new_tmp_dir_finish (GAsyncResult *result,
+ GError **error)
+{
+ 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_dir_async, NULL);
+ g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+ return g_task_propagate_pointer (G_TASK (result), error);
+}
+
/**
* 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 ef76d94540..13ffed655e 100644
--- a/gio/gfile.h
+++ b/gio/gfile.h
@@ -636,6 +636,15 @@ GLIB_AVAILABLE_IN_2_74
GFile * g_file_new_tmp_finish (GAsyncResult *result,
GFileIOStream **iostream,
GError **error);
+GLIB_AVAILABLE_IN_2_74
+void g_file_new_tmp_dir_async (const char *tmpl,
+ int io_priority,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+GLIB_AVAILABLE_IN_2_74
+GFile * g_file_new_tmp_dir_finish (GAsyncResult *result,
+ 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 919eb60d9f..c43f66d612 100644
--- a/gio/tests/file.c
+++ b/gio/tests/file.c
@@ -2076,6 +2076,100 @@ test_async_new_tmp (void)
g_main_loop_unref (loop);
}
+static void
+on_new_tmp_dir_done (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GFile *file;
+ GFile *parent;
+ GFileInfo *info;
+ GError *error = NULL;
+ GMainLoop *loop = user_data;
+ gchar *basename;
+ gchar *parent_path;
+
+ g_assert_null (object);
+
+ file = g_file_new_tmp_dir_finish (result, &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_dir_async_"));
+
+ info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
+ G_FILE_QUERY_INFO_NONE, NULL, &error);
+ g_assert_no_error (error);
+
+ g_assert_cmpuint (g_file_info_get_file_type (info), ==, G_FILE_TYPE_DIRECTORY);
+
+ 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 (info);
+ g_free (basename);
+ g_free (parent_path);
+}
+
+static void
+on_new_tmp_dir_error (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ AsyncErrorData *error_data = user_data;
+
+ g_assert_null (object);
+
+ g_assert_null (g_file_new_tmp_dir_finish (result, error_data->error));
+ g_assert_nonnull (error_data->error);
+
+ g_main_loop_quit (error_data->loop);
+}
+
+static void
+test_async_new_tmp_dir (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_dir_async ("g_file_new_tmp_dir_async_XXXXXX",
+ G_PRIORITY_DEFAULT, NULL,
+ on_new_tmp_dir_done, loop);
+ g_main_loop_run (loop);
+
+ g_file_new_tmp_dir_async ("g_file_new_tmp_dir_async",
+ G_PRIORITY_DEFAULT, NULL,
+ on_new_tmp_dir_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_dir_async ("g_file_new_tmp_dir_async_cancelled_XXXXXX",
+ G_PRIORITY_DEFAULT, cancellable,
+ on_new_tmp_dir_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,
@@ -3396,6 +3490,7 @@ main (int argc, char *argv[])
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-new-tmp-dir", test_async_new_tmp_dir);
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]