-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyfm_process.cpp
More file actions
288 lines (236 loc) · 8.43 KB
/
Copy pathyfm_process.cpp
File metadata and controls
288 lines (236 loc) · 8.43 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
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <chrono>
#include <ctime>
#include <string>
#include <QCoreApplication>
#include <QtCore>
#include "yfm_process.h"
yfmProcess::yfmProcess(QObject *parent) :
QObject(parent)
, notification(&log)
, configuration(&log)
{
returnValue=0;
casesProcessed=0;
}
void yfmProcess::run()
{
log.initialize();
if (!configuration.load())
{
log.error("Loading configuration failed");
terminate();
return;
}
prepare();
if (!checkFolders())
{
log.error("Unable to run file transfer");
terminate();
return;
}
if (!moveCases())
{
log.error("Problems during transfer of files");
}
log.info(QString::number(casesProcessed)+" cases moved");
terminate();
}
void yfmProcess::terminate()
{
log.finalize();
if (!log.errorLog.empty())
{
notification.sendErrorNotification(&configuration);
}
log.info("",false);
emit finished();
}
void yfmProcess::prepare()
{
yearPrefix=QDateTime::currentDateTime().toString("yyyy").toStdString();
time(¤tTime);
}
bool yfmProcess::checkFolders()
{
fs::path source(configuration.locationSource.toStdString());
fs::path target(configuration.locationTarget.toStdString());
if (!fs::exists(source))
{
log.error("Source folder not acccessible");
return false;
}
if (!fs::exists(target))
{
// If target path does not exist, check if parent path exists. If not,
// there seems to be a mounting problem. Otherwise, try creating the
// target path.
if (!fs::exists(target.parent_path()))
{
log.error("Target folder not acccessible");
return false;
}
else
{
try
{
fs::create_directories(target);
}
catch (const fs::filesystem_error & e)
{
std::string errorMsg="Unable to create target folder " + target.string() + " Cause: " + boost::diagnostic_information(e);
log.error(QString::fromStdString(errorMsg));
return false;
}
}
}
// TODO: Check upfront if we can write files into target folder
return true;
}
bool yfmProcess::moveCases()
{
casesProcessed=0;
fs::path sourcePath(configuration.locationSource.toStdString());
if (fs::is_directory(sourcePath))
{
try
{
log.info("");
// Recursively iterate through folder
fs::directory_iterator dir_entry(sourcePath);
fs::directory_iterator iter_end;
while (dir_entry != iter_end)
{
if (fs::is_directory(dir_entry->path()))
{
time_t folderWriteTime=fs::last_write_time(dir_entry->path());
int hoursSinceWrite=int(difftime(currentTime,folderWriteTime)/3600.);
if (hoursSinceWrite>=configuration.waitHours)
{
fs::path currentFolder=dir_entry->path().stem();
fs::path folderPrefix=fs::path(getTargetPrefix(currentFolder));
fs::path targetPath=fs::path(configuration.locationTarget.toStdString()) / folderPrefix / currentFolder;
log.info("Processing case \"" + QString::fromStdString(currentFolder.string()) + "\"...");
log.info("Target folder: " + QString::fromStdString(targetPath.string()) );
try
{
if (fs::exists(targetPath))
{
log.error("Target folder already exists "+QString::fromStdString(targetPath.string()));
log.error("Skipping case.");
++dir_entry;
continue;
}
}
catch (const fs::filesystem_error & e)
{
std::string errorMsg="Unable to check for folder " + targetPath.string() + " Cause: " + boost::diagnostic_information(e);
log.error(QString::fromStdString(errorMsg));
return false;
}
try
{
fs::create_directories(targetPath);
}
catch (const fs::filesystem_error & e)
{
std::string errorMsg="Unable to create folder " + targetPath.string() + " Cause: " + boost::diagnostic_information(e);
log.error(QString::fromStdString(errorMsg));
return false;
}
if (!fs::exists(targetPath))
{
std::string errorMsg="Folder was not created " + targetPath.string();
log.error(QString::fromStdString(errorMsg));
return false;
}
if (!copyFiles(dir_entry->path(), targetPath))
{
log.error("Copying files failed.");
return false;
}
try
{
log.info("Removing case on source location");
fs::remove_all(dir_entry->path());
}
catch (const fs::filesystem_error & e)
{
std::string errorMsg="Unable to remove folder " + dir_entry->path().string() + " Cause: " + boost::diagnostic_information(e);
log.error(QString::fromStdString(errorMsg));
return false;
}
log.info("");
casesProcessed++;
}
}
++dir_entry;
}
}
catch (const fs::filesystem_error & e)
{
std::string errorMsg="Unable to access directory " + sourcePath.string() + " Cause: " + boost::diagnostic_information(e);
log.error(QString::fromStdString(errorMsg));
return false;
}
}
else
{
log.error("Can't access path " + configuration.locationSource);
return false;
}
return true;
}
std::string yfmProcess::getTargetPrefix(fs::path filename)
{
if (configuration.prefixMode==yfmConfiguration::prefix_year)
{
return yearPrefix;
}
else
{
return "";
}
}
bool yfmProcess::copyFiles(fs::path sourceFolder, fs::path targetFolder)
{
try
{
fs::directory_iterator entry(sourceFolder);
fs::directory_iterator iter_end;
while (entry != iter_end)
{
uintmax_t sourceSize=fs::file_size(entry->path());
fs::path destName=targetFolder / entry->path().filename();
try
{
fs::copy_file(entry->path(), destName, fs::copy_option::fail_if_exists);
}
catch (const fs::filesystem_error & e)
{
std::string errorMsg="Unable to copy file " + entry->path().string() + " to " + destName.string() + " Cause: " + boost::diagnostic_information(e);
log.error(QString::fromStdString(errorMsg));
return false;
}
uintmax_t destSize=fs::file_size(destName);
if (sourceSize!=destSize)
{
log.error("File sizes to not match: " + QString::number(sourceSize) + " vs " + QString::number(destSize));
return false;
}
// Output filename and sizes for later debugging
log.info("File copied " + QString::fromStdString(entry->path().filename().string()) + " (" + QString::number(sourceSize)+" -> "+QString::number(destSize)+")" );
++entry;
}
}
catch (const fs::filesystem_error & e)
{
std::string errorMsg="Unable to iterate folder " + sourceFolder.string() + " Cause: " + boost::diagnostic_information(e);
log.error(QString::fromStdString(errorMsg));
return false;
}
return true;
}