1
0
Fork 0
mirror of https://git.tukaani.org/xz.git synced 2024-04-04 12:36:23 +02:00
xz-archive/src/xz/process.c

392 lines
9.9 KiB
C
Raw Normal View History

2007-12-08 23:42:33 +01:00
///////////////////////////////////////////////////////////////////////////////
//
/// \file process.c
/// \brief Compresses or uncompresses a file
//
// Copyright (C) 2007 Lasse Collin
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
///////////////////////////////////////////////////////////////////////////////
#include "private.h"
enum operation_mode opt_mode = MODE_COMPRESS;
2007-12-08 23:42:33 +01:00
enum format_type opt_format = FORMAT_AUTO;
2007-12-08 23:42:33 +01:00
/// Stream used to communicate with liblzma
static lzma_stream strm = LZMA_STREAM_INIT;
2007-12-08 23:42:33 +01:00
/// Filters needed for all encoding all formats, and also decoding in raw data
static lzma_filter filters[LZMA_FILTERS_MAX + 1];
2007-12-08 23:42:33 +01:00
/// Number of filters. Zero indicates that we are using a preset.
static size_t filters_count = 0;
2007-12-08 23:42:33 +01:00
/// Number of the preset (1-9)
static size_t preset_number = 7;
2007-12-08 23:42:33 +01:00
/// Indicate if no preset has been given. In that case, we will auto-adjust
/// the compression preset so that it doesn't use too much RAM.
// FIXME
static bool preset_default = true;
2007-12-08 23:42:33 +01:00
/// Integrity check type
static lzma_check check = LZMA_CHECK_CRC64;
2007-12-08 23:42:33 +01:00
extern void
coder_set_check(lzma_check new_check)
{
check = new_check;
return;
}
2007-12-08 23:42:33 +01:00
extern void
coder_set_preset(size_t new_preset)
2007-12-08 23:42:33 +01:00
{
preset_number = new_preset;
preset_default = false;
return;
}
2007-12-08 23:42:33 +01:00
extern void
coder_add_filter(lzma_vli id, void *options)
{
if (filters_count == LZMA_FILTERS_MAX)
message_fatal(_("Maximum number of filters is four"));
2007-12-08 23:42:33 +01:00
filters[filters_count].id = id;
filters[filters_count].options = options;
++filters_count;
2007-12-08 23:42:33 +01:00
return;
}
extern void
coder_set_compression_settings(void)
2007-12-08 23:42:33 +01:00
{
// Options for LZMA1 or LZMA2 in case we are using a preset.
static lzma_options_lzma opt_lzma;
if (filters_count == 0) {
// We are using a preset. This is not a good idea in raw mode
// except when playing around with things. Different versions
// of this software may use different options in presets, and
// thus make uncompressing the raw data difficult.
if (opt_format == FORMAT_RAW) {
// The message is shown only if warnings are allowed
// but the exit status isn't changed.
message(V_WARNING, _("Using a preset in raw mode "
"is discouraged."));
message(V_WARNING, _("The exact options of the "
"presets may vary between software "
"versions."));
}
2007-12-08 23:42:33 +01:00
// Get the preset for LZMA1 or LZMA2.
if (lzma_lzma_preset(&opt_lzma, preset_number))
message_bug();
2007-12-08 23:42:33 +01:00
// Use LZMA2 except with --format=lzma we use LZMA1.
filters[0].id = opt_format == FORMAT_LZMA
? LZMA_FILTER_LZMA1 : LZMA_FILTER_LZMA2;
filters[0].options = &opt_lzma;
filters_count = 1;
2007-12-08 23:42:33 +01:00
}
// Terminate the filter options array.
filters[filters_count].id = LZMA_VLI_UNKNOWN;
2007-12-08 23:42:33 +01:00
// If we are using the LZMA_Alone format, allow exactly one filter
// which has to be LZMA.
if (opt_format == FORMAT_LZMA && (filters_count != 1
|| filters[0].id != LZMA_FILTER_LZMA1))
message_fatal(_("With --format=lzma only the LZMA1 filter "
"is supported"));
2007-12-08 23:42:33 +01:00
// If using --format=raw, we can be decoding. The memusage function
// also validates the filter chain and the options used for the
// filters.
uint64_t memory_usage;
uint64_t memory_limit;
if (opt_mode == MODE_COMPRESS) {
memory_usage = lzma_memusage_encoder(filters);
memory_limit = hardware_memlimit_encoder();
} else {
memory_usage = lzma_memusage_decoder(filters);
memory_limit = hardware_memlimit_decoder();
}
2007-12-08 23:42:33 +01:00
if (memory_usage == UINT64_MAX)
message_fatal("Unsupported filter chain or filter options");
2007-12-08 23:42:33 +01:00
if (preset_default) {
// When no preset was explicitly requested, we use the default
// preset only if the memory usage limit allows. Otherwise we
// select a lower preset automatically.
while (memory_usage > memory_limit) {
if (preset_number == 1)
message_fatal(_("Memory usage limit is too "
"small for any internal "
"filter preset"));
2007-12-08 23:42:33 +01:00
if (lzma_lzma_preset(&opt_lzma, --preset_number))
message_bug();
2007-12-08 23:42:33 +01:00
memory_usage = lzma_memusage_encoder(filters);
2007-12-08 23:42:33 +01:00
}
} else {
if (memory_usage > memory_limit)
message_fatal(_("Memory usage limit is too small "
"for the given filter setup"));
2007-12-08 23:42:33 +01:00
}
2008-11-22 16:44:33 +01:00
// Limit the number of worker threads so that memory usage
// limit isn't exceeded.
assert(memory_usage > 0);
size_t thread_limit = memory_limit / memory_usage;
if (thread_limit == 0)
thread_limit = 1;
2007-12-08 23:42:33 +01:00
if (opt_threads > thread_limit)
opt_threads = thread_limit;
return;
}
2007-12-08 23:42:33 +01:00
static bool
coder_init(void)
2007-12-08 23:42:33 +01:00
{
2008-09-11 09:46:14 +02:00
lzma_ret ret = LZMA_PROG_ERROR;
2007-12-08 23:42:33 +01:00
if (opt_mode == MODE_COMPRESS) {
switch (opt_format) {
case FORMAT_AUTO:
// args.c ensures this.
assert(0);
break;
case FORMAT_XZ:
ret = lzma_stream_encoder(&strm, filters, check);
break;
case FORMAT_LZMA:
ret = lzma_alone_encoder(&strm, filters[0].options);
break;
case FORMAT_RAW:
ret = lzma_raw_encoder(&strm, filters);
break;
2007-12-08 23:42:33 +01:00
}
} else {
2008-09-06 14:14:30 +02:00
const uint32_t flags = LZMA_TELL_UNSUPPORTED_CHECK
| LZMA_CONCATENATED;
switch (opt_format) {
case FORMAT_AUTO:
ret = lzma_auto_decoder(&strm,
hardware_memlimit_decoder(), flags);
break;
case FORMAT_XZ:
ret = lzma_stream_decoder(&strm,
hardware_memlimit_decoder(), flags);
break;
case FORMAT_LZMA:
ret = lzma_alone_decoder(&strm,
hardware_memlimit_decoder());
break;
case FORMAT_RAW:
// Memory usage has already been checked in args.c.
// FIXME Comment
ret = lzma_raw_decoder(&strm, filters);
break;
}
2007-12-08 23:42:33 +01:00
}
if (ret != LZMA_OK) {
if (ret == LZMA_MEM_ERROR)
message_error("%s", message_strm(LZMA_MEM_ERROR));
2007-12-08 23:42:33 +01:00
else
message_bug();
2007-12-08 23:42:33 +01:00
return true;
2007-12-08 23:42:33 +01:00
}
return false;
2007-12-08 23:42:33 +01:00
}
static bool
coder_run(file_pair *pair)
2007-12-08 23:42:33 +01:00
{
// Buffers to hold input and output data.
uint8_t in_buf[IO_BUFFER_SIZE];
uint8_t out_buf[IO_BUFFER_SIZE];
// Initialize the progress indicator.
const uint64_t in_size = pair->src_st.st_size <= (off_t)(0)
? 0 : (uint64_t)(pair->src_st.st_size);
message_progress_start(pair->src_name, in_size);
2007-12-08 23:42:33 +01:00
lzma_action action = LZMA_RUN;
2008-09-02 18:33:32 +02:00
lzma_ret ret;
2007-12-08 23:42:33 +01:00
strm.avail_in = 0;
strm.next_out = out_buf;
strm.avail_out = IO_BUFFER_SIZE;
2007-12-08 23:42:33 +01:00
while (!user_abort) {
// Fill the input buffer if it is empty and we haven't reached
// end of file yet.
if (strm.avail_in == 0 && !pair->src_eof) {
strm.next_in = in_buf;
strm.avail_in = io_read(pair, in_buf, IO_BUFFER_SIZE);
2007-12-08 23:42:33 +01:00
if (strm.avail_in == SIZE_MAX)
2007-12-08 23:42:33 +01:00
break;
// Encoder needs to know when we have given all the
// input to it. The decoders need to know it too when
// we are using LZMA_CONCATENATED.
if (pair->src_eof)
2007-12-08 23:42:33 +01:00
action = LZMA_FINISH;
}
// Let liblzma do the actual work.
ret = lzma_code(&strm, action);
2007-12-08 23:42:33 +01:00
// Write out if the output buffer became full.
if (strm.avail_out == 0) {
if (opt_mode != MODE_TEST && io_write(pair, out_buf,
IO_BUFFER_SIZE - strm.avail_out))
return false;
2007-12-08 23:42:33 +01:00
strm.next_out = out_buf;
strm.avail_out = IO_BUFFER_SIZE;
}
2007-12-08 23:42:33 +01:00
if (ret != LZMA_OK) {
// Determine if the return value indicates that we
// won't continue coding.
const bool stop = ret != LZMA_NO_CHECK
&& ret != LZMA_UNSUPPORTED_CHECK;
if (stop) {
// First print the final progress info.
// This way the user sees more accurately
// where the error occurred. Note that we
// print this *before* the possible error
// message.
//
// FIXME: What if something goes wrong
// after this?
message_progress_end(strm.total_in,
strm.total_out,
ret == LZMA_STREAM_END);
// Write the remaining bytes even if something
// went wrong, because that way the user gets
// as much data as possible, which can be good
// when trying to get at least some useful
// data out of damaged files.
if (opt_mode != MODE_TEST && io_write(pair,
out_buf, IO_BUFFER_SIZE
- strm.avail_out))
return false;
2007-12-08 23:42:33 +01:00
}
if (ret == LZMA_STREAM_END) {
// Check that there is no trailing garbage.
// This is needed for LZMA_Alone and raw
// streams.
if (strm.avail_in == 0 && (pair->src_eof
|| io_read(pair, in_buf, 1)
== 0)) {
assert(pair->src_eof);
return true;
}
2007-12-08 23:42:33 +01:00
// FIXME: What about io_read() failing?
2007-12-08 23:42:33 +01:00
// We hadn't reached the end of the file.
ret = LZMA_DATA_ERROR;
assert(stop);
}
2007-12-08 23:42:33 +01:00
// If we get here and stop is true, something went
// wrong and we print an error. Otherwise it's just
// a warning and coding can continue.
if (stop) {
message_error("%s: %s", pair->src_name,
message_strm(ret));
} else {
message_warning("%s: %s", pair->src_name,
message_strm(ret));
// When compressing, all possible errors set
// stop to true.
assert(opt_mode != MODE_COMPRESS);
}
2007-12-08 23:42:33 +01:00
if (ret == LZMA_MEMLIMIT_ERROR) {
// Figure out how much memory would have
// actually needed.
// TODO
}
2007-12-08 23:42:33 +01:00
if (stop)
return false;
2007-12-08 23:42:33 +01:00
}
// Show progress information if --verbose was specified and
// stderr is a terminal.
message_progress_update(strm.total_in, strm.total_out);
2007-12-08 23:42:33 +01:00
}
return false;
}
2007-12-08 23:42:33 +01:00
extern void
process_file(const char *filename)
{
// First try initializing the coder. If it fails, it's useless to try
// opening the file. Check also for user_abort just in case if we had
// got a signal while initializing the coder.
if (coder_init() || user_abort)
2007-12-08 23:42:33 +01:00
return;
// Try to open the input and output files.
file_pair *pair = io_open(filename);
if (pair == NULL)
return;
2007-12-08 23:42:33 +01:00
// Do the actual coding.
const bool success = coder_run(pair);
// Close the file pair. It needs to know if coding was successful to
// know if the source or target file should be unlinked.
io_close(pair, success);
2007-12-08 23:42:33 +01:00
return;
}