-
Notifications
You must be signed in to change notification settings - Fork 6
/
rawcopy_mutex.cpp
187 lines (156 loc) · 4.5 KB
/
rawcopy_mutex.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
#include "winstrct.h"
#include <stdio.h>
#include <process.h>
#define BUFSIZ 512
char buffer[BUFSIZ][2];
unsigned long copylength = 0,
written = 0;
void _USERENTRY asynccopy( HANDLE = INVALID_HANDLE_VALUE );
HANDLE hIn = INVALID_HANDLE_VALUE,
hOut = INVALID_HANDLE_VALUE,
hReadMutex = NULL,
hWriteMutex = NULL;
int main( int argc, char **argv, char **/*envp*/ )
{
if( argc > 3 )
{
if( (copylength = strtol( argv[1], NULL, 0 )) == 0 )
{
cerr << "Invalid length." << endl;
return -1;
}
argv++;
argc--;
}
if( argc > 1 )
if( argv[1][0] )
hIn = CreateFile( argv[1], GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL );
else
hIn = hStdIn;
else
hIn = hStdIn;
if( hIn == INVALID_HANDLE_VALUE )
{
win_perror( argc > 1 ? argv[1] : "stdin" );
return -1;
}
if( argc > 2 )
if( argv[2][0] )
hOut = CreateFile( argv[2], GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, hIn );
else
hOut = hStdOut;
else
hOut = hStdOut;
if( hOut == INVALID_HANDLE_VALUE )
{
win_perror( argc > 2 ? argv[2] : "stdout" );
return -1;
}
if( (hReadMutex = CreateMutex( NULL, false, NULL )) == NULL )
asynccopy();
if( (hWriteMutex = CreateMutex( NULL, false, NULL )) == NULL )
asynccopy();
HANDLE hSubthread = (HANDLE)_beginthread( asynccopy, 4096, (HANDLE)1 );
if( hSubthread == INVALID_HANDLE_VALUE )
asynccopy();
asynccopy( 0 );
WaitForSingleObject( hSubthread, INFINITE );
return 0;
}
void _USERENTRY asynccopy( HANDLE idx )
{
char *bufptr = buffer[idx == INVALID_HANDLE_VALUE ? 0 : (int)idx];
while(true)
{
Sleep( 0 );
// Make sure other thread is finished before we read next block
if( WaitForSingleObject( hReadMutex, INFINITE ) == WAIT_FAILED )
{
win_perror( "I/O wait read-sync error" );
exit( -1 );
}
// Read next block
DWORD dwReadSize;
while( !ReadFile( hIn, bufptr, BUFSIZ, &dwReadSize, NULL ) )
{
char *errmsg = win_error;
wsprintf( bufptr, "ReadFile() failed: %s", errmsg );
LocalFree( errmsg );
switch( MessageBox( NULL, bufptr, "RawCopy Error", MB_ABORTRETRYIGNORE|
MB_ICONEXCLAMATION|MB_DEFBUTTON2|MB_TASKMODAL ) )
{
case IDABORT:
exit( -1 );
case IDRETRY:
continue;
case IDIGNORE:
dwReadSize = BUFSIZ;
ZeroMemory( bufptr, BUFSIZ );
if( SetFilePointer( hIn, BUFSIZ, NULL, FILE_CURRENT ) == 0xFFFFFFFF )
{
win_perror( "Can't ignore" );
exit( -1 );
}
}
break;
}
// Check for EOF condition
if( !dwReadSize )
{
ReleaseMutex( hReadMutex );
if( idx == INVALID_HANDLE_VALUE )
exit( 0 );
return;
}
// Make sure other thread is finished before we write next block
if( WaitForSingleObject( hWriteMutex, INFINITE ) == WAIT_FAILED )
{
win_perror( "I/O wait write-sync error" );
exit( -1 );
}
// Let other thread read next block
if( !ReleaseMutex( hReadMutex ) )
{
win_perror( "I/O release read-sync error" );
exit( -1 );
}
// Write next block
DWORD dwWriteSize;
while( !WriteFile( hOut, bufptr, dwReadSize, &dwWriteSize, NULL ) )
{
char *errmsg = win_error;
wsprintf( bufptr, "WriteFile() failed: %s", errmsg );
LocalFree( errmsg );
switch( MessageBox( NULL, bufptr, "RawCopy Error", MB_ABORTRETRYIGNORE|
MB_ICONEXCLAMATION|MB_DEFBUTTON2|MB_TASKMODAL ) )
{
case IDABORT:
exit( -1 );
case IDRETRY:
continue;
case IDIGNORE:
if( SetFilePointer( hOut, BUFSIZ, NULL, FILE_CURRENT ) == 0xFFFFFFFF )
{
win_perror( "Can't ignore" );
exit( -1 );
}
}
break;
}
if( copylength )
if( ++written >= copylength )
exit( 0 );
if( dwWriteSize < dwReadSize )
cerr << "Warning: " << (dwWriteSize - dwReadSize) << " bytes lost." << endl;
// Let other thread write next block
if( !ReleaseMutex( hWriteMutex ) )
{
win_perror( "I/O release write-sync error" );
exit( -1 );
}
}
}