2025-02-24 04:01:20 +01:00
|
|
|
/* Plzip - Parallel compressor compatible with lzip
|
2025-02-24 03:25:50 +01:00
|
|
|
Copyright (C) 2009 Laszlo Ersek.
|
2025-02-24 04:12:46 +01:00
|
|
|
Copyright (C) 2009-2016 Antonio Diaz Diaz.
|
2025-02-24 03:25:50 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2025-02-24 04:04:18 +01:00
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
2025-02-24 03:25:50 +01:00
|
|
|
(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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _FILE_OFFSET_BITS 64
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <climits>
|
|
|
|
#include <csignal>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <pthread.h>
|
2025-02-24 03:54:35 +01:00
|
|
|
#include <stdint.h>
|
2025-02-24 03:25:50 +01:00
|
|
|
#include <unistd.h>
|
2025-02-24 03:54:35 +01:00
|
|
|
#include <sys/stat.h>
|
2025-02-24 03:25:50 +01:00
|
|
|
#include <lzlib.h>
|
|
|
|
|
2025-02-24 03:54:35 +01:00
|
|
|
#include "lzip.h"
|
|
|
|
#include "file_index.h"
|
2025-02-24 03:25:50 +01:00
|
|
|
|
|
|
|
|
2025-02-24 04:09:55 +01:00
|
|
|
void Pretty_print::operator()( const char * const msg ) const
|
|
|
|
{
|
|
|
|
if( verbosity >= 0 )
|
|
|
|
{
|
|
|
|
if( first_post )
|
|
|
|
{
|
|
|
|
first_post = false;
|
|
|
|
std::fprintf( stderr, " %s: ", name_.c_str() );
|
2025-02-24 04:12:46 +01:00
|
|
|
for( unsigned i = name_.size(); i < longest_name; ++i )
|
2025-02-24 04:09:55 +01:00
|
|
|
std::fputc( ' ', stderr );
|
|
|
|
if( !msg ) std::fflush( stderr );
|
|
|
|
}
|
|
|
|
if( msg ) std::fprintf( stderr, "%s\n", msg );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-24 03:54:35 +01:00
|
|
|
// Returns the number of bytes really read.
|
|
|
|
// If (returned value < size) and (errno == 0), means EOF was reached.
|
|
|
|
//
|
|
|
|
int preadblock( const int fd, uint8_t * const buf, const int size,
|
|
|
|
const long long pos )
|
2025-02-24 03:26:46 +01:00
|
|
|
{
|
2025-02-24 04:04:37 +01:00
|
|
|
int sz = 0;
|
2025-02-24 03:54:35 +01:00
|
|
|
errno = 0;
|
2025-02-24 04:04:37 +01:00
|
|
|
while( sz < size )
|
2025-02-24 03:26:46 +01:00
|
|
|
{
|
2025-02-24 04:04:37 +01:00
|
|
|
const int n = pread( fd, buf + sz, size - sz, pos + sz );
|
|
|
|
if( n > 0 ) sz += n;
|
2025-02-24 03:54:35 +01:00
|
|
|
else if( n == 0 ) break; // EOF
|
2025-02-24 04:04:02 +01:00
|
|
|
else if( errno != EINTR ) break;
|
2025-02-24 03:54:35 +01:00
|
|
|
errno = 0;
|
2025-02-24 03:26:46 +01:00
|
|
|
}
|
2025-02-24 04:04:37 +01:00
|
|
|
return sz;
|
2025-02-24 03:54:35 +01:00
|
|
|
}
|
2025-02-24 03:26:46 +01:00
|
|
|
|
|
|
|
|
2025-02-24 03:54:35 +01:00
|
|
|
// Returns the number of bytes really written.
|
|
|
|
// If (returned value < size), it is always an error.
|
2025-02-24 03:42:28 +01:00
|
|
|
//
|
2025-02-24 03:54:35 +01:00
|
|
|
int pwriteblock( const int fd, const uint8_t * const buf, const int size,
|
|
|
|
const long long pos )
|
2025-02-24 03:42:28 +01:00
|
|
|
{
|
2025-02-24 04:04:37 +01:00
|
|
|
int sz = 0;
|
2025-02-24 03:54:35 +01:00
|
|
|
errno = 0;
|
2025-02-24 04:04:37 +01:00
|
|
|
while( sz < size )
|
2025-02-24 03:54:35 +01:00
|
|
|
{
|
2025-02-24 04:04:37 +01:00
|
|
|
const int n = pwrite( fd, buf + sz, size - sz, pos + sz );
|
|
|
|
if( n > 0 ) sz += n;
|
2025-02-24 04:04:02 +01:00
|
|
|
else if( n < 0 && errno != EINTR ) break;
|
2025-02-24 03:54:35 +01:00
|
|
|
errno = 0;
|
|
|
|
}
|
2025-02-24 04:04:37 +01:00
|
|
|
return sz;
|
2025-02-24 03:42:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-24 03:54:35 +01:00
|
|
|
int decompress_read_error( struct LZ_Decoder * const decoder,
|
|
|
|
const Pretty_print & pp, const int worker_id )
|
2025-02-24 03:26:46 +01:00
|
|
|
{
|
2025-02-24 03:54:35 +01:00
|
|
|
const LZ_Errno errcode = LZ_decompress_errno( decoder );
|
|
|
|
pp();
|
|
|
|
if( verbosity >= 0 )
|
2025-02-24 04:09:55 +01:00
|
|
|
std::fprintf( stderr, "LZ_decompress_read error in worker %d: %s\n",
|
2025-02-24 03:54:35 +01:00
|
|
|
worker_id, LZ_strerror( errcode ) );
|
|
|
|
if( errcode == LZ_header_error || errcode == LZ_unexpected_eof ||
|
|
|
|
errcode == LZ_data_error )
|
|
|
|
return 2;
|
|
|
|
return 1;
|
2025-02-24 03:25:50 +01:00
|
|
|
}
|
|
|
|
|
2025-02-24 03:26:46 +01:00
|
|
|
|
2025-02-24 03:54:35 +01:00
|
|
|
namespace {
|
|
|
|
|
2025-02-24 03:26:46 +01:00
|
|
|
struct Worker_arg
|
|
|
|
{
|
2025-02-24 03:54:35 +01:00
|
|
|
const File_index * file_index;
|
2025-02-24 03:26:46 +01:00
|
|
|
const Pretty_print * pp;
|
|
|
|
int worker_id;
|
2025-02-24 03:54:35 +01:00
|
|
|
int num_workers;
|
|
|
|
int infd;
|
|
|
|
int outfd;
|
2025-02-24 03:26:46 +01:00
|
|
|
};
|
2025-02-24 03:25:50 +01:00
|
|
|
|
|
|
|
|
2025-02-24 03:54:35 +01:00
|
|
|
// read members from file, decompress their contents, and
|
|
|
|
// write the produced data to file.
|
2025-02-24 03:27:22 +01:00
|
|
|
extern "C" void * dworker( void * arg )
|
2025-02-24 03:25:50 +01:00
|
|
|
{
|
2025-02-24 03:26:46 +01:00
|
|
|
const Worker_arg & tmp = *(Worker_arg *)arg;
|
2025-02-24 03:54:35 +01:00
|
|
|
const File_index & file_index = *tmp.file_index;
|
2025-02-24 03:26:46 +01:00
|
|
|
const Pretty_print & pp = *tmp.pp;
|
|
|
|
const int worker_id = tmp.worker_id;
|
2025-02-24 03:54:35 +01:00
|
|
|
const int num_workers = tmp.num_workers;
|
|
|
|
const int infd = tmp.infd;
|
|
|
|
const int outfd = tmp.outfd;
|
|
|
|
const int buffer_size = 65536;
|
2025-02-24 03:26:46 +01:00
|
|
|
|
2025-02-24 03:54:35 +01:00
|
|
|
uint8_t * const ibuffer = new( std::nothrow ) uint8_t[buffer_size];
|
|
|
|
uint8_t * const obuffer = new( std::nothrow ) uint8_t[buffer_size];
|
2025-02-24 03:25:50 +01:00
|
|
|
LZ_Decoder * const decoder = LZ_decompress_open();
|
2025-02-24 03:54:35 +01:00
|
|
|
if( !ibuffer || !obuffer || !decoder ||
|
|
|
|
LZ_decompress_errno( decoder ) != LZ_ok )
|
2025-02-24 04:04:02 +01:00
|
|
|
{ pp( "Not enough memory." ); cleanup_and_fail(); }
|
2025-02-24 03:25:50 +01:00
|
|
|
|
2025-02-24 04:04:02 +01:00
|
|
|
for( long i = worker_id; i < file_index.members(); i += num_workers )
|
2025-02-24 03:25:50 +01:00
|
|
|
{
|
2025-02-24 03:54:35 +01:00
|
|
|
long long data_pos = file_index.dblock( i ).pos();
|
|
|
|
long long data_rest = file_index.dblock( i ).size();
|
|
|
|
long long member_pos = file_index.mblock( i ).pos();
|
|
|
|
long long member_rest = file_index.mblock( i ).size();
|
2025-02-24 03:26:46 +01:00
|
|
|
|
2025-02-24 03:54:35 +01:00
|
|
|
while( member_rest > 0 )
|
2025-02-24 03:26:46 +01:00
|
|
|
{
|
2025-02-24 03:54:35 +01:00
|
|
|
while( LZ_decompress_write_size( decoder ) > 0 )
|
2025-02-24 03:26:46 +01:00
|
|
|
{
|
2025-02-24 03:54:35 +01:00
|
|
|
const int size = std::min( LZ_decompress_write_size( decoder ),
|
|
|
|
(int)std::min( (long long)buffer_size, member_rest ) );
|
|
|
|
if( size > 0 )
|
|
|
|
{
|
|
|
|
if( preadblock( infd, ibuffer, size, member_pos ) != size )
|
2025-02-24 04:01:20 +01:00
|
|
|
{ pp(); show_error( "Read error", errno ); cleanup_and_fail(); }
|
2025-02-24 03:54:35 +01:00
|
|
|
member_pos += size;
|
|
|
|
member_rest -= size;
|
|
|
|
if( LZ_decompress_write( decoder, ibuffer, size ) != size )
|
2025-02-24 04:04:02 +01:00
|
|
|
internal_error( "library error (LZ_decompress_write)." );
|
2025-02-24 03:54:35 +01:00
|
|
|
}
|
|
|
|
if( member_rest <= 0 ) { LZ_decompress_finish( decoder ); break; }
|
2025-02-24 03:26:46 +01:00
|
|
|
}
|
2025-02-24 03:54:35 +01:00
|
|
|
while( true ) // write decompressed data to file
|
2025-02-24 03:26:46 +01:00
|
|
|
{
|
2025-02-24 03:54:35 +01:00
|
|
|
const int rd = LZ_decompress_read( decoder, obuffer, buffer_size );
|
2025-02-24 03:26:46 +01:00
|
|
|
if( rd < 0 )
|
2025-02-24 04:01:20 +01:00
|
|
|
cleanup_and_fail( decompress_read_error( decoder, pp, worker_id ) );
|
2025-02-24 03:54:35 +01:00
|
|
|
if( rd > 0 && outfd >= 0 )
|
2025-02-24 03:26:46 +01:00
|
|
|
{
|
2025-02-24 03:54:35 +01:00
|
|
|
const int wr = pwriteblock( outfd, obuffer, rd, data_pos );
|
|
|
|
if( wr != rd )
|
2025-02-24 03:26:46 +01:00
|
|
|
{
|
2025-02-24 03:54:35 +01:00
|
|
|
pp();
|
|
|
|
if( verbosity >= 0 )
|
2025-02-24 04:09:55 +01:00
|
|
|
std::fprintf( stderr, "Write error in worker %d: %s\n",
|
2025-02-24 03:54:35 +01:00
|
|
|
worker_id, std::strerror( errno ) );
|
2025-02-24 04:01:20 +01:00
|
|
|
cleanup_and_fail();
|
2025-02-24 03:26:46 +01:00
|
|
|
}
|
|
|
|
}
|
2025-02-24 03:54:35 +01:00
|
|
|
if( rd > 0 )
|
|
|
|
{
|
|
|
|
data_pos += rd;
|
|
|
|
data_rest -= rd;
|
|
|
|
}
|
|
|
|
if( LZ_decompress_finished( decoder ) == 1 )
|
|
|
|
{
|
|
|
|
if( data_rest != 0 )
|
2025-02-24 04:04:02 +01:00
|
|
|
internal_error( "final data_rest is not zero." );
|
2025-02-24 03:54:35 +01:00
|
|
|
LZ_decompress_reset( decoder ); // prepare for new member
|
|
|
|
break;
|
|
|
|
}
|
2025-02-24 03:26:46 +01:00
|
|
|
if( rd == 0 ) break;
|
|
|
|
}
|
|
|
|
}
|
2025-02-24 03:25:50 +01:00
|
|
|
}
|
2025-02-24 03:26:46 +01:00
|
|
|
|
2025-02-24 03:54:35 +01:00
|
|
|
delete[] obuffer; delete[] ibuffer;
|
2025-02-24 03:42:28 +01:00
|
|
|
if( LZ_decompress_member_position( decoder ) != 0 )
|
2025-02-24 04:04:02 +01:00
|
|
|
{ pp( "Error, some data remains in decoder." ); cleanup_and_fail(); }
|
2025-02-24 03:42:28 +01:00
|
|
|
if( LZ_decompress_close( decoder ) < 0 )
|
2025-02-24 04:04:02 +01:00
|
|
|
{ pp( "LZ_decompress_close failed." ); cleanup_and_fail(); }
|
2025-02-24 03:26:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-02-24 03:54:35 +01:00
|
|
|
} // end namespace
|
2025-02-24 03:26:46 +01:00
|
|
|
|
|
|
|
|
2025-02-24 03:54:35 +01:00
|
|
|
// start the workers and wait for them to finish.
|
|
|
|
int decompress( int num_workers, const int infd, const int outfd,
|
|
|
|
const Pretty_print & pp, const int debug_level,
|
2025-02-24 04:12:46 +01:00
|
|
|
const bool ignore_trailing, const bool infd_isreg )
|
2025-02-24 03:54:35 +01:00
|
|
|
{
|
|
|
|
if( !infd_isreg )
|
2025-02-24 04:12:46 +01:00
|
|
|
return dec_stream( num_workers, infd, outfd, pp, debug_level, ignore_trailing );
|
2025-02-24 03:26:46 +01:00
|
|
|
|
2025-02-24 04:12:46 +01:00
|
|
|
const File_index file_index( infd, ignore_trailing );
|
2025-02-24 03:54:35 +01:00
|
|
|
if( file_index.retval() == 1 )
|
|
|
|
{
|
|
|
|
lseek( infd, 0, SEEK_SET );
|
2025-02-24 04:12:46 +01:00
|
|
|
return dec_stream( num_workers, infd, outfd, pp, debug_level, ignore_trailing );
|
2025-02-24 03:26:46 +01:00
|
|
|
}
|
2025-02-24 03:54:35 +01:00
|
|
|
if( file_index.retval() != 0 )
|
2025-02-24 04:01:20 +01:00
|
|
|
{ pp( file_index.error().c_str() ); return file_index.retval(); }
|
2025-02-24 03:26:46 +01:00
|
|
|
|
2025-02-24 04:07:51 +01:00
|
|
|
show_header( file_index.dictionary_size( 0 ) );
|
2025-02-24 03:54:35 +01:00
|
|
|
if( num_workers > file_index.members() )
|
|
|
|
num_workers = file_index.members();
|
2025-02-24 03:26:46 +01:00
|
|
|
|
2025-02-24 03:54:35 +01:00
|
|
|
if( outfd >= 0 )
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if( fstat( outfd, &st ) != 0 || !S_ISREG( st.st_mode ) ||
|
|
|
|
lseek( outfd, 0, SEEK_CUR ) < 0 )
|
|
|
|
return dec_stdout( num_workers, infd, outfd, pp, debug_level, file_index );
|
|
|
|
}
|
2025-02-24 03:26:46 +01:00
|
|
|
|
|
|
|
Worker_arg * worker_args = new( std::nothrow ) Worker_arg[num_workers];
|
|
|
|
pthread_t * worker_threads = new( std::nothrow ) pthread_t[num_workers];
|
2025-02-24 03:54:35 +01:00
|
|
|
if( !worker_args || !worker_threads )
|
2025-02-24 04:04:02 +01:00
|
|
|
{ pp( "Not enough memory." ); cleanup_and_fail(); }
|
2025-02-24 03:26:46 +01:00
|
|
|
for( int i = 0; i < num_workers; ++i )
|
|
|
|
{
|
2025-02-24 03:54:35 +01:00
|
|
|
worker_args[i].file_index = &file_index;
|
2025-02-24 03:26:46 +01:00
|
|
|
worker_args[i].pp = &pp;
|
|
|
|
worker_args[i].worker_id = i;
|
2025-02-24 03:54:35 +01:00
|
|
|
worker_args[i].num_workers = num_workers;
|
|
|
|
worker_args[i].infd = infd;
|
|
|
|
worker_args[i].outfd = outfd;
|
|
|
|
const int errcode =
|
|
|
|
pthread_create( &worker_threads[i], 0, dworker, &worker_args[i] );
|
2025-02-24 03:27:22 +01:00
|
|
|
if( errcode )
|
2025-02-24 04:01:20 +01:00
|
|
|
{ show_error( "Can't create worker threads", errcode ); cleanup_and_fail(); }
|
2025-02-24 03:26:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for( int i = num_workers - 1; i >= 0; --i )
|
2025-02-24 03:27:22 +01:00
|
|
|
{
|
2025-02-24 03:54:35 +01:00
|
|
|
const int errcode = pthread_join( worker_threads[i], 0 );
|
2025-02-24 03:27:22 +01:00
|
|
|
if( errcode )
|
2025-02-24 04:01:20 +01:00
|
|
|
{ show_error( "Can't join worker threads", errcode ); cleanup_and_fail(); }
|
2025-02-24 03:27:22 +01:00
|
|
|
}
|
2025-02-24 03:54:35 +01:00
|
|
|
delete[] worker_threads;
|
|
|
|
delete[] worker_args;
|
2025-02-24 03:26:46 +01:00
|
|
|
|
2025-02-24 03:54:35 +01:00
|
|
|
const unsigned long long in_size = file_index.file_end();
|
|
|
|
const unsigned long long out_size = file_index.data_end();
|
|
|
|
if( verbosity >= 2 && out_size > 0 && in_size > 0 )
|
2025-02-24 03:42:28 +01:00
|
|
|
std::fprintf( stderr, "%6.3f:1, %6.3f bits/byte, %5.2f%% saved. ",
|
|
|
|
(double)out_size / in_size,
|
|
|
|
( 8.0 * in_size ) / out_size,
|
|
|
|
100.0 * ( 1.0 - ( (double)in_size / out_size ) ) );
|
2025-02-24 04:07:51 +01:00
|
|
|
if( verbosity >= 4 )
|
2025-02-24 03:54:35 +01:00
|
|
|
std::fprintf( stderr, "decompressed size %9llu, size %9llu. ",
|
2025-02-24 03:26:46 +01:00
|
|
|
out_size, in_size );
|
|
|
|
|
2025-02-24 04:09:55 +01:00
|
|
|
if( verbosity >= 1 ) std::fputs( (outfd < 0) ? "ok\n" : "done\n", stderr );
|
2025-02-24 03:54:35 +01:00
|
|
|
|
2025-02-24 03:26:46 +01:00
|
|
|
return 0;
|
2025-02-24 03:25:50 +01:00
|
|
|
}
|