blob: 20378b58d16819cf6f97f005f920f3b496af3bf9 (
plain) (
blame)
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
|
/*
* 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
|