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 | 1×
1×
1×
1×
1×
1×
338×
65×
273×
273×
273×
273×
1×
273×
273×
606×
273×
273×
273×
606×
509×
273×
1×
606×
606×
606×
606×
606×
1×
169×
169×
169×
169×
169×
169×
169×
356×
356×
356×
356×
356×
356×
356×
356×
356×
356×
356×
287×
287×
287×
69×
31×
31×
31×
38×
38×
38×
356×
153×
153×
153×
73×
73×
73×
13×
60×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
56×
80×
153×
149×
4×
153×
153×
153×
147×
78×
78×
78×
78×
78×
69×
69×
203×
102×
102×
102×
102×
70×
32×
102×
102×
28×
28×
28×
28×
28×
101×
101×
101×
101×
101×
101×
58×
43×
101×
101×
37×
37×
37×
37×
37×
169×
169×
169×
4×
4×
4×
1×
153×
153×
153×
153×
73×
80×
1× | var fs = require('fs');
var common = require('./common');
var pathUtils = require('path');
var Promise = require('bluebird');
var wrapper = {
stat : Promise.promisify(fs.stat),
lstat : Promise.promisify(fs.lstat),
readdir : Promise.promisify(fs.readdir),
};
/**
* Returns the sorted list of entries in a directory.
*/
var getEntries = function (path, options) {
if (!path) {
return Promise.resolve([]);
} else{
return wrapper.stat(path).then(
function(stat){
Eif(stat.isDirectory()){
return wrapper.readdir(path).then(
function(rawEntries){
return buildEntries(path, rawEntries, options);
});
} else{
var name = pathUtils.basename(path);
return [
{
name : name,
path : path,
stat : stat
}
];
}
});
}
}
var buildEntries = function(path, rawEntries, options){
var promisedEntries = [];
rawEntries.forEach(function (entryName) {
promisedEntries.push(buildEntry(path, entryName));
});
return Promise.all(promisedEntries).then(
function(entries){
var result = [];
entries.forEach(function(entry){
if (common.filterEntry(entry, options)){
result.push(entry);
}
});
return options.ignoreCase?result.sort(common.compareEntryIgnoreCase):result.sort(common.compareEntryCaseSensitive);
});
}
var buildEntry = function(path, entryName){
var entryPath = path + '/' + entryName;
return Promise.all([wrapper.stat(entryPath), wrapper.lstat(entryPath)])
.then(
function(result){
var stat = result[0];
var lstat = result[1];
return {
name : entryName,
path : entryPath,
stat : stat,
symlink : lstat.isSymbolicLink(),
toString : function () {
return this.name;
}
};
});
}
/**
* Compares two directories asynchronously.
*/
var compare = function (path1, path2, level, relativePath, options, statistics, diffSet) {
return Promise.all([getEntries(path1, options), getEntries(path2, options)]).then(
function(entriesResult){
var entries1 = entriesResult[0];
var entries2 = entriesResult[1];
var i1 = 0, i2 = 0;
var comparePromises = [];
var compareFilePromises = [];
while (i1 < entries1.length || i2 < entries2.length) {
var entry1 = entries1[i1];
var entry2 = entries2[i2];
var n1 = entry1 ? entry1.name : undefined;
var n2 = entry2 ? entry2.name : undefined;
var p1 = entry1 ? entry1.path : undefined;
var p2 = entry2 ? entry2.path : undefined;
var fileStat1 = entry1 ? entry1.stat : undefined;
var fileStat2 = entry2 ? entry2.stat : undefined;
var type1, type2;
// compare entry name (-1, 0, 1)
var cmp;
if (i1 < entries1.length && i2 < entries2.length) {
cmp = options.ignoreCase?common.compareEntryIgnoreCase(entry1, entry2):common.compareEntryCaseSensitive(entry1, entry2);
type1 = common.getType(fileStat1);
type2 = common.getType(fileStat2);
} else if (i1 < entries1.length) {
type1 = common.getType(fileStat1);
type2 = common.getType(undefined);
cmp = -1;
} else {
type1 = common.getType(undefined);
type2 = common.getType(fileStat2);
cmp = 1;
}
// process entry
if (cmp === 0) {
// Both left/right exist and have the same name
Eif (type1 === type2) {
var samePromise = undefined, same = undefined;
if(type1==='file'){
var compareSize = options.compareSize === undefined ? false : options.compareSize;
var compareContent = options.compareContent === undefined ? false : options.compareContent;
if (compareSize && fileStat1.size !== fileStat2.size) {
same = false;
} else if(compareContent){
var cmpFile = function(entry1, entry2, type1, type2){
var subDiffSet;
Eif(!options.noDiffSet){
subDiffSet = [];
diffSet.push(subDiffSet);
}
samePromise = options.compareFileAsync(p1, fileStat1, p2, fileStat2, options).then(function(comparisonResult){
var same, error;
Eif(typeof(comparisonResult) === "boolean"){
same = comparisonResult;
} else{
error = comparisonResult;
}
return {entry1: entry1, entry2: entry2, same: same, error: error, type1: type1, type2: type2, diffSet: subDiffSet};
});
}
cmpFile(entry1, entry2, type1, type2);
} else{
same = true;
}
} else{
same = true;
}
if(same !== undefined){
doStats(entry1, entry2, same, statistics, options, level, relativePath, diffSet, type1, type2);
} else{
compareFilePromises.push(samePromise);
}
} else {
// File and directory with same name
if(!options.noDiffSet){
options.resultBuilder(entry1, entry2, 'distinct', level, relativePath, options, statistics, diffSet);
}
statistics.distinct+=2;
statistics.distinctFiles++;
statistics.distinctDirs++;
}
i1++;
i2++;
if(!options.skipSubdirs){
if (type1 === 'directory' && type2 === 'directory') {
var subDiffSet;
Eif(!options.noDiffSet){
subDiffSet = [];
diffSet.push(subDiffSet);
}
comparePromises.push(compare(p1, p2, level + 1,
relativePath + '/' + entry1.name,
options, statistics, subDiffSet));
} else Iif (type1 === 'directory') {
var subDiffSet;
if(!options.noDiffSet){
subDiffSet = [];
diffSet.push(subDiffSet);
}
comparePromises.push(compare(p1, undefined,
level + 1, relativePath + '/'
+ entry1.name, options, statistics, subDiffSet));
} else Iif (type2 === 'directory') {
var subDiffSet;
if(!options.noDiffSet){
subDiffSet = [];
diffSet.push(subDiffSet);
}
comparePromises.push(compare(undefined, p2,
level + 1, relativePath + '/'
+ entry2.name, options, statistics, subDiffSet));
}
}
} else if (cmp < 0) {
// Right missing
Eif(!options.noDiffSet){
options.resultBuilder(entry1, undefined, 'left', level, relativePath, options, statistics, diffSet);
}
statistics.left++;
if(type1==='file'){
statistics.leftFiles++;
} else{
statistics.leftDirs++;
}
i1++;
if (type1 === 'directory' && !options.skipSubdirs) {
var subDiffSet;
Eif(!options.noDiffSet){
subDiffSet = [];
diffSet.push(subDiffSet);
}
comparePromises.push(compare(p1, undefined,
level + 1,
relativePath + '/' + entry1.name, options, statistics, subDiffSet));
}
} else {
// Left missing
Eif(!options.noDiffSet){
var subDiffSet = [];
diffSet.push(subDiffSet);
options.resultBuilder(undefined, entry2, 'right', level, relativePath, options, statistics, subDiffSet);
}
statistics.right++;
if(type2==='file'){
statistics.rightFiles++;
} else{
statistics.rightDirs++;
}
i2++;
if (type2 === 'directory' && !options.skipSubdirs) {
var subDiffSet;
Eif(!options.noDiffSet){
subDiffSet = [];
diffSet.push(subDiffSet);
}
comparePromises.push(compare(undefined, p2,
level + 1,
relativePath + '/' + entry2.name, options, statistics, subDiffSet));
}
}
}
return Promise.all(comparePromises).then(function(){
return Promise.all(compareFilePromises).then(function(sameResults){
for(var i = 0; i<sameResults.length; i++){
var sameResult = sameResults[i];
Iif(sameResult.error){
return Promise.reject(sameResult.error);
} else{
doStats(sameResult.entry1, sameResult.entry2, sameResult.same, statistics, options, level, relativePath, sameResult.diffSet, sameResult.type1, sameResult.type2);
}
}
});
});
});
};
var doStats = function(entry1, entry2, same, statistics, options, level, relativePath, diffSet, type1, type2){
Eif(!options.noDiffSet){
options.resultBuilder(entry1, entry2, same ? 'equal' : 'distinct', level, relativePath, options, statistics, diffSet)
}
same ? statistics.equal++ : statistics.distinct++;
if(type1==='file'){
same ? statistics.equalFiles++ : statistics.distinctFiles++;
} else{
same ? statistics.equalDirs++ : statistics.distinctDirs++;
}
}
module.exports = compare; |