-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtardemux.c
400 lines (351 loc) · 11.6 KB
/
tardemux.c
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/**
* (C) 2016 Graham Leggett
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <archive.h>
#include <archive_entry.h>
#include "config.h"
typedef struct demux_t
{
char *pathname;
int fd;
} demux_t;
void help(const char *name)
{
printf(
"Usage: %s [-f streamname] [-a] [-r] [file1] [file2] [...]\n"
"\n"
"This tool demultiplexes streams that have been multiplexed by the\n"
"tarmux tool. It expects a series of tar files containing sparse file\n"
"fragments that are unpacked and written to form the original stream.\n"
"\n"
"In the simplest form, tardemux reads a stream from stdin, unpacks the\n"
"stream and writes the first stream to stdout, leaving additional data\n"
"intact. This allows tardemux to be run again to extract a further\n"
"stream.\n"
"\n"
"If file parameters are specified, the stream is expected to contain\n"
"entries matching these files parameters. The fragments will be unpacked\n"
"and written to the given paths.\n"
"\n"
" -f name, --file=name\tThe name of the input files from which tar\n"
"\t\t\tstreams will be read, defaults to stdin. Can be specified more\n"
"\t\t\tthan once.\n"
" -a\t\t\tUnpack all pathnames in a stream to individual files.\n"
" -r\t\t\tTreat the incoming stream as a raw compressed stream rather\n"
"\t\t\tthan a tar stream.\n"
" [file1] [...]\t\tOptional files/pipes expected in the tar stream.\n"
"\t\t\tData will be demultiplexed and written to each file/pipe. If this\n"
"\t\t\tfile/pipe exists, data will be written to the existing file.\n"
"\n"
"This tool is based on libarchive, and is licensed under the Apache License,\n"
"Version 2.0.\n"
"", name);
}
void version()
{
printf(PACKAGE_STRING "\n");
}
/*
* Returns the length of the path, ignoring any trailing
* numeric suffix following the last dot.
*
* If the path does not contain a numeric suffix, the
* length of the whole path is returned.
*/
static int pathlen(const char *pathname, intmax_t *index)
{
const char *slider;
*index = 0;
slider = strrchr(pathname, '.');
if (slider) {
int i, offset, valid = 1;
offset = slider - pathname;
i = offset + 1;
while (pathname[i]) {
if (!isdigit(pathname[i])) {
valid = 0;
}
else {
(*index) *= 10;
(*index) += (pathname[i] - '0');
}
i++;
}
if (valid) {
return offset;
}
return i;
}
return strlen(pathname);
}
ssize_t transfer(struct archive *a, demux_t *demux)
{
const void *buff;
size_t len;
off_t offset;
ssize_t size;
int rv;
ssize_t total = 0;
for (;;) {
rv = archive_read_data_block(a, &buff, &len, &offset);
if (rv == ARCHIVE_FATAL) {
fprintf(stderr, "Error: while reading data block: %s\n", archive_error_string(a));
break;
}
if (rv == ARCHIVE_WARN) {
fprintf(stderr, "Warning: while reading data block: %s\n", archive_error_string(a));
}
while (len) {
size = write(demux->fd, buff, len);
if (size < 0) {
fprintf(stderr, "Error: could not write data block to %s: %s\n",
demux->pathname, strerror(errno));
break;
}
len -= size;
buff += size;
total += size;
};
if (rv == ARCHIVE_RETRY) {
fprintf(stderr, "Warning (Retry): while reading data block: %s\n", archive_error_string(a));
continue;
}
if (rv == ARCHIVE_EOF) {
return total;
}
}
return -1;
}
int main(int argc, char * const argv[])
{
struct archive *a;
struct archive_entry *entry;
demux_t *demux = NULL;
demux_t *sdemux = NULL;
const char *name = argv[0];
const char **filenames = NULL;
size_t blocksize = 10240;
ssize_t total = 0;
int opt;
int all = 0;
int raw = 0;
int filenames_num = 0;
int rv;
int demux_count;
int i;
while ((opt = getopt(argc, argv, "hvarf:n:-:")) != -1) {
switch (opt) {
case '-':
if (!strcmp(optarg, "help")) {
help(name);
exit(0);
}
else if (!strcmp(optarg, "version")) {
version();
exit(0);
}
break;
case 'h':
help(name);
exit(0);
case 'v':
version();
exit(0);
case 'a':
all = 1;
break;
case 'r':
raw = 1;
break;
case 'f':
filenames = realloc(filenames,
(filenames_num + 2) * sizeof(const char *));
filenames[filenames_num] = optarg;
filenames_num++;
filenames[filenames_num] = NULL;
break;
default:
help(name);
exit(1);
}
}
/* make sure we don't die on sigpipe */
signal(SIGPIPE, SIG_IGN);
/* remaining parameters are files to mux, otherwise default to stdin */
demux_count = argc - optind;
if (demux_count || all) {
demux = calloc(demux_count, sizeof(demux_t));
for (i = 0; i < demux_count; i++) {
demux[i].pathname = strdup(argv[optind + i]);
if ((demux[i].fd = open(demux[i].pathname,
O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, 0666)) < 0) {
perror(demux[i].pathname);
exit(2);
}
}
}
else {
sdemux = calloc(1, sizeof(demux_t));
sdemux->fd = STDOUT_FILENO;
}
/* set up the input archive */
a = archive_read_new();
archive_read_support_filter_all(a);
if (raw) {
archive_read_support_format_raw(a);
}
else {
archive_read_support_format_all(a);
}
if (!filenames) {
archive_read_open_fd(a, STDIN_FILENO, blocksize);
}
else {
if ((rv = archive_read_open_filenames(a, filenames, blocksize))) {
fprintf(stderr, "Could not open archive(s): %s\n",
archive_error_string(a));
exit(1);
}
}
for (;;) {
intmax_t index;
rv = archive_read_next_header(a, &entry);
if (rv == ARCHIVE_FATAL) {
fprintf(stderr, "Error: while reading archive header: %s\n", archive_error_string(a));
exit(1);
}
else if (rv == ARCHIVE_WARN) {
fprintf(stderr, "Warning: while reading archive header: %s\n", archive_error_string(a));
}
else if (rv == ARCHIVE_RETRY) {
fprintf(stderr, "Warning (Retry): while reading archive header: %s\n", archive_error_string(a));
continue;
}
else if (rv == ARCHIVE_EOF) {
break;
}
/* otherwise ARCHIVE_OK */
/* handle demux to stdout */
if (sdemux) {
const char *pathname = archive_entry_pathname(entry);
if (!sdemux->pathname) {
sdemux->pathname = strndup(pathname, pathlen(pathname, &index));
if (index) {
fprintf(stderr,
"Error: First stream index is non-zero (%" PRIdMAX "), not at the start of the stream, aborting: %s\n",
index, archive_entry_pathname(entry));
exit(4);
}
total = transfer(a, sdemux);
if (total < 0) {
exit(1);
}
else if (total == 0) {
break;
}
}
else if (!strncmp(sdemux->pathname, pathname, pathlen(pathname, &index))) {
total = transfer(a, sdemux);
if (total < 0) {
exit(1);
}
else if (total == 0) {
break;
}
}
else {
fprintf(stderr,
"Error: Unexpected additional path in stream, aborting: %s\n",
archive_entry_pathname(entry));
exit(1);
}
}
/* handle demux to individual files */
else {
demux_t *dm = NULL;
int found = 0;
for (i = 0; i < demux_count; i++) {
const char *pathname = archive_entry_pathname(entry);
if (!strncmp(demux[i].pathname, pathname, pathlen(pathname, &index))) {
dm = &demux[i];
found = 1;
break;
}
}
if (!found) {
if (all) {
demux = realloc(demux, (demux_count + 1) * sizeof(demux_t));
demux[demux_count].pathname = strdup(
archive_entry_pathname(entry));
if ((demux[demux_count].fd = open(
demux[demux_count].pathname,
O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, 0666))
< 0) {
perror(demux[demux_count].pathname);
exit(2);
}
dm = &demux[demux_count];
demux_count++;
}
else {
fprintf(stderr,
"Error: Unnamed path in stream, aborting: %s\n",
archive_entry_pathname(entry));
exit(1);
}
}
if (dm) {
total = transfer(a, dm);
if (total < 0) {
exit(1);
}
else if (total == 0) {
if (close(dm->fd)) {
fprintf(stderr, "Error: Could not close %s: %s\n",
dm->pathname, strerror(errno));
exit(1);
}
dm->fd = 0;
break;
}
}
}
}
/* clean up the output files */
if (demux) {
for (i = 0; i < demux_count; i++) {
if (demux[i].fd) {
close(demux[i].fd);
}
free(demux[i].pathname);
}
free(demux);
}
if (sdemux) {
free(sdemux->pathname);
free(sdemux);
}
exit(0);
}