diff options
-rw-r--r-- | paludis/util/process.cc | 14 | ||||
-rw-r--r-- | paludis/util/process.hh | 2 | ||||
-rw-r--r-- | paludis/util/process_TEST.cc | 17 |
3 files changed, 33 insertions, 0 deletions
diff --git a/paludis/util/process.cc b/paludis/util/process.cc index 145812497..0da85b05e 100644 --- a/paludis/util/process.cc +++ b/paludis/util/process.cc @@ -21,6 +21,8 @@ #include <paludis/util/pimp-impl.hh> #include <paludis/util/thread.hh> #include <paludis/util/pipe.hh> +#include <paludis/util/fs_entry.hh> +#include <paludis/util/stringify.hh> #include <iostream> #include <functional> @@ -206,6 +208,7 @@ namespace paludis std::ostream * capture_stderr; std::map<std::string, std::string> setenvs; + std::string chdir; Imp(ProcessCommand && c) : command(std::move(c)), @@ -270,6 +273,10 @@ Process::run() m != m_end ; ++m) ::setenv(m->first.c_str(), m->second.c_str(), 1); + if (! _imp->chdir.empty()) + if (-1 == ::chdir(_imp->chdir.c_str())) + throw ProcessError("chdir() failed"); + _imp->command.exec(); } catch (const ProcessError & e) @@ -309,6 +316,13 @@ Process::setenv(const std::string & a, const std::string & b) return *this; } +Process & +Process::chdir(const FSEntry & f) +{ + _imp->chdir = stringify(f.realpath_if_exists()); + return *this; +} + namespace paludis { template <> diff --git a/paludis/util/process.hh b/paludis/util/process.hh index b6699f635..6e712647f 100644 --- a/paludis/util/process.hh +++ b/paludis/util/process.hh @@ -24,6 +24,7 @@ #include <paludis/util/attributes.hh> #include <paludis/util/pimp.hh> #include <paludis/util/exception.hh> +#include <paludis/util/fs_entry-fwd.hh> #include <string> #include <iosfwd> @@ -74,6 +75,7 @@ namespace paludis Process & capture_stdout(std::ostream &); Process & capture_stderr(std::ostream &); Process & setenv(const std::string &, const std::string &); + Process & chdir(const FSEntry &); }; class PALUDIS_VISIBLE RunningProcessHandle : diff --git a/paludis/util/process_TEST.cc b/paludis/util/process_TEST.cc index 9688c07c7..a882151c3 100644 --- a/paludis/util/process_TEST.cc +++ b/paludis/util/process_TEST.cc @@ -18,6 +18,7 @@ */ #include <paludis/util/process.hh> +#include <paludis/util/fs_entry.hh> #include <test/test_framework.hh> #include <test/test_runner.hh> #include <sstream> @@ -161,5 +162,21 @@ namespace test_cases TEST_CHECK_EQUAL(stdout_stream.str(), "in space\n"); } } test_setenv; + + struct ChdirTest : TestCase + { + ChdirTest() : TestCase("chdir") { } + + void run() + { + std::stringstream stdout_stream; + Process pwd_process(ProcessCommand({"pwd"})); + pwd_process.capture_stdout(stdout_stream); + pwd_process.chdir(FSEntry("/")); + + TEST_CHECK_EQUAL(pwd_process.run().wait(), 0); + TEST_CHECK_EQUAL(stdout_stream.str(), "/\n"); + } + } test_chdir; } |