summaryrefslogtreecommitdiffstats
path: root/delete.cc
blob: 0d8abde3e5c4fa49e2819da00305bd0dac4861ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* 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 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/>.
*/

#define _FILE_OFFSET_BITS 64

#include <cctype>
#include <cerrno>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <pthread.h>
#include <stdint.h>
#include <unistd.h>
#include <lzlib.h>

#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


bool safe_seek( const int fd, const long long pos )
  {
  if( lseek( fd, pos, SEEK_SET ) == pos ) return true;
  show_error( "Seek error", errno ); return false;
  }


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 )
  {
  const long long rest = 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." );
      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() )
    {
    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 );
      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( 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(), "Not found in archive." );
      retval = 1;
      }
  return retval;
  }


/* Deleting from a corrupt archive must not worsen the corruption. Stop and
   tail-copy as soon as corruption is found.
*/
int delete_members( const Cl_options & cl_opts )
  {
  if( cl_opts.filenames <= 0 )
    { 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 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( 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( 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; }
  // archive is uncompressed seekable, unless compressed corrupt

  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
  while( true )				// process one tar header per iteration
    {
    if( !prev_extended && ( member_begin = lseek( 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; }
    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; }
        break;
        }
      show_file_error( archive_namep, "Corrupt header in archive." );
      retval = 2; break;
      }

    const Typeflag typeflag = (Typeflag)header[typeflag_o];
    if( typeflag == tf_global )
      {
      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 ) )
        { show_file_error( archive_namep, gblrec_msg ); retval = 2; 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; }
      else if( !extended.crc_present() && cl_opts.missing_crc )
        { show_file_error( archive_namep, mcrc_msg ); retval = 2; break; }
      prev_extended = true;
      continue;
      }
    prev_extended = false;

    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; }
    }

    // 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; }
      const long long pos = lseek( infd, 0, SEEK_CUR );
      if( pos <= 0 || pos <= member_begin || member_begin < istream_pos )
        { show_file_error( archive_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; }
        }
      istream_pos = pos;
      }
    extended.reset();
    }

  return tail_copy( archive_namep, cl_opts.parser, name_pending, lzip_index,
                    istream_pos, infd, outfd, retval );
  }