mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
Update LLVM from stable upstream (#1653)
Replace CheckedMalloc with upstream safe_malloc.
This commit is contained in:
@@ -8,9 +8,9 @@
|
||||
*===------------------------------------------------------------------------=*/
|
||||
/*
|
||||
* Copyright 2001-2004 Unicode, Inc.
|
||||
*
|
||||
*
|
||||
* Disclaimer
|
||||
*
|
||||
*
|
||||
* This source code is provided as is by Unicode, Inc. No claims are
|
||||
* made as to fitness for any particular purpose. No warranties of any
|
||||
* kind are expressed or implied. The recipient agrees to determine
|
||||
@@ -18,9 +18,9 @@
|
||||
* purchased on magnetic or optical media from Unicode, Inc., the
|
||||
* sole remedy for any claim will be exchange of defective media
|
||||
* within 90 days of receipt.
|
||||
*
|
||||
*
|
||||
* Limitations on Rights to Redistribute This Code
|
||||
*
|
||||
*
|
||||
* Unicode, Inc. hereby grants the right to freely use the information
|
||||
* supplied in this file in the creation of products supporting the
|
||||
* Unicode Standard, and to make copies of this file in any form
|
||||
@@ -117,7 +117,7 @@ static const char trailingBytesForUTF8[256] = {
|
||||
* This table contains as many values as there might be trailing bytes
|
||||
* in a UTF-8 sequence.
|
||||
*/
|
||||
static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
|
||||
static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
|
||||
0x03C82080UL, 0xFA082080UL, 0x82082080UL };
|
||||
|
||||
/*
|
||||
@@ -143,7 +143,7 @@ static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
ConversionResult ConvertUTF32toUTF16 (
|
||||
const UTF32** sourceStart, const UTF32* sourceEnd,
|
||||
const UTF32** sourceStart, const UTF32* sourceEnd,
|
||||
UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
|
||||
ConversionResult result = conversionOK;
|
||||
const UTF32* source = *sourceStart;
|
||||
@@ -192,7 +192,7 @@ ConversionResult ConvertUTF32toUTF16 (
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
ConversionResult ConvertUTF16toUTF32 (
|
||||
const UTF16** sourceStart, const UTF16* sourceEnd,
|
||||
const UTF16** sourceStart, const UTF16* sourceEnd,
|
||||
UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
|
||||
ConversionResult result = conversionOK;
|
||||
const UTF16* source = *sourceStart;
|
||||
@@ -246,7 +246,7 @@ if (result == sourceIllegal) {
|
||||
return result;
|
||||
}
|
||||
ConversionResult ConvertUTF16toUTF8 (
|
||||
const UTF16** sourceStart, const UTF16* sourceEnd,
|
||||
const UTF16** sourceStart, const UTF16* sourceEnd,
|
||||
UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
|
||||
ConversionResult result = conversionOK;
|
||||
const UTF16* source = *sourceStart;
|
||||
@@ -255,7 +255,7 @@ ConversionResult ConvertUTF16toUTF8 (
|
||||
UTF32 ch;
|
||||
unsigned short bytesToWrite = 0;
|
||||
const UTF32 byteMask = 0xBF;
|
||||
const UTF32 byteMark = 0x80;
|
||||
const UTF32 byteMark = 0x80;
|
||||
const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */
|
||||
ch = *source++;
|
||||
/* If we have a surrogate pair, convert to UTF32 first. */
|
||||
@@ -316,7 +316,7 @@ ConversionResult ConvertUTF16toUTF8 (
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
ConversionResult ConvertUTF32toUTF8 (
|
||||
const UTF32** sourceStart, const UTF32* sourceEnd,
|
||||
const UTF32** sourceStart, const UTF32* sourceEnd,
|
||||
UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
|
||||
ConversionResult result = conversionOK;
|
||||
const UTF32* source = *sourceStart;
|
||||
@@ -325,7 +325,7 @@ ConversionResult ConvertUTF32toUTF8 (
|
||||
UTF32 ch;
|
||||
unsigned short bytesToWrite = 0;
|
||||
const UTF32 byteMask = 0xBF;
|
||||
const UTF32 byteMark = 0x80;
|
||||
const UTF32 byteMark = 0x80;
|
||||
ch = *source++;
|
||||
if (flags == strictConversion ) {
|
||||
/* UTF-16 surrogate values are illegal in UTF-32 */
|
||||
@@ -347,7 +347,7 @@ ConversionResult ConvertUTF32toUTF8 (
|
||||
ch = UNI_REPLACEMENT_CHAR;
|
||||
result = sourceIllegal;
|
||||
}
|
||||
|
||||
|
||||
target += bytesToWrite;
|
||||
if (target > targetEnd) {
|
||||
--source; /* Back up source pointer! */
|
||||
@@ -540,7 +540,7 @@ Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd) {
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
ConversionResult ConvertUTF8toUTF16 (
|
||||
const UTF8** sourceStart, const UTF8* sourceEnd,
|
||||
const UTF8** sourceStart, const UTF8* sourceEnd,
|
||||
UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
|
||||
ConversionResult result = conversionOK;
|
||||
const UTF8* source = *sourceStart;
|
||||
@@ -613,7 +613,7 @@ ConversionResult ConvertUTF8toUTF16 (
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
static ConversionResult ConvertUTF8toUTF32Impl(
|
||||
const UTF8** sourceStart, const UTF8* sourceEnd,
|
||||
const UTF8** sourceStart, const UTF8* sourceEnd,
|
||||
UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags,
|
||||
Boolean InputIsPartial) {
|
||||
ConversionResult result = conversionOK;
|
||||
|
||||
138
wpiutil/src/main/native/cpp/llvm/Error.cpp
Normal file
138
wpiutil/src/main/native/cpp/llvm/Error.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
//===----- lib/Support/Error.cpp - Error and associated utilities ---------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "wpi/Error.h"
|
||||
#include "wpi/Twine.h"
|
||||
#include "wpi/ErrorHandling.h"
|
||||
#include "wpi/ManagedStatic.h"
|
||||
#include <system_error>
|
||||
|
||||
using namespace wpi;
|
||||
|
||||
namespace {
|
||||
|
||||
enum class ErrorErrorCode : int {
|
||||
MultipleErrors = 1,
|
||||
FileError,
|
||||
InconvertibleError
|
||||
};
|
||||
|
||||
// FIXME: This class is only here to support the transition to wpi::Error. It
|
||||
// will be removed once this transition is complete. Clients should prefer to
|
||||
// deal with the Error value directly, rather than converting to error_code.
|
||||
class ErrorErrorCategory : public std::error_category {
|
||||
public:
|
||||
const char *name() const noexcept override { return "Error"; }
|
||||
|
||||
std::string message(int condition) const override {
|
||||
switch (static_cast<ErrorErrorCode>(condition)) {
|
||||
case ErrorErrorCode::MultipleErrors:
|
||||
return "Multiple errors";
|
||||
case ErrorErrorCode::InconvertibleError:
|
||||
return "Inconvertible error value. An error has occurred that could "
|
||||
"not be converted to a known std::error_code. Please file a "
|
||||
"bug.";
|
||||
case ErrorErrorCode::FileError:
|
||||
return "A file error occurred.";
|
||||
}
|
||||
wpi_unreachable("Unhandled error code");
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
|
||||
|
||||
namespace wpi {
|
||||
|
||||
void ErrorInfoBase::anchor() {}
|
||||
char ErrorInfoBase::ID = 0;
|
||||
char ErrorList::ID = 0;
|
||||
void ECError::anchor() {}
|
||||
char ECError::ID = 0;
|
||||
char StringError::ID = 0;
|
||||
char FileError::ID = 0;
|
||||
|
||||
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
|
||||
if (!E)
|
||||
return;
|
||||
OS << ErrorBanner;
|
||||
handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
|
||||
EI.log(OS);
|
||||
OS << "\n";
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
std::error_code ErrorList::convertToErrorCode() const {
|
||||
return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
|
||||
*ErrorErrorCat);
|
||||
}
|
||||
|
||||
std::error_code inconvertibleErrorCode() {
|
||||
return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
|
||||
*ErrorErrorCat);
|
||||
}
|
||||
|
||||
std::error_code FileError::convertToErrorCode() const {
|
||||
return std::error_code(static_cast<int>(ErrorErrorCode::FileError),
|
||||
*ErrorErrorCat);
|
||||
}
|
||||
|
||||
Error errorCodeToError(std::error_code EC) {
|
||||
if (!EC)
|
||||
return Error::success();
|
||||
return Error(wpi::make_unique<ECError>(ECError(EC)));
|
||||
}
|
||||
|
||||
std::error_code errorToErrorCode(Error Err) {
|
||||
std::error_code EC;
|
||||
handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
|
||||
EC = EI.convertToErrorCode();
|
||||
});
|
||||
if (EC == inconvertibleErrorCode())
|
||||
report_fatal_error(EC.message());
|
||||
return EC;
|
||||
}
|
||||
|
||||
StringError::StringError(std::error_code EC, const Twine &S)
|
||||
: Msg(S.str()), EC(EC) {}
|
||||
|
||||
StringError::StringError(const Twine &S, std::error_code EC)
|
||||
: Msg(S.str()), EC(EC), PrintMsgOnly(true) {}
|
||||
|
||||
void StringError::log(raw_ostream &OS) const {
|
||||
if (PrintMsgOnly) {
|
||||
OS << Msg;
|
||||
} else {
|
||||
OS << EC.message();
|
||||
if (!Msg.empty())
|
||||
OS << (" " + Msg);
|
||||
}
|
||||
}
|
||||
|
||||
std::error_code StringError::convertToErrorCode() const {
|
||||
return EC;
|
||||
}
|
||||
|
||||
Error createStringError(std::error_code EC, char const *Msg) {
|
||||
return make_error<StringError>(Msg, EC);
|
||||
}
|
||||
|
||||
void report_fatal_error(Error Err, bool GenCrashDiag) {
|
||||
assert(Err && "report_fatal_error called with success value");
|
||||
std::string ErrMsg;
|
||||
{
|
||||
raw_string_ostream ErrStream(ErrMsg);
|
||||
logAllUnhandledErrors(std::move(Err), ErrStream);
|
||||
}
|
||||
report_fatal_error(ErrMsg);
|
||||
}
|
||||
|
||||
} // end namespace wpi
|
||||
@@ -12,7 +12,191 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "wpi/ErrorHandling.h"
|
||||
#include "wpi/SmallVector.h"
|
||||
#include "wpi/Twine.h"
|
||||
#include "wpi/Error.h"
|
||||
#include "wpi/WindowsError.h"
|
||||
#include "wpi/raw_ostream.h"
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <mutex>
|
||||
#include <new>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#include <io.h>
|
||||
#endif
|
||||
|
||||
using namespace wpi;
|
||||
|
||||
static fatal_error_handler_t ErrorHandler = nullptr;
|
||||
static void *ErrorHandlerUserData = nullptr;
|
||||
|
||||
static fatal_error_handler_t BadAllocErrorHandler = nullptr;
|
||||
static void *BadAllocErrorHandlerUserData = nullptr;
|
||||
|
||||
// Mutexes to synchronize installing error handlers and calling error handlers.
|
||||
// Do not use ManagedStatic, or that may allocate memory while attempting to
|
||||
// report an OOM.
|
||||
//
|
||||
// This usage of std::mutex has to be conditionalized behind ifdefs because
|
||||
// of this script:
|
||||
// compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh
|
||||
// That script attempts to statically link the LLVM symbolizer library with the
|
||||
// STL and hide all of its symbols with 'opt -internalize'. To reduce size, it
|
||||
// cuts out the threading portions of the hermetic copy of libc++ that it
|
||||
// builds. We can remove these ifdefs if that script goes away.
|
||||
static std::mutex ErrorHandlerMutex;
|
||||
static std::mutex BadAllocErrorHandlerMutex;
|
||||
|
||||
void wpi::install_fatal_error_handler(fatal_error_handler_t handler,
|
||||
void *user_data) {
|
||||
std::lock_guard<std::mutex> Lock(ErrorHandlerMutex);
|
||||
assert(!ErrorHandler && "Error handler already registered!\n");
|
||||
ErrorHandler = handler;
|
||||
ErrorHandlerUserData = user_data;
|
||||
}
|
||||
|
||||
void wpi::remove_fatal_error_handler() {
|
||||
std::lock_guard<std::mutex> Lock(ErrorHandlerMutex);
|
||||
ErrorHandler = nullptr;
|
||||
ErrorHandlerUserData = nullptr;
|
||||
}
|
||||
|
||||
void wpi::report_fatal_error(const char *Reason, bool GenCrashDiag) {
|
||||
report_fatal_error(Twine(Reason), GenCrashDiag);
|
||||
}
|
||||
|
||||
void wpi::report_fatal_error(const std::string &Reason, bool GenCrashDiag) {
|
||||
report_fatal_error(Twine(Reason), GenCrashDiag);
|
||||
}
|
||||
|
||||
void wpi::report_fatal_error(StringRef Reason, bool GenCrashDiag) {
|
||||
report_fatal_error(Twine(Reason), GenCrashDiag);
|
||||
}
|
||||
|
||||
void wpi::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
|
||||
wpi::fatal_error_handler_t handler = nullptr;
|
||||
void* handlerData = nullptr;
|
||||
{
|
||||
// Only acquire the mutex while reading the handler, so as not to invoke a
|
||||
// user-supplied callback under a lock.
|
||||
std::lock_guard<std::mutex> Lock(ErrorHandlerMutex);
|
||||
handler = ErrorHandler;
|
||||
handlerData = ErrorHandlerUserData;
|
||||
}
|
||||
|
||||
if (handler) {
|
||||
handler(handlerData, Reason.str(), GenCrashDiag);
|
||||
} else {
|
||||
// Blast the result out to stderr. We don't try hard to make sure this
|
||||
// succeeds (e.g. handling EINTR) and we can't use errs() here because
|
||||
// raw ostreams can call report_fatal_error.
|
||||
SmallVector<char, 64> Buffer;
|
||||
raw_svector_ostream OS(Buffer);
|
||||
OS << "LLVM ERROR: " << Reason << "\n";
|
||||
StringRef MessageStr = OS.str();
|
||||
#ifdef _WIN32
|
||||
int written = ::_write(2, MessageStr.data(), MessageStr.size());
|
||||
#else
|
||||
ssize_t written = ::write(2, MessageStr.data(), MessageStr.size());
|
||||
#endif
|
||||
(void)written; // If something went wrong, we deliberately just give up.
|
||||
}
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void wpi::install_bad_alloc_error_handler(fatal_error_handler_t handler,
|
||||
void *user_data) {
|
||||
std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
|
||||
assert(!ErrorHandler && "Bad alloc error handler already registered!\n");
|
||||
BadAllocErrorHandler = handler;
|
||||
BadAllocErrorHandlerUserData = user_data;
|
||||
}
|
||||
|
||||
void wpi::remove_bad_alloc_error_handler() {
|
||||
std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
|
||||
BadAllocErrorHandler = nullptr;
|
||||
BadAllocErrorHandlerUserData = nullptr;
|
||||
}
|
||||
|
||||
void wpi::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) {
|
||||
fatal_error_handler_t Handler = nullptr;
|
||||
void *HandlerData = nullptr;
|
||||
{
|
||||
// Only acquire the mutex while reading the handler, so as not to invoke a
|
||||
// user-supplied callback under a lock.
|
||||
std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
|
||||
Handler = BadAllocErrorHandler;
|
||||
HandlerData = BadAllocErrorHandlerUserData;
|
||||
}
|
||||
|
||||
if (Handler) {
|
||||
Handler(HandlerData, Reason, GenCrashDiag);
|
||||
wpi_unreachable("bad alloc handler should not return");
|
||||
}
|
||||
|
||||
// Don't call the normal error handler. It may allocate memory. Directly write
|
||||
// an OOM to stderr and abort.
|
||||
char OOMMessage[] = "LLVM ERROR: out of memory\n";
|
||||
#ifdef _WIN32
|
||||
int written = ::_write(2, OOMMessage, strlen(OOMMessage));
|
||||
#else
|
||||
ssize_t written = ::write(2, OOMMessage, strlen(OOMMessage));
|
||||
#endif
|
||||
(void)written;
|
||||
abort();
|
||||
}
|
||||
|
||||
// Causes crash on allocation failure. It is called prior to the handler set by
|
||||
// 'install_bad_alloc_error_handler'.
|
||||
static void out_of_memory_new_handler() {
|
||||
wpi::report_bad_alloc_error("Allocation failed");
|
||||
}
|
||||
|
||||
// Installs new handler that causes crash on allocation failure. It does not
|
||||
// need to be called explicitly, if this file is linked to application, because
|
||||
// in this case it is called during construction of 'new_handler_installer'.
|
||||
void wpi::install_out_of_memory_new_handler() {
|
||||
static bool out_of_memory_new_handler_installed = false;
|
||||
if (!out_of_memory_new_handler_installed) {
|
||||
std::set_new_handler(out_of_memory_new_handler);
|
||||
out_of_memory_new_handler_installed = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Static object that causes installation of 'out_of_memory_new_handler' before
|
||||
// execution of 'main'.
|
||||
static class NewHandlerInstaller {
|
||||
public:
|
||||
NewHandlerInstaller() {
|
||||
install_out_of_memory_new_handler();
|
||||
}
|
||||
} new_handler_installer;
|
||||
|
||||
void wpi::wpi_unreachable_internal(const char *msg, const char *file,
|
||||
unsigned line) {
|
||||
// This code intentionally doesn't call the ErrorHandler callback, because
|
||||
// wpi_unreachable is intended to be used to indicate "impossible"
|
||||
// situations, and not legitimate runtime errors.
|
||||
if (msg)
|
||||
errs() << msg << "\n";
|
||||
errs() << "UNREACHABLE executed";
|
||||
if (file)
|
||||
errs() << " at " << file << ":" << line;
|
||||
errs() << "!\n";
|
||||
abort();
|
||||
#ifdef LLVM_BUILTIN_UNREACHABLE
|
||||
// Windows systems and possibly others don't declare abort() to be noreturn,
|
||||
// so use the unreachable builtin to avoid a Clang self-host warning.
|
||||
LLVM_BUILTIN_UNREACHABLE;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
|
||||
@@ -20,10 +20,10 @@ using namespace wpi;
|
||||
// Provide a definition and static initializer for the fixed seed. This
|
||||
// initializer should always be zero to ensure its value can never appear to be
|
||||
// non-zero, even during dynamic initialization.
|
||||
size_t wpi::hashing::detail::fixed_seed_override = 0;
|
||||
uint64_t wpi::hashing::detail::fixed_seed_override = 0;
|
||||
|
||||
// Implement the function for forced setting of the fixed seed.
|
||||
// FIXME: Use atomic operations here so that there is no data race.
|
||||
void wpi::set_fixed_execution_hash_seed(size_t fixed_value) {
|
||||
void wpi::set_fixed_execution_hash_seed(uint64_t fixed_value) {
|
||||
hashing::detail::fixed_seed_override = fixed_value;
|
||||
}
|
||||
|
||||
72
wpiutil/src/main/native/cpp/llvm/ManagedStatic.cpp
Normal file
72
wpiutil/src/main/native/cpp/llvm/ManagedStatic.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
//===-- ManagedStatic.cpp - Static Global wrapper -------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file implements the ManagedStatic class and wpi_shutdown().
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "wpi/ManagedStatic.h"
|
||||
#include "wpi/mutex.h"
|
||||
#include <cassert>
|
||||
#include <mutex>
|
||||
using namespace wpi;
|
||||
|
||||
static const ManagedStaticBase *StaticList = nullptr;
|
||||
static wpi::mutex *ManagedStaticMutex = nullptr;
|
||||
static std::once_flag mutex_init_flag;
|
||||
|
||||
static void initializeMutex() {
|
||||
ManagedStaticMutex = new wpi::mutex();
|
||||
}
|
||||
|
||||
static wpi::mutex* getManagedStaticMutex() {
|
||||
std::call_once(mutex_init_flag, initializeMutex);
|
||||
return ManagedStaticMutex;
|
||||
}
|
||||
|
||||
void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
|
||||
void (*Deleter)(void*)) const {
|
||||
assert(Creator);
|
||||
std::lock_guard<wpi::mutex> Lock(*getManagedStaticMutex());
|
||||
|
||||
if (!Ptr.load(std::memory_order_relaxed)) {
|
||||
void *Tmp = Creator();
|
||||
|
||||
Ptr.store(Tmp, std::memory_order_release);
|
||||
DeleterFn = Deleter;
|
||||
|
||||
// Add to list of managed statics.
|
||||
Next = StaticList;
|
||||
StaticList = this;
|
||||
}
|
||||
}
|
||||
|
||||
void ManagedStaticBase::destroy() const {
|
||||
assert(DeleterFn && "ManagedStatic not initialized correctly!");
|
||||
assert(StaticList == this &&
|
||||
"Not destroyed in reverse order of construction?");
|
||||
// Unlink from list.
|
||||
StaticList = Next;
|
||||
Next = nullptr;
|
||||
|
||||
// Destroy memory.
|
||||
DeleterFn(Ptr);
|
||||
|
||||
// Cleanup.
|
||||
Ptr = nullptr;
|
||||
DeleterFn = nullptr;
|
||||
}
|
||||
|
||||
/// wpi_shutdown - Deallocate and destroy all ManagedStatic variables.
|
||||
void wpi::wpi_shutdown() {
|
||||
std::lock_guard<wpi::mutex> Lock(*getManagedStaticMutex());
|
||||
|
||||
while (StaticList)
|
||||
StaticList->destroy();
|
||||
}
|
||||
@@ -12,7 +12,12 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "wpi/Path.h"
|
||||
|
||||
#include "wpi/ArrayRef.h"
|
||||
#include "wpi/Endian.h"
|
||||
#include "wpi/Errc.h"
|
||||
#include "wpi/ErrorHandling.h"
|
||||
#include "wpi/FileSystem.h"
|
||||
#include "wpi/SmallString.h"
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
|
||||
@@ -22,10 +27,8 @@
|
||||
#include <io.h>
|
||||
#endif
|
||||
|
||||
#include "wpi/FileSystem.h"
|
||||
#include "wpi/SmallString.h"
|
||||
|
||||
using namespace wpi;
|
||||
using namespace wpi::support::endian;
|
||||
|
||||
namespace {
|
||||
using wpi::StringRef;
|
||||
@@ -444,7 +447,7 @@ void replace_path_prefix(SmallVectorImpl<char> &Path,
|
||||
|
||||
// If prefixes have the same size we can simply copy the new one over.
|
||||
if (OldPrefix.size() == NewPrefix.size()) {
|
||||
std::copy(NewPrefix.begin(), NewPrefix.end(), Path.begin());
|
||||
wpi::copy(NewPrefix, Path.begin());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -674,9 +677,8 @@ std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
static std::error_code make_absolute(const Twine ¤t_directory,
|
||||
SmallVectorImpl<char> &path,
|
||||
bool use_current_directory) {
|
||||
void make_absolute(const Twine ¤t_directory,
|
||||
SmallVectorImpl<char> &path) {
|
||||
StringRef p(path.data(), path.size());
|
||||
|
||||
bool rootDirectory = path::has_root_directory(p);
|
||||
@@ -685,14 +687,11 @@ static std::error_code make_absolute(const Twine ¤t_directory,
|
||||
|
||||
// Already absolute.
|
||||
if (rootName && rootDirectory)
|
||||
return std::error_code();
|
||||
return;
|
||||
|
||||
// All of the following conditions will need the current directory.
|
||||
SmallString<128> current_dir;
|
||||
if (use_current_directory)
|
||||
current_directory.toVector(current_dir);
|
||||
else if (std::error_code ec = current_path(current_dir))
|
||||
return ec;
|
||||
current_directory.toVector(current_dir);
|
||||
|
||||
// Relative path. Prepend the current directory.
|
||||
if (!rootName && !rootDirectory) {
|
||||
@@ -700,7 +699,7 @@ static std::error_code make_absolute(const Twine ¤t_directory,
|
||||
path::append(current_dir, p);
|
||||
// Set path to the result.
|
||||
path.swap(current_dir);
|
||||
return std::error_code();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!rootName && rootDirectory) {
|
||||
@@ -709,7 +708,7 @@ static std::error_code make_absolute(const Twine ¤t_directory,
|
||||
path::append(curDirRootName, p);
|
||||
// Set path to the result.
|
||||
path.swap(curDirRootName);
|
||||
return std::error_code();
|
||||
return;
|
||||
}
|
||||
|
||||
if (rootName && !rootDirectory) {
|
||||
@@ -721,21 +720,23 @@ static std::error_code make_absolute(const Twine ¤t_directory,
|
||||
SmallString<128> res;
|
||||
path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
|
||||
path.swap(res);
|
||||
return std::error_code();
|
||||
return;
|
||||
}
|
||||
|
||||
assert(false && "All rootName and rootDirectory combinations should have "
|
||||
wpi_unreachable("All rootName and rootDirectory combinations should have "
|
||||
"occurred above!");
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
std::error_code make_absolute(const Twine ¤t_directory,
|
||||
SmallVectorImpl<char> &path) {
|
||||
return make_absolute(current_directory, path, true);
|
||||
}
|
||||
|
||||
std::error_code make_absolute(SmallVectorImpl<char> &path) {
|
||||
return make_absolute(Twine(), path, false);
|
||||
if (path::is_absolute(path))
|
||||
return {};
|
||||
|
||||
SmallString<128> current_dir;
|
||||
if (std::error_code ec = current_path(current_dir))
|
||||
return ec;
|
||||
|
||||
make_absolute(current_dir, path);
|
||||
return {};
|
||||
}
|
||||
|
||||
bool exists(const basic_file_status &status) {
|
||||
@@ -803,12 +804,13 @@ std::error_code is_other(const Twine &Path, bool &Result) {
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
void directory_entry::replace_filename(const Twine &filename,
|
||||
basic_file_status st) {
|
||||
SmallString<128> path = path::parent_path(Path);
|
||||
path::append(path, filename);
|
||||
Path = path.str();
|
||||
Status = st;
|
||||
void directory_entry::replace_filename(const Twine &Filename, file_type Type,
|
||||
basic_file_status Status) {
|
||||
SmallString<128> PathStr = path::parent_path(Path);
|
||||
path::append(PathStr, Filename);
|
||||
this->Path = PathStr.str();
|
||||
this->Type = Type;
|
||||
this->Status = Status;
|
||||
}
|
||||
|
||||
ErrorOr<perms> getPermissions(const Twine &Path) {
|
||||
@@ -829,20 +831,3 @@ ErrorOr<perms> getPermissions(const Twine &Path) {
|
||||
#else
|
||||
#include "Unix/Path.inc"
|
||||
#endif
|
||||
|
||||
namespace wpi {
|
||||
namespace sys {
|
||||
namespace path {
|
||||
|
||||
bool user_cache_directory(SmallVectorImpl<char> &Result, const Twine &Path1,
|
||||
const Twine &Path2, const Twine &Path3) {
|
||||
if (getUserCacheDir(Result)) {
|
||||
append(Result, Path1, Path2, Path3);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // end namespace path
|
||||
} // end namsspace sys
|
||||
} // end namespace wpi
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
#include "wpi/SmallPtrSet.h"
|
||||
#include "wpi/DenseMapInfo.h"
|
||||
#include "wpi/MathExtras.h"
|
||||
#include "wpi/memory.h"
|
||||
#include "wpi/ErrorHandling.h"
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
@@ -32,7 +32,8 @@ void SmallPtrSetImplBase::shrink_and_clear() {
|
||||
NumNonEmpty = NumTombstones = 0;
|
||||
|
||||
// Install the new array. Clear all the buckets to empty.
|
||||
CurArray = (const void**)CheckedMalloc(sizeof(void*) * CurArraySize);
|
||||
CurArray = (const void**)safe_malloc(sizeof(void*) * CurArraySize);
|
||||
|
||||
memset(CurArray, -1, CurArraySize*sizeof(void*));
|
||||
}
|
||||
|
||||
@@ -96,7 +97,10 @@ void SmallPtrSetImplBase::Grow(unsigned NewSize) {
|
||||
bool WasSmall = isSmall();
|
||||
|
||||
// Install the new array. Clear all the buckets to empty.
|
||||
CurArray = (const void**) CheckedMalloc(sizeof(void*) * NewSize);
|
||||
const void **NewBuckets = (const void**) safe_malloc(sizeof(void*) * NewSize);
|
||||
|
||||
// Reset member only if memory was allocated successfully
|
||||
CurArray = NewBuckets;
|
||||
CurArraySize = NewSize;
|
||||
memset(CurArray, -1, NewSize*sizeof(void*));
|
||||
|
||||
@@ -123,7 +127,7 @@ SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
|
||||
CurArray = SmallArray;
|
||||
// Otherwise, allocate new heap space (unless we were the same size)
|
||||
} else {
|
||||
CurArray = (const void**)CheckedMalloc(sizeof(void*) * that.CurArraySize);
|
||||
CurArray = (const void**)safe_malloc(sizeof(void*) * that.CurArraySize);
|
||||
}
|
||||
|
||||
// Copy over the that array.
|
||||
@@ -152,15 +156,12 @@ void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
|
||||
// Otherwise, allocate new heap space (unless we were the same size)
|
||||
} else if (CurArraySize != RHS.CurArraySize) {
|
||||
if (isSmall())
|
||||
CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
|
||||
CurArray = (const void**)safe_malloc(sizeof(void*) * RHS.CurArraySize);
|
||||
else {
|
||||
const void **T = (const void**)realloc(CurArray,
|
||||
const void **T = (const void**)safe_realloc(CurArray,
|
||||
sizeof(void*) * RHS.CurArraySize);
|
||||
if (!T)
|
||||
free(CurArray);
|
||||
CurArray = T;
|
||||
}
|
||||
assert(CurArray && "Failed to allocate memory?");
|
||||
}
|
||||
|
||||
CopyHelper(RHS);
|
||||
|
||||
@@ -12,30 +12,32 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "wpi/SmallVector.h"
|
||||
#include "wpi/memory.h"
|
||||
#include "wpi/MemAlloc.h"
|
||||
using namespace wpi;
|
||||
|
||||
/// grow_pod - This is an implementation of the grow() method which only works
|
||||
/// on POD-like datatypes and is out of line to reduce code duplication.
|
||||
void SmallVectorBase::grow_pod(void *FirstEl, size_t MinSizeInBytes,
|
||||
void SmallVectorBase::grow_pod(void *FirstEl, size_t MinCapacity,
|
||||
size_t TSize) {
|
||||
size_t CurSizeBytes = size_in_bytes();
|
||||
size_t NewCapacityInBytes = 2 * capacity_in_bytes() + TSize; // Always grow.
|
||||
if (NewCapacityInBytes < MinSizeInBytes)
|
||||
NewCapacityInBytes = MinSizeInBytes;
|
||||
// Ensure we can fit the new capacity in 32 bits.
|
||||
if (MinCapacity > UINT32_MAX)
|
||||
report_bad_alloc_error("SmallVector capacity overflow during allocation");
|
||||
|
||||
size_t NewCapacity = 2 * capacity() + 1; // Always grow.
|
||||
NewCapacity =
|
||||
std::min(std::max(NewCapacity, MinCapacity), size_t(UINT32_MAX));
|
||||
|
||||
void *NewElts;
|
||||
if (BeginX == FirstEl) {
|
||||
NewElts = CheckedMalloc(NewCapacityInBytes);
|
||||
NewElts = safe_malloc(NewCapacity * TSize);
|
||||
|
||||
// Copy the elements over. No need to run dtors on PODs.
|
||||
memcpy(NewElts, this->BeginX, CurSizeBytes);
|
||||
memcpy(NewElts, this->BeginX, size() * TSize);
|
||||
} else {
|
||||
// If this wasn't grown from the inline copy, grow the allocated space.
|
||||
NewElts = CheckedRealloc(this->BeginX, NewCapacityInBytes);
|
||||
NewElts = safe_realloc(this->BeginX, NewCapacity * TSize);
|
||||
}
|
||||
|
||||
this->EndX = (char*)NewElts+CurSizeBytes;
|
||||
this->BeginX = NewElts;
|
||||
this->CapacityX = (char*)this->BeginX + NewCapacityInBytes;
|
||||
this->Capacity = NewCapacity;
|
||||
}
|
||||
|
||||
@@ -58,16 +58,33 @@ void wpi::SplitString(StringRef Source,
|
||||
}
|
||||
}
|
||||
|
||||
void wpi::PrintEscapedString(StringRef Name, raw_ostream &Out) {
|
||||
void wpi::printEscapedString(StringRef Name, raw_ostream &Out) {
|
||||
for (unsigned i = 0, e = Name.size(); i != e; ++i) {
|
||||
unsigned char C = Name[i];
|
||||
if (isprint(C) && C != '\\' && C != '"')
|
||||
if (isPrint(C) && C != '\\' && C != '"')
|
||||
Out << C;
|
||||
else
|
||||
Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
|
||||
}
|
||||
}
|
||||
|
||||
void wpi::printHTMLEscaped(StringRef String, raw_ostream &Out) {
|
||||
for (char C : String) {
|
||||
if (C == '&')
|
||||
Out << "&";
|
||||
else if (C == '<')
|
||||
Out << "<";
|
||||
else if (C == '>')
|
||||
Out << ">";
|
||||
else if (C == '\"')
|
||||
Out << """;
|
||||
else if (C == '\'')
|
||||
Out << "'";
|
||||
else
|
||||
Out << C;
|
||||
}
|
||||
}
|
||||
|
||||
void wpi::printLowerCase(StringRef String, raw_ostream &Out) {
|
||||
for (const char C : String)
|
||||
Out << toLower(C);
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
#include "wpi/StringExtras.h"
|
||||
#include "wpi/Compiler.h"
|
||||
#include "wpi/MathExtras.h"
|
||||
#include "wpi/memory.h"
|
||||
#include <cassert>
|
||||
|
||||
using namespace wpi;
|
||||
@@ -59,7 +58,7 @@ void StringMapImpl::init(unsigned InitSize) {
|
||||
NumTombstones = 0;
|
||||
|
||||
TheTable = static_cast<StringMapEntryBase **>(
|
||||
CheckedCalloc(NewNumBuckets+1,
|
||||
safe_calloc(NewNumBuckets+1,
|
||||
sizeof(StringMapEntryBase **) + sizeof(unsigned)));
|
||||
|
||||
// Set the member only if TheTable was successfully allocated
|
||||
@@ -129,7 +128,6 @@ unsigned StringMapImpl::LookupBucketFor(StringRef Name) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// FindKey - Look up the bucket that contains the specified key. If it exists
|
||||
/// in the map, return the bucket number of the key. Otherwise return -1.
|
||||
/// This does not modify the map.
|
||||
@@ -219,7 +217,7 @@ unsigned StringMapImpl::RehashTable(unsigned BucketNo) {
|
||||
// Allocate one extra bucket which will always be non-empty. This allows the
|
||||
// iterators to stop at end.
|
||||
auto NewTableArray = static_cast<StringMapEntryBase **>(
|
||||
CheckedCalloc(NewSize+1, sizeof(StringMapEntryBase *) + sizeof(unsigned)));
|
||||
safe_calloc(NewSize+1, sizeof(StringMapEntryBase *) + sizeof(unsigned)));
|
||||
|
||||
unsigned *NewHashArray = (unsigned *)(NewTableArray + NewSize + 1);
|
||||
NewTableArray[NewSize] = (StringMapEntryBase*)2;
|
||||
|
||||
@@ -16,25 +16,45 @@
|
||||
//=== is guaranteed to work on *all* UNIX variants.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "wpi/Errno.h"
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#include <dirent.h>
|
||||
#define NAMLEN(dirent) strlen((dirent)->d_name)
|
||||
#include <pwd.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace wpi;
|
||||
|
||||
namespace wpi {
|
||||
namespace sys {
|
||||
namespace fs {
|
||||
|
||||
const file_t kInvalidFile = -1;
|
||||
|
||||
TimePoint<> basic_file_status::getLastAccessedTime() const {
|
||||
return toTimePoint(fs_st_atime, fs_st_atime_nsec);
|
||||
}
|
||||
|
||||
TimePoint<> basic_file_status::getLastModificationTime() const {
|
||||
return toTimePoint(fs_st_mtime, fs_st_mtime_nsec);
|
||||
}
|
||||
|
||||
UniqueID file_status::getUniqueID() const {
|
||||
return UniqueID(fs_st_dev, fs_st_ino);
|
||||
}
|
||||
|
||||
uint32_t file_status::getLinkCount() const {
|
||||
return fs_st_nlinks;
|
||||
}
|
||||
|
||||
std::error_code current_path(SmallVectorImpl<char> &result) {
|
||||
result.clear();
|
||||
|
||||
@@ -93,9 +113,9 @@ std::error_code access(const Twine &Path, AccessMode Mode) {
|
||||
// Don't say that directories are executable.
|
||||
struct stat buf;
|
||||
if (0 != stat(P.begin(), &buf))
|
||||
return std::make_error_code(std::errc::permission_denied);
|
||||
return errc::permission_denied;
|
||||
if (!S_ISREG(buf.st_mode))
|
||||
return std::make_error_code(std::errc::permission_denied);
|
||||
return errc::permission_denied;
|
||||
}
|
||||
|
||||
return std::error_code();
|
||||
@@ -117,37 +137,48 @@ std::error_code equivalent(const Twine &A, const Twine &B, bool &result) {
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
static file_type typeForMode(mode_t Mode) {
|
||||
if (S_ISDIR(Mode))
|
||||
return file_type::directory_file;
|
||||
else if (S_ISREG(Mode))
|
||||
return file_type::regular_file;
|
||||
else if (S_ISBLK(Mode))
|
||||
return file_type::block_file;
|
||||
else if (S_ISCHR(Mode))
|
||||
return file_type::character_file;
|
||||
else if (S_ISFIFO(Mode))
|
||||
return file_type::fifo_file;
|
||||
else if (S_ISSOCK(Mode))
|
||||
return file_type::socket_file;
|
||||
else if (S_ISLNK(Mode))
|
||||
return file_type::symlink_file;
|
||||
return file_type::type_unknown;
|
||||
}
|
||||
|
||||
static std::error_code fillStatus(int StatRet, const struct stat &Status,
|
||||
file_status &Result) {
|
||||
file_status &Result) {
|
||||
if (StatRet != 0) {
|
||||
std::error_code ec(errno, std::generic_category());
|
||||
if (ec == std::errc::no_such_file_or_directory)
|
||||
std::error_code EC(errno, std::generic_category());
|
||||
if (EC == errc::no_such_file_or_directory)
|
||||
Result = file_status(file_type::file_not_found);
|
||||
else
|
||||
Result = file_status(file_type::status_error);
|
||||
return ec;
|
||||
return EC;
|
||||
}
|
||||
|
||||
file_type Type = file_type::type_unknown;
|
||||
|
||||
if (S_ISDIR(Status.st_mode))
|
||||
Type = file_type::directory_file;
|
||||
else if (S_ISREG(Status.st_mode))
|
||||
Type = file_type::regular_file;
|
||||
else if (S_ISBLK(Status.st_mode))
|
||||
Type = file_type::block_file;
|
||||
else if (S_ISCHR(Status.st_mode))
|
||||
Type = file_type::character_file;
|
||||
else if (S_ISFIFO(Status.st_mode))
|
||||
Type = file_type::fifo_file;
|
||||
else if (S_ISSOCK(Status.st_mode))
|
||||
Type = file_type::socket_file;
|
||||
else if (S_ISLNK(Status.st_mode))
|
||||
Type = file_type::symlink_file;
|
||||
uint32_t atime_nsec, mtime_nsec;
|
||||
#if defined(__APPLE__)
|
||||
atime_nsec = Status.st_atimespec.tv_nsec;
|
||||
mtime_nsec = Status.st_mtimespec.tv_nsec;
|
||||
#else
|
||||
atime_nsec = Status.st_atim.tv_nsec;
|
||||
mtime_nsec = Status.st_mtim.tv_nsec;
|
||||
#endif
|
||||
|
||||
perms Perms = static_cast<perms>(Status.st_mode) & all_perms;
|
||||
Result = file_status(Type, Perms, Status.st_dev, Status.st_nlink,
|
||||
Status.st_ino, Status.st_atime, Status.st_mtime,
|
||||
Result = file_status(typeForMode(Status.st_mode), Perms, Status.st_dev,
|
||||
Status.st_nlink, Status.st_ino,
|
||||
Status.st_atime, atime_nsec, Status.st_mtime, mtime_nsec,
|
||||
Status.st_uid, Status.st_gid, Status.st_size);
|
||||
|
||||
return std::error_code();
|
||||
@@ -168,6 +199,71 @@ std::error_code status(int FD, file_status &Result) {
|
||||
return fillStatus(StatRet, Status, Result);
|
||||
}
|
||||
|
||||
std::error_code mapped_file_region::init(int FD, uint64_t Offset,
|
||||
mapmode Mode) {
|
||||
assert(Size != 0);
|
||||
|
||||
int flags = (Mode == readwrite) ? MAP_SHARED : MAP_PRIVATE;
|
||||
int prot = (Mode == readonly) ? PROT_READ : (PROT_READ | PROT_WRITE);
|
||||
#if defined(__APPLE__)
|
||||
//----------------------------------------------------------------------
|
||||
// Newer versions of MacOSX have a flag that will allow us to read from
|
||||
// binaries whose code signature is invalid without crashing by using
|
||||
// the MAP_RESILIENT_CODESIGN flag. Also if a file from removable media
|
||||
// is mapped we can avoid crashing and return zeroes to any pages we try
|
||||
// to read if the media becomes unavailable by using the
|
||||
// MAP_RESILIENT_MEDIA flag. These flags are only usable when mapping
|
||||
// with PROT_READ, so take care not to specify them otherwise.
|
||||
//----------------------------------------------------------------------
|
||||
if (Mode == readonly) {
|
||||
#if defined(MAP_RESILIENT_CODESIGN)
|
||||
flags |= MAP_RESILIENT_CODESIGN;
|
||||
#endif
|
||||
#if defined(MAP_RESILIENT_MEDIA)
|
||||
flags |= MAP_RESILIENT_MEDIA;
|
||||
#endif
|
||||
}
|
||||
#endif // #if defined (__APPLE__)
|
||||
|
||||
Mapping = ::mmap(nullptr, Size, prot, flags, FD, Offset);
|
||||
if (Mapping == MAP_FAILED)
|
||||
return std::error_code(errno, std::generic_category());
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
mapped_file_region::mapped_file_region(int fd, mapmode mode, size_t length,
|
||||
uint64_t offset, std::error_code &ec)
|
||||
: Size(length), Mapping(), Mode(mode) {
|
||||
(void)Mode;
|
||||
ec = init(fd, offset, mode);
|
||||
if (ec)
|
||||
Mapping = nullptr;
|
||||
}
|
||||
|
||||
mapped_file_region::~mapped_file_region() {
|
||||
if (Mapping)
|
||||
::munmap(Mapping, Size);
|
||||
}
|
||||
|
||||
size_t mapped_file_region::size() const {
|
||||
assert(Mapping && "Mapping failed but used anyway!");
|
||||
return Size;
|
||||
}
|
||||
|
||||
char *mapped_file_region::data() const {
|
||||
assert(Mapping && "Mapping failed but used anyway!");
|
||||
return reinterpret_cast<char*>(Mapping);
|
||||
}
|
||||
|
||||
const char *mapped_file_region::const_data() const {
|
||||
assert(Mapping && "Mapping failed but used anyway!");
|
||||
return reinterpret_cast<const char*>(Mapping);
|
||||
}
|
||||
|
||||
int mapped_file_region::alignment() {
|
||||
return ::sysconf(_SC_PAGE_SIZE);
|
||||
}
|
||||
|
||||
std::error_code detail::directory_iterator_construct(detail::DirIterState &it,
|
||||
StringRef path,
|
||||
bool follow_symlinks) {
|
||||
@@ -191,19 +287,25 @@ std::error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
std::error_code detail::directory_iterator_increment(detail::DirIterState &it) {
|
||||
static file_type direntType(dirent* Entry) {
|
||||
// Most platforms provide the file type in the dirent: Linux/BSD/Mac.
|
||||
// The DTTOIF macro lets us reuse our status -> type conversion.
|
||||
return typeForMode(DTTOIF(Entry->d_type));
|
||||
}
|
||||
|
||||
std::error_code detail::directory_iterator_increment(detail::DirIterState &It) {
|
||||
errno = 0;
|
||||
dirent *cur_dir = ::readdir(reinterpret_cast<DIR *>(it.IterationHandle));
|
||||
if (cur_dir == nullptr && errno != 0) {
|
||||
dirent *CurDir = ::readdir(reinterpret_cast<DIR *>(It.IterationHandle));
|
||||
if (CurDir == nullptr && errno != 0) {
|
||||
return std::error_code(errno, std::generic_category());
|
||||
} else if (cur_dir != nullptr) {
|
||||
StringRef name(cur_dir->d_name, NAMLEN(cur_dir));
|
||||
if ((name.size() == 1 && name[0] == '.') ||
|
||||
(name.size() == 2 && name[0] == '.' && name[1] == '.'))
|
||||
return directory_iterator_increment(it);
|
||||
it.CurrentEntry.replace_filename(name);
|
||||
} else if (CurDir != nullptr) {
|
||||
StringRef Name(CurDir->d_name);
|
||||
if ((Name.size() == 1 && Name[0] == '.') ||
|
||||
(Name.size() == 2 && Name[0] == '.' && Name[1] == '.'))
|
||||
return directory_iterator_increment(It);
|
||||
It.CurrentEntry.replace_filename(Name, direntType(CurDir));
|
||||
} else
|
||||
return directory_iterator_destruct(it);
|
||||
return directory_iterator_destruct(It);
|
||||
|
||||
return std::error_code();
|
||||
}
|
||||
@@ -224,14 +326,85 @@ static bool hasProcSelfFD() {
|
||||
}
|
||||
#endif
|
||||
|
||||
std::error_code openFileForRead(const Twine &Name, int &ResultFD,
|
||||
SmallVectorImpl<char> *RealPath) {
|
||||
static int nativeOpenFlags(CreationDisposition Disp, OpenFlags Flags,
|
||||
FileAccess Access) {
|
||||
int Result = 0;
|
||||
if (Access == FA_Read)
|
||||
Result |= O_RDONLY;
|
||||
else if (Access == FA_Write)
|
||||
Result |= O_WRONLY;
|
||||
else if (Access == (FA_Read | FA_Write))
|
||||
Result |= O_RDWR;
|
||||
|
||||
// This is for compatibility with old code that assumed F_Append implied
|
||||
// would open an existing file. See Windows/Path.inc for a longer comment.
|
||||
if (Flags & F_Append)
|
||||
Disp = CD_OpenAlways;
|
||||
|
||||
if (Disp == CD_CreateNew) {
|
||||
Result |= O_CREAT; // Create if it doesn't exist.
|
||||
Result |= O_EXCL; // Fail if it does.
|
||||
} else if (Disp == CD_CreateAlways) {
|
||||
Result |= O_CREAT; // Create if it doesn't exist.
|
||||
Result |= O_TRUNC; // Truncate if it does.
|
||||
} else if (Disp == CD_OpenAlways) {
|
||||
Result |= O_CREAT; // Create if it doesn't exist.
|
||||
} else if (Disp == CD_OpenExisting) {
|
||||
// Nothing special, just don't add O_CREAT and we get these semantics.
|
||||
}
|
||||
|
||||
if (Flags & F_Append)
|
||||
Result |= O_APPEND;
|
||||
|
||||
#ifdef O_CLOEXEC
|
||||
if (!(Flags & OF_ChildInherit))
|
||||
Result |= O_CLOEXEC;
|
||||
#endif
|
||||
|
||||
return Result;
|
||||
}
|
||||
|
||||
std::error_code openFile(const Twine &Name, int &ResultFD,
|
||||
CreationDisposition Disp, FileAccess Access,
|
||||
OpenFlags Flags, unsigned Mode) {
|
||||
int OpenFlags = nativeOpenFlags(Disp, Flags, Access);
|
||||
|
||||
SmallString<128> Storage;
|
||||
StringRef P = Name.toNullTerminatedStringRef(Storage);
|
||||
while ((ResultFD = open(P.begin(), O_RDONLY)) < 0) {
|
||||
if (errno != EINTR)
|
||||
return std::error_code(errno, std::generic_category());
|
||||
// Call ::open in a lambda to avoid overload resolution in RetryAfterSignal
|
||||
// when open is overloaded, such as in Bionic.
|
||||
auto Open = [&]() { return ::open(P.begin(), OpenFlags, Mode); };
|
||||
if ((ResultFD = sys::RetryAfterSignal(-1, Open)) < 0)
|
||||
return std::error_code(errno, std::generic_category());
|
||||
#ifndef O_CLOEXEC
|
||||
if (!(Flags & OF_ChildInherit)) {
|
||||
int r = fcntl(ResultFD, F_SETFD, FD_CLOEXEC);
|
||||
(void)r;
|
||||
assert(r == 0 && "fcntl(F_SETFD, FD_CLOEXEC) failed");
|
||||
}
|
||||
#endif
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
Expected<int> openNativeFile(const Twine &Name, CreationDisposition Disp,
|
||||
FileAccess Access, OpenFlags Flags,
|
||||
unsigned Mode) {
|
||||
|
||||
int FD;
|
||||
std::error_code EC = openFile(Name, FD, Disp, Access, Flags, Mode);
|
||||
if (EC)
|
||||
return errorCodeToError(EC);
|
||||
return FD;
|
||||
}
|
||||
|
||||
std::error_code openFileForRead(const Twine &Name, int &ResultFD,
|
||||
OpenFlags Flags,
|
||||
SmallVectorImpl<char> *RealPath) {
|
||||
std::error_code EC =
|
||||
openFile(Name, ResultFD, CD_OpenExisting, FA_Read, Flags, 0666);
|
||||
if (EC)
|
||||
return EC;
|
||||
|
||||
// Attempt to get the real name of the file, if the user asked
|
||||
if(!RealPath)
|
||||
return std::error_code();
|
||||
@@ -251,6 +424,9 @@ std::error_code openFileForRead(const Twine &Name, int &ResultFD,
|
||||
if (CharCount > 0)
|
||||
RealPath->append(Buffer, Buffer + CharCount);
|
||||
} else {
|
||||
SmallString<128> Storage;
|
||||
StringRef P = Name.toNullTerminatedStringRef(Storage);
|
||||
|
||||
// Use ::realpath to get the real path name
|
||||
if (::realpath(P.begin(), Buffer) != nullptr)
|
||||
RealPath->append(Buffer, Buffer + strlen(Buffer));
|
||||
@@ -259,38 +435,18 @@ std::error_code openFileForRead(const Twine &Name, int &ResultFD,
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
std::error_code openFileForWrite(const Twine &Name, int &ResultFD,
|
||||
OpenFlags Flags, unsigned Mode) {
|
||||
// Verify that we don't have both "append" and "excl".
|
||||
assert((!(Flags & F_Excl) || !(Flags & F_Append)) &&
|
||||
"Cannot specify both 'excl' and 'append' file creation flags!");
|
||||
Expected<file_t> openNativeFileForRead(const Twine &Name, OpenFlags Flags,
|
||||
SmallVectorImpl<char> *RealPath) {
|
||||
file_t ResultFD;
|
||||
std::error_code EC = openFileForRead(Name, ResultFD, Flags, RealPath);
|
||||
if (EC)
|
||||
return errorCodeToError(EC);
|
||||
return ResultFD;
|
||||
}
|
||||
|
||||
int OpenFlags = O_CREAT;
|
||||
|
||||
#ifdef O_CLOEXEC
|
||||
OpenFlags |= O_CLOEXEC;
|
||||
#endif
|
||||
|
||||
if (Flags & F_RW)
|
||||
OpenFlags |= O_RDWR;
|
||||
else
|
||||
OpenFlags |= O_WRONLY;
|
||||
|
||||
if (Flags & F_Append)
|
||||
OpenFlags |= O_APPEND;
|
||||
else if (!(Flags & F_NoTrunc))
|
||||
OpenFlags |= O_TRUNC;
|
||||
|
||||
if (Flags & F_Excl)
|
||||
OpenFlags |= O_EXCL;
|
||||
|
||||
SmallString<128> Storage;
|
||||
StringRef P = Name.toNullTerminatedStringRef(Storage);
|
||||
while ((ResultFD = open(P.begin(), OpenFlags, Mode)) < 0) {
|
||||
if (errno != EINTR)
|
||||
return std::error_code(errno, std::generic_category());
|
||||
}
|
||||
return std::error_code();
|
||||
void closeFile(file_t &F) {
|
||||
::close(F);
|
||||
F = kInvalidFile;
|
||||
}
|
||||
|
||||
} // end namespace fs
|
||||
@@ -298,13 +454,18 @@ std::error_code openFileForWrite(const Twine &Name, int &ResultFD,
|
||||
namespace path {
|
||||
|
||||
bool home_directory(SmallVectorImpl<char> &result) {
|
||||
if (char *RequestedDir = std::getenv("HOME")) {
|
||||
result.clear();
|
||||
result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
|
||||
return true;
|
||||
char *RequestedDir = getenv("HOME");
|
||||
if (!RequestedDir) {
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
if (pw && pw->pw_dir)
|
||||
RequestedDir = pw->pw_dir;
|
||||
}
|
||||
if (!RequestedDir)
|
||||
return false;
|
||||
|
||||
return false;
|
||||
result.clear();
|
||||
result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool getDarwinConfDir(bool TempDir, SmallVectorImpl<char> &Result) {
|
||||
@@ -332,29 +493,6 @@ static bool getDarwinConfDir(bool TempDir, SmallVectorImpl<char> &Result) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool getUserCacheDir(SmallVectorImpl<char> &Result) {
|
||||
// First try using XDG_CACHE_HOME env variable,
|
||||
// as specified in XDG Base Directory Specification at
|
||||
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
||||
if (const char *XdgCacheDir = std::getenv("XDG_CACHE_HOME")) {
|
||||
Result.clear();
|
||||
Result.append(XdgCacheDir, XdgCacheDir + strlen(XdgCacheDir));
|
||||
return true;
|
||||
}
|
||||
|
||||
// Try Darwin configuration query
|
||||
if (getDarwinConfDir(false, Result))
|
||||
return true;
|
||||
|
||||
// Use "$HOME/.cache" if $HOME is available
|
||||
if (home_directory(Result)) {
|
||||
append(Result, ".cache");
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static const char *getEnvTempDir() {
|
||||
// Check whether the temporary directory is specified by an environment
|
||||
// variable.
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "wpi/STLExtras.h"
|
||||
#include "wpi/ConvertUTF.h"
|
||||
#include "wpi/WindowsError.h"
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
@@ -31,6 +32,12 @@
|
||||
|
||||
#undef max
|
||||
|
||||
// MinGW doesn't define this.
|
||||
#ifndef _ERRNO_T_DEFINED
|
||||
#define _ERRNO_T_DEFINED
|
||||
typedef int errno_t;
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma comment(lib, "shell32.lib")
|
||||
# pragma comment(lib, "ole32.lib")
|
||||
@@ -116,6 +123,8 @@ std::error_code widenPath(const Twine &Path8,
|
||||
|
||||
namespace fs {
|
||||
|
||||
const file_t kInvalidFile = INVALID_HANDLE_VALUE;
|
||||
|
||||
UniqueID file_status::getUniqueID() const {
|
||||
// The file is uniquely identified by the volume serial number along
|
||||
// with the 64-bit file identifier.
|
||||
@@ -125,6 +134,24 @@ UniqueID file_status::getUniqueID() const {
|
||||
return UniqueID(VolumeSerialNumber, FileID);
|
||||
}
|
||||
|
||||
TimePoint<> basic_file_status::getLastAccessedTime() const {
|
||||
FILETIME Time;
|
||||
Time.dwLowDateTime = LastAccessedTimeLow;
|
||||
Time.dwHighDateTime = LastAccessedTimeHigh;
|
||||
return toTimePoint(Time);
|
||||
}
|
||||
|
||||
TimePoint<> basic_file_status::getLastModificationTime() const {
|
||||
FILETIME Time;
|
||||
Time.dwLowDateTime = LastWriteTimeLow;
|
||||
Time.dwHighDateTime = LastWriteTimeHigh;
|
||||
return toTimePoint(Time);
|
||||
}
|
||||
|
||||
uint32_t file_status::getLinkCount() const {
|
||||
return NumLinks;
|
||||
}
|
||||
|
||||
std::error_code current_path(SmallVectorImpl<char> &result) {
|
||||
SmallVector<wchar_t, MAX_PATH> cur_path;
|
||||
DWORD len = MAX_PATH;
|
||||
@@ -147,6 +174,51 @@ std::error_code current_path(SmallVectorImpl<char> &result) {
|
||||
return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result);
|
||||
}
|
||||
|
||||
static std::error_code realPathFromHandle(HANDLE H,
|
||||
SmallVectorImpl<wchar_t> &Buffer) {
|
||||
DWORD CountChars = ::GetFinalPathNameByHandleW(
|
||||
H, Buffer.begin(), Buffer.capacity() - 1, FILE_NAME_NORMALIZED);
|
||||
if (CountChars > Buffer.capacity()) {
|
||||
// The buffer wasn't big enough, try again. In this case the return value
|
||||
// *does* indicate the size of the null terminator.
|
||||
Buffer.reserve(CountChars);
|
||||
CountChars = ::GetFinalPathNameByHandleW(
|
||||
H, Buffer.data(), Buffer.capacity() - 1, FILE_NAME_NORMALIZED);
|
||||
}
|
||||
if (CountChars == 0)
|
||||
return mapWindowsError(GetLastError());
|
||||
Buffer.set_size(CountChars);
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
static std::error_code realPathFromHandle(HANDLE H,
|
||||
SmallVectorImpl<char> &RealPath) {
|
||||
RealPath.clear();
|
||||
SmallVector<wchar_t, MAX_PATH> Buffer;
|
||||
if (std::error_code EC = realPathFromHandle(H, Buffer))
|
||||
return EC;
|
||||
|
||||
const wchar_t *Data = Buffer.data();
|
||||
DWORD CountChars = Buffer.size();
|
||||
if (CountChars >= 4) {
|
||||
if (0 == ::memcmp(Data, L"\\\\?\\", 8)) {
|
||||
CountChars -= 4;
|
||||
Data += 4;
|
||||
}
|
||||
}
|
||||
|
||||
// Convert the result from UTF-16 to UTF-8.
|
||||
return UTF16ToUTF8(Data, CountChars, RealPath);
|
||||
}
|
||||
|
||||
static std::error_code setDeleteDisposition(HANDLE Handle, bool Delete) {
|
||||
FILE_DISPOSITION_INFO Disposition;
|
||||
Disposition.DeleteFile = Delete;
|
||||
if (!SetFileInformationByHandle(Handle, FileDispositionInfo, &Disposition,
|
||||
sizeof(Disposition)))
|
||||
return mapWindowsError(::GetLastError());
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
std::error_code access(const Twine &Path, AccessMode Mode) {
|
||||
SmallVector<wchar_t, 128> PathUtf16;
|
||||
@@ -162,11 +234,11 @@ std::error_code access(const Twine &Path, AccessMode Mode) {
|
||||
if (LastError != ERROR_FILE_NOT_FOUND &&
|
||||
LastError != ERROR_PATH_NOT_FOUND)
|
||||
return mapWindowsError(LastError);
|
||||
return std::make_error_code(std::errc::no_such_file_or_directory);
|
||||
return errc::no_such_file_or_directory;
|
||||
}
|
||||
|
||||
if (Mode == AccessMode::Write && (Attributes & FILE_ATTRIBUTE_READONLY))
|
||||
return std::make_error_code(std::errc::permission_denied);
|
||||
return errc::permission_denied;
|
||||
|
||||
return std::error_code();
|
||||
}
|
||||
@@ -315,6 +387,143 @@ std::error_code status(int FD, file_status &Result) {
|
||||
return getStatus(FileHandle, Result);
|
||||
}
|
||||
|
||||
std::error_code mapped_file_region::init(int FD, uint64_t Offset,
|
||||
mapmode Mode) {
|
||||
this->Mode = Mode;
|
||||
HANDLE OrigFileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
|
||||
if (OrigFileHandle == INVALID_HANDLE_VALUE)
|
||||
return make_error_code(errc::bad_file_descriptor);
|
||||
|
||||
DWORD flprotect;
|
||||
switch (Mode) {
|
||||
case readonly: flprotect = PAGE_READONLY; break;
|
||||
case readwrite: flprotect = PAGE_READWRITE; break;
|
||||
case priv: flprotect = PAGE_WRITECOPY; break;
|
||||
}
|
||||
|
||||
HANDLE FileMappingHandle =
|
||||
::CreateFileMappingW(OrigFileHandle, 0, flprotect,
|
||||
Hi_32(Size),
|
||||
Lo_32(Size),
|
||||
0);
|
||||
if (FileMappingHandle == NULL) {
|
||||
std::error_code ec = mapWindowsError(GetLastError());
|
||||
return ec;
|
||||
}
|
||||
|
||||
DWORD dwDesiredAccess;
|
||||
switch (Mode) {
|
||||
case readonly: dwDesiredAccess = FILE_MAP_READ; break;
|
||||
case readwrite: dwDesiredAccess = FILE_MAP_WRITE; break;
|
||||
case priv: dwDesiredAccess = FILE_MAP_COPY; break;
|
||||
}
|
||||
Mapping = ::MapViewOfFile(FileMappingHandle,
|
||||
dwDesiredAccess,
|
||||
Offset >> 32,
|
||||
Offset & 0xffffffff,
|
||||
Size);
|
||||
if (Mapping == NULL) {
|
||||
std::error_code ec = mapWindowsError(GetLastError());
|
||||
::CloseHandle(FileMappingHandle);
|
||||
return ec;
|
||||
}
|
||||
|
||||
if (Size == 0) {
|
||||
MEMORY_BASIC_INFORMATION mbi;
|
||||
SIZE_T Result = VirtualQuery(Mapping, &mbi, sizeof(mbi));
|
||||
if (Result == 0) {
|
||||
std::error_code ec = mapWindowsError(GetLastError());
|
||||
::UnmapViewOfFile(Mapping);
|
||||
::CloseHandle(FileMappingHandle);
|
||||
return ec;
|
||||
}
|
||||
Size = mbi.RegionSize;
|
||||
}
|
||||
|
||||
// Close the file mapping handle, as it's kept alive by the file mapping. But
|
||||
// neither the file mapping nor the file mapping handle keep the file handle
|
||||
// alive, so we need to keep a reference to the file in case all other handles
|
||||
// are closed and the file is deleted, which may cause invalid data to be read
|
||||
// from the file.
|
||||
::CloseHandle(FileMappingHandle);
|
||||
if (!::DuplicateHandle(::GetCurrentProcess(), OrigFileHandle,
|
||||
::GetCurrentProcess(), &FileHandle, 0, 0,
|
||||
DUPLICATE_SAME_ACCESS)) {
|
||||
std::error_code ec = mapWindowsError(GetLastError());
|
||||
::UnmapViewOfFile(Mapping);
|
||||
return ec;
|
||||
}
|
||||
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
mapped_file_region::mapped_file_region(int fd, mapmode mode, size_t length,
|
||||
uint64_t offset, std::error_code &ec)
|
||||
: Size(length), Mapping() {
|
||||
ec = init(fd, offset, mode);
|
||||
if (ec)
|
||||
Mapping = 0;
|
||||
}
|
||||
|
||||
static bool hasFlushBufferKernelBug() {
|
||||
static bool Ret{GetWindowsOSVersion() < wpi::VersionTuple(10, 0, 0, 17763)};
|
||||
return Ret;
|
||||
}
|
||||
|
||||
static bool isEXE(StringRef Magic) {
|
||||
static const char PEMagic[] = {'P', 'E', '\0', '\0'};
|
||||
if (Magic.startswith(StringRef("MZ")) && Magic.size() >= 0x3c + 4) {
|
||||
uint32_t off = read32le(Magic.data() + 0x3c);
|
||||
// PE/COFF file, either EXE or DLL.
|
||||
if (Magic.substr(off).startswith(StringRef(PEMagic, sizeof(PEMagic))))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
mapped_file_region::~mapped_file_region() {
|
||||
if (Mapping) {
|
||||
|
||||
bool Exe = isEXE(StringRef((char *)Mapping, Size));
|
||||
|
||||
::UnmapViewOfFile(Mapping);
|
||||
|
||||
if (Mode == mapmode::readwrite && Exe && hasFlushBufferKernelBug()) {
|
||||
// There is a Windows kernel bug, the exact trigger conditions of which
|
||||
// are not well understood. When triggered, dirty pages are not properly
|
||||
// flushed and subsequent process's attempts to read a file can return
|
||||
// invalid data. Calling FlushFileBuffers on the write handle is
|
||||
// sufficient to ensure that this bug is not triggered.
|
||||
// The bug only occurs when writing an executable and executing it right
|
||||
// after, under high I/O pressure.
|
||||
::FlushFileBuffers(FileHandle);
|
||||
}
|
||||
|
||||
::CloseHandle(FileHandle);
|
||||
}
|
||||
}
|
||||
|
||||
size_t mapped_file_region::size() const {
|
||||
assert(Mapping && "Mapping failed but used anyway!");
|
||||
return Size;
|
||||
}
|
||||
|
||||
char *mapped_file_region::data() const {
|
||||
assert(Mapping && "Mapping failed but used anyway!");
|
||||
return reinterpret_cast<char*>(Mapping);
|
||||
}
|
||||
|
||||
const char *mapped_file_region::const_data() const {
|
||||
assert(Mapping && "Mapping failed but used anyway!");
|
||||
return reinterpret_cast<const char*>(Mapping);
|
||||
}
|
||||
|
||||
int mapped_file_region::alignment() {
|
||||
SYSTEM_INFO SysInfo;
|
||||
::GetSystemInfo(&SysInfo);
|
||||
return SysInfo.dwAllocationGranularity;
|
||||
}
|
||||
|
||||
static basic_file_status status_from_find_data(WIN32_FIND_DATAW *FindData) {
|
||||
return basic_file_status(file_type_from_attrs(FindData->dwFileAttributes),
|
||||
perms_from_attrs(FindData->dwFileAttributes),
|
||||
@@ -325,28 +534,28 @@ static basic_file_status status_from_find_data(WIN32_FIND_DATAW *FindData) {
|
||||
FindData->nFileSizeHigh, FindData->nFileSizeLow);
|
||||
}
|
||||
|
||||
std::error_code detail::directory_iterator_construct(detail::DirIterState &it,
|
||||
StringRef path,
|
||||
bool follow_symlinks) {
|
||||
SmallVector<wchar_t, 128> path_utf16;
|
||||
std::error_code detail::directory_iterator_construct(detail::DirIterState &IT,
|
||||
StringRef Path,
|
||||
bool FollowSymlinks) {
|
||||
SmallVector<wchar_t, 128> PathUTF16;
|
||||
|
||||
if (std::error_code ec = widenPath(path, path_utf16))
|
||||
return ec;
|
||||
if (std::error_code EC = widenPath(Path, PathUTF16))
|
||||
return EC;
|
||||
|
||||
// Convert path to the format that Windows is happy with.
|
||||
if (path_utf16.size() > 0 &&
|
||||
!is_separator(path_utf16[path.size() - 1]) &&
|
||||
path_utf16[path.size() - 1] != L':') {
|
||||
path_utf16.push_back(L'\\');
|
||||
path_utf16.push_back(L'*');
|
||||
if (PathUTF16.size() > 0 &&
|
||||
!is_separator(PathUTF16[Path.size() - 1]) &&
|
||||
PathUTF16[Path.size() - 1] != L':') {
|
||||
PathUTF16.push_back(L'\\');
|
||||
PathUTF16.push_back(L'*');
|
||||
} else {
|
||||
path_utf16.push_back(L'*');
|
||||
PathUTF16.push_back(L'*');
|
||||
}
|
||||
|
||||
// Get the first directory entry.
|
||||
WIN32_FIND_DATAW FirstFind;
|
||||
ScopedFindHandle FindHandle(::FindFirstFileExW(
|
||||
c_str(path_utf16), FindExInfoBasic, &FirstFind, FindExSearchNameMatch,
|
||||
c_str(PathUTF16), FindExInfoBasic, &FirstFind, FindExSearchNameMatch,
|
||||
NULL, FIND_FIRST_EX_LARGE_FETCH));
|
||||
if (!FindHandle)
|
||||
return mapWindowsError(::GetLastError());
|
||||
@@ -359,43 +568,45 @@ std::error_code detail::directory_iterator_construct(detail::DirIterState &it,
|
||||
DWORD LastError = ::GetLastError();
|
||||
// Check for end.
|
||||
if (LastError == ERROR_NO_MORE_FILES)
|
||||
return detail::directory_iterator_destruct(it);
|
||||
return detail::directory_iterator_destruct(IT);
|
||||
return mapWindowsError(LastError);
|
||||
} else
|
||||
FilenameLen = ::wcslen(FirstFind.cFileName);
|
||||
|
||||
// Construct the current directory entry.
|
||||
SmallString<128> directory_entry_name_utf8;
|
||||
if (std::error_code ec =
|
||||
SmallString<128> DirectoryEntryNameUTF8;
|
||||
if (std::error_code EC =
|
||||
UTF16ToUTF8(FirstFind.cFileName, ::wcslen(FirstFind.cFileName),
|
||||
directory_entry_name_utf8))
|
||||
return ec;
|
||||
DirectoryEntryNameUTF8))
|
||||
return EC;
|
||||
|
||||
it.IterationHandle = intptr_t(FindHandle.take());
|
||||
SmallString<128> directory_entry_path(path);
|
||||
path::append(directory_entry_path, directory_entry_name_utf8);
|
||||
it.CurrentEntry = directory_entry(directory_entry_path, follow_symlinks,
|
||||
status_from_find_data(&FirstFind));
|
||||
IT.IterationHandle = intptr_t(FindHandle.take());
|
||||
SmallString<128> DirectoryEntryPath(Path);
|
||||
path::append(DirectoryEntryPath, DirectoryEntryNameUTF8);
|
||||
IT.CurrentEntry =
|
||||
directory_entry(DirectoryEntryPath, FollowSymlinks,
|
||||
file_type_from_attrs(FirstFind.dwFileAttributes),
|
||||
status_from_find_data(&FirstFind));
|
||||
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
std::error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
|
||||
if (it.IterationHandle != 0)
|
||||
std::error_code detail::directory_iterator_destruct(detail::DirIterState &IT) {
|
||||
if (IT.IterationHandle != 0)
|
||||
// Closes the handle if it's valid.
|
||||
ScopedFindHandle close(HANDLE(it.IterationHandle));
|
||||
it.IterationHandle = 0;
|
||||
it.CurrentEntry = directory_entry();
|
||||
ScopedFindHandle close(HANDLE(IT.IterationHandle));
|
||||
IT.IterationHandle = 0;
|
||||
IT.CurrentEntry = directory_entry();
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
std::error_code detail::directory_iterator_increment(detail::DirIterState &it) {
|
||||
std::error_code detail::directory_iterator_increment(detail::DirIterState &IT) {
|
||||
WIN32_FIND_DATAW FindData;
|
||||
if (!::FindNextFileW(HANDLE(it.IterationHandle), &FindData)) {
|
||||
if (!::FindNextFileW(HANDLE(IT.IterationHandle), &FindData)) {
|
||||
DWORD LastError = ::GetLastError();
|
||||
// Check for end.
|
||||
if (LastError == ERROR_NO_MORE_FILES)
|
||||
return detail::directory_iterator_destruct(it);
|
||||
return detail::directory_iterator_destruct(IT);
|
||||
return mapWindowsError(LastError);
|
||||
}
|
||||
|
||||
@@ -403,16 +614,18 @@ std::error_code detail::directory_iterator_increment(detail::DirIterState &it) {
|
||||
if ((FilenameLen == 1 && FindData.cFileName[0] == L'.') ||
|
||||
(FilenameLen == 2 && FindData.cFileName[0] == L'.' &&
|
||||
FindData.cFileName[1] == L'.'))
|
||||
return directory_iterator_increment(it);
|
||||
return directory_iterator_increment(IT);
|
||||
|
||||
SmallString<128> directory_entry_path_utf8;
|
||||
if (std::error_code ec =
|
||||
SmallString<128> DirectoryEntryPathUTF8;
|
||||
if (std::error_code EC =
|
||||
UTF16ToUTF8(FindData.cFileName, ::wcslen(FindData.cFileName),
|
||||
directory_entry_path_utf8))
|
||||
return ec;
|
||||
DirectoryEntryPathUTF8))
|
||||
return EC;
|
||||
|
||||
it.CurrentEntry.replace_filename(Twine(directory_entry_path_utf8),
|
||||
status_from_find_data(&FindData));
|
||||
IT.CurrentEntry.replace_filename(
|
||||
Twine(DirectoryEntryPathUTF8),
|
||||
file_type_from_attrs(FindData.dwFileAttributes),
|
||||
status_from_find_data(&FindData));
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
@@ -420,18 +633,82 @@ ErrorOr<basic_file_status> directory_entry::status() const {
|
||||
return Status;
|
||||
}
|
||||
|
||||
std::error_code openFileForRead(const Twine &Name, int &ResultFD,
|
||||
SmallVectorImpl<char> *RealPath) {
|
||||
ResultFD = -1;
|
||||
SmallVector<wchar_t, 128> PathUTF16;
|
||||
static std::error_code nativeFileToFd(Expected<HANDLE> H, int &ResultFD,
|
||||
OpenFlags Flags) {
|
||||
int CrtOpenFlags = 0;
|
||||
if (Flags & OF_Append)
|
||||
CrtOpenFlags |= _O_APPEND;
|
||||
|
||||
if (Flags & OF_Text)
|
||||
CrtOpenFlags |= _O_TEXT;
|
||||
|
||||
ResultFD = -1;
|
||||
if (!H)
|
||||
return errorToErrorCode(H.takeError());
|
||||
|
||||
ResultFD = ::_open_osfhandle(intptr_t(*H), CrtOpenFlags);
|
||||
if (ResultFD == -1) {
|
||||
::CloseHandle(*H);
|
||||
return mapWindowsError(ERROR_INVALID_HANDLE);
|
||||
}
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
static DWORD nativeDisposition(CreationDisposition Disp, OpenFlags Flags) {
|
||||
// This is a compatibility hack. Really we should respect the creation
|
||||
// disposition, but a lot of old code relied on the implicit assumption that
|
||||
// OF_Append implied it would open an existing file. Since the disposition is
|
||||
// now explicit and defaults to CD_CreateAlways, this assumption would cause
|
||||
// any usage of OF_Append to append to a new file, even if the file already
|
||||
// existed. A better solution might have two new creation dispositions:
|
||||
// CD_AppendAlways and CD_AppendNew. This would also address the problem of
|
||||
// OF_Append being used on a read-only descriptor, which doesn't make sense.
|
||||
if (Flags & OF_Append)
|
||||
return OPEN_ALWAYS;
|
||||
|
||||
switch (Disp) {
|
||||
case CD_CreateAlways:
|
||||
return CREATE_ALWAYS;
|
||||
case CD_CreateNew:
|
||||
return CREATE_NEW;
|
||||
case CD_OpenAlways:
|
||||
return OPEN_ALWAYS;
|
||||
case CD_OpenExisting:
|
||||
return OPEN_EXISTING;
|
||||
}
|
||||
wpi_unreachable("unreachable!");
|
||||
}
|
||||
|
||||
static DWORD nativeAccess(FileAccess Access, OpenFlags Flags) {
|
||||
DWORD Result = 0;
|
||||
if (Access & FA_Read)
|
||||
Result |= GENERIC_READ;
|
||||
if (Access & FA_Write)
|
||||
Result |= GENERIC_WRITE;
|
||||
if (Flags & OF_Delete)
|
||||
Result |= DELETE;
|
||||
if (Flags & OF_UpdateAtime)
|
||||
Result |= FILE_WRITE_ATTRIBUTES;
|
||||
return Result;
|
||||
}
|
||||
|
||||
static std::error_code openNativeFileInternal(const Twine &Name,
|
||||
file_t &ResultFile, DWORD Disp,
|
||||
DWORD Access, DWORD Flags,
|
||||
bool Inherit = false) {
|
||||
SmallVector<wchar_t, 128> PathUTF16;
|
||||
if (std::error_code EC = widenPath(Name, PathUTF16))
|
||||
return EC;
|
||||
|
||||
SECURITY_ATTRIBUTES SA;
|
||||
SA.nLength = sizeof(SA);
|
||||
SA.lpSecurityDescriptor = nullptr;
|
||||
SA.bInheritHandle = Inherit;
|
||||
|
||||
HANDLE H =
|
||||
::CreateFileW(PathUTF16.begin(), GENERIC_READ,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
::CreateFileW(PathUTF16.begin(), Access,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, &SA,
|
||||
Disp, Flags, NULL);
|
||||
if (H == INVALID_HANDLE_VALUE) {
|
||||
DWORD LastError = ::GetLastError();
|
||||
std::error_code EC = mapWindowsError(LastError);
|
||||
@@ -441,98 +718,102 @@ std::error_code openFileForRead(const Twine &Name, int &ResultFD,
|
||||
if (LastError != ERROR_ACCESS_DENIED)
|
||||
return EC;
|
||||
if (is_directory(Name))
|
||||
return std::make_error_code(std::errc::is_a_directory);
|
||||
return make_error_code(errc::is_a_directory);
|
||||
return EC;
|
||||
}
|
||||
ResultFile = H;
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
ResultFD = ::_open_osfhandle(intptr_t(H), 0);
|
||||
if (ResultFD == -1) {
|
||||
::CloseHandle(H);
|
||||
return mapWindowsError(ERROR_INVALID_HANDLE);
|
||||
}
|
||||
Expected<file_t> openNativeFile(const Twine &Name, CreationDisposition Disp,
|
||||
FileAccess Access, OpenFlags Flags,
|
||||
unsigned Mode) {
|
||||
// Verify that we don't have both "append" and "excl".
|
||||
assert((!(Disp == CD_CreateNew) || !(Flags & OF_Append)) &&
|
||||
"Cannot specify both 'CreateNew' and 'Append' file creation flags!");
|
||||
|
||||
// Fetch the real name of the file, if the user asked
|
||||
if (RealPath) {
|
||||
RealPath->clear();
|
||||
wchar_t RealPathUTF16[MAX_PATH];
|
||||
DWORD CountChars =
|
||||
::GetFinalPathNameByHandleW(H, RealPathUTF16, MAX_PATH,
|
||||
FILE_NAME_NORMALIZED);
|
||||
if (CountChars > 0 && CountChars < MAX_PATH) {
|
||||
// Convert the result from UTF-16 to UTF-8.
|
||||
SmallString<MAX_PATH> RealPathUTF8;
|
||||
if (!UTF16ToUTF8(RealPathUTF16, CountChars, RealPathUTF8))
|
||||
RealPath->append(RealPathUTF8.data(),
|
||||
RealPathUTF8.data() + strlen(RealPathUTF8.data()));
|
||||
DWORD NativeDisp = nativeDisposition(Disp, Flags);
|
||||
DWORD NativeAccess = nativeAccess(Access, Flags);
|
||||
|
||||
bool Inherit = false;
|
||||
if (Flags & OF_ChildInherit)
|
||||
Inherit = true;
|
||||
|
||||
file_t Result;
|
||||
std::error_code EC = openNativeFileInternal(
|
||||
Name, Result, NativeDisp, NativeAccess, FILE_ATTRIBUTE_NORMAL, Inherit);
|
||||
if (EC)
|
||||
return errorCodeToError(EC);
|
||||
|
||||
if (Flags & OF_UpdateAtime) {
|
||||
FILETIME FileTime;
|
||||
SYSTEMTIME SystemTime;
|
||||
GetSystemTime(&SystemTime);
|
||||
if (SystemTimeToFileTime(&SystemTime, &FileTime) == 0 ||
|
||||
SetFileTime(Result, NULL, &FileTime, NULL) == 0) {
|
||||
DWORD LastError = ::GetLastError();
|
||||
::CloseHandle(Result);
|
||||
return errorCodeToError(mapWindowsError(LastError));
|
||||
}
|
||||
}
|
||||
|
||||
return std::error_code();
|
||||
if (Flags & OF_Delete) {
|
||||
if ((EC = setDeleteDisposition(Result, true))) {
|
||||
::CloseHandle(Result);
|
||||
return errorCodeToError(EC);
|
||||
}
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
|
||||
std::error_code openFileForWrite(const Twine &Name, int &ResultFD,
|
||||
OpenFlags Flags, unsigned Mode) {
|
||||
// Verify that we don't have both "append" and "excl".
|
||||
assert((!(Flags & F_Excl) || !(Flags & F_Append)) &&
|
||||
"Cannot specify both 'excl' and 'append' file creation flags!");
|
||||
std::error_code openFile(const Twine &Name, int &ResultFD,
|
||||
CreationDisposition Disp, FileAccess Access,
|
||||
OpenFlags Flags, unsigned int Mode) {
|
||||
Expected<file_t> Result = openNativeFile(Name, Disp, Access, Flags);
|
||||
if (!Result)
|
||||
return errorToErrorCode(Result.takeError());
|
||||
|
||||
ResultFD = -1;
|
||||
SmallVector<wchar_t, 128> PathUTF16;
|
||||
|
||||
if (std::error_code EC = widenPath(Name, PathUTF16))
|
||||
return EC;
|
||||
|
||||
DWORD CreationDisposition;
|
||||
if (Flags & F_Excl)
|
||||
CreationDisposition = CREATE_NEW;
|
||||
else if ((Flags & F_Append) || (Flags & F_NoTrunc))
|
||||
CreationDisposition = OPEN_ALWAYS;
|
||||
else
|
||||
CreationDisposition = CREATE_ALWAYS;
|
||||
|
||||
DWORD Access = GENERIC_WRITE;
|
||||
DWORD Attributes = FILE_ATTRIBUTE_NORMAL;
|
||||
if (Flags & F_RW)
|
||||
Access |= GENERIC_READ;
|
||||
if (Flags & F_Delete) {
|
||||
Access |= DELETE;
|
||||
Attributes |= FILE_FLAG_DELETE_ON_CLOSE;
|
||||
}
|
||||
|
||||
HANDLE H =
|
||||
::CreateFileW(PathUTF16.data(), Access,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||
NULL, CreationDisposition, Attributes, NULL);
|
||||
|
||||
if (H == INVALID_HANDLE_VALUE) {
|
||||
DWORD LastError = ::GetLastError();
|
||||
std::error_code EC = mapWindowsError(LastError);
|
||||
// Provide a better error message when trying to open directories.
|
||||
// This only runs if we failed to open the file, so there is probably
|
||||
// no performances issues.
|
||||
if (LastError != ERROR_ACCESS_DENIED)
|
||||
return EC;
|
||||
if (is_directory(Name))
|
||||
return std::make_error_code(std::errc::is_a_directory);
|
||||
return EC;
|
||||
}
|
||||
|
||||
int OpenFlags = 0;
|
||||
if (Flags & F_Append)
|
||||
OpenFlags |= _O_APPEND;
|
||||
|
||||
if (Flags & F_Text)
|
||||
OpenFlags |= _O_TEXT;
|
||||
|
||||
ResultFD = ::_open_osfhandle(intptr_t(H), OpenFlags);
|
||||
if (ResultFD == -1) {
|
||||
::CloseHandle(H);
|
||||
return mapWindowsError(ERROR_INVALID_HANDLE);
|
||||
}
|
||||
|
||||
return std::error_code();
|
||||
return nativeFileToFd(*Result, ResultFD, Flags);
|
||||
}
|
||||
|
||||
static std::error_code directoryRealPath(const Twine &Name,
|
||||
SmallVectorImpl<char> &RealPath) {
|
||||
file_t File;
|
||||
std::error_code EC = openNativeFileInternal(
|
||||
Name, File, OPEN_EXISTING, GENERIC_READ, FILE_FLAG_BACKUP_SEMANTICS);
|
||||
if (EC)
|
||||
return EC;
|
||||
|
||||
EC = realPathFromHandle(File, RealPath);
|
||||
::CloseHandle(File);
|
||||
return EC;
|
||||
}
|
||||
|
||||
std::error_code openFileForRead(const Twine &Name, int &ResultFD,
|
||||
OpenFlags Flags,
|
||||
SmallVectorImpl<char> *RealPath) {
|
||||
Expected<HANDLE> NativeFile = openNativeFileForRead(Name, Flags, RealPath);
|
||||
return nativeFileToFd(std::move(NativeFile), ResultFD, OF_None);
|
||||
}
|
||||
|
||||
Expected<file_t> openNativeFileForRead(const Twine &Name, OpenFlags Flags,
|
||||
SmallVectorImpl<char> *RealPath) {
|
||||
Expected<file_t> Result =
|
||||
openNativeFile(Name, CD_OpenExisting, FA_Read, Flags);
|
||||
|
||||
// Fetch the real name of the file, if the user asked
|
||||
if (Result && RealPath)
|
||||
realPathFromHandle(*Result, *RealPath);
|
||||
|
||||
return Result;
|
||||
}
|
||||
|
||||
void closeFile(file_t &F) {
|
||||
::CloseHandle(F);
|
||||
F = kInvalidFile;
|
||||
}
|
||||
|
||||
|
||||
} // end namespace fs
|
||||
|
||||
namespace path {
|
||||
@@ -547,10 +828,6 @@ static bool getKnownFolderPath(KNOWNFOLDERID folderId,
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool getUserCacheDir(SmallVectorImpl<char> &Result) {
|
||||
return getKnownFolderPath(FOLDERID_LocalAppData, Result);
|
||||
}
|
||||
|
||||
bool home_directory(SmallVectorImpl<char> &result) {
|
||||
return getKnownFolderPath(FOLDERID_Profile, result);
|
||||
}
|
||||
|
||||
@@ -38,11 +38,40 @@
|
||||
#include "wpi/StringExtras.h"
|
||||
#include "wpi/StringRef.h"
|
||||
#include "wpi/Twine.h"
|
||||
#include "wpi/Chrono.h"
|
||||
#include "wpi/Compiler.h"
|
||||
#include "wpi/VersionTuple.h"
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#define WIN32_NO_STATUS
|
||||
#include <windows.h>
|
||||
#undef WIN32_NO_STATUS
|
||||
#include <winternl.h>
|
||||
#include <ntstatus.h>
|
||||
|
||||
namespace wpi {
|
||||
|
||||
/// Returns the Windows version as Major.Minor.0.BuildNumber. Uses
|
||||
/// RtlGetVersion or GetVersionEx under the hood depending on what is available.
|
||||
/// GetVersionEx is deprecated, but this API exposes the build number which can
|
||||
/// be useful for working around certain kernel bugs.
|
||||
inline wpi::VersionTuple GetWindowsOSVersion() {
|
||||
typedef NTSTATUS(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
|
||||
HMODULE hMod = ::GetModuleHandleW(L"ntdll.dll");
|
||||
if (hMod) {
|
||||
auto getVer = (RtlGetVersionPtr)::GetProcAddress(hMod, "RtlGetVersion");
|
||||
if (getVer) {
|
||||
RTL_OSVERSIONINFOEXW info{};
|
||||
info.dwOSVersionInfoSize = sizeof(info);
|
||||
if (getVer((PRTL_OSVERSIONINFOW)&info) == ((NTSTATUS)0x00000000L)) {
|
||||
return wpi::VersionTuple(info.dwMajorVersion, info.dwMinorVersion, 0,
|
||||
info.dwBuildNumber);
|
||||
}
|
||||
}
|
||||
}
|
||||
return wpi::VersionTuple(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
/// Determines if the program is running on Windows 8 or newer. This
|
||||
/// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
|
||||
@@ -50,20 +79,7 @@
|
||||
/// yet have VersionHelpers.h, so we have our own helper.
|
||||
inline bool RunningWindows8OrGreater() {
|
||||
// Windows 8 is version 6.2, service pack 0.
|
||||
OSVERSIONINFOEXW osvi = {};
|
||||
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
||||
osvi.dwMajorVersion = 6;
|
||||
osvi.dwMinorVersion = 2;
|
||||
osvi.wServicePackMajor = 0;
|
||||
|
||||
DWORDLONG Mask = 0;
|
||||
Mask = VerSetConditionMask(Mask, VER_MAJORVERSION, VER_GREATER_EQUAL);
|
||||
Mask = VerSetConditionMask(Mask, VER_MINORVERSION, VER_GREATER_EQUAL);
|
||||
Mask = VerSetConditionMask(Mask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
|
||||
|
||||
return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION |
|
||||
VER_SERVICEPACKMAJOR,
|
||||
Mask) != FALSE;
|
||||
return GetWindowsOSVersion() >= wpi::VersionTuple(6, 2, 0, 0);
|
||||
}
|
||||
|
||||
inline bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix) {
|
||||
@@ -90,8 +106,8 @@ class ScopedHandle {
|
||||
typedef typename HandleTraits::handle_type handle_type;
|
||||
handle_type Handle;
|
||||
|
||||
ScopedHandle(const ScopedHandle &other); // = delete;
|
||||
void operator=(const ScopedHandle &other); // = delete;
|
||||
ScopedHandle(const ScopedHandle &other) = delete;
|
||||
void operator=(const ScopedHandle &other) = delete;
|
||||
public:
|
||||
ScopedHandle()
|
||||
: Handle(HandleTraits::GetInvalid()) {}
|
||||
@@ -179,7 +195,6 @@ typedef ScopedHandle<RegTraits> ScopedRegHandle;
|
||||
typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
|
||||
typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
|
||||
|
||||
namespace wpi {
|
||||
template <class T>
|
||||
class SmallVectorImpl;
|
||||
|
||||
@@ -192,21 +207,39 @@ c_str(SmallVectorImpl<T> &str) {
|
||||
}
|
||||
|
||||
namespace sys {
|
||||
namespace path {
|
||||
std::error_code widenPath(const Twine &Path8,
|
||||
SmallVectorImpl<wchar_t> &Path16);
|
||||
} // end namespace path
|
||||
|
||||
namespace windows {
|
||||
std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
|
||||
/// Convert to UTF16 from the current code page used in the system
|
||||
std::error_code CurCPToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
|
||||
std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
|
||||
SmallVectorImpl<char> &utf8);
|
||||
/// Convert from UTF16 to the current code page used in the system
|
||||
std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
|
||||
SmallVectorImpl<char> &utf8);
|
||||
} // end namespace windows
|
||||
inline std::chrono::nanoseconds toDuration(FILETIME Time) {
|
||||
ULARGE_INTEGER TimeInteger;
|
||||
TimeInteger.LowPart = Time.dwLowDateTime;
|
||||
TimeInteger.HighPart = Time.dwHighDateTime;
|
||||
|
||||
// FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
|
||||
return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
|
||||
}
|
||||
|
||||
inline TimePoint<> toTimePoint(FILETIME Time) {
|
||||
ULARGE_INTEGER TimeInteger;
|
||||
TimeInteger.LowPart = Time.dwLowDateTime;
|
||||
TimeInteger.HighPart = Time.dwHighDateTime;
|
||||
|
||||
// Adjust for different epoch
|
||||
TimeInteger.QuadPart -= 11644473600ll * 10000000;
|
||||
|
||||
// FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
|
||||
return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
|
||||
}
|
||||
|
||||
inline FILETIME toFILETIME(TimePoint<> TP) {
|
||||
ULARGE_INTEGER TimeInteger;
|
||||
TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
|
||||
TimeInteger.QuadPart += 11644473600ll * 10000000;
|
||||
|
||||
FILETIME Time;
|
||||
Time.dwLowDateTime = TimeInteger.LowPart;
|
||||
Time.dwHighDateTime = TimeInteger.HighPart;
|
||||
return Time;
|
||||
}
|
||||
|
||||
} // end namespace sys
|
||||
} // end namespace wpi.
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "wpi/SmallVector.h"
|
||||
#include "wpi/StringExtras.h"
|
||||
#include "wpi/Compiler.h"
|
||||
#include "wpi/ErrorHandling.h"
|
||||
#include "wpi/FileSystem.h"
|
||||
#include "wpi/Format.h"
|
||||
#include "wpi/MathExtras.h"
|
||||
@@ -56,6 +57,7 @@
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "wpi/ConvertUTF.h"
|
||||
#include "Windows/WindowsSupport.h"
|
||||
#endif
|
||||
|
||||
@@ -149,7 +151,7 @@ raw_ostream &raw_ostream::write_escaped(StringRef Str,
|
||||
*this << '\\' << '"';
|
||||
break;
|
||||
default:
|
||||
if (std::isprint(c)) {
|
||||
if (isPrint(c)) {
|
||||
*this << c;
|
||||
break;
|
||||
}
|
||||
@@ -335,7 +337,7 @@ raw_ostream &raw_ostream::operator<<(const FormattedString &FS) {
|
||||
break;
|
||||
}
|
||||
default:
|
||||
assert(false && "Bad Justification");
|
||||
wpi_unreachable("Bad Justification");
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@@ -419,7 +421,7 @@ raw_ostream &raw_ostream::operator<<(const FormattedBytes &FB) {
|
||||
|
||||
// Print the ASCII char values for each byte on this line
|
||||
for (uint8_t Byte : Line) {
|
||||
if (isprint(Byte))
|
||||
if (isPrint(Byte))
|
||||
*this << static_cast<char>(Byte);
|
||||
else
|
||||
*this << '.';
|
||||
@@ -481,14 +483,18 @@ void format_object_base::home() {
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
static int getFD(StringRef Filename, std::error_code &EC,
|
||||
sys::fs::CreationDisposition Disp, sys::fs::FileAccess Access,
|
||||
sys::fs::OpenFlags Flags) {
|
||||
assert((Access & sys::fs::FA_Write) &&
|
||||
"Cannot make a raw_ostream from a read-only descriptor!");
|
||||
|
||||
// Handle "-" as stdout. Note that when we do this, we consider ourself
|
||||
// the owner of stdout and may set the "binary" flag globally based on Flags.
|
||||
if (Filename == "-") {
|
||||
EC = std::error_code();
|
||||
// If user requested binary then put stdout into binary mode if
|
||||
// possible.
|
||||
if (!(Flags & sys::fs::F_Text)) {
|
||||
if (!(Flags & sys::fs::OF_Text)) {
|
||||
#if defined(_WIN32)
|
||||
_setmode(_fileno(stdout), _O_BINARY);
|
||||
#endif
|
||||
@@ -497,16 +503,39 @@ static int getFD(StringRef Filename, std::error_code &EC,
|
||||
}
|
||||
|
||||
int FD;
|
||||
EC = sys::fs::openFileForWrite(Filename, FD, Flags);
|
||||
if (Access & sys::fs::FA_Read)
|
||||
EC = sys::fs::openFileForReadWrite(Filename, FD, Disp, Flags);
|
||||
else
|
||||
EC = sys::fs::openFileForWrite(Filename, FD, Disp, Flags);
|
||||
if (EC)
|
||||
return -1;
|
||||
|
||||
return FD;
|
||||
}
|
||||
|
||||
raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC)
|
||||
: raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, sys::fs::FA_Write,
|
||||
sys::fs::OF_None) {}
|
||||
|
||||
raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
|
||||
sys::fs::CreationDisposition Disp)
|
||||
: raw_fd_ostream(Filename, EC, Disp, sys::fs::FA_Write, sys::fs::OF_None) {}
|
||||
|
||||
raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
|
||||
sys::fs::FileAccess Access)
|
||||
: raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, Access,
|
||||
sys::fs::OF_None) {}
|
||||
|
||||
raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
|
||||
sys::fs::OpenFlags Flags)
|
||||
: raw_fd_ostream(getFD(Filename, EC, Flags), true) {}
|
||||
: raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, sys::fs::FA_Write,
|
||||
Flags) {}
|
||||
|
||||
raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
|
||||
sys::fs::CreationDisposition Disp,
|
||||
sys::fs::FileAccess Access,
|
||||
sys::fs::OpenFlags Flags)
|
||||
: raw_fd_ostream(getFD(Filename, EC, Disp, Access, Flags), true) {}
|
||||
|
||||
/// FD is the file descriptor that this writes to. If ShouldClose is true, this
|
||||
/// closes the file when the stream is destroyed.
|
||||
@@ -526,6 +555,12 @@ raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered)
|
||||
if (FD <= STDERR_FILENO)
|
||||
ShouldClose = false;
|
||||
|
||||
#ifdef _WIN32
|
||||
// Check if this is a console device. This is not equivalent to isatty.
|
||||
IsWindowsConsole =
|
||||
::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR;
|
||||
#endif
|
||||
|
||||
// Get the starting position.
|
||||
off_t loc = ::lseek(FD, 0, SEEK_CUR);
|
||||
#ifdef _WIN32
|
||||
@@ -554,27 +589,87 @@ raw_fd_ostream::~raw_fd_ostream() {
|
||||
// on FD == 2.
|
||||
if (FD == 2) return;
|
||||
#endif
|
||||
|
||||
// If there are any pending errors, report them now. Clients wishing
|
||||
// to avoid report_fatal_error calls should check for errors with
|
||||
// has_error() and clear the error flag with clear_error() before
|
||||
// destructing raw_ostream objects which may have errors.
|
||||
if (has_error())
|
||||
report_fatal_error("IO failure on output stream: " + error().message(),
|
||||
/*GenCrashDiag=*/false);
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
// The most reliable way to print unicode in a Windows console is with
|
||||
// WriteConsoleW. To use that, first transcode from UTF-8 to UTF-16. This
|
||||
// assumes that LLVM programs always print valid UTF-8 to the console. The data
|
||||
// might not be UTF-8 for two major reasons:
|
||||
// 1. The program is printing binary (-filetype=obj -o -), in which case it
|
||||
// would have been gibberish anyway.
|
||||
// 2. The program is printing text in a semi-ascii compatible codepage like
|
||||
// shift-jis or cp1252.
|
||||
//
|
||||
// Most LLVM programs don't produce non-ascii text unless they are quoting
|
||||
// user source input. A well-behaved LLVM program should either validate that
|
||||
// the input is UTF-8 or transcode from the local codepage to UTF-8 before
|
||||
// quoting it. If they don't, this may mess up the encoding, but this is still
|
||||
// probably the best compromise we can make.
|
||||
static bool write_console_impl(int FD, StringRef Data) {
|
||||
SmallVector<wchar_t, 256> WideText;
|
||||
|
||||
// Fall back to ::write if it wasn't valid UTF-8.
|
||||
if (auto EC = sys::windows::UTF8ToUTF16(Data, WideText))
|
||||
return false;
|
||||
|
||||
// On Windows 7 and earlier, WriteConsoleW has a low maximum amount of data
|
||||
// that can be written to the console at a time.
|
||||
size_t MaxWriteSize = WideText.size();
|
||||
if (!RunningWindows8OrGreater())
|
||||
MaxWriteSize = 32767;
|
||||
|
||||
size_t WCharsWritten = 0;
|
||||
do {
|
||||
size_t WCharsToWrite =
|
||||
std::min(MaxWriteSize, WideText.size() - WCharsWritten);
|
||||
DWORD ActuallyWritten;
|
||||
bool Success =
|
||||
::WriteConsoleW((HANDLE)::_get_osfhandle(FD), &WideText[WCharsWritten],
|
||||
WCharsToWrite, &ActuallyWritten,
|
||||
/*Reserved=*/nullptr);
|
||||
|
||||
// The most likely reason for WriteConsoleW to fail is that FD no longer
|
||||
// points to a console. Fall back to ::write. If this isn't the first loop
|
||||
// iteration, something is truly wrong.
|
||||
if (!Success)
|
||||
return false;
|
||||
|
||||
WCharsWritten += ActuallyWritten;
|
||||
} while (WCharsWritten != WideText.size());
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
|
||||
assert(FD >= 0 && "File already closed.");
|
||||
pos += Size;
|
||||
|
||||
// The maximum write size is limited to SSIZE_MAX because a write
|
||||
// greater than SSIZE_MAX is implementation-defined in POSIX.
|
||||
// Since SSIZE_MAX is not portable, we use SIZE_MAX >> 1 instead.
|
||||
size_t MaxWriteSize = SIZE_MAX >> 1;
|
||||
#if defined(_WIN32)
|
||||
// If this is a Windows console device, try re-encoding from UTF-8 to UTF-16
|
||||
// and using WriteConsoleW. If that fails, fall back to plain write().
|
||||
if (IsWindowsConsole)
|
||||
if (write_console_impl(FD, StringRef(Ptr, Size)))
|
||||
return;
|
||||
#endif
|
||||
|
||||
// The maximum write size is limited to INT32_MAX. A write
|
||||
// greater than SSIZE_MAX is implementation-defined in POSIX,
|
||||
// and Windows _write requires 32 bit input.
|
||||
size_t MaxWriteSize = INT32_MAX;
|
||||
|
||||
#if defined(__linux__)
|
||||
// It is observed that Linux returns EINVAL for a very large write (>2G).
|
||||
// Make it a reasonably small value.
|
||||
MaxWriteSize = 1024 * 1024 * 1024;
|
||||
#elif defined(_WIN32)
|
||||
// Writing a large size of output to Windows console returns ENOMEM. It seems
|
||||
// that, prior to Windows 8, WriteFile() is redirecting to WriteConsole(), and
|
||||
// the latter has a size limit (66000 bytes or less, depending on heap usage).
|
||||
if (::_isatty(FD) && !RunningWindows8OrGreater())
|
||||
MaxWriteSize = 32767;
|
||||
#endif
|
||||
|
||||
do {
|
||||
@@ -645,8 +740,17 @@ void raw_fd_ostream::pwrite_impl(const char *Ptr, size_t Size,
|
||||
}
|
||||
|
||||
size_t raw_fd_ostream::preferred_buffer_size() const {
|
||||
#if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__minix)
|
||||
// Windows and Minix have no st_blksize.
|
||||
#if defined(_WIN32)
|
||||
// Disable buffering for console devices. Console output is re-encoded from
|
||||
// UTF-8 to UTF-16 on Windows, and buffering it would require us to split the
|
||||
// buffer on a valid UTF-8 codepoint boundary. Terminal buffering is disabled
|
||||
// below on most other OSs, so do the same thing on Windows and avoid that
|
||||
// complexity.
|
||||
if (IsWindowsConsole)
|
||||
return 0;
|
||||
return raw_ostream::preferred_buffer_size();
|
||||
#elif !defined(__minix)
|
||||
// Minix has no st_blksize.
|
||||
assert(FD >= 0 && "File not yet open!");
|
||||
struct stat statbuf;
|
||||
if (fstat(FD, &statbuf) != 0)
|
||||
@@ -791,3 +895,5 @@ void raw_null_ostream::pwrite_impl(const char * /*Ptr*/, size_t /*Size*/,
|
||||
uint64_t /*Offset*/) {}
|
||||
|
||||
void raw_pwrite_stream::anchor() {}
|
||||
|
||||
void buffer_ostream::anchor() {}
|
||||
|
||||
Reference in New Issue
Block a user