diff options
author | crupest <crupest@outlook.com> | 2023-10-15 23:45:46 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2023-10-15 23:45:46 +0800 |
commit | 9b818f45345b41854bca1022daddcf19304030fc (patch) | |
tree | 9c20e48b2e11214d4861aa56e96c647c032b4163 | |
parent | 5d077a761fdb0873d593f7d3b03922bef2efe79a (diff) | |
download | cru-9b818f45345b41854bca1022daddcf19304030fc.tar.gz cru-9b818f45345b41854bca1022daddcf19304030fc.tar.bz2 cru-9b818f45345b41854bca1022daddcf19304030fc.zip |
Fix unix file stream creation mode and readable check bug.
-rw-r--r-- | include/cru/common/platform/unix/UnixFileStream.h | 2 | ||||
-rw-r--r-- | src/common/platform/unix/UnixFileStream.cpp | 15 | ||||
-rw-r--r-- | test/common/platform/unix/UnixFileStreamTest.cpp | 1 |
3 files changed, 11 insertions, 7 deletions
diff --git a/include/cru/common/platform/unix/UnixFileStream.h b/include/cru/common/platform/unix/UnixFileStream.h index 4b86649e..06b45d08 100644 --- a/include/cru/common/platform/unix/UnixFileStream.h +++ b/include/cru/common/platform/unix/UnixFileStream.h @@ -12,7 +12,7 @@ class UnixFileStream : public io::Stream { static constexpr auto kLogTag = u"cru::platform::unix::UnixFileStream"; public: - UnixFileStream(const char* path, int oflag); + UnixFileStream(const char* path, int oflag, mode_t mode = 0660); UnixFileStream(int fd, bool can_seek, bool can_read, bool can_write, bool auto_close); diff --git a/src/common/platform/unix/UnixFileStream.cpp b/src/common/platform/unix/UnixFileStream.cpp index a952a7f2..37f5110c 100644 --- a/src/common/platform/unix/UnixFileStream.cpp +++ b/src/common/platform/unix/UnixFileStream.cpp @@ -18,7 +18,11 @@ bool OflagCanSeek([[maybe_unused]] int oflag) { return true; } -bool OflagCanRead(int oflag) { return oflag & O_RDONLY || oflag & O_RDWR; } +bool OflagCanRead(int oflag) { + // There is a problem: O_RDONLY is 0. So we must test it specially. + // If it is not write-only. Then it can read. + return !(oflag & O_WRONLY); +} bool OflagCanWrite(int oflag) { return oflag & O_WRONLY || oflag & O_RDWR; } @@ -36,11 +40,12 @@ int MapSeekOrigin(Stream::SeekOrigin origin) { } } // namespace -UnixFileStream::UnixFileStream(const char *path, int oflag) { - file_descriptor_ = ::open(path, oflag); +UnixFileStream::UnixFileStream(const char *path, int oflag, mode_t mode) { + file_descriptor_ = ::open(path, oflag, mode); if (file_descriptor_ == -1) { - throw ErrnoException(Format(u"Failed to open file {} with oflag {}.", - String::FromUtf8(path), oflag)); + throw ErrnoException( + Format(u"Failed to open file {} with oflag {}, mode {}.", + String::FromUtf8(path), oflag, mode)); } can_seek_ = OflagCanSeek(oflag); diff --git a/test/common/platform/unix/UnixFileStreamTest.cpp b/test/common/platform/unix/UnixFileStreamTest.cpp index d5aebeeb..0f9d9834 100644 --- a/test/common/platform/unix/UnixFileStreamTest.cpp +++ b/test/common/platform/unix/UnixFileStreamTest.cpp @@ -1,4 +1,3 @@ -#include "cru/common/io/OpenFileFlag.h" #include "cru/common/platform/unix/UnixFileStream.h" #include <catch2/catch_test_macros.hpp> |