diff options
author | kientzle <kientzle@FreeBSD.org> | 2007-03-03 07:37:37 +0000 |
---|---|---|
committer | kientzle <kientzle@FreeBSD.org> | 2007-03-03 07:37:37 +0000 |
commit | 1a60578adb6be1f7da9ed95f38cf52c20be13591 (patch) | |
tree | 1b026213a337089af577dee3ed6d194ee1b87d88 /lib/libarchive/archive_write.c | |
parent | 56746aa8e0ffff51b371e72446368d6dbbe6b4ba (diff) | |
download | FreeBSD-src-1a60578adb6be1f7da9ed95f38cf52c20be13591.zip FreeBSD-src-1a60578adb6be1f7da9ed95f38cf52c20be13591.tar.gz |
libarchive 2.0
* libarchive_test program exercises many of the core features
* Refactored old "read_extract" into new "archive_write_disk", which
uses archive_write methods to put entries onto disk. In particular,
you can now use archive_write_disk to create objects on disk
without having an archive available.
* Pushed some security checks from bsdtar down into libarchive, where
they can be better optimized.
* Rearchitected the logic for creating objects on disk to reduce
the number of system calls. Several common cases now use a
minimum number of system calls.
* Virtualized some internal interfaces to provide a clearer separation
of read and write handling and make it simpler to override key
methods.
* New "empty" format reader.
* Corrected return types (this ABI breakage required the "2.0" version bump)
* Many bug fixes.
Diffstat (limited to 'lib/libarchive/archive_write.c')
-rw-r--r-- | lib/libarchive/archive_write.c | 154 |
1 files changed, 104 insertions, 50 deletions
diff --git a/lib/libarchive/archive_write.c b/lib/libarchive/archive_write.c index 0aff2f1..e68b0b8 100644 --- a/lib/libarchive/archive_write.c +++ b/lib/libarchive/archive_write.c @@ -55,6 +55,31 @@ __FBSDID("$FreeBSD$"); #include "archive.h" #include "archive_entry.h" #include "archive_private.h" +#include "archive_write_private.h" + +static struct archive_vtable *archive_write_vtable(void); + +static int _archive_write_close(struct archive *); +static int _archive_write_finish(struct archive *); +static int _archive_write_header(struct archive *, struct archive_entry *); +static int _archive_write_finish_entry(struct archive *); +static ssize_t _archive_write_data(struct archive *, const void *, size_t); + +static struct archive_vtable * +archive_write_vtable(void) +{ + static struct archive_vtable av; + static int inited = 0; + + if (!inited) { + av.archive_write_close = _archive_write_close; + av.archive_write_finish = _archive_write_finish; + av.archive_write_header = _archive_write_header; + av.archive_write_finish_entry = _archive_write_finish_entry; + av.archive_write_data = _archive_write_data; + } + return (&av); +} /* * Allocate, initialize and return an archive object. @@ -62,18 +87,18 @@ __FBSDID("$FreeBSD$"); struct archive * archive_write_new(void) { - struct archive *a; + struct archive_write *a; unsigned char *nulls; - a = (struct archive *)malloc(sizeof(*a)); + a = (struct archive_write *)malloc(sizeof(*a)); if (a == NULL) return (NULL); memset(a, 0, sizeof(*a)); - a->magic = ARCHIVE_WRITE_MAGIC; - a->user_uid = geteuid(); + a->archive.magic = ARCHIVE_WRITE_MAGIC; + a->archive.state = ARCHIVE_STATE_NEW; + a->archive.vtable = archive_write_vtable(); a->bytes_per_block = ARCHIVE_DEFAULT_BYTES_PER_BLOCK; a->bytes_in_last_block = -1; /* Default */ - a->state = ARCHIVE_STATE_NEW; a->pformat_data = &(a->format_data); /* Initialize a block of nulls for padding purposes. */ @@ -91,17 +116,19 @@ archive_write_new(void) * client to link in support for that format, even if they didn't * ever use it. */ - archive_write_set_compression_none(a); - return (a); + archive_write_set_compression_none(&a->archive); + return (&a->archive); } /* * Set the block size. Returns 0 if successful. */ int -archive_write_set_bytes_per_block(struct archive *a, int bytes_per_block) +archive_write_set_bytes_per_block(struct archive *_a, int bytes_per_block) { - __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW, "archive_write_set_bytes_per_block"); + struct archive_write *a = (struct archive_write *)_a; + __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, + ARCHIVE_STATE_NEW, "archive_write_set_bytes_per_block"); a->bytes_per_block = bytes_per_block; return (ARCHIVE_OK); } @@ -110,9 +137,11 @@ archive_write_set_bytes_per_block(struct archive *a, int bytes_per_block) * Get the current block size. -1 if it has never been set. */ int -archive_write_get_bytes_per_block(struct archive *a) +archive_write_get_bytes_per_block(struct archive *_a) { - __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_get_bytes_per_block"); + struct archive_write *a = (struct archive_write *)_a; + __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, + ARCHIVE_STATE_ANY, "archive_write_get_bytes_per_block"); return (a->bytes_per_block); } @@ -121,9 +150,11 @@ archive_write_get_bytes_per_block(struct archive *a) * Returns 0 if successful. */ int -archive_write_set_bytes_in_last_block(struct archive *a, int bytes) +archive_write_set_bytes_in_last_block(struct archive *_a, int bytes) { - __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_set_bytes_in_last_block"); + struct archive_write *a = (struct archive_write *)_a; + __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, + ARCHIVE_STATE_ANY, "archive_write_set_bytes_in_last_block"); a->bytes_in_last_block = bytes; return (ARCHIVE_OK); } @@ -132,9 +163,11 @@ archive_write_set_bytes_in_last_block(struct archive *a, int bytes) * Return the value set above. -1 indicates it has not been set. */ int -archive_write_get_bytes_in_last_block(struct archive *a) +archive_write_get_bytes_in_last_block(struct archive *_a) { - __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_get_bytes_in_last_block"); + struct archive_write *a = (struct archive_write *)_a; + __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, + ARCHIVE_STATE_ANY, "archive_write_get_bytes_in_last_block"); return (a->bytes_in_last_block); } @@ -144,9 +177,11 @@ archive_write_get_bytes_in_last_block(struct archive *a) * an archive to itself recursively. */ int -archive_write_set_skip_file(struct archive *a, dev_t d, ino_t i) +archive_write_set_skip_file(struct archive *_a, dev_t d, ino_t i) { - __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_set_skip_file"); + struct archive_write *a = (struct archive_write *)_a; + __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, + ARCHIVE_STATE_ANY, "archive_write_set_skip_file"); a->skip_file_dev = d; a->skip_file_ino = i; return (ARCHIVE_OK); @@ -157,16 +192,18 @@ archive_write_set_skip_file(struct archive *a, dev_t d, ino_t i) * Open the archive using the current settings. */ int -archive_write_open(struct archive *a, void *client_data, +archive_write_open(struct archive *_a, void *client_data, archive_open_callback *opener, archive_write_callback *writer, archive_close_callback *closer) { + struct archive_write *a = (struct archive_write *)_a; int ret; ret = ARCHIVE_OK; - __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW, "archive_write_open"); - archive_string_empty(&a->error_string); - a->state = ARCHIVE_STATE_HEADER; + __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, + ARCHIVE_STATE_NEW, "archive_write_open"); + archive_clear_error(&a->archive); + a->archive.state = ARCHIVE_STATE_HEADER; a->client_data = client_data; a->client_writer = writer; a->client_opener = opener; @@ -185,15 +222,17 @@ archive_write_open(struct archive *a, void *client_data, * Don't assume we actually wrote anything or performed any non-trivial * initialization. */ -int -archive_write_close(struct archive *a) +static int +_archive_write_close(struct archive *_a) { + struct archive_write *a = (struct archive_write *)_a; int r = ARCHIVE_OK, r1 = ARCHIVE_OK; - __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_close"); + __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, + ARCHIVE_STATE_ANY, "archive_write_close"); /* Finish the last entry. */ - if (a->state & ARCHIVE_STATE_DATA) + if (a->archive.state & ARCHIVE_STATE_DATA) r = ((a->format_finish_entry)(a)); /* Finish off the archive. */ @@ -203,6 +242,13 @@ archive_write_close(struct archive *a) r = r1; } + /* Release resources. */ + if (a->format_destroy != NULL) { + r1 = (a->format_destroy)(a); + if (r1 < r) + r = r1; + } + /* Finish the compression and close the stream. */ if (a->compression_finish != NULL) { r1 = (a->compression_finish)(a); @@ -210,41 +256,47 @@ archive_write_close(struct archive *a) r = r1; } - a->state = ARCHIVE_STATE_CLOSED; + a->archive.state = ARCHIVE_STATE_CLOSED; return (r); } /* * Destroy the archive structure. */ -void -archive_write_finish(struct archive *a) +static int +_archive_write_finish(struct archive *_a) { - __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_finish"); - if (a->state != ARCHIVE_STATE_CLOSED) - archive_write_close(a); + struct archive_write *a = (struct archive_write *)_a; + int r = ARCHIVE_OK; + + __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, + ARCHIVE_STATE_ANY, "archive_write_finish"); + if (a->archive.state != ARCHIVE_STATE_CLOSED) + r = archive_write_close(&a->archive); /* Release various dynamic buffers. */ free((void *)(uintptr_t)(const void *)a->nulls); - archive_string_free(&a->error_string); - a->magic = 0; + archive_string_free(&a->archive.error_string); + a->archive.magic = 0; free(a); + return (r); } /* * Write the appropriate header. */ -int -archive_write_header(struct archive *a, struct archive_entry *entry) +static int +_archive_write_header(struct archive *_a, struct archive_entry *entry) { + struct archive_write *a = (struct archive_write *)_a; int ret, r2; - __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, + __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_DATA | ARCHIVE_STATE_HEADER, "archive_write_header"); - archive_string_empty(&a->error_string); + archive_clear_error(&a->archive); /* In particular, "retry" and "fatal" get returned immediately. */ - ret = archive_write_finish_entry(a); + ret = archive_write_finish_entry(&a->archive); if (ret < ARCHIVE_OK && ret != ARCHIVE_WARN) return (ret); @@ -252,7 +304,8 @@ archive_write_header(struct archive *a, struct archive_entry *entry) archive_entry_dev(entry) == a->skip_file_dev && a->skip_file_ino != 0 && archive_entry_ino(entry) == a->skip_file_ino) { - archive_set_error(a, 0, "Can't add archive to itself"); + archive_set_error(&a->archive, 0, + "Can't add archive to itself"); return (ARCHIVE_WARN); } @@ -261,33 +314,34 @@ archive_write_header(struct archive *a, struct archive_entry *entry) if (r2 < ret) ret = r2; - a->state = ARCHIVE_STATE_DATA; + a->archive.state = ARCHIVE_STATE_DATA; return (ret); } -int -archive_write_finish_entry(struct archive * a) +static int +_archive_write_finish_entry(struct archive *_a) { + struct archive_write *a = (struct archive_write *)_a; int ret = ARCHIVE_OK; - __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, + __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, "archive_write_finish_entry"); - if (a->state & ARCHIVE_STATE_DATA) + if (a->archive.state & ARCHIVE_STATE_DATA) ret = (a->format_finish_entry)(a); - a->state = ARCHIVE_STATE_HEADER; + a->archive.state = ARCHIVE_STATE_HEADER; return (ret); } /* * Note that the compressor is responsible for blocking. */ -/* Should be "ssize_t", but that breaks the ABI. <sigh> */ -int -archive_write_data(struct archive *a, const void *buff, size_t s) +static ssize_t +_archive_write_data(struct archive *_a, const void *buff, size_t s) { - __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, + struct archive_write *a = (struct archive_write *)_a; + __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_DATA, "archive_write_data"); - archive_string_empty(&a->error_string); + archive_clear_error(&a->archive); return ((a->format_write_data)(a, buff, s)); } |