Repository navigation
Expand file tree
/
Copy pathnob.h
More file actions
364 lines (313 loc) · 11.1 KB
/
Copy pathnob.h
File metadata and controls
364 lines (313 loc) · 11.1 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
*
Copyright 2020 Alexey Kutepov <reximkut@gmail.com>
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
#define _POSIX_C_SOURCE 200809L
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#define PATH_SEP "/"
typedef pid_t Pid;
typedef int Fd;
#include <cassert>
#include <cstdio>
#include <cstdarg>
#include <cstring>
#include <iostream>
#include <filesystem>
#include <optional>
#include <sstream>
#include <string>
#include <string_view>
#include <system_error>
#include <vector>
using std::string;
using std::vector;
std::string cstr_no_ext(const char* path);
#define NOEXT(path) cstr_no_ext(path)
template<typename... Args>
vector<string> cstr_array_make(Args... args)
{
vector<string> result;
(result.emplace_back(args), ...);
return result;
}
vector<string> cstr_array_from_int_char(int argc, char** argv);
std::string cstr_array_join(const char* sep, const vector<string> &cstrs);
#define JOIN(sep, ...) cstr_array_join(sep, cstr_array_make(__VA_ARGS__))
#define CONCAT(...) JOIN("", __VA_ARGS__)
#define PATH(...) JOIN(PATH_SEP, __VA_ARGS__)
typedef struct {
vector<string> line;
} Cmd;
std::string cmd_show(const Cmd &cmd);
Pid cmd_run_async(Cmd cmd, Fd *fdin, Fd *fdout);
void cmd_run_sync(Cmd cmd);
typedef std::vector<Cmd> Cmd_Array;
// TODO(#1): no way to disable echo in nobuild scripts
// TODO(#2): no way to ignore fails
#define CMD(...) \
do { \
Cmd cmd = { \
.line = cstr_array_make(__VA_ARGS__) \
}; \
std::string cmd_to_show = cmd_show(cmd); \
INFO("CMD: %s", cmd_to_show.c_str()); \
cmd_run_sync(cmd); \
} while (0)
// TODO: make common with nob workflow
#ifndef REBUILD_URSELF
# define REBUILD_URSELF(binary_path, source_path) CMD("g++", "-std=c++23", "-fsanitize=address,undefined", "-fsanitize-address-use-after-scope -D_GLIBCXX_DEBUG", "-o", binary_path, source_path)
#endif
// Go Rebuild Urself™ Technology
//
// How to use it:
// int main(int argc, char** argv) {
// GO_REBUILD_URSELF(argc, argv);
// // actual work
// return 0;
// }
//
// After your added this macro every time you run ./nobuild it will detect
// that you modified its original source code and will try to rebuild itself
// before doing any actual work. So you only need to bootstrap your build system
// once.
//
// The modification is detected by comparing the last modified times of the executable
// and its source code. The same way the make utility usually does it.
//
// The rebuilding is done by using the REBUILD_URSELF macro which you can redefine
// if you need a special way of bootstraping your build system. (which I personally
// do not recommend since the whole idea of nobuild is to keep the process of bootstrapping
// as simple as possible and doing all of the actual work inside of the nobuild)
//
#define GO_REBUILD_URSELF(argc, argv) \
do { \
const char *source_path = __FILE__; \
assert(argc >= 1); \
const char *binary_path = argv[0]; \
\
if (is_path1_modified_after_path2(source_path, binary_path)) { \
RENAME(binary_path, CONCAT(binary_path, ".old").data()); \
REBUILD_URSELF(binary_path, source_path); \
Cmd cmd = { \
.line = cstr_array_from_int_char(argc, argv), \
}; \
std::string cmd_to_show = cmd_show(cmd); \
INFO("CMD: %s", cmd_to_show.c_str()); \
cmd_run_sync(cmd); \
exit(0); \
} \
} while(0)
// The implementation idea is stolen from https://github.com/zhiayang/nabs
void rebuild_urself(const char *binary_path, const char *source_path);
void path_rename(const char* old_path, const char* new_path);
#define RENAME(old_path, new_path) \
do { \
INFO("RENAME: %s -> %s", old_path, new_path); \
path_rename(old_path, new_path); \
} while (0)
#if defined(__GNUC__) || defined(__clang__)
// https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html
#define NOBUILD_PRINTF_FORMAT(STRING_INDEX, FIRST_TO_CHECK) __attribute__ ((format (printf, STRING_INDEX, FIRST_TO_CHECK)))
#else
#define NOBUILD_PRINTF_FORMAT(STRING_INDEX, FIRST_TO_CHECK)
#endif
void VLOG(FILE *stream, const char* tag, const char* fmt, va_list args);
void INFO(const char* fmt, ...) NOBUILD_PRINTF_FORMAT(1, 2);
void WARN(const char* fmt, ...) NOBUILD_PRINTF_FORMAT(1, 2);
void ERRO(const char* fmt, ...) NOBUILD_PRINTF_FORMAT(1, 2);
void PANIC(const char* fmt, ...) NOBUILD_PRINTF_FORMAT(1, 2);
////////////////////////////////////////////////////////////////////////////////
#ifdef NOBUILD_IMPLEMENTATION
vector<string> cstr_array_from_int_char(int argc, char** argv)
{
vector<string> result;
for (int i = 0; i < argc; ++i) {
result.push_back(argv[i]);
}
return result;
}
std::string cstr_no_ext(const char* path)
{
std::string_view p(path);
size_t dot_pos = p.find_last_of('.');
if (dot_pos != std::string_view::npos) {
return std::string(p.substr(0, dot_pos));
}
return std::string(p);
}
std::string cstr_array_join(const char* sep, const vector<string> &cstrs)
{
std::stringstream ss;
for (size_t i = 0; i < cstrs.size(); ++i) {
if (i > 0) {
ss << sep;
}
ss << cstrs[i];
}
return ss.str();
}
Fd fd_open_for_read(const char* path)
{
Fd result = open(path, O_RDONLY);
if (result < 0) {
PANIC("Could not open file %s: %s", path, strerror(errno));
}
return result;
}
Fd fd_open_for_write(const char* path)
{
Fd result = open(path,
O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (result < 0) {
PANIC("could not open file %s: %s", path, strerror(errno));
}
return result;
}
void fd_close(Fd fd)
{
close(fd);
}
void pid_wait(Pid pid)
{
for (;;) {
int wstatus = 0;
if (waitpid(pid, &wstatus, 0) < 0) {
PANIC("could not wait on command (pid %d): %s", pid, strerror(errno));
}
if (WIFEXITED(wstatus)) {
int exit_status = WEXITSTATUS(wstatus);
if (exit_status != 0) {
PANIC("command exited with exit code %d", exit_status);
}
break;
}
if (WIFSIGNALED(wstatus)) {
PANIC("command process was terminated by %s", strsignal(WTERMSIG(wstatus)));
}
}
}
std::string cmd_show(const Cmd &cmd)
{
// TODO(#31): cmd_show does not render the command line properly
// - No string literals when arguments contains space
// - No escaping of special characters
// - Etc.
return cstr_array_join(" ", cmd.line);
}
Pid cmd_run_async(Cmd cmd, Fd *fdin, Fd *fdout)
{
pid_t cpid = fork();
if (cpid < 0) {
std::string cmd_str = cmd_show(cmd);
PANIC("Could not fork child process: %s: %s",
cmd_str.c_str(), strerror(errno));
}
if (cpid == 0) {
std::vector<const char*> args;
for (const auto& s : cmd.line) {
args.push_back(s.c_str());
}
args.push_back(nullptr);
if (fdin) {
if (dup2(*fdin, STDIN_FILENO) < 0) {
PANIC("Could not setup stdin for child process: %s", strerror(errno));
}
}
if (fdout) {
if (dup2(*fdout, STDOUT_FILENO) < 0) {
PANIC("Could not setup stdout for child process: %s", strerror(errno));
}
}
if (execvp(args[0], (char * const*) args.data()) < 0) {
std::string cmd_str = cmd_show(cmd);
PANIC("Could not exec child process: %s: %s",
cmd_str.c_str(), strerror(errno));
}
}
return cpid;
}
void cmd_run_sync(Cmd cmd)
{
pid_wait(cmd_run_async(cmd, NULL, NULL));
}
void path_rename(const char* old_path, const char* new_path)
{
std::error_code ec;
std::filesystem::rename(old_path, new_path, ec);
if (ec) {
PANIC("could not rename %s to %s: %s", old_path, new_path, ec.message().c_str());
}
}
bool is_path1_modified_after_path2(const char *path1, const char *path2)
{
std::error_code ec;
auto time1 = std::filesystem::last_write_time(path1, ec);
if (ec) {
PANIC("could not stat %s: %s", path1, ec.message().c_str());
}
auto time2 = std::filesystem::last_write_time(path2, ec);
if (ec) {
PANIC("could not stat %s: %s", path2, ec.message().c_str());
}
return time1 > time2;
}
void VLOG(FILE *stream, const char* tag, const char* fmt, va_list args)
{
fprintf(stream, "[%s] ", tag);
vfprintf(stream, fmt, args);
fprintf(stream, "\n");
}
void INFO(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
VLOG(stderr, "INFO", fmt, args);
va_end(args);
}
void WARN(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
VLOG(stderr, "WARN", fmt, args);
va_end(args);
}
void ERRO(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
VLOG(stderr, "ERRO", fmt, args);
va_end(args);
}
void PANIC(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
VLOG(stderr, "ERRO", fmt, args);
va_end(args);
exit(1);
}
#endif // NOBUILD_IMPLEMENTATION