summaryrefslogtreecommitdiffstats
path: root/delete.cc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--delete.cc77
1 files changed, 37 insertions, 40 deletions
diff --git a/delete.cc b/delete.cc
index 234864c..0d8abde 100644
--- a/delete.cc
+++ b/delete.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,8 +31,8 @@
#include <lzlib.h>
#include "arg_parser.h"
-#include "lzip_index.h"
#include "tarlz.h"
+#include "lzip_index.h"
namespace {
@@ -90,7 +90,7 @@ int tail_copy( const char * const archive_namep, const Arg_parser & parser,
retval = 1; }
if( retval == 0 ) for( int i = 0; i < parser.arguments(); ++i )
- if( !parser.code( i ) && parser.argument( i ).size() && name_pending[i] )
+ if( nonempty_arg( parser, i ) && name_pending[i] )
{
show_file_error( parser.argument( i ).c_str(), "Not found in archive." );
retval = 1;
@@ -100,32 +100,31 @@ int tail_copy( const char * const archive_namep, const Arg_parser & parser,
/* Deleting from a corrupt archive must not worsen the corruption. Stop and
- tail-copy as soon as corruption is found. */
-int delete_members( const std::string & archive_name, const Arg_parser & parser,
- const int filenames, const bool missing_crc,
- const bool permissive )
+ tail-copy as soon as corruption is found.
+*/
+int delete_members( const Cl_options & cl_opts )
{
- if( !filenames )
+ if( cl_opts.filenames <= 0 )
{ if( verbosity >= 1 ) show_error( "Nothing to delete." ); return 0; }
- if( archive_name.empty() )
+ if( cl_opts.archive_name.empty() )
{ show_error( "Deleting from stdin not implemented yet." ); return 1; }
- const char * const archive_namep = archive_name.c_str();
- const int infd = open_instream( archive_name );
+ 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 int outfd = open_outstream( archive_name, false );
+ const int outfd = open_outstream( cl_opts.archive_name, false );
if( outfd < 0 ) { close( infd ); return 1; }
// mark member names to be deleted
- std::vector< char > name_pending( parser.arguments(), false );
- for( int i = 0; i < parser.arguments(); ++i )
- if( !parser.code( i ) && parser.argument( i ).size() &&
- !Exclude::excluded( parser.argument( i ).c_str() ) )
+ std::vector< char > name_pending( cl_opts.parser.arguments(), false );
+ for( int i = 0; i < cl_opts.parser.arguments(); ++i )
+ if( nonempty_arg( cl_opts.parser, i ) &&
+ !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( archive_namep, parser, name_pending, lzip_index,
- filenames, infd, outfd, missing_crc, permissive );
+ 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 )
@@ -153,7 +152,7 @@ int delete_members( const std::string & archive_name, const Arg_parser & parser,
{
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; }
break;
}
@@ -164,7 +163,7 @@ int delete_members( const std::string & archive_name, const Arg_parser & parser,
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; break; }
Extended dummy; // global headers are parsed and ignored
if( !parse_records( infd, dummy, header, rbuf, true ) )
@@ -173,11 +172,11 @@ int delete_members( const std::string & archive_name, const Arg_parser & parser,
}
if( typeflag == tf_extended )
{
- if( prev_extended && !permissive )
+ if( prev_extended && !cl_opts.permissive )
{ show_file_error( archive_namep, fv_msg3 ); retval = 2; break; }
- if( !parse_records( infd, extended, header, rbuf, permissive ) )
+ if( !parse_records( infd, extended, header, rbuf, cl_opts.permissive ) )
{ show_file_error( archive_namep, extrec_msg ); retval = 2; break; }
- else if( !extended.crc_present() && missing_crc )
+ else if( !extended.crc_present() && cl_opts.missing_crc )
{ show_file_error( archive_namep, mcrc_msg ); retval = 2; break; }
prev_extended = true;
continue;
@@ -187,16 +186,14 @@ int delete_members( const std::string & archive_name, const Arg_parser & parser,
extended.fill_from_ustar( header ); // copy metadata from header
{ // skip member
- 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( lseek( infd, rest, SEEK_CUR ) <= 0 )
{ show_file_error( archive_namep, "Seek error", errno );
retval = 1; break; }
}
- 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() ) )
{
if( !show_member_name( extended, header, 1, rbuf ) )
{ retval = 1; break; }
@@ -218,6 +215,6 @@ int delete_members( const std::string & archive_name, const Arg_parser & parser,
extended.reset();
}
- 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 );
}