Support/Windows/PathV2: Fix remove to handle both files and directories.
authorMichael J. Spencer <bigcheesegs@gmail.com>
Wed, 5 Jan 2011 16:39:22 +0000 (16:39 +0000)
committerMichael J. Spencer <bigcheesegs@gmail.com>
Wed, 5 Jan 2011 16:39:22 +0000 (16:39 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@122882 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/Windows/PathV2.inc

index c0c70e04726a8c820085bc47725668fe22870655..f3d625797dd9d4e560e0608bd87e0ea0a8a55f61 100644 (file)
@@ -268,17 +268,31 @@ error_code remove(const Twine &path, bool &existed) {
   SmallString<128> path_storage;
   SmallVector<wchar_t, 128> path_utf16;
 
+  file_status st;
+  if (error_code ec = status(path, st))
+    return ec;
+
   if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
                                   path_utf16))
     return ec;
 
-  if (!::DeleteFileW(path_utf16.begin())) {
-    error_code ec = windows_error(::GetLastError());
-    if (ec != windows_error::file_not_found)
-      return ec;
-    existed = false;
-  } else
-    existed = true;
+  if (st.type() == file_type::directory_file) {
+    if (!::RemoveDirectoryW(c_str(path_utf16))) {
+      error_code ec = windows_error(::GetLastError());
+      if (ec != windows_error::file_not_found)
+        return ec;
+      existed = false;
+    } else
+      existed = true;
+  } else {
+    if (!::DeleteFileW(c_str(path_utf16))) {
+      error_code ec = windows_error(::GetLastError());
+      if (ec != windows_error::file_not_found)
+        return ec;
+      existed = false;
+    } else
+      existed = true;
+  }
 
   return success;
 }