xref: /openbsd-src/gnu/llvm/lldb/source/Host/windows/FileSystem.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1dda28197Spatrick //===-- FileSystem.cpp ----------------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "lldb/Host/windows/windows.h"
10061da546Spatrick 
11061da546Spatrick #include <share.h>
12061da546Spatrick #include <shellapi.h>
13061da546Spatrick #include <sys/stat.h>
14061da546Spatrick #include <sys/types.h>
15061da546Spatrick 
16061da546Spatrick #include "lldb/Host/FileSystem.h"
17061da546Spatrick #include "lldb/Host/windows/AutoHandle.h"
18061da546Spatrick #include "lldb/Host/windows/PosixApi.h"
19061da546Spatrick 
20061da546Spatrick #include "llvm/Support/ConvertUTF.h"
21061da546Spatrick #include "llvm/Support/FileSystem.h"
22061da546Spatrick 
23061da546Spatrick using namespace lldb_private;
24061da546Spatrick 
25061da546Spatrick const char *FileSystem::DEV_NULL = "nul";
26061da546Spatrick 
27061da546Spatrick const char *FileSystem::PATH_CONVERSION_ERROR =
28061da546Spatrick     "Error converting path between UTF-8 and native encoding";
29061da546Spatrick 
Symlink(const FileSpec & src,const FileSpec & dst)30061da546Spatrick Status FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) {
31061da546Spatrick   Status error;
32061da546Spatrick   std::wstring wsrc, wdst;
33*f6aab3d8Srobert   if (!llvm::ConvertUTF8toWide(src.GetPath(), wsrc) ||
34*f6aab3d8Srobert       !llvm::ConvertUTF8toWide(dst.GetPath(), wdst))
35061da546Spatrick     error.SetErrorString(PATH_CONVERSION_ERROR);
36061da546Spatrick   if (error.Fail())
37061da546Spatrick     return error;
38061da546Spatrick   DWORD attrib = ::GetFileAttributesW(wdst.c_str());
39061da546Spatrick   if (attrib == INVALID_FILE_ATTRIBUTES) {
40061da546Spatrick     error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
41061da546Spatrick     return error;
42061da546Spatrick   }
43061da546Spatrick   bool is_directory = !!(attrib & FILE_ATTRIBUTE_DIRECTORY);
44061da546Spatrick   DWORD flag = is_directory ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0;
45061da546Spatrick   BOOL result = ::CreateSymbolicLinkW(wsrc.c_str(), wdst.c_str(), flag);
46061da546Spatrick   if (!result)
47061da546Spatrick     error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
48061da546Spatrick   return error;
49061da546Spatrick }
50061da546Spatrick 
Readlink(const FileSpec & src,FileSpec & dst)51061da546Spatrick Status FileSystem::Readlink(const FileSpec &src, FileSpec &dst) {
52061da546Spatrick   Status error;
53061da546Spatrick   std::wstring wsrc;
54*f6aab3d8Srobert   if (!llvm::ConvertUTF8toWide(src.GetPath(), wsrc)) {
55061da546Spatrick     error.SetErrorString(PATH_CONVERSION_ERROR);
56061da546Spatrick     return error;
57061da546Spatrick   }
58061da546Spatrick 
59061da546Spatrick   HANDLE h = ::CreateFileW(wsrc.c_str(), GENERIC_READ,
60061da546Spatrick                            FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
61061da546Spatrick                            OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT, NULL);
62061da546Spatrick   if (h == INVALID_HANDLE_VALUE) {
63061da546Spatrick     error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
64061da546Spatrick     return error;
65061da546Spatrick   }
66061da546Spatrick 
67061da546Spatrick   std::vector<wchar_t> buf(PATH_MAX + 1);
68061da546Spatrick   // Subtract 1 from the path length since this function does not add a null
69061da546Spatrick   // terminator.
70061da546Spatrick   DWORD result = ::GetFinalPathNameByHandleW(
71061da546Spatrick       h, buf.data(), buf.size() - 1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
72061da546Spatrick   std::string path;
73061da546Spatrick   if (result == 0)
74061da546Spatrick     error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
75061da546Spatrick   else if (!llvm::convertWideToUTF8(buf.data(), path))
76061da546Spatrick     error.SetErrorString(PATH_CONVERSION_ERROR);
77061da546Spatrick   else
78061da546Spatrick     dst.SetFile(path, FileSpec::Style::native);
79061da546Spatrick 
80061da546Spatrick   ::CloseHandle(h);
81061da546Spatrick   return error;
82061da546Spatrick }
83061da546Spatrick 
ResolveSymbolicLink(const FileSpec & src,FileSpec & dst)84061da546Spatrick Status FileSystem::ResolveSymbolicLink(const FileSpec &src, FileSpec &dst) {
85061da546Spatrick   return Status("ResolveSymbolicLink() isn't implemented on Windows");
86061da546Spatrick }
87061da546Spatrick 
Fopen(const char * path,const char * mode)88061da546Spatrick FILE *FileSystem::Fopen(const char *path, const char *mode) {
89061da546Spatrick   std::wstring wpath, wmode;
90061da546Spatrick   if (!llvm::ConvertUTF8toWide(path, wpath))
91061da546Spatrick     return nullptr;
92061da546Spatrick   if (!llvm::ConvertUTF8toWide(mode, wmode))
93061da546Spatrick     return nullptr;
94061da546Spatrick   FILE *file;
95061da546Spatrick   if (_wfopen_s(&file, wpath.c_str(), wmode.c_str()) != 0)
96061da546Spatrick     return nullptr;
97061da546Spatrick   return file;
98061da546Spatrick }
99061da546Spatrick 
Open(const char * path,int flags,int mode)100061da546Spatrick int FileSystem::Open(const char *path, int flags, int mode) {
101061da546Spatrick   std::wstring wpath;
102061da546Spatrick   if (!llvm::ConvertUTF8toWide(path, wpath))
103061da546Spatrick     return -1;
104061da546Spatrick   int result;
105061da546Spatrick   ::_wsopen_s(&result, wpath.c_str(), flags, _SH_DENYNO, mode);
106061da546Spatrick   return result;
107061da546Spatrick }
108