summaryrefslogtreecommitdiff
path: root/src/io.hh
blob: 90ad52ef20c31832b13bf9f85d2d4f8e081733f3 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef IO_HH
#define IO_HH

#include <cstddef>
#include <expected>
#include <memory>
#include <string>
#include <utility>

namespace io {

enum class ReadError : uint8_t {
  Error,
  Eof,
  InvalidData,  // invalid data read (not used by raw file)
  MaxTooSmall,  // max argument needs to be bigger (not used by raw file)
};

enum class WriteError : uint8_t {
  Error,
};

enum class OpenError : uint8_t {
  NoSuchFile,
  NoAccess,
  Error,
};

enum class CreateError : uint8_t {
  Exists,
  NoAccess,
  Error,
};

enum class PipeError : uint8_t {
  Error,
};

class Reader {
 public:
  virtual ~Reader() = default;

  [[nodiscard]] virtual std::expected<size_t, ReadError> read(void* dst,
                                                              size_t max) = 0;
  [[nodiscard]] virtual std::expected<size_t, ReadError> skip(size_t max) = 0;

  [[nodiscard]] std::expected<size_t, ReadError> repeat_read(void* dst,
                                                             size_t max);
  [[nodiscard]] std::expected<size_t, ReadError> repeat_skip(size_t max);

  [[nodiscard]]
  virtual int raw_fd() const = 0;

 protected:
  Reader() = default;

  Reader(Reader const&) = delete;
  Reader& operator=(Reader const&) = delete;
};

class Writer {
 public:
  virtual ~Writer() = default;

  [[nodiscard]]
  virtual std::expected<size_t, WriteError> write(void const* dst,
                                                  size_t size) = 0;

  // Use this instead of the destructor if you care if the call returned an
  // error or not. Regardless of returned value, after this method is called
  // write will always fail.
  [[nodiscard]]
  virtual std::expected<void, WriteError> close() = 0;

  [[nodiscard]]
  virtual int raw_fd() const = 0;

  [[nodiscard]] std::expected<size_t, WriteError> repeat_write(void const* dst,
                                                               size_t size);

 protected:
  Writer() = default;

  Writer(Writer const&) = delete;
  Writer& operator=(Writer const&) = delete;
};

[[nodiscard]] std::expected<std::unique_ptr<Reader>, OpenError> open(
    const std::string& file_path);
[[nodiscard]] std::expected<std::unique_ptr<Reader>, OpenError> openat(
    int dirfd, const std::string& file_path);
[[nodiscard]] std::unique_ptr<Reader> memory(std::string data);

[[nodiscard]] std::expected<std::unique_ptr<Writer>, CreateError> create(
    const std::string& file_path, bool replace_existing);
[[nodiscard]] std::expected<std::unique_ptr<Writer>, CreateError> createat(
    int dirfd, const std::string& file_path, bool replace_existing);

[[nodiscard]] std::expected<
    std::pair<std::unique_ptr<Reader>, std::unique_ptr<Writer>>, PipeError>
pipe();

// Takes ownership of the fd.
[[nodiscard]] std::unique_ptr<Reader> reader_from_raw(int fd);
[[nodiscard]] std::unique_ptr<Writer> writer_from_raw(int fd);

}  // namespace io

#endif  // IO_HH