Skip to content

set_working_directory to wrap chdir #3697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 3 additions & 25 deletions src/goto-cc/compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,6 @@ Date: June 2006
"size=\"30,40\";"\
"ratio=compress;"

// the following are for chdir

#if defined(__linux__) || \
defined(__FreeBSD_kernel__) || \
defined(__GNU__) || \
defined(__unix__) || \
defined(__CYGWIN__) || \
defined(__MACH__)
#include <unistd.h>
#endif

#ifdef _WIN32
#include <util/pragma_push.def>
#ifdef _MSC_VER
Expand All @@ -66,7 +55,6 @@ Date: June 2006
#endif
#include <direct.h>
#include <windows.h>
#define chdir _chdir
#define popen _popen
#define pclose _pclose
#include <util/pragma_pop.def>
Expand Down Expand Up @@ -240,18 +228,8 @@ bool compilet::add_files_from_archive(
{
tstr = get_temporary_directory("goto-cc.XXXXXX");

if(tstr=="")
{
error() << "Cannot create temporary directory" << eom;
return true;
}

tmp_dirs.push_back(tstr);
if(chdir(tmp_dirs.back().c_str())!=0)
{
error() << "Cannot switch to temporary directory" << eom;
return true;
}
set_current_path(tmp_dirs.back());

// unpack now
cmd << "ar x " << concat_dir_file(working_directory, file_name);
Expand Down Expand Up @@ -294,8 +272,8 @@ bool compilet::add_files_from_archive(
pclose(stream);
}

if(!thin_archive && chdir(working_directory.c_str()) != 0)
error() << "Could not change back to working directory" << eom;
if(!thin_archive)
set_current_path(working_directory);

return false;
}
Expand Down
10 changes: 10 additions & 0 deletions src/util/file_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Date: January 2012
#include <windows.h>
#include <direct.h>
#include <util/unicode.h>
#define chdir _chdir
#include <util/pragma_pop.def>
#endif

Expand Down Expand Up @@ -68,6 +69,15 @@ std::string get_current_working_directory()
return working_directory;
}

/// Set working directory.
/// \param path: New working directory to change to
void set_current_path(const std::string &path)
{
if(chdir(path.c_str()) != 0)
throw system_exceptiont(
std::string("chdir failed: ") + std::strerror(errno));
}

/// deletes all files in 'path' and then the directory itself
#ifdef _WIN32

Expand Down
4 changes: 3 additions & 1 deletion src/util/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ Author: Daniel Kroening, [email protected]
// C++17 will allow us to use std::filesystem::path::remove_all
void delete_directory(const std::string &path);

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably call that function set_current_path, to mimick the naming in C++17.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

// C++17 will allow us to use std::filesystem::path(dir).append(file)
std::string concat_dir_file(const std::string &directory,
Expand Down
12 changes: 11 additions & 1 deletion src/util/tempdir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Author: CM Wintersteiger
#endif

#include <cstdlib>
#include <cstring>
#include <vector>

#if defined(__linux__) || \
Expand Down Expand Up @@ -86,7 +87,16 @@ std::string get_temporary_directory(const std::string &name_template)
const char *td = mkdtemp(t.data());
if(!td)
throw system_exceptiont("Failed to create temporary directory");
result = std::string(td);

errno = 0;
char *wd = realpath(td, nullptr);

if(wd == nullptr || errno != 0)
throw system_exceptiont(
std::string("realpath failed: ") + std::strerror(errno));

result = std::string(wd);
free(wd);
#endif

return result;
Expand Down
14 changes: 14 additions & 0 deletions unit/util/file_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Author: Daniel Kroening

#include <testing-utils/catch.hpp>

#include <util/exception_utils.h>
#include <util/file_util.h>
#include <util/tempdir.h>
#include <util/unicode.h>
Expand Down Expand Up @@ -53,3 +54,16 @@ TEST_CASE("is_directory functionality", "[core][util][file_util]")
REQUIRE(!is_directory(temp_dir("file")));
REQUIRE(!is_directory(""));
}

TEST_CASE("get/set working directory", "[core][util][file_util]")
{
temp_dirt temp_dir("testXXXXXX");

std::string cwd = get_current_working_directory();
REQUIRE(cwd != temp_dir.path);
set_current_path(temp_dir.path);
REQUIRE(get_current_working_directory() == temp_dir.path);
REQUIRE_THROWS_AS(set_current_path("no-such-dir"), system_exceptiont);

set_current_path(cwd);
}