OstreeMutableTree: add _remove method
authorRobert McQueen <rob@endlessm.com>
Tue, 18 Sep 2018 14:46:24 +0000 (15:46 +0100)
committerAtomic Bot <atomic-devel@projectatomic.io>
Thu, 20 Sep 2018 17:49:55 +0000 (17:49 +0000)
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
src/libostree/libostree-devel.sym
src/libostree/ostree-mutable-tree.c
src/libostree/ostree-mutable-tree.h

index 70f93babe9d44d8dc69f667a13d9bc7c7de1401b..0ec1cfd3bd5d59922dd56ea98eeefe6526f30392 100644 (file)
@@ -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
index 0ba9a21581e136f1f9c789ee69abc2ad96790141..6d1fe9346ffae218650dfc7dc556f4bf9523d9c5 100644 (file)
@@ -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;
 
index cd18927a6dc34e93ce38b243a9cfede72f94de18..573f6260652de91d7328a074dcdf7fcaf6e9c03f 100644 (file)
@@ -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
index 4b7f853e5da3e4c9478d187fd457aff2a53bb639..753f96e7f04b652da944104a101cfb9ff4b2d248 100644 (file)
@@ -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,