128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross/*
228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Copyright (C) 2012 The Android Open Source Project
328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Licensed under the Apache License, Version 2.0 (the "License");
528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * you may not use this file except in compliance with the License.
628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * You may obtain a copy of the License at
728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
828fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *      http://www.apache.org/licenses/LICENSE-2.0
928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
1028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Unless required by applicable law or agreed to in writing, software
1128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * distributed under the License is distributed on an "AS IS" BASIS,
1228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * See the License for the specific language governing permissions and
1428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * limitations under the License.
1528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross */
1628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
1728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross#ifndef _LIBSPARSE_SPARSE_H_
1828fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross#define _LIBSPARSE_SPARSE_H_
1928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
2028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross#include <stdbool.h>
2128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross#include <stdint.h>
2228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
2328fa5bc347390480fe190294c6c385b6a9f0d68bColin Crossstruct sparse_file;
2428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
2528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross/**
2628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * sparse_file_new - create a new sparse file cookie
2728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
2828fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @block_size - minimum size of a chunk
2928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @len - size of the expanded sparse file.
3028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
3128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Creates a new sparse_file cookie that can be used to associate data
3228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * blocks.  Can later be written to a file with a variety of options.
3328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * block_size specifies the minimum size of a chunk in the file.  The maximum
3428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * size of the file is 2**32 * block_size (16TB for 4k block size).
3528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
3628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Returns the sparse file cookie, or NULL on error.
3728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross */
3828fa5bc347390480fe190294c6c385b6a9f0d68bColin Crossstruct sparse_file *sparse_file_new(unsigned int block_size, int64_t len);
3928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
4028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross/**
4128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * sparse_file_destroy - destroy a sparse file cookie
4228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
4328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @s - sparse file cookie
4428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
4528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Destroys a sparse file cookie.  After destroy, all memory passed in to
4628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * sparse_file_add_data can be freed by the caller
4728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross */
4828fa5bc347390480fe190294c6c385b6a9f0d68bColin Crossvoid sparse_file_destroy(struct sparse_file *s);
4928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
5028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross/**
5128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * sparse_file_add_data - associate a data chunk with a sparse file
5228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
5328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @s - sparse file cookie
5428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @data - pointer to data block
5528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @len - length of the data block
5628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @block - offset in blocks into the sparse file to place the data chunk
5728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
5828fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Associates a data chunk with a sparse file cookie.  The region
5928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * [block * block_size : block * block_size + len) must not already be used in
6028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * the sparse file. If len is not a multiple of the block size the data
6128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * will be padded with zeros.
6228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
6328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * The data pointer must remain valid until the sparse file is closed or the
6428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * data block is removed from the sparse file.
6528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
6628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Returns 0 on success, negative errno on error.
6728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross */
6828fa5bc347390480fe190294c6c385b6a9f0d68bColin Crossint sparse_file_add_data(struct sparse_file *s,
6928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross		void *data, unsigned int len, unsigned int block);
7028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
7128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross/**
7228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * sparse_file_add_fill - associate a fill chunk with a sparse file
7328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
7428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @s - sparse file cookie
7528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @fill_val - 32 bit fill data
7628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @len - length of the fill block
7728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @block - offset in blocks into the sparse file to place the fill chunk
7828fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
7928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Associates a chunk filled with fill_val with a sparse file cookie.
8028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * The region [block * block_size : block * block_size + len) must not already
8128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * be used in the sparse file. If len is not a multiple of the block size the
8228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * data will be padded with zeros.
8328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
8428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Returns 0 on success, negative errno on error.
8528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross */
8628fa5bc347390480fe190294c6c385b6a9f0d68bColin Crossint sparse_file_add_fill(struct sparse_file *s,
8728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross		uint32_t fill_val, unsigned int len, unsigned int block);
8828fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
8928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross/**
9028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * sparse_file_add_file - associate a chunk of a file with a sparse file
9128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
9228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @s - sparse file cookie
9328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @filename - filename of the file to be copied
9428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @file_offset - offset into the copied file
9528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @len - length of the copied block
9628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @block - offset in blocks into the sparse file to place the file chunk
9728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
9828fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Associates a chunk of an existing file with a sparse file cookie.
9928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * The region [block * block_size : block * block_size + len) must not already
10028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * be used in the sparse file. If len is not a multiple of the block size the
10128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * data will be padded with zeros.
10228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
10328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Allows adding large amounts of data to a sparse file without needing to keep
10428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * it all mapped.  File size is limited by available virtual address space,
10528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * exceptionally large files may need to be added in multiple chunks.
10628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
10728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Returns 0 on success, negative errno on error.
10828fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross */
10928fa5bc347390480fe190294c6c385b6a9f0d68bColin Crossint sparse_file_add_file(struct sparse_file *s,
11028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross		const char *filename, int64_t file_offset, unsigned int len,
11128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross		unsigned int block);
11228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
11328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross/**
1149e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * sparse_file_add_file - associate a chunk of a file with a sparse file
1159e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross *
1169e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * @s - sparse file cookie
1179e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * @filename - filename of the file to be copied
1189e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * @file_offset - offset into the copied file
1199e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * @len - length of the copied block
1209e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * @block - offset in blocks into the sparse file to place the file chunk
1219e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross *
1229e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * Associates a chunk of an existing fd with a sparse file cookie.
1239e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * The region [block * block_size : block * block_size + len) must not already
1249e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * be used in the sparse file. If len is not a multiple of the block size the
1259e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * data will be padded with zeros.
1269e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross *
1279e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * Allows adding large amounts of data to a sparse file without needing to keep
1289e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * it all mapped.  File size is limited by available virtual address space,
1299e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * exceptionally large files may need to be added in multiple chunks.
1309e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross *
1319e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * The fd must remain open until the sparse file is closed or the fd block is
1329e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * removed from the sparse file.
1339e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross *
1349e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross * Returns 0 on success, negative errno on error.
1359e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross */
1369e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Crossint sparse_file_add_fd(struct sparse_file *s,
1379e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross		int fd, int64_t file_offset, unsigned int len, unsigned int block);
1389e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross
1399e1f17e926fa20255c5f4b4d2f68aa98a964253aColin Cross/**
14028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * sparse_file_write - write a sparse file to a file
14128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
14228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @s - sparse file cookie
14328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @fd - file descriptor to write to
14428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @gz - write a gzipped file
14528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @sparse - write in the Android sparse file format
14628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * @crc - append a crc chunk
14728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
14828fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Writes a sparse file to a file.  If gz is true, the data will be passed
14928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * through zlib.  If sparse is true, the file will be written in the Android
15028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * sparse file format.  If sparse is false, the file will be written by seeking
15128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * over unused chunks, producing a smaller file if the filesystem supports
15228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * sparse files.  If crc is true, the crc of the expanded data will be
15328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * calculated and appended in a crc chunk.
15428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
15528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Returns 0 on success, negative errno on error.
15628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross */
15728fa5bc347390480fe190294c6c385b6a9f0d68bColin Crossint sparse_file_write(struct sparse_file *s, int fd, bool gz, bool sparse,
15828fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross		bool crc);
159317a09e2d47257df5e0972c85f14c2a6ffdbbfd2Colin Cross
160317a09e2d47257df5e0972c85f14c2a6ffdbbfd2Colin Cross/**
161317a09e2d47257df5e0972c85f14c2a6ffdbbfd2Colin Cross * sparse_file_len - return the length of a sparse file if written to disk
162317a09e2d47257df5e0972c85f14c2a6ffdbbfd2Colin Cross *
163317a09e2d47257df5e0972c85f14c2a6ffdbbfd2Colin Cross * @s - sparse file cookie
164317a09e2d47257df5e0972c85f14c2a6ffdbbfd2Colin Cross * @sparse - write in the Android sparse file format
165317a09e2d47257df5e0972c85f14c2a6ffdbbfd2Colin Cross * @crc - append a crc chunk
166317a09e2d47257df5e0972c85f14c2a6ffdbbfd2Colin Cross *
167317a09e2d47257df5e0972c85f14c2a6ffdbbfd2Colin Cross * Returns the size a sparse file would be on disk if it were written in the
168317a09e2d47257df5e0972c85f14c2a6ffdbbfd2Colin Cross * specified format.  If sparse is true, this is the size of the data in the
169317a09e2d47257df5e0972c85f14c2a6ffdbbfd2Colin Cross * sparse format.  If sparse is false, this is the size of the normal
170317a09e2d47257df5e0972c85f14c2a6ffdbbfd2Colin Cross * non-sparse file.
171317a09e2d47257df5e0972c85f14c2a6ffdbbfd2Colin Cross */
172317a09e2d47257df5e0972c85f14c2a6ffdbbfd2Colin Crossint64_t sparse_file_len(struct sparse_file *s, bool sparse, bool crc);
17328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
174a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross/**
1751e17b313a6257b7b5081e178e81435c09d60378eColin Cross * sparse_file_callback - call a callback for blocks in sparse file
1761e17b313a6257b7b5081e178e81435c09d60378eColin Cross *
1771e17b313a6257b7b5081e178e81435c09d60378eColin Cross * @s - sparse file cookie
1781e17b313a6257b7b5081e178e81435c09d60378eColin Cross * @sparse - write in the Android sparse file format
1791e17b313a6257b7b5081e178e81435c09d60378eColin Cross * @crc - append a crc chunk
1801e17b313a6257b7b5081e178e81435c09d60378eColin Cross * @write - function to call for each block
1811e17b313a6257b7b5081e178e81435c09d60378eColin Cross * @priv - value that will be passed as the first argument to write
1821e17b313a6257b7b5081e178e81435c09d60378eColin Cross *
1831e17b313a6257b7b5081e178e81435c09d60378eColin Cross * Writes a sparse file by calling a callback function.  If sparse is true, the
1841e17b313a6257b7b5081e178e81435c09d60378eColin Cross * file will be written in the Android sparse file format.  If crc is true, the
1851e17b313a6257b7b5081e178e81435c09d60378eColin Cross * crc of the expanded data will be calculated and appended in a crc chunk.
1861e17b313a6257b7b5081e178e81435c09d60378eColin Cross * The callback 'write' will be called with data and length for each data,
1871e17b313a6257b7b5081e178e81435c09d60378eColin Cross * and with data==NULL to skip over a region (only used for non-sparse format).
1881e17b313a6257b7b5081e178e81435c09d60378eColin Cross * The callback should return negative on error, 0 on success.
1891e17b313a6257b7b5081e178e81435c09d60378eColin Cross *
1901e17b313a6257b7b5081e178e81435c09d60378eColin Cross * Returns 0 on success, negative errno on error.
1911e17b313a6257b7b5081e178e81435c09d60378eColin Cross */
1921e17b313a6257b7b5081e178e81435c09d60378eColin Crossint sparse_file_callback(struct sparse_file *s, bool sparse, bool crc,
1931e17b313a6257b7b5081e178e81435c09d60378eColin Cross		int (*write)(void *priv, const void *data, int len), void *priv);
1941e17b313a6257b7b5081e178e81435c09d60378eColin Cross
1951e17b313a6257b7b5081e178e81435c09d60378eColin Cross/**
1960c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * sparse_file_read - read a file into a sparse file cookie
1970c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross *
1980c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * @s - sparse file cookie
1990c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * @fd - file descriptor to read from
2000c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * @sparse - read a file in the Android sparse file format
2010c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * @crc - verify the crc of a file in the Android sparse file format
2020c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross *
2030c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * Reads a file into a sparse file cookie.  If sparse is true, the file is
2040c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * assumed to be in the Android sparse file format.  If sparse is false, the
2050c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * file will be sparsed by looking for block aligned chunks of all zeros or
2060c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * another 32 bit value.  If crc is true, the crc of the sparse file will be
2070c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * verified.
2080c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross *
2090c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * Returns 0 on success, negative errno on error.
2100c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross */
2110c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Crossint sparse_file_read(struct sparse_file *s, int fd, bool sparse, bool crc);
2120c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross
2130c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross/**
2140c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * sparse_file_import - import an existing sparse file
2150c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross *
2160c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * @s - sparse file cookie
2170c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * @verbose - print verbose errors while reading the sparse file
2180c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * @crc - verify the crc of a file in the Android sparse file format
2190c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross *
2200c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * Reads an existing sparse file into a sparse file cookie, recreating the same
2210c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * sparse cookie that was used to write it.  If verbose is true, prints verbose
2220c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * errors when the sparse file is formatted incorrectly.
2230c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross *
2240c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * Returns a new sparse file cookie on success, NULL on error.
2250c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross */
2260c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Crossstruct sparse_file *sparse_file_import(int fd, bool verbose, bool crc);
2270c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross
2280c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross/**
2290c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * sparse_file_import_auto - import an existing sparse or normal file
2300c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross *
2310c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * @fd - file descriptor to read from
2320c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * @crc - verify the crc of a file in the Android sparse file format
2330c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross *
2340c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * Reads an existing sparse or normal file into a sparse file cookie.
2350c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * Attempts to determine if the file is sparse or not by looking for the sparse
2360c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * file magic number in the first 4 bytes.  If the file is not sparse, the file
2370c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * will be sparsed by looking for block aligned chunks of all zeros or another
2380c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * 32 bit value.  If crc is true, the crc of the sparse file will be verified.
2390c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross *
2400c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross * Returns a new sparse file cookie on success, NULL on error.
2410c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross */
2420c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Crossstruct sparse_file *sparse_file_import_auto(int fd, bool crc);
2430c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross
244bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross/** sparse_file_resparse - rechunk an existing sparse file into smaller files
245bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross *
246bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross * @in_s - sparse file cookie of the existing sparse file
247bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross * @max_len - maximum file size
248bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross * @out_s - array of sparse file cookies
249bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross * @out_s_count - size of out_s array
250bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross *
251bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross * Splits chunks of an existing sparse file into smaller sparse files such that
252bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross * each sparse file is less than max_len.  Returns the number of sparse_files
253bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross * that would have been written to out_s if out_s were big enough.
254bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross */
255bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Crossint sparse_file_resparse(struct sparse_file *in_s, unsigned int max_len,
256bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross		struct sparse_file **out_s, int out_s_count);
257bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross
2580c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross/**
259a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross * sparse_file_verbose - set a sparse file cookie to print verbose errors
260a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross *
261a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross * @s - sparse file cookie
262a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross *
263a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross * Print verbose sparse file errors whenever using the sparse file cookie.
264a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross */
265a21930b6b0dbb04a52948566d58fb48c6db58babColin Crossvoid sparse_file_verbose(struct sparse_file *s);
266a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross
267a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross/**
268a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross * sparse_print_verbose - function called to print verbose errors
269a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross *
270a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross * By default, verbose errors will print to standard error.
271a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross * sparse_print_verbose may be overridden to log verbose errors somewhere else.
272a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross *
273a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross */
274a21930b6b0dbb04a52948566d58fb48c6db58babColin Crossextern void (*sparse_print_verbose)(const char *fmt, ...);
275a21930b6b0dbb04a52948566d58fb48c6db58babColin Cross
27628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross#endif
277