aboutsummaryrefslogtreecommitdiff
path: root/src/common/platform/unix/UnixFileStream.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-01-25 17:48:53 +0800
committercrupest <crupest@outlook.com>2022-01-25 17:48:53 +0800
commit71c01a175a939d1a519ab235fdfdeec1101f8b84 (patch)
tree5d639e2f7bdfd08348e41bb2a64c67f0c5662d2f /src/common/platform/unix/UnixFileStream.cpp
parent839292efaaf8d997b2cbca7abae8c6e575f6c01a (diff)
downloadcru-71c01a175a939d1a519ab235fdfdeec1101f8b84.tar.gz
cru-71c01a175a939d1a519ab235fdfdeec1101f8b84.tar.bz2
cru-71c01a175a939d1a519ab235fdfdeec1101f8b84.zip
...
Diffstat (limited to 'src/common/platform/unix/UnixFileStream.cpp')
-rw-r--r--src/common/platform/unix/UnixFileStream.cpp135
1 files changed, 135 insertions, 0 deletions
diff --git a/src/common/platform/unix/UnixFileStream.cpp b/src/common/platform/unix/UnixFileStream.cpp
new file mode 100644
index 00000000..bb8d4207
--- /dev/null
+++ b/src/common/platform/unix/UnixFileStream.cpp
@@ -0,0 +1,135 @@
+#include "cru/common/platform/unix/UnixFileStream.hpp"
+#include "cru/common/Format.hpp"
+#include "cru/common/io/OpenFileFlag.hpp"
+#include "cru/common/platform/unix/ErrnoException.hpp"
+
+#include <fcntl.h>
+#include <sys/_types/_s_ifmt.h>
+#include <unistd.h>
+
+namespace cru::platform::unix {
+using namespace cru::io;
+
+namespace {
+int MapOpenFileFlag(OpenFileFlag flags) {
+ int result = 0;
+ if (flags & OpenFileFlags::Read) {
+ if (flags & OpenFileFlags::Write) {
+ result |= O_RDWR;
+ } else {
+ result |= O_RDONLY;
+ }
+ } else {
+ if (flags & OpenFileFlags::Write) {
+ result |= O_WRONLY;
+ } else {
+ throw Exception(u"Invalid open file flag.");
+ }
+ }
+
+ if (flags & OpenFileFlags::Append) {
+ result |= O_APPEND;
+ }
+
+ if (flags & OpenFileFlags::Create) {
+ result |= O_CREAT;
+ }
+
+ if (flags & OpenFileFlags::ThrowOnExist) {
+ result |= O_EXCL;
+ }
+
+ return result;
+}
+
+int MapSeekOrigin(Stream::SeekOrigin origin) {
+ switch (origin) {
+ case Stream::SeekOrigin::Begin:
+ return SEEK_SET;
+ case Stream::SeekOrigin::Current:
+ return SEEK_CUR;
+ case Stream::SeekOrigin::End:
+ return SEEK_END;
+ default:
+ throw Exception(u"Invalid seek origin.");
+ }
+}
+} // namespace
+
+UnixFileStream::~UnixFileStream() { Close(); }
+
+UnixFileStream::UnixFileStream(String path, OpenFileFlag flags)
+ : path_(std::move(path)), flags_(flags) {
+ auto p = path_.ToUtf8();
+ file_descriptor_ =
+ ::open(p.c_str(), MapOpenFileFlag(flags_), S_IRUSR | S_IWUSR);
+ if (file_descriptor_ == -1) {
+ throw ErrnoException(
+ Format(u"Failed to open file {} with flags {}.", path_, flags_.value));
+ }
+}
+
+bool UnixFileStream::CanSeek() {
+ CheckClosed();
+ return true;
+}
+
+Index UnixFileStream::Tell() {
+ CheckClosed();
+ auto result = ::lseek(file_descriptor_, 0, SEEK_CUR);
+ if (result == -1) {
+ throw ErrnoException(u"Failed to get file position.");
+ }
+ return result;
+}
+
+void UnixFileStream::Seek(Index offset, SeekOrigin origin) {
+ CheckClosed();
+ int result = ::lseek(file_descriptor_, offset, MapSeekOrigin(origin));
+ if (result == -1) {
+ throw ErrnoException(u"Failed to seek file.");
+ }
+}
+
+bool UnixFileStream::CanRead() {
+ CheckClosed();
+ return flags_ & OpenFileFlags::Read;
+}
+
+Index UnixFileStream::Read(std::byte *buffer, Index offset, Index size) {
+ CheckClosed();
+ auto result = ::read(file_descriptor_, buffer + offset, size);
+ if (result == -1) {
+ throw ErrnoException(u"Failed to read file.");
+ }
+ return result;
+}
+
+bool UnixFileStream::CanWrite() {
+ CheckClosed();
+ return flags_ & OpenFileFlags::Write;
+}
+
+Index UnixFileStream::Write(const std::byte *buffer, Index offset, Index size) {
+ CheckClosed();
+ auto result = ::write(file_descriptor_, buffer + offset, size);
+ if (result == -1) {
+ throw ErrnoException(u"Failed to write file.");
+ }
+ return result;
+}
+
+void UnixFileStream::Close() {
+ if (closed_) return;
+ if (::close(file_descriptor_) == -1) {
+ throw ErrnoException(u"Failed to close file.");
+ }
+ closed_ = true;
+}
+
+void UnixFileStream::CheckClosed() {
+ if (closed_) {
+ throw Exception(u"File is closed.");
+ }
+}
+} // namespace cru::platform::unix