diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/common/io/CFileStream.cpp | 6 | ||||
-rw-r--r-- | src/common/io/FileNotExistException.cpp | 12 | ||||
-rw-r--r-- | src/common/io/Stream.cpp | 4 | ||||
-rw-r--r-- | src/common/platform/unix/UnixFileStream.cpp | 12 |
5 files changed, 18 insertions, 17 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 31ec94db..97b49baf 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -7,7 +7,6 @@ add_library(CruBase StringToNumberConverter.cpp StringUtil.cpp io/CFileStream.cpp - io/FileNotExistException.cpp io/Stream.cpp io/Resource.cpp io/MemoryStream.cpp diff --git a/src/common/io/CFileStream.cpp b/src/common/io/CFileStream.cpp index de195538..29d4819f 100644 --- a/src/common/io/CFileStream.cpp +++ b/src/common/io/CFileStream.cpp @@ -97,6 +97,7 @@ bool CFileStream::CanRead() { Index CFileStream::Read(std::byte* buffer, Index offset, Index size) { CheckClosed(); + StreamOperationNotSupportedException::CheckRead(readable_); auto count = std::fread(buffer + offset, 1, size, file_); return count; } @@ -108,6 +109,7 @@ bool CFileStream::CanWrite() { Index CFileStream::Write(const std::byte* buffer, Index offset, Index size) { CheckClosed(); + StreamOperationNotSupportedException::CheckWrite(writable_); auto count = std::fwrite(buffer + offset, 1, size, file_); return count; } @@ -125,8 +127,6 @@ void CFileStream::Close() { } void CFileStream::CheckClosed() { - if (file_ == nullptr) { - throw StreamAlreadyClosedException(u"File is closed."); - } + StreamAlreadyClosedException::Check(file_ == nullptr); } } // namespace cru::io diff --git a/src/common/io/FileNotExistException.cpp b/src/common/io/FileNotExistException.cpp deleted file mode 100644 index a2e1fdb1..00000000 --- a/src/common/io/FileNotExistException.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "cru/common/io/FileNotExistException.h" - -#include "cru/common/Exception.h" -#include "cru/common/Format.h" - -#include <utility> - -namespace cru::io { - FileNotExistException::FileNotExistException(String path): Exception(), path_(std::move(path)) { - SetMessage(Format(u"File {} does not exist.", path_)); - } -} diff --git a/src/common/io/Stream.cpp b/src/common/io/Stream.cpp index 07677fa8..97669944 100644 --- a/src/common/io/Stream.cpp +++ b/src/common/io/Stream.cpp @@ -1,4 +1,5 @@ #include "cru/common/io/Stream.h" +#include "cru/common/Exception.h" #include "cru/common/Format.h" #include <utility> @@ -22,6 +23,9 @@ void StreamOperationNotSupportedException::CheckWrite(bool writable) { if (!writable) throw StreamOperationNotSupportedException(u"write"); } +StreamAlreadyClosedException::StreamAlreadyClosedException() + : Exception(u"Stream is already closed.") {} + void StreamAlreadyClosedException::Check(bool closed) { if (closed) throw StreamAlreadyClosedException(); } diff --git a/src/common/platform/unix/UnixFileStream.cpp b/src/common/platform/unix/UnixFileStream.cpp index 39870507..a952a7f2 100644 --- a/src/common/platform/unix/UnixFileStream.cpp +++ b/src/common/platform/unix/UnixFileStream.cpp @@ -2,10 +2,12 @@ #include "cru/common/Exception.h" #include "cru/common/Format.h" #include "cru/common/io/Stream.h" +#include "cru/common/log/Logger.h" #include <fcntl.h> #include <sys/fcntl.h> #include <unistd.h> +#include <cerrno> namespace cru::platform::unix { using namespace cru::io; @@ -56,7 +58,15 @@ UnixFileStream::UnixFileStream(int fd, bool can_seek, bool can_read, auto_close_ = auto_close; } -UnixFileStream::~UnixFileStream() { Close(); } +UnixFileStream::~UnixFileStream() { + if (auto_close_ && file_descriptor_ >= 0) { + if (::close(file_descriptor_) == -1) { + // We are in destructor, so we can not throw. + CRU_LOG_WARN(u"Failed to close file descriptor {}, errno {}.", + file_descriptor_, errno); + } + } +} bool UnixFileStream::CanSeek() { CheckClosed(); |