summaryrefslogtreecommitdiffstats
path: root/delete_lz.cc
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2020-08-08 17:10:15 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2020-08-08 17:10:15 +0000
commit3c320348b5b78d2a9923527f9eac089eb1e2778d (patch)
treed17322c3a703eb4683e689482c3dbd3041970180 /delete_lz.cc
parentAdding upstream version 0.16. (diff)
downloadtarlz-56f9c97bf5a20477d12e3480258ac6154c9a8a74.tar.xz
tarlz-56f9c97bf5a20477d12e3480258ac6154c9a8a74.zip
Adding upstream version 0.17.upstream/0.17
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'delete_lz.cc')
-rw-r--r--delete_lz.cc148
1 files changed, 116 insertions, 32 deletions
diff --git a/delete_lz.cc b/delete_lz.cc
index 6888520..961645f 100644
--- a/delete_lz.cc
+++ b/delete_lz.cc
@@ -1,18 +1,18 @@
-/* Tarlz - Archiver with multimember lzip compression
- Copyright (C) 2013-2019 Antonio Diaz Diaz.
+/* Tarlz - Archiver with multimember lzip compression
+ Copyright (C) 2013-2020 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
- the Free Software Foundation, either version 2 of the License, or
- (at your option) any later version.
+ 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
+ the Free Software Foundation, either version 2 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 General Public License for more details.
+ 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/>.
+ 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
@@ -31,18 +31,103 @@
#include <lzlib.h>
#include "arg_parser.h"
-#include "lzip_index.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;
+ }
/* 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 char * const archive_namep,
- const Arg_parser & parser,
+ tail-copy as soon as corruption is found.
+*/
+int delete_members_lz( const Cl_options & cl_opts,
+ const char * const archive_namep,
std::vector< char > & name_pending,
const Lzip_index & lzip_index,
- const int filenames, const int infd, const int outfd,
- const bool missing_crc, const bool permissive )
+ const int infd, const int outfd )
{
Resizable_buffer rbuf;
LZ_Decoder * const decoder = LZ_decompress_open();
@@ -87,7 +172,7 @@ int delete_members_lz( const char * const archive_namep,
{
if( block_is_zero( header, header_size ) ) // EOF
{
- if( prev_extended && !permissive )
+ if( prev_extended && !cl_opts.permissive )
{ show_file_error( archive_namep, fv_msg1 ); retval = 2; }
goto done;
}
@@ -100,48 +185,47 @@ int delete_members_lz( const char * const archive_namep,
const Typeflag typeflag = (Typeflag)header[typeflag_o];
if( typeflag == tf_global )
{
- if( prev_extended && !permissive )
+ if( prev_extended && !cl_opts.permissive )
{ show_file_error( archive_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 );
if( retval == 0 ) continue;
- show_file_error( archive_namep, msg ? msg : gblrec_msg );
+ show_file_error( archive_namep, gblrec_msg );
goto done;
}
if( typeflag == tf_extended )
{
- if( prev_extended && !permissive ) { msg = fv_msg3; retval = 2; }
+ 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, permissive );
- if( retval == 0 && !extended.crc_present() && missing_crc )
+ rbuf, &msg, 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, msg ? msg : extrec_msg );
+ show_file_error( archive_namep, extrec_msg );
goto done;
}
prev_extended = false;
extended.fill_from_ustar( header ); // copy metadata from header
- long long rest = extended.file_size();
- const int rem = rest % header_size;
- if( rem ) rest += header_size - rem; // padding
+ 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( !check_skip_filename( parser, name_pending, extended.path().c_str(),
- filenames ) ) // delete tar member
+ // 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 )
{ show_file_error( extended.path().c_str(),
- "Can't delete: not individually compressed." );
+ "Can't delete: not compressed individually." );
retval2 = 2; extended.reset(); continue; }
if( !show_member_name( extended, header, 1, rbuf ) )
{ retval = 1; goto done; }
@@ -163,6 +247,6 @@ done:
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, parser, name_pending, lzip_index,
+ return tail_copy( archive_namep, cl_opts.parser, name_pending, lzip_index,
istream_pos, infd, outfd, retval );
}