ENH: provide argList::envGlobalPath() static method

- this is identical to either of these solutions:

     * getEnv("FOAM_CASE")
     * stringOps::expand("<case>")

  but with a closer resemblance to argList or Time globalPath(),
  which makes the intent clearer.
  Avoids using raw strings in the caller, which improves compile-time checks.

  Used in situations where a class has no derivation path or other
  access to a time registry or command args.
This commit is contained in:
Mark Olesen
2018-12-15 14:39:12 +01:00
parent 455c8ef540
commit 9076f5b21b
9 changed files with 45 additions and 17 deletions

View File

@ -25,6 +25,7 @@ License
#include "dynamicCode.H" #include "dynamicCode.H"
#include "dynamicCodeContext.H" #include "dynamicCodeContext.H"
#include "argList.H"
#include "stringOps.H" #include "stringOps.H"
#include "Fstream.H" #include "Fstream.H"
#include "IOstreams.H" #include "IOstreams.H"
@ -64,10 +65,10 @@ void Foam::dynamicCode::checkSecurity
if (isAdministrator()) if (isAdministrator())
{ {
FatalIOErrorInFunction(dict) FatalIOErrorInFunction(dict)
<< "This code should not be executed by someone with administrator" << "This code should not be executed by someone"
<< " rights due to security reasons." << nl << " with administrator rights for security reasons." << nl
<< "(it writes a shared library which then gets loaded " << "It generates a shared library which is loaded using dlopen"
<< "using dlopen)" << nl << endl
<< exit(FatalIOError); << exit(FatalIOError);
} }
@ -292,8 +293,8 @@ bool Foam::dynamicCode::writeDigest(const std::string& sha1) const
Foam::dynamicCode::dynamicCode(const word& codeName, const word& codeDirName) Foam::dynamicCode::dynamicCode(const word& codeName, const word& codeDirName)
: :
codeRoot_(stringOps::expand("<case>")/topDirName), codeRoot_(argList::envGlobalPath()/topDirName),
libSubDir_(stringOps::expand("platforms/$WM_OPTIONS/lib")), libSubDir_(stringOps::expand("platforms/${WM_OPTIONS}/lib")),
codeName_(codeName), codeName_(codeName),
codeDirName_(codeDirName) codeDirName_(codeDirName)
{ {

View File

@ -463,6 +463,14 @@ bool Foam::argList::postProcess(int argc, char *argv[])
} }
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::fileName Foam::argList::envGlobalPath()
{
return Foam::getEnv("FOAM_CASE");
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::word Foam::argList::optionCompat(const word& optName) Foam::word Foam::argList::optionCompat(const word& optName)

View File

@ -272,6 +272,18 @@ public:
// Member Functions // Member Functions
// Environment
//- Global case (directory) from environment variable
//
// Returns the contents of the \c FOAM_CASE variable,
// which has previously been set by argList or by Time.
//
// This will normally be identical to the value of globalPath(),
// but obtained via the environment.
static fileName envGlobalPath();
// Low-level // Low-level
//- Scan for -help, -doc options etc prior to checking the validity //- Scan for -help, -doc options etc prior to checking the validity

View File

@ -114,9 +114,17 @@ bool Foam::functionObjects::systemCall::read(const dictionary& dict)
if (executeCalls_.empty() && endCalls_.empty() && writeCalls_.empty()) if (executeCalls_.empty() && endCalls_.empty() && writeCalls_.empty())
{ {
WarningInFunction WarningInFunction
<< "no executeCalls, endCalls or writeCalls defined." << "No executeCalls, endCalls or writeCalls defined."
<< endl; << endl;
} }
else if (isAdministrator())
{
FatalErrorInFunction
<< "System calls should not be executed by someone"
<< " with administrator rights for security reasons." << nl
<< nl << endl
<< exit(FatalError);
}
else if (!dynamicCode::allowSystemOperations) else if (!dynamicCode::allowSystemOperations)
{ {
FatalErrorInFunction FatalErrorInFunction

View File

@ -25,7 +25,7 @@ License
#include "noiseModel.H" #include "noiseModel.H"
#include "functionObject.H" #include "functionObject.H"
#include "stringOps.H" #include "argList.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -143,7 +143,7 @@ Foam::fileName Foam::noiseModel::baseFileDir(const label dataseti) const
{ {
return return
( (
stringOps::expand("<case>") // ie, globalPath() argList::envGlobalPath()
/ functionObject::outputPrefix / functionObject::outputPrefix
/ "noise" / "noise"
/ outputPrefix_ / outputPrefix_

View File

@ -25,7 +25,7 @@ License
#include "pointNoise.H" #include "pointNoise.H"
#include "noiseFFT.H" #include "noiseFFT.H"
#include "stringOps.H" #include "argList.H"
#include "addToRunTimeSelectionTable.H" #include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -244,8 +244,7 @@ void pointNoise::calculate()
if (!fName.isAbsolute()) if (!fName.isAbsolute())
{ {
// ie, globalPath() / name fName = argList::envGlobalPath()/fName;
fName = stringOps::expand("<case>")/fName;
} }
Function1Types::CSV<scalar> data("pressure", dict_, fName); Function1Types::CSV<scalar> data("pressure", dict_, fName);

View File

@ -27,8 +27,8 @@ License
#include "surfaceReader.H" #include "surfaceReader.H"
#include "surfaceWriter.H" #include "surfaceWriter.H"
#include "noiseFFT.H" #include "noiseFFT.H"
#include "argList.H"
#include "graph.H" #include "graph.H"
#include "stringOps.H"
#include "addToRunTimeSelectionTable.H" #include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -465,8 +465,7 @@ void surfaceNoise::calculate()
if (!fName.isAbsolute()) if (!fName.isAbsolute())
{ {
// ie, globalPath() / name fName = argList::envGlobalPath()/fName;
fName = stringOps::expand("<case>")/fName;
} }
initialise(fName); initialise(fName);

View File

@ -25,6 +25,7 @@ License
#include "boundaryDataSurfaceWriter.H" #include "boundaryDataSurfaceWriter.H"
#include "makeSurfaceWriterMethods.H" #include "makeSurfaceWriterMethods.H"
#include "argList.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -62,7 +63,7 @@ Foam::fileName Foam::boundaryDataSurfaceWriter::write
const pointField& points = surf.points(); const pointField& points = surf.points();
// Dummy time to use as an objectRegistry // Dummy time to use as an objectRegistry
const fileName caseDir(getEnv("FOAM_CASE")); const fileName caseDir(argList::envGlobalPath());
Time dummyTime Time dummyTime
( (

View File

@ -54,7 +54,7 @@ Foam::fileName Foam::boundaryDataSurfaceWriter::writeTemplate
const faceList& faces = surf.faces(); const faceList& faces = surf.faces();
// Dummy time to use as an objectRegistry // Dummy time to use as an objectRegistry
const fileName caseDir(getEnv("FOAM_CASE")); const fileName caseDir(argList::envGlobalPath());
Time dummyTime Time dummyTime
( (