Default kdump's data limit to 64 bytes and document how it can be disabled.
[dragonfly.git] / contrib / libarchive / archive_write.c
1 /*-
2  * Copyright (c) 2003-2004 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  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_write.c,v 1.14 2005/02/12 23:11:29 kientzle Exp $");
29
30 /*
31  * This file contains the "essential" portions of the write API, that
32  * is, stuff that will essentially always be used by any client that
33  * actually needs to write a archive.  Optional pieces have been, as
34  * far as possible, separated out into separate files to reduce
35  * needlessly bloating statically-linked clients.
36  */
37
38 #include <sys/wait.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44 #include <unistd.h>
45
46 #include "archive.h"
47 #include "archive_entry.h"
48 #include "archive_private.h"
49
50 extern char             **environ;
51
52 /*
53  * Allocate, initialize and return an archive object.
54  */
55 struct archive *
56 archive_write_new(void)
57 {
58         struct archive *a;
59         char *nulls;
60
61         a = malloc(sizeof(*a));
62         if (a == NULL)
63                 return (NULL);
64         memset(a, 0, sizeof(*a));
65         a->magic = ARCHIVE_WRITE_MAGIC;
66         a->user_uid = geteuid();
67         a->bytes_per_block = ARCHIVE_DEFAULT_BYTES_PER_BLOCK;
68         a->bytes_in_last_block = -1;    /* Default */
69         a->state = ARCHIVE_STATE_NEW;
70         a->pformat_data = &(a->format_data);
71
72         /* Initialize a block of nulls for padding purposes. */
73         a->null_length = 1024;
74         nulls = malloc(a->null_length);
75         if (nulls == NULL) {
76                 free(a);
77                 return (NULL);
78         }
79         memset(nulls, 0, a->null_length);
80         a->nulls = nulls;
81         /*
82          * Set default compression, but don't set a default format.
83          * Were we to set a default format here, we would force every
84          * client to link in support for that format, even if they didn't
85          * ever use it.
86          */
87         archive_write_set_compression_none(a);
88         return (a);
89 }
90
91
92 /*
93  * Set the block size.  Returns 0 if successful.
94  */
95 int
96 archive_write_set_bytes_per_block(struct archive *a, int bytes_per_block)
97 {
98         archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW);
99         a->bytes_per_block = bytes_per_block;
100         return (ARCHIVE_OK);
101 }
102
103
104 /*
105  * Set the size for the last block.
106  * Returns 0 if successful.
107  */
108 int
109 archive_write_set_bytes_in_last_block(struct archive *a, int bytes)
110 {
111         archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY);
112         a->bytes_in_last_block = bytes;
113         return (ARCHIVE_OK);
114 }
115
116
117 /*
118  * Open the archive using the current settings.
119  */
120 int
121 archive_write_open(struct archive *a, void *client_data,
122     archive_open_callback *opener, archive_write_callback *writer,
123     archive_close_callback *closer)
124 {
125         int ret;
126
127         ret = ARCHIVE_OK;
128         archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW);
129         archive_string_empty(&a->error_string);
130         a->state = ARCHIVE_STATE_HEADER;
131         a->client_data = client_data;
132         a->client_writer = writer;
133         a->client_opener = opener;
134         a->client_closer = closer;
135         ret = (a->compression_init)(a);
136         if (a->format_init && ret == ARCHIVE_OK)
137                 ret = (a->format_init)(a);
138         return (ret);
139 }
140
141
142 /*
143  * Close out the archive.
144  *
145  * Be careful: user might just call write_new and then write_finish.
146  * Don't assume we actually wrote anything or performed any non-trivial
147  * initialization.
148  */
149 int
150 archive_write_close(struct archive *a)
151 {
152         archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY);
153
154         /* Finish the last entry. */
155         if (a->state & ARCHIVE_STATE_DATA)
156                 ((a->format_finish_entry)(a));
157
158         /* Finish off the archive. */
159         if (a->format_finish != NULL)
160                 (a->format_finish)(a);
161
162         /* Finish the compression and close the stream. */
163         if (a->compression_finish != NULL)
164                 (a->compression_finish)(a);
165
166         a->state = ARCHIVE_STATE_CLOSED;
167         return (ARCHIVE_OK);
168 }
169
170 /*
171  * Destroy the archive structure.
172  */
173 void
174 archive_write_finish(struct archive *a)
175 {
176         archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY);
177         if (a->state != ARCHIVE_STATE_CLOSED)
178                 archive_write_close(a);
179
180         /* Release various dynamic buffers. */
181         free((void *)(uintptr_t)(const void *)a->nulls);
182         archive_string_free(&a->error_string);
183         a->magic = 0;
184         free(a);
185 }
186
187
188 /*
189  * Write the appropriate header.
190  */
191 int
192 archive_write_header(struct archive *a, struct archive_entry *entry)
193 {
194         int ret;
195
196         archive_check_magic(a, ARCHIVE_WRITE_MAGIC,
197             ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA);
198         archive_string_empty(&a->error_string);
199
200         /* Finish last entry. */
201         if (a->state & ARCHIVE_STATE_DATA)
202                 ((a->format_finish_entry)(a));
203
204         if (archive_entry_dev(entry) == a->skip_file_dev &&
205             archive_entry_ino(entry) == a->skip_file_ino) {
206                 archive_set_error(a, 0, "Can't add archive to itself");
207                 return (ARCHIVE_WARN);
208         }
209
210         /* Format and write header. */
211         ret = ((a->format_write_header)(a, entry));
212
213         a->state = ARCHIVE_STATE_DATA;
214         return (ret);
215 }
216
217 /*
218  * Note that the compressor is responsible for blocking.
219  */
220 /* Should be "ssize_t", but that breaks the ABI.  <sigh> */
221 int
222 archive_write_data(struct archive *a, const void *buff, size_t s)
223 {
224         int ret;
225         archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_DATA);
226         archive_string_empty(&a->error_string);
227         ret = (a->format_write_data)(a, buff, s);
228         return (ret == ARCHIVE_OK ? (ssize_t)s : -1);
229 }