From b32c9e0df9ea402bcae1fb23f56467311aa8d66e Mon Sep 17 00:00:00 2001 From: Robert McQueen Date: Tue, 18 Sep 2018 15:46:24 +0100 Subject: [PATCH] OstreeMutableTree: add _remove method There is no API method to remove a file or subdirectory from a MutableTree besides directly manipulating the GHashTable returned by _get_files or _get_subdirs. This isn't possible from an introspection binding that transforms the returned GHashTable, and may also leave the tree checksum in an invalid state. Introduce a new method so that removing files or subdirectories is safe, and possible from bindings. Closes: #1724 Approved by: jlebon --- apidoc/ostree-sections.txt | 1 + src/libostree/libostree-devel.sym | 1 + src/libostree/ostree-mutable-tree.c | 45 +++++++++++++++++++++++++++++ src/libostree/ostree-mutable-tree.h | 6 ++++ 4 files changed, 53 insertions(+) diff --git a/apidoc/ostree-sections.txt b/apidoc/ostree-sections.txt index 70f93bab..0ec1cfd3 100644 --- a/apidoc/ostree-sections.txt +++ b/apidoc/ostree-sections.txt @@ -257,6 +257,7 @@ ostree_mutable_tree_get_metadata_checksum ostree_mutable_tree_set_contents_checksum ostree_mutable_tree_get_contents_checksum ostree_mutable_tree_replace_file +ostree_mutable_tree_remove ostree_mutable_tree_ensure_dir ostree_mutable_tree_lookup ostree_mutable_tree_ensure_parent_dirs diff --git a/src/libostree/libostree-devel.sym b/src/libostree/libostree-devel.sym index 0ba9a215..6d1fe934 100644 --- a/src/libostree/libostree-devel.sym +++ b/src/libostree/libostree-devel.sym @@ -19,6 +19,7 @@ /* Add new symbols here. Release commits should copy this section into -released.sym. */ LIBOSTREE_2018.9 { + ostree_mutable_tree_remove; ostree_repo_get_min_free_space_bytes; } LIBOSTREE_2018.7; diff --git a/src/libostree/ostree-mutable-tree.c b/src/libostree/ostree-mutable-tree.c index cd18927a..573f6260 100644 --- a/src/libostree/ostree-mutable-tree.c +++ b/src/libostree/ostree-mutable-tree.c @@ -332,6 +332,51 @@ ostree_mutable_tree_replace_file (OstreeMutableTree *self, return ret; } +/** + * ostree_mutable_tree_remove: + * @self: Tree + * @name: Name of file or subdirectory to remove + * @allow_noent: If @FALSE, an error will be thrown if @name does not exist in the tree + * @error: a #GError + * + * Remove the file or subdirectory named @name from the mutable tree @self. + */ +gboolean +ostree_mutable_tree_remove (OstreeMutableTree *self, + const char *name, + gboolean allow_noent, + GError **error) +{ + gboolean ret = FALSE; + + g_return_val_if_fail (name != NULL, FALSE); + + if (!ot_util_filename_validate (name, error)) + goto out; + + if (!_ostree_mutable_tree_make_whole (self, NULL, error)) + goto out; + + if (!g_hash_table_remove (self->files, name) && + !g_hash_table_remove (self->subdirs, name)) + { + if (allow_noent) + { + ret = TRUE; + goto out; + } + + set_error_noent (error, name); + goto out; + } + + invalidate_contents_checksum (self); + + ret = TRUE; + out: + return ret; +} + /** * ostree_mutable_tree_ensure_dir: * @self: Tree diff --git a/src/libostree/ostree-mutable-tree.h b/src/libostree/ostree-mutable-tree.h index 4b7f853e..753f96e7 100644 --- a/src/libostree/ostree-mutable-tree.h +++ b/src/libostree/ostree-mutable-tree.h @@ -77,6 +77,12 @@ gboolean ostree_mutable_tree_replace_file (OstreeMutableTree *self, const char *checksum, GError **error); +_OSTREE_PUBLIC +gboolean ostree_mutable_tree_remove (OstreeMutableTree *self, + const char *name, + gboolean allow_noent, + GError **error); + _OSTREE_PUBLIC gboolean ostree_mutable_tree_ensure_dir (OstreeMutableTree *self, const char *name,