Add Filesystem::restorecwd()

This allows restoring the working directory to its original value, which may not actually be the DF root. See #1671, dfhack/scripts#152
develop
lethosor 2020-10-14 21:22:53 -04:00
parent cc159909e2
commit ce7772a1c2
No known key found for this signature in database
GPG Key ID: 76A269552F4F58C1
5 changed files with 28 additions and 1 deletions

@ -2160,6 +2160,10 @@ unless otherwise noted.
Changes the current directory to ``path``. Use with caution.
* ``dfhack.filesystem.restorecwd()``
Restores the current working directory to what it was when DF started.
* ``dfhack.filesystem.mkdir(path)``
Creates a new directory. Returns ``false`` if unsuccessful, including if ``path`` already exists.

@ -1618,7 +1618,10 @@ bool Core::Init()
freopen("stderr.log", "w", stderr);
#endif
fprintf(stderr, "DFHack build: %s\n", Version::git_description());
Filesystem::init();
cerr << "DFHack build: " << Version::git_description() << "\n"
<< "Starting with working directory: " << Filesystem::getcwd() << endl;
// find out what we are...
#ifdef LINUX_BUILD

@ -2371,6 +2371,7 @@ static const luaL_Reg dfhack_screen_funcs[] = {
static const LuaWrapper::FunctionReg dfhack_filesystem_module[] = {
WRAPM(Filesystem, getcwd),
WRAPM(Filesystem, restorecwd),
WRAPM(Filesystem, chdir),
WRAPM(Filesystem, mkdir),
WRAPM(Filesystem, mkdir_recursive),

@ -146,8 +146,10 @@ enum _filetype {
namespace DFHack {
namespace Filesystem {
DFHACK_EXPORT void init ();
DFHACK_EXPORT bool chdir (std::string path);
DFHACK_EXPORT std::string getcwd ();
DFHACK_EXPORT bool restorecwd ();
DFHACK_EXPORT bool mkdir (std::string path);
// returns true on success or if directory already exists
DFHACK_EXPORT bool mkdir_recursive (std::string path);

@ -52,6 +52,18 @@ SOFTWARE.
using namespace DFHack;
static bool initialized = false;
static std::string initial_cwd;
void Filesystem::init ()
{
if (!initialized)
{
initialized = true;
initial_cwd = Filesystem::getcwd();
}
}
bool Filesystem::chdir (std::string path)
{
return ::chdir(path.c_str()) == 0;
@ -71,6 +83,11 @@ std::string Filesystem::getcwd ()
return result;
}
bool Filesystem::restorecwd ()
{
return Filesystem::chdir(initial_cwd);
}
bool Filesystem::mkdir (std::string path)
{
int fail;