Skip to content

Commit 41951a1

Browse files
committed
set_current_path to wrap chdir
chdir is POSIX and will eventually be implemented via std::filesystem::current_path. Moving this functionality to util helps removing OS-specific parts from other areas of the code base. Furthermore make get_temporary_directory return an absolute, resolved path.
1 parent f57f2b8 commit 41951a1

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

src/util/file_util.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Date: January 2012
4141
#include <windows.h>
4242
#include <direct.h>
4343
#include <util/unicode.h>
44+
#define chdir _chdir
4445
#include <util/pragma_pop.def>
4546
#endif
4647

@@ -68,6 +69,15 @@ std::string get_current_working_directory()
6869
return working_directory;
6970
}
7071

72+
/// Set working directory.
73+
/// \param path: New working directory to change to
74+
void set_current_path(const std::string &path)
75+
{
76+
if(chdir(path.c_str()) != 0)
77+
throw system_exceptiont(
78+
std::string("chdir failed: ") + std::strerror(errno));
79+
}
80+
7181
/// deletes all files in 'path' and then the directory itself
7282
#ifdef _WIN32
7383

src/util/file_util.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ Author: Daniel Kroening, [email protected]
1515
// C++17 will allow us to use std::filesystem::path::remove_all
1616
void delete_directory(const std::string &path);
1717

18-
// C++17 will allow us to use std::filesystem::current_path
18+
// C++17 will allow us to use std::filesystem::current_path (for both get and
19+
// set)
1920
std::string get_current_working_directory();
21+
void set_current_path(const std::string &path);
2022

2123
// C++17 will allow us to use std::filesystem::path(dir).append(file)
2224
std::string concat_dir_file(const std::string &directory,

src/util/tempdir.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Author: CM Wintersteiger
2121
#endif
2222

2323
#include <cstdlib>
24+
#include <cstring>
2425
#include <vector>
2526

2627
#if defined(__linux__) || \
@@ -86,7 +87,16 @@ std::string get_temporary_directory(const std::string &name_template)
8687
const char *td = mkdtemp(t.data());
8788
if(!td)
8889
throw system_exceptiont("Failed to create temporary directory");
89-
result = std::string(td);
90+
91+
errno=0;
92+
char *wd = realpath(td, nullptr);
93+
94+
if(wd == nullptr || errno != 0)
95+
throw system_exceptiont(
96+
std::string("realpath failed: ") + std::strerror(errno));
97+
98+
result = std::string(wd);
99+
free(wd);
90100
#endif
91101

92102
return result;

unit/util/file_util.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Author: Daniel Kroening
88

99
#include <testing-utils/catch.hpp>
1010

11+
#include <util/exception_utils.h>
1112
#include <util/file_util.h>
1213
#include <util/tempdir.h>
1314
#include <util/unicode.h>
@@ -53,3 +54,16 @@ TEST_CASE("is_directory functionality", "[core][util][file_util]")
5354
REQUIRE(!is_directory(temp_dir("file")));
5455
REQUIRE(!is_directory(""));
5556
}
57+
58+
TEST_CASE("get/set working directory", "[core][util][file_util]")
59+
{
60+
temp_dirt temp_dir("testXXXXXX");
61+
62+
std::string cwd = get_current_working_directory();
63+
REQUIRE(cwd != temp_dir.path);
64+
set_current_path(temp_dir.path);
65+
REQUIRE(get_current_working_directory() == temp_dir.path);
66+
REQUIRE_THROWS_AS(set_current_path("no-such-dir"), system_exceptiont);
67+
68+
set_current_path(cwd);
69+
}

0 commit comments

Comments
 (0)