aboutsummaryrefslogtreecommitdiff
path: root/tools/tools/nanobsd/mtree-dedup.awk
blob: 5390540ff8282cd21500bace2a7cebb21c6958d2 (plain) (blame)
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
#!/usr/bin/awk -f

#
# Copyright (c) 2015 M. Warner Losh.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#

#
# Takes a meta-log created by installworld and friends, plus
# additions from NanoBSD to augment its build to communicate when
# files move around after installworld / installkernel phase of
# NanoBSD.
#
# All mtree lines from the metafile have a path, followed by
# a number of keywords.
#
# This script recognizes the following new keywords
#
# unlink[=x]	remove the path from the output.
# copy_from=x	create new entry for path copied from
#		the keywords from x.
# move_from=x	create new entry for path copied from
#		the keywords from x. Remove path from
#		the output.
#
# In addition, when path matches a previous entry, the
# new entry and previous entry are merged.
#
# Special note: when uid and uname are both present,
# uid is ignored. Ditto gid/gname.
#
# Also, the paths above have to match exactly, so X
# should start with "./".
#

function die(str)
{
	print str > "/dev/stderr";
	exit 1;
}

function kv(str)
{
	if (split(str, xxx, "=") == 2) {
		kv_key = xxx[1];
		kv_value = xxx[2];
	} else {
		kv_key = str;
		kv_value = nv;
	}
}

# Output the mtree for path based on the kvs.
function mtree_from_kvs(path, kvs)
{
	lv = path " ";
	for (k in kvs) {
		if (kvs[k] == nv)
			lv = lv k " ";
		else
			lv = lv k "=" kvs[k] " ";
	}
	return lv;
}

# Parse the mtree line into path + KVs. Use a sentinal value
# for a bare keyword, which is extremely unlikely to be used
# for real.
function line2kv(kvs, str)
{
	delete kvs;

	n = split(str, yyy, " ");
	for (i = 2; i <= n; i++) {
		s = yyy[i];
		if (split(s, xxx, "=") == 2)
			kvs[xxx[1]] = xxx[2];
		else
			kvs[s] = nv;
	}
}


# old += new
function merge_kvs(old, new)
{
	for (k in new) {
		# uname / uid -- last one wins.
		if (k == "uid" && "uname" in old)
			delete old["uname"]
		if (k == "uname" && "uid" in old)
			delete old["uid"];
		# gname / gid -- last one wins.
		if (k == "gid" && "gname" in old)
			delete old["gname"]
		if (k == "gname" && "gid" in old)
			delete old["gid"];
		# Otherwise newest value wins
		old[k] = new[k];
	}
}

# Process the line we've read in, per the comments below
function process_line(path, new)
{
	# Clear kvs
	line2kv(new_kvs, new);

	if ("unlink" in new_kvs) {
		# A file removed
		# Sanity check to see if tree[path] exists? 
		# Makes sure when foo/bar/baz exists and foo/bar
		# unlinked, baz is gone (for all baz).
		if (path !~ "^\./")
			die("bad path in : " new);
		delete tree[path];		# unlink
		return;
	# } else if (new_kvs["append_from"]) { # not implemented
	} else if ("copy_from" in new_kvs) {
		# A file copied from another location, preserve its
		# attribute for new file.
		# Also merge any new attributes from this line.
		from = new_kvs["copy_from"];
		if (from !~ "^\./")
			die("bad path in : " new);
		delete new_kvs["copy_from"];
		line2kv(old_kvs, tree[from]);	# old_kvs = kv's in entry
		merge_kvs(old_kvs, new_kvs);	# old_kvs += new_kvs
		tree[path] = mtree_from_kvs(path, old_kvs);
	} else if ("move_from" in new_kvs) {
		# A file moved from another location, preserve its
		# attribute for new file, and scrag old location
		# Also merge any new attributes from this line.
		from = new_kvs["move_from"];
		if (from !~ "^\./")
			die("bad path in : " new);
		delete new_kvs["move_from"];
		line2kv(old_kvs, tree[from]);	# old_kvs = kv's in entry
		merge_kvs(old_kvs, new_kvs);	# old_kvs += new_kvs
		tree[path] = mtree_from_kvs(path, old_kvs);
		delete tree[from];		# unlink
	} else if (tree[path]) {	# Update existing entry with new line
		line2kv(old_kvs, tree[path]);	# old_kvs = kv's in entry
		merge_kvs(old_kvs, new_kvs);	# old_kvs += new_kvs
		tree[path] = mtree_from_kvs(path, old_kvs);
	} else {			# Add entry plus defaults
		delete old_kvs;
		merge_kvs(old_kvs, defaults);
		merge_kvs(old_kvs, new_kvs);
		tree[path] = mtree_from_kvs(path, old_kvs);
	}
}

BEGIN {
	nv = "___NO__VALUE___";

	while ((getline < "/dev/stdin") > 0) {
		if ($1 ~ "^#")
			continue;
		if ($1 == "/set") {
			for (i = 2; i <= NF; i++) {
				kv($i);
				defaults[kv_key] = kv_value;
			}
		} else if ($1 == "/unset") {
			for (i = 2; i <= NF; i++) {
				kv($i);
				delete defaults[kv_key];
			}
		} else
			process_line($1, $0);
	}

	# Print the last set of defaults. This will carry
	# over, I think, to makefs' defaults
	print mtree_from_kvs("/set", defaults)
	for (x in tree)
		print tree[x];
}