diff options
| author | Lexi Winter <lexi@le-fay.org> | 2025-06-28 20:40:25 +0100 |
|---|---|---|
| committer | Lexi Winter <lexi@le-fay.org> | 2025-06-28 20:40:25 +0100 |
| commit | c54ff48ac3abb62a40eb1a438da8e3e7ef139797 (patch) | |
| tree | 4a78c556c7cbc6d3d7e364ca0c52c57ac0f5094b /nihil.posix/posix.write_file.ccm | |
| parent | a2d7181700ac64b8e7a4472ec26dfa253b38f188 (diff) | |
| download | nihil-c54ff48ac3abb62a40eb1a438da8e3e7ef139797.tar.gz nihil-c54ff48ac3abb62a40eb1a438da8e3e7ef139797.tar.bz2 | |
posix: add tempfile()
Diffstat (limited to 'nihil.posix/posix.write_file.ccm')
| -rw-r--r-- | nihil.posix/posix.write_file.ccm | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/nihil.posix/posix.write_file.ccm b/nihil.posix/posix.write_file.ccm new file mode 100644 index 0000000..867e0db --- /dev/null +++ b/nihil.posix/posix.write_file.ccm @@ -0,0 +1,82 @@ +/* + * This source code is released into the public domain. + */ + +module; + +#include <coroutine> +#include <expected> +#include <filesystem> +#include <ranges> +#include <system_error> +#include <vector> + +#include <fcntl.h> +#include <unistd.h> + +export module nihil.posix:write_file; + +import nihil.error; +import nihil.guard; +import nihil.monad; +import :fd; +import :open; +import :rename; + +namespace nihil { + +/* + * Write the contents of a range to a file. Returns the number of bytes + * written. + */ +export [[nodiscard]] +auto write_file(std::filesystem::path const &filename, + std::ranges::contiguous_range auto &&range, + int mode = 0777) + -> std::expected<std::size_t, error> +{ + auto file = co_await open(filename, O_CREAT|O_WRONLY, mode); + auto nbytes = co_await write(file, range); + co_return nbytes; +} + +/* + * Utility wrapper for non-contiguous ranges. + */ +export [[nodiscard]] +auto write_file(std::filesystem::path const &filename, + std::ranges::range auto &&range) + -> std::expected<std::size_t, error> +requires(!std::ranges::contiguous_range<decltype(range)>) +{ + return write_file(filename, std::vector(std::from_range, range)); +} + +/* + * Write the contents of a range to a file safely. The data will be written + * to "<filename>.tmp", and if the write succeeds, the temporary file will be + * renamed to the target filename. If an error occurs, the target file will + * not be modified. + */ +export [[nodiscard]] +auto safe_write_file(std::filesystem::path const &filename, + std::ranges::range auto &&range) + -> std::expected<void, error> +{ + auto tmpfile = filename; + tmpfile.remove_filename(); + tmpfile /= (filename.filename().native() + ".tmp"); + + auto tmpfile_guard = guard([&tmpfile] { + ::unlink(tmpfile.c_str()); + }); + + co_await write_file(tmpfile, range); + co_await nihil::rename(tmpfile, filename); + + tmpfile_guard.release(); + co_return {}; +} + + +} // namespace nihil |
