Skip to content

Commit c11b81c

Browse files
authored
Merge pull request #3697 from tautschnig/chdir
set_working_directory to wrap chdir
2 parents f57f2b8 + ce61fda commit c11b81c

File tree

5 files changed

+41
-27
lines changed

5 files changed

+41
-27
lines changed

src/goto-cc/compile.cpp

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,6 @@ Date: June 2006
4747
"size=\"30,40\";"\
4848
"ratio=compress;"
4949

50-
// the following are for chdir
51-
52-
#if defined(__linux__) || \
53-
defined(__FreeBSD_kernel__) || \
54-
defined(__GNU__) || \
55-
defined(__unix__) || \
56-
defined(__CYGWIN__) || \
57-
defined(__MACH__)
58-
#include <unistd.h>
59-
#endif
60-
6150
#ifdef _WIN32
6251
#include <util/pragma_push.def>
6352
#ifdef _MSC_VER
@@ -66,7 +55,6 @@ Date: June 2006
6655
#endif
6756
#include <direct.h>
6857
#include <windows.h>
69-
#define chdir _chdir
7058
#define popen _popen
7159
#define pclose _pclose
7260
#include <util/pragma_pop.def>
@@ -240,18 +228,8 @@ bool compilet::add_files_from_archive(
240228
{
241229
tstr = get_temporary_directory("goto-cc.XXXXXX");
242230

243-
if(tstr=="")
244-
{
245-
error() << "Cannot create temporary directory" << eom;
246-
return true;
247-
}
248-
249231
tmp_dirs.push_back(tstr);
250-
if(chdir(tmp_dirs.back().c_str())!=0)
251-
{
252-
error() << "Cannot switch to temporary directory" << eom;
253-
return true;
254-
}
232+
set_current_path(tmp_dirs.back());
255233

256234
// unpack now
257235
cmd << "ar x " << concat_dir_file(working_directory, file_name);
@@ -294,8 +272,8 @@ bool compilet::add_files_from_archive(
294272
pclose(stream);
295273
}
296274

297-
if(!thin_archive && chdir(working_directory.c_str()) != 0)
298-
error() << "Could not change back to working directory" << eom;
275+
if(!thin_archive)
276+
set_current_path(working_directory);
299277

300278
return false;
301279
}

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)