diff --git a/jbmc/src/java_bytecode/java_class_loader_base.cpp b/jbmc/src/java_bytecode/java_class_loader_base.cpp index 5a998bc4d636..2c3952719106 100644 --- a/jbmc/src/java_bytecode/java_class_loader_base.cpp +++ b/jbmc/src/java_bytecode/java_class_loader_base.cpp @@ -8,15 +8,15 @@ Author: Daniel Kroening, kroening@kroening.com #include "java_class_loader_base.h" -#include "jar_file.h" -#include "java_bytecode_parse_tree.h" -#include "java_bytecode_parser.h" - -#include #include #include #include +#include "jar_file.h" +#include "java_bytecode_parse_tree.h" +#include "java_bytecode_parser.h" + +#include #include void java_class_loader_baset::add_classpath_entry( @@ -40,7 +40,7 @@ void java_class_loader_baset::add_classpath_entry( } else { - if(is_directory(path)) + if(std::filesystem::is_directory(path)) { classpath_entries.push_back( classpath_entryt(classpath_entryt::DIRECTORY, path)); @@ -197,7 +197,8 @@ java_class_loader_baset::get_class_from_directory( { // Look in the given directory const std::string class_file = class_name_to_os_file(class_name); - const std::string full_path = concat_dir_file(path, class_file); + const std::string full_path = + std::filesystem::path(path).append(class_file).string(); if(std::ifstream(full_path)) { diff --git a/jbmc/unit/java-testing-utils/load_java_class.cpp b/jbmc/unit/java-testing-utils/load_java_class.cpp index e1f1fcf787b4..b8568719dc5b 100644 --- a/jbmc/unit/java-testing-utils/load_java_class.cpp +++ b/jbmc/unit/java-testing-utils/load_java_class.cpp @@ -8,19 +8,18 @@ Author: Diffblue Ltd. #include "load_java_class.h" -#include -#include -#include -#include - #include #include #include +#include #include +#include +#include +#include -#include -#include +#include +#include /// Go through the process of loading, type-checking and finalising loading a /// specific class file to build the symbol table. The functions are converted @@ -149,7 +148,7 @@ goto_modelt load_goto_model_from_java_class( // Log the working directory to help people identify the common error // of wrong working directory (should be the `unit` directory when running // the unit tests). - std::string path = get_current_working_directory(); + std::string path = std::filesystem::current_path().string(); INFO("Working directory: " << path); // if this fails it indicates the class was not loaded diff --git a/src/goto-analyzer/unreachable_instructions.cpp b/src/goto-analyzer/unreachable_instructions.cpp index 6ff7822bed79..5fc03e49f917 100644 --- a/src/goto-analyzer/unreachable_instructions.cpp +++ b/src/goto-analyzer/unreachable_instructions.cpp @@ -13,7 +13,6 @@ Date: April 2016 #include "unreachable_instructions.h" -#include #include #include #include @@ -23,6 +22,8 @@ Date: April 2016 #include #include +#include + typedef std::map dead_mapt; static void unreachable_instructions( @@ -106,9 +107,10 @@ file_name_string_opt(const source_locationt &source_location) if(source_location.get_file().empty()) return {}; - return concat_dir_file( - id2string(source_location.get_working_directory()), - id2string(source_location.get_file())); + return std::filesystem::path( + id2string(source_location.get_working_directory())) + .append(id2string(source_location.get_file())) + .string(); } static void add_to_json( diff --git a/src/goto-cc/as_mode.cpp b/src/goto-cc/as_mode.cpp index d811a757bc23..fee687b2a9ee 100644 --- a/src/goto-cc/as_mode.cpp +++ b/src/goto-cc/as_mode.cpp @@ -21,7 +21,6 @@ Author: Michael Tautschnig #include #include -#include #include #include #include @@ -31,6 +30,7 @@ Author: Michael Tautschnig #include "goto_cc_cmdline.h" #include "hybrid_binary.h" +#include #include // IWYU pragma: keep #include @@ -303,9 +303,9 @@ int as_modet::as_hybrid_binary(const compilet &compiler) std::string saved = output_file + ".goto-cc-saved"; try { - file_rename(output_file, saved); + std::filesystem::rename(output_file, saved); } - catch(const cprover_exception_baset &e) + catch(const std::filesystem::filesystem_error &e) { log.error() << "Rename failed: " << e.what() << messaget::eom; return 1; diff --git a/src/goto-cc/compile.cpp b/src/goto-cc/compile.cpp index 7823142a6b76..54623f731350 100644 --- a/src/goto-cc/compile.cpp +++ b/src/goto-cc/compile.cpp @@ -15,7 +15,6 @@ Date: June 2006 #include #include -#include #include #include #include @@ -39,6 +38,7 @@ Date: June 2006 #include #include +#include #include #include @@ -217,11 +217,14 @@ bool compilet::add_files_from_archive( tstr = get_temporary_directory("goto-cc.XXXXXX"); tmp_dirs.push_back(tstr); - set_current_path(tmp_dirs.back()); + std::filesystem::current_path(tmp_dirs.back()); // unpack now - int ret = - run("ar", {"ar", "x", concat_dir_file(working_directory, file_name)}); + int ret = run( + "ar", + {"ar", + "x", + std::filesystem::path(working_directory).append(file_name).string()}); if(ret != 0) { log.error() << "Failed to extract archive " << file_name << messaget::eom; @@ -233,7 +236,9 @@ bool compilet::add_files_from_archive( temporary_filet tmp_file_out("", ""); int ret = run( "ar", - {"ar", "t", concat_dir_file(working_directory, file_name)}, + {"ar", + "t", + std::filesystem::path(working_directory).append(file_name).string()}, "", tmp_file_out(), ""); @@ -248,7 +253,7 @@ bool compilet::add_files_from_archive( while(!in.fail() && std::getline(in, line)) { - std::string t = concat_dir_file(tstr, line); + std::string t = std::filesystem::path(tstr).append(line).string(); if(is_goto_binary(t, log.get_message_handler())) object_files.push_back(t); @@ -258,7 +263,7 @@ bool compilet::add_files_from_archive( } if(!thin_archive) - set_current_path(working_directory); + std::filesystem::current_path(working_directory); return false; } @@ -272,7 +277,8 @@ bool compilet::find_library(const std::string &name) for(const auto &library_path : library_paths) { - library_file_name = concat_dir_file(library_path, "lib" + name + ".a"); + library_file_name = + std::filesystem::path(library_path).append("lib" + name + ".a").string(); std::ifstream in(library_file_name); @@ -280,7 +286,9 @@ bool compilet::find_library(const std::string &name) return !add_input_file(library_file_name); else { - library_file_name = concat_dir_file(library_path, "lib" + name + ".so"); + library_file_name = std::filesystem::path(library_path) + .append("lib" + name + ".so") + .string(); switch(detect_file_type(library_file_name, log.get_message_handler())) { @@ -409,7 +417,11 @@ optionalt compilet::compile() get_base_name(file_name, true) + "." + object_file_extension; if(!output_directory_object.empty()) - cfn = concat_dir_file(output_directory_object, file_name_with_obj_ext); + { + cfn = std::filesystem::path(output_directory_object) + .append(file_name_with_obj_ext) + .string(); + } else cfn = file_name_with_obj_ext; } @@ -648,7 +660,7 @@ compilet::compilet(cmdlinet &_cmdline, message_handlert &mh, bool Werror) mode=COMPILE_LINK_EXECUTABLE; echo_file_name=false; wrote_object=false; - working_directory=get_current_working_directory(); + working_directory = std::filesystem::current_path().string(); if(cmdline.isset("export-function-local-symbols")) { @@ -665,7 +677,7 @@ compilet::~compilet() // clean up temp dirs for(const auto &dir : tmp_dirs) - delete_directory(dir); + std::filesystem::remove_all(dir); } std::size_t compilet::function_body_count(const goto_functionst &functions) diff --git a/src/goto-cc/gcc_mode.cpp b/src/goto-cc/gcc_mode.cpp index 5459ad361c1f..0c9d6d50cbf5 100644 --- a/src/goto-cc/gcc_mode.cpp +++ b/src/goto-cc/gcc_mode.cpp @@ -21,7 +21,6 @@ Author: CM Wintersteiger, 2006 #include #include -#include #include #include #include @@ -35,6 +34,7 @@ Author: CM Wintersteiger, 2006 #include "hybrid_binary.h" #include "linker_script_merge.h" +#include #include // IWYU pragma: keep #include #include @@ -1006,9 +1006,9 @@ int gcc_modet::gcc_hybrid_binary(compilet &compiler) try { - file_rename(*it, bin_name); + std::filesystem::rename(*it, bin_name); } - catch(const cprover_exception_baset &e) + catch(const std::filesystem::filesystem_error &e) { log.error() << "Rename failed: " << e.what() << messaget::eom; return 1; diff --git a/src/goto-cc/hybrid_binary.cpp b/src/goto-cc/hybrid_binary.cpp index 745fa3cd478a..434c08c34d3b 100644 --- a/src/goto-cc/hybrid_binary.cpp +++ b/src/goto-cc/hybrid_binary.cpp @@ -11,12 +11,12 @@ Author: Michael Tautschnig, 2018 #include "hybrid_binary.h" -#include #include #include #include #include +#include #if defined(__APPLE__) # include @@ -80,7 +80,7 @@ int hybrid_binary( } // delete the goto binary - bool remove_result = file_remove(goto_binary_file); + bool remove_result = std::filesystem::remove(goto_binary_file); if(!remove_result) { message.error() << "Remove failed: " << std::strerror(errno) @@ -140,7 +140,7 @@ int hybrid_binary( } // delete the goto binary - bool remove_result = file_remove(goto_binary_file); + bool remove_result = std::filesystem::remove(goto_binary_file); if(!remove_result) { message.error() << "Remove failed: " << std::strerror(errno) diff --git a/src/goto-cc/ld_mode.cpp b/src/goto-cc/ld_mode.cpp index b443a4ef033a..bee843fa3fa6 100644 --- a/src/goto-cc/ld_mode.cpp +++ b/src/goto-cc/ld_mode.cpp @@ -19,13 +19,8 @@ Author: CM Wintersteiger, 2006 #include #endif -#include -#include -#include - #include #include -#include #include #include @@ -34,6 +29,11 @@ Author: CM Wintersteiger, 2006 #include "hybrid_binary.h" #include "linker_script_merge.h" +#include +#include +#include +#include + static std::string linker_name(const cmdlinet &cmdline, const std::string &base_name) { @@ -183,9 +183,9 @@ int ld_modet::ld_hybrid_binary( try { - file_rename(output_file, goto_binary); + std::filesystem::rename(output_file, goto_binary); } - catch(const cprover_exception_baset &e) + catch(const std::filesystem::filesystem_error &e) { log.error() << "Rename failed: " << e.what() << messaget::eom; return 1; diff --git a/src/goto-cc/ms_cl_mode.cpp b/src/goto-cc/ms_cl_mode.cpp index ea9193a800b9..bbc78a165b1f 100644 --- a/src/goto-cc/ms_cl_mode.cpp +++ b/src/goto-cc/ms_cl_mode.cpp @@ -19,16 +19,16 @@ Author: CM Wintersteiger, 2006 #include #endif -#include - #include -#include #include #include #include "compile.h" #include "ms_cl_version.h" +#include +#include + static bool has_directory_suffix(const std::string &path) { // MS CL decides whether a parameter is a directory on the @@ -128,7 +128,7 @@ int ms_cl_modet::doit() { compiler.output_directory_object = Fo_value; - if(!is_directory(Fo_value)) + if(!std::filesystem::is_directory(Fo_value)) log.warning() << "not a directory: " << Fo_value << messaget::eom; } else @@ -154,7 +154,7 @@ int ms_cl_modet::doit() has_directory_suffix(compiler.output_file_executable) && cmdline.args.size() >= 1) { - if(!is_directory(compiler.output_file_executable)) + if(!std::filesystem::is_directory(compiler.output_file_executable)) { log.warning() << "not a directory: " << compiler.output_file_executable << messaget::eom; diff --git a/src/goto-instrument/count_eloc.cpp b/src/goto-instrument/count_eloc.cpp index 51bf3e825d4c..a2185e2f9ab6 100644 --- a/src/goto-instrument/count_eloc.cpp +++ b/src/goto-instrument/count_eloc.cpp @@ -13,10 +13,6 @@ Date: December 2012 #include "count_eloc.h" -#include -#include - -#include #include #include #include @@ -26,6 +22,10 @@ Date: December 2012 #include +#include +#include +#include + typedef std::unordered_set linest; typedef std::unordered_map filest; typedef std::unordered_map working_dirst; @@ -75,7 +75,8 @@ void list_eloc(const goto_modelt &goto_model) { std::string file=id2string(lines.first); if(!files.first.empty()) - file=concat_dir_file(id2string(files.first), file); + file = + std::filesystem::path(id2string(files.first)).append(file).string(); for(const irep_idt &line : lines.second) std::cout << file << ':' << line << '\n'; diff --git a/src/util/Makefile b/src/util/Makefile index 30452e2e3029..1e33ebc73db6 100644 --- a/src/util/Makefile +++ b/src/util/Makefile @@ -17,7 +17,6 @@ SRC = arith_tools.cpp \ expr_initializer.cpp \ expr_util.cpp \ exception_utils.cpp \ - file_util.cpp \ find_macros.cpp \ find_symbols.cpp \ fixedbv.cpp \ diff --git a/src/util/file_util.cpp b/src/util/file_util.cpp deleted file mode 100644 index d1cd3378a052..000000000000 --- a/src/util/file_util.cpp +++ /dev/null @@ -1,274 +0,0 @@ -/*******************************************************************\ - -Module: File Utilities - -Author: - -Date: January 2012 - -\*******************************************************************/ - -/// \file -/// File Utilities - -#include "file_util.h" - -#include "exception_utils.h" - -#include -#include - -#if defined(__linux__) || \ - defined(__FreeBSD_kernel__) || \ - defined(__GNU__) || \ - defined(__unix__) || \ - defined(__CYGWIN__) || \ - defined(__MACH__) -#include -#include -#include -#include -#include -#endif - -#ifdef _WIN32 -#include -#ifdef _MSC_VER -#pragma warning(disable:4668) - // using #if/#elif on undefined macro -#pragma warning(disable : 5039) -// pointer or reference to potentially throwing function passed to extern C -#endif -#include -#include -#include -#include -#define chdir _chdir -#include -#endif - -/// \return current working directory -std::string get_current_working_directory() -{ -#ifndef _WIN32 - errno=0; - char *wd=realpath(".", nullptr); - - if(wd == nullptr) - throw system_exceptiont( - std::string("realpath failed: ") + std::strerror(errno)); - - std::string working_directory=wd; - free(wd); -#else - TCHAR buffer[4096]; - DWORD retval=GetCurrentDirectory(4096, buffer); - if(retval == 0) - throw system_exceptiont("failed to get current directory of process"); - -# ifdef UNICODE - std::string working_directory(narrow(buffer)); -# else - std::string working_directory(buffer); -# endif - -#endif - - 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 - -void delete_directory_utf16(const std::wstring &path) -{ - std::wstring pattern=path + L"\\*"; - // NOLINTNEXTLINE(readability/identifiers) - struct _wfinddata_t info; - intptr_t hFile=_wfindfirst(pattern.c_str(), &info); - if(hFile!=-1) - { - do - { - if(wcscmp(info.name, L".")==0 || wcscmp(info.name, L"..")==0) - continue; - std::wstring sub_path=path+L"\\"+info.name; - if(info.attrib & _A_SUBDIR) - delete_directory_utf16(sub_path); - else - DeleteFileW(sub_path.c_str()); - } - while(_wfindnext(hFile, &info)==0); - _findclose(hFile); - RemoveDirectoryW(path.c_str()); - } -} - -#endif - -void delete_directory(const std::string &path) -{ -#ifdef _WIN32 - delete_directory_utf16(utf8_to_utf16_native_endian(path)); -#else - DIR *dir=opendir(path.c_str()); - if(dir!=nullptr) - { - struct dirent *ent; - while((ent=readdir(dir))!=nullptr) - { - // Needed for Alpine Linux - if(strcmp(ent->d_name, ".")==0 || strcmp(ent->d_name, "..")==0) - continue; - - std::string sub_path=path+"/"+ent->d_name; - - struct stat stbuf; - int result=stat(sub_path.c_str(), &stbuf); - if(result!=0) - throw system_exceptiont( - std::string("Stat failed: ") + std::strerror(errno)); - - if(S_ISDIR(stbuf.st_mode)) - delete_directory(sub_path); - else - { - result=remove(sub_path.c_str()); - if(result!=0) - throw system_exceptiont( - std::string("Remove failed: ") + std::strerror(errno)); - } - } - closedir(dir); - } - rmdir(path.c_str()); -#endif -} - -/// \par parameters: directory name and file name -/// \return concatenation of directory and file, if the file path is relative -std::string concat_dir_file( - const std::string &directory, - const std::string &file_name) -{ -#ifdef _WIN32 - if( - file_name.size() > 1 && file_name[0] != '/' && file_name[0] != '\\' && - file_name[1] == ':') - { - return file_name; - } - else if( - !directory.empty() && (directory.back() == '/' || directory.back() == '\\')) - { - return directory + file_name; - } - else - return directory + '\\' + file_name; -#else - if(!file_name.empty() && file_name[0] == '/') - return file_name; - else if(!directory.empty() && directory.back() == '/') - return directory + file_name; - else - return directory + '/' + file_name; -#endif -} - -bool is_directory(const std::string &path) -{ - if(path.empty()) - return false; - -#ifdef _WIN32 - - auto attributes = ::GetFileAttributesW(widen(path).c_str()); - if (attributes == INVALID_FILE_ATTRIBUTES) - return false; - else - return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0; - -#else - - struct stat buf; - - if(stat(path.c_str(), &buf)!=0) - return false; - else - return (buf.st_mode & S_IFDIR) != 0; - -#endif -} - -bool create_directory(const std::string &path) -{ -#ifdef _WIN32 - return _mkdir(path.c_str()) == 0; -#else - // the umask matches what std::filesystem::create_directory does - 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 -} - -void file_rename(const std::string &old_path, const std::string &new_path) -{ -#ifdef _WIN32 - if(is_directory(old_path)) - { - // rename() only renames directories, but does not move them. - // MoveFile is not atomic. - auto MoveFile_result = - MoveFileW(widen(old_path).c_str(), widen(new_path).c_str()); - - if(MoveFile_result == 0) - throw system_exceptiont("MoveFileW failed"); - } - else - { - // C++17 requires this to be atomic. - // MoveFile, MoveFileEx() or rename() do not guarantee this. - // Any existing file at new_path is to be overwritten. - // rename() does not do so on Windows. - auto MoveFileEx_result = MoveFileExW( - widen(old_path).c_str(), - widen(new_path).c_str(), - MOVEFILE_REPLACE_EXISTING); // flags - - if(MoveFileEx_result == 0) - throw system_exceptiont("MoveFileExW failed"); - } -#else - int rename_result = rename(old_path.c_str(), new_path.c_str()); - - if(rename_result != 0) - throw system_exceptiont( - std::string("rename failed: ") + std::strerror(errno)); -#endif -} diff --git a/src/util/file_util.h b/src/util/file_util.h deleted file mode 100644 index 0e749a889e03..000000000000 --- a/src/util/file_util.h +++ /dev/null @@ -1,50 +0,0 @@ -/*******************************************************************\ - -Module: - -Author: Daniel Kroening, kroening@kroening.com - -\*******************************************************************/ - - -#ifndef CPROVER_UTIL_FILE_UTIL_H -#define CPROVER_UTIL_FILE_UTIL_H - -#include - -// 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 (for both get and -// set) -std::string get_current_working_directory(); -void set_current_path(const std::string &path); - -// C++17 will allow us to use std::filesystem::path(dir).append(file) -std::string concat_dir_file(const std::string &directory, - const std::string &file_name); - -// C++17 will allow us to use std::filesystem::is_directory -bool is_directory(const std::string &path); - -/// Create a directory with given path -/// C++17 will allow us to use std::filesystem::create_directory -/// \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); - -/// Rename a file. -/// C++17 will allow us to use std::filesystem::rename -/// Throws an exception on failure. -void file_rename(const std::string &old_path, const std::string &new_path); - -#endif // CPROVER_UTIL_FILE_UTIL_H diff --git a/src/util/parser.h b/src/util/parser.h index 9fc1e3c577dd..70a796ec4a91 100644 --- a/src/util/parser.h +++ b/src/util/parser.h @@ -12,14 +12,14 @@ Author: Daniel Kroening, kroening@kroening.com #ifndef CPROVER_UTIL_PARSER_H #define CPROVER_UTIL_PARSER_H +#include "expr.h" +#include "message.h" + +#include #include #include #include -#include "expr.h" -#include "message.h" -#include "file_util.h" - class parsert { public: @@ -86,7 +86,7 @@ class parsert { source_location.set_file(file); source_location.set_working_directory( - get_current_working_directory()); + std::filesystem::current_path().string()); } irep_idt get_file() const diff --git a/src/util/source_location.cpp b/src/util/source_location.cpp index 58897b858da5..44656e09e4ca 100644 --- a/src/util/source_location.cpp +++ b/src/util/source_location.cpp @@ -8,11 +8,11 @@ Author: Daniel Kroening, kroening@kroening.com #include "source_location.h" -#include - -#include "file_util.h" #include "prefix.h" +#include +#include + bool source_locationt::is_built_in(const std::string &s) { std::string built_in1 = ""; @@ -37,8 +37,11 @@ std::string source_locationt::as_string(bool print_cwd) const dest+=' '; dest+="file "; if(print_cwd) - dest+= - concat_dir_file(id2string(get_working_directory()), id2string(file)); + { + dest += std::filesystem::path(id2string(get_working_directory())) + .append(id2string(file)) + .string(); + } else dest+=id2string(file); } @@ -89,7 +92,9 @@ optionalt source_locationt::full_path() const if(file.empty() || is_built_in(file)) return {}; - return concat_dir_file(id2string(get_working_directory()), file); + return std::filesystem::path(id2string(get_working_directory())) + .append(file) + .string(); } std::ostream &operator << ( diff --git a/src/util/tempdir.cpp b/src/util/tempdir.cpp index b8b0a0fe78b5..c957bd3c1718 100644 --- a/src/util/tempdir.cpp +++ b/src/util/tempdir.cpp @@ -8,34 +8,23 @@ Author: CM Wintersteiger #include "tempdir.h" -#ifdef _WIN32 -#include -#ifdef _MSC_VER -#pragma warning(disable:4668) - // using #if/#elif on undefined macro -#pragma warning(disable : 5039) -// pointer or reference to potentially throwing function passed to extern C -#endif -#include -#include -#include -#include -#endif - -#include -#include -#include +#include // clang-format off +#ifndef _WIN32 #if defined(__FreeBSD_kernel__) || \ defined(__CYGWIN__) || \ defined(__MACH__) #include #endif + +#include +#include +#include +#endif // clang-format on #include "exception_utils.h" -#include "file_util.h" std::string get_temporary_directory(const std::string &name_template) { @@ -43,36 +32,14 @@ std::string get_temporary_directory(const std::string &name_template) #ifdef _WIN32 (void)name_template; // unused parameter - DWORD dwBufSize = MAX_PATH + 1; - char lpPathBuffer[MAX_PATH + 1]; - DWORD dwRetVal = GetTempPathA(dwBufSize, lpPathBuffer); - - if(dwRetVal > dwBufSize || (dwRetVal == 0)) + try { - throw system_exceptiont("Couldn't get temporary path"); + result = std::filesystem::temp_directory_path().string(); } - - // GetTempFileNameA produces \
.TMP
-  // where 
 = "TLO"
