Skip to content

Commit

Permalink
Fix unix file stream creation mode and readable check bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
crupest committed Oct 15, 2023
1 parent 5d077a7 commit 9b818f4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion include/cru/common/platform/unix/UnixFileStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
15 changes: 10 additions & 5 deletions src/common/platform/unix/UnixFileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion test/common/platform/unix/UnixFileStreamTest.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "cru/common/io/OpenFileFlag.h"
#include "cru/common/platform/unix/UnixFileStream.h"

#include <catch2/catch_test_macros.hpp>
Expand Down

0 comments on commit 9b818f4

Please sign in to comment.