-
Notifications
You must be signed in to change notification settings - Fork 1
/
goodranges.cpp
65 lines (55 loc) · 1.86 KB
/
goodranges.cpp
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
/**
* @file goodranges.cpp Methods for quick search of continuous non-damaged
* ranges of HDD
*
* @copy Copyright (C) 2012 @author Roman Tereshin. This file may be
* redistributed under the terms of the GNU Public License.
*/
#include <cstdio>
#include "goodranges.h"
#include "badblocks_wrapper.h"
using namespace std;
bool testFull(string const &deviceName, Range const &range)
{
/* This code correctly work, but not always suitable. Uncomment, if you want */
// printf("RUN FULL TEST: applicant %8d - %8d\n", range.second, range.first);
// return badblocks(deviceName.c_str(), ReadOnly, range.second, range.first) == 0;
return true;
}
bool testChunk(string const &deviceName, int firstBlock, int chunkEnd, int testSize)
{
int chunkBegin = chunkEnd - testSize;
if (chunkBegin < firstBlock) {
chunkBegin = firstBlock;
}
return badblocks(deviceName.c_str(), ReadOnly, chunkEnd, chunkBegin) == 0;
}
int rangeSize(Range const &range)
{
return range.second - range.first;
}
vector<Range> findGoodRanges(string const &deviceName, int lastBlock, int firstBlock
, int minRangeSize, int precision, int testSize)
{
vector<Range> results;
int rightEdge = precision + firstBlock;
Range currentApplicant = Range(firstBlock, firstBlock);
for (; rightEdge < lastBlock; rightEdge += precision) {
if (testChunk(deviceName, firstBlock, rightEdge, testSize)) {
currentApplicant.second = rightEdge;
} else {
if (rangeSize(currentApplicant) >= minRangeSize
&& testFull(deviceName, currentApplicant)) {
results.push_back(currentApplicant);
}
currentApplicant = Range(rightEdge, rightEdge);
}
}
/* If encapsulate the code of this file in the class will be a lot
more possibilities refactor this code */
if (rangeSize(currentApplicant) >= minRangeSize
&& testFull(deviceName, currentApplicant)) {
results.push_back(currentApplicant);
}
return results;
}