From 4e2024e2e6b8309f2db6548e04e920f565a84b0b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 18 Mar 2021 15:47:49 +0200 Subject: Add try_rmfile_ignore_error() --- libbutl/filesystem.cxx | 6 ++++-- libbutl/filesystem.ixx | 17 +++++++++++++++++ libbutl/filesystem.mxx | 19 ++++++++++++++----- 3 files changed, 35 insertions(+), 7 deletions(-) (limited to 'libbutl') diff --git a/libbutl/filesystem.cxx b/libbutl/filesystem.cxx index 18be8a9..2147c6f 100644 --- a/libbutl/filesystem.cxx +++ b/libbutl/filesystem.cxx @@ -1063,8 +1063,8 @@ namespace butl } } - rmfile_status - try_rmfile (const path& p, bool ignore_error) + optional + try_rmfile_maybe_ignore_error (const path& p, bool ignore_error) { rmfile_status r (rmfile_status::success); const char* f (p.string ().c_str ()); @@ -1143,6 +1143,8 @@ namespace butl r = rmfile_status::not_exist; else if (!ignore_error) throw_generic_error (errno); + else + return nullopt; } return r; diff --git a/libbutl/filesystem.ixx b/libbutl/filesystem.ixx index f7c3777..8cd8456 100644 --- a/libbutl/filesystem.ixx +++ b/libbutl/filesystem.ixx @@ -38,6 +38,23 @@ namespace butl return e ? rmdir_status::success : rmdir_status::not_exist; } + LIBBUTL_SYMEXPORT optional + try_rmfile_maybe_ignore_error (const path&, bool ignore_error); + + inline rmfile_status + try_rmfile (const path& p, bool ignore_error) + { + auto r (try_rmfile_maybe_ignore_error (p, ignore_error)); + return r ? *r : rmfile_status::success; + } + + inline optional + try_rmfile_ignore_error (const path& p) + { + return try_rmfile_maybe_ignore_error (p, true); + } + + inline path followsymlink (const path& p) { diff --git a/libbutl/filesystem.mxx b/libbutl/filesystem.mxx index 935fc3f..7506439 100644 --- a/libbutl/filesystem.mxx +++ b/libbutl/filesystem.mxx @@ -42,12 +42,14 @@ import std.core; #endif import butl.path; +import butl.optional; import butl.timestamp; import butl.path_pattern; // path_match_flags import butl.utility; // operator<<(ostream,exception), throw_generic_error() #else #include +#include #include #include @@ -60,7 +62,8 @@ LIBBUTL_MODEXPORT namespace butl { // Return true if the path is to an existing regular file. Note that by // default this function follows symlinks. Underlying OS errors are reported - // by throwing std::system_error, unless ignore_error is true. + // by throwing std::system_error, unless ignore_error is true (in which case + // erroneous entries are treated as non-existent). // LIBBUTL_SYMEXPORT bool file_exists (const char*, @@ -73,7 +76,8 @@ LIBBUTL_MODEXPORT namespace butl // Return true if the path is to an existing directory. Note that this // function follows symlinks. Underlying OS errors are reported by throwing - // std::system_error, unless ignore_error is true. + // std::system_error, unless ignore_error is true (in which case erroneous + // entries are treated as non-existent). // LIBBUTL_SYMEXPORT bool dir_exists (const char*, bool ignore_error = false); @@ -84,7 +88,8 @@ LIBBUTL_MODEXPORT namespace butl // Return true if the path is to an existing file system entry. Note that by // default this function doesn't follow symlinks. Underlying OS errors are - // reported by throwing std::system_error, unless ignore_error is true. + // reported by throwing std::system_error, unless ignore_error is true (in + // which case erroneous entries are treated as non-existent). // LIBBUTL_SYMEXPORT bool entry_exists (const char*, @@ -117,7 +122,8 @@ LIBBUTL_MODEXPORT namespace butl // Return a flag indicating if the path is to an existing filesystem entry // and its info if so. Note that by default this function doesn't follow // symlinks. Underlying OS errors are reported by throwing - // std::system_error, unless ignore_error is true. + // std::system_error, unless ignore_error is true (in which case erroneous + // entries are treated as non-existent). // LIBBUTL_SYMEXPORT std::pair path_entry (const char*, @@ -206,9 +212,12 @@ LIBBUTL_MODEXPORT namespace butl // is not atomic. It is also not atomic for the directory-type reparse point // removal. // - LIBBUTL_SYMEXPORT rmfile_status + rmfile_status try_rmfile (const path&, bool ignore_error = false); + optional + try_rmfile_ignore_error (const path&); + // Automatically try to remove a non-empty path on destruction unless // cancelled. Since the non-cancelled destruction will normally happen as a // result of an exception, the failure to remove the path is silently -- cgit v1.1