Skip to content

Commit 41b366c

Browse files
author
Daniel Kroening
committed
added file_remove(path) as wrapper for unlink(path)
Added file_remove, the obvious wrapper around the OS-specific unlink. This will be replaced by std::filesystem::remove once we have C++17.
1 parent af7c6a5 commit 41b366c

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,12 @@ bool file_exists(const std::string &path)
212212
return access(path.c_str(), F_OK) == 0;
213213
#endif
214214
}
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ bool create_directory(const std::string &path);
3737
/// \return true iff the file exists
3838
bool file_exists(const std::string &path);
3939

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+
4045
#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)