summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'libsparse/output_file.cpp')
-rw-r--r--libsparse/output_file.cpp1071
1 files changed, 506 insertions, 565 deletions
diff --git a/libsparse/output_file.cpp b/libsparse/output_file.cpp
index 8d915508c..5388e7742 100644
--- a/libsparse/output_file.cpp
+++ b/libsparse/output_file.cpp
@@ -48,732 +48,673 @@
48#define off64_t off_t 48#define off64_t off_t
49#endif 49#endif
50 50
51#define min(a, b) \ 51#define min(a, b) \
52 ({ typeof(a) _a = (a); typeof(b) _b = (b); (_a < _b) ? _a : _b; }) 52 ({ \
53 typeof(a) _a = (a); \
54 typeof(b) _b = (b); \
55 (_a < _b) ? _a : _b; \
56 })
53 57
54#define SPARSE_HEADER_MAJOR_VER 1 58#define SPARSE_HEADER_MAJOR_VER 1
55#define SPARSE_HEADER_MINOR_VER 0 59#define SPARSE_HEADER_MINOR_VER 0
56#define SPARSE_HEADER_LEN (sizeof(sparse_header_t)) 60#define SPARSE_HEADER_LEN (sizeof(sparse_header_t))
57#define CHUNK_HEADER_LEN (sizeof(chunk_header_t)) 61#define CHUNK_HEADER_LEN (sizeof(chunk_header_t))
58 62
59#define container_of(inner, outer_t, elem) \ 63#define container_of(inner, outer_t, elem) ((outer_t*)((char*)(inner)-offsetof(outer_t, elem)))
60 ((outer_t *)((char *)(inner) - offsetof(outer_t, elem)))
61 64
62struct output_file_ops { 65struct output_file_ops {
63 int (*open)(struct output_file *, int fd); 66 int (*open)(struct output_file*, int fd);
64 int (*skip)(struct output_file *, int64_t); 67 int (*skip)(struct output_file*, int64_t);
65 int (*pad)(struct output_file *, int64_t); 68 int (*pad)(struct output_file*, int64_t);
66 int (*write)(struct output_file *, void *, size_t); 69 int (*write)(struct output_file*, void*, size_t);
67 void (*close)(struct output_file *); 70 void (*close)(struct output_file*);
68}; 71};
69 72
70struct sparse_file_ops { 73struct sparse_file_ops {
71 int (*write_data_chunk)(struct output_file *out, unsigned int len, 74 int (*write_data_chunk)(struct output_file* out, unsigned int len, void* data);
72 void *data); 75 int (*write_fill_chunk)(struct output_file* out, unsigned int len, uint32_t fill_val);
73 int (*write_fill_chunk)(struct output_file *out, unsigned int len, 76 int (*write_skip_chunk)(struct output_file* out, int64_t len);
74 uint32_t fill_val); 77 int (*write_end_chunk)(struct output_file* out);
75 int (*write_skip_chunk)(struct output_file *out, int64_t len);
76 int (*write_end_chunk)(struct output_file *out);
77}; 78};
78 79
79struct output_file { 80struct output_file {
80 int64_t cur_out_ptr; 81 int64_t cur_out_ptr;
81 unsigned int chunk_cnt; 82 unsigned int chunk_cnt;
82 uint32_t crc32; 83 uint32_t crc32;
83 struct output_file_ops *ops; 84 struct output_file_ops* ops;
84 struct sparse_file_ops *sparse_ops; 85 struct sparse_file_ops* sparse_ops;
85 int use_crc; 86 int use_crc;
86 unsigned int block_size; 87 unsigned int block_size;
87 int64_t len; 88 int64_t len;
88 char *zero_buf; 89 char* zero_buf;
89 uint32_t *fill_buf; 90 uint32_t* fill_buf;
90 char *buf; 91 char* buf;
91}; 92};
92 93
93struct output_file_gz { 94struct output_file_gz {
94 struct output_file out; 95 struct output_file out;
95 gzFile gz_fd; 96 gzFile gz_fd;
96}; 97};
97 98
98#define to_output_file_gz(_o) \ 99#define to_output_file_gz(_o) container_of((_o), struct output_file_gz, out)
99 container_of((_o), struct output_file_gz, out)
100 100
101struct output_file_normal { 101struct output_file_normal {
102 struct output_file out; 102 struct output_file out;
103 int fd; 103 int fd;
104}; 104};
105 105
106#define to_output_file_normal(_o) \ 106#define to_output_file_normal(_o) container_of((_o), struct output_file_normal, out)
107 container_of((_o), struct output_file_normal, out)
108 107
109struct output_file_callback { 108struct output_file_callback {
110 struct output_file out; 109 struct output_file out;
111 void *priv; 110 void* priv;
112 int (*write)(void *priv, const void *buf, size_t len); 111 int (*write)(void* priv, const void* buf, size_t len);
113}; 112};
114 113
115#define to_output_file_callback(_o) \ 114#define to_output_file_callback(_o) container_of((_o), struct output_file_callback, out)
116 container_of((_o), struct output_file_callback, out)
117 115
118static int file_open(struct output_file *out, int fd) 116static int file_open(struct output_file* out, int fd) {
119{ 117 struct output_file_normal* outn = to_output_file_normal(out);
120 struct output_file_normal *outn = to_output_file_normal(out);
121 118
122 outn->fd = fd; 119 outn->fd = fd;
123 return 0; 120 return 0;
124} 121}
125 122
126static int file_skip(struct output_file *out, int64_t cnt) 123static int file_skip(struct output_file* out, int64_t cnt) {
127{ 124 off64_t ret;
128 off64_t ret; 125 struct output_file_normal* outn = to_output_file_normal(out);
129 struct output_file_normal *outn = to_output_file_normal(out);
130 126
131 ret = lseek64(outn->fd, cnt, SEEK_CUR); 127 ret = lseek64(outn->fd, cnt, SEEK_CUR);
132 if (ret < 0) { 128 if (ret < 0) {
133 error_errno("lseek64"); 129 error_errno("lseek64");
134 return -1; 130 return -1;
135 } 131 }
136 return 0; 132 return 0;
137} 133}
138 134
139static int file_pad(struct output_file *out, int64_t len) 135static int file_pad(struct output_file* out, int64_t len) {
140{ 136 int ret;
141 int ret; 137 struct output_file_normal* outn = to_output_file_normal(out);
142 struct output_file_normal *outn = to_output_file_normal(out);
143 138
144 ret = ftruncate64(outn->fd, len); 139 ret = ftruncate64(outn->fd, len);
145 if (ret < 0) { 140 if (ret < 0) {
146 return -errno; 141 return -errno;
147 } 142 }
148 143
149 return 0; 144 return 0;
150} 145}
151 146
152static int file_write(struct output_file *out, void *data, size_t len) 147static int file_write(struct output_file* out, void* data, size_t len) {
153{ 148 ssize_t ret;
154 ssize_t ret; 149 struct output_file_normal* outn = to_output_file_normal(out);
155 struct output_file_normal *outn = to_output_file_normal(out);
156 150
157 while (len > 0) { 151 while (len > 0) {
158 ret = write(outn->fd, data, len); 152 ret = write(outn->fd, data, len);
159 if (ret < 0) { 153 if (ret < 0) {
160 if (errno == EINTR) { 154 if (errno == EINTR) {
161 continue; 155 continue;
162 } 156 }
163 error_errno("write"); 157 error_errno("write");
164 return -1; 158 return -1;
165 } 159 }
166 160
167 data = (char *)data + ret; 161 data = (char*)data + ret;
168 len -= ret; 162 len -= ret;
169 } 163 }
170 164
171 return 0; 165 return 0;
172} 166}
173 167
174static void file_close(struct output_file *out) 168static void file_close(struct output_file* out) {
175{ 169 struct output_file_normal* outn = to_output_file_normal(out);
176 struct output_file_normal *outn = to_output_file_normal(out);
177 170
178 free(outn); 171 free(outn);
179} 172}
180 173
181static struct output_file_ops file_ops = { 174static struct output_file_ops file_ops = {
182 .open = file_open, 175 .open = file_open,
183 .skip = file_skip, 176 .skip = file_skip,
184 .pad = file_pad, 177 .pad = file_pad,
185 .write = file_write, 178 .write = file_write,
186 .close = file_close, 179 .close = file_close,
187}; 180};
188 181
189static int gz_file_open(struct output_file *out, int fd) 182static int gz_file_open(struct output_file* out, int fd) {
190{ 183 struct output_file_gz* outgz = to_output_file_gz(out);
191 struct output_file_gz *outgz = to_output_file_gz(out);
192 184
193 outgz->gz_fd = gzdopen(fd, "wb9"); 185 outgz->gz_fd = gzdopen(fd, "wb9");
194 if (!outgz->gz_fd) { 186 if (!outgz->gz_fd) {
195 error_errno("gzopen"); 187 error_errno("gzopen");
196 return -errno; 188 return -errno;
197 } 189 }
198 190
199 return 0; 191 return 0;
200} 192}
201 193
194static int gz_file_skip(struct output_file* out, int64_t cnt) {
195 off64_t ret;
196 struct output_file_gz* outgz = to_output_file_gz(out);
202 197
203static int gz_file_skip(struct output_file *out, int64_t cnt) 198 ret = gzseek(outgz->gz_fd, cnt, SEEK_CUR);
204{ 199 if (ret < 0) {
205 off64_t ret; 200 error_errno("gzseek");
206 struct output_file_gz *outgz = to_output_file_gz(out); 201 return -1;
207 202 }
208 ret = gzseek(outgz->gz_fd, cnt, SEEK_CUR); 203 return 0;
209 if (ret < 0) {
210 error_errno("gzseek");
211 return -1;
212 }
213 return 0;
214} 204}
215 205
216static int gz_file_pad(struct output_file *out, int64_t len) 206static int gz_file_pad(struct output_file* out, int64_t len) {
217{ 207 off64_t ret;
218 off64_t ret; 208 struct output_file_gz* outgz = to_output_file_gz(out);
219 struct output_file_gz *outgz = to_output_file_gz(out);
220 209
221 ret = gztell(outgz->gz_fd); 210 ret = gztell(outgz->gz_fd);
222 if (ret < 0) { 211 if (ret < 0) {
223 return -1; 212 return -1;
224 } 213 }
225 214
226 if (ret >= len) { 215 if (ret >= len) {
227 return 0; 216 return 0;
228 } 217 }
229 218
230 ret = gzseek(outgz->gz_fd, len - 1, SEEK_SET); 219 ret = gzseek(outgz->gz_fd, len - 1, SEEK_SET);
231 if (ret < 0) { 220 if (ret < 0) {
232 return -1; 221 return -1;
233 } 222 }
234 223
235 gzwrite(outgz->gz_fd, "", 1); 224 gzwrite(outgz->gz_fd, "", 1);
236 225
237 return 0; 226 return 0;
238} 227}
239 228
240static int gz_file_write(struct output_file *out, void *data, size_t len) 229static int gz_file_write(struct output_file* out, void* data, size_t len) {
241{ 230 int ret;
242 int ret; 231 struct output_file_gz* outgz = to_output_file_gz(out);
243 struct output_file_gz *outgz = to_output_file_gz(out);
244 232
245 while (len > 0) { 233 while (len > 0) {
246 ret = gzwrite(outgz->gz_fd, data, 234 ret = gzwrite(outgz->gz_fd, data, min(len, (unsigned int)INT_MAX));
247 min(len, (unsigned int)INT_MAX)); 235 if (ret == 0) {
248 if (ret == 0) { 236 error("gzwrite %s", gzerror(outgz->gz_fd, NULL));
249 error("gzwrite %s", gzerror(outgz->gz_fd, NULL)); 237 return -1;
250 return -1; 238 }
251 } 239 len -= ret;
252 len -= ret; 240 data = (char*)data + ret;
253 data = (char *)data + ret; 241 }
254 }
255 242
256 return 0; 243 return 0;
257} 244}
258 245
259static void gz_file_close(struct output_file *out) 246static void gz_file_close(struct output_file* out) {
260{ 247 struct output_file_gz* outgz = to_output_file_gz(out);
261 struct output_file_gz *outgz = to_output_file_gz(out);
262 248
263 gzclose(outgz->gz_fd); 249 gzclose(outgz->gz_fd);
264 free(outgz); 250 free(outgz);
265} 251}
266 252
267static struct output_file_ops gz_file_ops = { 253static struct output_file_ops gz_file_ops = {
268 .open = gz_file_open, 254 .open = gz_file_open,
269 .skip = gz_file_skip, 255 .skip = gz_file_skip,
270 .pad = gz_file_pad, 256 .pad = gz_file_pad,
271 .write = gz_file_write, 257 .write = gz_file_write,
272 .close = gz_file_close, 258 .close = gz_file_close,
273}; 259};
274 260
275static int callback_file_open(struct output_file *out __unused, int fd __unused) 261static int callback_file_open(struct output_file* out __unused, int fd __unused) {
276{ 262 return 0;
277 return 0;
278} 263}
279 264
280static int callback_file_skip(struct output_file *out, int64_t off) 265static int callback_file_skip(struct output_file* out, int64_t off) {
281{ 266 struct output_file_callback* outc = to_output_file_callback(out);
282 struct output_file_callback *outc = to_output_file_callback(out); 267 int to_write;
283 int to_write; 268 int ret;
284 int ret;
285 269
286 while (off > 0) { 270 while (off > 0) {
287 to_write = min(off, (int64_t)INT_MAX); 271 to_write = min(off, (int64_t)INT_MAX);
288 ret = outc->write(outc->priv, NULL, to_write); 272 ret = outc->write(outc->priv, NULL, to_write);
289 if (ret < 0) { 273 if (ret < 0) {
290 return ret; 274 return ret;
291 } 275 }
292 off -= to_write; 276 off -= to_write;
293 } 277 }
294 278
295 return 0; 279 return 0;
296} 280}
297 281
298static int callback_file_pad(struct output_file *out __unused, int64_t len __unused) 282static int callback_file_pad(struct output_file* out __unused, int64_t len __unused) {
299{ 283 return -1;
300 return -1;
301} 284}
302 285
303static int callback_file_write(struct output_file *out, void *data, size_t len) 286static int callback_file_write(struct output_file* out, void* data, size_t len) {
304{ 287 struct output_file_callback* outc = to_output_file_callback(out);
305 struct output_file_callback *outc = to_output_file_callback(out);
306 288
307 return outc->write(outc->priv, data, len); 289 return outc->write(outc->priv, data, len);
308} 290}
309 291
310static void callback_file_close(struct output_file *out) 292static void callback_file_close(struct output_file* out) {
311{ 293 struct output_file_callback* outc = to_output_file_callback(out);
312 struct output_file_callback *outc = to_output_file_callback(out);
313 294
314 free(outc); 295 free(outc);
315} 296}
316 297
317static struct output_file_ops callback_file_ops = { 298static struct output_file_ops callback_file_ops = {
318 .open = callback_file_open, 299 .open = callback_file_open,
319 .skip = callback_file_skip, 300 .skip = callback_file_skip,
320 .pad = callback_file_pad, 301 .pad = callback_file_pad,
321 .write = callback_file_write, 302 .write = callback_file_write,
322 .close = callback_file_close, 303 .close = callback_file_close,
323}; 304};
324 305
325int read_all(int fd, void *buf, size_t len) 306int read_all(int fd, void* buf, size_t len) {
326{ 307 size_t total = 0;
327 size_t total = 0; 308 int ret;
328 int ret; 309 char* ptr = reinterpret_cast<char*>(buf);
329 char *ptr = reinterpret_cast<char*>(buf); 310
330 311 while (total < len) {
331 while (total < len) { 312 ret = read(fd, ptr, len - total);
332 ret = read(fd, ptr, len - total); 313
333 314 if (ret < 0) return -errno;
334 if (ret < 0) 315
335 return -errno; 316 if (ret == 0) return -EINVAL;
336 317
337 if (ret == 0) 318 ptr += ret;
338 return -EINVAL; 319 total += ret;
339 320 }
340 ptr += ret; 321
341 total += ret; 322 return 0;
342 } 323}
343 324
344 return 0; 325static int write_sparse_skip_chunk(struct output_file* out, int64_t skip_len) {
345} 326 chunk_header_t chunk_header;
346 327 int ret;
347static int write_sparse_skip_chunk(struct output_file *out, int64_t skip_len) 328
348{ 329 if (skip_len % out->block_size) {
349 chunk_header_t chunk_header; 330 error("don't care size %" PRIi64 " is not a multiple of the block size %u", skip_len,
350 int ret; 331 out->block_size);
351 332 return -1;
352 if (skip_len % out->block_size) { 333 }
353 error("don't care size %" PRIi64 " is not a multiple of the block size %u", 334
354 skip_len, out->block_size); 335 /* We are skipping data, so emit a don't care chunk. */
355 return -1; 336 chunk_header.chunk_type = CHUNK_TYPE_DONT_CARE;
356 } 337 chunk_header.reserved1 = 0;
357 338 chunk_header.chunk_sz = skip_len / out->block_size;
358 /* We are skipping data, so emit a don't care chunk. */ 339 chunk_header.total_sz = CHUNK_HEADER_LEN;
359 chunk_header.chunk_type = CHUNK_TYPE_DONT_CARE; 340 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
360 chunk_header.reserved1 = 0; 341 if (ret < 0) return -1;
361 chunk_header.chunk_sz = skip_len / out->block_size; 342
362 chunk_header.total_sz = CHUNK_HEADER_LEN; 343 out->cur_out_ptr += skip_len;
363 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header)); 344 out->chunk_cnt++;
364 if (ret < 0) 345
365 return -1; 346 return 0;
366 347}
367 out->cur_out_ptr += skip_len; 348
368 out->chunk_cnt++; 349static int write_sparse_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
369 350 chunk_header_t chunk_header;
370 return 0; 351 int rnd_up_len, count;
371} 352 int ret;
372 353
373static int write_sparse_fill_chunk(struct output_file *out, unsigned int len, 354 /* Round up the fill length to a multiple of the block size */
374 uint32_t fill_val) 355 rnd_up_len = ALIGN(len, out->block_size);
375{ 356
376 chunk_header_t chunk_header; 357 /* Finally we can safely emit a chunk of data */
377 int rnd_up_len, count; 358 chunk_header.chunk_type = CHUNK_TYPE_FILL;
378 int ret; 359 chunk_header.reserved1 = 0;
379 360 chunk_header.chunk_sz = rnd_up_len / out->block_size;
380 /* Round up the fill length to a multiple of the block size */ 361 chunk_header.total_sz = CHUNK_HEADER_LEN + sizeof(fill_val);
381 rnd_up_len = ALIGN(len, out->block_size); 362 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
382 363
383 /* Finally we can safely emit a chunk of data */ 364 if (ret < 0) return -1;
384 chunk_header.chunk_type = CHUNK_TYPE_FILL; 365 ret = out->ops->write(out, &fill_val, sizeof(fill_val));
385 chunk_header.reserved1 = 0; 366 if (ret < 0) return -1;
386 chunk_header.chunk_sz = rnd_up_len / out->block_size; 367
387 chunk_header.total_sz = CHUNK_HEADER_LEN + sizeof(fill_val); 368 if (out->use_crc) {
388 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header)); 369 count = out->block_size / sizeof(uint32_t);
389 370 while (count--) out->crc32 = sparse_crc32(out->crc32, &fill_val, sizeof(uint32_t));
390 if (ret < 0) 371 }
391 return -1; 372
392 ret = out->ops->write(out, &fill_val, sizeof(fill_val)); 373 out->cur_out_ptr += rnd_up_len;
393 if (ret < 0) 374 out->chunk_cnt++;
394 return -1; 375
395 376 return 0;
396 if (out->use_crc) { 377}
397 count = out->block_size / sizeof(uint32_t); 378
398 while (count--) 379static int write_sparse_data_chunk(struct output_file* out, unsigned int len, void* data) {
399 out->crc32 = sparse_crc32(out->crc32, &fill_val, sizeof(uint32_t)); 380 chunk_header_t chunk_header;
400 } 381 int rnd_up_len, zero_len;
401 382 int ret;
402 out->cur_out_ptr += rnd_up_len; 383
403 out->chunk_cnt++; 384 /* Round up the data length to a multiple of the block size */
404 385 rnd_up_len = ALIGN(len, out->block_size);
405 return 0; 386 zero_len = rnd_up_len - len;
406} 387
407 388 /* Finally we can safely emit a chunk of data */
408static int write_sparse_data_chunk(struct output_file *out, unsigned int len, 389 chunk_header.chunk_type = CHUNK_TYPE_RAW;
409 void *data) 390 chunk_header.reserved1 = 0;
410{ 391 chunk_header.chunk_sz = rnd_up_len / out->block_size;
411 chunk_header_t chunk_header; 392 chunk_header.total_sz = CHUNK_HEADER_LEN + rnd_up_len;
412 int rnd_up_len, zero_len; 393 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
413 int ret; 394
414 395 if (ret < 0) return -1;
415 /* Round up the data length to a multiple of the block size */ 396 ret = out->ops->write(out, data, len);
416 rnd_up_len = ALIGN(len, out->block_size); 397 if (ret < 0) return -1;
417 zero_len = rnd_up_len - len; 398 if (zero_len) {
418 399 ret = out->ops->write(out, out->zero_buf, zero_len);
419 /* Finally we can safely emit a chunk of data */ 400 if (ret < 0) return -1;
420 chunk_header.chunk_type = CHUNK_TYPE_RAW; 401 }
421 chunk_header.reserved1 = 0; 402
422 chunk_header.chunk_sz = rnd_up_len / out->block_size; 403 if (out->use_crc) {
423 chunk_header.total_sz = CHUNK_HEADER_LEN + rnd_up_len; 404 out->crc32 = sparse_crc32(out->crc32, data, len);
424 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header)); 405 if (zero_len) out->crc32 = sparse_crc32(out->crc32, out->zero_buf, zero_len);
425 406 }
426 if (ret < 0) 407
427 return -1; 408 out->cur_out_ptr += rnd_up_len;
428 ret = out->ops->write(out, data, len); 409 out->chunk_cnt++;
429 if (ret < 0) 410
430 return -1; 411 return 0;
431 if (zero_len) { 412}
432 ret = out->ops->write(out, out->zero_buf, zero_len); 413
433 if (ret < 0) 414int write_sparse_end_chunk(struct output_file* out) {
434 return -1; 415 chunk_header_t chunk_header;
435 } 416 int ret;
436 417
437 if (out->use_crc) { 418 if (out->use_crc) {
438 out->crc32 = sparse_crc32(out->crc32, data, len); 419 chunk_header.chunk_type = CHUNK_TYPE_CRC32;
439 if (zero_len) 420 chunk_header.reserved1 = 0;
440 out->crc32 = sparse_crc32(out->crc32, out->zero_buf, zero_len); 421 chunk_header.chunk_sz = 0;
441 } 422 chunk_header.total_sz = CHUNK_HEADER_LEN + 4;
442 423
443 out->cur_out_ptr += rnd_up_len; 424 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
444 out->chunk_cnt++; 425 if (ret < 0) {
445 426 return ret;
446 return 0; 427 }
447} 428 out->ops->write(out, &out->crc32, 4);
448 429 if (ret < 0) {
449int write_sparse_end_chunk(struct output_file *out) 430 return ret;
450{ 431 }
451 chunk_header_t chunk_header; 432
452 int ret; 433 out->chunk_cnt++;
453 434 }
454 if (out->use_crc) { 435
455 chunk_header.chunk_type = CHUNK_TYPE_CRC32; 436 return 0;
456 chunk_header.reserved1 = 0;
457 chunk_header.chunk_sz = 0;
458 chunk_header.total_sz = CHUNK_HEADER_LEN + 4;
459
460 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
461 if (ret < 0) {
462 return ret;
463 }
464 out->ops->write(out, &out->crc32, 4);
465 if (ret < 0) {
466 return ret;
467 }
468
469 out->chunk_cnt++;
470 }
471
472 return 0;
473} 437}
474 438
475static struct sparse_file_ops sparse_file_ops = { 439static struct sparse_file_ops sparse_file_ops = {
476 .write_data_chunk = write_sparse_data_chunk, 440 .write_data_chunk = write_sparse_data_chunk,
477 .write_fill_chunk = write_sparse_fill_chunk, 441 .write_fill_chunk = write_sparse_fill_chunk,
478 .write_skip_chunk = write_sparse_skip_chunk, 442 .write_skip_chunk = write_sparse_skip_chunk,
479 .write_end_chunk = write_sparse_end_chunk, 443 .write_end_chunk = write_sparse_end_chunk,
480}; 444};
481 445
482static int write_normal_data_chunk(struct output_file *out, unsigned int len, 446static int write_normal_data_chunk(struct output_file* out, unsigned int len, void* data) {
483 void *data) 447 int ret;
484{ 448 unsigned int rnd_up_len = ALIGN(len, out->block_size);
485 int ret;
486 unsigned int rnd_up_len = ALIGN(len, out->block_size);
487 449
488 ret = out->ops->write(out, data, len); 450 ret = out->ops->write(out, data, len);
489 if (ret < 0) { 451 if (ret < 0) {
490 return ret; 452 return ret;
491 } 453 }
492 454
493 if (rnd_up_len > len) { 455 if (rnd_up_len > len) {
494 ret = out->ops->skip(out, rnd_up_len - len); 456 ret = out->ops->skip(out, rnd_up_len - len);
495 } 457 }
496 458
497 return ret; 459 return ret;
498} 460}
499 461
500static int write_normal_fill_chunk(struct output_file *out, unsigned int len, 462static int write_normal_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
501 uint32_t fill_val) 463 int ret;
502{ 464 unsigned int i;
503 int ret; 465 unsigned int write_len;
504 unsigned int i;
505 unsigned int write_len;
506 466
507 /* Initialize fill_buf with the fill_val */ 467 /* Initialize fill_buf with the fill_val */
508 for (i = 0; i < out->block_size / sizeof(uint32_t); i++) { 468 for (i = 0; i < out->block_size / sizeof(uint32_t); i++) {
509 out->fill_buf[i] = fill_val; 469 out->fill_buf[i] = fill_val;
510 } 470 }
511 471
512 while (len) { 472 while (len) {
513 write_len = min(len, out->block_size); 473 write_len = min(len, out->block_size);
514 ret = out->ops->write(out, out->fill_buf, write_len); 474 ret = out->ops->write(out, out->fill_buf, write_len);
515 if (ret < 0) { 475 if (ret < 0) {
516 return ret; 476 return ret;
517 } 477 }
518 478
519 len -= write_len; 479 len -= write_len;
520 } 480 }
521 481
522 return 0; 482 return 0;
523} 483}
524 484
525static int write_normal_skip_chunk(struct output_file *out, int64_t len) 485static int write_normal_skip_chunk(struct output_file* out, int64_t len) {
526{ 486 return out->ops->skip(out, len);
527 return out->ops->skip(out, len);
528} 487}
529 488
530int write_normal_end_chunk(struct output_file *out) 489int write_normal_end_chunk(struct output_file* out) {
531{ 490 return out->ops->pad(out, out->len);
532 return out->ops->pad(out, out->len);
533} 491}
534 492
535static struct sparse_file_ops normal_file_ops = { 493static struct sparse_file_ops normal_file_ops = {
536 .write_data_chunk = write_normal_data_chunk, 494 .write_data_chunk = write_normal_data_chunk,
537 .write_fill_chunk = write_normal_fill_chunk, 495 .write_fill_chunk = write_normal_fill_chunk,
538 .write_skip_chunk = write_normal_skip_chunk, 496 .write_skip_chunk = write_normal_skip_chunk,
539 .write_end_chunk = write_normal_end_chunk, 497 .write_end_chunk = write_normal_end_chunk,
540}; 498};
541 499
542void output_file_close(struct output_file *out) 500void output_file_close(struct output_file* out) {
543{ 501 out->sparse_ops->write_end_chunk(out);
544 out->sparse_ops->write_end_chunk(out); 502 out->ops->close(out);
545 out->ops->close(out); 503}
546} 504
547 505static int output_file_init(struct output_file* out, int block_size, int64_t len, bool sparse,
548static int output_file_init(struct output_file *out, int block_size, 506 int chunks, bool crc) {
549 int64_t len, bool sparse, int chunks, bool crc) 507 int ret;
550{ 508
551 int ret; 509 out->len = len;
552 510 out->block_size = block_size;
553 out->len = len; 511 out->cur_out_ptr = 0ll;
554 out->block_size = block_size; 512 out->chunk_cnt = 0;
555 out->cur_out_ptr = 0ll; 513 out->crc32 = 0;
556 out->chunk_cnt = 0; 514 out->use_crc = crc;
557 out->crc32 = 0; 515
558 out->use_crc = crc; 516 out->zero_buf = reinterpret_cast<char*>(calloc(block_size, 1));
559 517 if (!out->zero_buf) {
560 out->zero_buf = reinterpret_cast<char*>(calloc(block_size, 1)); 518 error_errno("malloc zero_buf");
561 if (!out->zero_buf) { 519 return -ENOMEM;
562 error_errno("malloc zero_buf"); 520 }
563 return -ENOMEM; 521
564 } 522 out->fill_buf = reinterpret_cast<uint32_t*>(calloc(block_size, 1));
565 523 if (!out->fill_buf) {
566 out->fill_buf = reinterpret_cast<uint32_t*>(calloc(block_size, 1)); 524 error_errno("malloc fill_buf");
567 if (!out->fill_buf) { 525 ret = -ENOMEM;
568 error_errno("malloc fill_buf"); 526 goto err_fill_buf;
569 ret = -ENOMEM; 527 }
570 goto err_fill_buf; 528
571 } 529 if (sparse) {
572 530 out->sparse_ops = &sparse_file_ops;
573 if (sparse) { 531 } else {
574 out->sparse_ops = &sparse_file_ops; 532 out->sparse_ops = &normal_file_ops;
575 } else { 533 }
576 out->sparse_ops = &normal_file_ops; 534
577 } 535 if (sparse) {
578 536 sparse_header_t sparse_header = {
579 if (sparse) { 537 .magic = SPARSE_HEADER_MAGIC,
580 sparse_header_t sparse_header = { 538 .major_version = SPARSE_HEADER_MAJOR_VER,
581 .magic = SPARSE_HEADER_MAGIC, 539 .minor_version = SPARSE_HEADER_MINOR_VER,
582 .major_version = SPARSE_HEADER_MAJOR_VER, 540 .file_hdr_sz = SPARSE_HEADER_LEN,
583 .minor_version = SPARSE_HEADER_MINOR_VER, 541 .chunk_hdr_sz = CHUNK_HEADER_LEN,
584 .file_hdr_sz = SPARSE_HEADER_LEN, 542 .blk_sz = out->block_size,
585 .chunk_hdr_sz = CHUNK_HEADER_LEN, 543 .total_blks = static_cast<unsigned>(DIV_ROUND_UP(out->len, out->block_size)),
586 .blk_sz = out->block_size, 544 .total_chunks = static_cast<unsigned>(chunks),
587 .total_blks = static_cast<unsigned>(DIV_ROUND_UP(out->len, out->block_size)), 545 .image_checksum = 0};
588 .total_chunks = static_cast<unsigned>(chunks), 546
589 .image_checksum = 0 547 if (out->use_crc) {
590 }; 548 sparse_header.total_chunks++;
591 549 }
592 if (out->use_crc) { 550
593 sparse_header.total_chunks++; 551 ret = out->ops->write(out, &sparse_header, sizeof(sparse_header));
594 } 552 if (ret < 0) {
595 553 goto err_write;
596 ret = out->ops->write(out, &sparse_header, sizeof(sparse_header)); 554 }
597 if (ret < 0) { 555 }
598 goto err_write; 556
599 } 557 return 0;
600 }
601
602 return 0;
603 558
604err_write: 559err_write:
605 free(out->fill_buf); 560 free(out->fill_buf);
606err_fill_buf: 561err_fill_buf:
607 free(out->zero_buf); 562 free(out->zero_buf);
608 return ret; 563 return ret;
609} 564}
610 565
611static struct output_file *output_file_new_gz(void) 566static struct output_file* output_file_new_gz(void) {
612{ 567 struct output_file_gz* outgz =
613 struct output_file_gz *outgz = reinterpret_cast<struct output_file_gz*>( 568 reinterpret_cast<struct output_file_gz*>(calloc(1, sizeof(struct output_file_gz)));
614 calloc(1, sizeof(struct output_file_gz))); 569 if (!outgz) {
615 if (!outgz) { 570 error_errno("malloc struct outgz");
616 error_errno("malloc struct outgz"); 571 return NULL;
617 return NULL; 572 }
618 }
619 573
620 outgz->out.ops = &gz_file_ops; 574 outgz->out.ops = &gz_file_ops;
621 575
622 return &outgz->out; 576 return &outgz->out;
623} 577}
624 578
625static struct output_file *output_file_new_normal(void) 579static struct output_file* output_file_new_normal(void) {
626{ 580 struct output_file_normal* outn =
627 struct output_file_normal *outn = reinterpret_cast<struct output_file_normal*>( 581 reinterpret_cast<struct output_file_normal*>(calloc(1, sizeof(struct output_file_normal)));
628 calloc(1, sizeof(struct output_file_normal))); 582 if (!outn) {
629 if (!outn) { 583 error_errno("malloc struct outn");
630 error_errno("malloc struct outn"); 584 return NULL;
631 return NULL; 585 }
632 }
633 586
634 outn->out.ops = &file_ops; 587 outn->out.ops = &file_ops;
635 588
636 return &outn->out; 589 return &outn->out;
637} 590}
638 591
639struct output_file *output_file_open_callback( 592struct output_file* output_file_open_callback(int (*write)(void*, const void*, size_t), void* priv,
640 int (*write)(void *, const void *, size_t), 593 unsigned int block_size, int64_t len, int gz __unused,
641 void *priv, unsigned int block_size, int64_t len, 594 int sparse, int chunks, int crc) {
642 int gz __unused, int sparse, int chunks, int crc) 595 int ret;
643{ 596 struct output_file_callback* outc;
644 int ret;
645 struct output_file_callback *outc;
646 597
647 outc = reinterpret_cast<struct output_file_callback*>( 598 outc =
648 calloc(1, sizeof(struct output_file_callback))); 599 reinterpret_cast<struct output_file_callback*>(calloc(1, sizeof(struct output_file_callback)));
649 if (!outc) { 600 if (!outc) {
650 error_errno("malloc struct outc"); 601 error_errno("malloc struct outc");
651 return NULL; 602 return NULL;
652 } 603 }
653 604
654 outc->out.ops = &callback_file_ops; 605 outc->out.ops = &callback_file_ops;
655 outc->priv = priv; 606 outc->priv = priv;
656 outc->write = write; 607 outc->write = write;
657 608
658 ret = output_file_init(&outc->out, block_size, len, sparse, chunks, crc); 609 ret = output_file_init(&outc->out, block_size, len, sparse, chunks, crc);
659 if (ret < 0) { 610 if (ret < 0) {
660 free(outc); 611 free(outc);
661 return NULL; 612 return NULL;
662 } 613 }
663 614
664 return &outc->out; 615 return &outc->out;
665} 616}
666 617
667struct output_file *output_file_open_fd(int fd, unsigned int block_size, int64_t len, 618struct output_file* output_file_open_fd(int fd, unsigned int block_size, int64_t len, int gz,
668 int gz, int sparse, int chunks, int crc) 619 int sparse, int chunks, int crc) {
669{ 620 int ret;
670 int ret; 621 struct output_file* out;
671 struct output_file *out;
672 622
673 if (gz) { 623 if (gz) {
674 out = output_file_new_gz(); 624 out = output_file_new_gz();
675 } else { 625 } else {
676 out = output_file_new_normal(); 626 out = output_file_new_normal();
677 } 627 }
678 if (!out) { 628 if (!out) {
679 return NULL; 629 return NULL;
680 } 630 }
681 631
682 out->ops->open(out, fd); 632 out->ops->open(out, fd);
683 633
684 ret = output_file_init(out, block_size, len, sparse, chunks, crc); 634 ret = output_file_init(out, block_size, len, sparse, chunks, crc);
685 if (ret < 0) { 635 if (ret < 0) {
686 free(out); 636 free(out);
687 return NULL; 637 return NULL;
688 } 638 }
689 639
690 return out; 640 return out;
691} 641}
692 642
693/* Write a contiguous region of data blocks from a memory buffer */ 643/* Write a contiguous region of data blocks from a memory buffer */
694int write_data_chunk(struct output_file *out, unsigned int len, void *data) 644int write_data_chunk(struct output_file* out, unsigned int len, void* data) {
695{ 645 return out->sparse_ops->write_data_chunk(out, len, data);
696 return out->sparse_ops->write_data_chunk(out, len, data);
697} 646}
698 647
699/* Write a contiguous region of data blocks with a fill value */ 648/* Write a contiguous region of data blocks with a fill value */
700int write_fill_chunk(struct output_file *out, unsigned int len, 649int write_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
701 uint32_t fill_val) 650 return out->sparse_ops->write_fill_chunk(out, len, fill_val);
702{
703 return out->sparse_ops->write_fill_chunk(out, len, fill_val);
704} 651}
705 652
706int write_fd_chunk(struct output_file *out, unsigned int len, 653int write_fd_chunk(struct output_file* out, unsigned int len, int fd, int64_t offset) {
707 int fd, int64_t offset) 654 int ret;
708{ 655 int64_t aligned_offset;
709 int ret; 656 int aligned_diff;
710 int64_t aligned_offset; 657 uint64_t buffer_size;
711 int aligned_diff; 658 char* ptr;
712 uint64_t buffer_size;
713 char *ptr;
714 659
715 aligned_offset = offset & ~(4096 - 1); 660 aligned_offset = offset & ~(4096 - 1);
716 aligned_diff = offset - aligned_offset; 661 aligned_diff = offset - aligned_offset;
717 buffer_size = (uint64_t)len + (uint64_t)aligned_diff; 662 buffer_size = (uint64_t)len + (uint64_t)aligned_diff;
718 663
719#ifndef _WIN32 664#ifndef _WIN32
720 if (buffer_size > SIZE_MAX) 665 if (buffer_size > SIZE_MAX) return -E2BIG;
721 return -E2BIG; 666 char* data =
722 char *data = reinterpret_cast<char*>(mmap64(NULL, buffer_size, PROT_READ, MAP_SHARED, fd, 667 reinterpret_cast<char*>(mmap64(NULL, buffer_size, PROT_READ, MAP_SHARED, fd, aligned_offset));
723 aligned_offset)); 668 if (data == MAP_FAILED) {
724 if (data == MAP_FAILED) { 669 return -errno;
725 return -errno; 670 }
726 } 671 ptr = data + aligned_diff;
727 ptr = data + aligned_diff;
728#else 672#else
729 off64_t pos; 673 off64_t pos;
730 char *data = reinterpret_cast<char*>(malloc(len)); 674 char* data = reinterpret_cast<char*>(malloc(len));
731 if (!data) { 675 if (!data) {
732 return -errno; 676 return -errno;
733 } 677 }
734 pos = lseek64(fd, offset, SEEK_SET); 678 pos = lseek64(fd, offset, SEEK_SET);
735 if (pos < 0) { 679 if (pos < 0) {
736 free(data); 680 free(data);
737 return -errno; 681 return -errno;
738 } 682 }
739 ret = read_all(fd, data, len); 683 ret = read_all(fd, data, len);
740 if (ret < 0) { 684 if (ret < 0) {
741 free(data); 685 free(data);
742 return ret; 686 return ret;
743 } 687 }
744 ptr = data; 688 ptr = data;
745#endif 689#endif
746 690
747 ret = out->sparse_ops->write_data_chunk(out, len, ptr); 691 ret = out->sparse_ops->write_data_chunk(out, len, ptr);
748 692
749#ifndef _WIN32 693#ifndef _WIN32
750 munmap(data, buffer_size); 694 munmap(data, buffer_size);
751#else 695#else
752 free(data); 696 free(data);
753#endif 697#endif
754 698
755 return ret; 699 return ret;
756} 700}
757 701
758/* Write a contiguous region of data blocks from a file */ 702/* Write a contiguous region of data blocks from a file */
759int write_file_chunk(struct output_file *out, unsigned int len, 703int write_file_chunk(struct output_file* out, unsigned int len, const char* file, int64_t offset) {
760 const char *file, int64_t offset) 704 int ret;
761{
762 int ret;
763 705
764 int file_fd = open(file, O_RDONLY | O_BINARY); 706 int file_fd = open(file, O_RDONLY | O_BINARY);
765 if (file_fd < 0) { 707 if (file_fd < 0) {
766 return -errno; 708 return -errno;
767 } 709 }
768 710
769 ret = write_fd_chunk(out, len, file_fd, offset); 711 ret = write_fd_chunk(out, len, file_fd, offset);
770 712
771 close(file_fd); 713 close(file_fd);
772 714
773 return ret; 715 return ret;
774} 716}
775 717
776int write_skip_chunk(struct output_file *out, int64_t len) 718int write_skip_chunk(struct output_file* out, int64_t len) {
777{ 719 return out->sparse_ops->write_skip_chunk(out, len);
778 return out->sparse_ops->write_skip_chunk(out, len);
779} 720}