From 702dd1a3a303ac82c5717ff5e847130298dab5d5 Mon Sep 17 00:00:00 2001 From: Abir Viqar Date: Sat, 23 Nov 2013 13:35:23 -0500 Subject: [PATCH] open should follow symbolic links This functionality is tested implicitly through tests in writefile, readfile --- src/fs.js | 36 +++++++++++++++++++++++++++++++++++- tests/spec/idbfs.spec.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/fs.js b/src/fs.js index 5d0e905..3c94ede 100644 --- a/src/fs.js +++ b/src/fs.js @@ -414,6 +414,8 @@ define(function(require) { var fileNode; var fileData; + var followedCount = 0; + if(ROOT_DIRECTORY_NAME == name) { if(_(flags).contains(O_WRITE)) { callback(new EIsDirectory('the named file is a directory and O_WRITE is set')); @@ -446,7 +448,7 @@ define(function(require) { if(directoryEntry.type == MODE_DIRECTORY && _(flags).contains(O_WRITE)) { callback(new EIsDirectory('the named file is a directory and O_WRITE is set')); } else { - read_object(objectStore, directoryEntry.id, set_file_node); + read_object(objectStore, directoryEntry.id, check_if_symbolic_link); } } } else { @@ -459,6 +461,38 @@ define(function(require) { } } + function check_if_symbolic_link(error, result) { + if(error) { + callback(error); + } else { + var node = result; + if(node.mode == MODE_SYMBOLIC_LINK) { + followedCount++; + if(followedCount > SYMLOOP_MAX){ + callback(new ELoop('too many symbolic links were encountered')); + } else { + follow_symbolic_link(node.data); + } + } else { + set_file_node(undefined, node); + } + } + } + + function follow_symbolic_link(data) { + data = normalize(data); + parentPath = dirname(data); + name = basename(data); + if(ROOT_DIRECTORY_NAME == name) { + if(_(flags).contains(O_WRITE)) { + callback(new EIsDirectory('the named file is a directory and O_WRITE is set')); + } else { + find_node(objectStore, path, set_file_node); + } + } + find_node(objectStore, parentPath, read_directory_data); + } + function set_file_node(error, result) { if(error) { callback(error); diff --git a/tests/spec/idbfs.spec.js b/tests/spec/idbfs.spec.js index 64142c6..9163700 100644 --- a/tests/spec/idbfs.spec.js +++ b/tests/spec/idbfs.spec.js @@ -1063,6 +1063,36 @@ describe('fs.writeFile, fs.readFile', function() { }); }); + it('should follow symbolic links', function () { + var complete = false; + var _result; + var that = this; + + var contents = "This is a file."; + + that.fs.writeFile('/myfile', '', { encoding: 'utf8' }, function(error) { + if(error) throw error; + that.fs.symlink('/myfile', '/myFileLink', function (error) { + if (error) throw error; + that.fs.writeFile('/myFileLink', contents, 'utf8', function (error) { + if (error) throw error; + that.fs.readFile('/myFileLink', 'utf8', function(error, data) { + if(error) throw error; + _result = data; + complete = true; + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_result).toEqual(contents); + }); + }); }); describe('fs.read', function() {