-  // Thus, we must make the buffer 1+3+4+1+3=12 characters longer.
-
-  char t[MAX_PATH];
-  UINT uRetVal = GetTempFileNameA(lpPathBuffer, "TLO", 0, t);
-  if(uRetVal == 0)
+  catch(const std::filesystem::filesystem_error &)
   {
-    throw system_exceptiont(
-      std::string("Couldn't get new temporary file name in directory") +
-      lpPathBuffer);
-  }
-
-  unlink(t);
-  if(_mkdir(t) != 0)
-  {
-    throw system_exceptiont(
-      std::string("Couldn't create temporary directory at ") + t);
+    throw system_exceptiont("Failed to create temporary directory");
   }
-  result = std::string(t);
-
 #else
   std::string prefixed_name_template = "/tmp/";
   const char *TMPDIR_env = getenv("TMPDIR");
@@ -110,12 +77,12 @@ temp_dirt::temp_dirt(const std::string &name_template)
 
 std::string temp_dirt::operator()(const std::string &file)
 {
-  return concat_dir_file(path, file);
+  return std::filesystem::path(path).append(file).string();
 }
 
 void temp_dirt::clear()
 {
-  delete_directory(path);
+  std::filesystem::remove_all(path);
 }
 
 temp_dirt::~temp_dirt()
