1
0
Fork 0

added test cases for "fs" module

This commit is contained in:
Jan Steemann 2013-03-26 14:09:20 +01:00
parent d74de62bc0
commit 1492118545
9 changed files with 674 additions and 67 deletions

View File

@ -211,16 +211,17 @@ endif
SHELL_COMMON = @top_srcdir@/js/common/tests/shell-require.js \
@top_srcdir@/js/common/tests/shell-attributes.js \
@top_srcdir@/js/common/tests/shell-document.js \
@top_srcdir@/js/common/tests/shell-edge.js \
@top_srcdir@/js/common/tests/shell-database.js \
@top_srcdir@/js/common/tests/shell-collection.js \
@top_srcdir@/js/common/tests/shell-collection-volatile.js \
@top_srcdir@/js/common/tests/shell-compactor.js \
@top_srcdir@/js/common/tests/shell-crypto.js \
@top_srcdir@/js/common/tests/shell-database.js \
@top_srcdir@/js/common/tests/shell-document.js \
@top_srcdir@/js/common/tests/shell-edge.js \
@top_srcdir@/js/common/tests/shell-fs.js \
@top_srcdir@/js/common/tests/shell-keygen.js \
@top_srcdir@/js/common/tests/shell-simple-query.js \
@top_srcdir@/js/common/tests/shell-statement.js \
@top_srcdir@/js/common/tests/shell-crypto.js \
@top_srcdir@/js/common/tests/shell-users.js \
@top_srcdir@/js/common/tests/shell-index.js \
@top_srcdir@/js/common/tests/shell-index-geo.js \

View File

