1 //===-- FileSystem.cpp ------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Host/windows/windows.h" 11 12 #include <shellapi.h> 13 #include <sys/stat.h> 14 #include <sys/types.h> 15 16 #include "lldb/Host/FileSystem.h" 17 #include "lldb/Host/windows/AutoHandle.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 Error FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) { 30 Error 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 Error FileSystem::Readlink(const FileSpec &src, FileSpec &dst) { 51 Error 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, false); 78 79 ::CloseHandle(h); 80 return error; 81 } 82 83 Error FileSystem::ResolveSymbolicLink(const FileSpec &src, FileSpec &dst) { 84 return Error("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