summaryrefslogtreecommitdiffstats
path: root/tarlz.h
diff options
context:
space:
mode:
Diffstat (limited to 'tarlz.h')
-rw-r--r--tarlz.h184
1 files changed, 127 insertions, 57 deletions
diff --git a/tarlz.h b/tarlz.h
index d34374a..6baa1d8 100644
--- a/tarlz.h
+++ b/tarlz.h
@@ -41,6 +41,16 @@ const uint8_t ustar_magic[magic_l] =
inline bool verify_ustar_magic( const uint8_t * const header )
{ return std::memcmp( header + magic_o, ustar_magic, magic_l ) == 0; }
+inline void init_tar_header( Tar_header header ) // set magic and version
+ {
+ std::memset( header, 0, header_size );
+ std::memcpy( header + magic_o, ustar_magic, magic_l - 1 );
+ header[version_o] = header[version_o+1] = '0';
+ }
+
+inline void print_octal( uint8_t * const buf, int size, unsigned long long num )
+ { while( --size >= 0 ) { buf[size] = '0' + ( num % 8 ); num /= 8; } }
+
// Round "size" to the next multiple of header size (512).
//
@@ -52,30 +62,65 @@ inline unsigned long long round_up( const unsigned long long size )
}
+enum { initial_line_length = 1000 }; // must be >= 77 for 'mode user/group'
+
+class Resizable_buffer
+ {
+ char * p;
+ unsigned long size_; // size_ < LONG_MAX
+
+public:
+ explicit Resizable_buffer( const unsigned long initial_size )
+ : p( (char *)std::malloc( initial_size ) ), size_( p ? initial_size : 0 ) {}
+ ~Resizable_buffer() { if( p ) std::free( p ); p = 0; size_ = 0; }
+
+ bool resize( const unsigned long long new_size )
+ {
+ if( new_size >= LONG_MAX ) return false;
+ if( size_ < new_size )
+ {
+ char * const tmp = (char *)std::realloc( p, new_size );
+ if( !tmp ) return false;
+ p = tmp; size_ = new_size;
+ }
+ return true;
+ }
+ char * operator()() const { return p; }
+ unsigned long size() const { return size_; }
+ };
+
+
class Extended // stores metadata from/for extended records
{
- std::string linkpath_;
+ std::string linkpath_; // these are the real metadata
std::string path_;
unsigned long long file_size_;
- mutable long long full_size_; // cached sizes
- mutable int recsize_linkpath_;
- mutable int recsize_path_;
- mutable int recsize_file_size_;
+ // cached sizes; if full_size_ < 0 they must be recalculated
+ mutable long long edsize_; // extended data size
+ mutable long long padded_edsize_; // edsize rounded up
+ mutable long long full_size_; // header + padded edsize
+ mutable long long linkpath_recsize_;
+ mutable long long path_recsize_;
+ mutable int file_size_recsize_;
+
+ // true if CRC present in parsed or formatted records
+ mutable bool crc_present_;
- bool crc_present_; // true if CRC present in parsed records
+ void calculate_sizes() const;
public:
static const std::string crc_record;
Extended()
- : file_size_( 0 ), full_size_( -1 ), recsize_linkpath_( -1 ),
- recsize_path_( -1 ), recsize_file_size_( -1 ), crc_present_( false ) {}
+ : file_size_( 0 ), edsize_( 0 ), padded_edsize_( 0 ), full_size_( 0 ),
+ linkpath_recsize_( 0 ), path_recsize_( 0 ), file_size_recsize_( 0 ),
+ crc_present_( false ) {}
void reset()
- { linkpath_.clear(); path_.clear(); file_size_ = 0; full_size_ = -1;
- recsize_linkpath_ = -1; recsize_path_ = -1; recsize_file_size_ = -1;
- crc_present_ = false; }
+ { linkpath_.clear(); path_.clear(); file_size_ = 0; edsize_ = 0;
+ padded_edsize_ = 0; full_size_ = 0; linkpath_recsize_ = 0;
+ path_recsize_ = 0; file_size_recsize_ = 0; crc_present_ = false; }
bool empty() const
{ return linkpath_.empty() && path_.empty() && file_size_ == 0; }
@@ -84,27 +129,16 @@ public:
const std::string & path() const { return path_; }
unsigned long long file_size() const { return file_size_; }
- void linkpath( const char * const lp )
- { linkpath_ = lp; full_size_ = -1; recsize_linkpath_ = -1; }
- void path( const char * const p )
- { path_ = p; full_size_ = -1; recsize_path_ = -1; }
+ void linkpath( const char * const lp ) { linkpath_ = lp; full_size_ = -1; }
+ void path( const char * const p ) { path_ = p; full_size_ = -1; }
void file_size( const unsigned long long fs )
- { file_size_ = fs; full_size_ = -1; recsize_file_size_ = -1; }
-
- int recsize_linkpath() const;
- int recsize_path() const;
- int recsize_file_size() const;
- unsigned long long edsize() const // extended data size
- { return empty() ? 0 : recsize_linkpath() + recsize_path() +
- recsize_file_size() + crc_record.size(); }
- unsigned long long edsize_pad() const // edsize rounded up
- { return round_up( edsize() ); }
+ { file_size_ = fs; full_size_ = -1; }
+
unsigned long long full_size() const
- { if( full_size_ < 0 )
- full_size_ = ( empty() ? 0 : header_size + edsize_pad() );
- return full_size_; }
+ { if( full_size_ < 0 ) calculate_sizes(); return full_size_; }
bool crc_present() const { return crc_present_; }
+ long long format_block( Resizable_buffer & rbuf ) const;
bool parse( const char * const buf, const unsigned long long edsize,
const bool permissive );
};
@@ -253,37 +287,12 @@ public:
extern const CRC32 crc32c;
-
-enum { initial_line_length = 1000 }; // must be >= 77
-
-class Resizable_buffer
- {
- char * p;
- unsigned size_;
-
-public:
- explicit Resizable_buffer( const unsigned initial_size )
- : p( (char *)std::malloc( initial_size ) ), size_( p ? initial_size : 0 ) {}
- ~Resizable_buffer() { if( p ) std::free( p ); p = 0; size_ = 0; }
-
- bool resize( const unsigned new_size )
- {
- if( size_ < new_size )
- {
- char * const tmp = (char *)std::realloc( p, new_size );
- if( !tmp ) return false;
- p = tmp; size_ = new_size;
- }
- return true;
- }
- char * operator()() const { return p; }
- unsigned size() const { return size_; }
- };
-
const char * const bad_magic_msg = "Bad magic number (file not in lzip format).";
const char * const bad_dict_msg = "Invalid dictionary size in member header.";
const char * const corrupt_mm_msg = "Corrupt header in multimember file.";
const char * const trailing_msg = "Trailing data not allowed.";
+const char * const mem_msg = "Not enough memory.";
+const char * const mem_msg2 = "Not enough memory. Try a lower compression level.";
// defined in create.cc
enum Solidity { no_solid, bsolid, dsolid, asolid, solid };
@@ -291,19 +300,34 @@ extern int cl_owner;
extern int cl_group;
extern int cl_data_size;
extern Solidity solidity;
+const char * remove_leading_dotslash( const char * const filename,
+ const bool dotdot = false );
+bool fill_headers( const char * const filename, Extended & extended,
+ Tar_header header, unsigned long long & file_size,
+ const int flag );
+bool block_is_full( const Extended & extended,
+ const unsigned long long file_size,
+ unsigned long long & partial_data_size );
+void set_error_status( const int retval );
+int final_exit_status( int retval );
unsigned ustar_chksum( const uint8_t * const header );
bool verify_ustar_chksum( const uint8_t * const header );
class Arg_parser;
int concatenate( const std::string & archive_name, const Arg_parser & parser,
const int filenames );
int encode( const std::string & archive_name, const Arg_parser & parser,
- const int filenames, const int level, const bool append );
+ const int filenames, const int level, const int num_workers,
+ const int debug_level, const bool append );
+
+// defined in create_lz.cc
+int encode_lz( const char * const archive_name, const Arg_parser & parser,
+ const int dictionary_size, const int match_len_limit,
+ const int num_workers, const int outfd, const int debug_level );
// defined in extract.cc
bool block_is_zero( const uint8_t * const buf, const int size );
void format_member_name( const Extended & extended, const Tar_header header,
Resizable_buffer & rbuf, const bool long_format );
-const char * remove_leading_slash( const char * const filename );
bool compare_prefix_dir( const char * const dir, const char * const name );
bool compare_tslash( const char * const name1, const char * const name2 );
int readblock( const int fd, uint8_t * const buf, const int size );
@@ -315,6 +339,15 @@ int decode( const std::string & archive_name, const Arg_parser & parser,
const bool permissive );
// defined in list_lz.cc
+void xinit_mutex( pthread_mutex_t * const mutex );
+void xinit_cond( pthread_cond_t * const cond );
+void xdestroy_mutex( pthread_mutex_t * const mutex );
+void xdestroy_cond( pthread_cond_t * const cond );
+void xlock( pthread_mutex_t * const mutex );
+void xunlock( pthread_mutex_t * const mutex );
+void xwait( pthread_cond_t * const cond, pthread_mutex_t * const mutex );
+void xsignal( pthread_cond_t * const cond );
+void xbroadcast( pthread_cond_t * const cond );
class Lzip_index;
int list_lz( const Arg_parser & parser, std::vector< char > & name_pending,
const Lzip_index & lzip_index, const int filenames,
@@ -325,8 +358,45 @@ int list_lz( const Arg_parser & parser, std::vector< char > & name_pending,
extern int verbosity;
int open_instream( const std::string & name );
int open_outstream( const std::string & name, const bool create = true );
+void cleanup_and_fail( const int retval = 1 ); // terminate the program
void show_error( const char * const msg, const int errcode = 0,
const bool help = false );
void show_file_error( const char * const filename, const char * const msg,
const int errcode = 0 );
void internal_error( const char * const msg );
+
+
+class Slot_tally
+ {
+ const int num_slots; // total slots
+ int num_free; // remaining free slots
+ pthread_mutex_t mutex;
+ pthread_cond_t slot_av; // slot available
+
+ Slot_tally( const Slot_tally & ); // declared as private
+ void operator=( const Slot_tally & ); // declared as private
+
+public:
+ explicit Slot_tally( const int slots )
+ : num_slots( slots ), num_free( slots )
+ { xinit_mutex( &mutex ); xinit_cond( &slot_av ); }
+
+ ~Slot_tally() { xdestroy_cond( &slot_av ); xdestroy_mutex( &mutex ); }
+
+ bool all_free() { return ( num_free == num_slots ); }
+
+ void get_slot() // wait for a free slot
+ {
+ xlock( &mutex );
+ while( num_free <= 0 ) xwait( &slot_av, &mutex );
+ --num_free;
+ xunlock( &mutex );
+ }
+
+ void leave_slot() // return a slot to the tally
+ {
+ xlock( &mutex );
+ if( ++num_free == 1 ) xsignal( &slot_av ); // num_free was 0
+ xunlock( &mutex );
+ }
+ };