~kaction/config

ref: 42707d29c994c30956608b9c9f10cbaf1d00add4 config/system-v2/nix-sys/main.c -rw-r--r-- 6.6 KiB
42707d29 — Dmitry Bogatov nix-sys: generate build system inside of build system 1 year, 2 days 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
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
#include <cdb.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

#include "chattr.h"

struct copy_a {
	const char *source;
	const char *tmp;
	const char *destination;
	const char *backup;
	mode_t mode;
};

struct symlink_a {
	const char *source;
	const char *tmp;
	const char *destination;
	const char *backup;
};

#include "config.h" // generated by builder.py

#define ARRAY_SIZE(x) ((sizeof(x))/ (sizeof(x[0])))
#define write2(x) (void) write(2, x"", sizeof(x) - 1)

static inline void write2_s(const char *s)
{
	write(2, s, strlen(s));
}

static int ensure_directory(const char *path)
{
	struct stat sb;
	int err;

	err = lstat(path, &sb);

	/* Directory already exists */
	if (!err && ((sb.st_mode & S_IFMT) == S_IFDIR)) {
		write2("ok (exists)\n");
		return 0;
	}

	if (!err) {
		/* FIXME: include actual type into error message */
		write2("fail (exists, not a directory)\n");
		return 1;
	}

	err = mkdir(path, 0755);
	if (err) {
		/* FIXME: include reason of failure */
		write2("fail (can't create)\n");
		return 1;
	}
	write2("ok (created)\n");
	return 0;
}

static int prepare_copy(const struct copy_a *ptr)
{
	int err;
	int in, out;
	off_t off = 0;
	ssize_t bytes;

	in = open(ptr->source, O_RDONLY);
	if (in == -1) {
		write2("fail (can't open source file)\n");
		write2_s(ptr->source);
		write2("\n");
		return 1;
	}

	out = open(ptr->tmp, O_EXCL|O_WRONLY|O_CREAT|O_NOFOLLOW, 0);
	if (out == -1) {
		write2("fail (can't open temporary file)\n");
		write2_s(ptr->tmp);
		write2("\n");
		close(in);
		return 1;
	}

	/* Make it portable. sendfile is Linux-specific. */
	while ((bytes = sendfile(out, in, &off, 4096000)) > 0)
		;
	if (bytes == -1) {
		write2("fail (io error)\n");
		close(in);
		close(out);
		return 1;
	}
	close(in);
	close(out);

	err = chmod(ptr->tmp, ptr->mode);
	if (err) {
		write2("fail (can't set permissions)");
		return 1;
	}
	write2("ok\n");
	return 0;
}

static int prepare_symlink(const struct symlink_a *ptr)
{
	int err = symlink(ptr->source, ptr->tmp);
	if (err)
		write2("fail\n");
	else
		write2("ok\n");
	return err;
}

int xopen_cdb(struct cdb *cdbp, const char *filename, int missing_ok)
{
	int fd;
	int err;

	cdbp->cdb_fd = -1;
	fd = open(filename, O_RDONLY);
	if (fd < 0) {
		if (missing_ok && errno == ENOENT) {
			write2("skip (missing)\n");
			return 0;
		}
		write2("fail\n");
		return 1;
	}

	err = cdb_init(cdbp, fd);
	if (err) {
		write2("fail (corrupted)\n");
		return 1;
	}
	write2("ok\n");
	return 0;
}

/*
 * If target already exists and is not managed by previous generation,
 * create backup of it before overwriting target.
 */
int make_backup(struct cdb *cdbp, const char *target, const char *backup)
{
	int err;

	write2("checking if ");
	write2_s(target);
	write2(" needs backup... ");

	/* In old generation, nothing to do. */
	if (cdb_find(cdbp, target, strlen(target) + 1) == 1) {
		write2("no\n");
		return 0;
	}

	err = access(target, F_OK);
	if (err) {
		write2("no\n");
		return 0;
	}
	write2("yes\n");

	err = access(backup, F_OK);
	if (!err) {
		write2("error: backup file ");
		write2_s(backup);
		write2(" already exists\n");
		return 1;
	}

	write2("creating backup file for ");
	write2_s(target);
	write2("... ");

	err = link(target, backup);
	if (err) {
		write2("fail\n");
		return 1;
	}
	write2("ok\n");
	return 0;
}

