diff --git a/src/libotutil/ot-gio-utils.c b/src/libotutil/ot-gio-utils.c index 1c8c5dca..15dddc73 100644 --- a/src/libotutil/ot-gio-utils.c +++ b/src/libotutil/ot-gio-utils.c @@ -77,6 +77,91 @@ ot_gfile_ensure_directory (GFile *dir, return ret; } +GFile * +ot_gfile_get_child_strconcat (GFile *parent, + const char *first, + ...) +{ + va_list args; + GFile *ret; + GString *buf; + const char *arg; + + g_return_val_if_fail (first != NULL, NULL); + + va_start (args, first); + + buf = g_string_new (first); + + while ((arg = va_arg (args, const char *)) != NULL) + g_string_append (buf, arg); + + ret = g_file_get_child (parent, buf->str); + + g_string_free (buf, TRUE); + + return ret; +} + +/** + * ot_gfile_unlink: + * @path: Path to file + * @cancellable: a #GCancellable + * @error: a #GError + * + * Like g_file_delete(), except this function does not follow Unix + * symbolic links, and will delete a symbolic link even if it's + * pointing to a nonexistent file. In other words, this function + * merely wraps the raw Unix function unlink(). + * + * Returns: %TRUE on success, %FALSE on error + */ +gboolean +ot_gfile_unlink (GFile *path, + GCancellable *cancellable, + GError **error) +{ + if (g_cancellable_set_error_if_cancelled (cancellable, error)) + return FALSE; + + if (unlink (ot_gfile_get_path_cached (path)) < 0) + { + ot_util_set_error_from_errno (error, errno); + return FALSE; + } + return TRUE; +} + +/** + * ot_gfile_rename: + * @from: Current path + * @to: New path + * @cancellable: a #GCancellable + * @error: a #GError + * + * This function wraps the raw Unix function rename(). + * + * Returns: %TRUE on success, %FALSE on error + */ +gboolean +ot_gfile_rename (GFile *from, + GFile *to, + GCancellable *cancellable, + GError **error) +{ + if (g_cancellable_set_error_if_cancelled (cancellable, error)) + return FALSE; + + if (rename (ot_gfile_get_path_cached (from), + ot_gfile_get_path_cached (to)) < 0) + { + ot_util_set_error_from_errno (error, errno); + return FALSE; + } + return TRUE; + +} + gboolean ot_gfile_load_contents_utf8 (GFile *file, char **contents_out, diff --git a/src/libotutil/ot-gio-utils.h b/src/libotutil/ot-gio-utils.h index 46c8e3cf..5fda5da5 100644 --- a/src/libotutil/ot-gio-utils.h +++ b/src/libotutil/ot-gio-utils.h @@ -36,6 +36,8 @@ G_BEGIN_DECLS GFileType ot_gfile_type_for_mode (guint32 mode); +GFile *ot_gfile_get_child_strconcat (GFile *parent, const char *first, ...) G_GNUC_NULL_TERMINATED; + GFile *ot_gfile_new_for_path (const char *path); const char *ot_gfile_get_path_cached (GFile *file); @@ -44,6 +46,15 @@ const char *ot_gfile_get_basename_cached (GFile *file); gboolean ot_gfile_ensure_directory (GFile *dir, gboolean with_parents, GError **error); +gboolean ot_gfile_rename (GFile *src, + GFile *dest, + GCancellable *cancellable, + GError **error); + +gboolean ot_gfile_unlink (GFile *path, + GCancellable *cancellable, + GError **error); + gboolean ot_gfile_load_contents_utf8 (GFile *file, char **contents_out, char **etag_out,