-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCReadDirChangesImpl.cpp
308 lines (261 loc) · 11.4 KB
/
DCReadDirChangesImpl.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
#include "DCReadDirChangesImpl.h"
#include "iostream"
#include "chrono"
#include "fileapi.h"
#include "LogImpl.h"
#include "TimeUtils.h"
#include "FilePathUtils.h"
#include "IniFileUtils.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
const static wchar_t * g_sDelete = L"delete_";
CDCReadDirChangesImpl::CDCReadDirChangesImpl(const wchar_t *sHotPath, const wchar_t *sBcpPath, const wchar_t *sLogFile, const std::vector<std::pair<std::wstring, std::wstring>> &arrToDelete)
: m_sHotPath(sHotPath), m_sBcpPath(sBcpPath), m_sLogFile(sLogFile), m_arrToDelete(arrToDelete)
{
for (const auto &pair : m_arrToDelete) {
std::error_code ec;
if (std::filesystem::exists(pair.first, ec)) {
std::tm timeToDelete = { 0 };
const bool bFindTime = time_utils::GetTimeFromFilePath(pair.first.c_str(), timeToDelete);
if (bFindTime) {
StartFileDeleteThreadByTime(pair.first, pair.second, m_sLogFile, timeToDelete, false);
}
}
}
}
CDCReadDirChangesImpl::~CDCReadDirChangesImpl()
{
threadWatchDir.join();
threadInput.join();
if (INVALID_HANDLE_VALUE != m_hDir) {
CloseHandle(m_hDir);
}
SaveNotDeletedFiles();
}
void CDCReadDirChangesImpl::StartFileDeleteThreadByTime(std::wstring sFilePath, std::wstring sFileBcpPath, std::wstring sLogFile, std::tm timeToDelete, bool bAddToArr)
{
std::thread threadDeleteFile(&CDCReadDirChangesImpl::FileDeleteThreadByTime, this, sFilePath, sFileBcpPath, sLogFile, timeToDelete);
threadDeleteFile.detach();
if (bAddToArr) {
m_arrToDelete.push_back(std::make_pair(sFilePath, sFileBcpPath));
}
}
void CDCReadDirChangesImpl::FileDeleteThreadByTime(std::wstring sFilePath, std::wstring sFileBcpPath, std::wstring sLogFile, std::tm timeToDelete)
{
const std::time_t tTime = std::mktime(&timeToDelete);
const std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::from_time_t(tTime);
std::this_thread::sleep_until(timePoint);
std::error_code ec;
const bool bRetFile = std::filesystem::remove(sFilePath, ec);
const bool bRetBcp = std::filesystem::remove(sFileBcpPath, ec);
std::wstring sInfo;
if (bRetFile && bRetBcp) {
sInfo = std::wstring(L"The file was deleted because of \"delete_ISODATETIME\" prefix: ") + sFilePath + L" Backup file also was deleted: " + sFileBcpPath;
}
else {
sInfo = std::wstring(L"The delete operation on file ") + sFilePath + L" failed.";
}
CDCOut::OutInfoNoConsole(sLogFile.c_str(), sInfo.c_str());
}
void CDCReadDirChangesImpl::FileDeleteThread(std::wstring sFilePath, std::wstring sFileBcpPath, std::wstring sLogFile)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::error_code ec;
const bool bRetFile = std::filesystem::remove(sFilePath, ec);
const bool bRetBcp = std::filesystem::remove(sFileBcpPath, ec);
std::wstring sInfo;
if (bRetFile && bRetBcp) {
sInfo = std::wstring(L"The file was deleted because of \"delete_\" prefix: ") + sFilePath + L" Backup file also was deleted: " + sFileBcpPath;
}
else {
sInfo = std::wstring(L"The delete operation on file ") + sFilePath + L" failed.";
}
CDCOut::OutInfoNoConsole(sLogFile.c_str(), sInfo.c_str());
}
void CDCReadDirChangesImpl::ApplyLogFileFilter(const wchar_t *sInput)
{
std::vector<std::wstring> arrLinesFromLog;
CLogImpl::GetLinesByRegexFromLogFile(m_sLogFile.c_str(), arrLinesFromLog, sInput);
for (const std::wstring &sLine : arrLinesFromLog) {
std::wcout << sLine << std::endl;
}
std::wcout << L"Please enter regex, to filter log file." << std::endl;
}
void CDCReadDirChangesImpl::Start()
{
m_hDir = CreateFile(m_sHotPath.c_str(), // pointer to the file name
FILE_LIST_DIRECTORY, // access (read/write) mode
FILE_SHARE_READ|FILE_SHARE_DELETE|FILE_SHARE_WRITE, // share mode
NULL, // security descriptor
OPEN_EXISTING, // how to create
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, // file attributes
NULL // file with attributes to copy
);
if (INVALID_HANDLE_VALUE == m_hDir) {
CDCOut::OutInfo(m_sLogFile.c_str(), L"Invalid watch folder handle");
return;
}
threadWatchDir = std::thread(&CDCReadDirChangesImpl::WatchDirThread, this, m_hDir, m_sHotPath.c_str(), m_sBcpPath.c_str(), m_sLogFile.c_str());
threadInput = std::thread(&CDCReadDirChangesImpl::WaitForInputThread, this);
}
void CDCReadDirChangesImpl::Quit()
{
m_bRunning = false;
}
void CDCReadDirChangesImpl::SaveNotDeletedFiles()
{
for (const auto &pair : m_arrToDelete) {
std::error_code ec;
if (std::filesystem::exists(pair.first, ec)) {
ini_file_utils::AppendFileToDelete(pair.first.c_str(), pair.second.c_str(), m_sLogFile.c_str());
}
}
}
void CDCReadDirChangesImpl::WaitForInputThread()
{
while (m_bRunning) {
std::wstring sInput;
std::getline(std::wcin, sInput);
if (sInput == L":q") {
Quit();
}
else {
ApplyLogFileFilter(sInput.c_str());
}
}
}
void CDCReadDirChangesImpl::WatchDirThread(HANDLE hDir, const wchar_t *sHotPath, const wchar_t *sBcpPath, const wchar_t *sLogFile)
{
if (INVALID_HANDLE_VALUE == m_hDir) {
return;
}
OVERLAPPED overlapped;
overlapped.hEvent = CreateEvent(NULL, FALSE, 0, NULL);;
uint8_t change_buf[1024];
BOOL bSuccess = ReadDirectoryChangesW(hDir, change_buf, 1024, TRUE,
FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE,
NULL, &overlapped, NULL);
if (!bSuccess) {
return;
}
std::wstring sOldFileName;
while (true) {
if (!m_bRunning) {
return;
}
DWORD result = WaitForSingleObject(overlapped.hEvent, 200);
if (result == WAIT_OBJECT_0) {
DWORD bytes_transferred;
GetOverlappedResult(hDir, &overlapped, &bytes_transferred, FALSE);
FILE_NOTIFY_INFORMATION *pEvent = (FILE_NOTIFY_INFORMATION*)change_buf;
for (;;) {
std::wstring sOperationText;
std::wstring sFileName;
bool bDeleted(false);
bool bRenamedOld(false);
GetEventInformation(pEvent, sOperationText, bDeleted, bRenamedOld, sOldFileName, sFileName);
// Only do job if it's not old file name action
if (!bRenamedOld) {
if (sFileName != sLogFile) {
const std::filesystem::path pathFile = file_path_utils::MakePath(sHotPath, sFileName.c_str());
const std::filesystem::path pathBcp = file_path_utils::MakeBcpPath(sBcpPath, sFileName.c_str());
std::wstring sInfo;
if (!sOldFileName.empty()) {
sInfo = sOperationText + L": " + sOldFileName + L" -> "+ sFileName;
if (sFileName.rfind(g_sDelete, 0) == 0) {
bDeleted = true;
DeleteFileOnPrefix(pathFile, sBcpPath, sLogFile, sOldFileName.c_str(), sInfo, sFileName.c_str());
}
sOldFileName.clear();
}
else {
sInfo = sOperationText + L": " + sFileName;
}
CDCOut::OutInfoNoConsole(sLogFile, sInfo.c_str());
if (!bDeleted) {
std::error_code ec;
std::filesystem::copy(pathFile, pathBcp, std::filesystem::copy_options::update_existing, ec);
if (!ec) {
const std::wstring sBcpInfo = std::wstring(L"The file backup was created: ") + pathBcp.c_str();
CDCOut::OutInfoNoConsole(sLogFile, sBcpInfo.c_str());
}
}
}
}
// Are there more events to handle?
if (pEvent->NextEntryOffset) {
*((uint8_t**)&pEvent) += pEvent->NextEntryOffset;
}
else {
break;
}
}
// Queue the next event
bSuccess = ReadDirectoryChangesW(hDir, change_buf, 1024, TRUE,
FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE,
NULL, &overlapped, NULL);
if (!bSuccess) {
return;
}
}
}
}
void CDCReadDirChangesImpl::GetEventInformation(const FILE_NOTIFY_INFORMATION *pEvent, std::wstring &sOperationText, bool &bDeleted, bool &bRenamedOld, std::wstring &sOldFileName, std::wstring &sFileName)
{
DWORD name_len = pEvent->FileNameLength / sizeof(wchar_t);
sFileName.assign(pEvent->FileName, name_len);
switch(pEvent->Action)
{
case FILE_ACTION_ADDED:
{
sOperationText = L"The file was added to the directory";
}
break;
case FILE_ACTION_REMOVED:
{
sOperationText = L"The file was removed from the directory";
bDeleted = true;
}
break;
case FILE_ACTION_MODIFIED:
{
sOperationText = L"The file was modified";
}
break;
case FILE_ACTION_RENAMED_OLD_NAME:
{
bRenamedOld = true;
sOldFileName = sFileName; //To have correct backup file path when delete will be required
}
break;
case FILE_ACTION_RENAMED_NEW_NAME:
{
sOperationText = L"The file was renamed";
}
break;
default:
{
sOperationText = L"Unknown action";
}
break;
}
}
void CDCReadDirChangesImpl::StartFileDeleteThread(std::wstring sFilePath, std::wstring sFileBcpPath, std::wstring sLogFile)
{
std::thread threadDeleteFile(&CDCReadDirChangesImpl::FileDeleteThread, this, sFilePath, sFileBcpPath, sLogFile);
threadDeleteFile.detach();
}
void CDCReadDirChangesImpl::DeleteFileOnPrefix(const std::filesystem::path &pathFile, const wchar_t *sBcpPath, const wchar_t *sLogFile, const wchar_t *sOldFileName, std::wstring &sInfo, const wchar_t *sFileName)
{
std::tm timeToDelete = {0};
const bool bFindTime = time_utils::GetTimeFromFilePath(pathFile.c_str(), timeToDelete);
const std::filesystem::path pathToDeleteBcp = file_path_utils::MakeBcpPath(sBcpPath, sOldFileName);
if (bFindTime) {
StartFileDeleteThreadByTime(pathFile, pathToDeleteBcp, sLogFile, timeToDelete, true);
sInfo = std::wstring(L"The file will be deleted later because of \"delete_ISODATETIME\" prefix: ") + sFileName + L" Backup file also will be deleted: " + pathToDeleteBcp.c_str();
}
else {
StartFileDeleteThread(pathFile, pathToDeleteBcp, sLogFile);
}
}