diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2013-06-18 20:56:38 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2013-06-18 20:56:38 +0000 |
commit | 68ba1825fcaf94bdf7e057ef19585226ec48ce5d (patch) | |
tree | fcbb31282038cfbfdf949b899cd866d8934774a5 | |
parent | 41502e1af77443c31138cee309bd89898f23e33a (diff) | |
download | external_llvm-68ba1825fcaf94bdf7e057ef19585226ec48ce5d.zip external_llvm-68ba1825fcaf94bdf7e057ef19585226ec48ce5d.tar.gz external_llvm-68ba1825fcaf94bdf7e057ef19585226ec48ce5d.tar.bz2 |
Add a can_write function to PathV2.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@184233 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/llvm/Support/FileSystem.h | 6 | ||||
-rw-r--r-- | lib/Support/Unix/PathV2.inc | 6 | ||||
-rw-r--r-- | lib/Support/Windows/PathV2.inc | 12 |
3 files changed, 24 insertions, 0 deletions
diff --git a/include/llvm/Support/FileSystem.h b/include/llvm/Support/FileSystem.h index 6de8759..21159dd 100644 --- a/include/llvm/Support/FileSystem.h +++ b/include/llvm/Support/FileSystem.h @@ -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. diff --git a/lib/Support/Unix/PathV2.inc b/lib/Support/Unix/PathV2.inc index 9ea4f9e..a806083 100644 --- a/lib/Support/Unix/PathV2.inc +++ b/lib/Support/Unix/PathV2.inc @@ -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); diff --git a/lib/Support/Windows/PathV2.inc b/lib/Support/Windows/PathV2.inc index 3b2992d..bec95e3 100644 --- a/lib/Support/Windows/PathV2.inc +++ b/lib/Support/Windows/PathV2.inc @@ -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; |