[PATCH] D41830: [libc++] Fix PR#35780 - make std::experimental::filesystem::remove and remove_all return false or 0 if the file doesn't exist

Ekaterina Vaartis via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 9 10:02:05 PST 2018


TyanNN updated this revision to Diff 129115.
TyanNN retitled this revision from "[libc++] Fix PR#35780 - make std::experimental::filesystem::remove return false if the file doesn't exist" to "[libc++] Fix PR#35780 - make std::experimental::filesystem::remove and remove_all return false or 0 if the file doesn't exist".
TyanNN edited the summary of this revision.
TyanNN added a comment.

Fix remove_all too. Use std error codes instead of those from C


https://reviews.llvm.org/D41830

Files:
  src/experimental/filesystem/operations.cpp
  test/libcxx/experimental/filesystem/class.path/path.remove/remove_should_not_throw_on_nonexistant.pass.cpp


Index: test/libcxx/experimental/filesystem/class.path/path.remove/remove_should_not_throw_on_nonexistant.pass.cpp
===================================================================
--- test/libcxx/experimental/filesystem/class.path/path.remove/remove_should_not_throw_on_nonexistant.pass.cpp
+++ test/libcxx/experimental/filesystem/class.path/path.remove/remove_should_not_throw_on_nonexistant.pass.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++98, c++03
+
+// <experimental/filesystem>
+
+// class path
+
+#define _LIBCPP_DEBUG 0
+#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : (void)::AssertCount++)
+int AssertCount = 0;
+
+#include <experimental/filesystem>
+#include <cassert>
+#include <fstream>
+
+namespace fs = std::experimental::filesystem;
+
+int main()
+{
+    using namespace fs;
+
+    // filesystem::remove test
+
+    path nFile("nonexistant.file");
+    assert(remove(nFile) == false);
+
+    path tempFilePath = temp_directory_path();
+    tempFilePath += path("existingFile");
+
+    std::ofstream theTempFile(tempFilePath);
+    theTempFile.close();
+
+    assert(remove(tempFilePath) == true);
+
+    // filesystem::remove_all
+
+    assert(remove_all(nFile) == 0);
+
+    std::ofstream theTempFile2(tempFilePath);
+    theTempFile2.close();
+    assert(remove_all(tempFilePath) == 1);
+
+    return 0;
+}
Index: src/experimental/filesystem/operations.cpp
===================================================================
--- src/experimental/filesystem/operations.cpp
+++ src/experimental/filesystem/operations.cpp
@@ -661,8 +661,10 @@
 
 bool __remove(const path& p, std::error_code *ec) {
     if (ec) ec->clear();
+
     if (::remove(p.c_str()) == -1) {
-        set_or_throw(ec, "remove", p);
+        if (ec != nullptr && *ec != errc::no_such_file_or_directory)
+            set_or_throw(ec, "remove", p);
         return false;
     }
     return true;
@@ -695,8 +697,12 @@
     std::error_code mec;
     auto count = remove_all_impl(p, mec);
     if (mec) {
-        set_or_throw(mec, ec, "remove_all", p);
-        return static_cast<std::uintmax_t>(-1);
+        if (mec != errc::no_such_file_or_directory) {
+            set_or_throw(mec, ec, "remove_all", p);
+            return static_cast<std::uintmax_t>(-1);
+        } else {
+            return static_cast<std::uintmax_t>(0);
+        }
     }
     if (ec) ec->clear();
     return count;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41830.129115.patch
Type: text/x-patch
Size: 2742 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180109/6a03494f/attachment-0001.bin>


More information about the cfe-commits mailing list