Skip to content

Commit ac74e68

Browse files
author
Daniel Kroening
authored
Merge pull request #5215 from diffblue/file-exists
add file_exists(path)
2 parents 9bde863 + 41b366c commit ac74e68

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

src/goto-cc/hybrid_binary.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Author: Michael Tautschnig, 2018
1111

1212
#include "hybrid_binary.h"
1313

14+
#include <util/file_util.h>
1415
#include <util/run.h>
1516
#include <util/suffix.h>
1617

@@ -64,8 +65,8 @@ int hybrid_binary(
6465
}
6566

6667
// delete the goto binary
67-
int remove_result = remove(goto_binary_file.c_str());
68-
if(remove_result != 0)
68+
bool remove_result = file_remove(goto_binary_file);
69+
if(!remove_result)
6970
{
7071
message.error() << "Remove failed: " << std::strerror(errno)
7172
<< messaget::eom;
@@ -124,8 +125,8 @@ int hybrid_binary(
124125
}
125126

126127
// delete the goto binary
127-
int remove_result = remove(goto_binary_file.c_str());
128-
if(remove_result != 0)
128+
bool remove_result = file_remove(goto_binary_file);
129+
if(!remove_result)
129130
{
130131
message.error() << "Remove failed: " << std::strerror(errno)
131132
<< messaget::eom;

src/util/file_util.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,21 @@ bool create_directory(const std::string &path)
203203
return mkdir(path.c_str(), 0777) == 0;
204204
#endif
205205
}
206+
207+
bool file_exists(const std::string &path)
208+
{
209+
#ifdef _WIN32
210+
return _waccess(utf8_to_utf16_native_endian(path).c_str(), 0) == 0;
211+
#else
212+
return access(path.c_str(), F_OK) == 0;
213+
#endif
214+
}
215+
216+
bool file_remove(const std::string &path)
217+
{
218+
#ifdef _WIN32
219+
return _wunlink(utf8_to_utf16_native_endian(path).c_str()) == 0;
220+
#else
221+
return unlink(path.c_str()) == 0;
222+
#endif
223+
}

src/util/file_util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@ bool is_directory(const std::string &path);
3232
/// \return true iff the directory was created
3333
bool create_directory(const std::string &path);
3434

35+
/// Check whether file with given path exists.
36+
/// C++17 will allow us to use std::filesystem::directory_entry(file).exists()
37+
/// \return true iff the file exists
38+
bool file_exists(const std::string &path);
39+
40+
// Delete a file with given path
41+
/// C++17 will allow us to use std::filesystem::remove
42+
/// \return true if the file was deleted, false if it did not exist
43+
bool file_remove(const std::string &path);
44+
3545
#endif // CPROVER_UTIL_FILE_UTIL_H

src/util/tempfile.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Author: Daniel Kroening
3434
#include <cstring>
3535

3636
#include "exception_utils.h"
37+
#include "file_util.h"
3738

3839
#if defined(__linux__) || \
3940
defined(__FreeBSD_kernel__) || \
@@ -143,5 +144,5 @@ std::string get_temporary_file(
143144
temporary_filet::~temporary_filet()
144145
{
145146
if(!name.empty())
146-
std::remove(name.c_str());
147+
file_remove(name);
147148
}

0 commit comments

Comments
 (0)