diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2013-07-17 14:58:25 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2013-07-17 14:58:25 +0000 |
commit | b0aa9e9718181952d5a636c72ff32c8d4352685b (patch) | |
tree | 6c6fa65ca4b3dbd4b94de38d3332f22104cd078f /lib/Support/Windows/Path.inc | |
parent | 86f4f6526b18765cdb78bee593e1354bc9f55085 (diff) | |
download | external_llvm-b0aa9e9718181952d5a636c72ff32c8d4352685b.zip external_llvm-b0aa9e9718181952d5a636c72ff32c8d4352685b.tar.gz external_llvm-b0aa9e9718181952d5a636c72ff32c8d4352685b.tar.bz2 |
Split openFileForRead into Windows and Unix versions.
This has some advantages:
* Lets us use native, utf16 windows functions.
* Easy to produce good errors on windows about trying to use a
directory when we want a file.
* Simplifies the unix version a bit.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186511 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/Windows/Path.inc')
-rw-r--r-- | lib/Support/Windows/Path.inc | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/Support/Windows/Path.inc b/lib/Support/Windows/Path.inc index 5a62a60..1da60d6 100644 --- a/lib/Support/Windows/Path.inc +++ b/lib/Support/Windows/Path.inc @@ -1041,7 +1041,38 @@ error_code unmap_file_pages(void *base, size_t size) { return windows_error::invalid_function; } +error_code openFileForRead(const Twine &Name, int &ResultFD) { + SmallString<128> PathStorage; + SmallVector<wchar_t, 128> PathUTF16; + + if (error_code EC = UTF8ToUTF16(Name.toStringRef(PathStorage), + PathUTF16)) + return EC; + HANDLE H = ::CreateFileW(PathUTF16.begin(), GENERIC_READ, + FILE_SHARE_READ, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (H == INVALID_HANDLE_VALUE) { + error_code EC = windows_error(::GetLastError()); + // Provide a better error massage when trying to open directories. + // This only runs if we failed to open the file, so there is probably + // no performances issues. + if (EC != windows_error::access_denied) + return EC; + if (is_directory(Name)) + return error_code(errc::is_a_directory, posix_category()); + return EC; + } + + int FD = ::_open_osfhandle(intptr_t(H), 0); + if (FD == -1) { + ::CloseHandle(H); + return windows_error::invalid_handle; + } + + ResultFD = FD; + return error_code::success(); +} } // end namespace fs } // end namespace sys |