From 317a09e2d47257df5e0972c85f14c2a6ffdbbfd2 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Thu, 24 May 2012 17:15:43 -0700 Subject: [PATCH] libsparse: add sparse_file_len Add sparse_file_len, which will compute the size of data that would be produced if sparse_file_write was called. Useful combined with sparse_file_callback. Change-Id: I1a156d1071760f5559483954a5c62ffc20298703 --- libsparse/include/sparse/sparse.h | 14 ++++++++++++++ libsparse/sparse.c | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/libsparse/include/sparse/sparse.h b/libsparse/include/sparse/sparse.h index fe003f61b..17d085ca4 100644 --- a/libsparse/include/sparse/sparse.h +++ b/libsparse/include/sparse/sparse.h @@ -157,6 +157,20 @@ int sparse_file_add_fd(struct sparse_file *s, int sparse_file_write(struct sparse_file *s, int fd, bool gz, bool sparse, bool crc); +/** + * sparse_file_len - return the length of a sparse file if written to disk + * + * @s - sparse file cookie + * @sparse - write in the Android sparse file format + * @crc - append a crc chunk + * + * Returns the size a sparse file would be on disk if it were written in the + * specified format. If sparse is true, this is the size of the data in the + * sparse format. If sparse is false, this is the size of the normal + * non-sparse file. + */ +int64_t sparse_file_len(struct sparse_file *s, bool sparse, bool crc); + /** * sparse_file_callback - call a callback for blocks in sparse file * diff --git a/libsparse/sparse.c b/libsparse/sparse.c index 77f02fc49..f04f687b9 100644 --- a/libsparse/sparse.c +++ b/libsparse/sparse.c @@ -196,6 +196,30 @@ static int out_counter_write(void *priv, const void *data, int len) return 0; } +int64_t sparse_file_len(struct sparse_file *s, bool sparse, bool crc) +{ + int ret; + int chunks = sparse_count_chunks(s); + int64_t count = 0; + struct output_file *out; + + out = open_output_callback(out_counter_write, &count, + s->block_size, s->len, false, sparse, chunks, crc); + if (!out) { + return -1; + } + + ret = write_all_blocks(s, out); + + close_output_file(out); + + if (ret < 0) { + return -1; + } + + return count; +} + static struct backed_block *move_chunks_up_to_len(struct sparse_file *from, struct sparse_file *to, unsigned int len) {