@ -1,5 +1,6 @@
/*jslint indent: 2, nomen: true, maxlen: 120, sloppy: true, vars: true, white: true, plusplus: true, nonpropdel: true, proto: true */
/*global require, module, Module, FS_CREATE_DIRECTORY, FS_MOVE, FS_REMOVE, FS_EXISTS, FS_IS_DIRECTORY, FS_IS_FILE,
/*global require, module, Module, FS_MAKE_DIRECTORY, FS_MOVE, FS_REMOVE, FS_REMOVE_DIRECTORY,
FS_REMOVE_RECURSIVE_DIRECTORY, FS_EXISTS, FS_IS_DIRECTORY, FS_IS_FILE, FS_FILESIZE,
FS_GET_TEMP_FILE, FS_GET_TEMP_PATH, FS_LIST_TREE, FS_UNZIP_FILE, FS_ZIP_FILE, SYS_DOWNLOAD,
SYS_EXECUTE, SYS_LOAD, SYS_LOG, SYS_LOG_LEVEL, SYS_MD5, SYS_OUTPUT, SYS_PROCESS_STAT,
SYS_RAND, SYS_READ, SYS_SPRINTF, SYS_TIME, SYS_START_PAGER, SYS_STOP_PAGER, SYS_SHA256, SYS_WAIT,
@ -169,9 +170,14 @@
delete SYS_WAIT;
}
if (typeof FS_CREATE_DIRECTORY !== "undefined") {
internal.createDirectory = FS_CREATE_DIRECTORY;
delete FS_CREATE_DIRECTORY;
if (typeof FS_FILESIZE !== "undefined") {
internal.fileSize = FS_FILESIZE;
delete FS_FILESIZE;
}
if (typeof FS_MAKE_DIRECTORY !== "undefined") {
internal.makeDirectory = FS_MAKE_DIRECTORY;
delete FS_MAKE_DIRECTORY;
}
if (typeof FS_EXISTS !== "undefined") {
@ -214,6 +220,16 @@
delete FS_REMOVE;
}
if (typeof FS_REMOVE_DIRECTORY !== "undefined") {
internal.removeDirectory = FS_REMOVE_DIRECTORY;
delete FS_REMOVE_DIRECTORY;
}
if (typeof FS_REMOVE_RECURSIVE_DIRECTORY !== "undefined") {
internal.removeDirectoryRecursive = FS_REMOVE_RECURSIVE_DIRECTORY;
delete FS_REMOVE_RECURSIVE_DIRECTORY;
}
if (typeof FS_UNZIP_FILE !== "undefined") {
internal.unzipFile = FS_UNZIP_FILE;
delete FS_UNZIP_FILE;

View File

@ -99,7 +99,7 @@ function POST_api_aal (req, res) {
}
var path = BuildPath(name, version);
fs.createDirectory(path, true);
fs.makeDirectoryRecursive(path);
internal.unzipFile(realFile, path, false, true);
var storage = GetStorage();

View File

@ -45,12 +45,6 @@
var internal = require("internal");
var fs = require("fs");
////////////////////////////////////////////////////////////////////////////////
/// @brief creates a directory
////////////////////////////////////////////////////////////////////////////////
fs.createDirectory = internal.createDirectory;
////////////////////////////////////////////////////////////////////////////////
/// @brief tests if a file exists
////////////////////////////////////////////////////////////////////////////////
@ -98,7 +92,13 @@
var i;
for (i = 0; i < arguments.length; ++i) {
list.push(arguments[i]);
var p = arguments[i];
if (p !== '') {
if (p.substr(- internal.PATH_SEPARATOR.length) !== internal.PATH_SEPARATOR) {
list.push(p);
}
}
}
return list.join(internal.PATH_SEPARATOR);
@ -110,6 +110,37 @@
fs.listTree = internal.listTree;
////////////////////////////////////////////////////////////////////////////////
/// @brief creates a directory
////////////////////////////////////////////////////////////////////////////////
fs.makeDirectory = internal.makeDirectory;
////////////////////////////////////////////////////////////////////////////////
/// @brief make a directory with all parent paths
////////////////////////////////////////////////////////////////////////////////
fs.makeDirectoryRecursive = function (path) {
var parts, subPart;
parts = path.split(internal.PATH_SEPARATOR);
subPart = '';
parts.forEach(function (s, i) {
if (i > 0) {
subPart += internal.PATH_SEPARATOR;
}
subPart += s;
try {
// directory may already exist
fs.makeDirectory(subPart);
}
catch (err) {
}
});
};
////////////////////////////////////////////////////////////////////////////////
/// @brief moves a file or directory
////////////////////////////////////////////////////////////////////////////////
@ -128,6 +159,24 @@
fs.remove = internal.remove;
////////////////////////////////////////////////////////////////////////////////
/// @brief removes a directory. the directory must be empty
////////////////////////////////////////////////////////////////////////////////
fs.removeDirectory = internal.removeDirectory;
////////////////////////////////////////////////////////////////////////////////
/// @brief removes a directory with all data in it
////////////////////////////////////////////////////////////////////////////////
fs.removeDirectoryRecursive = internal.removeDirectoryRecursive;
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the size of a file
////////////////////////////////////////////////////////////////////////////////
fs.size = internal.fileSize;
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////

View File

@ -1,5 +1,6 @@
/*jslint indent: 2, nomen: true, maxlen: 120, sloppy: true, vars: true, white: true, plusplus: true, nonpropdel: true, proto: true */
/*global require, module, Module, FS_CREATE_DIRECTORY, FS_MOVE, FS_REMOVE, FS_EXISTS, FS_IS_DIRECTORY, FS_IS_FILE,
/*global require, module, Module, FS_MAKE_DIRECTORY, FS_MOVE, FS_REMOVE, FS_REMOVE_DIRECTORY,
FS_REMOVE_RECURSIVE_DIRECTORY, FS_EXISTS, FS_IS_DIRECTORY, FS_IS_FILE, FS_FILESIZE,
FS_GET_TEMP_FILE, FS_GET_TEMP_PATH, FS_LIST_TREE, FS_UNZIP_FILE, FS_ZIP_FILE, SYS_DOWNLOAD,
SYS_EXECUTE, SYS_LOAD, SYS_LOG, SYS_LOG_LEVEL, SYS_MD5, SYS_OUTPUT, SYS_PROCESS_STAT,
SYS_RAND, SYS_READ, SYS_SPRINTF, SYS_TIME, SYS_START_PAGER, SYS_STOP_PAGER, SYS_SHA256, SYS_WAIT,
@ -169,9 +170,14 @@
delete SYS_WAIT;
}
if (typeof FS_CREATE_DIRECTORY !== "undefined") {
internal.createDirectory = FS_CREATE_DIRECTORY;
delete FS_CREATE_DIRECTORY;
if (typeof FS_FILESIZE !== "undefined") {
internal.fileSize = FS_FILESIZE;
delete FS_FILESIZE;
}
if (typeof FS_MAKE_DIRECTORY !== "undefined") {
internal.makeDirectory = FS_MAKE_DIRECTORY;
delete FS_MAKE_DIRECTORY;
}
if (typeof FS_EXISTS !== "undefined") {
@ -214,6 +220,16 @@
delete FS_REMOVE;
}
if (typeof FS_REMOVE_DIRECTORY !== "undefined") {
internal.removeDirectory = FS_REMOVE_DIRECTORY;
delete FS_REMOVE_DIRECTORY;
}
if (typeof FS_REMOVE_RECURSIVE_DIRECTORY !== "undefined") {
internal.removeDirectoryRecursive = FS_REMOVE_RECURSIVE_DIRECTORY;
delete FS_REMOVE_RECURSIVE_DIRECTORY;
}
if (typeof FS_UNZIP_FILE !== "undefined") {
internal.unzipFile = FS_UNZIP_FILE;
delete FS_UNZIP_FILE;

380
js/common/tests/shell-fs.js Normal file
View File

@ -0,0 +1,380 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief test filesystem functions
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2010-2012 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Jan Steemann
/// @author Copyright 2012, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
var jsunity = require("jsunity");
var fs = require("fs");
var internal = require("internal");
// -----------------------------------------------------------------------------
// --SECTION-- filesystem
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief test attributes
////////////////////////////////////////////////////////////////////////////////
function FileSystemSuite () {
var ERRORS = require("internal").errors;
var tempDir;
return {
////////////////////////////////////////////////////////////////////////////////
/// @brief set up
////////////////////////////////////////////////////////////////////////////////
setUp : function () {
tempDir = fs.join(fs.getTempFile('', false));
try {
fs.makeDirectoryRecursive(tempDir);
}
catch (err) {
}
},
////////////////////////////////////////////////////////////////////////////////
/// @brief tear down
////////////////////////////////////////////////////////////////////////////////
tearDown : function () {
if (tempDir.length > 5) {
fs.removeDirectoryRecursive(tempDir);
}
},
////////////////////////////////////////////////////////////////////////////////
/// @brief exists()
////////////////////////////////////////////////////////////////////////////////
testExists : function () {
var tempName;
// existing directory
tempName = fs.join(tempDir, 'foo');
try {
fs.removeDirectory(tempName);
}
catch (err) {
}
fs.makeDirectoryRecursive(tempName);
assertTrue(fs.exists(tempName));
fs.removeDirectory(tempName);
// non-existing directory/file
tempName = fs.join(tempDir, 'meow');
assertFalse(fs.exists(tempName));
// file
tempName = fs.getTempFile('', true);
assertTrue(fs.exists(tempName));
fs.remove(tempName);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief getTempFile()
////////////////////////////////////////////////////////////////////////////////
testGetTempFile : function () {
var tempName;
// creating a new file
tempName = fs.getTempFile('', true);
assertTrue(tempName !== '');
assertEqual(fs.getTempPath(), tempName.substr(0, fs.getTempPath().length));
assertTrue(fs.exists(tempName));
fs.remove(tempName);
// without creating
tempName = fs.getTempFile('', false);
assertTrue(tempName !== '');
assertEqual(fs.getTempPath(), tempName.substr(0, fs.getTempPath().length));
assertFalse(fs.exists(tempName));
// in a subdirectory
tempName = fs.getTempFile('tests', false);
assertTrue(tempName !== '');
assertEqual(fs.join(fs.getTempPath(), 'test'), tempName.substr(0, fs.join(fs.getTempPath(), 'test').length));
assertFalse(fs.exists(tempName));
fs.removeDirectory(fs.join(fs.getTempPath(), 'tests'));
},
////////////////////////////////////////////////////////////////////////////////
/// @brief getTempPath()
////////////////////////////////////////////////////////////////////////////////
testGetTempPath : function () {
var tempName;
tempName = fs.getTempPath();
assertTrue(tempName !== '');
assertTrue(fs.isDirectory(tempName));
},
////////////////////////////////////////////////////////////////////////////////
/// @brief isDirectory()
////////////////////////////////////////////////////////////////////////////////
testIsDirectory : function () {
var tempName;
// existing directory
tempName = fs.join(tempDir, 'foo');
try {
fs.removeDirectory(tempName);
}
catch (err) {
}
fs.makeDirectoryRecursive(tempName);
assertTrue(fs.exists(tempName));
assertTrue(fs.isDirectory(tempName));
fs.removeDirectory(tempName);
// non-existing directory/file
tempName = fs.join(tempDir, 'meow');
assertFalse(fs.exists(tempName));
assertFalse(fs.isDirectory(tempName));
// file
tempName = fs.getTempFile('', true);
assertTrue(fs.exists(tempName));
assertFalse(fs.isDirectory(tempName));
fs.remove(tempName);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief isFile()
////////////////////////////////////////////////////////////////////////////////
testIsFile : function () {
var tempName;
// existing directory
tempName = fs.join(tempDir, 'foo');
try {
fs.removeDirectory(tempName);
}
catch (err) {
}
try {
fs.remove(tempName);
}
catch (err) {
}
fs.makeDirectoryRecursive(tempName);
assertTrue(fs.exists(tempName));
assertFalse(fs.isFile(tempName));
fs.removeDirectory(tempName);
// non-existing directory/file
tempName = fs.join(tempDir, 'meow');
assertFalse(fs.exists(tempName));
assertFalse(fs.isFile(tempName));
// file
tempName = fs.getTempFile('', true);
assertTrue(fs.exists(tempName));
assertTrue(fs.isFile(tempName));
fs.remove(tempName);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief listTree()
////////////////////////////////////////////////////////////////////////////////
testListTree : function () {
},
////////////////////////////////////////////////////////////////////////////////
/// @brief makeDirectory()
////////////////////////////////////////////////////////////////////////////////
testMakeDirectory : function () {
var tempName;
tempName = fs.join(tempDir, 'bar');
try {
fs.removeDirectoryRecursive(tempName);
}
catch (err) {
}
assertFalse(fs.exists(tempName));
fs.makeDirectory(tempName);
assertTrue(fs.exists(tempName));
fs.removeDirectory(tempName);
tempName = fs.join(tempDir, 'baz');
try {
fs.removeDirectoryRecursive(tempName);
}
catch (err) {
}
// make with an existing directory
assertFalse(fs.exists(tempName));
fs.makeDirectory(tempName);
assertTrue(fs.exists(tempName));
try {
fs.makeDirectory(tempName);
fail();
}
catch (err) {
assertEqual(ERRORS.ERROR_SYS_ERROR.code, err.errorNum);
}
// make subdirectory
tempName = fs.join(tempDir, 'baz', 'foo');
fs.makeDirectory(tempName);
assertTrue(fs.exists(tempName));
tempName = fs.join(tempDir, 'baz', 'foo', 'test');
internal.write(tempName, "this is a test");
assertTrue(fs.exists(tempName));
try {
fs.makeDirectory(tempName);
fail();
}
catch (err) {
assertEqual(ERRORS.ERROR_SYS_ERROR.code, err.errorNum);
}
fs.removeDirectoryRecursive(fs.join(tempDir, 'baz'));
},
////////////////////////////////////////////////////////////////////////////////
/// @brief makeDirectoryRecursive()
////////////////////////////////////////////////////////////////////////////////
testMakeDirectoryRecursive : function () {
var tempName;
tempName = fs.join(tempDir, 'bar');
try {
fs.removeDirectoryRecursive(tempName);
}
catch (err) {
}
// create
fs.makeDirectoryRecursive(tempName);
assertTrue(fs.isDirectory(tempName));
// create again
fs.makeDirectoryRecursive(tempName);
assertTrue(fs.isDirectory(tempName));
// create subdirectories
tempName = fs.join(tempDir, 'bar', 'baz', 'test');
fs.makeDirectoryRecursive(tempName);
assertTrue(fs.isDirectory(tempName));
},
////////////////////////////////////////////////////////////////////////////////
/// @brief move()
////////////////////////////////////////////////////////////////////////////////
testMove : function () {
},
////////////////////////////////////////////////////////////////////////////////
/// @brief read()
////////////////////////////////////////////////////////////////////////////////
testRead : function () {
},
////////////////////////////////////////////////////////////////////////////////
/// @brief remove()
////////////////////////////////////////////////////////////////////////////////
testRemove : function () {
},
////////////////////////////////////////////////////////////////////////////////
/// @brief size()
////////////////////////////////////////////////////////////////////////////////
testSize : function () {
var tempName;
// existing file
tempName = fs.join(tempDir, 'foo');
internal.write(tempName, "this is a test file");
assertEqual(19, fs.size(tempName));
fs.remove(tempName);
// non-existing file
try {
fs.size(tempName);
fail();
}
catch (err) {
assertEqual(ERRORS.ERROR_FILE_NOT_FOUND.code, err.errorNum);
}
// directory
fs.makeDirectory(tempName);
try {
fs.size(tempName);
fail();
}
catch (err) {
assertEqual(ERRORS.ERROR_FILE_NOT_FOUND.code, err.errorNum);
}
fs.removeDirectory(tempName);
}
};
}
// -----------------------------------------------------------------------------
// --SECTION-- main
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief executes the test suite
////////////////////////////////////////////////////////////////////////////////
jsunity.run(FileSystemSuite);
return jsunity.done();
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// @addtogroup\\|// --SECTION--\\|/// @page\\|/// @}\\)"
// End:

View File

@ -95,6 +95,25 @@ static TRI_read_write_lock_t FileNamesLock;
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief remove trailing path separators from path.
/// path will be modified in-place
////////////////////////////////////////////////////////////////////////////////
static void RemoveTrailingSeparator (char* path) {
size_t n;
n = strlen(path);
if (n > 0) {
char* p = path + n - 1;
while (*p == TRI_DIR_SEPARATOR_CHAR) {
*p = '\0';
--p;
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief linear search of the giving element
///
@ -338,7 +357,7 @@ bool TRI_IsSymbolicLink (char const* path) {
#endif
////////////////////////////////////////////////////////////////////////////////
/// @brief checks if file exists
/// @brief checks if file or directory exists
////////////////////////////////////////////////////////////////////////////////
bool TRI_ExistsFile (char const* path) {
@ -1633,7 +1652,10 @@ int TRI_GetTempName (char const* directory,
}
TRI_Free(TRI_CORE_MEM_ZONE, temp);
// remove trailing PATH_SEPARATOR
RemoveTrailingSeparator(dir);
TRI_CreateRecursiveDirectory(dir);
if (! TRI_IsDirectory(dir)) {

View File

@ -82,7 +82,7 @@ bool TRI_IsDirectory (char const* path);
bool TRI_IsSymbolicLink (char const* path);
////////////////////////////////////////////////////////////////////////////////
/// @brief checks if file exists
/// @brief checks if file or directory exists
////////////////////////////////////////////////////////////////////////////////
bool TRI_ExistsFile (char const* path);

View File

@ -628,40 +628,6 @@ static v8::Handle<v8::Value> JS_Execute (v8::Arguments const& argv) {
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief create a directory
///
/// @FUN{fs.createDirectory(@FA{path}, @FA{createParents})}
///
/// Creates the directory specified @FA{path}. If @FA{createParents} is set to
/// @LIT{true}, then the path is created recursively.
////////////////////////////////////////////////////////////////////////////////
static v8::Handle<v8::Value> JS_CreateDirectory (v8::Arguments const& argv) {
v8::HandleScope scope;
// extract arguments
if (argv.Length() == 0 || argv.Length() > 2) {
return scope.Close(v8::ThrowException(v8::String::New("usage: createDirectory(<path>, <createParents>)")));
}
const string path = TRI_ObjectToString(argv[0]);
bool createParents = false;
if (argv.Length() > 1) {
createParents = TRI_ObjectToBoolean(argv[1]);
}
bool result;
if (createParents) {
result = TRI_CreateRecursiveDirectory(path.c_str());
}
else {
result = TRI_CreateDirectory(path.c_str());
}
return scope.Close(result ? v8::True() : v8::False());
}
////////////////////////////////////////////////////////////////////////////////
/// @brief checks if a file of any type or directory exists
///
@ -684,6 +650,41 @@ static v8::Handle<v8::Value> JS_Exists (v8::Arguments const& argv) {
return scope.Close(TRI_ExistsFile(filename.c_str()) ? v8::True() : v8::False());
}
////////////////////////////////////////////////////////////////////////////////
/// @brief get the size of a file
///
/// @FUN{fs.size(@FA{path})}
///
/// Returns the size of the file specified by @FA{path}.
////////////////////////////////////////////////////////////////////////////////
static v8::Handle<v8::Value> JS_SizeFile (v8::Arguments const& argv) {
v8::HandleScope scope;
// extract arguments
if (argv.Length() != 1) {
return scope.Close(v8::ThrowException(v8::String::New("usage: size(<path>)")));
}
TRI_Utf8ValueNFC name(TRI_UNKNOWN_MEM_ZONE, argv[0]);
if (*name == 0) {
return scope.Close(v8::ThrowException(CreateErrorObject(TRI_ERROR_BAD_PARAMETER, "<path> must be a string")));
}
if (! TRI_ExistsFile(*name) || TRI_IsDirectory(*name)) {
return scope.Close(v8::ThrowException(CreateErrorObject(TRI_ERROR_FILE_NOT_FOUND, "file not found")));
}
int64_t size = TRI_SizeFile(*name);
if (size < 0) {
return scope.Close(v8::ThrowException(CreateErrorObject(TRI_ERROR_FILE_NOT_FOUND, "file not found")));
}
return scope.Close(v8::Number::New((double) size));
}
////////////////////////////////////////////////////////////////////////////////
/// @brief reads in a line from stdin
///
@ -723,7 +724,7 @@ static v8::Handle<v8::Value> JS_GetTempPath (v8::Arguments const& argv) {
////////////////////////////////////////////////////////////////////////////////
/// @brief return the name for a (new) temporary file
///
/// @FUN{fs.getTempFile(@FA{directory})}
/// @FUN{fs.getTempFile(@FA{directory}, @FA{createFile})}
///
/// Returns the name for a new temporary file in directory @FA{directory}.
/// If @FA{createFile} is @LIT{true}, an empty file will be created so no other
@ -782,7 +783,7 @@ static v8::Handle<v8::Value> JS_IsDirectory (v8::Arguments const& argv) {
TRI_Utf8ValueNFC name(TRI_UNKNOWN_MEM_ZONE, argv[0]);
if (*name == 0) {
return scope.Close(v8::ThrowException(v8::String::New("<path> must be a string")));
return scope.Close(v8::ThrowException(CreateErrorObject(TRI_ERROR_BAD_PARAMETER, "<path> must be a string")));
}
// return result
@ -808,11 +809,11 @@ static v8::Handle<v8::Value> JS_IsFile (v8::Arguments const& argv) {
TRI_Utf8ValueNFC name(TRI_UNKNOWN_MEM_ZONE, argv[0]);
if (*name == 0) {
return scope.Close(v8::ThrowException(v8::String::New("<path> must be a string")));
return scope.Close(v8::ThrowException(CreateErrorObject(TRI_ERROR_BAD_PARAMETER, "<path> must be a string")));
}
// return result
return scope.Close(TRI_ExistsFile(*name) ? v8::True() : v8::False());
return scope.Close((TRI_ExistsFile(*name) && ! TRI_IsDirectory(*name)) ? v8::True() : v8::False());
}
////////////////////////////////////////////////////////////////////////////////
@ -838,7 +839,7 @@ static v8::Handle<v8::Value> JS_ListTree (v8::Arguments const& argv) {
TRI_Utf8ValueNFC name(TRI_UNKNOWN_MEM_ZONE, argv[0]);
if (*name == 0) {
return scope.Close(v8::ThrowException(v8::String::New("<path> must be a string")));
return scope.Close(v8::ThrowException(CreateErrorObject(TRI_ERROR_BAD_PARAMETER, "<path> must be a string")));
}
// constructed listing
@ -857,6 +858,39 @@ static v8::Handle<v8::Value> JS_ListTree (v8::Arguments const& argv) {
return scope.Close(result);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief create a directory
///
/// @FUN{fs.makeDirectory(@FA{path})}
///
/// Creates the directory specified by @FA{path}.
////////////////////////////////////////////////////////////////////////////////
static v8::Handle<v8::Value> JS_MakeDirectory (v8::Arguments const& argv) {
v8::HandleScope scope;
// 2nd argument (permissions) are ignored for now
// extract arguments
if (argv.Length() != 1 && argv.Length() != 2) {
return scope.Close(v8::ThrowException(v8::String::New("usage: makeDirectory(<path>)")));
}
TRI_Utf8ValueNFC name(TRI_UNKNOWN_MEM_ZONE, argv[0]);
if (*name == 0) {
return scope.Close(v8::ThrowException(CreateErrorObject(TRI_ERROR_BAD_PARAMETER, "<path> must be a string")));
}
bool result = TRI_CreateDirectory(*name);
if (! result) {
return scope.Close(v8::ThrowException(CreateErrorObject(TRI_errno(), "cannot create directory")));
}
return scope.Close(v8::Undefined());
}
////////////////////////////////////////////////////////////////////////////////
/// @brief unzips a file
///
@ -997,7 +1031,7 @@ static v8::Handle<v8::Value> JS_Load (v8::Arguments const& argv) {
TRI_Utf8ValueNFC name(TRI_UNKNOWN_MEM_ZONE, argv[0]);
if (*name == 0) {
return scope.Close(v8::ThrowException(v8::String::New("<filename> must be a string")));
return scope.Close(v8::ThrowException(CreateErrorObject(TRI_ERROR_BAD_PARAMETER, "<filename> must be a string")));
}
char* content = TRI_SlurpFile(TRI_UNKNOWN_MEM_ZONE, *name, NULL);
@ -1511,14 +1545,18 @@ static v8::Handle<v8::Value> JS_Save (v8::Arguments const& argv) {
static v8::Handle<v8::Value> JS_Remove (v8::Arguments const& argv) {
v8::HandleScope scope;
// extract two arguments
// extract the arguments
if (argv.Length() != 1) {
return scope.Close(v8::ThrowException(v8::String::New("usage: remove(<filename>)")));
}
TRI_Utf8ValueNFC name(TRI_UNKNOWN_MEM_ZONE, argv[0]);
string filename = TRI_ObjectToString(argv[0]);
if (*name == 0) {
return scope.Close(v8::ThrowException(v8::String::New("<path> must be a string")));
}
int res = TRI_UnlinkFile(filename.c_str());
int res = TRI_UnlinkFile(*name);
if (res != TRI_ERROR_NO_ERROR) {
return scope.Close(v8::ThrowException(TRI_CreateErrorObject(res, "cannot remove file")));
@ -1527,6 +1565,88 @@ static v8::Handle<v8::Value> JS_Remove (v8::Arguments const& argv) {
return scope.Close(v8::Undefined());
}
////////////////////////////////////////////////////////////////////////////////
/// @brief removes an empty directory
///
/// @FUN{fs.removeDirectory(@FA{path})}
///
/// Removes a directory if it is empty. Throws an exception if the path is not
/// an empty directory.
////////////////////////////////////////////////////////////////////////////////
static v8::Handle<v8::Value> JS_RemoveDirectory (v8::Arguments const& argv) {
v8::HandleScope scope;
// extract the arguments
if (argv.Length() != 1) {
return scope.Close(v8::ThrowException(v8::String::New("usage: removeDirectory(<path>)")));
}
TRI_Utf8ValueNFC name(TRI_UNKNOWN_MEM_ZONE, argv[0]);
if (*name == 0) {
return scope.Close(v8::ThrowException(v8::String::New("<path> must be a string")));
}
if (! TRI_IsDirectory(*name)) {
return scope.Close(v8::ThrowException(TRI_CreateErrorObject(TRI_ERROR_BAD_PARAMETER, "<path> must be a valid directory name")));
}
int res = TRI_RemoveEmptyDirectory(*name);
if (res != TRI_ERROR_NO_ERROR) {
return scope.Close(v8::ThrowException(TRI_CreateErrorObject(res, "cannot remove directory")));
}
return scope.Close(v8::Undefined());
}
////////////////////////////////////////////////////////////////////////////////
/// @brief removes a directory
///
/// @FUN{fs.removeDirectoryRecursive(@FA{path})}
///
/// Removes a directory with all subelements. Throws an exception if the path
/// is not a directory.
////////////////////////////////////////////////////////////////////////////////
static v8::Handle<v8::Value> JS_RemoveRecursiveDirectory (v8::Arguments const& argv) {
v8::HandleScope scope;
// extract the arguments
if (argv.Length() != 1) {
return scope.Close(v8::ThrowException(v8::String::New("usage: removeDirectoryRecursive(<path>)")));
}
TRI_Utf8ValueNFC name(TRI_UNKNOWN_MEM_ZONE, argv[0]);
if (*name == 0) {
return scope.Close(v8::ThrowException(v8::String::New("<path> must be a string")));
}
if (! TRI_IsDirectory(*name)) {
return scope.Close(v8::ThrowException(TRI_CreateErrorObject(TRI_ERROR_BAD_PARAMETER, "<path> must be a valid directory name")));
}
if (TempPath.size() < 8) {
// some security measure so we don't accidently delete all our files
return scope.Close(v8::ThrowException(TRI_CreateErrorObject(TRI_ERROR_INTERNAL, "temporary directory name is too short. will not remove directory")));
}
const string path(*name);
if (path.substr(0, TempPath.size()) != TempPath) {
return scope.Close(v8::ThrowException(TRI_CreateErrorObject(TRI_ERROR_BAD_PARAMETER, "directory to be removed is outside of temporary path")));
}
int res = TRI_RemoveDirectory(*name);
if (res != TRI_ERROR_NO_ERROR) {
return scope.Close(v8::ThrowException(TRI_CreateErrorObject(res, "cannot remove directory")));
}
return scope.Close(v8::Undefined());
}
////////////////////////////////////////////////////////////////////////////////
/// @brief formats the arguments
///
@ -2170,15 +2290,18 @@ void TRI_InitV8Utils (v8::Handle<v8::Context> context,
// create the global functions
// .............................................................................
TRI_AddGlobalFunctionVocbase(context, "FS_CREATE_DIRECTORY", JS_CreateDirectory);
TRI_AddGlobalFunctionVocbase(context, "FS_EXISTS", JS_Exists);
TRI_AddGlobalFunctionVocbase(context, "FS_GET_TEMP_FILE", JS_GetTempFile);
TRI_AddGlobalFunctionVocbase(context, "FS_GET_TEMP_PATH", JS_GetTempPath);
TRI_AddGlobalFunctionVocbase(context, "FS_IS_DIRECTORY", JS_IsDirectory);
TRI_AddGlobalFunctionVocbase(context, "FS_IS_FILE", JS_IsFile);
TRI_AddGlobalFunctionVocbase(context, "FS_LIST_TREE", JS_ListTree);
TRI_AddGlobalFunctionVocbase(context, "FS_MAKE_DIRECTORY", JS_MakeDirectory);
TRI_AddGlobalFunctionVocbase(context, "FS_MOVE", JS_Move);
TRI_AddGlobalFunctionVocbase(context, "FS_REMOVE", JS_Remove);
TRI_AddGlobalFunctionVocbase(context, "FS_REMOVE_DIRECTORY", JS_RemoveDirectory);
TRI_AddGlobalFunctionVocbase(context, "FS_REMOVE_RECURSIVE_DIRECTORY", JS_RemoveRecursiveDirectory);
TRI_AddGlobalFunctionVocbase(context, "FS_FILESIZE", JS_SizeFile);
TRI_AddGlobalFunctionVocbase(context, "FS_UNZIP_FILE", JS_UnzipFile);
TRI_AddGlobalFunctionVocbase(context, "FS_ZIP_FILE", JS_ZipFile);