summaryrefslogtreecommitdiff
path: root/include/fud_file.hpp
blob: 82f8291d9b87cc5a2628a9922dde2462baaf9d7e (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * libfud
 * Copyright 2024 Dominick Allen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FUD_FILE_HPP
#define FUD_FILE_HPP

#include "fud_drain.hpp"
#include "fud_option.hpp"
#include "fud_permissions.hpp"
#include "fud_result.hpp"
#include "fud_status.hpp"
#include "fud_string_view.hpp"
#include "fud_vector.hpp"

#include <fcntl.h>

namespace fud {

/** \brief Access Modes for File */
enum class FileAccessMode : uint8_t
{
    Read = 0x01,
    Write = 0x02,
    ReadWrite = Read | Write
};

// enum class OpenFlagEnum : uint32_t
enum class OpenFlagEnum : uint8_t
{
    Append = 0x01,
    Truncate = Append << 1,
    CloseOnExec = Truncate << 1,
    DataSync = CloseOnExec << 1,
    Direct = DataSync << 1,
    NoAtime = Direct << 1,
    NonBlock = NoAtime << 1,
    FileSync = NonBlock << 1
};

class OpenFlags {
  public:
    using FlagType = std::underlying_type_t<OpenFlagEnum>;

    constexpr OpenFlags() noexcept = default;
    constexpr OpenFlags(const OpenFlags& rhs) noexcept = default;
    constexpr OpenFlags(OpenFlags&& rhs) noexcept = default;
    constexpr ~OpenFlags() noexcept = default;
    constexpr OpenFlags& operator=(const OpenFlags& rhs) noexcept = default;
    constexpr OpenFlags& operator=(OpenFlags&& rhs) noexcept = default;

    constexpr OpenFlags(OpenFlagEnum mode) noexcept : m_mask{static_cast<FlagType>(mode)}
    {
    }

    constexpr OpenFlags operator|(OpenFlags rhs) const noexcept
    {
        OpenFlags mode{*this};
        mode.m_mask |= rhs.m_mask;
        return mode;
    }

    constexpr OpenFlags& operator|=(OpenFlags rhs) noexcept
    {
        m_mask |= rhs.m_mask;
        return *this;
    }

    constexpr OpenFlags& operator|=(OpenFlagEnum rhs) noexcept
    {
        m_mask |= static_cast<FlagType>(rhs);
        return *this;
    }

    constexpr OpenFlags operator|(OpenFlagEnum rhs) const noexcept
    {
        OpenFlags mode{*this};
        mode.m_mask |= static_cast<FlagType>(rhs);
        return mode;
    }

    constexpr uint32_t flags() const noexcept
    {
        uint32_t openFlags = 0;
        openFlags |= static_cast<uint32_t>(hasFlag(OpenFlagEnum::Append)) * O_APPEND;
        openFlags |= static_cast<uint32_t>(hasFlag(OpenFlagEnum::Truncate)) * O_TRUNC;
        openFlags |= static_cast<uint32_t>(hasFlag(OpenFlagEnum::CloseOnExec)) * O_CLOEXEC;
        openFlags |= static_cast<uint32_t>(hasFlag(OpenFlagEnum::DataSync)) * O_DSYNC;
        openFlags |= static_cast<uint32_t>(hasFlag(OpenFlagEnum::Direct)) * O_DIRECT;
        openFlags |= static_cast<uint32_t>(hasFlag(OpenFlagEnum::NoAtime)) * O_NOATIME;
        openFlags |= static_cast<uint32_t>(hasFlag(OpenFlagEnum::NonBlock)) * O_NONBLOCK;
        openFlags |= static_cast<uint32_t>(hasFlag(OpenFlagEnum::FileSync)) * O_SYNC;
        return openFlags;
    }

    constexpr bool hasFlag(OpenFlagEnum flag) const noexcept
    {
        return (m_mask & static_cast<FlagType>(flag)) != 0;
    }

  private:
    FlagType m_mask{0};
};

constexpr OpenFlags operator|(OpenFlagEnum lhs, OpenFlagEnum rhs)
{
    OpenFlags mode{lhs};
    return mode | rhs;
}

class RegularFile;
using FileResult = Result<RegularFile, FudStatus>;

class RegularFile {
  public:
    friend class BufferedRegularFile;
    static FileResult open(StringView filename, FileAccessMode mode, OpenFlags flags, Option<int> dirFdoption);

    static FileResult create(
        StringView filename,
        FileAccessMode mode,
        OpenFlags flags,
        Permissions permissions,
        bool createOnly,
        Option<int> dirFdOption);

    FudStatus close();

    FudStatus take(RegularFile& rhs);

    Result<size_t, FudStatus> size() const;

    constexpr int fileDescriptor() const
    {
        return m_fd;
    }

    [[nodiscard]] constexpr bool isOpen() const
    {
        return m_fd >= 0;
    }

    FudStatus seekStart();

    FudStatus seekEnd();

    FudStatus seek(size_t position);

    /** \brief Write from source to file as sink. */
    DrainResult write(const std::byte* source, size_t length, size_t maxExtraAttempts = 0);

    DrainResult read(std::byte* sink, size_t length, size_t maxExtraAttempts = 0);

  private:
    constexpr RegularFile() = default;

    FudStatus validateIOParameters(const std::byte* source) const;

  public:
    RegularFile(const RegularFile& rhs) = delete;

    constexpr RegularFile(RegularFile&& rhs) noexcept :
        m_position{rhs.m_position}, m_fd{rhs.m_fd}, m_openFlags{rhs.m_openFlags}, m_modeFlags{rhs.m_modeFlags}
    {
        rhs.m_fd = -1;
    }

    ~RegularFile();

    RegularFile& operator=(const RegularFile& rhs) = delete;

    RegularFile& operator=(RegularFile&& rhs) noexcept;

  private:
    size_t m_position{0};
    int m_fd{-1};
    OpenFlags m_openFlags{};
    FileAccessMode m_modeFlags{};
};

class BufferedRegularFile {
  public:
    static BufferedRegularFile make(RegularFile&& file, Vector<std::byte>&& buffer = Vector<std::byte>::NullVector());

    FudStatus close(bool discardBuffer);

    /** \brief Write from source to file as sink. */
    DrainResult write(const std::byte* source, size_t length, Option<size_t> maxExtraAttempts);

    /** \brief Read from file as source to sink. */
    DrainResult read(std::byte* sink, size_t length, Option<size_t> maxExtraAttempts);

    FudStatus setBuffer(Vector<std::byte>&& buffer, bool discardOldBuffer);

    DrainResult flush(size_t maxExtraAttempts = 0);

    void discard();

    FudStatus resizeBuffer(size_t size);

    FudStatus seekStart();

    FudStatus seekEnd();

    FudStatus seek(size_t position);

    constexpr const RegularFile& file() const
    {
        return m_file;
    }

    constexpr RegularFile& file()
    {
        return m_file;
    }

    [[nodiscard]] constexpr bool bufferEmpty() const
    {
        return m_bufferLength == 0;
    }

  private:
    constexpr BufferedRegularFile() noexcept = default;

    constexpr BufferedRegularFile(RegularFile&& regularFile, Vector<std::byte>&& buffer) noexcept :
        m_buffer{std::move(buffer)}, m_file{std::move(regularFile)}
    {
    }

    Vector<std::byte> m_buffer{Vector<std::byte>::NullVector()};

    RegularFile m_file;

    size_t m_bufferLength{0};
    size_t m_bufferPosition{0};

    enum class Operation : uint8_t
    {
        None,
        Write,
        Read
    };

    Operation m_lastOperation{Operation::None};
};

} // namespace fud

#endif