-
Notifications
You must be signed in to change notification settings - Fork 15
/
block.cc
139 lines (117 loc) · 3.99 KB
/
block.cc
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
/* 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/>.
*/
#define _FILE_OFFSET_BITS 64
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include "block.h"
// Align pos to next boundary if size is big enough
//
void Block::align_pos( const int alignment )
{
if( alignment > 1 )
{
const int disp = alignment - ( pos_ % alignment );
if( disp < alignment && disp < size_ )
{ pos_ += disp; size_ -= disp; }
}
}
// Align end to previous boundary if size is big enough
//
void Block::align_end( const int alignment )
{
if( alignment > 1 )
{
const int rest = end() % alignment;
if( size_ > rest ) size_ -= rest;
}
}
void Block::crop( const Block & b )
{
const long long p = std::max( pos_, b.pos_ );
const long long s = std::max( 0LL, std::min( end(), b.end() ) - p );
pos_ = p; size_ = s;
}
bool Block::join( const Block & b )
{
if( this->follows( b ) ) pos_ = b.pos_;
else if( !b.follows( *this ) ) return false;
if( b.size_ > LLONG_MAX - end() )
internal_error( "size overflow joining two Blocks." );
size_ += b.size_;
return true;
}
// shift the boundary of two consecutive Blocks
void Block::shift_boundary( Block & b, const long long pos )
{
if( end() != b.pos_ || pos <= pos_ || pos >= b.end() )
internal_error( "bad argument shifting the border of two Blocks." );
b.size_ = b.end() - pos; b.pos_ = pos; size_ = pos - pos_;
}
Block Block::split( long long pos, const int hardbs )
{
if( hardbs > 1 ) pos -= pos % hardbs;
if( pos_ < pos && end() > pos )
{
const Block b( pos_, pos - pos_ );
pos_ = pos; size_ -= b.size_;
return b;
}
return Block( 0, 0 );
}
Domain::Domain( const long long p, const long long s,
const char * const mapname, const bool loose )
{
reset_cached_in_size();
const Block b( p, s );
if( !mapname || !mapname[0] ) { block_vector.push_back( b ); return; }
Mapfile mapfile( mapname );
if( !mapfile.read_mapfile( loose ? '?' : 0 ) )
{
char buf[80];
snprintf( buf, sizeof buf,
"Mapfile '%s' does not exist or is not readable.", mapname );
show_error( buf );
std::exit( 1 );
}
mapfile.compact_sblock_vector();
for( long i = 0; i < mapfile.sblocks(); ++i )
{
const Sblock & sb = mapfile.sblock( i );
if( sb.status() == Sblock::finished ) block_vector.push_back( sb );
}
if( block_vector.empty() ) block_vector.push_back( Block( 0, 0 ) );
else this->crop( b );
}
void Domain::crop( const Block & b )
{
reset_cached_in_size();
unsigned long r = block_vector.size();
while( r > 0 && b < block_vector[r-1] ) --r;
if( r > 0 ) block_vector[r-1].crop( b );
if( r <= 0 || block_vector[r-1].size() <= 0 ) // no block overlaps b
{ block_vector.clear(); block_vector.push_back( Block( 0, 0 ) ); return; }
if( r < block_vector.size() ) // remove blocks beyond b
block_vector.erase( block_vector.begin() + r, block_vector.end() );
if( b.pos() <= 0 ) return;
--r; // block_vector[r] is now the last non-cropped-out block
unsigned long l = 0;
while( l < r && block_vector[l] < b ) ++l;
if( l < r ) block_vector[l].crop( b ); // crop block overlapping b
if( l > 0 ) // remove blocks before b
block_vector.erase( block_vector.begin(), block_vector.begin() + l );
}