Link tests.

This commit is contained in:
Alan Kligman 2013-07-25 22:49:53 -04:00
parent ea8fbd48ad
commit 717bbc25c1
4 changed files with 310 additions and 6 deletions

134
dist/idbfs.js vendored
View File

@ -6363,7 +6363,6 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
function stat_file(objectStore, path, callback) { function stat_file(objectStore, path, callback) {
path = normalize(path); path = normalize(path);
var name = basename(path); var name = basename(path);
var parentPath = dirname(path);
find_node(objectStore, path, check_file); find_node(objectStore, path, check_file);
@ -6376,6 +6375,97 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
}; };
}; };
function link_node(objectStore, oldpath, newpath, callback) {
oldpath = normalize(oldpath);
var oldname = basename(oldpath);
var oldParentPath = dirname(oldpath);
newpath = normalize(newpath);
var newname = basename(newpath);
var newParentPath = dirname(newpath);
var oldDirectoryNode;
var oldDirectoryData;
var newDirectoryNode;
var newDirectoryData;
var directoryEntry;
var fileNode;
find_node(objectStore, oldParentPath, read_old_directory_data);
function read_old_directory_data(error, result) {
if(error) {
callback(error);
} else {
oldDirectoryNode = result;
read_object(objectStore, oldDirectoryNode.data, check_if_old_file_exists);
}
};
function check_if_old_file_exists(error, result) {
if(error) {
callback(error);
} else {
oldDirectoryData = result;
if(!_(oldDirectoryData).has(oldname)) {
callback(new ENoEntry('a component of either path prefix does not exist'));
} else {
find_node(objectStore, newParentPath, read_new_directory_data);
}
}
};
function read_new_directory_data(error, result) {
if(error) {
callback(error);
} else {
newDirectoryNode = result;
read_object(objectStore, newDirectoryNode.data, check_if_new_file_exists);
}
};
function check_if_new_file_exists(error, result) {
if(error) {
callback(error);
} else {
newDirectoryData = result;
if(_(newDirectoryData).has(newname)) {
callback(new EExists('newpath resolves to an existing file'));
} else {
newDirectoryData[newname] = oldDirectoryData[oldname];
write_object(objectStore, newDirectoryData, newDirectoryNode.data, read_directory_entry);
}
}
};
function read_directory_entry(error, result) {
if(error) {
callback(error);
} else {
read_object(objectStore, newDirectoryData[newname].id, read_file_node);
}
}
function read_file_node(error, result) {
if(error) {
callback(error);
} else {
directoryEntry = result;
read_object(objectStore, directoryEntry.id, update_file_node);
}
};
function update_file_node(error, result) {
if(error) {
callback(error);
} else {
fileNode = result;
fileNode.nlinks += 1
write_object(objectStore, fileNode, directoryEntry.id, callback);
}
};
};
/* /*
* FileSystem * FileSystem
*/ */
@ -6599,6 +6689,7 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
deferred.reject(error); deferred.reject(error);
} else { } else {
var stats = { var stats = {
node: result.id,
dev: that.name, dev: that.name,
size: result.size, size: result.size,
nlinks: result.nlinks, nlinks: result.nlinks,
@ -6636,11 +6727,20 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW); var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME); var files = transaction.objectStore(FILE_STORE_NAME);
function check_result(error) {
if(error) {
// if(transaction.error) transaction.abort();
deferred.reject(error);
} else {
deferred.resolve();
}
};
link_node(files, oldpath, newpath, check_result);
deferred.promise.then( deferred.promise.then(
function(result) { function(result) {
callback(undefined, result); callback();
}, },
function(error) { function(error) {
callback(error); callback(error);
@ -6653,7 +6753,37 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
); );
}; };
FileSystem.prototype.unlink = function unlink(path, callback) { FileSystem.prototype.unlink = function unlink(path, callback) {
var that = this;
this.promise.then(
function() {
var deferred = when.defer();
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
function check_result(error) {
if(error) {
// if(transaction.error) transaction.abort();
deferred.reject(error);
} else {
deferred.resolve();
}
};
unlink_node(files, path, check_result);
deferred.promise.then(
function(result) {
callback();
},
function(error) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}; };
FileSystem.prototype.getxattr = function getxattr(path, name, callback) { FileSystem.prototype.getxattr = function getxattr(path, name, callback) {

View File

@ -34,9 +34,9 @@ fs.open('/myfile', 'w+', function(error, fd) {
fs.close(fd, function(error) { fs.close(fd, function(error) {
if(error) throw error; if(error) throw error;
console.log('closed'); console.log('closed');
fs.write(fd, buffer, 0, buffer.length, undefined, function(error, nbytes) { fs.link('/myfile', '/myotherfile', function(error) {
if(error) throw error; if(error) throw error;
console.log('write:', nbytes); console.log('linked');
fs.stat('/myfile', function(error, stats) { fs.stat('/myfile', function(error, stats) {
if(error) throw error; if(error) throw error;
console.log(stats); console.log(stats);

View File

@ -563,7 +563,6 @@ define(function(require) {
function stat_file(objectStore, path, callback) { function stat_file(objectStore, path, callback) {
path = normalize(path); path = normalize(path);
var name = basename(path); var name = basename(path);
var parentPath = dirname(path);
find_node(objectStore, path, check_file); find_node(objectStore, path, check_file);
@ -576,6 +575,97 @@ define(function(require) {
}; };
}; };
function link_node(objectStore, oldpath, newpath, callback) {
oldpath = normalize(oldpath);
var oldname = basename(oldpath);
var oldParentPath = dirname(oldpath);
newpath = normalize(newpath);
var newname = basename(newpath);
var newParentPath = dirname(newpath);
var oldDirectoryNode;
var oldDirectoryData;
var newDirectoryNode;
var newDirectoryData;
var directoryEntry;
var fileNode;
find_node(objectStore, oldParentPath, read_old_directory_data);
function read_old_directory_data(error, result) {
if(error) {
callback(error);
} else {
oldDirectoryNode = result;
read_object(objectStore, oldDirectoryNode.data, check_if_old_file_exists);
}
};
function check_if_old_file_exists(error, result) {
if(error) {
callback(error);
} else {
oldDirectoryData = result;
if(!_(oldDirectoryData).has(oldname)) {
callback(new ENoEntry('a component of either path prefix does not exist'));
} else {
find_node(objectStore, newParentPath, read_new_directory_data);
}
}
};
function read_new_directory_data(error, result) {
if(error) {
callback(error);
} else {
newDirectoryNode = result;
read_object(objectStore, newDirectoryNode.data, check_if_new_file_exists);
}
};
function check_if_new_file_exists(error, result) {
if(error) {
callback(error);
} else {
newDirectoryData = result;
if(_(newDirectoryData).has(newname)) {
callback(new EExists('newpath resolves to an existing file'));
} else {
newDirectoryData[newname] = oldDirectoryData[oldname];
write_object(objectStore, newDirectoryData, newDirectoryNode.data, read_directory_entry);
}
}
};
function read_directory_entry(error, result) {
if(error) {
callback(error);
} else {
read_object(objectStore, newDirectoryData[newname].id, read_file_node);
}
}
function read_file_node(error, result) {
if(error) {
callback(error);
} else {
directoryEntry = result;
read_object(objectStore, directoryEntry.id, update_file_node);
}
};
function update_file_node(error, result) {
if(error) {
callback(error);
} else {
fileNode = result;
fileNode.nlinks += 1
write_object(objectStore, fileNode, directoryEntry.id, callback);
}
};
};
/* /*
* FileSystem * FileSystem
*/ */
@ -799,6 +889,7 @@ define(function(require) {
deferred.reject(error); deferred.reject(error);
} else { } else {
var stats = { var stats = {
node: result.id,
dev: that.name, dev: that.name,
size: result.size, size: result.size,
nlinks: result.nlinks, nlinks: result.nlinks,
@ -836,11 +927,20 @@ define(function(require) {
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW); var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME); var files = transaction.objectStore(FILE_STORE_NAME);
function check_result(error) {
if(error) {
// if(transaction.error) transaction.abort();
deferred.reject(error);
} else {
deferred.resolve();
}
};
link_node(files, oldpath, newpath, check_result);
deferred.promise.then( deferred.promise.then(
function(result) { function(result) {
callback(undefined, result); callback();
}, },
function(error) { function(error) {
callback(error); callback(error);
@ -853,7 +953,37 @@ define(function(require) {
); );
}; };
FileSystem.prototype.unlink = function unlink(path, callback) { FileSystem.prototype.unlink = function unlink(path, callback) {
var that = this;
this.promise.then(
function() {
var deferred = when.defer();
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
function check_result(error) {
if(error) {
// if(transaction.error) transaction.abort();
deferred.reject(error);
} else {
deferred.resolve();
}
};
unlink_node(files, path, check_result);
deferred.promise.then(
function(result) {
callback();
},
function(error) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}; };
FileSystem.prototype.getxattr = function getxattr(path, name, callback) { FileSystem.prototype.getxattr = function getxattr(path, name, callback) {

View File

@ -138,6 +138,7 @@ describe('fs.stat', function() {
runs(function() { runs(function() {
expect(_result).toBeDefined(); expect(_result).toBeDefined();
expect(_error).not.toBeDefined(); expect(_error).not.toBeDefined();
expect(_result['node']).toBeDefined();
expect(_result['dev']).toEqual(that.db_name); expect(_result['dev']).toEqual(that.db_name);
expect(_result['size']).toBeDefined(); expect(_result['size']).toBeDefined();
expect(_result['nlinks']).toEqual(jasmine.any(Number)); expect(_result['nlinks']).toEqual(jasmine.any(Number));
@ -750,6 +751,49 @@ describe('fs.link', function() {
it('should be a function', function() { it('should be a function', function() {
expect(typeof this.fs.link).toEqual('function'); expect(typeof this.fs.link).toEqual('function');
}); });
it('should create a link to an existing file', function() {
var complete = false;
var _error, _oldstats, _newstats;
var that = this;
that.fs.open('/myfile', 'w+', function(error, result) {
if(error) throw error;
var fd = result;
that.fs.close(fd, function(error) {
if(error) throw error;
that.fs.link('/myfile', '/myotherfile', function(error) {
if(error) throw error;
that.fs.stat('/myfile', function(error, result) {
if(error) throw error;
_oldstats = result;
that.fs.stat('/myotherfile', function(error, result) {
if(error) throw error;
_newstats = result;
complete = true;
});
});
});
});
});
waitsFor(function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).not.toBeDefined();
expect(_newstats.node).toEqual(_oldstats.node);
expect(_newstats.nlinks).toEqual(2);
expect(_newstats).toEqual(_oldstats);
});
});
}); });
describe('fs.unlink', function() { describe('fs.unlink', function() {