-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFolderPair.cpp
154 lines (128 loc) · 4.34 KB
/
FolderPair.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
#include "FolderPair.h"
using namespace std;
FolderPair::FolderPair(const String* leftFolderRootPath, const String* rightFolderRootPath, const String relativePath, const Filter conditions, const enum CompareMode mode)
{
if (leftFolderRootPath->empty() || rightFolderRootPath->empty())
{
// At least one path is empty, invalid arguments
return;
}
else
{
LeftPath = leftFolderRootPath;
RightPath = rightFolderRootPath;
RelativePath = relativePath;
Conditions = conditions;
Mode = mode;
}
}
void FolderPair::Sync()
{
IOfunction::CreateDirectoryByPath(EMPTYSTRING, *RightPath + RelativePath);
DirectoryIndex leftFolder = DirectoryIndex((*LeftPath) + RelativePath);
DirectoryIndex rightFolder = DirectoryIndex((*RightPath) + RelativePath);
File currentLeftFile;
File currentRightFile;
bool LastFileInFolderLeft = leftFolder.FindNext(¤tLeftFile, Conditions) != 0;
bool LastFileInFolderRight = rightFolder.FindNext(¤tRightFile) != 0;
if (!LastFileInFolderLeft)
{
while (!LastFileInFolderRight)
{
if (currentLeftFile.IsDirectory != currentRightFile.IsDirectory)
{
// Only one file is directory
currentRightFile.Delete();
LastFileInFolderRight = rightFolder.FindNext(¤tRightFile) != 0;
}
else
{
switch (currentLeftFile.CompareName(currentRightFile))
{
case 0: // Files have the same name
if (currentLeftFile.IsDirectory)
{
// Both files are directories with the same name
FolderPair subfolderPair = FolderPair(LeftPath, RightPath, (RelativePath + BACKSLASH + currentLeftFile.Name), Conditions, Mode);
subfolderPair.Sync();
}
else
{
if (currentLeftFile.CompareLastModificationDate(currentRightFile) != 0)
{
// Files have a different last modification date
currentLeftFile.CopyToPath(&(rightFolder.GetPath()));
}
else
{
if (currentLeftFile.CompareSize(currentRightFile) != 0)
{
// Files have a different size
currentLeftFile.CopyToPath(&(rightFolder.GetPath()));
}
else
{
// Files are supposed identical at this point because they have the same name/date/size
if (Mode == CompareMode::Accurate)
{
if (currentLeftFile.CompareHash(currentRightFile) != 0)
{
// Files are different
currentLeftFile.CopyToPath(&(rightFolder.GetPath()));
}
else
{
// The files are identical
}
}
}
}
}
// At this point both files are the same
LastFileInFolderLeft = leftFolder.FindNext(¤tLeftFile, Conditions) != 0;
LastFileInFolderRight = rightFolder.FindNext(¤tRightFile) != 0;
break;
case -1: // Left file is before right file in the sorting order
// Left file has to be copied to the right
currentLeftFile.CopyToPath(&(rightFolder.GetPath()));
// At this point the current left file exists in both folders
LastFileInFolderLeft = leftFolder.FindNext(¤tLeftFile, Conditions) != 0;
break;
case 1: // Right file is before left file in the sorting order
// Right file does not exsists in the left folder and has to be deleted
currentRightFile.Delete();
LastFileInFolderRight = rightFolder.FindNext(¤tRightFile) != 0;
break;
}
if (LastFileInFolderLeft)
{
// We finishing going throught the left folder before the right folder
// Every file remaining in the right folder does not exists in the left
while (!LastFileInFolderRight)
{
currentRightFile.Delete();
LastFileInFolderRight = rightFolder.FindNext(¤tRightFile) != 0;
}
break; // Getting out of the while loop
}
}
}// End while
// At this point either we finished going through both folders of only the right one
while (!LastFileInFolderLeft)
{
// The remaining files from the left folder are not present in the right so we copy them
currentLeftFile.CopyToPath(&(rightFolder.GetPath()));
LastFileInFolderLeft = leftFolder.FindNext(¤tLeftFile, Conditions) != 0;
}
// Both folder are identical (I hope) ! Yay !
}
else
{
// Left folder is empty we delete what is left in the right one
while (!LastFileInFolderRight)
{
currentRightFile.Delete();
LastFileInFolderRight = rightFolder.FindNext(¤tRightFile) != 0;
}
}
}