Support/FileSystem: Add remove implementation.
authorMichael J. Spencer <bigcheesegs@gmail.com>
Fri, 3 Dec 2010 17:53:43 +0000 (17:53 +0000)
committerMichael J. Spencer <bigcheesegs@gmail.com>
Fri, 3 Dec 2010 17:53:43 +0000 (17:53 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120817 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/Unix/PathV2.inc
lib/Support/Windows/PathV2.inc
unittests/Support/Path.cpp

index 985dde7426a9fefba910e316b96147636c6d6a2d..e6972f83363d0266bae214cfca92c3ab246fa5ff 100644 (file)
@@ -186,6 +186,20 @@ error_code create_symlink(const Twine &to, const Twine &from) {
   return make_error_code(errc::success);
 }
 
+error_code remove(const Twine &path, bool &existed) {
+  SmallString<128> path_storage;
+  StringRef p = path.toNullTerminatedStringRef(path_storage);
+
+  if (::remove(p.begin()) == -1) {
+    if (errno != ENOENT)
+      return error_code(errno, system_category());
+    existed = false;
+  } else
+    existed = true;
+
+  return make_error_code(errc::success);
+}
+
 error_code exists(const Twine &path, bool &result) {
   SmallString<128> path_storage;
   StringRef p = path.toNullTerminatedStringRef(path_storage);
index 98db5fdf388cdb3599df449ebd77c34c9adcc5e4..c86c24e811e1b72cc05ef69f402334b8564aa4c1 100644 (file)
@@ -252,6 +252,25 @@ error_code create_symlink(const Twine &to, const Twine &from) {
   return make_error_code(errc::success);
 }
 
+error_code remove(const Twine &path, bool &existed) {
+  SmallString<128> path_storage;
+  SmallVector<wchar_t, 128> path_utf16;
+
+  if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
+                                  path_utf16))
+    return ec;
+
+  if (!::DeleteFileW(path_utf16.begin())) {
+    error_code ec = make_error_code(windows_error(::GetLastError()));
+    if (ec != make_error_code(windows_error::file_not_found))
+      return ec;
+    existed = false;
+  } else
+    existed = true;
+
+  return make_error_code(errc::success);
+}
+
 error_code exists(const Twine &path, bool &result) {
   SmallString<128> path_storage;
   SmallVector<wchar_t, 128> path_utf16;
index d19bf4a2efc8ff5f94704bfeff2f5b77f6e0c86b..40e62811d02723c94427b7683ff3c303aca884bf 100644 (file)
@@ -128,7 +128,8 @@ TEST(Support, Path) {
   EXPECT_TRUE(TempFileExists);
 
   ::close(FileDescriptor);
-  ::remove(TempPath.c_str());
+  ASSERT_FALSE(fs::remove(Twine(TempPath), TempFileExists));
+  EXPECT_TRUE(TempFileExists);
 
   ASSERT_FALSE(fs::exists(Twine(TempPath), TempFileExists));
   EXPECT_FALSE(TempFileExists);