summaryrefslogtreecommitdiff
path: root/src/io.hh
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2021-11-17 22:34:57 +0100
committerJoel Klinghed <the_jk@spawned.biz>2021-11-17 22:34:57 +0100
commit6232d13f5321b87ddf12a1aa36b4545da45f173d (patch)
tree23f3316470a14136debd9d02f9e920ca2b06f4cc /src/io.hh
Travel3: Simple image and video display site
Reads the images and videos from filesystem and builds a site in memroy.
Diffstat (limited to 'src/io.hh')
-rw-r--r--src/io.hh113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/io.hh b/src/io.hh
new file mode 100644
index 0000000..f13b51b
--- /dev/null
+++ b/src/io.hh
@@ -0,0 +1,113 @@
+#ifndef IO_HH
+#define IO_HH
+
+#include "unique_fd.hh"
+
+#include <filesystem>
+#include <stdint.h>
+
+class Buffer;
+class RoBuffer;
+
+namespace io {
+
+bool make_nonblocking(int fd);
+
+bool mkdirs(std::filesystem::path const& path, mode_t mode);
+bool mkdirs(int fd, std::filesystem::path const& path, mode_t mode);
+
+bool read_all(int fd, void* data, size_t size);
+bool write_all(int fd, void const* data, size_t size);
+bool seek_all(int fd, size_t bytes);
+
+enum class Return {
+ OK,
+ ERR,
+ CLOSED,
+};
+
+Return fill(int fd, Buffer* out, size_t buf_request_size = 1,
+ size_t* bytes = nullptr);
+// Returns false in case of error. EAGAIN or EWOULDBLOCK are not errors.
+bool drain(RoBuffer* in, int fd, size_t* bytes = nullptr);
+
+enum class open_flags : unsigned {
+ rdonly = 00,
+ wronly = 01,
+ rdwr = 02,
+ create = 0100,
+ excl = 0200,
+ trunc = 01000,
+ append = 02000,
+ directory = 0200000,
+ path = 010000000,
+};
+
+unique_fd open(std::filesystem::path const& path, open_flags flags,
+ std::filesystem::perms perms = std::filesystem::perms::none);
+unique_fd openat(int fd, std::filesystem::path const& path, open_flags flags,
+ std::filesystem::perms perms = std::filesystem::perms::none);
+
+bool close(int fd);
+
+ssize_t pread(int fd, void *buf, size_t count, off_t offset);
+ssize_t read(int fd, void *buf, size_t count);
+ssize_t write(int fd, const void *buf, size_t count);
+
+enum class access_mode : unsigned {
+ exists = 00,
+ read = 01,
+ write = 02,
+ exec = 04,
+};
+
+bool access(std::filesystem::path const& path, access_mode mode);
+bool unlinkat(int fd, std::filesystem::path const& path);
+bool fallocate(int fd, off_t offset, off_t len);
+
+constexpr open_flags operator&(open_flags a, open_flags b) noexcept {
+ using utype = typename std::underlying_type<open_flags>::type;
+ return static_cast<open_flags>(static_cast<utype>(a) & static_cast<utype>(b));
+}
+
+constexpr open_flags operator|(open_flags a, open_flags b) noexcept {
+ using utype = typename std::underlying_type<open_flags>::type;
+ return static_cast<open_flags>(static_cast<utype>(a) | static_cast<utype>(b));
+}
+
+constexpr open_flags operator^(open_flags a, open_flags b) noexcept {
+ using utype = typename std::underlying_type<open_flags>::type;
+ return static_cast<open_flags>(static_cast<utype>(a) ^ static_cast<utype>(b));
+}
+
+constexpr open_flags operator~(open_flags a) noexcept {
+ using utype = typename std::underlying_type<open_flags>::type;
+ return static_cast<open_flags>(~static_cast<utype>(a));
+}
+
+constexpr access_mode operator&(access_mode a, access_mode b) noexcept {
+ using utype = typename std::underlying_type<access_mode>::type;
+ return static_cast<access_mode>(
+ static_cast<utype>(a) & static_cast<utype>(b));
+}
+
+constexpr access_mode operator|(access_mode a, access_mode b) noexcept {
+ using utype = typename std::underlying_type<access_mode>::type;
+ return static_cast<access_mode>(
+ static_cast<utype>(a) | static_cast<utype>(b));
+}
+
+constexpr access_mode operator^(access_mode a, access_mode b) noexcept {
+ using utype = typename std::underlying_type<access_mode>::type;
+ return static_cast<access_mode>(
+ static_cast<utype>(a) ^ static_cast<utype>(b));
+}
+
+constexpr access_mode operator~(access_mode a) noexcept {
+ using utype = typename std::underlying_type<access_mode>::type;
+ return static_cast<access_mode>(~static_cast<utype>(a));
+}
+
+} // namespace io
+
+#endif // IO_HH