-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbfcopy.cpp
More file actions
177 lines (142 loc) · 3.84 KB
/
bfcopy.cpp
File metadata and controls
177 lines (142 loc) · 3.84 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
/* Stream Archive I/O utility, Copyright (C) Olof Lagerkvist 2004-2022
*
* bfcopy.cpp
* Backup based copy file feature.
*/
#ifndef _UNICODE
#define _UNICODE
#endif
#ifndef _DLL
#define _DLL
#endif
#ifndef UNICODE
#define UNICODE
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#define WIN32_NO_STATUS
#include <process.h>
#include <windows.h>
#include <intsafe.h>
#undef WIN32_NO_STATUS
#include <ntdll.h>
#include <winstrct.h>
#include <wio.h>
#include "strarc.hpp"
class StrArc::FileCopyContext
{
StrArc *SourceReadSession;
StrArc *TargetWriteSession;
PUNICODE_STRING SourceName;
HANDLE SourceHandle;
HANDLE SourceReadThreadHandle;
PUNICODE_STRING TargetName;
HANDLE TargetHandle;
static
unsigned
CALLBACK
SourceReadThread(void *lpCtx)
{
FileCopyContext *Context = (FileCopyContext *)lpCtx;
DWORD dwResult = NO_ERROR;
if (!Context->SourceReadSession->
ReadFileStreamsToArchive(Context->SourceName,
Context->SourceHandle))
dwResult = GetLastError();
delete Context->SourceReadSession;
Context->SourceReadSession = NULL;
return dwResult;
}
public:
StrArc *GetSourceReadSession()
{
return SourceReadSession;
}
StrArc *GetTargetWriteSession()
{
return TargetWriteSession;
}
bool
GetSourceReadThreadResult(DWORD dwMilliseconds = INFINITE)
{
WaitForSingleObject(SourceReadThreadHandle, dwMilliseconds);
DWORD dwExitCode;
if (!GetExitCodeThread(SourceReadThreadHandle, &dwExitCode))
dwExitCode = GetLastError();
CloseHandle(SourceReadThreadHandle);
if (dwExitCode == NO_ERROR)
return true;
SetLastError(dwExitCode);
return false;
}
bool
StartSourceReadThread()
{
unsigned uiThreadId;
SourceReadThreadHandle = (HANDLE)
_beginthreadex(NULL, 0, SourceReadThread, this, 0, &uiThreadId);
return SourceReadThreadHandle != INVALID_HANDLE_VALUE;
}
bool operator!()
{
if ((SourceReadSession == NULL) |
(TargetWriteSession == NULL))
return true;
else
return false;
}
FileCopyContext(StrArc *Template,
DWORD dwPipeBufferSize,
PUNICODE_STRING SourceName,
HANDLE SourceHandle,
PUNICODE_STRING TargetName,
HANDLE TargetHandle)
: SourceReadSession(NULL),
TargetWriteSession(NULL),
SourceName(SourceName),
SourceHandle(SourceHandle),
TargetName(TargetName),
TargetHandle(TargetHandle),
SourceReadThreadHandle(INVALID_HANDLE_VALUE)
{
HANDLE hReadPipe;
HANDLE hWritePipe;
if (!CreatePipe(&hReadPipe, &hWritePipe, NULL, dwPipeBufferSize))
return;
SourceReadSession = Template->TemplateNew(hWritePipe);
TargetWriteSession = Template->TemplateNew(hReadPipe);
}
~FileCopyContext()
{
if (SourceReadSession != NULL)
delete SourceReadSession;
if (TargetWriteSession != NULL)
delete TargetWriteSession;
}
};
bool
StrArc::BackupCopyFile(HANDLE SourceHandle,
HANDLE TargetHandle,
PUNICODE_STRING SourceName,
PUNICODE_STRING TargetName)
{
FileCopyContext Context(this,
dwBufferSize,
SourceName,
SourceHandle,
TargetName,
TargetHandle);
if (!Context)
return false;
bool bSeekOnly = false;
if (!Context.StartSourceReadThread())
return false;
bool bWriteResult = Context.GetTargetWriteSession()->
WriteFileFromArchive(TargetName,
TargetHandle,
bSeekOnly);
bool bReadResult =
Context.GetSourceReadThreadResult();
return bReadResult & bWriteResult;
}