1 /*- 2 * Copyright (c) 2003-2010 Tim Kientzle 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "archive_platform.h" 27 __FBSDID("$FreeBSD: head/lib/libarchive/archive_write.c 201099 2009-12-28 03:03:00Z kientzle $"); 28 29 /* 30 * This file contains the "essential" portions of the write API, that 31 * is, stuff that will essentially always be used by any client that 32 * actually needs to write an archive. Optional pieces have been, as 33 * far as possible, separated out into separate files to reduce 34 * needlessly bloating statically-linked clients. 35 */ 36 37 #ifdef HAVE_SYS_WAIT_H 38 #include <sys/wait.h> 39 #endif 40 #ifdef HAVE_ERRNO_H 41 #include <errno.h> 42 #endif 43 #ifdef HAVE_LIMITS_H 44 #include <limits.h> 45 #endif 46 #include <stdio.h> 47 #ifdef HAVE_STDLIB_H 48 #include <stdlib.h> 49 #endif 50 #ifdef HAVE_STRING_H 51 #include <string.h> 52 #endif 53 #include <time.h> 54 #ifdef HAVE_UNISTD_H 55 #include <unistd.h> 56 #endif 57 58 #include "archive.h" 59 #include "archive_entry.h" 60 #include "archive_private.h" 61 #include "archive_write_private.h" 62 63 static struct archive_vtable *archive_write_vtable(void); 64 65 static int _archive_filter_code(struct archive *, int); 66 static const char *_archive_filter_name(struct archive *, int); 67 static int64_t _archive_filter_bytes(struct archive *, int); 68 static int _archive_write_filter_count(struct archive *); 69 static int _archive_write_close(struct archive *); 70 static int _archive_write_free(struct archive *); 71 static int _archive_write_header(struct archive *, struct archive_entry *); 72 static int _archive_write_finish_entry(struct archive *); 73 static ssize_t _archive_write_data(struct archive *, const void *, size_t); 74 75 struct archive_none { 76 size_t buffer_size; 77 size_t avail; 78 char *buffer; 79 char *next; 80 }; 81 82 static struct archive_vtable * 83 archive_write_vtable(void) 84 { 85 static struct archive_vtable av; 86 static int inited = 0; 87 88 if (!inited) { 89 av.archive_close = _archive_write_close; 90 av.archive_filter_bytes = _archive_filter_bytes; 91 av.archive_filter_code = _archive_filter_code; 92 av.archive_filter_name = _archive_filter_name; 93 av.archive_filter_count = _archive_write_filter_count; 94 av.archive_free = _archive_write_free; 95 av.archive_write_header = _archive_write_header; 96 av.archive_write_finish_entry = _archive_write_finish_entry; 97 av.archive_write_data = _archive_write_data; 98 inited = 1; 99 } 100 return (&av); 101 } 102 103 /* 104 * Allocate, initialize and return an archive object. 105 */ 106 struct archive * 107 archive_write_new(void) 108 { 109 struct archive_write *a; 110 unsigned char *nulls; 111 112 a = (struct archive_write *)malloc(sizeof(*a)); 113 if (a == NULL) 114 return (NULL); 115 memset(a, 0, sizeof(*a)); 116 a->archive.magic = ARCHIVE_WRITE_MAGIC; 117 a->archive.state = ARCHIVE_STATE_NEW; 118 a->archive.vtable = archive_write_vtable(); 119 /* 120 * The value 10240 here matches the traditional tar default, 121 * but is otherwise arbitrary. 122 * TODO: Set the default block size from the format selected. 123 */ 124 a->bytes_per_block = 10240; 125 a->bytes_in_last_block = -1; /* Default */ 126 127 /* Initialize a block of nulls for padding purposes. */ 128 a->null_length = 1024; 129 nulls = (unsigned char *)malloc(a->null_length); 130 if (nulls == NULL) { 131 free(a); 132 return (NULL); 133 } 134 memset(nulls, 0, a->null_length); 135 a->nulls = nulls; 136 return (&a->archive); 137 } 138 139 /* 140 * Set the block size. Returns 0 if successful. 141 */ 142 int 143 archive_write_set_bytes_per_block(struct archive *_a, int bytes_per_block) 144 { 145 struct archive_write *a = (struct archive_write *)_a; 146 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, 147 ARCHIVE_STATE_NEW, "archive_write_set_bytes_per_block"); 148 a->bytes_per_block = bytes_per_block; 149 return (ARCHIVE_OK); 150 } 151 152 /* 153 * Get the current block size. -1 if it has never been set. 154 */ 155 int 156 archive_write_get_bytes_per_block(struct archive *_a) 157 { 158 struct archive_write *a = (struct archive_write *)_a; 159 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, 160 ARCHIVE_STATE_ANY, "archive_write_get_bytes_per_block"); 161 return (a->bytes_per_block); 162 } 163 164 /* 165 * Set the size for the last block. 166 * Returns 0 if successful. 167 */ 168 int 169 archive_write_set_bytes_in_last_block(struct archive *_a, int bytes) 170 { 171 struct archive_write *a = (struct archive_write *)_a; 172 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, 173 ARCHIVE_STATE_ANY, "archive_write_set_bytes_in_last_block"); 174 a->bytes_in_last_block = bytes; 175 return (ARCHIVE_OK); 176 } 177 178 /* 179 * Return the value set above. -1 indicates it has not been set. 180 */ 181 int 182 archive_write_get_bytes_in_last_block(struct archive *_a) 183 { 184 struct archive_write *a = (struct archive_write *)_a; 185 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, 186 ARCHIVE_STATE_ANY, "archive_write_get_bytes_in_last_block"); 187 return (a->bytes_in_last_block); 188 } 189 190 /* 191 * dev/ino of a file to be rejected. Used to prevent adding 192 * an archive to itself recursively. 193 */ 194 int 195 archive_write_set_skip_file(struct archive *_a, int64_t d, int64_t i) 196 { 197 struct archive_write *a = (struct archive_write *)_a; 198 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, 199 ARCHIVE_STATE_ANY, "archive_write_set_skip_file"); 200 a->skip_file_set = 1; 201 a->skip_file_dev = d; 202 a->skip_file_ino = i; 203 return (ARCHIVE_OK); 204 } 205 206 /* 207 * Allocate and return the next filter structure. 208 */ 209 struct archive_write_filter * 210 __archive_write_allocate_filter(struct archive *_a) 211 { 212 struct archive_write *a = (struct archive_write *)_a; 213 struct archive_write_filter *f; 214 215 f = calloc(1, sizeof(*f)); 216 f->archive = _a; 217 if (a->filter_first == NULL) 218 a->filter_first = f; 219 else 220 a->filter_last->next_filter = f; 221 a->filter_last = f; 222 return f; 223 } 224 225 /* 226 * Write data to a particular filter. 227 */ 228 int 229 __archive_write_filter(struct archive_write_filter *f, 230 const void *buff, size_t length) 231 { 232 int r; 233 if (length == 0) 234 return(ARCHIVE_OK); 235 r = (f->write)(f, buff, length); 236 f->bytes_written += length; 237 return (r); 238 } 239 240 /* 241 * Open a filter. 242 */ 243 int 244 __archive_write_open_filter(struct archive_write_filter *f) 245 { 246 if (f->open == NULL) 247 return (ARCHIVE_OK); 248 return (f->open)(f); 249 } 250 251 /* 252 * Close a filter. 253 */ 254 int 255 __archive_write_close_filter(struct archive_write_filter *f) 256 { 257 if (f->close != NULL) 258 return (f->close)(f); 259 if (f->next_filter != NULL) 260 return (__archive_write_close_filter(f->next_filter)); 261 return (ARCHIVE_OK); 262 } 263 264 int 265 __archive_write_output(struct archive_write *a, const void *buff, size_t length) 266 { 267 return (__archive_write_filter(a->filter_first, buff, length)); 268 } 269 270 int 271 __archive_write_nulls(struct archive_write *a, size_t length) 272 { 273 if (length == 0) 274 return (ARCHIVE_OK); 275 276 while (length > 0) { 277 size_t to_write = length < a->null_length ? length : a->null_length; 278 int r = __archive_write_output(a, a->nulls, to_write); 279 if (r < ARCHIVE_OK) 280 return (r); 281 length -= to_write; 282 } 283 return (ARCHIVE_OK); 284 } 285 286 static int 287 archive_write_client_open(struct archive_write_filter *f) 288 { 289 struct archive_write *a = (struct archive_write *)f->archive; 290 struct archive_none *state; 291 void *buffer; 292 size_t buffer_size; 293 294 f->bytes_per_block = archive_write_get_bytes_per_block(f->archive); 295 f->bytes_in_last_block = 296 archive_write_get_bytes_in_last_block(f->archive); 297 buffer_size = f->bytes_per_block; 298 299 state = (struct archive_none *)calloc(1, sizeof(*state)); 300 buffer = (char *)malloc(buffer_size); 301 if (state == NULL || buffer == NULL) { 302 free(state); 303 free(buffer); 304 archive_set_error(f->archive, ENOMEM, 305 "Can't allocate data for output buffering"); 306 return (ARCHIVE_FATAL); 307 } 308 309 state->buffer_size = buffer_size; 310 state->buffer = buffer; 311 state->next = state->buffer; 312 state->avail = state->buffer_size; 313 f->data = state; 314 315 if (a->client_opener == NULL) 316 return (ARCHIVE_OK); 317 return (a->client_opener(f->archive, a->client_data)); 318 } 319 320 static int 321 archive_write_client_write(struct archive_write_filter *f, 322 const void *_buff, size_t length) 323 { 324 struct archive_write *a = (struct archive_write *)f->archive; 325 struct archive_none *state = (struct archive_none *)f->data; 326 const char *buff = (const char *)_buff; 327 ssize_t remaining, to_copy; 328 ssize_t bytes_written; 329 330 remaining = length; 331 332 /* 333 * If there is no buffer for blocking, just pass the data 334 * straight through to the client write callback. In 335 * particular, this supports "no write delay" operation for 336 * special applications. Just set the block size to zero. 337 */ 338 if (state->buffer_size == 0) { 339 while (remaining > 0) { 340 bytes_written = (a->client_writer)(&a->archive, 341 a->client_data, buff, remaining); 342 if (bytes_written <= 0) 343 return (ARCHIVE_FATAL); 344 remaining -= bytes_written; 345 buff += bytes_written; 346 } 347 return (ARCHIVE_OK); 348 } 349 350 /* If the copy buffer isn't empty, try to fill it. */ 351 if (state->avail < state->buffer_size) { 352 /* If buffer is not empty... */ 353 /* ... copy data into buffer ... */ 354 to_copy = ((size_t)remaining > state->avail) ? 355 state->avail : (size_t)remaining; 356 memcpy(state->next, buff, to_copy); 357 state->next += to_copy; 358 state->avail -= to_copy; 359 buff += to_copy; 360 remaining -= to_copy; 361 /* ... if it's full, write it out. */ 362 if (state->avail == 0) { 363 char *p = state->buffer; 364 size_t to_write = state->buffer_size; 365 while (to_write > 0) { 366 bytes_written = (a->client_writer)(&a->archive, 367 a->client_data, p, to_write); 368 if (bytes_written <= 0) 369 return (ARCHIVE_FATAL); 370 if ((size_t)bytes_written > to_write) { 371 archive_set_error(&(a->archive), 372 -1, "write overrun"); 373 return (ARCHIVE_FATAL); 374 } 375 p += bytes_written; 376 to_write -= bytes_written; 377 } 378 state->next = state->buffer; 379 state->avail = state->buffer_size; 380 } 381 } 382 383 while ((size_t)remaining > state->buffer_size) { 384 /* Write out full blocks directly to client. */ 385 bytes_written = (a->client_writer)(&a->archive, 386 a->client_data, buff, state->buffer_size); 387 if (bytes_written <= 0) 388 return (ARCHIVE_FATAL); 389 buff += bytes_written; 390 remaining -= bytes_written; 391 } 392 393 if (remaining > 0) { 394 /* Copy last bit into copy buffer. */ 395 memcpy(state->next, buff, remaining); 396 state->next += remaining; 397 state->avail -= remaining; 398 } 399 return (ARCHIVE_OK); 400 } 401 402 static int 403 archive_write_client_close(struct archive_write_filter *f) 404 { 405 struct archive_write *a = (struct archive_write *)f->archive; 406 struct archive_none *state = (struct archive_none *)f->data; 407 ssize_t block_length; 408 ssize_t target_block_length; 409 ssize_t bytes_written; 410 int ret = ARCHIVE_OK; 411 412 /* If there's pending data, pad and write the last block */ 413 if (state->next != state->buffer) { 414 block_length = state->buffer_size - state->avail; 415 416 /* Tricky calculation to determine size of last block */ 417 if (a->bytes_in_last_block <= 0) 418 /* Default or Zero: pad to full block */ 419 target_block_length = a->bytes_per_block; 420 else 421 /* Round to next multiple of bytes_in_last_block. */ 422 target_block_length = a->bytes_in_last_block * 423 ( (block_length + a->bytes_in_last_block - 1) / 424 a->bytes_in_last_block); 425 if (target_block_length > a->bytes_per_block) 426 target_block_length = a->bytes_per_block; 427 if (block_length < target_block_length) { 428 memset(state->next, 0, 429 target_block_length - block_length); 430 block_length = target_block_length; 431 } 432 bytes_written = (a->client_writer)(&a->archive, 433 a->client_data, state->buffer, block_length); 434 ret = bytes_written <= 0 ? ARCHIVE_FATAL : ARCHIVE_OK; 435 } 436 if (a->client_closer) 437 (*a->client_closer)(&a->archive, a->client_data); 438 free(state->buffer); 439 free(state); 440 a->client_data = NULL; 441 return (ret); 442 } 443 444 /* 445 * Open the archive using the current settings. 446 */ 447 int 448 archive_write_open(struct archive *_a, void *client_data, 449 archive_open_callback *opener, archive_write_callback *writer, 450 archive_close_callback *closer) 451 { 452 struct archive_write *a = (struct archive_write *)_a; 453 struct archive_write_filter *client_filter; 454 int ret, r1; 455 456 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, 457 ARCHIVE_STATE_NEW, "archive_write_open"); 458 archive_clear_error(&a->archive); 459 460 a->client_writer = writer; 461 a->client_opener = opener; 462 a->client_closer = closer; 463 a->client_data = client_data; 464 465 client_filter = __archive_write_allocate_filter(_a); 466 client_filter->open = archive_write_client_open; 467 client_filter->write = archive_write_client_write; 468 client_filter->close = archive_write_client_close; 469 470 ret = __archive_write_open_filter(a->filter_first); 471 if (ret < ARCHIVE_WARN) { 472 r1 = __archive_write_close_filter(a->filter_first); 473 return (r1 < ret ? r1 : ret); 474 } 475 476 a->archive.state = ARCHIVE_STATE_HEADER; 477 if (a->format_init) 478 ret = (a->format_init)(a); 479 return (ret); 480 } 481 482 /* 483 * Close out the archive. 484 */ 485 static int 486 _archive_write_close(struct archive *_a) 487 { 488 struct archive_write *a = (struct archive_write *)_a; 489 int r = ARCHIVE_OK, r1 = ARCHIVE_OK; 490 491 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, 492 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, 493 "archive_write_close"); 494 if (a->archive.state == ARCHIVE_STATE_NEW 495 || a->archive.state == ARCHIVE_STATE_CLOSED) 496 return (ARCHIVE_OK); /* Okay to close() when not open. */ 497 498 archive_clear_error(&a->archive); 499 500 /* Finish the last entry. */ 501 if (a->archive.state == ARCHIVE_STATE_DATA) 502 r = ((a->format_finish_entry)(a)); 503 504 /* Finish off the archive. */ 505 /* TODO: have format closers invoke compression close. */ 506 if (a->format_close != NULL) { 507 r1 = (a->format_close)(a); 508 if (r1 < r) 509 r = r1; 510 } 511 512 /* Finish the compression and close the stream. */ 513 r1 = __archive_write_close_filter(a->filter_first); 514 if (r1 < r) 515 r = r1; 516 517 if (a->archive.state != ARCHIVE_STATE_FATAL) 518 a->archive.state = ARCHIVE_STATE_CLOSED; 519 return (r); 520 } 521 522 static int 523 _archive_write_filter_count(struct archive *_a) 524 { 525 struct archive_write *a = (struct archive_write *)_a; 526 struct archive_write_filter *p = a->filter_first; 527 int count = 0; 528 while(p) { 529 count++; 530 p = p->next_filter; 531 } 532 return count; 533 } 534 535 void 536 __archive_write_filters_free(struct archive *_a) 537 { 538 struct archive_write *a = (struct archive_write *)_a; 539 int r = ARCHIVE_OK, r1; 540 541 while (a->filter_first != NULL) { 542 struct archive_write_filter *next 543 = a->filter_first->next_filter; 544 if (a->filter_first->free != NULL) { 545 r1 = (*a->filter_first->free)(a->filter_first); 546 if (r > r1) 547 r = r1; 548 } 549 free(a->filter_first); 550 a->filter_first = next; 551 } 552 a->filter_last = NULL; 553 } 554 555 /* 556 * Destroy the archive structure. 557 * 558 * Be careful: user might just call write_new and then write_free. 559 * Don't assume we actually wrote anything or performed any non-trivial 560 * initialization. 561 */ 562 static int 563 _archive_write_free(struct archive *_a) 564 { 565 struct archive_write *a = (struct archive_write *)_a; 566 int r = ARCHIVE_OK, r1; 567 568 if (_a == NULL) 569 return (ARCHIVE_OK); 570 /* It is okay to call free() in state FATAL. */ 571 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, 572 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_free"); 573 if (a->archive.state != ARCHIVE_STATE_FATAL) 574 r = archive_write_close(&a->archive); 575 576 /* Release format resources. */ 577 if (a->format_free != NULL) { 578 r1 = (a->format_free)(a); 579 if (r1 < r) 580 r = r1; 581 } 582 583 __archive_write_filters_free(_a); 584 585 /* Release various dynamic buffers. */ 586 free((void *)(uintptr_t)(const void *)a->nulls); 587 archive_string_free(&a->archive.error_string); 588 a->archive.magic = 0; 589 __archive_clean(&a->archive); 590 free(a); 591 return (r); 592 } 593 594 /* 595 * Write the appropriate header. 596 */ 597 static int 598 _archive_write_header(struct archive *_a, struct archive_entry *entry) 599 { 600 struct archive_write *a = (struct archive_write *)_a; 601 int ret, r2; 602 603 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, 604 ARCHIVE_STATE_DATA | ARCHIVE_STATE_HEADER, "archive_write_header"); 605 archive_clear_error(&a->archive); 606 607 if (a->format_write_header == NULL) { 608 archive_set_error(&(a->archive), -1, 609 "Format must be set before you can write to an archive."); 610 a->archive.state = ARCHIVE_STATE_FATAL; 611 return (ARCHIVE_FATAL); 612 } 613 614 /* In particular, "retry" and "fatal" get returned immediately. */ 615 ret = archive_write_finish_entry(&a->archive); 616 if (ret == ARCHIVE_FATAL) { 617 a->archive.state = ARCHIVE_STATE_FATAL; 618 return (ARCHIVE_FATAL); 619 } 620 if (ret < ARCHIVE_OK && ret != ARCHIVE_WARN) 621 return (ret); 622 623 if (a->skip_file_set && 624 archive_entry_dev_is_set(entry) && 625 archive_entry_ino_is_set(entry) && 626 archive_entry_dev(entry) == a->skip_file_dev && 627 archive_entry_ino64(entry) == a->skip_file_ino) { 628 archive_set_error(&a->archive, 0, 629 "Can't add archive to itself"); 630 return (ARCHIVE_FAILED); 631 } 632 633 /* Format and write header. */ 634 r2 = ((a->format_write_header)(a, entry)); 635 if (r2 == ARCHIVE_FATAL) { 636 a->archive.state = ARCHIVE_STATE_FATAL; 637 return (ARCHIVE_FATAL); 638 } 639 if (r2 < ret) 640 ret = r2; 641 642 a->archive.state = ARCHIVE_STATE_DATA; 643 return (ret); 644 } 645 646 static int 647 _archive_write_finish_entry(struct archive *_a) 648 { 649 struct archive_write *a = (struct archive_write *)_a; 650 int ret = ARCHIVE_OK; 651 652 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, 653 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, 654 "archive_write_finish_entry"); 655 if (a->archive.state & ARCHIVE_STATE_DATA) 656 ret = (a->format_finish_entry)(a); 657 a->archive.state = ARCHIVE_STATE_HEADER; 658 return (ret); 659 } 660 661 /* 662 * Note that the compressor is responsible for blocking. 663 */ 664 static ssize_t 665 _archive_write_data(struct archive *_a, const void *buff, size_t s) 666 { 667 struct archive_write *a = (struct archive_write *)_a; 668 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, 669 ARCHIVE_STATE_DATA, "archive_write_data"); 670 archive_clear_error(&a->archive); 671 return ((a->format_write_data)(a, buff, s)); 672 } 673 674 static struct archive_write_filter * 675 filter_lookup(struct archive *_a, int n) 676 { 677 struct archive_write *a = (struct archive_write *)_a; 678 struct archive_write_filter *f = a->filter_first; 679 if (n == -1) 680 return a->filter_last; 681 if (n < 0) 682 return NULL; 683 while (n > 0 && f != NULL) { 684 f = f->next_filter; 685 --n; 686 } 687 return f; 688 } 689 690 static int 691 _archive_filter_code(struct archive *_a, int n) 692 { 693 struct archive_write_filter *f = filter_lookup(_a, n); 694 return f == NULL ? -1 : f->code; 695 } 696 697 static const char * 698 _archive_filter_name(struct archive *_a, int n) 699 { 700 struct archive_write_filter *f = filter_lookup(_a, n); 701 return f == NULL ? NULL : f->name; 702 } 703 704 static int64_t 705 _archive_filter_bytes(struct archive *_a, int n) 706 { 707 struct archive_write_filter *f = filter_lookup(_a, n); 708 return f == NULL ? -1 : f->bytes_written; 709 } 710