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
24using namespace lldb_private;
25
26const char *FileSystem::DEV_NULL = "nul";
27
29 "Error converting path between UTF-8 and native encoding";
30
31Status FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) {
33 std::wstring wsrc, wdst;
34 if (!llvm::ConvertUTF8toWide(src.GetPath(), wsrc) ||
35 !llvm::ConvertUTF8toWide(dst.GetPath(), wdst))
37 if (error.Fail())
38 return error;
39 DWORD attrib = ::GetFileAttributesW(wdst.c_str());
40 if (attrib == INVALID_FILE_ATTRIBUTES) {
41 error = Status(::GetLastError(), lldb::eErrorTypeWin32);
42 return error;
43 }
44 bool is_directory = !!(attrib & FILE_ATTRIBUTE_DIRECTORY);
45 DWORD flag = is_directory ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0;
46 BOOL result = ::CreateSymbolicLinkW(wsrc.c_str(), wdst.c_str(), flag);
47 if (!result)
48 error = Status(::GetLastError(), lldb::eErrorTypeWin32);
49 return error;
50}
51
54 std::wstring wsrc;
55 if (!llvm::ConvertUTF8toWide(src.GetPath(), wsrc)) {
57 return error;
58 }
59
60 HANDLE h = ::CreateFileW(
61 wsrc.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
62 OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT, nullptr);
63 if (h == INVALID_HANDLE_VALUE) {
64 error = Status(::GetLastError(), lldb::eErrorTypeWin32);
65 return error;
66 }
67
68 std::vector<wchar_t> buf(PATH_MAX + 1);
69 // Subtract 1 from the path length since this function does not add a null
70 // terminator. The result is an extended-length ("\\?\") path.
71 DWORD result = ::GetFinalPathNameByHandleW(
72 h, buf.data(), buf.size() - 1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
73 std::string path;
74 if (result == 0)
75 error = Status(::GetLastError(), lldb::eErrorTypeWin32);
76 else if (!llvm::convertWideToUTF8(buf.data(), path))
78 else
79 dst.SetFile(StripExtendedLengthPrefix(path), FileSpec::Style::native);
80
81 ::CloseHandle(h);
82 return error;
83}
84
87 "ResolveSymbolicLink() isn't implemented on Windows");
88}
89
90FILE *FileSystem::Fopen(const char *path, const char *mode) {
91 std::wstring wpath, wmode;
92 if (!llvm::ConvertUTF8toWide(path, wpath))
93 return nullptr;
94 if (!llvm::ConvertUTF8toWide(mode, wmode))
95 return nullptr;
96 FILE *file;
97 if (_wfopen_s(&file, wpath.c_str(), wmode.c_str()) != 0)
98 return nullptr;
99 return file;
100}
101
102int FileSystem::Open(const char *path, int flags, int mode) {
103 std::wstring wpath;
104 if (!llvm::ConvertUTF8toWide(path, wpath))
105 return -1;
106 // All other bits are rejected by _wsopen_s
107 mode = mode & (_S_IREAD | _S_IWRITE);
108 int result;
109 ::_wsopen_s(&result, wpath.c_str(), flags, _SH_DENYNO, mode);
110 return result;
111}
static llvm::raw_ostream & error(Stream &strm)
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.
#define PATH_MAX