summaryrefslogtreecommitdiffstats
path: root/file_index.cc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--file_index.cc85
1 files changed, 37 insertions, 48 deletions
diff --git a/file_index.cc b/file_index.cc
index f6250bd..24790b9 100644
--- a/file_index.cc
+++ b/file_index.cc
@@ -38,95 +38,84 @@ int seek_read( const int fd, uint8_t * const buf, const int size,
}
-const char * format_num( unsigned long long num,
- unsigned long long limit = -1ULL,
- const int set_prefix = 0 )
+void File_index::set_errno_error( const char * const msg )
{
- const char * const si_prefix[8] =
- { "k", "M", "G", "T", "P", "E", "Z", "Y" };
- const char * const binary_prefix[8] =
- { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" };
- static bool si = true;
- static char buf[32];
-
- if( set_prefix ) si = ( set_prefix > 0 );
- const unsigned factor = ( si ? 1000 : 1024 );
- const char * const * prefix = ( si ? si_prefix : binary_prefix );
- const char * p = "";
- bool exact = ( num % factor == 0 );
+ error_ = msg; error_ += std::strerror( errno ); error_ += '.';
+ retval_ = 1;
+ }
- for( int i = 0; i < 8 && ( num > limit || ( exact && num >= factor ) ); ++i )
- { num /= factor; if( num % factor != 0 ) exact = false; p = prefix[i]; }
- snprintf( buf, sizeof buf, "%llu %s", num, p );
- return buf;
+void File_index::set_num_error( const char * const msg1, unsigned long long num,
+ const char * const msg2 )
+ {
+ char buf[80];
+ snprintf( buf, sizeof buf, "%s%llu%s", msg1, num, msg2 );
+ error_ = buf;
+ retval_ = 2;
}
-File_index::File_index( const int infd ) : retval_( 0 )
+File_index::File_index( const int infd )
+ : retval_( 0 )
{
const long long isize = lseek( infd, 0, SEEK_END );
if( isize < 0 )
- { error_ = "Input file is not seekable :";
- error_ += std::strerror( errno ); retval_ = 1; return; }
+ { set_errno_error( "Input file is not seekable :" ); return; }
+ if( isize < min_member_size )
+ { error_ = "Input file is too short."; retval_ = 2; return; }
if( isize > INT64_MAX )
- { error_ = "Input file is too long (2^63 bytes or more)";
+ { error_ = "Input file is too long (2^63 bytes or more).";
retval_ = 2; return; }
- long long pos = isize; // always points to a header or EOF
- File_header header;
- File_trailer trailer;
- if( isize < min_member_size )
- { error_ = "Input file is too short"; retval_ = 2; return; }
+ File_header header;
if( seek_read( infd, header.data, File_header::size, 0 ) != File_header::size )
- { error_ = "Error reading member header :";
- error_ += std::strerror( errno ); retval_ = 1; return; }
+ { set_errno_error( "Error reading member header :" ); return; }
if( !header.verify_magic() )
- { error_ = "Bad magic number (file not in lzip format)";
+ { error_ = "Bad magic number (file not in lzip format).";
retval_ = 2; return; }
if( !header.verify_version() )
- { error_ = "Version "; error_ += format_num( header.version() );
- error_ += "member format not supported"; retval_ = 2; return; }
+ { set_num_error( "Version ", header.version(),
+ " member format not supported." ); return; }
+ long long pos = isize; // always points to a header or to EOF
while( pos >= min_member_size )
{
+ File_trailer trailer;
if( seek_read( infd, trailer.data, File_trailer::size,
pos - File_trailer::size ) != File_trailer::size )
- { error_ = "Error reading member trailer :";
- error_ += std::strerror( errno ); retval_ = 1; break; }
+ { set_errno_error( "Error reading member trailer :" ); break; }
const long long member_size = trailer.member_size();
if( member_size < min_member_size || member_size > pos )
{
- if( member_vector.size() == 0 ) // maybe trailing garbage
+ if( member_vector.empty() ) // maybe trailing garbage
{ --pos; continue; }
- error_ = "Member size in trailer is corrupt at pos ";
- error_ += format_num( pos - 8 ); retval_ = 2; break;
+ set_num_error( "Member size in trailer is corrupt at pos ", pos - 8 );
+ break;
}
if( seek_read( infd, header.data, File_header::size,
pos - member_size ) != File_header::size )
- { error_ = "Error reading member header :";
- error_ += std::strerror( errno ); retval_ = 1; break; }
+ { set_errno_error( "Error reading member header :" ); break; }
if( !header.verify_magic() || !header.verify_version() )
{
- if( member_vector.size() == 0 ) // maybe trailing garbage
+ if( member_vector.empty() ) // maybe trailing garbage
{ --pos; continue; }
- error_ = "Bad header at pos ";
- error_ += format_num( pos - member_size ); retval_ = 2; break;
+ set_num_error( "Bad header at pos ", pos - member_size );
+ break;
}
- if( member_vector.size() == 0 && isize - pos > File_header::size &&
+ if( member_vector.empty() && isize - pos > File_header::size &&
seek_read( infd, header.data, File_header::size, pos ) == File_header::size &&
header.verify_magic() && header.verify_version() )
{
- error_ = "Last member in input file is truncated or corrupt";
+ error_ = "Last member in input file is truncated or corrupt.";
retval_ = 2; break;
}
pos -= member_size;
member_vector.push_back( Member( 0, trailer.data_size(),
pos, member_size ) );
}
- if( pos != 0 || member_vector.size() == 0 )
+ if( pos != 0 || member_vector.empty() )
{
member_vector.clear();
- if( retval_ == 0 ) { error_ = "Can't create file index"; retval_ = 2; }
+ if( retval_ == 0 ) { error_ = "Can't create file index."; retval_ = 2; }
return;
}
std::reverse( member_vector.begin(), member_vector.end() );
@@ -136,7 +125,7 @@ File_index::File_index( const int infd ) : retval_( 0 )
if( end < 0 || end > INT64_MAX )
{
member_vector.clear();
- error_ = "Data in input file is too long (2^63 bytes or more)";
+ error_ = "Data in input file is too long (2^63 bytes or more).";
retval_ = 2; return;
}
member_vector[i+1].dblock.pos( end );