-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathCCopyAction.h
171 lines (149 loc) · 5.44 KB
/
CCopyAction.h
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
/*
Copyright (c) 2011 Wangdera Corporation ([email protected])
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include "CCopyFilter.h"
#include "CDirectoryAction.h"
#include "OutputWriter.h"
#include "CHoboCopyException.h"
class CCopyAction : public CDirectoryAction
{
private:
LONGLONG _byteCount;
LPCTSTR _destination;
int _directoryCount;
int _fileCount;
vector<CCopyFilter*> _filters;
int _skipCount;
bool _skipDenied;
LPCTSTR _source;
public:
CCopyAction::CCopyAction(LPCTSTR source, LPCTSTR destination, bool skipDenied, vector<CCopyFilter*> filters) : _filters(filters)
{
_source = source;
_destination = destination;
_skipDenied = skipDenied;
_fileCount = 0;
_directoryCount = 0;
_skipCount = 0;
_byteCount = 0;
}
LONGLONG get_ByteCount(void)
{
return _byteCount;
}
int get_DirectoryCount(void)
{
return _directoryCount;
}
int get_FileCount(void)
{
return _fileCount;
}
int get_SkipCount(void)
{
return _skipCount;
}
void VisitDirectoryFinal(LPCTSTR path)
{
CString message;
message.AppendFormat(TEXT("Copied directory %s"), path);
OutputWriter::WriteLine(message, VERBOSITY_THRESHOLD_NORMAL);
++_directoryCount;
}
void VisitDirectoryInitial(LPCTSTR path)
{
CString destDir;
Utilities::CombinePath(_destination, path, destDir);
Utilities::CreateDirectory(destDir);
CString message;
message.AppendFormat(TEXT("Created directory %s"), destDir);
OutputWriter::WriteLine(message, VERBOSITY_THRESHOLD_IF_VERBOSE);
}
void VisitFile(LPCTSTR path)
{
CString sourceFile;
Utilities::CombinePath(_source, path, sourceFile);
CString destinationFile;
Utilities::CombinePath(_destination, path, destinationFile);
Utilities::FixLongFilenames(destinationFile);
if (IsFileMatch(sourceFile))
{
BOOL worked = ::CopyFile(sourceFile, destinationFile, false);
if (!worked)
{
DWORD error = ::GetLastError();
if ((error == 5 || error == 32) && _skipDenied)
{
CString message;
message.Format(TEXT("Error %d accessing file %s. Skipping."), error, sourceFile);
OutputWriter::WriteLine(message, VERBOSITY_THRESHOLD_NORMAL);
++_skipCount;
}
else
{
CString errorMessage;
Utilities::FormatErrorMessage(error, errorMessage);
CString message;
message.AppendFormat(TEXT("Copy of file failed with error %s on file %s"),
errorMessage, sourceFile);
throw new CHoboCopyException(message);
}
}
else
{
CString message;
message.AppendFormat(TEXT("Copied file %s to %s"), sourceFile, destinationFile);
OutputWriter::WriteLine(message, VERBOSITY_THRESHOLD_IF_VERBOSE);
++_fileCount;
try
{
_byteCount += Utilities::GetFileSize(sourceFile);
}
catch (CHoboCopyException* x)
{
CString message;
message.AppendFormat(TEXT("Unable to calculate size of file. Size calculations may be incorrect. Message was: %s"),
x->get_Message());
OutputWriter::WriteLine(message, VERBOSITY_THRESHOLD_UNLESS_SILENT);
delete x;
}
}
}
else
{
CString message;
message.AppendFormat(TEXT("Skipping file %s because it doesn't meet filter criteria."), path);
OutputWriter::WriteLine(message);
++_skipCount;
}
}
private:
bool IsFileMatch(CString& file)
{
for (unsigned int iFilter = 0; iFilter < _filters.size(); ++iFilter)
{
if (!_filters[iFilter]->IsFileMatch(file))
{
return false;
}
}
return true;
}
};