aboutsummaryrefslogtreecommitdiffstats
path: root/liblfjail/fileutils.hh
blob: 0f7c1a7a58c6c8102d825dab50daabf249d360af (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
/*
 * This source code is released into the public domain.
 */

#ifndef	LFJAIL_READ_FILE_HH
#define	LFJAIL_READ_FILE_HH

#include <array>
#include <iterator>
#include <filesystem>
#include <format>
#include <ranges>
#include <span>
#include <string>
#include <vector>

#include <fcntl.h>
#include <unistd.h>

#include "context.hh"
#include "io_error.hh"
#include "guard.hh"

/*
 * std::formatter for path was only added in C++26; LLVM 19 doesn't have it.
 */

#ifndef __cpp_lib_format_path
template<>
struct std::formatter<std::filesystem::path, char>
{
	template<typename ParseContext>
	constexpr auto parse(ParseContext &ctx) -> ParseContext::iterator {
		return ctx.begin();
	}

	template<typename FmtContext>
	auto format(std::filesystem::path const &path,
		    FmtContext& ctx) const -> FmtContext::iterator {
		return std::ranges::copy(path.native(), ctx.out()).out;
	}
};
#endif	// !__cpp_lib_format_path

namespace lfjail {

void ensure_dir(context const &ctx, std::filesystem::path const &dir);

/*
 * Load the contents of a file into an output iterator.  Throws io_error
 * on failure.
 */

void read_file(std::filesystem::path const &filename,
	       std::output_iterator<char> auto &&iter)
{
	constexpr std::size_t bufsize = 1024;
	std::array<char, bufsize> buffer;
	int fd, err;

	if ((fd = ::open(filename.c_str(), O_RDONLY)) == -1)
		throw io_error(filename, errno);

	auto fd_guard = guard([fd] { ::close(fd); });

	while ((err = ::read(fd, &buffer[0], buffer.size())) > 0) {
		auto data = std::span(buffer).subspan(0, err);
		std::ranges::copy(data, iter);
	}

	if (err == -1)
		throw io_error(filename, errno);
}


/*
 * Write the contents of a range to a file.  Throws io_error on error.
 */

void write_file(std::filesystem::path const &filename,
		std::ranges::range auto &&range) {
	// Convert the range into an array.
	std::vector<char> chars;
	std::ranges::copy(range, std::back_inserter(chars));

	// Open the output file.
	int fd;

	if ((fd = ::open(filename.c_str(), O_CREAT|O_WRONLY, 0777)) == -1)
		throw io_error(filename, errno);

	auto fd_guard = guard([fd] { ::close(fd); });

	// Write the data.
	if (int err = ::write(fd, chars.data(), chars.size()); err < 0)
		throw io_error(filename, errno);
}

/*
 * 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.
 */
void safe_write_file(std::filesystem::path const &filename,
		     std::ranges::range auto &&range) {
	auto tmpfile(filename);
	tmpfile.remove_filename();
	tmpfile /= (filename.filename().native() + ".tmp");

	auto tmpfile_guard = guard([tmpfile] { ::unlink(tmpfile.c_str()); });

	write_file(tmpfile, std::forward<decltype(range)>(range));
	int err = ::rename(tmpfile.c_str(), filename.c_str());
	if (err != 0)
		throw io_error(filename, errno);
}

} // namespace lfjail

#endif	// !LFJAIL_FILEUTILS_HH