aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.posix/tempfile.ccm
blob: 15edccbfcdec74e540cfe0ce20dea50bc134e7c5 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// This source code is released into the public domain.
export module nihil.posix:tempfile;

import nihil.std;
import nihil.util;
import :fd;
import :getenv;
import :open;
import :unlink;

// tempfile: create a temporary file.

namespace nihil {

struct tempfile_flags_tag
{
};
export using tempfile_flags = flagset<std::uint8_t, tempfile_flags_tag>;

// No flags.
export inline constexpr auto tempfile_none = tempfile_flags();

// Unlink the tempfile immediately after creating it
export inline constexpr auto tempfile_unlink = tempfile_flags::bit<0>();

export struct temporary_file final
{
	// Fetch the file's fd.
	[[nodiscard]] auto fd(this temporary_file &self) -> nihil::fd &
	{
		if (!self.m_fd)
			throw std::logic_error("fd() called on empty temporary_file");

		return self.m_fd;
	}

	// Fetch the name of this file.  If tempfile_unlink was specified,
	// throws std::logic_error.
	[[nodiscard]] auto path(this temporary_file const &self) -> std::filesystem::path const &
	{
		if (self.m_path.empty())
			throw std::logic_error("path() called on unlinked temporary_file");

		return self.m_path;
	}

	// Release this temporary file, causing it to be closed and deleted immediately
	// Throws std::logic_error if the file has already been released.
	auto release(this temporary_file &self) -> void
	{
		if (!self.m_fd)
			throw std::logic_error("release() called on already-released tempfile");

		if (!self.m_path.empty()) {
			std::ignore = unlink(self.path());
			self.m_path.clear();
		}

		std::ignore = self.m_fd.close();
	}

	// Destructor; unlink the file if we didn't already.
	~temporary_file() // NOLINT
	{
		if (m_fd)
			release();
	}

	// Not copyable.
	temporary_file(temporary_file const &) = delete;

	// Movable.
	temporary_file(temporary_file &&other) noexcept = default;

	// Not assignable.
	auto operator=(this temporary_file &, temporary_file const &) -> temporary_file & = delete;
	auto
	operator=(this temporary_file &, temporary_file &&) noexcept -> temporary_file & = delete;

private:
	// The file descriptor for the file.
	nihil::fd m_fd;
	std::filesystem::path m_path;

	temporary_file(nihil::fd &&fd, std::filesystem::path path) noexcept
		: m_fd(std::move(fd))
		, m_path(std::move(path))
	{
	}

	explicit temporary_file(nihil::fd &&fd) noexcept
		: m_fd(std::move(fd))
	{
	}

	friend auto tempfile(tempfile_flags flags) -> std::expected<temporary_file, error>;
};

/*
 * Create a temporary file and return it.
 */
export [[nodiscard]] auto
tempfile(tempfile_flags flags = tempfile_none) -> std::expected<temporary_file, error>
{
	auto rng = std::default_random_engine(std::random_device{}());

	auto random_name = [&] -> std::string {
		auto constexpr length = std::size_t{12};
		auto constexpr randchars = std::string_view("abcdefghijklmnopqrstuvwxyz"
		                                            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		                                            "0123456789");

		auto dist = std::uniform_int_distribution<>(0, randchars.size() - 1);
		auto ret = std::string(length, 0);
		std::ranges::generate_n(ret.begin(), length,
		                        [&] -> char { return randchars[dist(rng)]; });
		return ret;
	};

	auto dir = std::filesystem::path(getenv("TMPDIR").value_or("/tmp")); // NOLINT

	// Keep trying until we don't get EEXIST.
	for (;;) {
		auto filename = dir / (random_name() + ".tmp");
		auto fd =
			nihil::open(filename, open_readwrite | open_create | open_exclusive, 0600);
		if (!fd) {
			if (fd.error() == std::errc::file_exists)
				continue;
			return std::unexpected(fd.error());
		}

		if (flags & tempfile_unlink) {
			std::ignore = unlink(filename);
			return temporary_file(std::move(*fd));
		}

		return temporary_file(std::move(*fd), std::move(filename));
	}
}

} // namespace nihil