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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
// * Copyright (C) 2008-2011 ZenJu (zhnmju123 AT gmx.de) *
// **************************************************************************
#include "file_hierarchy.h"
#include <zen/i18n.h>
#include <zen/utf8.h>
using namespace zen;
void HierarchyObject::removeEmptyRec()
{
bool haveEmpty = false;
auto isEmpty = [&](const FileSystemObject& fsObj) -> bool
{
bool objEmpty = fsObj.isEmpty();
if (objEmpty)
haveEmpty = true;
return objEmpty;
};
refSubFiles().remove_if(isEmpty);
refSubLinks().remove_if(isEmpty);
refSubDirs ().remove_if(isEmpty);
if (haveEmpty) //notify if actual deletion happened
notifySyncCfgChanged(); //mustn't call this in ~FileSystemObject(), since parent, usually a DirMapping, is already partially destroyed and existing as a pure HierarchyObject!
//recurse
std::for_each(refSubDirs().begin(), refSubDirs().end(), std::mem_fun_ref(&HierarchyObject::removeEmptyRec));
}
namespace
{
SyncOperation proposedSyncOperation(CompareFilesResult cmpResult,
bool selectedForSynchronization,
SyncDirection syncDir,
const std::wstring& syncDirConflict)
{
if (!selectedForSynchronization)
return cmpResult == FILE_EQUAL ?
SO_EQUAL :
SO_DO_NOTHING;
switch (cmpResult)
{
case FILE_LEFT_SIDE_ONLY:
switch (syncDir)
{
case SYNC_DIR_LEFT:
return SO_DELETE_LEFT; //delete files on left
case SYNC_DIR_RIGHT:
return SO_CREATE_NEW_RIGHT; //copy files to right
case SYNC_DIR_NONE:
return syncDirConflict.empty() ? SO_DO_NOTHING : SO_UNRESOLVED_CONFLICT;
}
break;
case FILE_RIGHT_SIDE_ONLY:
switch (syncDir)
{
case SYNC_DIR_LEFT:
return SO_CREATE_NEW_LEFT; //copy files to left
case SYNC_DIR_RIGHT:
return SO_DELETE_RIGHT; //delete files on right
case SYNC_DIR_NONE:
return syncDirConflict.empty() ? SO_DO_NOTHING : SO_UNRESOLVED_CONFLICT;
}
break;
case FILE_LEFT_NEWER:
case FILE_RIGHT_NEWER:
case FILE_DIFFERENT:
case FILE_CONFLICT:
switch (syncDir)
{
case SYNC_DIR_LEFT:
return SO_OVERWRITE_LEFT; //copy from right to left
case SYNC_DIR_RIGHT:
return SO_OVERWRITE_RIGHT; //copy from left to right
case SYNC_DIR_NONE:
return syncDirConflict.empty() ? SO_DO_NOTHING : SO_UNRESOLVED_CONFLICT;
}
break;
case FILE_DIFFERENT_METADATA:
switch (syncDir)
{
case SYNC_DIR_LEFT:
return SO_COPY_METADATA_TO_LEFT;
case SYNC_DIR_RIGHT:
return SO_COPY_METADATA_TO_RIGHT;
case SYNC_DIR_NONE:
return syncDirConflict.empty() ? SO_DO_NOTHING : SO_UNRESOLVED_CONFLICT;
}
break;
case FILE_EQUAL:
assert(syncDir == SYNC_DIR_NONE);
return SO_EQUAL;
}
return SO_DO_NOTHING; //dummy
}
template <class Predicate> inline
bool hasDirectChild(const HierarchyObject& hierObj, Predicate p)
{
return std::find_if(hierObj.refSubFiles().begin(), hierObj.refSubFiles().end(), p) != hierObj.refSubFiles().end() ||
std::find_if(hierObj.refSubLinks().begin(), hierObj.refSubLinks().end(), p) != hierObj.refSubLinks().end() ||
std::find_if(hierObj.refSubDirs(). begin(), hierObj.refSubDirs(). end(), p) != hierObj.refSubDirs ().end();
}
}
SyncOperation FileSystemObject::testSyncOperation(SyncDirection testSyncDir, bool active) const
{
return proposedSyncOperation(getCategory(), active, testSyncDir, getSyncOpConflict());
}
SyncOperation FileSystemObject::getSyncOperation() const
{
return FileSystemObject::testSyncOperation(syncDir, selectedForSynchronization);
//no *not* make a virtual call to testSyncOperation()! See FileMapping::testSyncOperation()!
}
//SyncOperation DirMapping::testSyncOperation() const -> not required: we do NOT want to consider child elements when testing!
SyncOperation DirMapping::getSyncOperation() const
{
if (!syncOpUpToDate)
{
syncOpUpToDate = true;
//redetermine...
//suggested operation *not* considering child elements
syncOpBuffered = FileSystemObject::getSyncOperation();
//action for child elements may occassionally have to overwrite parent task:
switch (syncOpBuffered)
{
case SO_OVERWRITE_LEFT:
case SO_OVERWRITE_RIGHT:
case SO_MOVE_LEFT_SOURCE:
case SO_MOVE_LEFT_TARGET:
case SO_MOVE_RIGHT_SOURCE:
case SO_MOVE_RIGHT_TARGET:
assert(false);
case SO_CREATE_NEW_LEFT:
case SO_CREATE_NEW_RIGHT:
case SO_COPY_METADATA_TO_LEFT:
case SO_COPY_METADATA_TO_RIGHT:
case SO_EQUAL:
break; //take over suggestion, no problem for child-elements
case SO_DELETE_LEFT:
case SO_DELETE_RIGHT:
case SO_DO_NOTHING:
case SO_UNRESOLVED_CONFLICT:
{
if (isEmpty<LEFT_SIDE>())
{
//1. if at least one child-element is to be created, make sure parent folder is created also
//note: this automatically fulfills "create parent folders even if excluded";
//see http://sourceforge.net/tracker/index.php?func=detail&aid=2628943&group_id=234430&atid=1093080
if (hasDirectChild(*this,
[](const FileSystemObject& fsObj) -> bool { const SyncOperation op = fsObj.getSyncOperation(); return op == SO_CREATE_NEW_LEFT || op == SO_MOVE_LEFT_TARGET; }))
syncOpBuffered = SO_CREATE_NEW_LEFT;
//2. cancel parent deletion if only a single child is not also scheduled for deletion
else if (syncOpBuffered == SO_DELETE_RIGHT &&
hasDirectChild(*this,
[](const FileSystemObject& fsObj) -> bool
{
if (fsObj.isEmpty()) return false; //fsObj may already be empty because it once contained a "move source"
const SyncOperation op = fsObj.getSyncOperation(); return op != SO_DELETE_RIGHT && op != SO_MOVE_RIGHT_SOURCE;
}))
syncOpBuffered = SO_DO_NOTHING;
}
else if (isEmpty<RIGHT_SIDE>())
{
if (hasDirectChild(*this,
[](const FileSystemObject& fsObj) -> bool { const SyncOperation op = fsObj.getSyncOperation(); return op == SO_CREATE_NEW_RIGHT || op == SO_MOVE_RIGHT_TARGET; }))
syncOpBuffered = SO_CREATE_NEW_RIGHT;
else if (syncOpBuffered == SO_DELETE_LEFT &&
hasDirectChild(*this,
[](const FileSystemObject& fsObj) -> bool
{
if (fsObj.isEmpty()) return false;
const SyncOperation op = fsObj.getSyncOperation();
return op != SO_DELETE_LEFT && op != SO_MOVE_LEFT_SOURCE;
}))
syncOpBuffered = SO_DO_NOTHING;
}
}
break;
}
}
return syncOpBuffered;
}
SyncOperation FileMapping::testSyncOperation(SyncDirection testSyncDir, bool active) const
{
SyncOperation op = FileSystemObject::testSyncOperation(testSyncDir, active);
/*
check whether we can optimize "create + delete" via "move":
note: as long as we consider "create + delete" cases only, detection of renamed files, should be fine even for "binary" comparison variant!
*/
if (const FileSystemObject* refFile = dynamic_cast<const FileMapping*>(FileSystemObject::retrieve(moveFileRef))) //we expect a "FileMapping", but only need a "FileSystemObject"
{
SyncOperation opRef = refFile->FileSystemObject::getSyncOperation(); //do *not* make a virtual call!
if (op == SO_CREATE_NEW_LEFT &&
opRef == SO_DELETE_LEFT)
op = SO_MOVE_LEFT_TARGET;
else if (op == SO_DELETE_LEFT &&
opRef == SO_CREATE_NEW_LEFT)
op = SO_MOVE_LEFT_SOURCE;
else if (op == SO_CREATE_NEW_RIGHT &&
opRef == SO_DELETE_RIGHT)
op = SO_MOVE_RIGHT_TARGET;
else if (op == SO_DELETE_RIGHT &&
opRef == SO_CREATE_NEW_RIGHT)
op = SO_MOVE_RIGHT_SOURCE;
}
return op;
}
SyncOperation FileMapping::getSyncOperation() const
{
return FileMapping::testSyncOperation(getSyncDir(), isActive());
}
std::wstring zen::getCategoryDescription(CompareFilesResult cmpRes)
{
switch (cmpRes)
{
case FILE_LEFT_SIDE_ONLY:
return _("File/folder exists on left side only");
case FILE_RIGHT_SIDE_ONLY:
return _("File/folder exists on right side only");
case FILE_LEFT_NEWER:
return _("Left file is newer");
case FILE_RIGHT_NEWER:
return _("Right file is newer");
case FILE_DIFFERENT:
return _("Files have different content");
case FILE_EQUAL:
return _("Both sides are equal");
case FILE_DIFFERENT_METADATA:
return _("Files/folders differ in attributes only");
case FILE_CONFLICT:
return _("Conflict/file cannot be categorized");
}
assert(false);
return std::wstring();
}
std::wstring zen::getCategoryDescription(const FileSystemObject& fsObj)
{
const CompareFilesResult cmpRes = fsObj.getCategory();
if (cmpRes == FILE_CONFLICT)
return fsObj.getCatConflict();
return getCategoryDescription(cmpRes);
}
std::wstring zen::getSyncOpDescription(SyncOperation op)
{
switch (op)
{
case SO_CREATE_NEW_LEFT:
return _("Copy new file/folder to left");
case SO_CREATE_NEW_RIGHT:
return _("Copy new file/folder to right");
case SO_DELETE_LEFT:
return _("Delete left file/folder");
case SO_DELETE_RIGHT:
return _("Delete right file/folder");
case SO_MOVE_LEFT_SOURCE:
case SO_MOVE_LEFT_TARGET:
return _("Move file on left");
case SO_MOVE_RIGHT_SOURCE:
case SO_MOVE_RIGHT_TARGET:
return _("Move file on right");
case SO_OVERWRITE_LEFT:
return _("Overwrite left file/folder with right one");
case SO_OVERWRITE_RIGHT:
return _("Overwrite right file/folder with left one");
case SO_DO_NOTHING:
return _("Do nothing");
case SO_EQUAL:
return _("Both sides are equal");
case SO_COPY_METADATA_TO_LEFT:
return _("Copy file attributes only to left");
case SO_COPY_METADATA_TO_RIGHT:
return _("Copy file attributes only to right");
case SO_UNRESOLVED_CONFLICT: //not used on GUI, but in .csv
_("Conflict/file cannot be categorized");
}
assert(false);
return std::wstring();
}
std::wstring zen::getSyncOpDescription(const FileSystemObject& fsObj)
{
const SyncOperation op = fsObj.getSyncOperation();
switch (op)
{
case SO_CREATE_NEW_LEFT:
case SO_CREATE_NEW_RIGHT:
case SO_DELETE_LEFT:
case SO_DELETE_RIGHT:
case SO_OVERWRITE_LEFT:
case SO_OVERWRITE_RIGHT:
case SO_DO_NOTHING:
case SO_EQUAL:
case SO_COPY_METADATA_TO_LEFT:
case SO_COPY_METADATA_TO_RIGHT:
return getSyncOpDescription(op); //use generic description
case SO_MOVE_LEFT_SOURCE:
case SO_MOVE_LEFT_TARGET:
case SO_MOVE_RIGHT_SOURCE:
case SO_MOVE_RIGHT_TARGET:
if (const FileMapping* sourceFile = dynamic_cast<const FileMapping*>(&fsObj))
if (const FileMapping* targetFile = dynamic_cast<const FileMapping*>(FileSystemObject::retrieve(sourceFile->getMoveRef())))
{
const bool onLeft = op == SO_MOVE_LEFT_SOURCE || op == SO_MOVE_LEFT_TARGET;
const bool isSource = op == SO_MOVE_LEFT_SOURCE || op == SO_MOVE_RIGHT_SOURCE;
if (!isSource)
std::swap(sourceFile, targetFile);
auto getRelName = [&](const FileSystemObject& fso, bool leftSide) { return leftSide ? fso.getRelativeName<LEFT_SIDE>() : fso.getRelativeName<RIGHT_SIDE>(); };
const Zstring relSource = getRelName(*sourceFile, onLeft);
const Zstring relTarget = getRelName(*targetFile, !onLeft);
return getSyncOpDescription(op) + L"\n" +
(EqualFilename()(beforeLast(relSource, FILE_NAME_SEPARATOR), beforeLast(relTarget, FILE_NAME_SEPARATOR)) ? //returns empty string if ch not found
//detected pure "rename"
L"\"" + utf8CvrtTo<std::wstring>(afterLast(relSource, FILE_NAME_SEPARATOR)) + L"\"" + L" ->\n" + //show short name only
L"\"" + utf8CvrtTo<std::wstring>(afterLast(relTarget, FILE_NAME_SEPARATOR)) + L"\"" :
//"move" or "move + rename"
L"\"" + utf8CvrtTo<std::wstring>(relSource) + L"\"" + L" ->\n" +
L"\"" + utf8CvrtTo<std::wstring>(relTarget) + L"\"");
//attention: ::SetWindowText() doesn't handle tab characters correctly in combination with certain file names, so don't use them
}
break;
case SO_UNRESOLVED_CONFLICT:
return fsObj.getSyncOpConflict();
}
assert(false);
return std::wstring();
}
|