summaryrefslogtreecommitdiffstats
path: root/file_index.cc
diff options
context:
space:
mode:
authorDaniel Baumann <mail@daniel-baumann.ch>2015-11-07 11:45:12 +0000
committerDaniel Baumann <mail@daniel-baumann.ch>2015-11-07 11:45:12 +0000
commit2085b1344e4c2b3c4a3d21d4bc10227679a55507 (patch)
tree86328decac9bdf54fb41b6af1bd15b23fff1edd7 /file_index.cc
parentAdding debian version 1.15-2. (diff)
downloadlziprecover-2085b1344e4c2b3c4a3d21d4bc10227679a55507.tar.xz
lziprecover-2085b1344e4c2b3c4a3d21d4bc10227679a55507.zip
Merging upstream version 1.16~pre1.
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'file_index.cc')
-rw-r--r--file_index.cc104
1 files changed, 43 insertions, 61 deletions
diff --git a/file_index.cc b/file_index.cc
index b3d9629..cdb4031 100644
--- a/file_index.cc
+++ b/file_index.cc
@@ -1,5 +1,5 @@
/* Lziprecover - Data recovery tool for lzip files
- Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz.
+ Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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
@@ -28,33 +28,9 @@
#include "file_index.h"
-const char * format_num( unsigned long long num,
- unsigned long long limit,
- const int set_prefix )
- {
- 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 );
-
- 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;
- }
-
-
Block Block::split( const long long pos )
{
- if( pos_ < pos && end() > pos )
+ if( pos > pos_ && pos < end() )
{
const Block b( pos_, pos - pos_ );
pos_ = pos; size_ -= b.size_;
@@ -64,13 +40,27 @@ Block Block::split( const long long pos )
}
+void File_index::set_errno_error( const char * const msg )
+ {
+ error_ = msg; error_ += std::strerror( errno ); error_ += '.';
+ retval_ = 1;
+ }
+
+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 )
- :
- isize( lseek( infd, 0, SEEK_END ) ), retval_( 0 )
+ : isize( lseek( infd, 0, SEEK_END ) ), retval_( 0 )
{
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 )
@@ -79,14 +69,13 @@ File_index::File_index( const int infd )
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).";
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 )
@@ -94,28 +83,26 @@ File_index::File_index( const int infd )
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() )
{
@@ -126,7 +113,7 @@ File_index::File_index( const int infd )
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; }
@@ -150,12 +137,10 @@ File_index::File_index( const int infd )
// All files in 'infd_vector' must be at least 'fsize' bytes long.
File_index::File_index( const std::vector< int > & infd_vector,
const long long fsize )
- :
- isize( fsize ), retval_( 0 )
+ : isize( fsize ), retval_( 0 )
{
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 )
@@ -169,8 +154,7 @@ File_index::File_index( const std::vector< int > & infd_vector,
{
const int infd = infd_vector[i];
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() && header.verify_version() ) done = true;
}
if( !done )
@@ -188,8 +172,7 @@ File_index::File_index( const std::vector< int > & infd_vector,
const int tfd = infd_vector[it];
if( seek_read( tfd, trailer.data, File_trailer::size,
pos - File_trailer::size ) != File_trailer::size )
- { error_ = "Error reading member trailer :";
- error_ += std::strerror( errno ); retval_ = 1; goto error; }
+ { set_errno_error( "Error reading member trailer :" ); goto error; }
member_size = trailer.member_size();
if( member_size >= min_member_size && member_size <= pos )
for( int ih = 0; ih < files && !done; ++ih )
@@ -197,19 +180,18 @@ File_index::File_index( const std::vector< int > & infd_vector,
const int hfd = infd_vector[ih];
if( seek_read( hfd, header.data, File_header::size,
pos - member_size ) != File_header::size )
- { error_ = "Error reading member header :";
- error_ += std::strerror( errno ); retval_ = 1; goto error; }
+ { set_errno_error( "Error reading member header :" ); goto error; }
if( header.verify_magic() && header.verify_version() ) done = true;
}
}
if( !done )
{
- if( member_vector.size() == 0 ) // maybe trailing garbage
+ if( member_vector.empty() ) // maybe trailing garbage
{ --pos; continue; }
- error_ = "Member size in trailer may be corrupt at pos ";
- error_ += format_num( pos - 8 ); retval_ = 2; break;
+ set_num_error( "Member size in trailer may be corrupt at pos ", pos - 8 );
+ break;
}
- if( member_vector.size() == 0 && isize - pos > File_header::size )
+ if( member_vector.empty() && isize - pos > File_header::size )
for( int i = 0; i < files; ++i )
{
const int infd = infd_vector[i];
@@ -225,7 +207,7 @@ File_index::File_index( const std::vector< int > & infd_vector,
pos, member_size ) );
}
error:
- 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; }