2025-02-17 21:15:18 +01:00
|
|
|
/* Tarlz - Archiver with multimember lzip compression
|
2025-03-04 07:39:30 +01:00
|
|
|
Copyright (C) 2013-2025 Antonio Diaz Diaz.
|
2025-02-17 21:13:41 +01:00
|
|
|
|
2025-02-17 21:15:18 +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
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2025-02-17 21:13:41 +01:00
|
|
|
|
2025-02-17 21:15:18 +01:00
|
|
|
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.
|
2025-02-17 21:13:41 +01:00
|
|
|
|
2025-02-17 21:15:18 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2025-02-17 21:13:41 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define _FILE_OFFSET_BITS 64
|
|
|
|
|
|
|
|
#include <cctype>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio>
|
2025-02-17 21:27:57 +01:00
|
|
|
#include <stdint.h> // for lzlib.h
|
2025-02-17 21:13:41 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <lzlib.h>
|
|
|
|
|
|
|
|
#include "tarlz.h"
|
2025-02-17 21:16:04 +01:00
|
|
|
#include "arg_parser.h"
|
2025-02-17 21:15:18 +01:00
|
|
|
#include "lzip_index.h"
|
2025-02-17 21:15:39 +01:00
|
|
|
#include "archive_reader.h"
|
2025-02-17 21:13:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
bool safe_seek( const int fd, const long long pos )
|
|
|
|
{
|
|
|
|
if( lseek( fd, pos, SEEK_SET ) == pos ) return true;
|
2025-02-17 21:17:43 +01:00
|
|
|
show_error( seek_msg, errno ); return false;
|
2025-02-17 21:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-17 21:15:39 +01:00
|
|
|
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 )
|
2025-02-17 21:13:41 +01:00
|
|
|
{
|
2025-02-17 21:15:39 +01:00
|
|
|
const long long rest = ad.lzip_index.file_size() - istream_pos;
|
2025-02-17 21:13:41 +01:00
|
|
|
if( istream_pos > 0 && rest > 0 &&
|
2025-02-17 21:15:39 +01:00
|
|
|
( !safe_seek( ad.infd, istream_pos ) ||
|
2025-02-17 21:27:57 +01:00
|
|
|
!copy_file( ad.infd, outfd, ad.namep, rest ) ) )
|
2025-02-17 21:15:39 +01:00
|
|
|
{ show_file_error( ad.namep, "Error during tail copy." );
|
2025-02-17 21:13:41 +01:00
|
|
|
return retval ? retval : 1; }
|
|
|
|
const long long ostream_pos = lseek( outfd, 0, SEEK_CUR );
|
2025-02-17 21:17:43 +01:00
|
|
|
if( ostream_pos < 0 )
|
|
|
|
{ show_file_error( ad.namep, seek_msg, errno ); retval = 1; }
|
2025-02-17 21:15:39 +01:00
|
|
|
else if( ostream_pos > 0 && ostream_pos < ad.lzip_index.file_size() )
|
2025-02-17 21:13:41 +01:00
|
|
|
{
|
2025-02-17 21:14:17 +01:00
|
|
|
int ret;
|
|
|
|
do ret = ftruncate( outfd, ostream_pos );
|
|
|
|
while( ret != 0 && errno == EINTR );
|
|
|
|
if( ret != 0 || lseek( outfd, 0, SEEK_END ) != ostream_pos )
|
2025-02-17 21:13:41 +01:00
|
|
|
{
|
2025-02-17 21:15:39 +01:00
|
|
|
show_file_error( ad.namep, "Can't truncate archive", errno );
|
2025-02-17 21:13:41 +01:00
|
|
|
if( retval < 1 ) retval = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-17 21:17:43 +01:00
|
|
|
if( ( close( outfd ) | close( ad.infd ) ) != 0 && retval == 0 )
|
|
|
|
{ show_file_error( ad.namep, eclosa_msg, errno ); retval = 1; }
|
2025-02-17 21:13:41 +01:00
|
|
|
|
2025-02-17 21:17:43 +01:00
|
|
|
if( retval == 0 )
|
|
|
|
for( int i = 0; i < parser.arguments(); ++i )
|
|
|
|
if( nonempty_arg( parser, i ) && name_pending[i] )
|
|
|
|
{ show_file_error( parser.argument( i ).c_str(), nfound_msg );
|
|
|
|
retval = 1; }
|
2025-02-17 21:13:41 +01:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Deleting from a corrupt archive must not worsen the corruption. Stop and
|
2025-02-17 21:15:18 +01:00
|
|
|
tail-copy as soon as corruption is found.
|
|
|
|
*/
|
|
|
|
int delete_members( const Cl_options & cl_opts )
|
2025-02-17 21:13:41 +01:00
|
|
|
{
|
2025-02-17 21:16:04 +01:00
|
|
|
if( cl_opts.num_files <= 0 )
|
2025-02-17 21:13:41 +01:00
|
|
|
{ if( verbosity >= 1 ) show_error( "Nothing to delete." ); return 0; }
|
2025-02-17 21:15:18 +01:00
|
|
|
if( cl_opts.archive_name.empty() )
|
2025-02-17 21:13:41 +01:00
|
|
|
{ show_error( "Deleting from stdin not implemented yet." ); return 1; }
|
2025-02-17 21:15:39 +01:00
|
|
|
const Archive_descriptor ad( cl_opts.archive_name );
|
|
|
|
if( ad.infd < 0 ) return 1;
|
2025-02-17 21:27:57 +01:00
|
|
|
if( ad.name.size() && ad.indexed && ad.lzip_index.multi_empty() )
|
|
|
|
{ show_file_error( ad.namep, empty_msg ); close( ad.infd ); return 2; }
|
2025-02-17 21:15:18 +01:00
|
|
|
const int outfd = open_outstream( cl_opts.archive_name, false );
|
2025-02-17 21:15:39 +01:00
|
|
|
if( outfd < 0 ) { close( ad.infd ); return 1; }
|
2025-02-17 21:13:41 +01:00
|
|
|
|
|
|
|
// mark member names to be deleted
|
2025-02-17 21:15:18 +01:00
|
|
|
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() ) )
|
2025-02-17 21:13:41 +01:00
|
|
|
name_pending[i] = true;
|
|
|
|
|
2025-02-17 21:15:39 +01:00
|
|
|
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 )
|
2025-02-17 21:27:15 +01:00
|
|
|
{ show_file_error( ad.namep, has_lz_ext( ad.name ) ?
|
|
|
|
posix_lz_msg : posix_msg ); return 2; }
|
2025-02-17 21:13:41 +01:00
|
|
|
// archive is uncompressed seekable, unless compressed corrupt
|
|
|
|
|
2025-02-17 21:17:43 +01:00
|
|
|
Archive_reader ar( ad ); // serial reader
|
2025-02-17 21:13:41 +01:00
|
|
|
Resizable_buffer rbuf;
|
|
|
|
long long istream_pos = 0; // source of next data move
|
|
|
|
long long member_begin = 0; // first pos of current tar member
|
|
|
|
Extended extended; // metadata from extended records
|
|
|
|
int retval = 0;
|
|
|
|
bool prev_extended = false; // prev header was extended
|
2025-02-17 21:17:43 +01:00
|
|
|
if( !rbuf.size() ) { show_error( mem_msg ); return 1; }
|
|
|
|
|
2025-02-17 21:13:41 +01:00
|
|
|
while( true ) // process one tar header per iteration
|
|
|
|
{
|
2025-02-17 21:15:39 +01:00
|
|
|
if( !prev_extended && ( member_begin = lseek( ad.infd, 0, SEEK_CUR ) ) < 0 )
|
2025-02-17 21:17:43 +01:00
|
|
|
{ show_file_error( ad.namep, seek_msg, errno ); retval = 1; break; }
|
2025-02-17 21:13:41 +01:00
|
|
|
Tar_header header;
|
2025-02-17 21:17:43 +01:00
|
|
|
if( ( retval = ar.read( header, header_size ) ) != 0 )
|
|
|
|
{ show_file_error( ad.namep, ar.e_msg(), ar.e_code() ); break; }
|
2025-02-17 21:27:15 +01:00
|
|
|
if( !check_ustar_chksum( header ) ) // error or EOA
|
2025-02-17 21:13:41 +01:00
|
|
|
{
|
2025-02-17 21:17:43 +01:00
|
|
|
if( block_is_zero( header, header_size ) ) // EOA
|
2025-02-17 21:13:41 +01:00
|
|
|
{
|
2025-02-17 21:15:18 +01:00
|
|
|
if( prev_extended && !cl_opts.permissive )
|
2025-02-17 21:15:39 +01:00
|
|
|
{ show_file_error( ad.namep, fv_msg1 ); retval = 2; }
|
2025-02-17 21:13:41 +01:00
|
|
|
break;
|
|
|
|
}
|
2025-02-17 21:27:15 +01:00
|
|
|
// posix format already checked by archive reader
|
2025-02-17 21:17:43 +01:00
|
|
|
show_file_error( ad.namep, bad_hdr_msg );
|
2025-02-17 21:13:41 +01:00
|
|
|
retval = 2; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Typeflag typeflag = (Typeflag)header[typeflag_o];
|
|
|
|
if( typeflag == tf_global )
|
|
|
|
{
|
2025-02-17 21:15:18 +01:00
|
|
|
if( prev_extended && !cl_opts.permissive )
|
2025-02-17 21:15:39 +01:00
|
|
|
{ show_file_error( ad.namep, fv_msg2 ); retval = 2; break; }
|
2025-02-17 21:13:41 +01:00
|
|
|
Extended dummy; // global headers are parsed and ignored
|
2025-02-17 21:17:43 +01:00
|
|
|
retval = ar.parse_records( dummy, header, rbuf, gblrec_msg, true );
|
|
|
|
if( retval )
|
|
|
|
{ show_file_error( ad.namep, ar.e_msg(), ar.e_code() ); break; }
|
2025-02-17 21:13:41 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if( typeflag == tf_extended )
|
|
|
|
{
|
2025-02-17 21:15:18 +01:00
|
|
|
if( prev_extended && !cl_opts.permissive )
|
2025-02-17 21:15:39 +01:00
|
|
|
{ show_file_error( ad.namep, fv_msg3 ); retval = 2; break; }
|
2025-02-17 21:17:43 +01:00
|
|
|
if( ( retval = ar.parse_records( extended, header, rbuf, extrec_msg,
|
|
|
|
cl_opts.permissive ) ) != 0 )
|
|
|
|
{ show_file_error( ad.namep, ar.e_msg(), ar.e_code() ); break; }
|
|
|
|
if( !extended.crc_present() && cl_opts.missing_crc )
|
|
|
|
{ show_file_error( ad.namep, miscrc_msg ); retval = 2; break; }
|
|
|
|
prev_extended = true; continue;
|
2025-02-17 21:13:41 +01:00
|
|
|
}
|
|
|
|
prev_extended = false;
|
|
|
|
|
|
|
|
extended.fill_from_ustar( header ); // copy metadata from header
|
|
|
|
|
2025-02-17 21:17:43 +01:00
|
|
|
if( ( retval = ar.skip_member( extended ) ) != 0 )
|
|
|
|
{ show_file_error( ad.namep, seek_msg, errno ); break; }
|
2025-02-17 21:13:41 +01:00
|
|
|
|
2025-02-17 21:15:18 +01:00
|
|
|
// delete tar member
|
|
|
|
if( !check_skip_filename( cl_opts, name_pending, extended.path().c_str() ) )
|
2025-02-17 21:13:41 +01:00
|
|
|
{
|
2025-02-17 21:17:43 +01:00
|
|
|
print_removed_prefix( extended.removed_prefix );
|
2025-02-17 21:13:41 +01:00
|
|
|
if( !show_member_name( extended, header, 1, rbuf ) )
|
|
|
|
{ retval = 1; break; }
|
2025-02-17 21:15:39 +01:00
|
|
|
const long long pos = lseek( ad.infd, 0, SEEK_CUR );
|
2025-02-17 21:13:41 +01:00
|
|
|
if( pos <= 0 || pos <= member_begin || member_begin < istream_pos )
|
2025-02-17 21:17:43 +01:00
|
|
|
{ show_file_error( ad.namep, seek_msg, errno ); retval = 1; break; }
|
2025-02-17 21:13:41 +01:00
|
|
|
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; } }
|
2025-02-17 21:15:39 +01:00
|
|
|
else if( !safe_seek( ad.infd, istream_pos ) ||
|
2025-02-17 21:27:57 +01:00
|
|
|
!copy_file( ad.infd, outfd, ad.namep, size ) ||
|
2025-02-17 21:15:39 +01:00
|
|
|
!safe_seek( ad.infd, pos ) ) { retval = 1; break; }
|
2025-02-17 21:13:41 +01:00
|
|
|
}
|
|
|
|
istream_pos = pos;
|
|
|
|
}
|
|
|
|
extended.reset();
|
|
|
|
}
|
|
|
|
|
2025-02-17 21:15:39 +01:00
|
|
|
return tail_copy( cl_opts.parser, ad, name_pending, istream_pos, outfd, retval );
|
2025-02-17 21:13:41 +01:00
|
|
|
}
|