From a7300a828db26749e79d2e290eb561698d84655a Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 14 Jan 2015 22:03:02 -0500 Subject: [PATCH] core: Add an API to parse a content file using dirfd relative lookup This will be used for a later change to use openat() for the fetching code. Note that we drop the code to use mmap() - it was an attempt to avoid keeping a fd open, but we do correctly close anyways. --- src/libostree/ostree-core.c | 106 ++++++++++++++++++++---------------- src/libostree/ostree-core.h | 10 ++++ 2 files changed, 69 insertions(+), 47 deletions(-) diff --git a/src/libostree/ostree-core.c b/src/libostree/ostree-core.c index 2d70e226..08e0ea22 100644 --- a/src/libostree/ostree-core.c +++ b/src/libostree/ostree-core.c @@ -573,6 +573,60 @@ ostree_content_stream_parse (gboolean compressed, return ret; } +/** + * ostree_content_file_parse_at: + * @compressed: Whether or not the stream is zlib-compressed + * @parent_dfd: Directory file descriptor + * @path: Subpath + * @trusted: If %TRUE, assume the content has been validated + * @out_input: (out): The raw file content stream + * @out_file_info: (out): Normal metadata + * @out_xattrs: (out): Extended attributes + * @cancellable: Cancellable + * @error: Error + * + * A thin wrapper for ostree_content_stream_parse(); this function + * converts an object content stream back into components. + */ +gboolean +ostree_content_file_parse_at (gboolean compressed, + int parent_dfd, + const char *path, + gboolean trusted, + GInputStream **out_input, + GFileInfo **out_file_info, + GVariant **out_xattrs, + GCancellable *cancellable, + GError **error) +{ + gboolean ret = FALSE; + struct stat stbuf; + gs_unref_object GInputStream *file_input = NULL; + gs_unref_object GInputStream *ret_input = NULL; + gs_unref_object GFileInfo *ret_file_info = NULL; + gs_unref_variant GVariant *ret_xattrs = NULL; + + if (!ot_openat_read_stream (parent_dfd, path, TRUE, &file_input, + cancellable, error)) + goto out; + + if (!gs_stream_fstat ((GFileDescriptorBased*)file_input, &stbuf, cancellable, error)) + goto out; + + if (!ostree_content_stream_parse (compressed, file_input, stbuf.st_size, trusted, + out_input ? &ret_input : NULL, + &ret_file_info, &ret_xattrs, + cancellable, error)) + goto out; + + ret = TRUE; + ot_transfer_out_value (out_input, &ret_input); + ot_transfer_out_value (out_file_info, &ret_file_info); + ot_transfer_out_value (out_xattrs, &ret_xattrs); + out: + return ret; +} + /** * ostree_content_file_parse: * @compressed: Whether or not the stream is zlib-compressed @@ -597,53 +651,11 @@ ostree_content_file_parse (gboolean compressed, GCancellable *cancellable, GError **error) { - gboolean ret = FALSE; - guint64 length; - struct stat stbuf; - gs_unref_object GInputStream *file_input = NULL; - gs_unref_object GInputStream *ret_input = NULL; - gs_unref_object GFileInfo *ret_file_info = NULL; - gs_unref_variant GVariant *ret_xattrs = NULL; - - if (out_input) - { - file_input = (GInputStream*)gs_file_read_noatime (content_path, cancellable, error); - if (!file_input) - goto out; - - if (!gs_stream_fstat ((GFileDescriptorBased*)file_input, &stbuf, cancellable, error)) - goto out; - - length = stbuf.st_size; - } - else - { - GMappedFile *mmaped; - GBytes *bytes; - - mmaped = gs_file_map_noatime (content_path, cancellable, error); - if (!mmaped) - goto out; - bytes = g_mapped_file_get_bytes (mmaped); - g_mapped_file_unref (mmaped); - mmaped = NULL; - file_input = g_memory_input_stream_new_from_bytes (bytes); - length = g_bytes_get_size (bytes); - g_bytes_unref (bytes); - } - - if (!ostree_content_stream_parse (compressed, file_input, length, trusted, - out_input ? &ret_input : NULL, - &ret_file_info, &ret_xattrs, - cancellable, error)) - goto out; - - ret = TRUE; - ot_transfer_out_value (out_input, &ret_input); - ot_transfer_out_value (out_file_info, &ret_file_info); - ot_transfer_out_value (out_xattrs, &ret_xattrs); - out: - return ret; + return ostree_content_file_parse_at (compressed, AT_FDCWD, + gs_file_get_path_cached (content_path), + trusted, + out_input, out_file_info, out_xattrs, + cancellable, error); } /** diff --git a/src/libostree/ostree-core.h b/src/libostree/ostree-core.h index c5c940e6..b8b0f586 100644 --- a/src/libostree/ostree-core.h +++ b/src/libostree/ostree-core.h @@ -229,6 +229,16 @@ gboolean ostree_content_file_parse (gboolean compressed, GCancellable *cancellable, GError **error); +gboolean ostree_content_file_parse_at (gboolean compressed, + int parent_dfd, + const char *path, + gboolean trusted, + GInputStream **out_input, + GFileInfo **out_file_info, + GVariant **out_xattrs, + GCancellable *cancellable, + GError **error); + gboolean ostree_raw_file_to_content_stream (GInputStream *input, GFileInfo *file_info, GVariant *xattrs,