aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libbutl/filesystem.cxx6
-rw-r--r--libbutl/filesystem.ixx17
-rw-r--r--libbutl/filesystem.mxx19
3 files changed, 35 insertions, 7 deletions
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<rmfile_status>
+ 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<rmfile_status>
+ 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<rmfile_status>
+ 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 <libbutl/path.mxx>
+#include <libbutl/optional.mxx>
#include <libbutl/timestamp.mxx>
#include <libbutl/path-pattern.mxx>
@@ -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<bool, entry_stat>
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<rmfile_status>
+ 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