Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions llvm/include/llvm/Support/File.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
/// \file
/// This file declares llvm::sys::fs::file_t type.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_FILE_H
#define LLVM_SUPPORT_FILE_H

#include "llvm/Support/Compiler.h"

namespace llvm::sys::fs {

/// This class wraps the platform specific file handle/descriptor type to
/// provide an unified representation.
struct file_t {
#if defined(_WIN32)
// A Win32 HANDLE is a typedef of void*
using value_type = void *;
#else
// A file descriptor on UNIX.
using value_type = int;
#endif
value_type Value;

LLVM_ABI static const value_type Invalid;

/// Default constructor to invalid file.
file_t() : Value(Invalid) {}

/// Implicit constructor from underlying value.
// TODO: Make this explicit to flush out type mismatches.
file_t(value_type Value) : Value(Value) {}

/// Is a valid file.
bool isValid() const { return Value != Invalid; }

/// Get the underlying value and return a platform specific value.
value_type get() const { return Value; }
};

inline bool operator==(file_t LHS, file_t RHS) {
return LHS.get() == RHS.get();
}

inline bool operator!=(file_t LHS, file_t RHS) { return !(LHS == RHS); }

} // namespace llvm::sys::fs

#endif
28 changes: 9 additions & 19 deletions llvm/include/llvm/Support/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/File.h"
#include "llvm/Support/FileSystem/UniqueID.h"
#include "llvm/Support/MD5.h"
#include <cassert>
Expand All @@ -49,15 +50,6 @@ namespace llvm {
namespace sys {
namespace fs {

#if defined(_WIN32)
// A Win32 HANDLE is a typedef of void*
using file_t = void *;
#else
using file_t = int;
#endif

LLVM_ABI extern const file_t kInvalidFile;

/// An enumeration for the file system's view of the type.
enum class file_type {
status_error,
Expand Down Expand Up @@ -645,13 +637,11 @@ LLVM_ABI std::error_code is_other(const Twine &path, bool &result);
LLVM_ABI std::error_code status(const Twine &path, file_status &result,
bool follow = true);

/// A version for when a file descriptor is already available.
LLVM_ABI std::error_code status(int FD, file_status &Result);
/// A version for when a file_t is already available.
LLVM_ABI std::error_code status(file_t F, file_status &Result);

#ifdef _WIN32
/// A version for when a file descriptor is already available.
LLVM_ABI std::error_code status(file_t FD, file_status &Result);
#endif
LLVM_ABI std::error_code status(int FD, file_status &Result);

/// Get file creation mode mask of the process.
///
Expand Down Expand Up @@ -1000,19 +990,19 @@ LLVM_ABI Expected<file_t> openNativeFile(const Twine &Name,
LLVM_ABI file_t convertFDToNativeFile(int FD);

#ifndef _WIN32
inline file_t convertFDToNativeFile(int FD) { return FD; }
inline file_t convertFDToNativeFile(int FD) { return file_t(FD); }
#endif

/// Return an open handle to standard in. On Unix, this is typically FD 0.
/// Returns kInvalidFile when the stream is closed.
/// Returns invalid file_t when the stream is closed.
LLVM_ABI file_t getStdinHandle();

/// Return an open handle to standard out. On Unix, this is typically FD 1.
/// Returns kInvalidFile when the stream is closed.
/// Returns invalid file_t when the stream is closed.
LLVM_ABI file_t getStdoutHandle();

/// Return an open handle to standard error. On Unix, this is typically FD 2.
/// Returns kInvalidFile when the stream is closed.
/// Returns invalid file_t when the stream is closed.
LLVM_ABI file_t getStderrHandle();

/// Reads \p Buf.size() bytes from \p FileHandle into \p Buf. Returns the number
Expand Down Expand Up @@ -1302,7 +1292,7 @@ class mapped_file_region {
size_t Size = 0;
void *Mapping = nullptr;
#ifdef _WIN32
sys::fs::file_t FileHandle = nullptr;
sys::fs::file_t FileHandle;
#endif
mapmode Mode = readonly;

Expand Down
12 changes: 1 addition & 11 deletions llvm/include/llvm/Support/MemoryBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,13 @@
#include "llvm/Support/CBindingWrapping.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/File.h"
#include "llvm/Support/MemoryBufferRef.h"
#include <cstddef>
#include <cstdint>
#include <memory>

namespace llvm {
namespace sys {
namespace fs {
// Duplicated from FileSystem.h to avoid a dependency.
#if defined(_WIN32)
// A Win32 HANDLE is a typedef of void*
using file_t = void *;
#else
using file_t = int;
#endif
} // namespace fs
} // namespace sys

/// This interface provides simple read-only access to a block of memory, and
/// provides simple methods for reading files and standard input into a memory
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CAS/MappedFileRegionArena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ Expected<int64_t> MappedFileRegionArena::allocateOffset(uint64_t AllocSize) {
ErrorOr<FileSizeInfo> FileSizeInfo::get(sys::fs::file_t File) {
#if LLVM_ON_UNIX && defined(MAPPED_FILE_BSIZE)
struct stat Status;
int StatRet = ::fstat(File, &Status);
int StatRet = ::fstat(File.get(), &Status);
if (StatRet)
return errnoAsErrorCode();
uint64_t AllocatedSize = uint64_t(Status.st_blksize) * MAPPED_FILE_BSIZE;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Object/ArchiveWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Expected<NewArchiveMember> NewArchiveMember::getFile(StringRef FileName,
if (!FDOrErr)
return FDOrErr.takeError();
sys::fs::file_t FD = *FDOrErr;
assert(FD != sys::fs::kInvalidFile);
assert(FD.isValid());

if (auto EC = sys::fs::status(FD, Status))
return errorCodeToError(EC);
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Support/FileUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ Error FilePermissionsApplier::apply(
return createFileError(OutputFilename, EC);

sys::fs::file_status OStat;
if (std::error_code EC = sys::fs::status(FD, OStat))
if (std::error_code EC =
sys::fs::status(sys::fs::convertFDToNativeFile(FD), OStat))
return createFileError(OutputFilename, EC);
if (OStat.type() == sys::fs::file_type::regular_file) {
#ifndef _WIN32
Expand Down
29 changes: 17 additions & 12 deletions llvm/lib/Support/Unix/Path.inc
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ namespace llvm {
namespace sys {
namespace fs {

const file_t kInvalidFile = -1;
const file_t::value_type file_t::Invalid = -1;

#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
defined(__FreeBSD_kernel__) || defined(__linux__) || \
Expand Down Expand Up @@ -768,6 +768,10 @@ std::error_code status(const Twine &Path, file_status &Result, bool Follow) {
return fillStatus(StatRet, Status, Result);
}

std::error_code status(file_t F, file_status &Result) {
return status(F.get(), Result);
}

std::error_code status(int FD, file_status &Result) {
struct stat Status;
int StatRet = ::fstat(FD, &Status);
Expand Down Expand Up @@ -832,7 +836,7 @@ std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime,
#endif
}

std::error_code mapped_file_region::init(int FD, uint64_t Offset,
std::error_code mapped_file_region::init(file_t FD, uint64_t Offset,
mapmode Mode) {
assert(Size != 0);

Expand Down Expand Up @@ -861,13 +865,13 @@ std::error_code mapped_file_region::init(int FD, uint64_t Offset,
}
#endif // #if defined (__APPLE__)

Mapping = ::mmap(nullptr, Size, prot, flags, FD, Offset);
Mapping = ::mmap(nullptr, Size, prot, flags, FD.get(), Offset);
if (Mapping == MAP_FAILED)
return errnoAsErrorCode();
return std::error_code();
}

mapped_file_region::mapped_file_region(int fd, mapmode mode, size_t length,
mapped_file_region::mapped_file_region(file_t fd, mapmode mode, size_t length,
uint64_t offset, std::error_code &ec)
: Size(length), Mode(mode) {
(void)Mode;
Expand Down Expand Up @@ -1128,9 +1132,9 @@ std::error_code openFile(const Twine &Name, int &ResultFD,
return std::error_code();
}

Expected<int> openNativeFile(const Twine &Name, CreationDisposition Disp,
FileAccess Access, OpenFlags Flags,
unsigned Mode) {
Expected<file_t> 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);
Expand Down Expand Up @@ -1183,7 +1187,7 @@ std::error_code openFileForRead(const Twine &Name, int &ResultFD,

Expected<file_t> openNativeFileForRead(const Twine &Name, OpenFlags Flags,
SmallVectorImpl<char> *RealPath) {
file_t ResultFD;
int ResultFD;
std::error_code EC = openFileForRead(Name, ResultFD, Flags, RealPath);
if (EC)
return errorCodeToError(EC);
Expand All @@ -1200,7 +1204,8 @@ Expected<size_t> readNativeFile(file_t FD, MutableArrayRef<char> Buf) {
#else
size_t Size = Buf.size();
#endif
ssize_t NumRead = sys::RetryAfterSignal(-1, ::read, FD, Buf.data(), Size);
ssize_t NumRead =
sys::RetryAfterSignal(-1, ::read, FD.get(), Buf.data(), Size);
if (NumRead == -1)
return errorCodeToError(errnoAsErrorCode());
// The underlying operation on these platforms allow opening directories
Expand All @@ -1224,7 +1229,7 @@ Expected<size_t> readNativeFileSlice(file_t FD, MutableArrayRef<char> Buf,
#endif
#ifdef HAVE_PREAD
ssize_t NumRead =
sys::RetryAfterSignal(-1, ::pread, FD, Buf.data(), Size, Offset);
sys::RetryAfterSignal(-1, ::pread, FD.get(), Buf.data(), Size, Offset);
#else
if (lseek(FD, Offset, SEEK_SET) == -1)
return errorCodeToError(errnoAsErrorCode());
Expand Down Expand Up @@ -1297,8 +1302,8 @@ std::error_code unlockFile(int FD) {

std::error_code closeFile(file_t &F) {
file_t TmpF = F;
F = kInvalidFile;
return Process::SafelyCloseFileDescriptor(TmpF);
F = file_t::Invalid;
return Process::SafelyCloseFileDescriptor(TmpF.get());
}

template <typename T>
Expand Down
11 changes: 5 additions & 6 deletions llvm/lib/Support/VirtualFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@
using namespace llvm;
using namespace llvm::vfs;

using llvm::sys::fs::file_t;
using llvm::sys::fs::file_status;
using llvm::sys::fs::file_t;
using llvm::sys::fs::file_type;
using llvm::sys::fs::kInvalidFile;
using llvm::sys::fs::perms;
using llvm::sys::fs::UniqueID;

Expand Down Expand Up @@ -198,7 +197,7 @@ class RealFile : public File {
: FD(RawFD), S(NewName, {}, {}, {}, {}, {},
llvm::sys::fs::file_type::status_error, {}),
RealName(NewRealPathName.str()) {
assert(FD != kInvalidFile && "Invalid or inactive file descriptor");
assert(FD.isValid() && "Invalid or inactive file descriptor");
}

public:
Expand All @@ -219,7 +218,7 @@ class RealFile : public File {
RealFile::~RealFile() { close(); }

ErrorOr<Status> RealFile::status() {
assert(FD != kInvalidFile && "cannot stat closed file");
assert(FD.isValid() && "cannot stat closed file");
if (!S.isStatusKnown()) {
file_status RealStatus;
if (std::error_code EC = sys::fs::status(FD, RealStatus))
Expand All @@ -236,14 +235,14 @@ ErrorOr<std::string> RealFile::getName() {
ErrorOr<std::unique_ptr<MemoryBuffer>>
RealFile::getBuffer(const Twine &Name, int64_t FileSize,
bool RequiresNullTerminator, bool IsVolatile) {
assert(FD != kInvalidFile && "cannot get buffer for closed file");
assert(FD.isValid() && "cannot get buffer for closed file");
return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator,
IsVolatile);
}

std::error_code RealFile::close() {
std::error_code EC = sys::fs::closeFile(FD);
FD = kInvalidFile;
FD = file_t::Invalid;
return EC;
}

Expand Down
Loading