20#include "llvm/Support/Errno.h"
21#include "llvm/Support/Process.h"
28#define MAX_READ_SIZE INT_MAX
29#define MAX_WRITE_SIZE INT_MAX
33 bool transfer_ownership)
39 int mode = fcntl(fd, F_GETFL);
46 (required_mode == O_RDWR && mode == O_RDWR) ||
47 (required_mode == O_RDONLY && (mode == O_RDWR || mode == O_RDONLY) ||
48 (required_mode == O_WRONLY &&
49 (mode == O_RDWR || mode == O_WRONLY))) &&
50 "invalid file access mode");
57 bool transfer_ownership)
68 if (llvm::sys::RetryAfterSignal(-1, ::fsync,
m_descriptor) == -1)
88 struct winsize window_size;
89 if (::ioctl(fd, TIOCGWINSZ, &window_size) == 0) {
90 if (window_size.ws_col > 0) {
92 if (llvm::sys::Process::FileDescriptorHasColors(fd))
107 file_spec.
SetFile(path, FileSpec::Style::native);
111#elif defined(__linux__)
114 if (::snprintf(proc,
sizeof(proc),
"/proc/self/fd/%d",
GetDescriptor()) < 0)
118 if ((len = ::readlink(proc, path,
sizeof(path) - 1)) == -1)
122 file_spec.
SetFile(path, FileSpec::Style::native);
127 "NativeFile::GetFileSpec is not supported on this platform");
138#if defined(MAX_READ_SIZE)
139 if (num_bytes > MAX_READ_SIZE) {
140 uint8_t *p = (uint8_t *)buf;
141 size_t bytes_left = num_bytes;
145 while (bytes_left > 0) {
146 size_t curr_num_bytes;
147 if (bytes_left > MAX_READ_SIZE)
148 curr_num_bytes = MAX_READ_SIZE;
150 curr_num_bytes = bytes_left;
152 error =
Read(p + num_bytes, curr_num_bytes, offset);
155 num_bytes += curr_num_bytes;
156 if (bytes_left < curr_num_bytes)
159 bytes_left -= curr_num_bytes;
171 llvm::sys::RetryAfterSignal(-1, ::pread, fd, buf, num_bytes, offset);
172 if (bytes_read < 0) {
176 offset += bytes_read;
177 num_bytes = bytes_read;
190#if defined(MAX_WRITE_SIZE)
191 if (num_bytes > MAX_WRITE_SIZE) {
192 const uint8_t *p = (
const uint8_t *)buf;
193 size_t bytes_left = num_bytes;
197 while (bytes_left > 0) {
198 size_t curr_num_bytes;
199 if (bytes_left > MAX_WRITE_SIZE)
200 curr_num_bytes = MAX_WRITE_SIZE;
202 curr_num_bytes = bytes_left;
204 error =
Write(p + num_bytes, curr_num_bytes, offset);
207 num_bytes += curr_num_bytes;
208 if (bytes_left < curr_num_bytes)
211 bytes_left -= curr_num_bytes;
222 ssize_t bytes_written = llvm::sys::RetryAfterSignal(
224 if (bytes_written < 0) {
228 offset += bytes_written;
229 num_bytes = bytes_written;
static llvm::raw_ostream & error(Stream &strm)
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
void Clear()
Clears the object state.
static int kInvalidDescriptor
LazyBool m_is_real_terminal
LazyBool m_is_interactive
static bool DescriptorIsValid(int descriptor)
static mode_t ConvertOpenOptionsForPOSIXOpen(OpenOptions open_options)
LazyBool m_supports_colors
lldb::file_t WaitableHandle
int GetDescriptor() const override
Get underlying OS file descriptor for this file, or kInvalidDescriptor.
ValueGuard DescriptorIsValid() const
bool IsValid() const override
IsValid.
Status Read(void *dst, size_t &num_bytes, off_t &offset) override
Read bytes from a file from the specified file offset.
Status Sync() override
Sync to disk.
WaitableHandle GetWaitableHandle() override
Get a handle that can be used for OS polling interfaces, such as WaitForMultipleObjects,...
Status Write(const void *src, size_t &num_bytes, off_t &offset) override
Write bytes to a file at the specified file offset.
void CalculateInteractiveAndTerminal() override
Refresh the cached interactive / terminal / color flags by inspecting the underlying descriptor.
NativeFilePosix()=default
Status GetFileSpec(FileSpec &file_spec) const override
Get the file specification for this file, if possible.
static Status FromErrno()
Set the current error to errno.
static Status FromErrorString(const char *str)
A class that represents a running process on the host machine.