1 /* pxzcat derived from nbdkit
2 * Copyright (C) 2013 Red Hat Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of Red Hat nor the names of its contributors may be
17 * used to endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 #include <sys/types.h>
54 #define debug(fs,...) fprintf (stderr, "pxzcat: debug: " fs "\n", ## __VA_ARGS__)
56 #define debug(fs,...) /* nothing */
59 /* Size of buffers used in decompression loop. */
60 #define BUFFER_SIZE (64*1024)
62 #define XZ_HEADER_MAGIC "\xfd" "7zXZ\0"
63 #define XZ_HEADER_MAGIC_LEN 6
64 #define XZ_FOOTER_MAGIC "YZ"
65 #define XZ_FOOTER_MAGIC_LEN 2
67 static void usage (int exitcode);
68 static void xzfile_uncompress (const char *filename, const char *outputfile, unsigned nr_threads);
69 static int check_header_magic (int fd);
70 static lzma_index *parse_indexes (const char *filename, int fd);
71 static void iter_blocks (lzma_index *idx, unsigned nr_threads, const char *filename, int fd, const char *outputfile, int ofd);
73 static struct option long_options[] = {
74 { "output", required_argument, 0, 'o' },
75 { "threads", required_argument, 0, 'T' },
76 { "help", 0, 0, '?' },
80 static const char *options = "o:T:";
83 main (int argc, char *argv[])
87 unsigned nr_threads = 0;
88 const char *outputfile = NULL;
91 c = getopt_long (argc, argv, options, long_options, &longopt_index);
96 /* Long option with no short opt equivalent. */
105 if (sscanf (optarg, "%u", &nr_threads) != 1)
106 error (EXIT_FAILURE, 0, "cannot parse -T option");
110 usage (EXIT_SUCCESS);
113 usage (EXIT_FAILURE);
117 if (optind != argc - 1)
118 usage (EXIT_FAILURE);
120 if (outputfile == NULL)
121 error (EXIT_FAILURE, 0, "you must give the -o (output file) option");
123 /* -T 0 (default) means use all cores. */
124 if (nr_threads == 0) {
125 long i = sysconf (_SC_NPROCESSORS_ONLN);
127 error (EXIT_FAILURE, errno, "could not get number of cores");
128 nr_threads = (unsigned) i;
130 debug ("nr_threads = %u", nr_threads);
132 xzfile_uncompress (argv[optind], outputfile, nr_threads);
140 printf ("usage: pxzcat -o output [-T #threads] input.xz\n");
145 xzfile_uncompress (const char *filename, const char *outputfile,
153 fd = open (filename, O_RDONLY);
155 error (EXIT_FAILURE, errno, "open: %s", filename);
157 /* Check file magic. */
158 if (!check_header_magic (fd))
159 error (EXIT_FAILURE, 0, "%s: not an xz file", filename);
161 /* Read and parse the indexes. */
162 idx = parse_indexes (filename, fd);
164 /* Get the file uncompressed size, create the output file. */
165 size = lzma_index_uncompressed_size (idx);
166 debug ("uncompressed size = %" PRIu64 " bytes", size);
168 ofd = open (outputfile, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0644);
170 error (EXIT_FAILURE, errno, "open: %s", outputfile);
171 if (ftruncate (ofd, size) == -1)
172 error (EXIT_FAILURE, errno, "ftruncate: %s", outputfile);
174 /* Tell the kernel we won't read the output file. */
175 posix_fadvise (fd, 0, 0, POSIX_FADV_RANDOM|POSIX_FADV_DONTNEED);
177 /* Iterate over blocks. */
178 iter_blocks (idx, nr_threads, filename, fd, outputfile, ofd);
184 check_header_magic (int fd)
186 char buf[XZ_HEADER_MAGIC_LEN];
188 if (lseek (fd, 0, SEEK_SET) == -1)
190 if (read (fd, buf, XZ_HEADER_MAGIC_LEN) != XZ_HEADER_MAGIC_LEN)
192 if (memcmp (buf, XZ_HEADER_MAGIC, XZ_HEADER_MAGIC_LEN) != 0)
197 /* For explanation of this function, see src/xz/list.c:parse_indexes
201 parse_indexes (const char *filename, int fd)
204 off_t pos, index_size;
205 uint8_t footer[LZMA_STREAM_HEADER_SIZE];
206 uint8_t header[LZMA_STREAM_HEADER_SIZE];
207 lzma_stream_flags footer_flags;
208 lzma_stream_flags header_flags;
209 lzma_stream strm = LZMA_STREAM_INIT;
211 lzma_index *combined_index = NULL;
212 lzma_index *this_index = NULL;
213 lzma_vli stream_padding = 0;
214 size_t nr_streams = 0;
216 /* Check file size is a multiple of 4 bytes. */
217 pos = lseek (fd, 0, SEEK_END);
218 if (pos == (off_t) -1)
219 error (EXIT_FAILURE, errno, "%s: lseek", filename);
222 error (EXIT_FAILURE, 0,
223 "%s: not an xz file: size is not a multiple of 4 bytes",
226 /* Jump backwards through the file identifying each stream. */
228 debug ("looping through streams: pos = %" PRIu64, (uint64_t) pos);
230 if (pos < LZMA_STREAM_HEADER_SIZE)
231 error (EXIT_FAILURE, 0,
232 "%s: corrupted file at %" PRIu64, filename, (uint64_t) pos);
234 if (lseek (fd, -LZMA_STREAM_HEADER_SIZE, SEEK_CUR) == -1)
235 error (EXIT_FAILURE, errno, "%s: lseek", filename);
237 if (read (fd, footer, LZMA_STREAM_HEADER_SIZE) != LZMA_STREAM_HEADER_SIZE)
238 error (EXIT_FAILURE, errno, "%s: read stream footer", filename);
240 /* Skip stream padding. */
241 if (footer[8] == 0 && footer[9] == 0 &&
242 footer[10] == 0 && footer[11] == 0) {
248 pos -= LZMA_STREAM_HEADER_SIZE;
251 debug ("decode stream footer at pos = %" PRIu64, (uint64_t) pos);
253 /* Does the stream footer look reasonable? */
254 r = lzma_stream_footer_decode (&footer_flags, footer);
256 error (EXIT_FAILURE, 0,
257 "%s: invalid stream footer (error %d)", filename, r);
259 debug ("backward_size = %" PRIu64, (uint64_t) footer_flags.backward_size);
260 index_size = footer_flags.backward_size;
261 if (pos < index_size + LZMA_STREAM_HEADER_SIZE)
262 error (EXIT_FAILURE, 0, "%s: invalid stream footer", filename);
265 debug ("decode index at pos = %" PRIu64, (uint64_t) pos);
267 /* Seek backwards to the index of this stream. */
268 if (lseek (fd, pos, SEEK_SET) == -1)
269 error (EXIT_FAILURE, errno, "%s: lseek", filename);
271 /* Decode the index. */
272 r = lzma_index_decoder (&strm, &this_index, UINT64_MAX);
274 error (EXIT_FAILURE, 0,
275 "%s: invalid stream index (error %d)", filename, r);
280 strm.avail_in = index_size;
281 if (strm.avail_in > BUFSIZ)
282 strm.avail_in = BUFSIZ;
284 n = read (fd, &buf, strm.avail_in);
286 error (EXIT_FAILURE, errno, "%s: read", filename);
288 index_size -= strm.avail_in;
291 r = lzma_code (&strm, LZMA_RUN);
292 } while (r == LZMA_OK);
294 if (r != LZMA_STREAM_END)
295 error (EXIT_FAILURE, 0, "%s: could not parse index (error %d)",
298 pos -= lzma_index_total_size (this_index) + LZMA_STREAM_HEADER_SIZE;
300 debug ("decode stream header at pos = %" PRIu64, (uint64_t) pos);
302 /* Read and decode the stream header. */
303 if (lseek (fd, pos, SEEK_SET) == -1)
304 error (EXIT_FAILURE, errno, "%s: lseek", filename);
306 if (read (fd, header, LZMA_STREAM_HEADER_SIZE) != LZMA_STREAM_HEADER_SIZE)
307 error (EXIT_FAILURE, errno, "%s: read stream header", filename);
309 r = lzma_stream_header_decode (&header_flags, header);
311 error (EXIT_FAILURE, 0,
312 "%s: invalid stream header (error %d)", filename, r);
314 /* Header and footer of the stream should be equal. */
315 r = lzma_stream_flags_compare (&header_flags, &footer_flags);
317 error (EXIT_FAILURE, 0,
318 "%s: header and footer of stream are not equal (error %d)",
321 /* Store the decoded stream flags in this_index. */
322 r = lzma_index_stream_flags (this_index, &footer_flags);
324 error (EXIT_FAILURE, 0,
325 "%s: cannot read stream_flags from index (error %d)",
328 /* Store the amount of stream padding so far. Needed to calculate
329 * compressed offsets correctly in multi-stream files.
331 r = lzma_index_stream_padding (this_index, stream_padding);
333 error (EXIT_FAILURE, 0,
334 "%s: cannot set stream_padding in index (error %d)",
337 if (combined_index != NULL) {
338 r = lzma_index_cat (this_index, combined_index, NULL);
340 error (EXIT_FAILURE, 0, "%s: cannot combine indexes", filename);
343 combined_index = this_index;
349 return combined_index;
352 /* Return true iff the buffer is all zero bytes.
354 * Note that gcc is smart enough to optimize this properly:
355 * http://stackoverflow.com/questions/1493936/faster-means-of-checking-for-an-empty-buffer-in-c/1493989#1493989
358 is_zero (const char *buffer, size_t size)
362 for (i = 0; i < size; ++i) {
370 struct global_state {
371 /* Current iterator. Threads update this, but it is protected by a
372 * mutex, and each thread takes a copy of it when working on it.
374 lzma_index_iter iter;
375 lzma_bool iter_finished;
376 pthread_mutex_t iter_mutex;
378 /* Note that all threads are accessing these fds, so you have
379 * to use pread/pwrite instead of lseek!
383 const char *filename;
387 const char *outputfile;
391 struct per_thread_state {
393 struct global_state *global;
397 /* Create threads to iterate over the blocks and uncompress. */
398 static void *worker_thread (void *vp);
401 iter_blocks (lzma_index *idx, unsigned nr_threads,
402 const char *filename, int fd, const char *outputfile, int ofd)
404 struct global_state global;
405 struct per_thread_state per_thread[nr_threads];
406 pthread_t thread[nr_threads];
407 unsigned u, nr_errors;
411 lzma_index_iter_init (&global.iter, idx);
412 global.iter_finished = 0;
413 err = pthread_mutex_init (&global.iter_mutex, NULL);
415 error (EXIT_FAILURE, err, "pthread_mutex_init");
417 global.filename = filename;
419 global.outputfile = outputfile;
422 for (u = 0; u < nr_threads; ++u) {
423 per_thread[u].thread_num = u;
424 per_thread[u].global = &global;
427 /* Start the threads. */
428 for (u = 0; u < nr_threads; ++u) {
429 err = pthread_create (&thread[u], NULL, worker_thread, &per_thread[u]);
431 error (EXIT_FAILURE, err, "pthread_create (%u)", u);
434 /* Wait for the threads to exit. */
436 for (u = 0; u < nr_threads; ++u) {
437 err = pthread_join (thread[u], &status);
439 error (0, err, "pthread_join (%u)", u);
442 if (*(int *)status == -1)
450 /* Iterate over the blocks and uncompress. */
452 worker_thread (void *vp)
454 struct per_thread_state *state = vp;
455 struct global_state *global = state->global;
456 lzma_index_iter iter;
458 off_t position, oposition;
459 uint8_t header[LZMA_BLOCK_HEADER_SIZE_MAX];
462 lzma_filter filters[LZMA_FILTERS_MAX + 1];
464 lzma_stream strm = LZMA_STREAM_INIT;
465 char outbuf[BUFFER_SIZE];
467 lzma_bool iter_finished;
472 /* Get the next block. */
473 err = pthread_mutex_lock (&global->iter_mutex);
474 if (err != 0) abort ();
475 iter_finished = global->iter_finished;
476 if (!iter_finished) {
477 iter_finished = global->iter_finished =
478 lzma_index_iter_next (&global->iter, LZMA_INDEX_ITER_NONEMPTY_BLOCK);
480 /* Take a local copy of this iterator since another thread will
481 * update the global version.
485 err = pthread_mutex_unlock (&global->iter_mutex);
486 if (err != 0) abort ();
490 /* Read the block header. Start by reading a single byte which
491 * tell us how big the block header is.
493 position = iter.block.compressed_file_offset;
494 n = pread (global->fd, header, 1, position);
497 "%s: read: unexpected end of file reading block header byte",
499 return &state->status;
502 error (0, errno, "%s: read", global->filename);
503 return &state->status;
507 if (header[0] == '\0') {
509 "%s: read: unexpected invalid block in file, header[0] = 0",
511 return &state->status;
515 block.check = iter.stream.flags->check;
516 block.filters = filters;
517 block.header_size = lzma_block_header_size_decode (header[0]);
519 /* Now read and decode the block header. */
520 n = pread (global->fd, &header[1], block.header_size-1, position);
521 if (n >= 0 && n != block.header_size-1) {
523 "%s: read: unexpected end of file reading block header",
525 return &state->status;
528 error (0, errno, "%s: read", global->filename);
529 return &state->status;
533 r = lzma_block_header_decode (&block, NULL, header);
535 error (0, errno, "%s: invalid block header (error %d)",
536 global->filename, r);
537 return &state->status;
540 /* What this actually does is it checks that the block header
543 r = lzma_block_compressed_size (&block, iter.block.unpadded_size);
546 "%s: cannot calculate compressed size (error %d)",
547 global->filename, r);
548 return &state->status;
551 /* Where we will start writing to. */
552 oposition = iter.block.uncompressed_file_offset;
554 /* Read the block data and uncompress it. */
555 r = lzma_block_decoder (&strm, &block);
557 error (0, 0, "%s: invalid block (error %d)", global->filename, r);
558 return &state->status;
563 strm.next_out = outbuf;
564 strm.avail_out = sizeof outbuf;
567 uint8_t buf[BUFFER_SIZE];
568 lzma_action action = LZMA_RUN;
570 if (strm.avail_in == 0) {
572 n = pread (global->fd, buf, sizeof buf, position);
574 error (0, errno, "%s: read", global->filename);
575 return &state->status;
580 action = LZMA_FINISH;
583 r = lzma_code (&strm, action);
585 if (strm.avail_out == 0 || r == LZMA_STREAM_END) {
586 size_t wsz = sizeof outbuf - strm.avail_out;
588 /* Don't write if the block is all zero, to preserve output file
589 * sparseness. However we have to update oposition.
591 if (!is_zero (outbuf, wsz)) {
592 if (pwrite (global->ofd, outbuf, wsz, oposition) != wsz) {
593 /* XXX Handle short writes. */
594 error (0, errno, "%s: write", global->filename);
595 return &state->status;
600 strm.next_out = outbuf;
601 strm.avail_out = sizeof outbuf;
604 if (r == LZMA_STREAM_END)
608 "%s: could not parse block data (error %d)",
609 global->filename, r);
610 return &state->status;
616 for (i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i)
617 free (filters[i].options);
621 return &state->status;