Skip to content

add file_exists(path) #5215

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 2 commits into from
Feb 21, 2020
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
9 changes: 5 additions & 4 deletions src/goto-cc/hybrid_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Author: Michael Tautschnig, 2018

#include "hybrid_binary.h"

#include <util/file_util.h>
#include <util/run.h>
#include <util/suffix.h>

Expand Down Expand Up @@ -64,8 +65,8 @@ int hybrid_binary(
}

// delete the goto binary
int remove_result = remove(goto_binary_file.c_str());
if(remove_result != 0)
bool remove_result = file_remove(goto_binary_file);
Copy link
Member

Choose a reason for hiding this comment

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

⛏ const

if(!remove_result)
{
message.error() << "Remove failed: " << std::strerror(errno)
<< messaget::eom;
Expand Down Expand Up @@ -124,8 +125,8 @@ int hybrid_binary(
}

// delete the goto binary
int remove_result = remove(goto_binary_file.c_str());
if(remove_result != 0)
bool remove_result = file_remove(goto_binary_file);
Copy link
Member

Choose a reason for hiding this comment

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

⛏ const

if(!remove_result)
{
message.error() << "Remove failed: " << std::strerror(errno)
<< messaget::eom;
Expand Down
18 changes: 18 additions & 0 deletions src/util/file_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,21 @@ bool create_directory(const std::string &path)
return mkdir(path.c_str(), 0777) == 0;
#endif
}

bool file_exists(const std::string &path)
{
#ifdef _WIN32
return _waccess(utf8_to_utf16_native_endian(path).c_str(), 0) == 0;
#else
return access(path.c_str(), F_OK) == 0;
#endif
}

bool file_remove(const std::string &path)
{
#ifdef _WIN32
return _wunlink(utf8_to_utf16_native_endian(path).c_str()) == 0;
#else
return unlink(path.c_str()) == 0;
#endif
}
10 changes: 10 additions & 0 deletions src/util/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ bool is_directory(const std::string &path);
/// \return true iff the directory was created
bool create_directory(const std::string &path);

/// Check whether file with given path exists.
/// C++17 will allow us to use std::filesystem::directory_entry(file).exists()
/// \return true iff the file exists
bool file_exists(const std::string &path);

// Delete a file with given path
/// C++17 will allow us to use std::filesystem::remove
/// \return true if the file was deleted, false if it did not exist
bool file_remove(const std::string &path);

#endif // CPROVER_UTIL_FILE_UTIL_H
3 changes: 2 additions & 1 deletion src/util/tempfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Author: Daniel Kroening
#include <cstring>

#include "exception_utils.h"
#include "file_util.h"

#if defined(__linux__) || \
defined(__FreeBSD_kernel__) || \
Expand Down Expand Up @@ -143,5 +144,5 @@ std::string get_temporary_file(
temporary_filet::~temporary_filet()
{
if(!name.empty())
std::remove(name.c_str());
file_remove(name);
}