-
Notifications
You must be signed in to change notification settings - Fork 6
/
rawcopy_without_diff.cpp
672 lines (603 loc) · 15.5 KB
/
rawcopy_without_diff.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
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#define WIN32_LEAN_AND_MEAN
#include <stdlib.h>
#include <winstrct.h>
#include <winioctl.h>
/*
#ifdef _DLL
#pragma comment(lib, "minwcrt")
#endif
*/
#define STDBUFSIZ 512
#define BIGBUFSIZ (512 << 10)
char buffer[STDBUFSIZ];
LARGE_INTEGER copylength = { 0 };
LARGE_INTEGER readbytes = { 0 };
LARGE_INTEGER writtenblocks = { 0 };
DWORD bufsiz = STDBUFSIZ;
HANDLE hIn = INVALID_HANDLE_VALUE, hOut = INVALID_HANDLE_VALUE;
int
main(int argc, char **argv)
{
bool bBigBufferMode = false; // -m switch
bool bDeviceLockRequired = true; // not -l switch
bool bVerboseMode = false; // -v switch
bool bIgnoreErrors = false; // -i switch
bool bCreateSparse = false; // -s switch
bool bNonBufferedIn = false; // -r switch
bool bNonBufferedOut = false; // -w switch
bool bWriteThrough = false; // -x switch
bool bExtendedDASDIO = false; // -d switch
bool bDisplayHelp = false;
DWORD retrycount = 0; // -f switch
SIZE_T sizBigBufferSize = BIGBUFSIZ; // -m:nnn parameter
// Nice argument parse loop :)
while (argv[1] ? argv[1][0] ? (argv[1][0] | 0x02) == '/' : false : false)
{
while ((++argv[1])[0])
switch (argv[1][0] | 0x20)
{
case 'm':
bBigBufferMode = true;
if (argv[1][1] == ':')
{
char *szSizeSuffix = NULL;
sizBigBufferSize = strtoul(argv[1] + 2, &szSizeSuffix, 0);
if (szSizeSuffix == argv[1] + 2)
{
fprintf(stderr,
"Invalid buffer size: %s\r\n", szSizeSuffix);
return -1;
}
if (szSizeSuffix[0] ? szSizeSuffix[1] != 0 : false)
{
fprintf(stderr,
"Invalid buffer size: %s\r\n", argv[1] + 2);
return -1;
}
switch (szSizeSuffix[0])
{
case 'G':
sizBigBufferSize <<= 10;
case 'M':
sizBigBufferSize <<= 10;
case 'K':
sizBigBufferSize <<= 10;
break;
case 'g':
sizBigBufferSize *= 1000;
case 'm':
sizBigBufferSize *= 1000;
case 'k':
sizBigBufferSize *= 1000;
case 0:
break;
default:
fprintf(stderr,
"Invalid size suffix: %c\r\n", szSizeSuffix[0]);
return -1;
}
argv[1] += strlen(argv[1]) - 1;
}
break;
case 'f':
if (argv[1][1] == ':')
{
retrycount = strtoul(argv[1] + 2, NULL, 0);
if (retrycount == 0)
retrycount = 10;
argv[1] += strlen(argv[1]) - 1;
}
break;
case 'l':
bDeviceLockRequired = false;
break;
case 'i':
bIgnoreErrors = true;
break;
case 'v':
bVerboseMode = true;
break;
case 's':
bCreateSparse = true;
break;
case 'r':
bNonBufferedIn = true;
break;
case 'w':
bNonBufferedOut = true;
break;
case 'x':
bWriteThrough = true;
break;
case 'd':
bExtendedDASDIO = true;
break;
default:
bDisplayHelp = true;
}
--argc;
++argv;
}
if (bIgnoreErrors)
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
if (bDisplayHelp)
{
puts("File and device read/write utility.\r\n"
"\n"
"Version 1.2.5. Copyright (C) Olof Lagerkvist 1997-2011.\r\n"
"This program is open source freeware.\r\n"
"http://www.ltr-data.se [email protected]\r\n"
"\n"
"Usage:\r\n"
"rawcopy [-lvirwxsd] [-f[:count]] [-m[:bufsize]]\r\n"
" [[[[[skipforward] copylength] infile] outfile]\r\n"
"\n"
"Default infile/outfile if none or blank given is standard input/output device.\r\n"
"-l Access devices without locking access to them. Use this switch when you\r\n"
" are reading/writing physical drives and other processes are using the\r\n"
" drives.\r\n"
" Note! This may destroy your data!\r\n"
"\n"
"-v Verbose mode. Writes to stderr what is being done.\r\n"
"\n"
"-f Number of retries on failed I/O operations.\r\n"
"\n"
"-m Try to buffer more of input file into memory before writing to output\r\n"
" file. It is not possible to ignore read/write failures if this switch is\r\n"
" used.\r\n"
"\n"
" Buffer size may be suffixed with K,M or G. If -m is given without a\r\n"
" buffer size, 512 KB is assumed. If -m is not given a buffer size of 512\r\n"
" bytes is used and read/write failures can be ignored.\r\n"
"\n"
"-i Ignores and skip over read/write failures without displaying any dialog\r\n"
" boxes.\r\n"
"\n"
"-r Read input without intermediate buffering.\r\n"
"\n"
"-w Write output without intermediate buffering.\r\n"
"\n"
"-x Write through to output without going via system cache.\r\n"
"\n"
"-s Creates output file as sparse file on NTFS volumes.\r\n"
"\n"
"-d Use extended DASD I/O when reading/writing disks. You also need to\n"
" select a compatible buffer size with the -m switch for successful\r\n"
" operation.\r\n"
"\n"
"Examples for Windows NT:\r\n"
"rawcopy -m diskimage.img \\\\.\\A:\r\n"
" Writes a diskette image file called \"diskimage.img\" to a physical diskette\r\n"
" in drive A:.\r\n"
"rawcopy \\\\.\\PIPE\\MYPIPE \"\"\r\n"
" Reads data from named pipe \"MYPIPE\" on the local machine and write on\r\n"
" standard output.\r\n"
"rawcopy 1 \\\\.\\PhysicalDrive0 parttable\r\n"
" Copies sector 1 (partition table) from first physical harddrive to file\r\n"
" called \"parttable\".");
return 0;
}
LARGE_INTEGER skipforward = { 0 };
if (argc > 4)
{
char SizeSuffix;
switch (sscanf(argv[1], "%I64i%c",
&skipforward, &SizeSuffix))
{
case 2:
switch (SizeSuffix)
{
case 'G':
skipforward.QuadPart <<= 10;
case 'M':
skipforward.QuadPart <<= 10;
case 'K':
skipforward.QuadPart <<= 10;
break;
case 'g':
skipforward.QuadPart *= 1000;
case 'm':
skipforward.QuadPart *= 1000;
case 'k':
skipforward.QuadPart *= 1000;
case 0:
break;
default:
fprintf(stderr,
"Invalid forward skip suffix: %c\r\n", SizeSuffix);
return -1;
}
case 1:
break;
default:
fprintf(stderr, "Invalid skip size: %s\n", argv[1]);
return -1;
}
if (bVerboseMode)
printf("Skipping %I64i bytes.\n", skipforward);
argv++;
argc--;
}
if (argc > 3)
{
char SizeSuffix;
switch (sscanf(argv[1], "%I64i%c",
©length, &SizeSuffix))
{
case 2:
switch (SizeSuffix)
{
case 'G':
copylength.QuadPart <<= 10;
case 'M':
copylength.QuadPart <<= 10;
case 'K':
copylength.QuadPart <<= 10;
break;
case 'g':
copylength.QuadPart *= 1000;
case 'm':
copylength.QuadPart *= 1000;
case 'k':
copylength.QuadPart *= 1000;
case 0:
break;
default:
fprintf(stderr, "Invalid copylength suffix: %c\r\n",
SizeSuffix);
return -1;
}
case 1:
break;
default:
fprintf(stderr, "Invalid copylength: %s\n", argv[1]);
return 1;
}
if (bVerboseMode)
printf("Copying %I64i bytes.\n", copylength);
argv++;
argc--;
}
if (argc > 2 ? argv[1][0] : false)
hIn = CreateFile(argv[1],
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN |
(bNonBufferedIn ? FILE_FLAG_NO_BUFFERING : 0),
NULL);
else
hIn = hStdIn;
if (hIn == INVALID_HANDLE_VALUE)
{
win_perror(argc > 1 ? argv[1] : "stdin");
return -1;
}
if (skipforward.QuadPart)
{
if (SetFilePointer(hIn, skipforward.LowPart, &skipforward.HighPart,
FILE_CURRENT) == 0xFFFFFFFF)
if (win_errno != NO_ERROR)
{
win_perror("SetFilePointer()");
return -1;
}
}
if (bExtendedDASDIO)
{
// Turn on FSCTL_ALLOW_EXTENDED_DASD_IO so that we can make sure that we
// read the entire drive
DWORD dwReadSize;
DeviceIoControl(hIn,
FSCTL_ALLOW_EXTENDED_DASD_IO,
NULL,
0,
NULL,
0,
&dwReadSize,
NULL);
}
char *bufptr;
// If -m option is used we use big buffer
if (bBigBufferMode)
{
if ((bufptr = (char *)LocalAlloc(LPTR, sizBigBufferSize)) != NULL)
bufsiz = LocalSize(bufptr);
else
{
bufptr = buffer;
win_perror("Memory allocation error, "
"using 512 bytes buffer instead%%nError message");
}
if (bVerboseMode)
fprintf(stderr, "Buffering %u bytes.\n", bufsiz);
}
// -m is not used
else
{
bufptr = buffer;
if (bVerboseMode)
fprintf(stderr,
"Sequential scan mode used, "
"max read/write buffer is %u bytes.\n", bufsiz);
}
// Jump to next parameter if input file was given
if (argc > 2)
{
argv++;
argc--;
}
if (argc > 1 ? argv[1][0] : false)
hOut = CreateFile(argv[1],
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL |
(bNonBufferedOut ? FILE_FLAG_NO_BUFFERING : 0) |
(bWriteThrough ? FILE_FLAG_WRITE_THROUGH : 0),
hIn);
else
hOut = hStdOut;
if (hOut == INVALID_HANDLE_VALUE)
{
win_perror(argc > 1 ? argv[1] : "stdout");
return -1;
}
if (bExtendedDASDIO)
{
// Turn on FSCTL_ALLOW_EXTENDED_DASD_IO so that we can make sure that we
// read the entire drive
DWORD dwReadSize;
DeviceIoControl(hOut,
FSCTL_ALLOW_EXTENDED_DASD_IO,
NULL,
0,
NULL,
0,
&dwReadSize,
NULL);
}
// If not regular disk file, try to lock volume using FSCTL operation
BY_HANDLE_FILE_INFORMATION ByHandleFileInfo;
if (!GetFileInformationByHandle(hIn, &ByHandleFileInfo))
{
DWORD z;
FlushFileBuffers(hIn);
if (DeviceIoControl(hIn, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &z, NULL))
if (DeviceIoControl(hIn, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &z,
NULL))
{
if (bVerboseMode)
fputs("Source device locked and dismounted.\r\n", stderr);
}
else
{
if (bVerboseMode)
fputs("Source device locked.\r\n", stderr);
}
else
switch (win_errno)
{
case ERROR_NOT_SUPPORTED:
case ERROR_INVALID_FUNCTION:
case ERROR_INVALID_HANDLE:
case ERROR_INVALID_PARAMETER:
break;
default:
if (bDeviceLockRequired)
{
if (bVerboseMode)
{
DWORD dwSavedErrno = win_errno;
fprintf(stderr, "System Error %u.\n", dwSavedErrno);
win_errno = dwSavedErrno;
}
win_perror("Cannot lock source device (use -l to ignore)");
return -1;
}
else
win_perror("Warning! Source device not locked");
}
}
if (!GetFileInformationByHandle(hOut, &ByHandleFileInfo))
{
DWORD z;
FlushFileBuffers(hOut);
if (DeviceIoControl(hOut, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &z, NULL))
if (DeviceIoControl(hOut, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &z,
NULL))
{
if (bVerboseMode)
fputs("Target device locked and dismounted.\r\n", stderr);
}
else
{
if (bVerboseMode)
fputs("Target device locked.\r\n", stderr);
}
else
switch (win_errno)
{
case ERROR_NOT_SUPPORTED:
case ERROR_INVALID_FUNCTION:
case ERROR_INVALID_HANDLE:
case ERROR_INVALID_PARAMETER:
break;
default:
if (bDeviceLockRequired)
{
if (bVerboseMode)
{
DWORD dwSavedErrno = win_errno;
fprintf(stderr, "System Error %u.\n", dwSavedErrno);
win_errno = dwSavedErrno;
}
win_perror("Cannot lock target device (use -l to ignore)");
return -1;
}
else
win_perror("Warning! Target device not locked");
}
}
if (bCreateSparse)
{
DWORD dw;
if (!DeviceIoControl(hOut, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &dw,
NULL))
win_perror("Warning! Failed to set sparse flag for file");
}
for (;;)
{
Sleep(0);
if (bVerboseMode)
fprintf(stderr, "Reading block %I64u\r", writtenblocks);
// Read next block
DWORD dwBlockSize = bufsiz;
DWORD retries = 0;
if (copylength.QuadPart != 0)
if (readbytes.QuadPart >= copylength.QuadPart)
return 0;
else if ((dwBlockSize + readbytes.QuadPart) >= copylength.QuadPart)
{
dwBlockSize = (DWORD)(copylength.QuadPart - readbytes.QuadPart);
if (bVerboseMode)
fprintf(stderr, "Reading last %u bytes...\r", dwBlockSize);
}
DWORD dwReadSize;
while (!ReadFile(hIn, bufptr, dwBlockSize, &dwReadSize, NULL))
{
// End of pipe?
if (win_errno == ERROR_BROKEN_PIPE)
return 0;
WErrMsg errmsg;
if (retries < retrycount)
{
fprintf(stderr, "Rawcopy read failure: %s\nRetrying...\n",
(char*)errmsg);
retries++;
Sleep(500);
continue;
}
retries = 0;
switch (bIgnoreErrors ? IDIGNORE :
MessageBox(NULL, errmsg, "Rawcopy read failure",
MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION |
MB_DEFBUTTON2 | MB_TASKMODAL))
{
case IDABORT:
return -1;
case IDRETRY:
continue;
case IDIGNORE:
if (bVerboseMode)
{
CharToOem(errmsg, errmsg);
fprintf(stderr, "Skipping block %I64u: %s\r\n",
writtenblocks, errmsg);
}
dwReadSize = dwBlockSize;
ZeroMemory(bufptr, dwReadSize);
LONG lZero = 0;
if (SetFilePointer(hIn, bufsiz, &lZero, FILE_CURRENT) ==
0xFFFFFFFF)
if (win_errno != NO_ERROR)
{
win_perror("SetFilePointer()");
return -1;
}
}
break;
}
// Check for EOF condition
if (dwReadSize == 0)
{
if (bVerboseMode)
fputs("End of input.\r\n", stderr);
return 0;
}
readbytes.QuadPart += dwReadSize;
// Check for all-zeroes block
bool bZeroBlock = false;
if (bCreateSparse)
{
bZeroBlock = true;
for (PULONGLONG bufptr2 = (PULONGLONG) bufptr;
bufptr2 < (PULONGLONG) (bufptr + dwReadSize);
bufptr2++)
if (*bufptr2 != 0)
{
bZeroBlock = false;
break;
}
}
DWORD dwWriteSize = 0;
if (bZeroBlock)
{
LONG lZero = 0;
if (SetFilePointer(hOut, dwReadSize, &lZero, FILE_CURRENT) ==
0xFFFFFFFF)
if (win_errno != NO_ERROR)
{
win_perror("Fatal, cannot move file pointer");
return -1;
}
SetEndOfFile(hOut);
dwWriteSize = dwReadSize;
}
else
{
if (bVerboseMode)
fprintf(stderr, "Writing block %I64u\r", writtenblocks);
DWORD retries = 0;
// Write next block
while (!WriteFile(hOut, bufptr, dwReadSize, &dwWriteSize, NULL))
{
WErrMsg errmsg;
if (retries < retrycount)
{
fprintf(stderr, "Rawcopy write failure: %s\nRetrying...\n",
(char*)errmsg);
retries++;
Sleep(500);
continue;
}
retries = 0;
switch (bIgnoreErrors ? IDIGNORE :
MessageBox(NULL, errmsg, "Rawcopy write failure",
MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION |
MB_DEFBUTTON2 | MB_TASKMODAL))
{
case IDABORT:
return -1;
case IDRETRY:
continue;
case IDIGNORE:
if (bVerboseMode)
{
CharToOem(errmsg, errmsg);
fprintf(stderr, "Skipping block %I64u: %s\r\n",
writtenblocks, errmsg);
}
LONG lZero = 0;
if (SetFilePointer(hOut, dwReadSize - dwWriteSize, &lZero,
FILE_CURRENT) == 0xFFFFFFFF)
if (win_errno != NO_ERROR)
{
win_perror("Fatal, cannot move file pointer");
return -1;
}
}
break;
}
}
++writtenblocks.QuadPart;
if (copylength.QuadPart != 0)
if (writtenblocks.QuadPart >= copylength.QuadPart)
return 0;
if (dwWriteSize < dwReadSize)
fprintf(stderr, "Warning: %u bytes lost.\n",
dwReadSize - dwWriteSize);
}
}