~nabijaczleweli/klapki.deb

ref: 50de836242adb578bc1bb6c288ddc450c4a4765c klapki.deb/src/context.hpp -rw-r--r-- 6.6 KiB
50de8362наб Fix upstream homepage link 1 year, 2 months ago
                                                                                
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// The MIT License (MIT)

// Copyright (c) 2020 наб <nabijaczleweli@nabijaczleweli.xyz>

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


#pragma once


#include "state.hpp"
#include <fmt/format.h>
#include <map>
#include <set>
#include <string_view>
#include <variant>
#include <vector>
extern "C" {
#include <efivar/efiboot.h>
#include <efivar/efivar-dp.h>
}

namespace klapki {
	struct config;
}


namespace klapki::context {
	struct our_kernel {
		std::string description;
		std::string cmdline;

		std::pair<std::string, std::string> image_path;                              // path in ESP, basename
		std::vector<std::pair<state::nonbase_dirname_t, std::string>> initrd_paths;  // path in ESP, basename
	};

	struct fresh_kernel {
		std::string_view version;
		std::pair<std::string_view, std::string_view> image;
		std::vector<std::pair<std::string_view, std::string_view>> initrds;
	};

	struct context {
		std::map<std::uint16_t, our_kernel> our_kernels;
		std::vector<fresh_kernel> fresh_kernels;

		std::set<std::pair<std::string, std::string>> deleted_files;  // path in ESP, basename

		std::optional<std::string> allocate_kernel_variant(const config & cfg, state::state & state, std::string version, std::string var, std::string image_dir,
		                                                   state::sha_t & image_sha, std::vector<std::pair<state::nonbase_dirname_t, state::shaa_t>> initrd_dirs,
		                                                   std::string image_basename, std::vector<std::pair<state::nonbase_dirname_t, std::string>> initrd_paths);
		template <class F>
		void purge_allocations(state::state & state, F && pred);
		void purge_allocations_impl(state::state & state, const state::stated_config_entry & skern);

		std::optional<std::string> propagate(const config & cfg, state::state & state);
		std::optional<std::string> age(const config & cfg, state::state & state);
		std::optional<std::string> wisen(const config & cfg, state::state & state);
		std::optional<std::string> save(const config & cfg, state::state & state);
		std::optional<std::string> commit(const config & cfg, state::state & state) const;  // Only updates file SHAs in state

		static context derive(const config & cfg, state::state & input_state);
	};

	std::variant<state::state, std::string> resolve_state_context(const config & cfg, const state::state & input_state);
}


template <class F>
void klapki::context::context::purge_allocations(state::state & state, F && pred) {
	state.statecfg.wanted_entries.erase(std::remove_if(std::begin(state.statecfg.wanted_entries), std::end(state.statecfg.wanted_entries),
	                                                   [&](auto && skern) {
		                                                   if(!pred(skern))
			                                                   return false;

		                                                   this->purge_allocations_impl(state, skern);
		                                                   return true;
	                                                   }),
	                                    std::end(state.statecfg.wanted_entries));
}


template <>
struct fmt::formatter<klapki::context::context> {
	constexpr auto parse(format_parse_context & ctx) { return ctx.begin(); }

	template <typename FormatContext>
	auto format(const klapki::context::context & context, FormatContext & ctx) {
		auto out = ctx.out();

		out = format_to(out, "{} of our kernels: [", context.our_kernels.size());

		bool first = true;
		for(auto && el : context.our_kernels) {
			if(first) {
				*out++ = '\n';
				first  = false;
			}

			out = format_to(out, "  {:04X}: {}\n", el.first, el.second);
		}


		out = format_to(out, "]\n"
		                     "Fresh kernels: [");

		first = true;
		for(auto && el : context.fresh_kernels) {
			if(first) {
				*out++ = '\n';
				first  = false;
			}

			out = format_to(out, "  {}\n", el);
		}

		out = format_to(out, "]\n"
		                     "Deleted files: [");

		first = true;
		for(auto && el : context.deleted_files) {
			if(first) {
				*out++ = '\n';
				first  = false;
			}

			out = format_to(out, "  (\"{}\", \"{}\")\n", el.first, el.second);
		}

		out = format_to(out, "]");

		return out;
	}
};

template <>
struct fmt::formatter<klapki::context::our_kernel> {
	constexpr auto parse(format_parse_context & ctx) { return ctx.begin(); }

	template <typename FormatContext>
	auto format(const klapki::context::our_kernel & kern, FormatContext & ctx) {
		auto out = ctx.out();

		out = format_to(out, "{{ \"{}\", \"{}\", (\"{}\", \"{}\"), [", kern.description, kern.cmdline, kern.image_path.first, kern.image_path.second);

		bool first = true;
		for(auto && el : kern.initrd_paths) {
			if(!first)
				out = format_to(out, ", ");
			else
				first = false;

			*out++ = '(';
			if(el.first)
				*out++ = '\"';
			out = format_to(out, "{}", el.first ? std::string_view{*el.first} : "←");
			if(el.first)
				*out++ = '\"';
			out = format_to(out, ", \"{}\")", el.second);
		}

		out = format_to(out, "] }}");

		return out;
	}
};

template <>
struct fmt::formatter<klapki::context::fresh_kernel> {
	constexpr auto parse(format_parse_context & ctx) { return ctx.begin(); }

	template <typename FormatContext>
	auto format(const klapki::context::fresh_kernel & kern, FormatContext & ctx) {
		auto out = ctx.out();

		out = format_to(out, "{{ \"{}\", (\"{}\", \"{}\"), [", kern.version, kern.image.first, kern.image.second);

		bool first = true;
		for(auto && el : kern.initrds) {
			if(!first)
				out = format_to(out, ", ");
			else
				first = false;

			out = format_to(out, "(\"{}\", \"{}\")", el.first, el.second);
		}

		out = format_to(out, "] }}");

		return out;
	}
};