sparse.h revision 9e1f17e926fa20255c5f4b4d2f68aa98a964253a
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _LIBSPARSE_SPARSE_H_
18#define _LIBSPARSE_SPARSE_H_
19
20#include <stdbool.h>
21#include <stdint.h>
22
23struct sparse_file;
24
25/**
26 * sparse_file_new - create a new sparse file cookie
27 *
28 * @block_size - minimum size of a chunk
29 * @len - size of the expanded sparse file.
30 *
31 * Creates a new sparse_file cookie that can be used to associate data
32 * blocks.  Can later be written to a file with a variety of options.
33 * block_size specifies the minimum size of a chunk in the file.  The maximum
34 * size of the file is 2**32 * block_size (16TB for 4k block size).
35 *
36 * Returns the sparse file cookie, or NULL on error.
37 */
38struct sparse_file *sparse_file_new(unsigned int block_size, int64_t len);
39
40/**
41 * sparse_file_destroy - destroy a sparse file cookie
42 *
43 * @s - sparse file cookie
44 *
45 * Destroys a sparse file cookie.  After destroy, all memory passed in to
46 * sparse_file_add_data can be freed by the caller
47 */
48void sparse_file_destroy(struct sparse_file *s);
49
50/**
51 * sparse_file_add_data - associate a data chunk with a sparse file
52 *
53 * @s - sparse file cookie
54 * @data - pointer to data block
55 * @len - length of the data block
56 * @block - offset in blocks into the sparse file to place the data chunk
57 *
58 * Associates a data chunk with a sparse file cookie.  The region
59 * [block * block_size : block * block_size + len) must not already be used in
60 * the sparse file. If len is not a multiple of the block size the data
61 * will be padded with zeros.
62 *
63 * The data pointer must remain valid until the sparse file is closed or the
64 * data block is removed from the sparse file.
65 *
66 * Returns 0 on success, negative errno on error.
67 */
68int sparse_file_add_data(struct sparse_file *s,
69		void *data, unsigned int len, unsigned int block);
70
71/**
72 * sparse_file_add_fill - associate a fill chunk with a sparse file
73 *
74 * @s - sparse file cookie
75 * @fill_val - 32 bit fill data
76 * @len - length of the fill block
77 * @block - offset in blocks into the sparse file to place the fill chunk
78 *
79 * Associates a chunk filled with fill_val with a sparse file cookie.
80 * The region [block * block_size : block * block_size + len) must not already
81 * be used in the sparse file. If len is not a multiple of the block size the
82 * data will be padded with zeros.
83 *
84 * Returns 0 on success, negative errno on error.
85 */
86int sparse_file_add_fill(struct sparse_file *s,
87		uint32_t fill_val, unsigned int len, unsigned int block);
88
89/**
90 * sparse_file_add_file - associate a chunk of a file with a sparse file
91 *
92 * @s - sparse file cookie
93 * @filename - filename of the file to be copied
94 * @file_offset - offset into the copied file
95 * @len - length of the copied block
96 * @block - offset in blocks into the sparse file to place the file chunk
97 *
98 * Associates a chunk of an existing file with a sparse file cookie.
99 * The region [block * block_size : block * block_size + len) must not already
100 * be used in the sparse file. If len is not a multiple of the block size the
101 * data will be padded with zeros.
102 *
103 * Allows adding large amounts of data to a sparse file without needing to keep
104 * it all mapped.  File size is limited by available virtual address space,
105 * exceptionally large files may need to be added in multiple chunks.
106 *
107 * Returns 0 on success, negative errno on error.
108 */
109int sparse_file_add_file(struct sparse_file *s,
110		const char *filename, int64_t file_offset, unsigned int len,
111		unsigned int block);
112
113/**
114 * sparse_file_add_file - associate a chunk of a file with a sparse file
115 *
116 * @s - sparse file cookie
117 * @filename - filename of the file to be copied
118 * @file_offset - offset into the copied file
119 * @len - length of the copied block
120 * @block - offset in blocks into the sparse file to place the file chunk
121 *
122 * Associates a chunk of an existing fd with a sparse file cookie.
123 * The region [block * block_size : block * block_size + len) must not already
124 * be used in the sparse file. If len is not a multiple of the block size the
125 * data will be padded with zeros.
126 *
127 * Allows adding large amounts of data to a sparse file without needing to keep
128 * it all mapped.  File size is limited by available virtual address space,
129 * exceptionally large files may need to be added in multiple chunks.
130 *
131 * The fd must remain open until the sparse file is closed or the fd block is
132 * removed from the sparse file.
133 *
134 * Returns 0 on success, negative errno on error.
135 */
136int sparse_file_add_fd(struct sparse_file *s,
137		int fd, int64_t file_offset, unsigned int len, unsigned int block);
138
139/**
140 * sparse_file_write - write a sparse file to a file
141 *
142 * @s - sparse file cookie
143 * @fd - file descriptor to write to
144 * @gz - write a gzipped file
145 * @sparse - write in the Android sparse file format
146 * @crc - append a crc chunk
147 *
148 * Writes a sparse file to a file.  If gz is true, the data will be passed
149 * through zlib.  If sparse is true, the file will be written in the Android
150 * sparse file format.  If sparse is false, the file will be written by seeking
151 * over unused chunks, producing a smaller file if the filesystem supports
152 * sparse files.  If crc is true, the crc of the expanded data will be
153 * calculated and appended in a crc chunk.
154 *
155 * Returns 0 on success, negative errno on error.
156 */
157int sparse_file_write(struct sparse_file *s, int fd, bool gz, bool sparse,
158		bool crc);
159
160#endif
161