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
|
#!/bin/sh
#
# Copyright (c) 2013 Codethink Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License Version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
set -eu
: ${mounting_script="$(dirname "$0")/../libexec/mount-system-versions-dir"}
: ${unmount="umount"}
usage() {
echo "Usage: $(basename $0) merge NEW_VERSION_LABEL" >&2
echo " $(basename $0) sync CANONICAL_VERSION_LABEL" >&2
exit 1
}
die () {
echo $@ >&2
exit 1
}
file_type() {
mode=$(printf '%o\n' 0x$(stat -c %f "$1"))
case "$mode" in
140???) echo "socket" ;;
120???) echo "symlink" ;;
100???) echo "regular" ;;
60???) echo "block" ;;
40???) echo "directory" ;;
20???) echo "char" ;;
10???) echo "fifo" ;;
*) echo "Unknown file type $1: $mode" 1>&2; exit 1 ;;
esac
}
check_same_type() {
type1="$(file_type $1)"
type2="$(file_type $2)"
if [ "$type1" != "$type2" ]; then
die "ERROR: found two different types for '$3':" \
"$type1 and $type2"
fi
}
merge() {
local vp_dir="$1" # version being processed
local v1_dir="$2" # factory version
local vu_dir="$3" # user modified version
local v2_dir="$4" # unmodified new deployed version
local vt_dir="$5" # target version where the result of the
# merge should be placed
# use `find "$vp_dir/"*` instead of `find "$vp_dir"` because
# the last one also gives $vp_dir in the list of directories
find "$vp_dir/"* | while read f; do
# echo "Processing $f"
# strip first component from file name
local stripped_filename=${f#$vp_dir/}
local vp="$vp_dir/$stripped_filename"
local v1="$v1_dir/$stripped_filename"
local vu="$vu_dir/$stripped_filename"
local v2="$v2_dir/$stripped_filename"
local vt="$vt_dir/$stripped_filename"
if [ ! -e "$vt" ]; then
if [ -e "$v1" -a -e "$vu" ]; then
check_same_type "$v1" "$vu" "$stripped_filename"
fi
if [ -e "$vu" -a -e "$v2" ]; then
check_same_type "$vu" "$v2" "$stripped_filename"
fi
if [ -e "$v1" -a -e "$v2" ]; then
check_same_type "$v1" "$v2" "$stripped_filename"
fi
if [ -d "$vp" -a ! -h "$vp" ]; then
mkdir "$vt"
elif [ -h "$vp" ]; then
# chose a symbolic link in this order
# of preference: Vuser, V2, V1
if [ -h "$vu" ]; then
cp -a "$vu" "$vt"
elif [ -h "$v2" ]; then
cp -a "$v2" "$vt"
else
cp -a "$v1" "$vt"
fi
elif [ -f "$vp" ]; then
merge_regular_file "$v1" "$vu" "$v2" "$vt"
else
die "ERROR: unexpected error"
fi
fi
done
}
merge_regular_file() {
local v1="$1"
local vu="$2"
local v2="$3"
local vt="$4"
# Reference table for merging a regular file
#
# V1 Vuser V2 action
# ------------------------------------------
# none none none inconceivable!
# exists none none use V1
# none exists none use Vuser
# none none exists use V2
# exists none exists use V2
# exists exists none use V2
# none exists exists diff V2 Vuser applied to V2
# exists exists exists diff V1 Vuser applied to V2
v1_exists=$(test -f "$v1" && echo exists || echo none)
vu_exists=$(test -f "$vu" && echo exists || echo none)
v2_exists=$(test -f "$v2" && echo exists || echo none)
case "$v1_exists $vu_exists $v2_exists" in
'exists none none')
cp -a "$v1" "$vt"
;;
'none exists none')
cp -a "$vu" "$vt"
;;
'none none exists')
cp -a "$v2" "$vt"
;;
'exists none exists')
cp -a "$v2" "$vt"
;;
'exists exists none')
cp -a "$vu" "$vt"
;;
'none exists exists')
if ! (diff "$v2" "$vu" | patch "$v2" -t -o "$vt"); then
cp -a "$v2" "$vt" # merge failed, use v2
fi
;;
'exists exists exists')
if ! (diff "$v1" "$vu" | patch "$v2" -t -o "$vt"); then
cp -a "$v2" "$vt" # merge failed, use v2
fi
;;
*)
die "ERROR: unexpected error"
;;
esac
}
if [ "$#" = 0 ]; then
usage "$0"
fi
if [ "$1" = "merge" ]; then
if [ "$#" != 2 ]; then
usage "$0"
fi
new_version="$2"
mounting_point=$(mktemp -d)
"$mounting_script" "$mounting_point"
if [ ! -d "$mounting_point/systems/$new_version" ]; then
"$unmount" "$mounting_point"
die "Error: version not found - '$new_version'"
fi
v1_dir="$mounting_point/systems/default/orig/etc"
vu_dir="$mounting_point/systems/default/run/etc"
v2_dir="$mounting_point/systems/$new_version/run/etc"
vt_dir="$mounting_point/systems/$new_version/run/etc.new"
mkdir "$vt_dir"
trap 'rm -rvf "$vt_dir"; "$unmount" "$mounting_point"' EXIT
# For every pathname in V1, Vuser, or V2
merge "$v1_dir" "$v1_dir" "$vu_dir" "$v2_dir" "$vt_dir"
merge "$vu_dir" "$v1_dir" "$vu_dir" "$v2_dir" "$vt_dir"
merge "$v2_dir" "$v1_dir" "$vu_dir" "$v2_dir" "$vt_dir"
rm -rf "$v2_dir"
mv "$vt_dir" "$v2_dir"
elif [ "$1" = "sync" ]; then
canonical_version="$2"
mounting_point=$(mktemp -d)
"$mounting_script" "$mounting_point"
trap '"$unmount" "$mounting_point"' EXIT
if [ ! -d "$mounting_point/systems/$canonical_version" ]; then
die "Error: version not found - '$canonical_version'"
fi
for version_dir in "$mounting_point/systems/"*; do
version="$(basename "$version_dir")"
if [ -d "$version_dir" -a ! -h "$version_dir" \
-a $version != $canonical_version ]; then
cp -a "$mounting_point/systems/$canonical_version/run/etc" \
"$version_dir/run/etc.new"
mv "$version_dir/run/etc" "$version_dir/run/etc.old"
mv "$version_dir/run/etc.new" "$version_dir/run/etc"
rm -rf "$version_dir/run/etc.old"
fi
done
else
usage "$0"
fi
|