LLDB mainline
windows/FileSystem.cpp
Go to the documentation of this file.
1//===-- FileSystem.cpp ----------------------------------------------------===//
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
10
11#include <share.h>
12#include <shellapi.h>
13#include <sys/stat.h>
14#include <sys/types.h>
15
20
21#include "llvm/Support/ConvertUTF.h"
22#include "llvm/Support/FileSystem.h"
23#include "llvm/Support/Path.h"
24#include "llvm/Support/Windows/WindowsSupport.h"
25
26using namespace lldb_private;
27
28const char *FileSystem::DEV_NULL = "nul";
29
31 "Error converting path between UTF-8 and native encoding";
32
33Status FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) {
35 llvm::SmallVector<wchar_t, MAX_PATH> wsrc, wdst_long;
36 std::wstring wdst;
37 if (llvm::sys::windows::widenPath(src.GetPath(), wsrc) ||
38 llvm::sys::windows::widenPath(dst.GetPath(), wdst_long) ||
39 !llvm::ConvertUTF8toWide(dst.GetPath(), wdst))
41 if (error.Fail())
42 return error;
43 DWORD attrib = ::GetFileAttributesW(wdst_long.data());
44 if (attrib == INVALID_FILE_ATTRIBUTES) {
45 error = Status(::GetLastError(), lldb::eErrorTypeWin32);
46 return error;
47 }
48 bool is_directory = !!(attrib & FILE_ATTRIBUTE_DIRECTORY);
49 DWORD flag = is_directory ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0;
50 // The link stores its target as given. widenPath makes a long relative
51 // target absolute, which would change what the link points to, so only an
52 // absolute target gets the "\\?\" prefix.
53 const wchar_t *target = llvm::sys::path::is_absolute(dst.GetPath())
54 ? wdst_long.data()
55 : wdst.c_str();
56 BOOL result = ::CreateSymbolicLinkW(wsrc.data(), target, flag);
57 if (!result)
58 error = Status(::GetLastError(), lldb::eErrorTypeWin32);
59 return error;
60}
61
64 llvm::SmallVector<wchar_t, MAX_PATH> wsrc;
65 if (llvm::sys::windows::widenPath(src.GetPath(), wsrc)) {
67 return error;
68 }
69
70 HANDLE h = ::CreateFileW(
71 wsrc.data(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
72 OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT, nullptr);
73 if (h == INVALID_HANDLE_VALUE) {
74 error = Status(::GetLastError(), lldb::eErrorTypeWin32);
75 return error;
76 }
77
78 llvm::SmallVector<wchar_t, MAX_PATH> buf(MAX_PATH + 1);
79 // Subtract 1 from the path length since this function does not add a null
80 // terminator. The result is an extended-length ("\\?\") path.
81 DWORD result = ::GetFinalPathNameByHandleW(
82 h, buf.data(), buf.size() - 1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
83 while (result >= buf.size() - 1) {
84 buf.resize(result + 1);
85 result = ::GetFinalPathNameByHandleW(
86 h, buf.data(), buf.size() - 1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
87 }
88 std::string path;
89 if (result == 0)
90 error = Status(::GetLastError(), lldb::eErrorTypeWin32);
91 else if (!llvm::convertWideToUTF8(std::wstring(buf.data(), result), path))
93 else
94 dst.SetFile(StripExtendedLengthPrefix(path), FileSpec::Style::native);
95
96 ::CloseHandle(h);
97 return error;
98}
99
102 "ResolveSymbolicLink() isn't implemented on Windows");
103}
104
105FILE *FileSystem::Fopen(const char *path, const char *mode) {
106 llvm::SmallVector<wchar_t, MAX_PATH> wpath;
107 std::wstring wmode;
108 if (llvm::sys::windows::widenPath(path, wpath))
109 return nullptr;
110 if (!llvm::ConvertUTF8toWide(mode, wmode))
111 return nullptr;
112 FILE *file;
113 if (_wfopen_s(&file, wpath.data(), wmode.c_str()) != 0)
114 return nullptr;
115 return file;
116}
117
118int FileSystem::Open(const char *path, int flags, int mode) {
119 llvm::SmallVector<wchar_t, MAX_PATH> wpath;
120 if (llvm::sys::windows::widenPath(path, wpath))
121 return -1;
122 // All other bits are rejected by _wsopen_s
123 mode = mode & (_S_IREAD | _S_IWRITE);
124 int result;
125 ::_wsopen_s(&result, wpath.data(), flags, _SH_DENYNO, mode);
126 return result;
127}
static llvm::raw_ostream & error(Stream &strm)
#define MAX_PATH
void * HANDLE
A file utility class.
Definition FileSpec.h:56
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition FileSpec.cpp:174
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:380
static const char * PATH_CONVERSION_ERROR
Definition FileSystem.h:33
static const char * DEV_NULL
Definition FileSystem.h:32
Status Symlink(const FileSpec &src, const FileSpec &dst)
Status ResolveSymbolicLink(const FileSpec &src, FileSpec &dst)
int Open(const char *path, int flags, int mode=0600)
Wraps open in a platform-independent way.
Status Readlink(const FileSpec &src, FileSpec &dst)
FILE * Fopen(const char *path, const char *mode)
Wraps fopen in a platform-independent way.
An error handling class.
Definition Status.h:118
static Status FromErrorString(const char *str)
Definition Status.h:141
A class that represents a running process on the host machine.
std::string StripExtendedLengthPrefix(llvm::StringRef path)
Convert an extended-length path to a regular DOS path: "\\?\C:\a" becomes "C:\a" and "\\?...
Definition PathUtils.h:21
@ eErrorTypeWin32
Standard Win32 error codes.