diff --git a/src/util/tempfile.cpp b/src/util/tempfile.cpp
index 278c115d8a55..ad0e6dec577a 100644
--- a/src/util/tempfile.cpp
+++ b/src/util/tempfile.cpp
@@ -33,9 +33,9 @@ Author: Daniel Kroening
 
 #include 
 #include 
+#include 
 
 #include "exception_utils.h"
-#include "file_util.h"
 
 #if defined(__linux__) || \
     defined(__FreeBSD_kernel__) || \
@@ -145,5 +145,5 @@ std::string get_temporary_file(
 temporary_filet::~temporary_filet()
 {
   if(!name.empty())
-    file_remove(name);
+    std::filesystem::remove(name);
 }
diff --git a/unit/Makefile b/unit/Makefile
index de364726e36b..af2afbb02076 100644
--- a/unit/Makefile
+++ b/unit/Makefile
@@ -141,7 +141,6 @@ SRC += analyses/ai/ai.cpp \
        util/expr_initializer.cpp \
        util/expr.cpp \
        util/expr_iterator.cpp \
-       util/file_util.cpp \
        util/format.cpp \
        util/format_expr.cpp \
        util/format_number_range.cpp \
diff --git a/unit/util/file_util.cpp b/unit/util/file_util.cpp
deleted file mode 100644
index 93c03fcfa927..000000000000
--- a/unit/util/file_util.cpp
+++ /dev/null
@@ -1,72 +0,0 @@
-/*******************************************************************\
-
-Module: Unit tests for file_util.h
-
-Author: Daniel Kroening
-
-\*******************************************************************/
-
-#include  // IWYU pragma: keep
-#include 
-#include 
-
-#include 
-
-#ifdef _MSC_VER
-#  include 
-#endif
-
-#include 
-
-TEST_CASE("concat_dir_file functionality", "[core][util][file_util]")
-{
-  temp_dirt temp_dir("testXXXXXX");
-  const std::string path = concat_dir_file(temp_dir.path, "bla.txt");
-
-  REQUIRE(path.size() > temp_dir.path.size() + std::string("bla.txt").size());
-#ifdef _WIN32
-  REQUIRE(path.find('\\') != std::string::npos);
-#else
-  REQUIRE(path.find('/') != std::string::npos);
-#endif
-
-#ifdef _WIN32
-  const std::string qualified_path = "z:\\some\\path\\foo.txt";
-#else
-  const std::string qualified_path = "/some/path/foo.txt";
-#endif
-  const std::string path2 = concat_dir_file(temp_dir.path, qualified_path);
-  REQUIRE(path2 == qualified_path);
-}
-
-TEST_CASE("is_directory functionality", "[core][util][file_util]")
-{
-  temp_dirt temp_dir("testXXXXXX");
-
-#ifdef _MSC_VER
-  std::ofstream outfile(widen(temp_dir("file")));
-#else
-  std::ofstream outfile(temp_dir("file"));
-#endif
-
-  outfile.close();
-
-  REQUIRE(is_directory(temp_dir.path));
-  REQUIRE(is_directory(temp_dir.path+"/"));
-  REQUIRE(!is_directory(temp_dir("whatnot")));
-  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);
-}