-
Notifications
You must be signed in to change notification settings - Fork 23
/
syncjob.cpp
316 lines (294 loc) · 9.27 KB
/
syncjob.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
309
310
311
312
313
314
315
316
/////////////////////////////////////////////////////////////////////////////////
// Author: Steven Lamerton
// Copyright: Copyright (C) 2009 - 2010 Steven Lamerton
// License: GNU GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
/////////////////////////////////////////////////////////////////////////////////
#include "syncjob.h"
#include "../rules.h"
#include "../toucan.h"
#include "../data/syncdata.h"
#include "../basicfunctions.h"
#include "../fileops.h"
#include "../path.h"
#include <list>
#include <map>
#include <wx/string.h>
#include <wx/log.h>
#include <wx/dir.h>
#include <wx/filename.h>
SyncJob::SyncJob(SyncData *Data) : Job(Data){
;
}
void* SyncJob::Entry(){
SyncData *data = static_cast<SyncData*>(GetData());
SyncFiles sync(data->GetSource(), data->GetDest(), data);
sync.Execute();
return NULL;
}
SyncFiles::SyncFiles(const wxFileName &syncsource, const wxFileName &syncdest, SyncData* syncdata)
: SyncBase(syncsource, syncdest, syncdata)
{
Path::CreateDirectoryPath(sourceroot);
Path::CreateDirectoryPath(destroot);
}
bool SyncFiles::Execute(){
auto sourcepaths = FolderContentsToList(sourceroot);
auto destpaths = FolderContentsToList(destroot);
auto mergeresult = MergeListsToMap(sourcepaths, destpaths);
OperationCaller(mergeresult);
return true;
}
void SyncFiles::OnSourceNotDestFile(const wxFileName &source, const wxFileName &dest){
//Clean doesnt copy any files
if(data->GetFunction() != _("Clean")){
if(data->GetRules()->Matches(source) != Excluded){
if(CopyIfNeeded(source, dest)){
if(data->GetFunction() == _("Move")){
RemoveFile(source);
}
}
}
}
}
void SyncFiles::OnNotSourceDestFile(const wxFileName &source, const wxFileName &dest){
if(data->GetFunction() == _("Mirror") || data->GetFunction() == _("Clean")){
if(data->GetRules()->Matches(dest) != Excluded){
RemoveFile(dest);
}
}
else if(data->GetFunction() == _("Equalise")){
if(data->GetRules()->Matches(dest) != Excluded){
CopyIfNeeded(dest, source);
}
}
}
void SyncFiles::OnSourceAndDestFile(const wxFileName &source, const wxFileName &dest){
if(data->GetFunction() == _("Copy") || data->GetFunction() == _("Mirror") || data->GetFunction() == _("Move")){
if(data->GetRules()->Matches(source) != Excluded){
if(CopyIfNeeded(source, dest)){
if(data->GetFunction() == _("Move")){
RemoveFile(source);
}
}
}
}
else if(data->GetFunction() == _("Equalise")){
SourceAndDestCopy(source, dest);
}
}
void SyncFiles::OnSourceNotDestFolder(const wxFileName &source, const wxFileName &dest){
//Always recurse into the next directory unless we have an absolute exclude
RuleResult res = data->GetRules()->Matches(source);
if(res != AbsoluteExcluded){
SyncFiles sync(source, dest, data);
sync.Execute();
if(data->GetFunction() != _("Clean")){
wxDir destdir(dest.GetFullPath());
wxDir sourcedir(source.GetFullPath());
if(!destdir.HasFiles() && !destdir.HasSubDirs() && res == Excluded){
DeleteDirectory(dest);
}
else{
//Set the timestamps if needed
if(data->GetTimeStamps()){
CopyFolderTimestamp(source, dest);
}
}
if(!sourcedir.HasFiles() && !sourcedir.HasSubDirs() && data->GetFunction() == _("Move")){
//If we are moving and there are no files left then we need to remove the folder
DeleteDirectory(source);
}
}
}
}
void SyncFiles::OnNotSourceDestFolder(const wxFileName &source, const wxFileName &dest){
RuleResult res = data->GetRules()->Matches(dest);
if(data->GetFunction() == _("Mirror") || data->GetFunction() == _("Clean")){
if(res != Excluded && res != AbsoluteExcluded){
DeleteDirectory(dest);
}
}
else if(data->GetFunction() == _("Equalise")){
if(res != AbsoluteExcluded){
SyncFiles sync(source, dest, data);
sync.Execute();
}
//Set the timestamps if needed
if(data->GetTimeStamps()){
CopyFolderTimestamp(dest, source);
}
}
}
void SyncFiles::OnSourceAndDestFolder(const wxFileName &source, const wxFileName &dest){
//Always recurse into the next directory
RuleResult res = data->GetRules()->Matches(source);
if(res != AbsoluteExcluded){
SyncFiles sync(source, dest, data);
sync.Execute();
}
if(data->GetFunction() != _("Clean")){
wxDir destdir(dest.GetFullPath());
wxDir sourcedir(source.GetFullPath());
if(!destdir.HasFiles() && !destdir.HasSubDirs() && res != Excluded && res != AbsoluteExcluded){
DeleteDirectory(dest);
}
else{
//Set the timestamps if needed
if(data->GetTimeStamps()){
CopyFolderTimestamp(source, dest);
}
}
if(!sourcedir.HasFiles() && !sourcedir.HasSubDirs() && data->GetFunction() == _("Move")){
//If we are moving and there are no files left then we need to remove the folder
DeleteDirectory(source);
}
}
}
bool SyncFiles::CopyFile(const wxFileName &source, const wxFileName &dest){
wxString sourcepath = source.GetFullPath(), destpath = dest.GetFullPath();
//ATTN : Needs linux support
#ifdef __WXMSW__
long destAttributes = 0;
long sourceAttributes = 0;
if(data->GetIgnoreRO() || data->GetAttributes()){
destAttributes = dest.FileExists() ? GetFileAttributes(destpath.fn_str()) : FILE_ATTRIBUTE_NORMAL;
sourceAttributes = source.FileExists() ? GetFileAttributes(sourcepath.fn_str()) : FILE_ATTRIBUTE_NORMAL;
}
if(data->GetIgnoreRO()){
SetFileAttributes(destpath.fn_str(), FILE_ATTRIBUTE_NORMAL);
}
#endif
wxString desttemp = dest.GetPathWithSep() + wxT("Toucan.tmp");
if(File::Copy(source, desttemp)){
if(File::Rename(desttemp, dest, true)){
OutputProgress(_("Copied ") + sourcepath, Message);
}
else{
OutputProgress(_("Failed to copy ") + sourcepath, Error);
if(wxFileExists(desttemp)){
wxRemoveFile(desttemp);
}
#ifdef __WXMSW__
if(data->GetIgnoreRO()){
SetFileAttributes(sourcepath, sourceAttributes);
}
#endif
return false;
}
}
else{
OutputProgress(_("Failed to copy ") + sourcepath, Error);
#ifdef __WXMSW__
if(data->GetIgnoreRO()){
SetFileAttributes(sourcepath.fn_str(), sourceAttributes);
}
#endif
return false;
}
if(data->GetTimeStamps()){
wxDateTime access, mod, created;
if(source.GetTimes(&access, &mod, &created)){
dest.SetTimes(&access, &mod, &created);
}
}
#ifdef __WXMSW__
if(data->GetIgnoreRO()){
SetFileAttributes(destpath.fn_str(), destAttributes);
SetFileAttributes(sourcepath.fn_str(), sourceAttributes);
}
if(data->GetAttributes()){
SetFileAttributes(destpath.fn_str(), sourceAttributes);
}
#endif
return true;
}
bool SyncFiles::CopyIfNeeded(const wxFileName &source, const wxFileName &dest){
//If the dest file doesn't exists then we must copy
if(!dest.FileExists()){
return CopyFile(source, dest);
}
//If copy if anything says copy
if((data->GetCheckSize() && ShouldCopySize(source, dest))
|| (data->GetCheckTime() && ShouldCopyTime(source, dest))
|| (data->GetCheckShort() && ShouldCopyShort(source, dest))
|| (data->GetCheckFull() && ShouldCopyFull(source, dest))
|| (!data->GetCheckSize() && !data->GetCheckTime()
&& !data->GetCheckShort() && !data->GetCheckFull())){
return CopyFile(source, dest);
}
if(!data->GetNoSkipped()) {
OutputProgress(_("Skipped ") + source.GetFullPath(), Message);
}
return false;
}
bool SyncFiles::DeleteDirectory(const wxFileName &path){
if(wxGetApp().GetAbort()){
return true;
}
//If we have a root directory then return
if (path.GetDirCount() ==0)
return false;
wxDir* dir = new wxDir(path.GetFullPath());
wxString filename;
if(dir->GetFirst(&filename)){
do {
if(wxGetApp().GetAbort()){
return true;
}
if(wxDirExists(path.GetFullPath() + filename)){
//This is a directory, use DirName() so that it has a trailing path separator
DeleteDirectory(wxFileName::DirName(path.GetFullPath() + filename));
}
else{
if(RemoveFile(wxFileName(path.GetFullPath() + filename))){
OutputProgress(_("Removed file ") + path.GetFullPath() + filename, Message);
}
else{
OutputProgress(_("Failed to remove file ") + path.GetFullPath() + filename, Error);
}
}
}
while (dir->GetNext(&filename) );
}
delete dir;
{
wxLogNull log;
if(wxRmdir(path.GetFullPath())){
OutputProgress(_("Removed directory ") + path.GetFullPath(), Message);
}
else{
OutputProgress(_("Failed to remove directory ") + path.GetFullPath(), Error);
}
}
return true;
}
bool SyncFiles::CopyFolderTimestamp(const wxFileName &source, const wxFileName &dest){
wxDateTime access, mod, created;
source.GetTimes(&access, &mod, &created);
dest.SetTimes(&access, &mod, &created);
return true;
}
bool SyncFiles::RemoveFile(const wxFileName &path){
if(File::Delete(path, data->GetRecycle(), data->GetIgnoreRO())){
return true;
}
return false;
}
bool SyncFiles::SourceAndDestCopy(const wxFileName &source, const wxFileName &dest){
wxDateTime to, from;
dest.GetTimes(NULL, &to, NULL);
source.GetTimes(NULL, &from, NULL);
from.MakeTimezone(wxDateTime::UTC, true);
to.MakeTimezone(wxDateTime::UTC, true);
if(from.IsLaterThan(to)){
if(data->GetRules()->Matches(source) != Excluded){
CopyIfNeeded(source, dest);
}
}
else if(to.IsLaterThan(from)){
if(data->GetRules()->Matches(source) != Excluded){
CopyIfNeeded(dest, source);
}
}
return true;
}