1 //===-- FileSystem.cpp ------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Host/windows/windows.h" 10 11 #include <shellapi.h> 12 #include <sys/stat.h> 13 #include <sys/types.h> 14 15 #include "lldb/Host/FileSystem.h" 16 #include "lldb/Host/windows/AutoHandle.h" 17 #include "lldb/Host/windows/PosixApi.h" 18 19 #include "llvm/Support/ConvertUTF.h" 20 #include "llvm/Support/FileSystem.h" 21 22 using namespace lldb_private; 23 24 const char *FileSystem::DEV_NULL = "nul"; 25 26 const char *FileSystem::PATH_CONVERSION_ERROR = 27 "Error converting path between UTF-8 and native encoding"; 28 29 Status FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) { 30 Status error; 31 std::wstring wsrc, wdst; 32 if (!llvm::ConvertUTF8toWide(src.GetCString(), wsrc) || 33 !llvm::ConvertUTF8toWide(dst.GetCString(), wdst)) 34 error.SetErrorString(PATH_CONVERSION_ERROR); 35 if (error.Fail()) 36 return error; 37 DWORD attrib = ::GetFileAttributesW(wdst.c_str()); 38 if (attrib == INVALID_FILE_ATTRIBUTES) { 39 error.SetError(::GetLastError(), lldb::eErrorTypeWin32); 40 return error; 41 } 42 bool is_directory = !!(attrib & FILE_ATTRIBUTE_DIRECTORY); 43 DWORD flag = is_directory ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0; 44 BOOL result = ::CreateSymbolicLinkW(wsrc.c_str(), wdst.c_str(), flag); 45 if (!result) 46 error.SetError(::GetLastError(), lldb::eErrorTypeWin32); 47 return error; 48 } 49 50 Status FileSystem::Readlink(const FileSpec &src, FileSpec &dst) { 51 Status error; 52 std::wstring wsrc; 53 if (!llvm::ConvertUTF8toWide(src.GetCString(), wsrc)) { 54 error.SetErrorString(PATH_CONVERSION_ERROR); 55 return error; 56 } 57 58 HANDLE h = ::CreateFileW(wsrc.c_str(), GENERIC_READ, 59 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 60 OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT, NULL); 61 if (h == INVALID_HANDLE_VALUE) { 62 error.SetError(::GetLastError(), lldb::eErrorTypeWin32); 63 return error; 64 } 65 66 std::vector<wchar_t> buf(PATH_MAX + 1); 67 // Subtract 1 from the path length since this function does not add a null 68 // terminator. 69 DWORD result = ::GetFinalPathNameByHandleW( 70 h, buf.data(), buf.size() - 1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); 71 std::string path; 72 if (result == 0) 73 error.SetError(::GetLastError(), lldb::eErrorTypeWin32); 74 else if (!llvm::convertWideToUTF8(buf.data(), path)) 75 error.SetErrorString(PATH_CONVERSION_ERROR); 76 else 77 dst.SetFile(path, FileSpec::Style::native); 78 79 ::CloseHandle(h); 80 return error; 81 } 82 83 Status FileSystem::ResolveSymbolicLink(const FileSpec &src, FileSpec &dst) { 84 return Status("ResolveSymbolicLink() isn't implemented on Windows"); 85 } 86 87 FILE *FileSystem::Fopen(const char *path, const char *mode) { 88 std::wstring wpath, wmode; 89 if (!llvm::ConvertUTF8toWide(path, wpath)) 90 return nullptr; 91 if (!llvm::ConvertUTF8toWide(mode, wmode)) 92 return nullptr; 93 FILE *file; 94 if (_wfopen_s(&file, wpath.c_str(), wmode.c_str()) != 0) 95 return nullptr; 96 return file; 97 } 98 99 int FileSystem::Open(const char *path, int flags, int mode) { 100 std::wstring wpath; 101 if (!llvm::ConvertUTF8toWide(path, wpath)) 102 return -1; 103 int result; 104 ::_wsopen_s(&result, wpath.c_str(), flags, _SH_DENYNO, mode); 105 return result; 106 } 107