-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdupdate.c
528 lines (402 loc) · 14.7 KB
/
sdupdate.c
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
* Copyright (C) 2011 Codethink Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 2 as
* published by the Free Software Foundation.
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#if defined(__linux__)
#define _GNU_SOURCE
#include <linux/fs.h>
#else
#define _POSIX_C_SOURCE 2
#endif
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <limits.h>
#include <sys/ioctl.h>
unsigned long buffparser (char* buff) {
int size = strlen(buff);
char lastch = buff[size-1];
if (lastch >= '0' && lastch <= '9')
return atof(buff);
else {
int i;
char* newbuff = malloc(size-1);
for (i = 0; i < size-1; i++)
newbuff[i] = buff[i];
float mltplr = atof(newbuff);
switch (lastch) {
case 'K':
return mltplr*1024;
case 'M':
return mltplr*1024*1024;
case 'G':
return mltplr*1024*1024*1024;
default:
return 0;
}
}
}
char* big (size_t buffsize) {
char sizes[] = {'K', 'M', 'G', 'T', 'E', 'P', 'Z', 'Y'};
char* size = malloc(6);
int count = -1;
unsigned long long buff = buffsize;
while (buff >= 1024) {
buff /= 1024;
count++;
}
if (buff > 9999)
return NULL;
if (count == -1) {
sprintf(size, "%d ", (int) buff);
return size;
}
else {
sprintf(size, "%d%c", (int) buff, sizes[count]);
return size;
}
}
int far (long where, long end) {
struct winsize w;
int i;
int count = 0;
int perc = where*100/end;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for (i = 0; i < w.ws_col; i++)
printf("\b");
char* currsize;
if ((currsize = big(where)) == NULL)
return 1;
printf("%5sB synced [>", currsize);
for (i = 0; i < (w.ws_col-24)*perc/100; i++) {
printf("\b");
printf("=>");
count++;
}
for (i = 0; i < (w.ws_col-24)-count; i++)
printf(" ");
printf("] %3d%% ", perc);
fflush(stdout);
return 0;
}
int all_zero(const char* data, size_t size) {
int chunks = size / sizeof(uint64_t);
while(chunks--) {
if (*(uint64_t*)data != 0)
return 0;
data += sizeof(uint64_t);
}
int bytes = size % sizeof(uint64_t);
for(int n = 0; n < bytes; ++n) {
if (data[n] != 0)
return 0;
}
return 1;
}
int write_actural_zeros(char* tempmem, size_t tempmemsize, uint64_t start, uint64_t len, const char* dest, FILE *destfile) {
if (fseek(destfile, start, SEEK_SET)){
fprintf(stderr,"%s: file seek error: %d: %s\n", dest,
errno, strerror(errno));
return 1;
}
memset(tempmem, 0, tempmemsize);
while(len) {
size_t zeros = (tempmemsize < len)?tempmemsize:len;
fwrite(tempmem, 1, zeros, destfile);
if (ferror(destfile)) {
fprintf(stderr,"%s: first file write error: %d: %s %llu\n", dest,
errno, strerror(errno), (unsigned long long)len);
return 1;
}
fflush(destfile);
if (ferror(destfile)) {
fprintf(stderr,"%s: first file flush error: %d: %s %llu\n", dest,
errno, strerror(errno), (unsigned long long)len);
return 1;
}
len -= zeros;
}
return 0;
}
int write_zeros(char* tempmem, size_t tempmemsize, uint64_t start, uint64_t len, const char* dest, FILE *destfile, mode_t dstmode, int dstisnew) {
fprintf(stderr, "write_zeros %llu -> %llu\n", (unsigned long long)start, (unsigned long long)start+len );
#if defined(__linux__)
if (S_ISBLK(dstmode)) {
int fd = fileno(destfile);
static int candiscard = -1;
if (candiscard == -1) {
int zeroes = 0;
int e = ioctl(fd, BLKDISCARDZEROES, &zeroes);
if (e)
fprintf(stderr, "%s : error getting discard zeros of block device : %i\n", dest, e);
if (zeroes)
candiscard = 1;
else {
candiscard = 0;
fprintf(stderr, "%s : no zero discards on block device\n", dest);
}
}
if (candiscard)
{
static size_t sectorsize = 0;
if (!sectorsize) {
int e = ioctl(fd, BLKSSZGET, §orsize);
if (e)
fprintf(stderr, "%s : error getting sector size of block device : %i\n", dest, e);
}
if (sectorsize) {
unsigned long long range[2];
unsigned long long unaligned = start%sectorsize;
if (unaligned) {
unaligned = sectorsize-unaligned;
fprintf(stderr, "prefix %llu bytes of zero to sector align\n", unaligned);
if (write_actural_zeros(tempmem, tempmemsize, start, unaligned, dest, destfile))
return 1;
start += unaligned;
len -= unaligned;
}
unaligned = len%sectorsize;
if (unaligned)
{
fprintf(stderr, "append %llu bytes of zeros post sector align\n", unaligned);
len -= unaligned;
}
range[0] = start/sectorsize;
range[1] = len/sectorsize;
fprintf(stderr,"discard sectors %llu from sector %llu\n", range[1], range[0]);
int e = ioctl(fileno(destfile), BLKDISCARD, &range);
if (!e) {
if (write_actural_zeros(tempmem, tempmemsize, start+len, unaligned, dest, destfile))
return 1;
return 0;
}
else len += unaligned;
fprintf(stderr, "discard failed error %i\n", e);
}
}
}
#endif
if (S_ISREG(dstmode)) {
if (dstisnew) {
int dstfileno = fileno(destfile);
off_t r = lseek(dstfileno, start+len, SEEK_SET);
if (r != start+len) {
fprintf(stderr,"%s: file seek error:\n", dest);
return 1;
}
r = lseek(dstfileno, start+len-1, SEEK_SET);
if (r != start+len-1) {
fprintf(stderr,"%s: file seek error:\n", dest);
return 1;
}
char c = 0;
if (write(dstfileno, &c, 1) != 1) {
fprintf(stderr,"%s: file write error:\n", dest);
return 1;
}
fseek(destfile, start+len, SEEK_SET);
return 0;
}
}
return write_actural_zeros(tempmem, tempmemsize, start, len, dest, destfile);
}
int copy (const char* srce, const char* dest, size_t buffsize, int progress) {
FILE *srcfile = fopen (srce,"rb");
if (srcfile == NULL) {
fprintf(stderr, "%s: file could not be read: %d: %s\n", srce, errno,
strerror(errno));
return 1;
}
int newfile = 0;
FILE *destfile;
//if cannot open r+b then file doesn't exist so open w+b (create it)
if ((destfile = fopen (dest,"r+b")) == NULL)
newfile = 1;
if ((destfile = fopen (dest,"w+b")) == NULL) {
fprintf(stderr, "%s: file could not be read: %d: %s\n", dest, errno,
strerror(errno));
return 1;
}
char* tmpch;
char* cmpch;
if ( (tmpch = malloc(buffsize)) == NULL) {
fprintf(stderr,"%lu: memory allocation error: %d: %s\n", (unsigned long)buffsize,
errno, strerror(errno));
return EXIT_FAILURE;
}
if ( (cmpch = malloc(buffsize)) == NULL) {
fprintf(stderr,"%lu: memory allocation error: %d: %s\n", (unsigned long)buffsize,
errno, strerror(errno));
return EXIT_FAILURE;
}
long offset = buffsize;
int lastw;
int blocksize = 1;
fseek(srcfile, 0, SEEK_END);
long end = ftell(srcfile);
rewind(srcfile);
long where = 0;
uint64_t zerosstart = 0;
uint64_t zeroslen = 0;
struct stat dststat;
if (fstat(fileno(destfile), &dststat)) {
fprintf(stderr,"%s: file stat error: %d: %s\n", srce, errno,
strerror(errno));
return 1;
}
int zeroaware = S_ISBLK(dststat.st_mode) || S_ISREG(dststat.st_mode);
do {
lastw = fread (tmpch, blocksize, buffsize, srcfile);
if (zeroaware && lastw!=0 && all_zero(tmpch, lastw)) {
if (!zeroslen)
zerosstart = where;
zeroslen += lastw;
}
else if (zeroslen) {
if (write_zeros(cmpch, buffsize, zerosstart, zeroslen, dest, destfile, dststat.st_mode, newfile))
return 1;
zerosstart = 0;
zeroslen = 0;
}
if (!zeroslen) {
//if cmpch contains something then compare and write
if (fread (cmpch, blocksize, lastw, destfile) == lastw) {
//compare corresponding blocks in src and dest and if equal skip
if (memcmp(cmpch,tmpch,lastw) != 0) {
//rewind destfile to where it was before comparison
fseek(destfile, -offset, SEEK_CUR);
fwrite(tmpch, blocksize, lastw, destfile);
if (ferror(destfile)) {
fprintf(stderr,"%s: first file write error: %d: %s\n", dest,
errno, strerror(errno));
return 1;
}
}
}
//if cmpch is empty then don't bother, just
else
{
fwrite(tmpch, blocksize, lastw, destfile);
if (ferror(destfile)) {
fprintf(stderr,"%s: first file write error: %d: %s\n", dest,
errno, strerror(errno));
return 1;
}
}
}
where += lastw;
if (progress) {
if (far(where, end)) {
fprintf(stderr,"progress bar failure: %d: %s\n", errno,
strerror(errno));
return 1;
}
}
//on the last instance fread will have nothing left to read so lastw = 0
} while (lastw > 0);
if (ferror(srcfile)) {
fprintf(stderr,"%s: file read error: %d: %s\n", srce, errno,
strerror(errno));
return 1;
}
if (fflush(destfile)) {
fprintf(stderr,"error while emptying buffer: %d: %s\n", errno,
strerror(errno));
return 1;
}
if (ferror(destfile)) {
fprintf(stderr,"%s: file write error: %d: %s\n", dest, errno,
strerror(errno));
return 1;
}
if (fclose(destfile)) {
fprintf(stderr,"%s: error closing file: %d: %s\n", dest, errno,
strerror(errno));
return 1;
}
free(tmpch);
free(cmpch);
return 0;
}
int main (int argc, char **argv) {
char* fname = argv[0];
unsigned long buffsize = 1024*1024;
int opt;
int progress = 0;
while ((opt = getopt(argc,argv,"pb:")) != -1) {
switch (opt) {
case 'b':
if ((buffsize = buffparser(optarg)) == 0) {
fprintf(stderr,"%s: invalid buffer multiplier\n", fname);
return EXIT_FAILURE;
}
if (buffsize <= 0) {
fprintf(stderr,"%s: invalid buffer size: %lu\n", fname,
buffsize);
return EXIT_FAILURE;
}
/* buffsize is used as a long later on so cannot be a value
between l_max and lu_max */
if (buffsize >= LONG_MAX) {
fprintf(stderr,"%s: exorbitant buffer size: %lu\n", fname,
buffsize);
return EXIT_FAILURE;
}
break;
case 'p':
progress = 1;
break;
case '?':
/* if -b is not given a value then getopt returns '?' and stores
b in optopt */
if (optopt == 'b') {
fprintf(stderr,"%s: invalid argument to -%c\n", fname,
optopt);
return EXIT_FAILURE;
}
fprintf(stderr, "Usage: %s [OPTIONS] SOURCE DEST\n",
fname);
break;
}
}
if (argv[optind] == NULL) {
fprintf(stderr,"%s: no source file specified\n", fname);
fprintf(stderr, "Usage: %s [OPTIONS] SOURCE DEST\n",
fname);
return EXIT_FAILURE;
}
if (argv[optind+1] == NULL) {
fprintf(stderr,"%s: no destination file specified\n", fname);
fprintf(stderr, "Usage: %s [OPTIONS] SOURCE DEST\n",
fname);
return EXIT_FAILURE;
}
if (argv[optind+2] != NULL) {
fprintf(stderr,"%s: unexpected extra argument\n", argv[optind+2]);
fprintf(stderr, "Usage: %s [OPTIONS] SOURCE DEST\n",
fname);
return EXIT_FAILURE;
}
const char* srce = argv[optind];
const char* dest = argv[optind+1];
int failure = copy(srce, dest, buffsize, progress);
if (failure)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}