aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.posix/posix.tempfile.ccm
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-28 20:40:25 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-28 20:40:25 +0100
commitc54ff48ac3abb62a40eb1a438da8e3e7ef139797 (patch)
tree4a78c556c7cbc6d3d7e364ca0c52c57ac0f5094b /nihil.posix/posix.tempfile.ccm
parenta2d7181700ac64b8e7a4472ec26dfa253b38f188 (diff)
downloadnihil-c54ff48ac3abb62a40eb1a438da8e3e7ef139797.tar.gz
nihil-c54ff48ac3abb62a40eb1a438da8e3e7ef139797.tar.bz2
posix: add tempfile()
Diffstat (limited to 'nihil.posix/posix.tempfile.ccm')
-rw-r--r--nihil.posix/posix.tempfile.ccm85
1 files changed, 85 insertions, 0 deletions
diff --git a/nihil.posix/posix.tempfile.ccm b/nihil.posix/posix.tempfile.ccm
new file mode 100644
index 0000000..20378b5
--- /dev/null
+++ b/nihil.posix/posix.tempfile.ccm
@@ -0,0 +1,85 @@
+/*
+ * This source code is released into the public domain.
+ */
+
+module;
+
+/*
+ * tempfile: create a temporary file.
+ */
+
+#include <cstdint>
+#include <expected>
+#include <filesystem>
+#include <string>
+
+export module nihil.posix:tempfile;
+
+import nihil.error;
+import nihil.flagset;
+import :fd;
+
+namespace nihil {
+
+struct tempfile_flags_tag {};
+export using tempfile_flags_t = flagset<std::uint8_t, tempfile_flags_tag>;
+
+// No flags.
+export inline constexpr auto tempfile_none = tempfile_flags_t();
+
+// Unlink the tempfile immediately after creating it
+export inline constexpr auto tempfile_unlink = tempfile_flags_t::bit<0>();
+
+export struct temporary_file final {
+ /*
+ * Fetch the file's fd.
+ */
+ [[nodiscard]] auto fd(this temporary_file &) -> nihil::fd &;
+
+ /*
+ * Fetch the name of this file. If tempfile_unlink was specified,
+ * throws std::logic_error.
+ */
+ [[nodiscard]] auto path(this temporary_file const &)
+ -> std::filesystem::path const &;
+
+ /*
+ * Release this temporary file, causing it to be deleted immediately.
+ * Throws std::logic_error if the file has already been released.
+ */
+ auto release(this temporary_file &) -> void;
+
+ /*
+ * Destructor; unlink the file if we didn't already.
+ */
+ ~temporary_file();
+
+ // Not copyable.
+ temporary_file(temporary_file const &) = delete;
+ auto operator=(this temporary_file &, temporary_file const &)
+ -> temporary_file & = delete;
+
+ // Movable.
+ temporary_file(temporary_file &&other) noexcept;
+ auto operator=(this temporary_file &, temporary_file &&) noexcept
+ -> temporary_file &;
+
+private:
+ // The file descriptor for the file.
+ nihil::fd m_fd;
+ std::filesystem::path m_path;
+
+ temporary_file(nihil::fd &&fd, std::filesystem::path) noexcept;
+ temporary_file(nihil::fd &&fd) noexcept;
+
+ friend auto tempfile(tempfile_flags_t flags)
+ -> std::expected<temporary_file, error>;
+};
+
+/*
+ * Create a temporary file and return it.
+ */
+export [[nodiscard]] auto tempfile(tempfile_flags_t flags = tempfile_none)
+ -> std::expected<temporary_file, error>;
+
+} // namespace nihil