From 50e6029a4e4b9c9bd6065548ced523ec5b01e705 Mon Sep 17 00:00:00 2001 From: Jerry Zhang Date: Tue, 5 Jun 2018 11:44:52 -0700 Subject: libsparse: Add method to create sparse file from buffer Refactor elements of sparse file parsing that depend on an fd into SparseFileSource class, then create implementations using both fd and buffer. Add sparse_file_read_buf which reads the given buffer into a sparse file cookie without copying. Test: flash system with sparse images Bug: 78793464 Change-Id: Ice6c8e1ff075d6867e070f80fcf5aa4f530a1b95 --- libsparse/include/sparse/sparse.h | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'libsparse/include') diff --git a/libsparse/include/sparse/sparse.h b/libsparse/include/sparse/sparse.h index 1b91ead5c..3d5fb0c53 100644 --- a/libsparse/include/sparse/sparse.h +++ b/libsparse/include/sparse/sparse.h @@ -246,9 +246,24 @@ int sparse_file_foreach_chunk(struct sparse_file *s, bool sparse, bool crc, int sparse_file_read(struct sparse_file *s, int fd, bool sparse, bool crc); /** - * sparse_file_import - import an existing sparse file + * sparse_file_read_buf - read a buffer into a sparse file cookie * * @s - sparse file cookie + * @buf - buffer to read from + * @crc - verify the crc of a file in the Android sparse file format + * + * Reads a buffer into a sparse file cookie. The buffer must remain + * valid until the sparse file cookie is freed. If crc is true, the + * crc of the sparse file will be verified. + * + * Returns 0 on success, negative errno on error. + */ +int sparse_file_read_buf(struct sparse_file *s, char *buf, bool crc); + +/** + * sparse_file_import - import an existing sparse file + * + * @fd - file descriptor to read from * @verbose - print verbose errors while reading the sparse file * @crc - verify the crc of a file in the Android sparse file format * @@ -260,6 +275,21 @@ int sparse_file_read(struct sparse_file *s, int fd, bool sparse, bool crc); */ struct sparse_file *sparse_file_import(int fd, bool verbose, bool crc); +/** + * sparse_file_import_buf - import an existing sparse file from a buffer + * + * @buf - buffer to read from + * @verbose - print verbose errors while reading the sparse file + * @crc - verify the crc of a file in the Android sparse file format + * + * Reads existing sparse file data into a sparse file cookie, recreating the same + * sparse cookie that was used to write it. If verbose is true, prints verbose + * errors when the sparse file is formatted incorrectly. + * + * Returns a new sparse file cookie on success, NULL on error. + */ +struct sparse_file *sparse_file_import_buf(char* buf, bool verbose, bool crc); + /** * sparse_file_import_auto - import an existing sparse or normal file * -- cgit v1.2.3