summaryrefslogtreecommitdiffstats
path: root/delete_lz.cc
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2021-01-27 16:07:35 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2021-01-27 16:08:24 +0000
commit2a00d366f34bfdfa7e5a3019b4753bc94a80748d (patch)
treefad13d976fa52e336b4bb0b85eff6de1350c9906 /delete_lz.cc
parentReleasing debian version 0.17-1. (diff)
downloadtarlz-2a00d366f34bfdfa7e5a3019b4753bc94a80748d.tar.xz
tarlz-2a00d366f34bfdfa7e5a3019b4753bc94a80748d.zip
Merging upstream version 0.19.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'delete_lz.cc')
-rw-r--r--delete_lz.cc172
1 files changed, 32 insertions, 140 deletions
diff --git a/delete_lz.cc b/delete_lz.cc
index 961645f..c566c00 100644
--- a/delete_lz.cc
+++ b/delete_lz.cc
@@ -1,5 +1,5 @@
/* Tarlz - Archiver with multimember lzip compression
- Copyright (C) 2013-2020 Antonio Diaz Diaz.
+ Copyright (C) 2013-2021 Antonio Diaz Diaz.
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
@@ -25,7 +25,7 @@
#include <cstring>
#include <string>
#include <vector>
-#include <pthread.h>
+#include <pthread.h> // for tarlz.h
#include <stdint.h>
#include <unistd.h>
#include <lzlib.h>
@@ -33,150 +33,52 @@
#include "arg_parser.h"
#include "tarlz.h"
#include "lzip_index.h"
-
-
-/* Read 'size' decompressed bytes from the archive.
- Return value: 0 = OK, 1 = damaged member, 2 = fatal error. */
-int archive_read_lz( LZ_Decoder * const decoder, const int infd,
- long long & file_pos, const long long member_end,
- const long long cdata_size, uint8_t * const buf,
- const int size, const char ** msg )
- {
- int sz = 0;
-
- while( sz < size )
- {
- const int rd = LZ_decompress_read( decoder, buf + sz, size - sz );
- if( rd < 0 )
- { *msg = LZ_strerror( LZ_decompress_errno( decoder ) ); return 1; }
- if( rd == 0 && LZ_decompress_finished( decoder ) == 1 )
- { *msg = end_msg; return 2; }
- sz += rd;
- if( sz < size && LZ_decompress_write_size( decoder ) > 0 )
- {
- const long long ibuf_size = 16384;
- uint8_t ibuf[ibuf_size];
- const long long rest = ( file_pos < member_end ) ?
- member_end - file_pos : cdata_size - file_pos;
- const int rsize = std::min( LZ_decompress_write_size( decoder ),
- (int)std::min( ibuf_size, rest ) );
- if( rsize <= 0 ) LZ_decompress_finish( decoder );
- else
- {
- const int rd = preadblock( infd, ibuf, rsize, file_pos );
- if( LZ_decompress_write( decoder, ibuf, rd ) != rd )
- internal_error( "library error (LZ_decompress_write)." );
- file_pos += rd;
- if( rd < rsize )
- {
- LZ_decompress_finish( decoder );
- if( errno ) { *msg = "Error reading archive"; return 2; }
- }
- }
- }
- }
- return 0;
- }
-
-
-int parse_records_lz( LZ_Decoder * const decoder, const int infd,
- long long & file_pos, const long long member_end,
- const long long cdata_size, long long & data_pos,
- Extended & extended, const Tar_header header,
- Resizable_buffer & rbuf, const char ** msg,
- const bool permissive )
- {
- const long long edsize = parse_octal( header + size_o, size_l );
- const long long bufsize = round_up( edsize );
- if( edsize <= 0 || edsize >= 1LL << 33 || bufsize >= INT_MAX )
- return 1; // overflow or no extended data
- if( !rbuf.resize( bufsize ) ) return 1; // extended records buffer
- int retval = archive_read_lz( decoder, infd, file_pos, member_end,
- cdata_size, (uint8_t *)rbuf(), bufsize, msg );
- if( retval == 0 )
- { if( extended.parse( rbuf(), edsize, permissive ) ) data_pos += bufsize;
- else retval = 2; }
- return retval;
- }
-
-
-int skip_member_lz( LZ_Decoder * const decoder, const int infd,
- long long & file_pos, const long long member_end,
- const long long cdata_size, long long & data_pos,
- long long rest, const char ** msg )
- {
- const int bufsize = 32 * header_size;
- uint8_t buf[bufsize];
- while( rest > 0 ) // skip tar member
- {
- const int rsize = ( rest >= bufsize ) ? bufsize : rest;
- const int ret = archive_read_lz( decoder, infd, file_pos, member_end,
- cdata_size, buf, rsize, msg );
- if( ret != 0 ) return ret;
- data_pos += rsize;
- rest -= rsize;
- }
- return 0;
- }
+#include "archive_reader.h"
/* Deleting from a corrupt archive must not worsen the corruption. Stop and
tail-copy as soon as corruption is found.
*/
int delete_members_lz( const Cl_options & cl_opts,
- const char * const archive_namep,
+ const Archive_descriptor & ad,
std::vector< char > & name_pending,
- const Lzip_index & lzip_index,
- const int infd, const int outfd )
+ const int outfd )
{
Resizable_buffer rbuf;
- LZ_Decoder * const decoder = LZ_decompress_open();
- if( !rbuf.size() || !decoder || LZ_decompress_errno( decoder ) != LZ_ok )
- { show_error( mem_msg ); return 1; }
+ Archive_reader_i ar( ad ); // indexed reader
+ if( !rbuf.size() || ar.fatal() ) { show_error( mem_msg ); return 1; }
long long istream_pos = 0; // source of next data move
- const long long cdata_size = lzip_index.cdata_size();
int retval = 0, retval2 = 0;
- for( long i = 0; i < lzip_index.members(); ++i )
+ for( long i = 0; i < ad.lzip_index.members(); ++i )
{
- const long long mdata_pos = lzip_index.dblock( i ).pos();
- long long data_pos = mdata_pos;
- const long long mdata_end = lzip_index.dblock( i ).end();
- if( data_pos >= mdata_end ) continue; // empty lzip member
- const long long member_pos = lzip_index.mblock( i ).pos();
- long long file_pos = member_pos;
- const long long member_end = lzip_index.mblock( i ).end();
-
+ if( ad.lzip_index.dblock( i ).size() == 0 ) continue; // empty lzip member
long long member_begin = 0; // first pos of current tar member
Extended extended; // metadata from extended records
bool prev_extended = false; // prev header was extended
- LZ_decompress_reset( decoder ); // prepare for new member
- if( !safe_seek( infd, member_pos ) ) { retval = 1; break; }
+ ar.set_member( i ); // prepare for new member
while( true ) // process one tar header per iteration
{
- if( data_pos >= mdata_end )
+ if( ar.data_pos() >= ar.mdata_end() )
{
- if( data_pos == mdata_end && !prev_extended ) break;
+ if( ar.at_member_end() && !prev_extended ) break;
// member end exceeded or ends in extended
- show_file_error( archive_namep, "Member misalignment found." );
+ show_file_error( ad.namep, "Member misalignment found." );
retval = 2; goto done;
}
- if( !prev_extended ) member_begin = data_pos;
+ if( !prev_extended ) member_begin = ar.data_pos();
Tar_header header;
- const char * msg = 0;
- retval = archive_read_lz( decoder, infd, file_pos, member_end,
- cdata_size, header, header_size, &msg );
- if( retval != 0 ) { show_file_error( archive_namep, msg ); goto done; }
- data_pos += header_size;
+ retval = ar.read( header, header_size );
+ if( retval != 0 ) { show_file_error( ad.namep, ar.e_msg() ); goto done; }
if( !verify_ustar_chksum( header ) )
{
if( block_is_zero( header, header_size ) ) // EOF
{
if( prev_extended && !cl_opts.permissive )
- { show_file_error( archive_namep, fv_msg1 ); retval = 2; }
+ { show_file_error( ad.namep, fv_msg1 ); retval = 2; }
goto done;
}
- show_file_error( archive_namep, ( data_pos > header_size ) ?
+ show_file_error( ad.namep, ( ar.data_pos() > header_size ) ?
bad_hdr_msg : posix_lz_msg );
retval = 2;
goto done;
@@ -186,67 +88,57 @@ int delete_members_lz( const Cl_options & cl_opts,
if( typeflag == tf_global )
{
if( prev_extended && !cl_opts.permissive )
- { show_file_error( archive_namep, fv_msg2 ); retval = 2; goto done; }
+ { show_file_error( ad.namep, fv_msg2 ); retval = 2; goto done; }
Extended dummy; // global headers are parsed and ignored
- retval = parse_records_lz( decoder, infd, file_pos, member_end,
- cdata_size, data_pos, dummy, header,
- rbuf, &msg, true );
+ retval = ar.parse_records( dummy, header, rbuf, true );
if( retval == 0 ) continue;
- show_file_error( archive_namep, gblrec_msg );
+ show_file_error( ad.namep, gblrec_msg );
goto done;
}
if( typeflag == tf_extended )
{
+ const char * msg = 0;
if( prev_extended && !cl_opts.permissive )
{ msg = fv_msg3; retval = 2; }
- else retval = parse_records_lz( decoder, infd, file_pos, member_end,
- cdata_size, data_pos, extended, header,
- rbuf, &msg, cl_opts.permissive );
+ else retval = ar.parse_records( extended, header, rbuf,
+ cl_opts.permissive );
if( retval == 0 && !extended.crc_present() && cl_opts.missing_crc )
{ msg = mcrc_msg; retval = 2; }
if( retval == 0 ) { prev_extended = true; continue; }
- show_file_error( archive_namep, extrec_msg );
+ show_file_error( ad.namep, msg ? msg : extrec_msg );
goto done;
}
prev_extended = false;
extended.fill_from_ustar( header ); // copy metadata from header
- long long rest = round_up( extended.file_size() ); // size + padding
- if( data_pos + rest >= mdata_end ) data_pos += rest;
- else // skip tar member
- if( ( retval = skip_member_lz( decoder, infd, file_pos, member_end,
- cdata_size, data_pos, rest, &msg ) ) != 0 )
- goto done;
+ if( ( retval = ar.skip_member( extended ) ) != 0 ) goto done;
// delete tar member
if( !check_skip_filename( cl_opts, name_pending, extended.path().c_str() ) )
{
// verify that members match
- if( member_begin != mdata_pos || data_pos != mdata_end )
+ if( member_begin != ad.lzip_index.dblock( i ).pos() || !ar.at_member_end() )
{ show_file_error( extended.path().c_str(),
"Can't delete: not compressed individually." );
retval2 = 2; extended.reset(); continue; }
if( !show_member_name( extended, header, 1, rbuf ) )
{ retval = 1; goto done; }
- const long long size = member_pos - istream_pos;
+ const long long size = ad.lzip_index.mblock( i ).pos() - istream_pos;
if( size > 0 ) // move pending data each time a member is deleted
{
if( istream_pos == 0 )
{ if( !safe_seek( outfd, size ) ) { retval = 1; goto done; } }
- else if( !safe_seek( infd, istream_pos ) ||
- !copy_file( infd, outfd, size ) ) { retval = 1; goto done; }
+ else if( !safe_seek( ad.infd, istream_pos ) ||
+ !copy_file( ad.infd, outfd, size ) ) { retval = 1; goto done; }
}
- istream_pos = member_end;
+ istream_pos = ad.lzip_index.mblock( i ).end(); // member end
}
extended.reset();
}
}
done:
if( retval < retval2 ) retval = retval2;
- if( LZ_decompress_close( decoder ) < 0 && !retval )
- { show_error( "LZ_decompress_close failed." ); retval = 1; }
// tail copy keeps trailing data
- return tail_copy( archive_namep, cl_opts.parser, name_pending, lzip_index,
- istream_pos, infd, outfd, retval );
+ return tail_copy( cl_opts.parser, ad, name_pending, istream_pos, outfd, retval );
}