int main(int argc, char **argv)
{
	int err, err2 = 0, status;
	int i, j;
	struct cdb ix_new, ix_old;
	unsigned int iter;

	write2("opening index of new generation... ");
	err = xopen_cdb(&ix_new, newcdb_path, 0); /* missing_ok == false */
	if (err)
		return err;

	write2("opening index of previous generation... ");
	err = xopen_cdb(&ix_old, oldcdb_path, 1);
	if (err)
		return err;

	umask(0); /* always succeeds */

	for (i = 0; i != ARRAY_SIZE(parents); ++i) {
		write2("checking parent directory ");
		write2_s(parents[i]);
		write2("... ");

		err = ensure_directory(parents[i]);
		if (err) return 1;
	}

	for (i = 0; i != ARRAY_SIZE(to_copy); ++i) {
		write2("preparing copy for ");
		write2_s(to_copy[i].destination);
		write2("... ");
		err = prepare_copy(to_copy + i);
		if (!err) {
			err2 = make_backup(&ix_old, to_copy[i].destination
			                          , to_copy[i].backup);
		}
		if (err || err2) { /* clean-up */
			for (j = 0; j <= i; ++j) {
				unlink(to_copy[j].backup);
				unlink(to_copy[j].tmp);
			}
			return 1;
		}
	};

	for (i = 0; i != ARRAY_SIZE(to_symlink); ++i) {
		write2("preparing symlink for ");
		write2_s(to_symlink[i].destination);
		write2("... ");
		err = prepare_symlink(to_symlink + i);
		if (!err) {
			err2 = make_backup(&ix_old, to_symlink[i].destination
			                          , to_symlink[i].backup);
		}

		if (err || err2) {
			for (j = 0; j <= i; ++j) {
				unlink(to_symlink[j].backup);
				unlink(to_symlink[j].tmp);
			}
			for (j = 0; j != ARRAY_SIZE(to_copy); ++j) {
				unlink(to_copy[j].backup);
				unlink(to_copy[j].tmp);
			}
			return 1;
		}
	}

	for (i = 0; i != ARRAY_SIZE(to_copy); ++i) {
		const struct copy_a *obj = &to_copy[i];

		write2("renaming copy into ");
		write2_s(obj->destination);
		write2("... ");

		/* Make file immutable (chattr +i) so it can't be
		 * modified by mistake. Linux-only.
		 *
		 * TODO: Check errors properly.
		 */
		(void) immutable_off(obj->destination);
		err = rename(obj->tmp, obj->destination);
		(void) immutable_on(obj->destination);

		if (err) {
			write2("fail\n");
			return 1;
		}
		write2("ok\n");
	}

	for (i = 0; i != ARRAY_SIZE(to_symlink); ++i) {
		const struct symlink_a *obj = &to_symlink[i];

		write2("renaming symlink into ");
		write2_s(obj->destination);
		write2("... ");
		err = rename(obj->tmp, obj->destination);
		if (err) {
			write2("fail\n");
			return 1;
		}
		write2("ok\n");
	}


	for (i = 0; i != ARRAY_SIZE(to_unlink); ++i) {
		const char *target = to_unlink[i];
		write2("unlinking ");
		write2_s(target);
		write2("... ");

		err = unlink(target);
		if (err && (errno == ENOENT)) {
			write2("skip (does not exist)\n");
		} else if (!err) {
			write2("ok (unlinked)\n");
		} else {
			write2("fail (contining anyway)\n");
			status = 1;
		}
	}

	if (ix_old.cdb_fd != -1) {
		cdb_seqinit(&iter, &ix_old);

		while (cdb_seqnext(&iter, &ix_old) > 0) {
			const char *key = cdb_getkey(&ix_old);
			unsigned int keylen = cdb_keylen(&ix_old);
			if (cdb_find(&ix_new, key, keylen) != 0)
				continue;
			write2("purging stale ");
			write2_s(key);
			write2("... ");
			err = unlink(key);
			if (!err)
				write2("ok\n");
			else
				write2("fail (contining anyway)\n");
		}
	}
	cdb_free(&ix_new);
	close(ix_new.cdb_fd);

	if (ix_old.cdb_fd != -1) {
		cdb_free(&ix_old);
		close(ix_old.cdb_fd);
	}

	return status;
}