|
Packit Service |
1d0348 |
.TH ARCHIVE_WRITE 3 "February 2, 2012" ""
|
|
Packit Service |
1d0348 |
.SH NAME
|
|
Packit Service |
1d0348 |
.ad l
|
|
Packit Service |
1d0348 |
\fB\%archive_write\fP
|
|
Packit Service |
1d0348 |
\- functions for creating archives
|
|
Packit Service |
1d0348 |
.SH LIBRARY
|
|
Packit Service |
1d0348 |
.ad l
|
|
Packit Service |
1d0348 |
Streaming Archive Library (libarchive, -larchive)
|
|
Packit Service |
1d0348 |
.SH SYNOPSIS
|
|
Packit Service |
1d0348 |
.ad l
|
|
Packit Service |
1d0348 |
\fB#include <archive.h>\fP
|
|
Packit Service |
1d0348 |
.SH DESCRIPTION
|
|
Packit Service |
1d0348 |
.ad l
|
|
Packit Service |
1d0348 |
These functions provide a complete API for creating streaming
|
|
Packit Service |
1d0348 |
archive files.
|
|
Packit Service |
1d0348 |
The general process is to first create the
|
|
Packit Service |
1d0348 |
Tn struct archive
|
|
Packit Service |
1d0348 |
object, set any desired options, initialize the archive, append entries, then
|
|
Packit Service |
1d0348 |
close the archive and release all resources.
|
|
Packit Service |
1d0348 |
.SS Create archive object
|
|
Packit Service |
1d0348 |
See
|
|
Packit Service |
1d0348 |
\fBarchive_write_new\fP(3).
|
|
Packit Service |
1d0348 |
.PP
|
|
Packit Service |
1d0348 |
To write an archive, you must first obtain an initialized
|
|
Packit Service |
1d0348 |
Tn struct archive
|
|
Packit Service |
1d0348 |
object from
|
|
Packit Service |
1d0348 |
\fB\%archive_write_new\fP().
|
|
Packit Service |
1d0348 |
.SS Enable filters and formats, configure block size and padding
|
|
Packit Service |
1d0348 |
See
|
|
Packit Service |
1d0348 |
\fBarchive_write_filter\fP(3),
|
|
Packit Service |
1d0348 |
\fBarchive_write_format\fP(3)
|
|
Packit Service |
1d0348 |
and
|
|
Packit Service |
1d0348 |
\fBarchive_write_blocksize\fP(3).
|
|
Packit Service |
1d0348 |
.PP
|
|
Packit Service |
1d0348 |
You can then modify this object for the desired operations with the
|
|
Packit Service |
1d0348 |
various
|
|
Packit Service |
1d0348 |
\fB\%archive_write_set_XXX\fP()
|
|
Packit Service |
1d0348 |
functions.
|
|
Packit Service |
1d0348 |
In particular, you will need to invoke appropriate
|
|
Packit Service |
1d0348 |
\fB\%archive_write_add_XXX\fP()
|
|
Packit Service |
1d0348 |
and
|
|
Packit Service |
1d0348 |
\fB\%archive_write_set_XXX\fP()
|
|
Packit Service |
1d0348 |
functions to enable the corresponding compression and format
|
|
Packit Service |
1d0348 |
support.
|
|
Packit Service |
1d0348 |
.SS Set options
|
|
Packit Service |
1d0348 |
See
|
|
Packit Service |
1d0348 |
\fBarchive_write_set_options\fP(3).
|
|
Packit Service |
1d0348 |
.SS Open archive
|
|
Packit Service |
1d0348 |
See
|
|
Packit Service |
1d0348 |
\fBarchive_write_open\fP(3).
|
|
Packit Service |
1d0348 |
.PP
|
|
Packit Service |
1d0348 |
Once you have prepared the
|
|
Packit Service |
1d0348 |
Tn struct archive
|
|
Packit Service |
1d0348 |
object, you call
|
|
Packit Service |
1d0348 |
\fB\%archive_write_open\fP()
|
|
Packit Service |
1d0348 |
to actually open the archive and prepare it for writing.
|
|
Packit Service |
1d0348 |
There are several variants of this function;
|
|
Packit Service |
1d0348 |
the most basic expects you to provide pointers to several
|
|
Packit Service |
1d0348 |
functions that can provide blocks of bytes from the archive.
|
|
Packit Service |
1d0348 |
There are convenience forms that allow you to
|
|
Packit Service |
1d0348 |
specify a filename, file descriptor,
|
|
Packit Service |
1d0348 |
\fIFILE *\fP
|
|
Packit Service |
1d0348 |
object, or a block of memory from which to write the archive data.
|
|
Packit Service |
1d0348 |
.SS Produce archive
|
|
Packit Service |
1d0348 |
See
|
|
Packit Service |
1d0348 |
\fBarchive_write_header\fP(3)
|
|
Packit Service |
1d0348 |
and
|
|
Packit Service |
1d0348 |
\fBarchive_write_data\fP(3).
|
|
Packit Service |
1d0348 |
.PP
|
|
Packit Service |
1d0348 |
Individual archive entries are written in a three-step
|
|
Packit Service |
1d0348 |
process:
|
|
Packit Service |
1d0348 |
You first initialize a
|
|
Packit Service |
1d0348 |
Tn struct archive_entry
|
|
Packit Service |
1d0348 |
structure with information about the new entry.
|
|
Packit Service |
1d0348 |
At a minimum, you should set the pathname of the
|
|
Packit Service |
1d0348 |
entry and provide a
|
|
Packit Service |
1d0348 |
\fIstruct\fP stat
|
|
Packit Service |
1d0348 |
with a valid
|
|
Packit Service |
1d0348 |
\fIst_mode\fP
|
|
Packit Service |
1d0348 |
field, which specifies the type of object and
|
|
Packit Service |
1d0348 |
\fIst_size\fP
|
|
Packit Service |
1d0348 |
field, which specifies the size of the data portion of the object.
|
|
Packit Service |
1d0348 |
.SS Release resources
|
|
Packit Service |
1d0348 |
See
|
|
Packit Service |
1d0348 |
\fBarchive_write_free\fP(3).
|
|
Packit Service |
1d0348 |
.PP
|
|
Packit Service |
1d0348 |
After all entries have been written, use the
|
|
Packit Service |
1d0348 |
\fB\%archive_write_free\fP()
|
|
Packit Service |
1d0348 |
function to release all resources.
|
|
Packit Service |
1d0348 |
.SH EXAMPLE
|
|
Packit Service |
1d0348 |
.ad l
|
|
Packit Service |
1d0348 |
The following sketch illustrates basic usage of the library.
|
|
Packit Service |
1d0348 |
In this example,
|
|
Packit Service |
1d0348 |
the callback functions are simply wrappers around the standard
|
|
Packit Service |
1d0348 |
\fBopen\fP(2),
|
|
Packit Service |
1d0348 |
\fBwrite\fP(2),
|
|
Packit Service |
1d0348 |
and
|
|
Packit Service |
1d0348 |
\fBclose\fP(2)
|
|
Packit Service |
1d0348 |
system calls.
|
|
Packit Service |
1d0348 |
.RS 4
|
|
Packit Service |
1d0348 |
.nf
|
|
Packit Service |
1d0348 |
#ifdef __linux__
|
|
Packit Service |
1d0348 |
#define _FILE_OFFSET_BITS 64
|
|
Packit Service |
1d0348 |
#endif
|
|
Packit Service |
1d0348 |
#include <sys/stat.h>
|
|
Packit Service |
1d0348 |
#include <archive.h>
|
|
Packit Service |
1d0348 |
#include <archive_entry.h>
|
|
Packit Service |
1d0348 |
#include <fcntl.h>
|
|
Packit Service |
1d0348 |
#include <stdlib.h>
|
|
Packit Service |
1d0348 |
#include <unistd.h>
|
|
Packit Service |
1d0348 |
struct mydata {
|
|
Packit Service |
1d0348 |
const char *name;
|
|
Packit Service |
1d0348 |
int fd;
|
|
Packit Service |
1d0348 |
};
|
|
Packit Service |
1d0348 |
int
|
|
Packit Service |
1d0348 |
myopen(struct archive *a, void *client_data)
|
|
Packit Service |
1d0348 |
{
|
|
Packit Service |
1d0348 |
struct mydata *mydata = client_data;
|
|
Packit Service |
1d0348 |
mydata->fd = open(mydata->name, O_WRONLY | O_CREAT, 0644);
|
|
Packit Service |
1d0348 |
if (mydata->fd >= 0)
|
|
Packit Service |
1d0348 |
return (ARCHIVE_OK);
|
|
Packit Service |
1d0348 |
else
|
|
Packit Service |
1d0348 |
return (ARCHIVE_FATAL);
|
|
Packit Service |
1d0348 |
}
|
|
Packit Service |
1d0348 |
la_ssize_t
|
|
Packit Service |
1d0348 |
mywrite(struct archive *a, void *client_data, const void *buff, size_t n)
|
|
Packit Service |
1d0348 |
{
|
|
Packit Service |
1d0348 |
struct mydata *mydata = client_data;
|
|
Packit Service |
1d0348 |
return (write(mydata->fd, buff, n));
|
|
Packit Service |
1d0348 |
}
|
|
Packit Service |
1d0348 |
int
|
|
Packit Service |
1d0348 |
myclose(struct archive *a, void *client_data)
|
|
Packit Service |
1d0348 |
{
|
|
Packit Service |
1d0348 |
struct mydata *mydata = client_data;
|
|
Packit Service |
1d0348 |
if (mydata->fd > 0)
|
|
Packit Service |
1d0348 |
close(mydata->fd);
|
|
Packit Service |
1d0348 |
return (0);
|
|
Packit Service |
1d0348 |
}
|
|
Packit Service |
1d0348 |
void
|
|
Packit Service |
1d0348 |
write_archive(const char *outname, const char **filename)
|
|
Packit Service |
1d0348 |
{
|
|
Packit Service |
1d0348 |
struct mydata *mydata = malloc(sizeof(struct mydata));
|
|
Packit Service |
1d0348 |
struct archive *a;
|
|
Packit Service |
1d0348 |
struct archive_entry *entry;
|
|
Packit Service |
1d0348 |
struct stat st;
|
|
Packit Service |
1d0348 |
char buff[8192];
|
|
Packit Service |
1d0348 |
int len;
|
|
Packit Service |
1d0348 |
int fd;
|
|
Packit Service |
1d0348 |
a = archive_write_new();
|
|
Packit Service |
1d0348 |
mydata->name = outname;
|
|
Packit Service |
1d0348 |
/* Set archive format and filter according to output file extension.
|
|
Packit Service |
1d0348 |
* If it fails, set default format. Platform depended function.
|
|
Packit Service |
1d0348 |
* See supported formats in archive_write_set_format_filter_by_ext.c */
|
|
Packit Service |
1d0348 |
if (archive_write_set_format_filter_by_ext(a, outname) != ARCHIVE_OK) {
|
|
Packit Service |
1d0348 |
archive_write_add_filter_gzip(a);
|
|
Packit Service |
1d0348 |
archive_write_set_format_ustar(a);
|
|
Packit Service |
1d0348 |
}
|
|
Packit Service |
1d0348 |
archive_write_open(a, mydata, myopen, mywrite, myclose);
|
|
Packit Service |
1d0348 |
while (*filename) {
|
|
Packit Service |
1d0348 |
stat(*filename, &st);
|
|
Packit Service |
1d0348 |
entry = archive_entry_new();
|
|
Packit Service |
1d0348 |
archive_entry_copy_stat(entry, &st);
|
|
Packit Service |
1d0348 |
archive_entry_set_pathname(entry, *filename);
|
|
Packit Service |
1d0348 |
archive_write_header(a, entry);
|
|
Packit Service |
1d0348 |
if ((fd = open(*filename, O_RDONLY)) != -1) {
|
|
Packit Service |
1d0348 |
len = read(fd, buff, sizeof(buff));
|
|
Packit Service |
1d0348 |
while (len > 0) {
|
|
Packit Service |
1d0348 |
archive_write_data(a, buff, len);
|
|
Packit Service |
1d0348 |
len = read(fd, buff, sizeof(buff));
|
|
Packit Service |
1d0348 |
}
|
|
Packit Service |
1d0348 |
close(fd);
|
|
Packit Service |
1d0348 |
}
|
|
Packit Service |
1d0348 |
archive_entry_free(entry);
|
|
Packit Service |
1d0348 |
filename++;
|
|
Packit Service |
1d0348 |
}
|
|
Packit Service |
1d0348 |
archive_write_free(a);
|
|
Packit Service |
1d0348 |
}
|
|
Packit Service |
1d0348 |
int main(int argc, const char **argv)
|
|
Packit Service |
1d0348 |
{
|
|
Packit Service |
1d0348 |
const char *outname;
|
|
Packit Service |
1d0348 |
argv++;
|
|
Packit Service |
1d0348 |
outname = *argv++;
|
|
Packit Service |
1d0348 |
write_archive(outname, argv);
|
|
Packit Service |
1d0348 |
return 0;
|
|
Packit Service |
1d0348 |
}
|
|
Packit Service |
1d0348 |
.RE
|
|
Packit Service |
1d0348 |
.SH SEE ALSO
|
|
Packit Service |
1d0348 |
.ad l
|
|
Packit Service |
1d0348 |
\fBtar\fP(1),
|
|
Packit Service |
1d0348 |
\fBlibarchive\fP(3),
|
|
Packit Service |
1d0348 |
\fBarchive_write_set_options\fP(3),
|
|
Packit Service |
1d0348 |
\fBcpio\fP(5),
|
|
Packit Service |
1d0348 |
\fBmtree\fP(5),
|
|
Packit Service |
1d0348 |
\fBtar\fP(5)
|
|
Packit Service |
1d0348 |
.SH HISTORY
|
|
Packit Service |
1d0348 |
.ad l
|
|
Packit Service |
1d0348 |
The
|
|
Packit Service |
1d0348 |
\fB\%libarchive\fP
|
|
Packit Service |
1d0348 |
library first appeared in
|
|
Packit Service |
1d0348 |
FreeBSD 5.3.
|
|
Packit Service |
1d0348 |
.SH AUTHORS
|
|
Packit Service |
1d0348 |
.ad l
|
|
Packit Service |
1d0348 |
-nosplit
|
|
Packit Service |
1d0348 |
The
|
|
Packit Service |
1d0348 |
\fB\%libarchive\fP
|
|
Packit Service |
1d0348 |
library was written by
|
|
Packit Service |
1d0348 |
Tim Kientzle \%<kientzle@acm.org.>
|
|
Packit Service |
1d0348 |
.SH BUGS
|
|
Packit Service |
1d0348 |
.ad l
|
|
Packit Service |
1d0348 |
There are many peculiar bugs in historic tar implementations that may cause
|
|
Packit Service |
1d0348 |
certain programs to reject archives written by this library.
|
|
Packit Service |
1d0348 |
For example, several historic implementations calculated header checksums
|
|
Packit Service |
1d0348 |
incorrectly and will thus reject valid archives; GNU tar does not fully support
|
|
Packit Service |
1d0348 |
pax interchange format; some old tar implementations required specific
|
|
Packit Service |
1d0348 |
field terminations.
|
|
Packit Service |
1d0348 |
.PP
|
|
Packit Service |
1d0348 |
The default pax interchange format eliminates most of the historic
|
|
Packit Service |
1d0348 |
tar limitations and provides a generic key/value attribute facility
|
|
Packit Service |
1d0348 |
for vendor-defined extensions.
|
|
Packit Service |
1d0348 |
One oversight in POSIX is the failure to provide a standard attribute
|
|
Packit Service |
1d0348 |
for large device numbers.
|
|
Packit Service |
1d0348 |
This library uses
|
|
Packit Service |
1d0348 |
``SCHILY.devminor''
|
|
Packit Service |
1d0348 |
and
|
|
Packit Service |
1d0348 |
``SCHILY.devmajor''
|
|
Packit Service |
1d0348 |
for device numbers that exceed the range supported by the backwards-compatible
|
|
Packit Service |
1d0348 |
ustar header.
|
|
Packit Service |
1d0348 |
These keys are compatible with Joerg Schilling's
|
|
Packit Service |
1d0348 |
\fB\%star\fP
|
|
Packit Service |
1d0348 |
archiver.
|
|
Packit Service |
1d0348 |
Other implementations may not recognize these keys and will thus be unable
|
|
Packit Service |
1d0348 |
to correctly restore device nodes with large device numbers from archives
|
|
Packit Service |
1d0348 |
created by this library.
|