summaryrefslogtreecommitdiff
path: root/src/libostree/ostree-repo-libarchive.c
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2016-02-18 16:49:30 -0500
committerColin Walters <walters@verbum.org>2016-02-19 08:54:04 -0500
commit3a555114bc22f599d72d95d71c2579fd2345cf6f (patch)
treea88ed0c064d01036d4d7da39d8aeb6a59bbd4960 /src/libostree/ostree-repo-libarchive.c
parentb08b1abccdb6f8614750b31f2e3dc572e7b7bc8a (diff)
downloadostree-3a555114bc22f599d72d95d71c2579fd2345cf6f.tar.gz
repo: Add ostree_repo_import_archive_to_mtree
This is a more flexible version of the previous ostree_repo_write_archive_to_mtree() which took a file reference. This has an extensible options structure, and in particular now supports `ignore_unsupported_content`. I plan to use this for importing Docker images which contain device nodes. (There's no reason for container images to have those, so we'll just ignore them). Also here, just like the export variant, the caller is responsible for setting up libarchive.
Diffstat (limited to 'src/libostree/ostree-repo-libarchive.c')
-rw-r--r--src/libostree/ostree-repo-libarchive.c141
1 files changed, 102 insertions, 39 deletions
diff --git a/src/libostree/ostree-repo-libarchive.c b/src/libostree/ostree-repo-libarchive.c
index 19fae757..64410cb2 100644
--- a/src/libostree/ostree-repo-libarchive.c
+++ b/src/libostree/ostree-repo-libarchive.c
@@ -78,6 +78,7 @@ file_info_from_archive_entry_and_modifier (OstreeRepo *repo,
static gboolean
import_libarchive_entry_file (OstreeRepo *self,
+ OstreeRepoImportArchiveOptions *opts,
struct archive *a,
struct archive_entry *entry,
GFileInfo *file_info,
@@ -93,8 +94,27 @@ import_libarchive_entry_file (OstreeRepo *self,
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return FALSE;
- if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_REGULAR)
- archive_stream = _ostree_libarchive_input_stream_new (a);
+ switch (g_file_info_get_file_type (file_info))
+ {
+ case G_FILE_TYPE_REGULAR:
+ archive_stream = _ostree_libarchive_input_stream_new (a);
+ break;
+ case G_FILE_TYPE_SYMBOLIC_LINK:
+ break;
+ default:
+ if (opts->ignore_unsupported_content)
+ {
+ ret = TRUE;
+ goto out;
+ }
+ else
+ {
+ g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+ "Unable to import non-regular/non-symlink file '%s'",
+ archive_entry_pathname (entry));
+ goto out;
+ }
+ }
if (!ostree_raw_file_to_content_stream (archive_stream, file_info, NULL,
&file_object_input, &length, cancellable, error))
@@ -111,6 +131,7 @@ import_libarchive_entry_file (OstreeRepo *self,
static gboolean
write_libarchive_entry_to_mtree (OstreeRepo *self,
+ OstreeRepoImportArchiveOptions *opts,
OstreeMutableTree *root,
struct archive *a,
struct archive_entry *entry,
@@ -249,24 +270,92 @@ write_libarchive_entry_to_mtree (OstreeRepo *self,
goto out;
}
- if (!import_libarchive_entry_file (self, a, entry, file_info, &tmp_csum,
+ if (!import_libarchive_entry_file (self, opts, a, entry, file_info, &tmp_csum,
cancellable, error))
goto out;
+
+ if (tmp_csum)
+ {
+ g_free (tmp_checksum);
+ tmp_checksum = ostree_checksum_from_bytes (tmp_csum);
+ if (!ostree_mutable_tree_replace_file (parent, basename,
+ tmp_checksum,
+ error))
+ goto out;
+ }
+ }
+ }
+
+ ret = TRUE;
+ out:
+ return ret;
+}
+#endif
+
+/**
+ * ostree_repo_import_archive_to_mtree:
+ * @self: An #OstreeRepo
+ * @opts: Options structure, ensure this is zeroed, then set specific variables
+ * @archive: Really this is "struct archive*"
+ * @mtree: The #OstreeMutableTree to write to
+ * @modifier: (allow-none): Optional commit modifier
+ * @cancellable: Cancellable
+ * @error: Error
+ *
+ * Import an archive file @archive into the repository, and write its
+ * file structure to @mtree.
+ */
+gboolean
+ostree_repo_import_archive_to_mtree (OstreeRepo *self,
+ OstreeRepoImportArchiveOptions *opts,
+ void *archive,
+ OstreeMutableTree *mtree,
+ OstreeRepoCommitModifier *modifier,
+ GCancellable *cancellable,
+ GError **error)
+{
+ gboolean ret = FALSE;
+ struct archive *a = archive;
+ struct archive_entry *entry;
+ g_autofree guchar *tmp_csum = NULL;
+ int r;
+
+ while (TRUE)
+ {
+ r = archive_read_next_header (a, &entry);
+ if (r == ARCHIVE_EOF)
+ break;
+ else if (r != ARCHIVE_OK)
+ {
+ propagate_libarchive_error (error, a);
+ goto out;
+ }
+
+ /* TODO - refactor this to only create the metadata on demand
+ * (i.e. if there is a missing parent dir)
+ */
+ if (opts->autocreate_parents && !tmp_csum)
+ {
+ g_autoptr(GFileInfo) tmp_dir_info = g_file_info_new ();
- g_free (tmp_checksum);
- tmp_checksum = ostree_checksum_from_bytes (tmp_csum);
- if (!ostree_mutable_tree_replace_file (parent, basename,
- tmp_checksum,
- error))
+ g_file_info_set_attribute_uint32 (tmp_dir_info, "unix::uid", archive_entry_uid (entry));
+ g_file_info_set_attribute_uint32 (tmp_dir_info, "unix::gid", archive_entry_gid (entry));
+ g_file_info_set_attribute_uint32 (tmp_dir_info, "unix::mode", 0755 | S_IFDIR);
+
+ if (!_ostree_repo_write_directory_meta (self, tmp_dir_info, NULL, &tmp_csum, cancellable, error))
goto out;
}
+
+ if (!write_libarchive_entry_to_mtree (self, opts, mtree, a,
+ entry, modifier, tmp_csum,
+ cancellable, error))
+ goto out;
}
ret = TRUE;
out:
return ret;
}
-#endif
/**
* ostree_repo_write_archive_to_mtree:
@@ -293,10 +382,8 @@ ostree_repo_write_archive_to_mtree (OstreeRepo *self,
#ifdef HAVE_LIBARCHIVE
gboolean ret = FALSE;
struct archive *a = NULL;
- struct archive_entry *entry;
- int r;
g_autoptr(GFileInfo) tmp_dir_info = NULL;
- g_autofree guchar *tmp_csum = NULL;
+ OstreeRepoImportArchiveOptions opts = { 0, };
a = archive_read_new ();
#ifdef HAVE_ARCHIVE_READ_SUPPORT_FILTER_ALL
@@ -311,35 +398,11 @@ ostree_repo_write_archive_to_mtree (OstreeRepo *self,
goto out;
}
- while (TRUE)
- {
- r = archive_read_next_header (a, &entry);
- if (r == ARCHIVE_EOF)
- break;
- else if (r != ARCHIVE_OK)
- {
- propagate_libarchive_error (error, a);
- goto out;
- }
+ opts.autocreate_parents = !!autocreate_parents;
- if (autocreate_parents && !tmp_csum)
- {
- tmp_dir_info = g_file_info_new ();
-
- g_file_info_set_attribute_uint32 (tmp_dir_info, "unix::uid", archive_entry_uid (entry));
- g_file_info_set_attribute_uint32 (tmp_dir_info, "unix::gid", archive_entry_gid (entry));
- g_file_info_set_attribute_uint32 (tmp_dir_info, "unix::mode", 0755 | S_IFDIR);
-
- if (!_ostree_repo_write_directory_meta (self, tmp_dir_info, NULL, &tmp_csum, cancellable, error))
- goto out;
- }
+ if (!ostree_repo_import_archive_to_mtree (self, &opts, a, mtree, modifier, cancellable, error))
+ goto out;
- if (!write_libarchive_entry_to_mtree (self, mtree, a,
- entry, modifier,
- autocreate_parents ? tmp_csum : NULL,
- cancellable, error))
- goto out;
- }
if (archive_read_close (a) != ARCHIVE_OK)
{
propagate_libarchive_error (error, a);