~torresjrjr/hare

ref: 554a40c042084f92da0d91913466c64c125cd546 hare/hare/module/manifest.ha -rw-r--r-- 9.4 KiB
554a40c0Drew DeVault hare::module: incorporate tags into manifest 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// License: MPL-2.0
// (c) 2021-2022 Alexey Yerin <yyp@disroot.org>
// (c) 2021 Bor Grošelj Simić <bor.groseljsimic@telemach.net>
// (c) 2021 Drew DeVault <sir@cmpwn.com>
// (c) 2021 Eyal Sawady <ecs@d2evs.net>
// (c) 2021 Thomas Bracht Laumann Jespersen <t@laumann.xyz>
use bufio;
use bytes;
use encoding::hex;
use encoding::utf8;
use errors;
use fmt;
use fs;
use hare::ast;
use hare::unparse;
use io;
use os;
use path;
use strconv;
use strings;
use time;
use temp;

// The manifest file format is a series of line-oriented records. Lines starting
// with # are ignored.
//
// - "version" indicates the manifest format version, currently 2.
// - "input" is an input file, and its fields are the file hash, path, inode,
//   and mtime as a Unix timestamp.
// - "module" is a version of a module, and includes the module hash and the set
//   of input hashes which produce it.
// - "tags" is a list of tags associated with a module version

def VERSION: int = 2;

fn getinput(in: []input, hash: []u8) nullable *input = {
	for (let i = 0z; i < len(in); i += 1) {
		if (bytes::equal(in[i].hash, hash)) {
			return &in[i];
		};
	};
	return null;
};

// Loads the module manifest from the build cache for the given ident. The
// return value borrows the ident parameter. If the module is not found, an
// empty manifest is returned.
export fn manifest_load(ctx: *context, ident: ast::ident) (manifest | error) = {
	let manifest = manifest {
		ident = ident,
		inputs = [],
		versions = [],
	};
	let ipath = identpath(manifest.ident);
	defer free(ipath);
	let cachedir = path::join(ctx.cache, ipath);
	defer free(cachedir);

	let mpath = path::join(cachedir, "manifest");
	defer free(mpath);

	let file = match (fs::open(ctx.fs, mpath, fs::flags::RDONLY)) {
	case errors::noentry =>
		return manifest;
	case let err: fs::error =>
		return err;
	case let file: io::handle =>
		yield file;
	};

	let inputs: []input = [], versions: []version = [];

	let buf: [4096]u8 = [0...];
	let file = bufio::buffered(file, buf, []);
	for (true) {
		let line = match (bufio::scanline(&file)) {
		case io::EOF =>
			break;
		case let err: io::error =>
			io::close(&file): void;
			return err;
		case let line: []u8 =>
			yield line;
		};
		defer free(line);

		let line = match (strings::try_fromutf8(line)) {
		case utf8::invalid =>
			// Treat an invalid manifest as empty
			return manifest;
		case let s: str =>
			yield s;
		};

		if (strings::hasprefix(line, "#")) {
			continue;
		};

		let tok = strings::tokenize(line, " ");
		let kind = match (strings::next_token(&tok)) {
		case void =>
			continue;
		case let s: str =>
			yield s;
		};

		switch (kind) {
		case "version" =>
			let ver = match (strings::next_token(&tok)) {
			case void =>
				return manifest;
			case let s: str =>
				yield s;
			};
			match (strconv::stoi(ver)) {
			case let v: int =>
				if (v != VERSION) {
					return manifest;
				};
			case =>
				return manifest;
			};
		case "input" =>
			let hash = match (strings::next_token(&tok)) {
			case void =>
				return manifest;
			case let s: str =>
				yield s;
			}, path = match (strings::next_token(&tok)) {
			case void =>
				return manifest;
			case let s: str =>
				yield s;
			}, inode = match (strings::next_token(&tok)) {
			case void =>
				return manifest;
			case let s: str =>
				yield s;
			}, mtime = match (strings::next_token(&tok)) {
			case void =>
				return manifest;
			case let s: str =>
				yield s;
			};

			let hash = match (hex::decodestr(hash)) {
			case let b: []u8 =>
				yield b;
			case =>
				return manifest;
			};
			let inode = match (strconv::stoz(inode)) {
			case let z: size =>
				yield z;
			case =>
				return manifest;
			};
			let mtime = match (strconv::stoi64(mtime)) {
			case let i: i64 =>
				yield time::from_unix(i);
			case =>
				return manifest;
			};

			let parsed = parsename(path);
			let ftype = match (type_for_ext(path)) {
			case void =>
				return manifest;
			case let ft: filetype =>
				yield ft;
			};

			append(inputs, input {
				hash = hash,
				path = strings::dup(path),
				ft = ftype,
				stat = fs::filestat {
					mask = fs::stat_mask::MTIME | fs::stat_mask::INODE,
					mtime = mtime,
					inode = inode,
					...
				},
				basename = strings::dup(parsed.0),
				tags = parsed.2,
			});
		case "module" =>
			let modhash = match (strings::next_token(&tok)) {
			case void =>
				return manifest;
			case let s: str =>
				yield s;
			};
			let modhash = match (hex::decodestr(modhash)) {
			case let b: []u8 =>
				yield b;
			case =>
				return manifest;
			};

			let minputs: []input = [];
			for (true) {
				let hash = match (strings::next_token(&tok)) {
				case void =>
					break;
				case let s: str =>
					yield s;
				};
				let hash = match (hex::decodestr(hash)) {
				case let b: []u8 =>
					yield b;
				case =>
					return manifest;
				};
				defer free(hash);

				let input = match (getinput(inputs, hash)) {
				case null =>
					return manifest;
				case let i: *input =>
					yield i;
				};
				append(minputs, *input);
			};

			append(versions, version {
				hash = modhash,
				inputs = minputs,
				...
			});
		case "tags" =>
			let modhash = match (strings::next_token(&tok)) {
			case void =>
				return manifest;
			case let s: str =>
				yield s;
			};
			let modhash = match (hex::decodestr(modhash)) {
			case let b: []u8 =>
				yield b;
			case =>
				return manifest;
			};

			const tags = strings::remaining_tokens(&tok);
			const tags = parsetags(tags) as []tag;
			let found = false;
			for (let i = 0z; i < len(versions); i += 1) {
				if (bytes::equal(versions[i].hash, modhash)) {
					versions[i].tags = tags;
					found = true;
					break;
				};
			};
			// Implementation detail: tags always follows module
			// directive for a given module version
			assert(found);
		case =>
			return manifest;
		};

		// Check for extra tokens
		match (strings::next_token(&tok)) {
		case void => void;
		case str =>
			return manifest;
		};
	};

	io::close(&file)?;
	manifest.inputs = inputs;
	manifest.versions = versions;
	return manifest;
};

