-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
228 lines (201 loc) · 5.61 KB
/
main.cpp
File metadata and controls
228 lines (201 loc) · 5.61 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include "fmt_no.h"
#ifdef NO_COLORS
# define RED ""
# define AMBER ""
# define YELLOW ""
# define GREEN ""
# define BOLD ""
# define INV ""
# define NORM ""
#else
#ifndef RED
# define RED "\x1b[0;31m"
# define AMBER "\x1b[0;33m"
# define YELLOW "\x1b[1;33m"
# define GREEN "\x1b[0;32m"
# define BOLD "\x1b[0;1m"
# define ULINE "\x1b[0;4m"
# define INV "\x1b[0;7m"
# define NORM "\x1b[0;0m"
#endif
#endif
#define ANSI_UP "\x1b[A"
#define ANSI_RESTORE_POS "\x1b[u"
#define ANSI_SAVE_Pos
#define min(a,b) (a < b ? a : b)
const char *bold = BOLD, *norm = NORM;
double starttime=0;
size_t compareSize=0;
int originalErrors=0;
int targetErrors=0;
int diffBlocks=0;
int diffBytes=0;
/**
* @return different bytes
*/
int compareAreas(const void *original, const void *target, int len) {
int ret=0;
for(int i=0; i < len; i++) {
if(*((char *)original+i) != *((char *)target+i)) {
ret++;
}
}
return ret;
}
void printProgress(size_t position) {
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
double now=spec.tv_sec + (spec.tv_nsec / 1.0e9);
double percentDone=((double)(position) / compareSize );
static double last=0;
if(last == 0) {
last=starttime;
}
static size_t lastPosition=0;
char progress[51];
for(int i=0; i < sizeof(progress)-1; i++) {
if(i <= (percentDone * 100 / 2)) {
progress[i]='-';
} else {
progress[i]='.';
}
}
progress[50]='\0';
printf("%sdd_verify: (info): pos: %skB\n"
" errOriginal: %d, errTarget: %d, diff blocks: %d, diff bytes: %d \n"
" +curr.rate: %skB/s, avg.rate: %skB/s, avg.load: xx%% \n"
" >%s< %d%% ETA: %ds \n",
ANSI_UP ANSI_UP ANSI_UP ANSI_UP,
fmt_int(10, 1, 1024, position, bold, norm, 1),
originalErrors, targetErrors, diffBlocks, diffBytes,
fmt_int(10, 1, 1024, (position-lastPosition)/(now-last), bold, norm, 1), fmt_int(10, 1, 1024,(position/(now-starttime)), bold, norm, 1),
progress,(int)(percentDone*100), (int) (percentDone > 0 ? (now-starttime)/percentDone -(now-starttime): -1)
);
last=now;
lastPosition=position;
}
int main(int argc, char *argv[]) {
if(argc != 3) {
fprintf(stderr,"dd_verify original target\n");
exit(1);
}
int softbs=128*1024;
int hardbs=4;
printf("dd_verify: (info): Using softbs=%dB, hardbs=%dB\n",softbs,hardbs);
struct stat buf;
int original=open(argv[1],O_RDONLY | O_LARGEFILE | O_DIRECT);
if(!original) {
fprintf(stderr, "error opening %s\n", argv[1]);
exit(1);
}
int target=open(argv[2],O_RDONLY | O_LARGEFILE | O_DIRECT);
if(!target) {
fprintf(stderr, "error opening %s\n", argv[2]);
exit(1);
}
if(fstat(original, &buf)) {
fprintf(stderr, "error fstat %s\n", argv[1]);
exit(1);
}
size_t originalSize=buf.st_size;
if(originalSize == 0) {
printf("reading device size\n");
uint64_t size;
ioctl(original, BLKGETSIZE64, &size);
originalSize=size;
}
if(fstat(target, &buf)) {
fprintf(stderr, "error fstat %s\n", argv[2]);
exit(1);
}
size_t targetSize=buf.st_size;
if(targetSize == 0) {
printf("reading device size\n");
uint64_t size;
ioctl(target, BLKGETSIZE64, &size);
targetSize=size;
}
if(originalSize != targetSize) {
printf("warning: original: %skB target: %skB\n", fmt_int(10, 1, 1024, originalSize, bold, norm, 1), fmt_int(10, 1, 1024, targetSize, bold, norm, 1) );
}
compareSize=min(originalSize, targetSize);
printf("dd_verify: (info): expect to compare %s from %s and %s\n", fmt_int(10, 1, 1024, compareSize, bold, norm, 1), argv[1], argv[2]);
void *originalBuffer=NULL;
posix_memalign(&originalBuffer, softbs, softbs);
void *targetBuffer=NULL;
posix_memalign(&targetBuffer, softbs, softbs);
// save cursor position:
// printf("\x1b[s");
size_t position=0;
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
starttime=spec.tv_sec + (spec.tv_nsec / 1.0e9);
double lastUpdate=0;
while(position < compareSize) {
// printf("startloop: position %zd\n", position);
ssize_t originalRc=read(original, originalBuffer, softbs);
bool originalSeek=false;
if(originalRc < 0) {
perror("");
originalErrors++;
originalSeek=true;
}
if(originalRc != softbs && ((position+originalRc) != compareSize)) {
fprintf(stderr, "error reading %dbytes from original (only %d)\n", softbs, originalRc);
abort();
}
if(originalSeek) {
if(lseek(original, SEEK_SET, position+softbs) < 0) {
fprintf(stderr, "error seek original to %zd\n", position+softbs);
perror("");
abort();
}
}
ssize_t targetRc=read(target, targetBuffer, softbs);
bool targetSeek=false;
if(targetRc < 0) {
perror("");
targetErrors++;
targetSeek=true;
}
if(targetRc != softbs) {
printf("error reading %dbytes from target (only %d)\n", softbs, targetRc);
}
if(targetSeek) {
if(lseek(target, SEEK_SET, position+softbs) < 0) {
fprintf(stderr, "error seek target to %zd\n", position+softbs);
perror("");
abort();
}
}
int d=compareAreas(originalBuffer, targetBuffer, min(originalRc,targetRc));
if(d > 0) {
diffBlocks++;
diffBytes+=d;
}
// printf("lastUpdate:%f\n",lastUpdate);
position+=originalRc;
clock_gettime(CLOCK_REALTIME, &spec);
double now=spec.tv_sec + (spec.tv_nsec / 1.0e9);
if(lastUpdate < now - 0.1) {
printProgress(position);
lastUpdate=now;
}
}
printf("done: position: %s\n", fmt_int(10, 1, 1024, position, bold, norm, 1));
printProgress(position);
}