-
Notifications
You must be signed in to change notification settings - Fork 15
/
block.h
318 lines (271 loc) · 11.3 KB
/
block.h
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* GNU ddrescue - Data recovery tool
Copyright (C) 2004-2019 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/>.
*/
#ifndef LLONG_MAX
#define LLONG_MAX 0x7FFFFFFFFFFFFFFFLL
#endif
#ifndef LLONG_MIN
#define LLONG_MIN (-LLONG_MAX - 1LL)
#endif
#ifndef ULLONG_MAX
#define ULLONG_MAX 0xFFFFFFFFFFFFFFFFULL
#endif
// requires '#include <cstdio>' for 'FILE *'
class Block
{
long long pos_, size_; // pos >= 0 && size >= 0 && pos + size <= LLONG_MAX
void fix_size() // limit size_ to largest possible value
{ if( size_ < 0 || size_ > LLONG_MAX - pos_ ) size_ = LLONG_MAX - pos_; }
public:
Block() {} // default constructor
Block( const long long p, const long long s ) : pos_( p ), size_( s )
{ if( p < 0 ) { pos_ = 0; if( s > 0 ) size_ -= std::min( s, -p ); }
fix_size(); }
long long pos() const { return pos_; }
long long size() const { return size_; }
long long end() const { return pos_ + size_; }
void pos( const long long p )
{ pos_ = std::max( p, 0LL );
if( size_ > LLONG_MAX - pos_ ) size_ = LLONG_MAX - pos_; }
void shift( const long long offset )
{ if( offset >= 0 )
{ pos_ += std::min( offset, LLONG_MAX - pos_ );
if( size_ > LLONG_MAX - pos_ ) size_ = LLONG_MAX - pos_; }
else if( ( pos_ += offset ) < 0 )
{ size_ = std::max( size_ + pos_, 0LL ); pos_ = 0; } }
void size( const long long s ) { size_ = s; fix_size(); }
void enlarge( long long s )
{ if( s < 0 ) s = LLONG_MAX;
if( s > LLONG_MAX - end() ) s = LLONG_MAX - end();
size_ += s; }
void end( long long e ) // also moves pos
{ if( e < 0 ) e = LLONG_MAX;
if( size_ <= e ) pos_ = e - size_; else { pos_ = 0; size_ = e; } }
Block & assign( const long long p, const long long s )
{
pos_ = p; size_ = s;
if( p < 0 ) { pos_ = 0; if( s > 0 ) size_ -= std::min( s, -p ); }
fix_size(); return *this;
}
void align_pos( const int alignment );
void align_end( const int alignment );
bool operator==( const Block & b ) const
{ return pos_ == b.pos_ && size_ == b.size_; }
bool operator!=( const Block & b ) const
{ return pos_ != b.pos_ || size_ != b.size_; }
bool operator<( const Block & b ) const { return ( end() <= b.pos() ); }
bool follows( const Block & b ) const
{ return ( pos_ == b.end() ); }
bool includes( const Block & b ) const
{ return ( pos_ <= b.pos_ && end() >= b.end() ); }
bool includes( const long long pos ) const
{ return ( pos_ <= pos && end() > pos ); }
bool strictly_includes( const long long pos ) const
{ return ( pos_ < pos && end() > pos ); }
void crop( const Block & b );
bool join( const Block & b );
void shift_boundary( Block & b, const long long pos );
Block split( long long pos, const int hardbs = 1 );
};
class Sblock : public Block
{
public:
enum Status // ordered from less to more processed state
{ non_tried = '?', non_trimmed = '*', non_scraped = '/',
bad_sector = '-', finished = '+' };
private:
Status status_;
public:
Sblock() {} // default constructor
Sblock( const Block & b, const Status st )
: Block( b ), status_( st ) {}
Sblock( const long long p, const long long s, const Status st )
: Block( p, s ), status_( st ) {}
Status status() const { return status_; }
void status( const Status st ) { status_ = st; }
bool operator!=( const Sblock & sb ) const
{ return Block::operator!=( sb ) || status_ != sb.status_; }
bool join( const Sblock & sb )
{ if( status_ == sb.status_ ) return Block::join( sb ); else return false; }
Sblock split( const long long pos, const int hardbs = 1 )
{ return Sblock( Block::split( pos, hardbs ), status_ ); }
static bool isstatus( const int st )
{ return ( st == non_tried || st == non_trimmed || st == non_scraped ||
st == bad_sector || st == finished ); }
static bool is_good_status( const Status st ) { return st != bad_sector; }
static int processed_state( const Status st )
{
switch( st )
{
case non_tried: return 0;
case non_trimmed: return 1;
case non_scraped: return 2;
case bad_sector: return 3;
default: return 4;
}
}
};
class Domain
{
std::vector< Block > block_vector; // blocks are ordered and don't overlap
mutable long long cached_in_size;
void reset_cached_in_size() { cached_in_size = -1; }
public:
Domain( const long long p, const long long s,
const char * const mapname = 0, const bool loose = false );
long long pos() const { return block_vector.front().pos(); }
long long end() const { return block_vector.back().end(); }
long long size() const { return end() - pos(); }
const Block & block( const long i ) const { return block_vector[i]; }
long blocks() const { return block_vector.size(); }
bool empty() const { return ( end() <= pos() ); }
bool full() const { return ( !empty() && end() >= LLONG_MAX ); }
long long in_size() const
{
if( cached_in_size < 0 )
{
cached_in_size = 0;
for( unsigned long i = 0; i < block_vector.size(); ++i )
cached_in_size += block_vector[i].size();
}
return cached_in_size;
}
bool operator!=( const Domain & d ) const
{
if( block_vector.size() != d.block_vector.size() ) return true;
for( unsigned long i = 0; i < block_vector.size(); ++i )
if( block_vector[i] != d.block_vector[i] ) return true;
return false;
}
bool operator<( const Block & b ) const { return ( end() <= b.pos() ); }
bool operator>( const Block & b ) const { return ( pos() >= b.end() ); }
bool includes( const Block & b ) const
{
unsigned long l = 0, r = block_vector.size();
while( l < r )
{
const long m = ( l + r ) / 2;
const Block & db = block_vector[m];
if( db.includes( b ) ) return true;
if( db < b ) l = m + 1; else if( b < db ) r = m; else break;
}
return false;
}
bool includes( const long long pos ) const
{
for( unsigned long i = 0; i < block_vector.size(); ++i )
if( block_vector[i].includes( pos ) ) return true;
return false;
}
void clear()
{
block_vector.clear(); block_vector.push_back( Block( 0, 0 ) );
cached_in_size = 0;
}
void crop( const Block & b );
void crop_by_file_size( const long long size ) { crop( Block( 0, size ) ); }
};
class Mapfile
{
public:
enum Status
{ copying = '?', trimming = '*', scraping = '/', retrying = '-',
filling = 'F', generating = 'G', finished = '+' };
private:
long long current_pos_;
const char * const filename_;
std::string current_msg;
Status current_status_;
int current_pass_;
mutable long index_; // cached index of last find or change
bool read_only_;
std::vector< Sblock > sblock_vector; // note: blocks are consecutive
void insert_sblock( const long i, const Sblock & sb )
{ sblock_vector.insert( sblock_vector.begin() + i, sb ); }
public:
explicit Mapfile( const char * const mapname )
: current_pos_( 0 ), filename_( mapname ), current_status_( copying ),
current_pass_( 1 ), index_( 0 ), read_only_( false ) {}
void compact_sblock_vector();
void join_subsectors( const int hardbs );
void extend_sblock_vector( const long long insize );
void shift_blocks( const long long offset );
bool truncate_vector( const long long end, const bool force = false );
void set_to_status( const Sblock::Status st )
{ sblock_vector.assign( 1, Sblock( 0, -1, st ) ); }
bool read_mapfile( const int default_sblock_status = 0, const bool ro = true );
bool write_mapfile( FILE * f = 0, const bool timestamp = false,
const bool mf_sync = false,
const Domain * const annotate_domainp = 0 ) const;
bool blank() const;
long long current_pos() const { return current_pos_; }
Status current_status() const { return current_status_; }
int current_pass() const { return current_pass_; }
const char * filename() const { return filename_; }
bool read_only() const { return read_only_; }
void current_pos( const long long pos ) { current_pos_ = pos; }
void current_status( const Status st, const char * const msg = "" )
{ current_status_ = st;
current_msg = ( st == finished ) ? "Finished" : msg; }
void current_pass( const int pass ) { current_pass_ = pass; }
Block extent() const
{ if( sblock_vector.empty() ) return Block( 0, 0 );
return Block( sblock_vector.front().pos(),
sblock_vector.back().end() - sblock_vector.front().pos() ); }
const Sblock & sblock( const long i ) const { return sblock_vector[i]; }
long sblocks() const { return sblock_vector.size(); }
void change_sblock_status( const long i, const Sblock::Status st )
{ sblock_vector[i].status( st ); }
void split_by_domain_borders( const Domain & domain );
void split_by_mapfile_borders( const Mapfile & mapfile );
bool try_split_sblock_by( const long long pos, const long i )
{
if( sblock_vector[i].strictly_includes( pos ) )
{ insert_sblock( i, sblock_vector[i].split( pos ) ); return true; }
return false;
}
long find_index( const long long pos ) const;
bool find_chunk( Block & b, const Sblock::Status st,
const Domain & domain, const int alignment,
const bool after_finished = false,
const bool unfinished = false ) const;
bool rfind_chunk( Block & b, const Sblock::Status st,
const Domain & domain, const int alignment,
const bool before_finished = false ) const;
int change_chunk_status( const Block & b, const Sblock::Status st,
const Domain & domain,
Sblock::Status * const old_stp = 0 );
static bool isstatus( const int st )
{ return ( st == copying || st == trimming || st == scraping ||
st == retrying || st == filling || st == generating ||
st == finished ); }
static const char * status_name( const Status st );
};
// Defined in main_common.cc
extern int verbosity;
void show_error( const char * const msg,
const int errcode = 0, const bool help = false );
void internal_error( const char * const msg );
int empty_domain();
int not_readable( const char * const mapname );
int not_writable( const char * const mapname );
long initial_time();
bool write_file_header( FILE * const f, const char * const filetype );
bool write_timestamp( FILE * const f );
bool write_final_timestamp( FILE * const f );
const char * format_num( long long num, long long limit = 999999,
const int set_prefix = 0 );
const char * format_percentage( long long num, long long den,
const int iwidth = 3, int prec = -2 );
// defined in main.cc
const char * format_num3( long long num );