Skip to content

Commit

Permalink
Improved check for 2 files whether they are different, now also using…
Browse files Browse the repository at this point in the history
… the size of the files.
  • Loading branch information
Amit Apple committed Dec 5, 2012
1 parent 2800e41 commit 9c9213b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 25 deletions.
25 changes: 19 additions & 6 deletions bin/kuduSync.js
Expand Up @@ -84,16 +84,29 @@ var __extends = this.__extends || function (d, b) {
}
var FileInfo = (function (_super) {
__extends(FileInfo, _super);
function FileInfo(path, modifiedTime) {
function FileInfo(path, fileStat) {
_super.call(this, path);
Ensure.argNotNull(modifiedTime, "modifiedTime");
this._modifiedTime = modifiedTime;
Ensure.argNotNull(fileStat, "fileStat");
this._fileStat = fileStat;
}
FileInfo.prototype.modifiedTime = function () {
return this._modifiedTime;
return this._fileStat.mtime;
};
FileInfo.prototype.size = function () {
return this._fileStat.size;
};
return FileInfo;
})(FileInfoBase);
function fileEquals(file1, file2) {
if(file1 == null) {
return file2 == null;
}
if(file1.modifiedTime() == null) {
return file2.modifiedTime() == null;
}
return;
file2 != null && file2.modifiedTime() != null && file1.modifiedTime().getTime() === file2.modifiedTime().getTime() && file1.size === file2.size;
}
var DirectoryInfo = (function (_super) {
__extends(DirectoryInfo, _super);
function DirectoryInfo(path) {
Expand Down Expand Up @@ -136,7 +149,7 @@ var DirectoryInfo = (function (_super) {
subDirectoriesMapping[fileName.toUpperCase()] = directoryInfo;
subDirectoriesList.push(directoryInfo);
} else {
var fileInfo = new FileInfo(path, stat.mtime);
var fileInfo = new FileInfo(path, stat);
filesMapping[fileName.toUpperCase()] = fileInfo;
filesList.push(fileInfo);
}
Expand Down Expand Up @@ -376,7 +389,7 @@ function kuduSyncDirectory(from, to, fromRootPath, toRootPath, manifest, outMani
}
outManifest.addFileToManifest(fromFile.path(), fromRootPath);
var toFile = to.getFile(fromFile.name());
if(toFile == null || fromFile.modifiedTime().getTime() !== toFile.modifiedTime().getTime()) {
if(toFile == null || !fileEquals(fromFile, toFile)) {
return copyFile(fromFile, pathUtil.join(to.path(), fromFile.name()), whatIf);
}
return Q.resolve();
Expand Down
2 changes: 1 addition & 1 deletion lib/DirectoryInfo.ts
Expand Up @@ -54,7 +54,7 @@ class DirectoryInfo extends FileInfoBase {
}
else {
// Store both as mapping as an array
var fileInfo = new FileInfo(path, stat.mtime);
var fileInfo = new FileInfo(path, stat);
filesMapping[fileName.toUpperCase()] = fileInfo;
filesList.push(fileInfo);
}
Expand Down
30 changes: 25 additions & 5 deletions lib/FileInfo.ts
@@ -1,17 +1,37 @@
///<reference path='fileInfoBase.ts'/>

class FileInfo extends FileInfoBase {
private _modifiedTime: Date;
private _fileStat: FileStat;

constructor (path: string, modifiedTime: Date) {
constructor (path: string, fileStat: FileStat) {
super(path);

Ensure.argNotNull(modifiedTime, "modifiedTime");
Ensure.argNotNull(fileStat, "fileStat");

this._modifiedTime = modifiedTime;
this._fileStat = fileStat;
}

modifiedTime() {
return this._modifiedTime;
return this._fileStat.mtime;
}

size() {
return this._fileStat.size;
}
}

function fileEquals(file1: FileInfo, file2: FileInfo) {
if (file1 == null) {
return file2 == null;
}

if (file1.modifiedTime() == null) {
return file2.modifiedTime() == null;
}

return
file2 != null &&
file2.modifiedTime() != null &&
file1.modifiedTime().getTime() === file2.modifiedTime().getTime() &&
file1.size === file2.size;
}
2 changes: 1 addition & 1 deletion lib/FileUtils.ts
Expand Up @@ -203,7 +203,7 @@ function kuduSyncDirectory(from: DirectoryInfo, to: DirectoryInfo, fromRootPath:
// last write time is different than the same file in the source (only if it changed)
var toFile = to.getFile(fromFile.name());

if (toFile == null || fromFile.modifiedTime().getTime() !== toFile.modifiedTime().getTime()) {
if (toFile == null || !fileEquals(fromFile, toFile)) {
return copyFile(fromFile, pathUtil.join(to.path(), fromFile.name()), whatIf);
}

Expand Down
15 changes: 3 additions & 12 deletions test/functionalTests.js
Expand Up @@ -40,20 +40,14 @@ suite('Kudu Sync Functional Tests', function () {
test('Single file updated should be sync\'d', function (done) {
runKuduSyncTestScenario(["file1.bin"], ["file1.bin"], null, function () {

// Waiting 1 second for updated file to have a newer modified time
setTimeout(function () {
runKuduSyncTestScenario(["file1.bin"], ["file1.bin"], null, done);
}, 1000);
runKuduSyncTestScenario(["file1.bin"], ["file1.bin"], null, done);
});
});

test('Several files updated should be sync\'d', function (done) {
runKuduSyncTestScenario(["file1.bin", "file2", "dir1/file3", "dir1/dir2/dir3/file4"], ["file1.bin", "file2", "dir1/file3", "dir1/dir2/dir3/file4"], null, function () {

// Waiting 1 second for updated file to have a newer modified time
setTimeout(function () {
runKuduSyncTestScenario(["file2", "dir1/file3", "dir1/dir2/dir3/file5", "dir2/file6.txt"], ["file1.bin", "file2", "dir1/file3", "dir1/dir2/dir3/file4", "dir1/dir2/dir3/file5", "dir2/file6.txt"], null, done);
}, 1000);
runKuduSyncTestScenario(["file2", "dir1/file3", "dir1/dir2/dir3/file5", "dir2/file6.txt"], ["file1.bin", "file2", "dir1/file3", "dir1/dir2/dir3/file4", "dir1/dir2/dir3/file5", "dir2/file6.txt"], null, done);
});
});

Expand All @@ -66,10 +60,7 @@ suite('Kudu Sync Functional Tests', function () {
test('Several files some created some removed should be sync\'d', function (done) {
runKuduSyncTestScenario(["file1.bin", "file2", "dir1/file3", "dir1/dir2/dir3/file4"], ["file1.bin", "file2", "dir1/file3", "dir1/dir2/dir3/file4"], null, function () {

// Waiting 1 second for updated file to have a newer modified time
setTimeout(function () {
runKuduSyncTestScenario(["file2", "-dir1/file3", "-dir1/dir2/dir3/file4"], ["file1.bin", "file2", "-dir1/file3", "-dir1/dir2/dir3/file4"], null, done);
}, 1000);
runKuduSyncTestScenario(["file2", "-dir1/file3", "-dir1/dir2/dir3/file4"], ["file1.bin", "file2", "-dir1/file3", "-dir1/dir2/dir3/file4"], null, done);
});
});

Expand Down
14 changes: 14 additions & 0 deletions typings/node.d.ts
Expand Up @@ -75,6 +75,20 @@ declare var Buffer: {
* *
************************************************/

interface FileStat {
dev: number;
mode: number;
nlink: number;
uid: number;
gid: number;
rdev: number;
ino: number;
size: number;
atime: Date;
mtime: Date;
ctime: Date;
}

interface EventEmitter {
addListener(event: string, listener: Function);
on(event: string, listener: Function);
Expand Down

0 comments on commit 9c9213b

Please sign in to comment.