// Returns true if the desired module version is present and current in this
// manifest.
export fn current(man: *manifest, ver: *version) bool = {
	// TODO: This is kind of dumb. What we really need to do is:
	// 1. Update scan to avoid hashing the file if a manifest is present,
	//    and indicate that the hash is cached somewhere in the type. Get an
	//    up-to-date stat.
	// 2. In [current], test if the inode and mtime are equal to the
	//    manifest version. If so, presume the file is up-to-date. If not,
	//    check the hash and update the manifest to the new inode/mtime if
	//    the hash matches. If not, the module is not current; rebuild.
	let cached: nullable *version = null;
	for (let i = 0z; i < len(man.versions); i += 1) {
		if (bytes::equal(man.versions[i].hash, ver.hash)) {
			cached = &man.versions[i];
			break;
		};
	};
	let cached = match (cached) {
	case null =>
		return false;
	case let v: *version =>
		yield v;
	};

	assert(len(cached.inputs) == len(ver.inputs));
	for (let i = 0z; i < len(cached.inputs); i += 1) {
		let a = cached.inputs[i], b = cached.inputs[i];
		assert(a.path == b.path);
		let ast = a.stat, bst = b.stat;
		if (ast.inode != bst.inode
				|| time::compare(ast.mtime, bst.mtime) != 0) {
			return false;
		};
	};
	return true;
};

// Writes a module manifest to the build cache.
export fn manifest_write(ctx: *context, man: *manifest) (void | error) = {
	let ipath = identpath(man.ident);
	defer free(ipath);
	let cachedir = path::join(ctx.cache, ipath);
	defer free(cachedir);

	let mpath = path::join(cachedir, "manifest");
	defer free(mpath);

	let (file, name) = temp::named(ctx.fs, cachedir, io::mode::WRITE, 0o644)?;
	defer {
		fs::remove(ctx.fs, name): void;
		io::close(file)!;
	};

	let ident = unparse::identstr(man.ident);
	defer free(ident);
	fmt::fprintfln(file, "# {}", ident)?;
	fmt::fprintln(file, "# This file is an internal Hare implementation detail.")?;
	fmt::fprintln(file, "# The format is not stable.")?;
	fmt::fprintfln(file, "version {}", VERSION)?;
	for (let i = 0z; i < len(man.inputs); i += 1) {
		const input = man.inputs[i];
		let hash = hex::encodestr(input.hash);
		defer free(hash);

		const want = fs::stat_mask::INODE | fs::stat_mask::MTIME;
		assert(input.stat.mask & want == want);
		fmt::fprintfln(file, "input {} {} {} {}",
			hash, input.path, input.stat.inode,
			time::unix(input.stat.mtime))?;
	};

	for (let i = 0z; i < len(man.versions); i += 1) {
		const ver = man.versions[i];
		let hash = hex::encodestr(ver.hash);
		defer free(hash);

		fmt::fprintf(file, "module {}", hash)?;

		for (let j = 0z; j < len(ver.inputs); j += 1) {
			let hash = hex::encodestr(ver.inputs[i].hash);
			defer free(hash);

			fmt::fprintf(file, " {}", hash)?;
		};

		fmt::fprintln(file)?;

		fmt::fprintf(file, "tags {} ", hash)?;
		for (let i = 0z; i < len(ver.tags); i += 1) {
			const tag = &ver.tags[i];
			fmt::fprintf(file, "{}{}",
				switch (tag.mode) {
				case tag_mode::INCLUSIVE =>
					yield "+";
				case tag_mode::EXCLUSIVE =>
					yield "-";
				},
				tag.name)?;
		};
		fmt::fprintln(file)!;
	};

	fs::move(ctx.fs, name, mpath)?;
};

fn input_finish(in: *input) void = {
	free(in.hash);
	free(in.path);
	free(in.basename);
	tags_free(in.tags);
};

// Frees resources associated with this manifest.
export fn manifest_finish(m: *manifest) void = {
	for (let i = 0z; i < len(m.inputs); i += 1) {
		input_finish(&m.inputs[i]);
	};

	for (let i = 0z; i < len(m.versions); i += 1) {
		free(m.versions[i].inputs);
		tags_free(m.versions[i].tags);
	};
};