forked from abligh/sparsecopy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparsecopy.c
More file actions
600 lines (528 loc) · 13.9 KB
/
sparsecopy.c
File metadata and controls
600 lines (528 loc) · 13.9 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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/* (c) 2010 Flexiant Limited
*
* This file is released under the GPL v2
*
* See under usage() for a complete description of this program.
*/
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <getopt.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>
/* for BLKRRPART*/
#ifdef __linux__
#include <linux/fs.h>
#endif
#define NUMCHARS 40
/* State passed to copy routines */
static int truncate_flag =1;
static int nocheck_flag = 0;
static int quiet_flag = 0;
static int finalsize_flag = 0;
static int check_flag = 0;
static int progress_flag = 0;
static int errorremainder_flag =0;
static off_t blocksize = 512;
static off_t finalsize = 0;
static off_t seekpos = 0;
static off_t maxwrite = -1;
static off_t syncevery = -1;
static int source;
static int dest;
static off_t origdestextent;
static off_t origsourceextent;
/* Statistics flowing back again */
static long long int real=0, sparse=0, nonzerodest=0, total=0, towrite=0;
void usage()
{
fprintf(stderr,"\
sparsecopy v0.10\n\
\n\
Usage:\n\
sparsecopy [OPTIONS] SOURCE DEST\n\
\n\
Options:\n\
-o, --overlay Overlay existing file DEST (rather than truncating)\n\
-n, --nocheck Skip blank check\n\
-q, --quiet Quiet\n\
-m, --max SIZE Copy SIZE bytes maximum from SOURCE\n\
-e, --errorremainder Error if there is more to write beyond --max value\n\
-b, --blocksize SIZE Use SIZE blocksize in bytes (default 512)\n\
-s, --seek POS Start writing SOURCE at position POS in DEST\n\
-f, --finalsize SIZE Set size of DEST to SIZE when done (no effect on block devices)\n\
-c, --check Check the first block in the destination is zero, else abort\n\
-p, --progress Display progress indicator on stderr\n\
-y, --syncevery SIZE Issue an fdatasync() every SIZE byets\n\
-h, --help Display usage\n\
\n\
Sparsecopy copies from a source file (either sparse or non-sparse) to a destination file\n\
making the destination file sparse on the way. If the destination file already exists\n\
then the source file can be superimposed on top of it by using the -o switch (ignored\n\
if the destination file doesn't exist); in this case sparsecopy will (unless -n is\n\
specified as well) check that any blocks to be overlayed with zero (i.e. made sparse)\n\
are already zero in the destination images. When used with block devices, sparsecopy\n\
will not affect the size of the device.\n\
\n\
SOURCE can be \"-\" to indicate stdin. SOURCE or DEST can be raw devices if you wish.\n\
If you want to make a sparse file, either use /dev/zero for SOURCE and -m, or (quicker) use\n\
/dev/null for SOURCE and -f.\n\
\n\
SIZE and POS can be specified in blocks (default), or use the following suffixes:\n\
B Bytes (2^0 bytes)\n\
K Kilobytes (2^10 bytes)\n\
M Megabytes (2^20 bytes)\n\
G Gigabtytes (2^30 bytes)\n\
T Terabytes (2^40 bytes)\n\
P Perabytes (2^50 bytes)\n\
E Exabytes (2^60 bytes)\n\
\n\
Note that blocksize=1024 will set blocksize to 1024 512byte blocks (use 1024B if this is not\n\
what you mean). Also note that disk capacity is often measured using decimal megabytes etc.;\n\
we do not adopt this convention for compatibility with dd.\n");
}
/*
* getsize() returns the size of an argument that may have a sizing suffix
*/
off_t getsize(char * arg)
{
off_t param=0;
char *end, *found;
const char *suffix = "bkmgtpe";
param = strtoull(arg, &end, 10);
if (*end == '\0')
{
param *= blocksize;
}
else if ((found = strchr(suffix, tolower(*end))))
{
param <<= (10 * (found-suffix));
}
else
{
fprintf(stderr,"sparsecopy: Bad parameter\n");
exit(1);
}
return param;
}
/* First parse our options */
void parse_command_line(int argc, char **argv)
{
/* We can't evaluate these in the while() loop
* As blocksize might not have been altered yet
*/
char * maxwrite_arg=NULL;
char * blocksize_arg=NULL;
char * seekpos_arg=NULL;
char * finalsize_arg=NULL;
char * syncevery_arg=NULL;
while (1)
{
static struct option long_options[] =
{
{"overlay", no_argument, 0, 'o'},
{"nocheck", no_argument, 0, 'n'},
{"quiet", no_argument, 0, 'q'},
{"check", no_argument, 0, 'c'},
{"progress", no_argument, 0, 'p'},
{"help", no_argument, 0, 'h'},
{"errorremainder", no_argument, 0, 'e'},
{"maxwrite", required_argument, 0, 'm'},
{"blocksize", required_argument, 0, 'b'},
{"seek", required_argument, 0, 's'},
{"finalsize", required_argument, 0, 'f'},
{"syncevery", required_argument, 0, 'y'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
int c;
c = getopt_long (argc, argv, "onqcphem:b:s:f:y:",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c)
{
case 0:
/* If this option set a flag, do nothing else now. */
break;
case 'o':
truncate_flag=0;
break;
case 'n':
nocheck_flag=1;
break;
case 'q':
quiet_flag=1;
break;
case 'c':
check_flag=1;
break;
case 'p':
if (isatty(STDERR_FILENO))
progress_flag=1;
break;
case 'h':
usage();
exit(0);
break;
case 'e':
errorremainder_flag = 1;
break;
case 'b':
blocksize_arg = strdup(optarg);
break;
case 's':
seekpos_arg = strdup(optarg);
break;
case 'f':
finalsize_arg = strdup(optarg);
break;
case 'm':
maxwrite_arg = strdup(optarg);
break;
case 'y':
syncevery_arg = strdup(optarg);
break;
default:
usage();
exit(1);
}
}
/* Do blocksize first */
if (blocksize_arg)
{
blocksize = getsize(blocksize_arg);
if (blocksize<=0)
{
fprintf(stderr,"sparsecopy: Bad block size %lld\n",(long long int)blocksize);
exit(1);
}
free(blocksize_arg);
}
if (seekpos_arg)
{
seekpos = getsize(seekpos_arg);
if (seekpos<0)
{
fprintf(stderr,"sparsecopy: Bad seek position %lld\n",(long long int)seekpos);
exit(1);
}
free(seekpos_arg);
}
if (finalsize_arg)
{
finalsize = getsize(finalsize_arg);
if (finalsize<0)
{
fprintf(stderr,"sparsecopy: Bad final size %lld\n",(long long int)finalsize);
exit(1);
}
finalsize_flag++;
free(finalsize_arg);
}
if (maxwrite_arg)
{
maxwrite = getsize(maxwrite_arg);
if (maxwrite<0)
{
fprintf(stderr,"sparsecopy: Bad max write %lld\n",(long long int)maxwrite);
exit(1);
}
free(maxwrite_arg);
}
if (syncevery_arg)
{
syncevery = getsize(syncevery_arg);
if (syncevery<0)
{
fprintf(stderr,"sparsecopy: Bad sync %lld\n",(long long int)syncevery);
exit(1);
}
free(syncevery_arg);
}
/* Check we have exactly 2 remaining parameters */
if ((optind+2) != argc)
{
usage();
exit(1);
}
/* Print any remaining command line arguments (not options). */
if (!strcmp(argv[optind], "-"))
{
if (-1 == (source = dup(STDIN_FILENO)))
{
perror("dup() Could not open source file");
exit(2);
}
}
else
{
if (-1 == (source = open(argv[optind], O_RDONLY)))
{
perror("open() Could not open source file");
exit(2);
}
}
if (-1 == (dest = open(argv[optind+1],
((check_flag || !nocheck_flag)?O_RDWR:O_WRONLY)|
O_CREAT|
(truncate_flag?O_TRUNC:0),
0666
)))
{
perror("open() Could not open destination file");
exit(3);
}
if (-1 == (origdestextent = lseek(dest, 0, SEEK_END)))
{
perror("lseek(dest) Seek on destination file failed");
exit(4);
}
if (-1 == (lseek(dest, seekpos, SEEK_SET)))
{
perror("lseek(dest) Seek on destination file failed");
exit(4);
}
if (-1 == (origsourceextent = lseek(source, 0, SEEK_END)))
{
/* Seek will fail on STDIN */
progress_flag=0;
}
else if (-1 == (lseek(source, 0, SEEK_SET)))
{
perror("lseek(source) Seek on source file failed");
exit(4);
}
towrite = origsourceextent;
if ((maxwrite>0) && (maxwrite < origsourceextent))
{
towrite = maxwrite;
}
}
void dosparsecopy(const int source, const int dest)
{
char *zero, *incoming, *check;
size_t available;
size_t writtensincesync = 0;
int skipwrite;
double lastpercent = -1.0;
if (!(zero = calloc(1, blocksize)) || !(incoming = calloc(1, blocksize)) || !(check = calloc(1, blocksize)))
{
perror("calloc()/malloc() failed");
exit(5);
}
/* If they have specified the check option, check the first block of the dest file is zero*/
if (check_flag)
{
long long checklen=0;
if (-1 == (checklen = read(dest, check, blocksize)))
{
perror ("read(dest) Check read of destination failed");
exit(6);
}
/* Restore file pointer */
if (-1 == lseek(dest, 0, SEEK_SET))
{
perror("lseek(dest) Can't set destination file pointer");
exit(7);
}
/* Check for zeroes */
if (checklen && memcmp(check, zero, checklen))
{
fprintf(stderr, "Destination file failed initial zero block check\n");
exit(11);
}
}
/*
* This is the main loop
*/
while ((-1 != (available = read(source, incoming, (maxwrite>=0)?((maxwrite<blocksize)?maxwrite:blocksize):blocksize)))
&& (available > 0)
&& maxwrite)
{
skipwrite = 0;
if (maxwrite>0)
{
maxwrite-=available;
}
if(!memcmp(incoming, zero, available))
{
/* It is a zero block in the input stream */
off_t destpos;
if (-1 == (destpos = lseek(dest, 0, SEEK_CUR)))
{
perror("lseek(dest) Get desitination file position failed");
exit(4);
}
skipwrite = 1;
/* We don't do the check if nocheck_flag is set, or if we are beyond the
* end of the original file
*/
if (!(nocheck_flag || (destpos>=origdestextent)))
{
/*
* We need to check the resultant block is zero in the output stream is zero
* It might be crud left from a previous write if we are overwriting a file
*
* Note if overlay (!trunc_flag) was set, then origdestextent will be zero,
* and this code will never be executed
*/
long long checklen=0;
if (-1 == (checklen = read(dest, check, blocksize)))
{
perror ("read(dest) Check read of destination failed");
exit(6);
}
/* Restore file pointer */
if (-1 == lseek(dest, destpos, SEEK_SET))
{
perror("lseek(dest) Can't set destination file pointer");
exit(7);
}
if (checklen && memcmp(check, zero, checklen))
{
skipwrite=0;
nonzerodest += available;
}
}
}
if (skipwrite)
{
/* Everything we read was zero, so we can just seek past it */
if(-1 == lseek(dest, available, SEEK_CUR))
{
perror("lseek(dest) sparse skip failed");
exit(8);
}
sparse += available;
}
else
{
/* Non-zero block, or non-zero destination just write it out */
if(write(dest, incoming, available) < available)
{
perror("write(dest) failed");
exit(9);
}
real += available;
writtensincesync += available;
if ((syncevery >0) && (writtensincesync >= syncevery))
{
fdatasync(dest); /* ignore errors */
writtensincesync = 0;
}
}
total = real + sparse;
if (progress_flag)
{
double percent = 100;
if (towrite)
percent = round((total * 1000.0) / towrite)/10;
if (lastpercent != percent)
{
char display[NUMCHARS+1];
int i;
int j;
j = round(NUMCHARS*percent/100.0);
display[NUMCHARS]=0;
lastpercent = percent;
for (i=0; i<NUMCHARS; i++)
{
display[i]=(i<j)?'=':((i==j)?'>':'-');
}
fprintf (stderr, "|%s| %3.1f%% %.3fMB of %.3fMB \r",
display,
percent,
(total*1.0/1024.0/1024.0),
(towrite*1.0/1024.0/1024.0)
);
}
}
}
if (progress_flag)
fprintf(stderr, "\n");
if ((maxwrite == 0) && errorremainder_flag)
{
available = read(source, incoming, 1);
if (available)
{
fprintf (stderr, "More data supplied than maximum permissible\n");
exit (12);
}
}
}
/*
* We may have lseek'd beyond the end of the file; this doesn't set the
* file length, so we need to call ftruncate(). But this errors on
* block devices, so we don't do it if the dest is a block device
*/
void force_length(int dest, off_t desired)
{
struct stat sb;
if (-1 == fstat(dest, &sb))
{
perror("stat failed");
exit(9);
}
close(source);
if (S_ISBLK(sb.st_mode))
{
close(dest);
/* Block device, may need to reread partition table */
#ifdef __linux__
int retries=40;
if (!quiet_flag)
fprintf(stderr,"Rereading partition table\n");
sync();
while (ioctl(dest, BLKRRPART) && (errno==EBUSY) && retries--)
{
/* Never let it be said that linux partition handling is a bodge */
usleep(250*1000);
}
#endif
}
else if (S_ISREG(sb.st_mode))
{
off_t truncpos;
if (-1 == (truncpos = lseek(dest, 0, SEEK_CUR)))
{
perror("lseek(dest) Get desitination file position failed");
exit(4);
}
if (origdestextent > truncpos)
{
truncpos = origdestextent;
}
if (finalsize_flag)
{
truncpos = finalsize;
}
if (-1 == ftruncate(dest, truncpos))
{
perror("ftruncate(dest) failed");
exit(10);
}
close(dest);
}
}
int main(int argc, char **argv, char *env)
{
parse_command_line(argc, argv);
dosparsecopy(source, dest);
force_length(dest, finalsize);
if (!quiet_flag)
{
fprintf(stderr, "sparsecopy: total bytes = %lld (real = %lld, sparse = %lld), nonzerodest = %lld\n",
real + sparse, real, sparse, nonzerodest);
}
exit (0);
}