1 .\" Copyright (c) 2003-2011 Tim Kientzle
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 .\" $FreeBSD: head/lib/libarchive/archive_write.3 201110 2009-12-28 03:31:29Z kientzle $
32 .Nd functions for creating archives
36 These functions provide a complete API for creating streaming
38 The general process is to first create the
40 object, set any desired options, initialize the archive, append entries, then
41 close the archive and release all resources.
43 .Ss Create archive object
45 .Xr archive_write_new 3 .
47 To write an archive, you must first obtain an initialized
50 .Fn archive_write_new .
52 .Ss Enable filters and formats, configure block size and padding
54 .Xr archive_write_filter 3 ,
55 .Xr archive_write_format 3
57 .Xr archive_write_blocksize 3 .
59 You can then modify this object for the desired operations with the
61 .Fn archive_write_set_XXX
63 In particular, you will need to invoke appropriate
64 .Fn archive_write_add_XXX
66 .Fn archive_write_set_XXX
67 functions to enable the corresponding compression and format
72 .Xr archive_read_set_options 3 .
76 .Xr archive_write_open 3 .
78 Once you have prepared the
81 .Fn archive_write_open
82 to actually open the archive and prepare it for writing.
83 There are several variants of this function;
84 the most basic expects you to provide pointers to several
85 functions that can provide blocks of bytes from the archive.
86 There are convenience forms that allow you to
87 specify a filename, file descriptor,
89 object, or a block of memory from which to write the archive data.
93 .Xr archive_write_header 3
95 .Xr archive_write_data 3 .
97 Individual archive entries are written in a three-step
99 You first initialize a
100 .Tn struct archive_entry
101 structure with information about the new entry.
102 At a minimum, you should set the pathname of the
107 field, which specifies the type of object and
109 field, which specifies the size of the data portion of the object.
111 .Ss Release resources
113 .Xr archive_write_free 3 .
115 After all entries have been written, use the
116 .Fn archive_write_free
117 function to release all resources.
120 The following sketch illustrates basic usage of the library.
122 the callback functions are simply wrappers around the standard
128 .Bd -literal -offset indent
130 #define _FILE_OFFSET_BITS 64
132 #include <sys/stat.h>
134 #include <archive_entry.h>
145 myopen(struct archive *a, void *client_data)
147 struct mydata *mydata = client_data;
149 mydata->fd = open(mydata->name, O_WRONLY | O_CREAT, 0644);
153 return (ARCHIVE_FATAL);
157 mywrite(struct archive *a, void *client_data, const void *buff, size_t n)
159 struct mydata *mydata = client_data;
161 return (write(mydata->fd, buff, n));
165 myclose(struct archive *a, void *client_data)
167 struct mydata *mydata = client_data;
175 write_archive(const char *outname, const char **filename)
177 struct mydata *mydata = malloc(sizeof(struct mydata));
179 struct archive_entry *entry;
185 a = archive_write_new();
186 mydata->name = outname;
187 archive_write_add_filter_gzip(a);
188 archive_write_set_format_ustar(a);
189 archive_write_open(a, mydata, myopen, mywrite, myclose);
191 stat(*filename, &st);
192 entry = archive_entry_new();
193 archive_entry_copy_stat(entry, &st);
194 archive_entry_set_pathname(entry, *filename);
195 archive_write_header(a, entry);
196 if ((fd = open(*filename, O_RDONLY)) != -1) {
197 len = read(fd, buff, sizeof(buff));
199 archive_write_data(a, buff, len);
200 len = read(fd, buff, sizeof(buff));
204 archive_entry_free(entry);
207 archive_write_free(a);
210 int main(int argc, const char **argv)
215 write_archive(outname, argv);
222 .Xr archive_write_set_options 3 ,
229 library first appeared in
235 library was written by
236 .An Tim Kientzle Aq kientzle@acm.org .
238 There are many peculiar bugs in historic tar implementations that may cause
239 certain programs to reject archives written by this library.
240 For example, several historic implementations calculated header checksums
241 incorrectly and will thus reject valid archives; GNU tar does not fully support
242 pax interchange format; some old tar implementations required specific
245 The default pax interchange format eliminates most of the historic
246 tar limitations and provides a generic key/value attribute facility
247 for vendor-defined extensions.
248 One oversight in POSIX is the failure to provide a standard attribute
249 for large device numbers.
254 for device numbers that exceed the range supported by the backwards-compatible
256 These keys are compatible with Joerg Schilling's
259 Other implementations may not recognize these keys and will thus be unable
260 to correctly restore device nodes with large device numbers from archives
261 created by this library.