summaryrefslogtreecommitdiff
path: root/include/fud_file.hpp
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-10-30 09:51:54 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-10-30 09:51:54 -0500
commit6c7fd1db481ff10a16ecab958c6542784fa60b9c (patch)
treee0162f930ec44a7c53b07061311d52910c36e481 /include/fud_file.hpp
parent8dcb1de91e15ff7fc66279cd9cd9ad8a70f624e0 (diff)
Use std::byte* instead of void* for allocators.
Diffstat (limited to 'include/fud_file.hpp')
-rw-r--r--include/fud_file.hpp68
1 files changed, 39 insertions, 29 deletions
diff --git a/include/fud_file.hpp b/include/fud_file.hpp
index 9ef2cbc..51826a6 100644
--- a/include/fud_file.hpp
+++ b/include/fud_file.hpp
@@ -18,12 +18,13 @@
#ifndef FUD_FILE_HPP
#define FUD_FILE_HPP
-#include "fud_allocator.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 "fud_drain.hpp"
#include <fcntl.h>
@@ -37,16 +38,17 @@ enum class FileAccessMode : uint8_t
ReadWrite = Read | Write
};
-enum class OpenFlagEnum : uint32_t
+// enum class OpenFlagEnum : uint32_t
+enum class OpenFlagEnum : uint8_t
{
- Append = O_APPEND,
- Truncate = O_TRUNC,
- CloseOnExec = O_CLOEXEC,
- DataSync = O_DSYNC,
- Direct = O_DIRECT,
- NoAtime = O_NOATIME,
- NonBlock = O_NONBLOCK,
- FileSync = O_SYNC
+ 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 {
@@ -90,12 +92,20 @@ class OpenFlags {
return mode;
}
- constexpr FlagType flags() const noexcept
- {
- return m_mask;
+ 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) noexcept
+ constexpr bool hasFlag(OpenFlagEnum flag) const noexcept
{
return (m_mask & static_cast<FlagType>(flag)) != 0;
}
@@ -110,16 +120,6 @@ constexpr OpenFlags operator|(OpenFlagEnum lhs, OpenFlagEnum rhs)
return mode | rhs;
}
-struct [[nodiscard]] ReadResult {
- size_t bytesRead{0};
- FudStatus status{FudStatus::Success};
-};
-
-struct [[nodiscard]] WriteResult {
- size_t bytesWritten{0};
- FudStatus status{FudStatus::Success};
-};
-
class RegularFile;
using FileResult = Result<RegularFile, FudStatus>;
@@ -129,8 +129,7 @@ class RegularFile {
StringView filename,
FileAccessMode mode,
OpenFlags flags,
- Option<int> dirFdoption,
- Allocator* allocator = &globalFudAllocator);
+ Option<int> dirFdoption);
static FileResult create(
StringView filename,
@@ -138,8 +137,7 @@ class RegularFile {
OpenFlags flags,
Permissions permissions,
bool exclusive,
- Option<int> dirFdOption,
- Allocator* allocator = &globalFudAllocator);
+ Option<int> dirFdOption);
FudStatus close();
@@ -163,8 +161,20 @@ class RegularFile {
private:
int m_fd{-1};
- FileAccessMode m_modeFlags{};
OpenFlags m_openFlags{};
+ FileAccessMode m_modeFlags{};
+};
+
+class BufferedRegularFile {
+public:
+ DrainResult write(const void* source, size_t sourceSize, size_t length, size_t offset);
+ // DrainResult read(void* sink, );
+private:
+ Vector<std::byte> m_readBuffer{Vector<std::byte>::NullVector()};
+ Vector<std::byte> m_writeBuffer{Vector<std::byte>::NullVector()};
+ RegularFile m_file;
+ bool m_readBuffered{false};
+ bool m_writeBuffered{false};
};
} // namespace fud