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.
This commit is contained in:
Marco Trevisan (Treviño)
2022-06-01 20:32:36 +02:00
parent fa24391529
commit 22e951e6ac
4 changed files with 212 additions and 0 deletions

View File

@@ -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