aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2010-08-19 18:45:40 +0100
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2010-08-21 14:58:16 +0100
commit1bb84c09850dcf2136bc9134ead3409a3ccaf547 (patch)
treec740028982d8655ab4fa337dc34509f8b8328999
parent625454e26c1ea5fa5640135d34f043adeec54527 (diff)
downloadpaludis-1bb84c09850dcf2136bc9134ead3409a3ccaf547.tar.gz
paludis-1bb84c09850dcf2136bc9134ead3409a3ccaf547.tar.xz
Process::chdir
-rw-r--r--paludis/util/process.cc14
-rw-r--r--paludis/util/process.hh2
-rw-r--r--paludis/util/process_TEST.cc17
3 files changed, 33 insertions, 0 deletions
diff --git a/paludis/util/process.cc b/paludis/util/process.cc
index 1458124..0da85b0 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 b6699f6..6e71264 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 9688c07..a882151 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;
}