diff options
author | Michael J. Spencer <bigcheesegs@gmail.com> | 2011-01-15 18:52:33 +0000 |
---|---|---|
committer | Michael J. Spencer <bigcheesegs@gmail.com> | 2011-01-15 18:52:33 +0000 |
commit | d6cdf1d3cb0263c413684aa20cb8aa91f9e128de (patch) | |
tree | 4290d11cb57b0225d12eb808779d7ec766261c1d /lib/Support/Windows/PathV2.inc | |
parent | cd7f0a1a7f216418856812f6417faf4fe5e72046 (diff) |
Support/PathV2: Implement get_magic.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123544 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/Windows/PathV2.inc')
-rw-r--r-- | lib/Support/Windows/PathV2.inc | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/Support/Windows/PathV2.inc b/lib/Support/Windows/PathV2.inc index c618dbf520..f4629a15c4 100644 --- a/lib/Support/Windows/PathV2.inc +++ b/lib/Support/Windows/PathV2.inc @@ -616,6 +616,48 @@ retry_create_file: return success; } +error_code get_magic(const Twine &path, uint32_t len, + SmallVectorImpl<char> &result) { + SmallString<128> path_storage; + SmallVector<wchar_t, 128> path_utf16; + result.set_size(0); + + // Convert path to UTF-16. + if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage), + path_utf16)) + return ec; + + // Open file. + HANDLE file = ::CreateFileW(c_str(path_utf16), + GENERIC_READ, + FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_READONLY, + NULL); + if (file == INVALID_HANDLE_VALUE) + return windows_error(::GetLastError()); + + // Allocate buffer. + result.reserve(len); + + // Get magic! + DWORD bytes_read = 0; + BOOL read_success = ::ReadFile(file, result.data(), len, &bytes_read, NULL); + error_code ec = windows_error(::GetLastError()); + ::CloseHandle(file); + if (!read_success || (bytes_read != len)) { + // Set result size to the number of bytes read if it's valid. + if (bytes_read >= 0 && bytes_read <= len) + result.set_size(bytes_read); + // ERROR_HANDLE_EOF is mapped to errc::value_too_large. + return ec; + } + + result.set_size(len); + return success; +} + error_code directory_iterator_construct(directory_iterator &it, StringRef path){ SmallVector<wchar_t, 128> path_utf16; |