aboutsummaryrefslogtreecommitdiff
blob: 6ec4491573b16fc59c20569013279397ff02a45b (plain)
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
/*
 * Copyright 2005-2024 Gentoo Foundation
 * Distributed under the terms of the GNU General Public License v2
 *
 * Copyright 2005-2008 Ned Ludd        - <solar@gentoo.org>
 * Copyright 2005-2014 Mike Frysinger  - <vapier@gentoo.org>
 * Copyright 2018-     Fabian Groffen  - <grobian@gentoo.org>
 */

#include "main.h"

#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include "contents.h"

/*
 * Parse a line of CONTENTS file and provide access to the individual fields
 */
contents_entry *
contents_parse_line_len(char *line, size_t len)
{
	static contents_entry e;
	char *p;

	if (len == 0 || line == NULL || *line == '\0' || *line == '\n')
		return NULL;

	/* chop trailing newline */
	p = &line[len - 1];
	if (*p == '\n') {
		*p = '\0';
		len--;
	}

	if (len <= 4)  /* minimal: "dir /" */
		return NULL;

	memset(&e, 0x00, sizeof(e));
	e._data = line;

	if (!strncmp(e._data, "obj ", 4))
		e.type = CONTENTS_OBJ;
	else if (!strncmp(e._data, "dir ", 4))
		e.type = CONTENTS_DIR;
	else if (!strncmp(e._data, "sym ", 4))
		e.type = CONTENTS_SYM;
	else
		return NULL;

	e.name = e._data + 4;
	len   -= 4;

	switch (e.type) {
		/* dir /bin */
		case CONTENTS_DIR:
			break;

		/* obj /bin/bash 62ed51c8b23866777552643ec57614b0 1120707577 */
		case CONTENTS_OBJ:
			for (p = &e.name[len - 1]; p >= e.name; p--) {
				if (*p == ' ') {
					if (e.mtime_str == NULL)
						e.mtime_str = p + 1;
					else if (e.digest == NULL)
						e.digest = p + 1;
					*p = '\0';

					if (e.digest != NULL)
						break;
				}
			}
			break;

		/* sym /bin/sh -> bash 1120707577 */
		case CONTENTS_SYM:
			for (p = &e.name[len - 1]; p >= e.name; p--) {
				if (*p == ' ') {
					if (e.mtime_str == NULL) {
						e.mtime_str = p + 1;
					} else if (e.sym_target == NULL) {
						if (strncmp(p, " -> ", sizeof(" -> ") - 1) == 0)
							e.sym_target = p + sizeof(" -> ") - 1;
						else
							continue;
					}
					*p = '\0';

					if (e.sym_target != NULL)
						break;
				}
			}
			break;
	}

	if (e.mtime_str) {
		e.mtime = strtol(e.mtime_str, NULL, 10);
		if (e.mtime == LONG_MAX) {
			e.mtime = 0;
			e.mtime_str = NULL;
		}
	}

	return &e;
}