From c59f00efa8dfa90ece1ce978c8f44e7604dc5b5c Mon Sep 17 00:00:00 2001 From: Jan Steemann Date: Wed, 15 May 2013 15:55:48 +0200 Subject: [PATCH] fixed handling of --temp-path --- CHANGELOG | 2 + arangod/RestServer/ArangoServer.cpp | 8 +++- arangod/V8Server/ApplicationV8.cpp | 5 +-- arangod/V8Server/ApplicationV8.h | 8 +--- arangod/VocBase/vocbase.c | 3 ++ arangosh/ArangoShell/ArangoClient.cpp | 13 +++--- arangosh/ArangoShell/ArangoClient.h | 6 --- arangosh/V8Client/arangosh.cpp | 3 +- lib/BasicsC/files.c | 61 ++++++++++++++++++++++++++- lib/BasicsC/files.h | 25 +++++++++++ lib/BasicsC/init.c | 3 ++ lib/V8/v8-utils.cpp | 19 ++++++--- lib/V8/v8-utils.h | 9 +--- 13 files changed, 123 insertions(+), 42 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 42b1df7b0b..b807dda859 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,8 @@ v1.4 ------ +* fixed usage of --temp-path in aranogd and arangosh + * issue #526: Unable to escape when an errorneous command is entered into the js shell * issue #523: Graph and vertex methods for the javascript api diff --git a/arangod/RestServer/ArangoServer.cpp b/arangod/RestServer/ArangoServer.cpp index 4a387fd8c6..4cabbc49ab 100644 --- a/arangod/RestServer/ArangoServer.cpp +++ b/arangod/RestServer/ArangoServer.cpp @@ -256,7 +256,7 @@ void ArangoServer::buildApplicationServer () { // V8 engine // ............................................................................. - _applicationV8 = new ApplicationV8(_binaryPath, _tempPath); + _applicationV8 = new ApplicationV8(_binaryPath); _applicationServer->addFeature(_applicationV8); // ............................................................................. @@ -411,9 +411,15 @@ void ArangoServer::buildApplicationServer () { CLEANUP_LOGGING_AND_EXIT_ON_FATAL_ERROR(); } + // set the temp-path + if (_applicationServer->programOptions().has("temp-path")) { + TRI_SetUserTempPath((char*) _tempPath.c_str()); + } + // dump version details LOGGER_INFO(rest::Version::getVerboseVersionString()); + // configure v8 if (_applicationServer->programOptions().has("development-mode")) { _developmentMode = true; _applicationV8->enableDevelopmentMode(); diff --git a/arangod/V8Server/ApplicationV8.cpp b/arangod/V8Server/ApplicationV8.cpp index 5cce5f744c..37f6dd4f96 100644 --- a/arangod/V8Server/ApplicationV8.cpp +++ b/arangod/V8Server/ApplicationV8.cpp @@ -174,9 +174,8 @@ void ApplicationV8::V8Context::handleGlobalContextMethods () { /// @brief constructor //////////////////////////////////////////////////////////////////////////////// -ApplicationV8::ApplicationV8 (string const& binaryPath, string const& tempPath) +ApplicationV8::ApplicationV8 (string const& binaryPath) : ApplicationFeature("V8"), - _tempPath(tempPath), _startupPath(), _modulesPath(), _packagePath(), @@ -739,7 +738,7 @@ bool ApplicationV8::prepareV8Instance (const size_t i) { TRI_InitV8Buffer(context->_context); TRI_InitV8Conversions(context->_context); - TRI_InitV8Utils(context->_context, _modulesPath, _packagePath, _tempPath); + TRI_InitV8Utils(context->_context, _modulesPath, _packagePath); TRI_InitV8Shell(context->_context); { diff --git a/arangod/V8Server/ApplicationV8.h b/arangod/V8Server/ApplicationV8.h index 4e45b67f8a..58e2496c18 100644 --- a/arangod/V8Server/ApplicationV8.h +++ b/arangod/V8Server/ApplicationV8.h @@ -173,7 +173,7 @@ namespace triagens { /// @brief constructor //////////////////////////////////////////////////////////////////////////////// - ApplicationV8 (string const&, string const&); + ApplicationV8 (string const&); //////////////////////////////////////////////////////////////////////////////// /// @brief destructor @@ -345,12 +345,6 @@ namespace triagens { /// @{ //////////////////////////////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////// -/// @brief temporary path -//////////////////////////////////////////////////////////////////////////////// - - string _tempPath; - //////////////////////////////////////////////////////////////////////////////// /// @brief path to the directory containing alternate startup scripts /// diff --git a/arangod/VocBase/vocbase.c b/arangod/VocBase/vocbase.c index d4e878fe98..acfb172e12 100644 --- a/arangod/VocBase/vocbase.c +++ b/arangod/VocBase/vocbase.c @@ -2224,8 +2224,11 @@ void TRI_ReleaseCollectionVocBase (TRI_vocbase_t* vocbase, TRI_vocbase_col_t* co //////////////////////////////////////////////////////////////////////////////// void TRI_InitialiseVocBase () { + // TODO: these two fcalls can probably be removed because we're initialising + // BasicsC anyway TRI_InitialiseHashes(); TRI_InitialiseRandom(); + TRI_GlobalInitStatementListAql(); ServerIdentifier = TRI_UInt16Random(); diff --git a/arangosh/ArangoShell/ArangoClient.cpp b/arangosh/ArangoShell/ArangoClient.cpp index b7772ede4a..b5f5eda85f 100644 --- a/arangosh/ArangoShell/ArangoClient.cpp +++ b/arangosh/ArangoShell/ArangoClient.cpp @@ -341,6 +341,11 @@ void ArangoClient::parse (ProgramOptions& options, } } + // set temp path + if (options.has("temp-path")) { + TRI_SetUserTempPath((char*) _tempPath.c_str()); + } + // check if have a password _hasPassword = options.has("server.password") || options.has("server.disable-authentication") || @@ -668,14 +673,6 @@ void ArangoClient::setUsePager (bool value) { _usePager = value; } -//////////////////////////////////////////////////////////////////////////////// -/// @brief gets the temporary path -//////////////////////////////////////////////////////////////////////////////// - -string const& ArangoClient::tempPath () const { - return _tempPath; -} - //////////////////////////////////////////////////////////////////////////////// /// @brief gets endpoint to connect to as string //////////////////////////////////////////////////////////////////////////////// diff --git a/arangosh/ArangoShell/ArangoClient.h b/arangosh/ArangoShell/ArangoClient.h index cd66b0d2fd..e71b72d598 100644 --- a/arangosh/ArangoShell/ArangoClient.h +++ b/arangosh/ArangoShell/ArangoClient.h @@ -325,12 +325,6 @@ namespace triagens { void setUsePager (bool); -//////////////////////////////////////////////////////////////////////////////// -/// @brief gets the temporary path -//////////////////////////////////////////////////////////////////////////////// - - string const& tempPath () const; - //////////////////////////////////////////////////////////////////////////////// /// @brief gets endpoint to connect to as string //////////////////////////////////////////////////////////////////////////////// diff --git a/arangosh/V8Client/arangosh.cpp b/arangosh/V8Client/arangosh.cpp index f11ee9f6ab..ecafcac876 100644 --- a/arangosh/V8Client/arangosh.cpp +++ b/arangosh/V8Client/arangosh.cpp @@ -1454,7 +1454,8 @@ int main (int argc, char* argv[]) { TRI_AddGlobalVariableVocbase(context, "SYS_OUTPUT", v8::FunctionTemplate::New(JS_PagerOutput)->GetFunction()); TRI_InitV8Buffer(context); - TRI_InitV8Utils(context, StartupModules, StartupPackages, BaseClient.tempPath()); + + TRI_InitV8Utils(context, StartupModules, StartupPackages); TRI_InitV8Shell(context); // reset the prompt error flag (will determine prompt colors) diff --git a/lib/BasicsC/files.c b/lib/BasicsC/files.c index d3ea4c41ac..cef17f659e 100644 --- a/lib/BasicsC/files.c +++ b/lib/BasicsC/files.c @@ -67,6 +67,12 @@ static bool Initialised = false; +//////////////////////////////////////////////////////////////////////////////// +/// @brief user-defined temporary path +//////////////////////////////////////////////////////////////////////////////// + +static char* TempPath; + //////////////////////////////////////////////////////////////////////////////// /// @brief names of blocking files //////////////////////////////////////////////////////////////////////////////// @@ -1746,7 +1752,8 @@ int TRI_GetTempName (char const* directory, char* temp; int tries; - temp = TRI_GetTempPath(); + temp = TRI_GetUserTempPath(); + if (directory != NULL) { dir = TRI_Concatenate2File(temp, directory); } @@ -1816,6 +1823,58 @@ int TRI_GetTempName (char const* directory, return TRI_ERROR_INTERNAL; } +//////////////////////////////////////////////////////////////////////////////// +/// @brief return the user-defined temp path, with a fallback to the system's +/// temp path if none is specified +//////////////////////////////////////////////////////////////////////////////// + +char* TRI_GetUserTempPath (void) { + if (TempPath == NULL) { + return TRI_GetTempPath(); + } + + return TRI_DuplicateString(TempPath); +} + +//////////////////////////////////////////////////////////////////////////////// +/// @brief set a new user-defined temp path +//////////////////////////////////////////////////////////////////////////////// + +void TRI_SetUserTempPath (char* path) { + if (TempPath != NULL) { + TRI_FreeString(TRI_CORE_MEM_ZONE, TempPath); + } + + if (path == NULL) { + // unregister user-defined temp path + TempPath = NULL; + } + else { + // copy the user-defined temp path + TempPath = TRI_DuplicateString(path); + } +} + +//////////////////////////////////////////////////////////////////////////////// +/// @brief initialise the files subsystem +//////////////////////////////////////////////////////////////////////////////// + +void TRI_InitialiseFiles (void) { + // clear user-defined temp path + TempPath = NULL; +} + +//////////////////////////////////////////////////////////////////////////////// +/// @brief shutdown the files subsystem +//////////////////////////////////////////////////////////////////////////////// + +void TRI_ShutdownFiles (void) { + if (TempPath != NULL) { + // free any user-defined temp-path + TRI_FreeString(TRI_CORE_MEM_ZONE, TempPath); + } +} + //////////////////////////////////////////////////////////////////////////////// /// @} //////////////////////////////////////////////////////////////////////////////// diff --git a/lib/BasicsC/files.h b/lib/BasicsC/files.h index 25cb80f7ee..99d1da5199 100644 --- a/lib/BasicsC/files.h +++ b/lib/BasicsC/files.h @@ -270,6 +270,31 @@ char* TRI_GetTempPath (void); int TRI_GetTempName (char const*, char**, const bool); +//////////////////////////////////////////////////////////////////////////////// +/// @brief return the user-defined temp path, with a fallback to the system's +/// temp path if none is specified +//////////////////////////////////////////////////////////////////////////////// + +char* TRI_GetUserTempPath (void); + +//////////////////////////////////////////////////////////////////////////////// +/// @brief set a new user-defined temp path +//////////////////////////////////////////////////////////////////////////////// + +void TRI_SetUserTempPath (char*); + +//////////////////////////////////////////////////////////////////////////////// +/// @brief initialise the files subsystem +//////////////////////////////////////////////////////////////////////////////// + +void TRI_InitialiseFiles (void); + +//////////////////////////////////////////////////////////////////////////////// +/// @brief shutdown the files subsystem +//////////////////////////////////////////////////////////////////////////////// + +void TRI_ShutdownFiles (void); + //////////////////////////////////////////////////////////////////////////////// /// @} //////////////////////////////////////////////////////////////////////////////// diff --git a/lib/BasicsC/init.c b/lib/BasicsC/init.c index 7c0a75eb94..60fcf52f00 100644 --- a/lib/BasicsC/init.c +++ b/lib/BasicsC/init.c @@ -27,6 +27,7 @@ #include "init.h" +#include "BasicsC/files.h" #include "BasicsC/hashes.h" #include "BasicsC/logging.h" #include "BasicsC/mersenne.h" @@ -53,6 +54,7 @@ void TRI_InitialiseC (int argc, char* argv[]) { TRI_InitialiseMemory(); TRI_InitialiseMersenneTwister(); TRI_InitialiseError(); + TRI_InitialiseFiles(); TRI_InitialiseMimetypes(); TRI_InitialiseLogging(false); TRI_InitialiseHashes(); @@ -74,6 +76,7 @@ void TRI_ShutdownC () { TRI_ShutdownHashes(); TRI_ShutdownLogging(); TRI_ShutdownMimetypes(); + TRI_ShutdownFiles(); TRI_ShutdownError(); TRI_ShutdownMemory(); } diff --git a/lib/V8/v8-utils.cpp b/lib/V8/v8-utils.cpp index 921615f9a8..aed213c210 100644 --- a/lib/V8/v8-utils.cpp +++ b/lib/V8/v8-utils.cpp @@ -698,7 +698,7 @@ static v8::Handle JS_GetTempPath (v8::Arguments const& argv) { } // return result - return scope.Close(v8::String::New(TempPath.c_str(), TempPath.size())); + return scope.Close(v8::String::New(TRI_GetUserTempPath())); } //////////////////////////////////////////////////////////////////////////////// @@ -1693,16 +1693,24 @@ static v8::Handle JS_RemoveRecursiveDirectory (v8::Arguments const& a TRI_V8_EXCEPTION_PARAMETER(scope, " must be a valid directory name"); } - if (TempPath.size() < 8) { + char* tempPath = TRI_GetUserTempPath(); + + if (tempPath == NULL || strlen(tempPath) < 6) { // some security measure so we don't accidently delete all our files + TRI_FreeString(TRI_CORE_MEM_ZONE, tempPath); + TRI_V8_EXCEPTION_PARAMETER(scope, "temporary directory name is too short. will not remove directory"); } const string path(*name); - if (path.substr(0, TempPath.size()) != TempPath) { + if (! TRI_EqualString2(path.c_str(), tempPath, strlen(tempPath))) { + TRI_FreeString(TRI_CORE_MEM_ZONE, tempPath); + TRI_V8_EXCEPTION_PARAMETER(scope, "directory to be removed is outside of temporary path"); } + TRI_FreeString(TRI_CORE_MEM_ZONE, tempPath); + int res = TRI_RemoveDirectory(*name); if (res != TRI_ERROR_NO_ERROR) { @@ -2389,8 +2397,7 @@ v8::Handle TRI_V8PathList (string const& modules) { void TRI_InitV8Utils (v8::Handle context, string const& modules, - string const& packages, - string const& tempPath) { + string const& packages) { v8::HandleScope scope; // check the isolate @@ -2400,8 +2407,6 @@ void TRI_InitV8Utils (v8::Handle context, v8::Handle ft; v8::Handle rt; - TempPath = tempPath; - // ............................................................................. // generate the general error template // ............................................................................. diff --git a/lib/V8/v8-utils.h b/lib/V8/v8-utils.h index 3046a50644..fcbdfb6407 100644 --- a/lib/V8/v8-utils.h +++ b/lib/V8/v8-utils.h @@ -65,12 +65,6 @@ class TRI_Utf8ValueNFC { // --SECTION-- public constants // ----------------------------------------------------------------------------- -/////////////////////////////////////////////////////////////////////////////// -/// @brief temporary path -//////////////////////////////////////////////////////////////////////////////// - -static std::string TempPath; - /////////////////////////////////////////////////////////////////////////////// /// @brief slot for a type //////////////////////////////////////////////////////////////////////////////// @@ -201,8 +195,7 @@ v8::Handle TRI_V8PathList (std::string const& modules); void TRI_InitV8Utils (v8::Handle, std::string const& modules, - std::string const& nodes, - std::string const& tempPath); + std::string const& nodes); #endif