aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/cru/common/platform/unix/UnixFileStream.h2
-rw-r--r--src/common/platform/unix/UnixFileStream.cpp15
-rw-r--r--test/common/platform/unix/UnixFileStreamTest.cpp1
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>