Add a can_write function to PathV2.
authorRafael Espindola <rafael.espindola@gmail.com>
Tue, 18 Jun 2013 20:56:38 +0000 (20:56 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Tue, 18 Jun 2013 20:56:38 +0000 (20:56 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@184233 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/FileSystem.h
lib/Support/Unix/PathV2.inc
lib/Support/Windows/PathV2.inc

index 6de8759dda2da819a009025f78d086c9b439fd82..21159dd26420ba4cd70c89fa99b9d0a0fe70fdca 100644 (file)
@@ -356,6 +356,12 @@ inline bool exists(const Twine &path) {
 /// @returns True if we can execute it, false otherwise.
 bool can_execute(const Twine &Path);
 
+/// @brief Can we write this file?
+///
+/// @param Path Input path.
+/// @returns True if we can write to it, false otherwise.
+bool can_write(const Twine &Path);
+
 /// @brief Do file_status's represent the same thing?
 ///
 /// @param A Input file_status.
index 9ea4f9ed51ad03cc2ed550264a35d2800396f33b..a806083b1936153c293091b0bc81a800f5cd685c 100644 (file)
@@ -308,6 +308,12 @@ error_code exists(const Twine &path, bool &result) {
   return error_code::success();
 }
 
+bool can_write(const Twine &Path) {
+  SmallString<128> PathStorage;
+  StringRef P = Path.toNullTerminatedStringRef(PathStorage);
+  return 0 == access(P.begin(), W_OK);
+}
+
 bool can_execute(const Twine &Path) {
   SmallString<128> PathStorage;
   StringRef P = Path.toNullTerminatedStringRef(PathStorage);
index 3b2992d5a3011b6573b83635b55ac5b5d892f55e..bec95e36cc596bebd517a89bd4d018a7de46bd03 100644 (file)
@@ -362,6 +362,18 @@ error_code exists(const Twine &path, bool &result) {
   return error_code::success();
 }
 
+bool can_write(const Twine &Path) {
+  // FIXME: take security attributes into account.
+  SmallString<128> PathStorage;
+  SmallVector<wchar_t, 128> PathUtf16;
+
+  if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16))
+    return false;
+
+  DWORD Attr = ::GetFileAttributesW(PathUtf16.begin());
+  return (Attr != INVALID_FILE_ATTRIBUTES) && !(Attr & FILE_ATTRIBUTE_READONLY);
+}
+
 bool can_execute(const Twine &Path) {
   SmallString<128> PathStorage;
   SmallVector<wchar_t, 128> PathUtf16;