From ce7772a1c267bdba1f2c305a872fe8b088302a51 Mon Sep 17 00:00:00 2001 From: lethosor Date: Wed, 14 Oct 2020 21:22:53 -0400 Subject: [PATCH] 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 --- docs/Lua API.rst | 4 ++++ library/Core.cpp | 5 ++++- library/LuaApi.cpp | 1 + library/include/modules/Filesystem.h | 2 ++ library/modules/Filesystem.cpp | 17 +++++++++++++++++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/Lua API.rst b/docs/Lua API.rst index fb67610d1..b310b449e 100644 --- a/docs/Lua API.rst +++ b/docs/Lua API.rst @@ -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. diff --git a/library/Core.cpp b/library/Core.cpp index c6ce9ea48..ff4ccc98e 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -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 diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index c863a8d4d..d38cb31ba 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -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), diff --git a/library/include/modules/Filesystem.h b/library/include/modules/Filesystem.h index c9218ba33..eac8a1102 100644 --- a/library/include/modules/Filesystem.h +++ b/library/include/modules/Filesystem.h @@ -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); diff --git a/library/modules/Filesystem.cpp b/library/modules/Filesystem.cpp index c0d0860ad..7738bfd5e 100644 --- a/library/modules/Filesystem.cpp +++ b/library/modules/Filesystem.cpp @@ -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;