aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/cpp/util
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2017-01-10 13:53:13 +0000
committerGravatar Marcel Hlopko <hlopko@google.com>2017-01-10 14:32:00 +0000
commit9a13899b1492738f8d1a9118cebc9ef9d90c6b34 (patch)
tree5c5b88eb9e379d3229737eab700b699451abc727 /src/main/cpp/util
parent25a275ae595f312110f663ea98a32cf6562deb80 (diff)
Bazel client, Windows: implement UnlinkPath
See https://github.com/bazelbuild/bazel/issues/2107 -- PiperOrigin-RevId: 144073888 MOS_MIGRATED_REVID=144073888
Diffstat (limited to 'src/main/cpp/util')
-rw-r--r--src/main/cpp/util/file_windows.cc36
1 files changed, 30 insertions, 6 deletions
diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc
index 2b4a105f05..758bfb71dc 100644
--- a/src/main/cpp/util/file_windows.cc
+++ b/src/main/cpp/util/file_windows.cc
@@ -38,6 +38,12 @@ using std::wstring;
// The result may have a UNC prefix.
static unique_ptr<WCHAR[]> GetCwdW();
+// Returns true the file or junction at `path` is successfully deleted.
+// Returns false otherwise, or if `path` doesn't exist or is a directory.
+// `path` must be a normalized Windows path, with UNC prefix (and absolute) if
+// necessary.
+static bool UnlinkPathW(const wstring& path);
+
// Like `AsWindowsPath` but the result is absolute and has UNC prefix if needed.
static bool AsWindowsPathWithUncPrefix(const string& path, wstring* wpath);
@@ -384,14 +390,32 @@ bool WriteFile(const void* data, size_t size, const string& filename) {
#else // not COMPILER_MSVC
#endif // COMPILER_MSVC
-#ifdef COMPILER_MSVC
+static bool UnlinkPathW(const wstring& path) {
+ DWORD attrs = ::GetFileAttributesW(path.c_str());
+ if (attrs == INVALID_FILE_ATTRIBUTES) {
+ // Path does not exist.
+ return false;
+ }
+ if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
+ if (!(attrs & FILE_ATTRIBUTE_REPARSE_POINT)) {
+ // Path is a directory; unlink(2) also cannot remove directories.
+ return false;
+ }
+ // Otherwise it's a junction, remove using RemoveDirectoryW.
+ return ::RemoveDirectoryW(path.c_str()) == TRUE;
+ } else {
+ // Otherwise it's a file, remove using DeleteFileW.
+ return ::DeleteFileW(path.c_str()) == TRUE;
+ }
+}
+
bool UnlinkPath(const string& file_path) {
- // TODO(bazel-team): implement this.
- pdie(255, "blaze_util::UnlinkPath is not implemented on Windows");
- return false;
+ wstring wpath;
+ if (!AsWindowsPathWithUncPrefix(file_path, &wpath)) {
+ return false;
+ }
+ return UnlinkPathW(wpath);
}
-#else // not COMPILER_MSVC
-#endif // COMPILER_MSVC
HANDLE OpenDirectory(const WCHAR* path, bool read_write) {
return ::CreateFileW(