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