summaryrefslogtreecommitdiffstats
path: root/delete.cc
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2021-01-27 16:07:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2021-01-27 16:07:31 +0000
commitadfd78d2ce2ac3feab8926823ec6349d79a83437 (patch)
treec876cb9bf2908ca4082c65f20a380b1e13172e44 /delete.cc
parentAdding upstream version 0.17. (diff)
downloadtarlz-adfd78d2ce2ac3feab8926823ec6349d79a83437.tar.xz
tarlz-adfd78d2ce2ac3feab8926823ec6349d79a83437.zip
Adding upstream version 0.19.upstream/0.19
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'delete.cc')
-rw-r--r--delete.cc123
1 files changed, 50 insertions, 73 deletions
diff --git a/delete.cc b/delete.cc
index 0d8abde..e9645d8 100644
--- a/delete.cc
+++ b/delete.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,24 +33,7 @@
#include "arg_parser.h"
#include "tarlz.h"
#include "lzip_index.h"
-
-
-namespace {
-
-bool parse_records( const int infd, Extended & extended,
- const Tar_header header, Resizable_buffer & rbuf,
- 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 false; // overflow or no extended data
- if( !rbuf.resize( bufsize ) ) return false; // extended records buffer
- return ( readblock( infd, (uint8_t *)rbuf(), bufsize ) == bufsize &&
- extended.parse( rbuf(), edsize, permissive ) );
- }
-
-} // end namespace
+#include "archive_reader.h"
bool safe_seek( const int fd, const long long pos )
@@ -60,34 +43,32 @@ bool safe_seek( const int fd, const long long pos )
}
-int tail_copy( const char * const archive_namep, const Arg_parser & parser,
- std::vector< char > & name_pending,
- const Lzip_index & lzip_index, const long long istream_pos,
- const int infd, const int outfd, int retval )
+int tail_copy( const Arg_parser & parser, const Archive_descriptor & ad,
+ std::vector< char > & name_pending, const long long istream_pos,
+ const int outfd, int retval )
{
- const long long rest = lzip_index.file_size() - istream_pos;
+ const long long rest = ad.lzip_index.file_size() - istream_pos;
if( istream_pos > 0 && rest > 0 &&
- ( !safe_seek( infd, istream_pos ) ||
- !copy_file( infd, outfd, rest ) ) )
- { show_file_error( archive_namep, "Error during tail copy." );
+ ( !safe_seek( ad.infd, istream_pos ) ||
+ !copy_file( ad.infd, outfd, rest ) ) )
+ { show_file_error( ad.namep, "Error during tail copy." );
return retval ? retval : 1; }
const long long ostream_pos = lseek( outfd, 0, SEEK_CUR );
if( ostream_pos < 0 ) { show_error( "Seek error", errno ); retval = 1; }
- else if( ostream_pos > 0 && ostream_pos < lzip_index.file_size() )
+ else if( ostream_pos > 0 && ostream_pos < ad.lzip_index.file_size() )
{
int ret;
do ret = ftruncate( outfd, ostream_pos );
while( ret != 0 && errno == EINTR );
if( ret != 0 || lseek( outfd, 0, SEEK_END ) != ostream_pos )
{
- show_file_error( archive_namep, "Can't truncate archive", errno );
+ show_file_error( ad.namep, "Can't truncate archive", errno );
if( retval < 1 ) retval = 1;
}
}
- if( ( close( outfd ) != 0 || close( infd ) != 0 ) && !retval )
- { show_file_error( archive_namep, "Error closing archive", errno );
- retval = 1; }
+ if( ( close( outfd ) != 0 || close( ad.infd ) != 0 ) && !retval )
+ { show_file_error( ad.namep, "Error closing archive", errno ); retval = 1; }
if( retval == 0 ) for( int i = 0; i < parser.arguments(); ++i )
if( nonempty_arg( parser, i ) && name_pending[i] )
@@ -108,11 +89,10 @@ int delete_members( const Cl_options & cl_opts )
{ if( verbosity >= 1 ) show_error( "Nothing to delete." ); return 0; }
if( cl_opts.archive_name.empty() )
{ show_error( "Deleting from stdin not implemented yet." ); return 1; }
- const char * const archive_namep = cl_opts.archive_name.c_str();
- const int infd = open_instream( cl_opts.archive_name );
- if( infd < 0 ) return 1;
+ const Archive_descriptor ad( cl_opts.archive_name );
+ if( ad.infd < 0 ) return 1;
const int outfd = open_outstream( cl_opts.archive_name, false );
- if( outfd < 0 ) { close( infd ); return 1; }
+ if( outfd < 0 ) { close( ad.infd ); return 1; }
// mark member names to be deleted
std::vector< char > name_pending( cl_opts.parser.arguments(), false );
@@ -121,16 +101,15 @@ int delete_members( const Cl_options & cl_opts )
!Exclude::excluded( cl_opts.parser.argument( i ).c_str() ) )
name_pending[i] = true;
- const Lzip_index lzip_index( infd, true, false ); // only regular files
- if( lzip_index.retval() == 0 ) // compressed
- return delete_members_lz( cl_opts, archive_namep, name_pending, lzip_index,
- infd, outfd );
- if( lseek( infd, 0, SEEK_SET ) != 0 )
- { show_file_error( archive_namep, "Archive is not seekable." ); return 1; }
- if( lzip_index.file_size() < 3 * header_size )
- { show_file_error( archive_namep, posix_msg ); return 2; }
+ if( ad.indexed ) // archive is a compressed regular file
+ return delete_members_lz( cl_opts, ad, name_pending, outfd );
+ if( !ad.seekable )
+ { show_file_error( ad.namep, "Archive is not seekable." ); return 1; }
+ if( ad.lzip_index.file_size() < 3 * header_size )
+ { show_file_error( ad.namep, posix_msg ); return 2; }
// archive is uncompressed seekable, unless compressed corrupt
+ Archive_reader ar( ad );
Resizable_buffer rbuf;
long long istream_pos = 0; // source of next data move
long long member_begin = 0; // first pos of current tar member
@@ -139,24 +118,21 @@ int delete_members( const Cl_options & cl_opts )
bool prev_extended = false; // prev header was extended
while( true ) // process one tar header per iteration
{
- if( !prev_extended && ( member_begin = lseek( infd, 0, SEEK_CUR ) ) < 0 )
+ if( !prev_extended && ( member_begin = lseek( ad.infd, 0, SEEK_CUR ) ) < 0 )
{ show_error( "Seek error", errno ); retval = 1; break; }
Tar_header header;
- const int rd = readblock( infd, header, header_size );
- if( rd == 0 && errno == 0 ) // missing EOF blocks
- { show_file_error( archive_namep, end_msg ); retval = 2; break; }
- if( rd != header_size )
- { show_file_error( archive_namep, "Read error", errno );
- retval = 2; break; }
+ const int ret = ar.read( header, header_size );
+ if( ret != 0 ) { show_file_error( ar.ad.namep, ar.e_msg(), ar.e_code() );
+ retval = ret; break; }
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; }
break;
}
- show_file_error( archive_namep, "Corrupt header in archive." );
+ show_file_error( ad.namep, "Corrupt header in archive." );
retval = 2; break;
}
@@ -164,20 +140,23 @@ int delete_members( const Cl_options & cl_opts )
if( typeflag == tf_global )
{
if( prev_extended && !cl_opts.permissive )
- { show_file_error( archive_namep, fv_msg2 ); retval = 2; break; }
+ { show_file_error( ad.namep, fv_msg2 ); retval = 2; break; }
Extended dummy; // global headers are parsed and ignored
- if( !parse_records( infd, dummy, header, rbuf, true ) )
- { show_file_error( archive_namep, gblrec_msg ); retval = 2; break; }
+ const int ret = ar.parse_records( dummy, header, rbuf, true );
+ if( ret != 0 )
+ { show_file_error( ad.namep, gblrec_msg ); retval = ret; break; }
continue;
}
if( typeflag == tf_extended )
{
if( prev_extended && !cl_opts.permissive )
- { show_file_error( archive_namep, fv_msg3 ); retval = 2; break; }
- if( !parse_records( infd, extended, header, rbuf, cl_opts.permissive ) )
- { show_file_error( archive_namep, extrec_msg ); retval = 2; break; }
+ { show_file_error( ad.namep, fv_msg3 ); retval = 2; break; }
+ const int ret = ar.parse_records( extended, header, rbuf,
+ cl_opts.permissive );
+ if( ret != 0 )
+ { show_file_error( ad.namep, extrec_msg ); retval = ret; break; }
else if( !extended.crc_present() && cl_opts.missing_crc )
- { show_file_error( archive_namep, mcrc_msg ); retval = 2; break; }
+ { show_file_error( ad.namep, mcrc_msg ); retval = 2; break; }
prev_extended = true;
continue;
}
@@ -185,11 +164,10 @@ int delete_members( const Cl_options & cl_opts )
extended.fill_from_ustar( header ); // copy metadata from header
- { // skip member
- long long rest = round_up( extended.file_size() ); // size + padding
- if( lseek( infd, rest, SEEK_CUR ) <= 0 )
- { show_file_error( archive_namep, "Seek error", errno );
- retval = 1; break; }
+ {
+ const int ret = ar.skip_member( extended );
+ if( ret != 0 )
+ { show_file_error( ad.namep, "Seek error", errno ); retval = ret; break; }
}
// delete tar member
@@ -197,24 +175,23 @@ int delete_members( const Cl_options & cl_opts )
{
if( !show_member_name( extended, header, 1, rbuf ) )
{ retval = 1; break; }
- const long long pos = lseek( infd, 0, SEEK_CUR );
+ const long long pos = lseek( ad.infd, 0, SEEK_CUR );
if( pos <= 0 || pos <= member_begin || member_begin < istream_pos )
- { show_file_error( archive_namep, "Seek error", errno );
+ { show_file_error( ad.namep, "Seek error", errno );
retval = 1; break; }
const long long size = member_begin - 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; break; } }
- else if( !safe_seek( infd, istream_pos ) ||
- !copy_file( infd, outfd, size ) ||
- !safe_seek( infd, pos ) ) { retval = 1; break; }
+ else if( !safe_seek( ad.infd, istream_pos ) ||
+ !copy_file( ad.infd, outfd, size ) ||
+ !safe_seek( ad.infd, pos ) ) { retval = 1; break; }
}
istream_pos = pos;
}
extended.reset();
}
- 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 );
}