diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2023-12-18 20:30:12 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2024-04-06 20:11:55 +0000 |
commit | 5f757f3ff9144b609b3c433dfd370cc6bdc191ad (patch) | |
tree | 1b4e980b866cd26a00af34c0a653eb640bd09caf /contrib/llvm-project/lldb/source/Host/common/File.cpp | |
parent | 3e1c8a35f741a5d114d0ba670b15191355711fe9 (diff) | |
parent | 312c0ed19cc5276a17bacf2120097bec4515b0f1 (diff) |
Diffstat (limited to 'contrib/llvm-project/lldb/source/Host/common/File.cpp')
-rw-r--r-- | contrib/llvm-project/lldb/source/Host/common/File.cpp | 116 |
1 files changed, 77 insertions, 39 deletions
diff --git a/contrib/llvm-project/lldb/source/Host/common/File.cpp b/contrib/llvm-project/lldb/source/Host/common/File.cpp index 7c5d71d9426e..174feecf2de0 100644 --- a/contrib/llvm-project/lldb/source/Host/common/File.cpp +++ b/contrib/llvm-project/lldb/source/Host/common/File.cpp @@ -219,7 +219,7 @@ size_t File::Printf(const char *format, ...) { size_t File::PrintfVarArg(const char *format, va_list args) { llvm::SmallString<0> s; if (VASprintf(s, format, args)) { - size_t written = s.size();; + size_t written = s.size(); Write(s.data(), written); return written; } @@ -247,15 +247,21 @@ uint32_t File::GetPermissions(Status &error) const { return file_stats.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); } +bool NativeFile::IsValid() const { + std::scoped_lock<std::mutex, std::mutex> lock(m_descriptor_mutex, m_stream_mutex); + return DescriptorIsValidUnlocked() || StreamIsValidUnlocked(); +} + Expected<File::OpenOptions> NativeFile::GetOptions() const { return m_options; } int NativeFile::GetDescriptor() const { - if (DescriptorIsValid()) + if (ValueGuard descriptor_guard = DescriptorIsValid()) { return m_descriptor; + } // Don't open the file descriptor if we don't need to, just get it from the // stream if we have one. - if (StreamIsValid()) { + if (ValueGuard stream_guard = StreamIsValid()) { #if defined(_WIN32) return _fileno(m_stream); #else @@ -272,8 +278,9 @@ IOObject::WaitableHandle NativeFile::GetWaitableHandle() { } FILE *NativeFile::GetStream() { - if (!StreamIsValid()) { - if (DescriptorIsValid()) { + ValueGuard stream_guard = StreamIsValid(); + if (!stream_guard) { + if (ValueGuard descriptor_guard = DescriptorIsValid()) { auto mode = GetStreamOpenModeFromOptions(m_options); if (!mode) llvm::consumeError(mode.takeError()); @@ -282,9 +289,9 @@ FILE *NativeFile::GetStream() { // We must duplicate the file descriptor if we don't own it because when you // call fdopen, the stream will own the fd #ifdef _WIN32 - m_descriptor = ::_dup(GetDescriptor()); + m_descriptor = ::_dup(m_descriptor); #else - m_descriptor = dup(GetDescriptor()); + m_descriptor = dup(m_descriptor); #endif m_own_descriptor = true; } @@ -306,8 +313,11 @@ FILE *NativeFile::GetStream() { } Status NativeFile::Close() { + std::scoped_lock<std::mutex, std::mutex> lock(m_descriptor_mutex, m_stream_mutex); + Status error; - if (StreamIsValid()) { + + if (StreamIsValidUnlocked()) { if (m_own_stream) { if (::fclose(m_stream) == EOF) error.SetErrorToErrno(); @@ -322,15 +332,17 @@ Status NativeFile::Close() { } } } - if (DescriptorIsValid() && m_own_descriptor) { + + if (DescriptorIsValidUnlocked() && m_own_descriptor) { if (::close(m_descriptor) != 0) error.SetErrorToErrno(); } - m_descriptor = kInvalidDescriptor; + m_stream = kInvalidStream; - m_options = OpenOptions(0); m_own_stream = false; + m_descriptor = kInvalidDescriptor; m_own_descriptor = false; + m_options = OpenOptions(0); m_is_interactive = eLazyBoolCalculate; m_is_real_terminal = eLazyBoolCalculate; return error; @@ -374,7 +386,7 @@ Status NativeFile::GetFileSpec(FileSpec &file_spec) const { off_t NativeFile::SeekFromStart(off_t offset, Status *error_ptr) { off_t result = 0; - if (DescriptorIsValid()) { + if (ValueGuard descriptor_guard = DescriptorIsValid()) { result = ::lseek(m_descriptor, offset, SEEK_SET); if (error_ptr) { @@ -383,7 +395,10 @@ off_t NativeFile::SeekFromStart(off_t offset, Status *error_ptr) { else error_ptr->Clear(); } - } else if (StreamIsValid()) { + return result; + } + + if (ValueGuard stream_guard = StreamIsValid()) { result = ::fseek(m_stream, offset, SEEK_SET); if (error_ptr) { @@ -392,15 +407,17 @@ off_t NativeFile::SeekFromStart(off_t offset, Status *error_ptr) { else error_ptr->Clear(); } - } else if (error_ptr) { - error_ptr->SetErrorString("invalid file handle"); + return result; } + + if (error_ptr) + error_ptr->SetErrorString("invalid file handle"); return result; } off_t NativeFile::SeekFromCurrent(off_t offset, Status *error_ptr) { off_t result = -1; - if (DescriptorIsValid()) { + if (ValueGuard descriptor_guard = DescriptorIsValid()) { result = ::lseek(m_descriptor, offset, SEEK_CUR); if (error_ptr) { @@ -409,7 +426,10 @@ off_t NativeFile::SeekFromCurrent(off_t offset, Status *error_ptr) { else error_ptr->Clear(); } - } else if (StreamIsValid()) { + return result; + } + + if (ValueGuard stream_guard = StreamIsValid()) { result = ::fseek(m_stream, offset, SEEK_CUR); if (error_ptr) { @@ -418,15 +438,17 @@ off_t NativeFile::SeekFromCurrent(off_t offset, Status *error_ptr) { else error_ptr->Clear(); } - } else if (error_ptr) { - error_ptr->SetErrorString("invalid file handle"); + return result; } + + if (error_ptr) + error_ptr->SetErrorString("invalid file handle"); return result; } off_t NativeFile::SeekFromEnd(off_t offset, Status *error_ptr) { off_t result = -1; - if (DescriptorIsValid()) { + if (ValueGuard descriptor_guard = DescriptorIsValid()) { result = ::lseek(m_descriptor, offset, SEEK_END); if (error_ptr) { @@ -435,7 +457,10 @@ off_t NativeFile::SeekFromEnd(off_t offset, Status *error_ptr) { else error_ptr->Clear(); } - } else if (StreamIsValid()) { + return result; + } + + if (ValueGuard stream_guard = StreamIsValid()) { result = ::fseek(m_stream, offset, SEEK_END); if (error_ptr) { @@ -444,26 +469,32 @@ off_t NativeFile::SeekFromEnd(off_t offset, Status *error_ptr) { else error_ptr->Clear(); } - } else if (error_ptr) { - error_ptr->SetErrorString("invalid file handle"); } + + if (error_ptr) + error_ptr->SetErrorString("invalid file handle"); return result; } Status NativeFile::Flush() { Status error; - if (StreamIsValid()) { + if (ValueGuard stream_guard = StreamIsValid()) { if (llvm::sys::RetryAfterSignal(EOF, ::fflush, m_stream) == EOF) error.SetErrorToErrno(); - } else if (!DescriptorIsValid()) { - error.SetErrorString("invalid file handle"); + return error; + } + + { + ValueGuard descriptor_guard = DescriptorIsValid(); + if (!descriptor_guard) + error.SetErrorString("invalid file handle"); } return error; } Status NativeFile::Sync() { Status error; - if (DescriptorIsValid()) { + if (ValueGuard descriptor_guard = DescriptorIsValid()) { #ifdef _WIN32 int err = FlushFileBuffers((HANDLE)_get_osfhandle(m_descriptor)); if (err == 0) @@ -518,14 +549,18 @@ Status NativeFile::Read(void *buf, size_t &num_bytes) { #endif ssize_t bytes_read = -1; - if (DescriptorIsValid()) { - bytes_read = llvm::sys::RetryAfterSignal(-1, ::read, m_descriptor, buf, num_bytes); + if (ValueGuard descriptor_guard = DescriptorIsValid()) { + bytes_read = + llvm::sys::RetryAfterSignal(-1, ::read, m_descriptor, buf, num_bytes); if (bytes_read == -1) { error.SetErrorToErrno(); num_bytes = 0; } else num_bytes = bytes_read; - } else if (StreamIsValid()) { + return error; + } + + if (ValueGuard file_lock = StreamIsValid()) { bytes_read = ::fread(buf, 1, num_bytes, m_stream); if (bytes_read == 0) { @@ -536,10 +571,11 @@ Status NativeFile::Read(void *buf, size_t &num_bytes) { num_bytes = 0; } else num_bytes = bytes_read; - } else { - num_bytes = 0; - error.SetErrorString("invalid file handle"); + return error; } + + num_bytes = 0; + error.SetErrorString("invalid file handle"); return error; } @@ -577,7 +613,7 @@ Status NativeFile::Write(const void *buf, size_t &num_bytes) { #endif ssize_t bytes_written = -1; - if (DescriptorIsValid()) { + if (ValueGuard descriptor_guard = DescriptorIsValid()) { bytes_written = llvm::sys::RetryAfterSignal(-1, ::write, m_descriptor, buf, num_bytes); if (bytes_written == -1) { @@ -585,7 +621,10 @@ Status NativeFile::Write(const void *buf, size_t &num_bytes) { num_bytes = 0; } else num_bytes = bytes_written; - } else if (StreamIsValid()) { + return error; + } + + if (ValueGuard stream_guard = StreamIsValid()) { bytes_written = ::fwrite(buf, 1, num_bytes, m_stream); if (bytes_written == 0) { @@ -596,12 +635,11 @@ Status NativeFile::Write(const void *buf, size_t &num_bytes) { num_bytes = 0; } else num_bytes = bytes_written; - - } else { - num_bytes = 0; - error.SetErrorString("invalid file handle"); + return error; } + num_bytes = 0; + error.SetErrorString("invalid file handle"); return error; } |