aboutsummaryrefslogtreecommitdiff
path: root/src/base/platform/unix/UnixFileStream.cpp
blob: e1a6080b943a36117a60a2d77d9b75c80b7eb695 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "cru/base/platform/unix/UnixFileStream.h"
#include "cru/base/Exception.h"
#include "cru/base/Format.h"
#include "cru/base/io/Stream.h"

#include <fcntl.h>
#include <sys/fcntl.h>
#include <unistd.h>

namespace cru::platform::unix {
using namespace cru::io;

namespace {
bool OflagCanSeek([[maybe_unused]] int oflag) {
  // Treat every file seekable.
  return true;
}

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; }

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(const char *path, int oflag, mode_t mode) {
  file_descriptor_ = UnixFileDescriptor(::open(path, oflag, mode));
  if (file_descriptor_ == -1) {
    throw ErrnoException(
        Format(u"Failed to open file {} with oflag {}, mode {}.",
               String::FromUtf8(path), oflag, mode));
  }

  SetSupportedOperations(
      {OflagCanSeek(oflag), OflagCanRead(oflag), OflagCanWrite(oflag)});
}

UnixFileStream::UnixFileStream(UnixFileDescriptor fd, bool can_seek,
                               bool can_read, bool can_write)
    : Stream(can_seek, can_read, can_write), file_descriptor_(std::move(fd)) {}

UnixFileStream::~UnixFileStream() { DoClose(); }

Index UnixFileStream::DoSeek(Index offset, SeekOrigin origin) {
  off_t result = ::lseek(file_descriptor_, offset, MapSeekOrigin(origin));
  if (result == -1) {
    throw ErrnoException(u"Failed to seek file.");
  }
  return result;
}

Index UnixFileStream::DoRead(std::byte *buffer, Index offset, Index size) {
  auto result = ::read(file_descriptor_, buffer + offset, size);
  if (result == -1) {
    throw ErrnoException(u"Failed to read file.");
  }
  return result;
}

Index UnixFileStream::DoWrite(const std::byte *buffer, Index offset,
                              Index size) {
  auto result = ::write(file_descriptor_, buffer + offset, size);
  if (result == -1) {
    throw ErrnoException(u"Failed to write file.");
  }
  return result;
}

void UnixFileStream::DoClose() {
  CRU_STREAM_BEGIN_CLOSE
  if (file_descriptor_) {
    file_descriptor_ = {};
  }
}
}  // namespace cru::platform::unix