struct
archive
*
archive_write_new(
void
)
int
archive_write_get_bytes_per_block(
struct archive *
)
int
archive_write_set_bytes_per_block(
struct archive *
, int bytes_per_block
)
int
archive_write_set_bytes_in_last_block(
struct archive *
, int
)
int
archive_write_set_compression_bzip2(
struct archive *
)
int
archive_write_set_compression_compress(
struct archive *
)
int
archive_write_set_compression_gzip(
struct archive *
)
int
archive_write_set_compression_none(
struct archive *
)
int
archive_write_set_compression_program(
struct archive *
const char * cmd
)
int
archive_write_set_format_cpio(
struct archive *
)
int
archive_write_set_format_pax(
struct archive *
)
int
archive_write_set_format_pax_restricted(
struct archive *
)
int
archive_write_set_format_shar(
struct archive *
)
int
archive_write_set_format_shar_binary(
struct archive *
)
int
archive_write_set_format_ustar(
struct archive *
)
int
archive_write_open(
struct archive *
void *client_data
archive_open_callback *
archive_write_callback *
archive_close_callback *
)
int
archive_write_open_fd(
struct archive *
, int fd
)
int
archive_write_open_FILE(
struct archive *
, FILE *file
)
int
archive_write_open_filename(
struct archive *
, const char *filename
)
int
archive_write_open_memory(
struct archive *
void *buffer
size_t bufferSize
size_t *outUsed
)
int
archive_write_header(
struct archive *
, struct archive_entry *
)
ssize_t
archive_write_data(
struct archive *
, const void *
, size_t
)
int
archive_write_finish_entry(
struct archive *
)
int
archive_write_close(
struct archive *
)
int
archive_write_finish(
struct archive *
)
)
)
)
)
)
will set this based on the file type).
Unlike the other
``set''
functions, this function can be called after the archive is opened.
)
),
archive_write_set_format_pax(
),
archive_write_set_format_pax_restricted(
),
archive_write_set_format_shar(
),
archive_write_set_format_shar_binary(
),
archive_write_set_format_ustar(
)
Sets the format that will be used for the archive.
The library can write
POSIX octet-oriented cpio format archives,
POSIX-standard
``pax interchange''
format archives,
traditional
``shar''
archives,
enhanced
``binary''
shar archives that store a variety of file attributes and handle binary files,
and
POSIX-standard
``ustar''
archives.
The pax interchange format is a backwards-compatible tar format that
adds key/value attributes to each entry and supports arbitrary
filenames, linknames, uids, sizes, etc.
``Restricted pax interchange format''
is the library default; this is the same as pax format, but suppresses
the pax extended header for most normal files.
In most cases, this will result in ordinary ustar archives.
),
archive_write_set_compression_compress(
),
archive_write_set_compression_gzip(
),
archive_write_set_compression_none(
)
The resulting archive will be compressed as specified.
Note that the compressed output is always properly blocked.
)
)
)
)
that accepts a file descriptor.
The
archive_write_open_fd(
)
function is safe for use with tape drives or other
block-oriented devices.
)
)
that accepts a
FILE *
pointer.
Note that
archive_write_open_FILE(
)
is not safe for writing to tape drives or other devices
that require correct blocking.
)
).
)
)
that accepts a filename.
A NULL argument indicates that the output should be written to standard output;
an argument of
``-''
will open a file with that name.
If you have not invoked
archive_write_set_bytes_in_last_block(
),
then
archive_write_open_filename(
)
will adjust the last-block padding depending on the file:
it will enable padding when writing to standard output or
to a character or block device node, it will disable padding otherwise.
You can override this by manually invoking
archive_write_set_bytes_in_last_block(
)
before calling
archive_write_open(
).
The
archive_write_open_filename(
)
function is safe for use with tape drives or other
block-oriented devices.
)
)
that accepts a pointer to a block of memory that will receive
the archive.
The final
size_t *
argument points to a variable that will be updated
after each write to reflect how much of the buffer
is currently in use.
You should be careful to ensure that this variable
remains allocated until after the archive is
closed.
)
)
)
)
and
archive_write_close(
)
as needed.
)
)
)
if it was not invoked manually, then releases all resources.
Note that this function was declared to return
void
in libarchive 1.x, which made it impossible to detect errors when
archive_write_close(
)
was invoked implicitly from this function.
This is corrected beginning with libarchive 2.0.
):
typedef
int
archive_open_callback(
struct archive *
, void *client_data
)
The open callback is invoked by
archive_write_open().
It should return
ARCHIVE_OK
if the underlying file or data source is successfully
opened.
If the open fails, it should call
archive_set_error(
)
to register an error code and message and return
ARCHIVE_FATAL.
typedef
ssize_t
archive_write_callback(
struct archive *
void *client_data
void *buffer
size_t length
)
The write callback is invoked whenever the library
needs to write raw bytes to the archive.
For correct blocking, each call to the write callback function
should translate into a single
write(2)
system call.
This is especially critical when writing archives to tape drives.
On success, the write callback should return the
number of bytes actually written.
On error, the callback should invoke
archive_set_error()
to register an error code and message and return -1.
typedef
int
archive_close_callback(
struct archive *
, void *client_data
)
The close callback is invoked by archive_close when
the archive processing is complete.
The callback should return
ARCHIVE_OK
on success.
On failure, the callback should invoke
archive_set_error()
to register an error code and message and
return
ARCHIVE_FATAL.
#include
#include
#include
#include
#include
#include
struct mydata {
const char *name;
int fd;
};
int
myopen(struct archive *a, void *client_data)
{
struct mydata *mydata = client_data;
mydata->fd = open(mydata->name, O_WRONLY | O_CREAT, 0644);
if (mydata->fd >= 0)
return (ARCHIVE_OK);
else
return (ARCHIVE_FATAL);
}
ssize_t
mywrite(struct archive *a, void *client_data, void *buff, size_t n)
{
struct mydata *mydata = client_data;
return (write(mydata->fd, buff, n));
}
int
myclose(struct archive *a, void *client_data)
{
struct mydata *mydata = client_data;
if (mydata->fd > 0)
close(mydata->fd);
return (0);
}
void
write_archive(const char *outname, const char **filename)
{
struct mydata *mydata = malloc(sizeof(struct mydata));
struct archive *a;
struct archive_entry *entry;
struct stat st;
char buff[8192];
int len;
int fd;
a = archive_write_new();
mydata->name = outname;
archive_write_set_compression_gzip(a);
archive_write_set_format_ustar(a);
archive_write_open(a, mydata, myopen, mywrite, myclose);
while (*filename) {
stat(*filename, &st);
entry = archive_entry_new();
archive_entry_copy_stat(entry, &st);
archive_entry_set_pathname(entry, *filename);
archive_write_header(a, entry);
fd = open(*filename, O_RDONLY);
len = read(fd, buff, sizeof(buff));
while ( len > 0 ) {
archive_write_data(a, buff, len);
len = read(fd, buff, sizeof(buff));
}
archive_entry_free(entry);
filename++;
}
archive_write_finish(a);
}
int main(int argc, const char **argv)
{
const char *outname;
argv++;
outname = argv++;
write_archive(outname, argv);
return 0;
}
)
and
archive_error_string(
)
functions can be used to retrieve an appropriate error code and a
textual error message.
archive_write_new()
returns a pointer to a newly-allocated
struct archive
object.
archive_write_data()
returns a count of the number of bytes actually written.
On error, -1 is returned and the
archive_errno(
)
and
archive_error_string(
)
functions will return appropriate values.
Note that if the client-provided write callback function
returns a non-zero value, that error will be propagated back to the caller
through whatever API function resulted in that call, which
may include
archive_write_header(
),
archive_write_data(
),
archive_write_close(
),
or
archive_write_finish(
).
The client callback can call
archive_set_error(
)
to provide values that can then be retrieved by
archive_errno(
)
and
archive_error_string(
).
The libarchive library was written by Tim Kientzle <kientzle@acm.org>.
The default pax interchange format eliminates most of the historic tar limitations and provides a generic key/value attribute facility for vendor-defined extensions. One oversight in POSIX is the failure to provide a standard attribute for large device numbers. This library uses ``SCHILY.devminor'' and ``SCHILY.devmajor'' for device numbers that exceed the range supported by the backwards-compatible ustar header. These keys are compatible with Joerg Schilling's star archiver. Other implementations may not recognize these keys and will thus be unable to correctly restore device nodes with large device numbers from archives created by this library.