-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathpm_apply
247 lines (198 loc) · 5.94 KB
/
pm_apply
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
#!/bin/bash
set -o pipefail
#
# Some constants
#
# Root
PM_VAR_DIR="/tmp/patchmanager3"
PATCH_ROOT_DIR="/usr/share/patchmanager/patches"
# Paths / Files
PARAMETER="$1"
PATCH_DIR="$PATCH_ROOT_DIR/$1"
PM_LOG_FILE="$PM_VAR_DIR/patchmanager.log"
PM_PATCH_BACKUP_ROOT_DIR="$PM_VAR_DIR/patches"
PM_PATCH_BACKUP_DIR="$PM_PATCH_BACKUP_ROOT_DIR/$1"
SYS_BITNESS=$(/usr/bin/getconf LONG_BIT)
# Constants
PATCH_NAME="unified_diff.patch"
PATCH_PATH="$PATCH_DIR/$PATCH_NAME"
PATCH_EDITED_NAME="unified_diff_${SYS_BITNESS}bit.patch"
# list of candidate paths to attempt 32-/64-bit library path correction
MANGLE_CANDIDATES=""
if [ -z "$DISABLE_MANGLING" ] && [ -r "/etc/patchmanager/manglelist.conf" ] ; then
source /etc/patchmanager/manglelist.conf
fi
ROOT_DIR="/tmp/patchmanager"
# Applications
PATCH_EXEC="/usr/bin/patch"
#
# Here starts the part where all the magic happens
#
#
# Helper functions that do all the needed heavy work
#
log() {
echo "$@" | tee -a "$PM_LOG_FILE"
}
failure() {
log
log "*** FAILED ***"
log
exit 1
}
success() {
log
log "*** SUCCESS ***"
log
exit 0
}
test_if_applied() {
if [ -f "$PATCH_PATH" ]; then
log
log "----------------------------------"
log "Test if already applied patch"
log "----------------------------------"
log
$PATCH_EXEC -R -p 1 -d "$ROOT_DIR" --dry-run < "$PATCH_PATH" 2>&1 | tee -a "$PM_LOG_FILE"
if [ $? -eq 0 ]; then
success
fi
fi
}
# For details see issue #71: https://github.com/sailfishos-patches/patchmanager/issues/71
mangle_libpath() {
if [ -f "$PATCH_PATH" ]; then
log
log "----------------------------------"
[ $SYS_BITNESS -eq 32 ] && log "Checking paths for 32-bit --> 64-bit conversion"
[ $SYS_BITNESS -eq 64 ] && log "Checking paths for 64-bit --> 32-bit conversion"
log "----------------------------------"
log
candidates="$MANGLE_CANDIDATES"
if [ $SYS_BITNESS -eq 32 ]; then
# first, convert the candidate list
# variable expansion ${foo/lib/lib64} would work on bash, but not POSIX sh or busybox's ash
candidates=$(printf '%s' "$MANGLE_CANDIDATES" | sed 's@/usr/lib/@/usr/lib64/@g')
fi
log "Mangle candidates: $candidates"
log
found=0
sedcmd=""
# look for lines to convert, if some are found add pattern to sed scriptlet
for p in $candidates; do
cand_lines=$(grep -c "^+++ [^/]*$p" "$PATCH_PATH")
if [ $cand_lines -eq 0 ]; then
continue # nothing found, try next
fi
found=$(( $found + $cand_lines ))
log "Converting library path reference $p $cand_lines times"
# prepare the path replacement pattern
pr=""
if [ $SYS_BITNESS -eq 32 ]; then
pr=$(printf '%s' "$p" | sed 's@/usr/lib64/@/usr/lib/@')
elif [ $SYS_BITNESS -eq 64 ]; then
pr=$(printf '%s' "$p" | sed 's@/usr/lib/@/usr/lib64/@')
else
failure
fi
# append at the front so ';' can be easily used for separation
sedcmd="s@^+++ \([^/]*\)$p@+++ \1$pr@g;s@^--- \([^/]*\)$p@--- \1$pr@g;$sedcmd"
done
if [ $found -eq 0 ]; then
log
log "OK, found nothing to convert."
log
else
mkdir -p "$PM_PATCH_BACKUP_DIR"
patch_edited_path="$PM_PATCH_BACKUP_DIR"/"$PATCH_EDITED_NAME"
# create mangled patch file and add a note to it
printf '#\n# Patch converted to %sbit library paths from its original by Patchmanager > 3.1\n# Date: %s\n#\n' $SYS_BITNESS $(date -Iseconds) \
| cat - "$PATCH_PATH" | sed "$sedcmd" > "$patch_edited_path" # patch the Patch
if [ $? -ne 0 ]; then
failure
fi
log
log "OK, converted $found library path references and created: $patch_edited_path"
log
# set the patch path to the new one:
PATCH_PATH="$patch_edited_path"
fi
fi
}
verify_text_patch() {
if [ -f "$PATCH_PATH" ]; then
log
log "----------------------------------"
log "Dry running patch file"
log "----------------------------------"
log
$PATCH_EXEC -p 1 -d "$ROOT_DIR" --dry-run < "$PATCH_PATH" 2>&1 | tee -a "$PM_LOG_FILE"
if [ $? -ne 0 ]; then
failure
fi
fi
}
install_text_patch() {
if [ -f "$PATCH_PATH" ]; then
log
log "----------------------------------"
log "Applying patch file"
log "----------------------------------"
log
$PATCH_EXEC -p 1 -d "$ROOT_DIR" --no-backup-if-mismatch < "$PATCH_PATH" 2>&1 | tee -a "$PM_LOG_FILE"
fi
}
create_backup_patch() {
log
log "----------------------------------"
log "Creating backup of patch file"
log "----------------------------------"
log
mkdir -p "$PM_PATCH_BACKUP_DIR" > /dev/null 2>&1
# Backup the original patch file, NOT $PATCH_PATH, which might be altered by 32-/64-bit mangling in line 143.
# If mangling was performed successfully, it already wrote its patched $PATCH_EDITED_NAME to $PM_PATCH_BACKUP_DIR in line 134.
cp -f "$PATCH_DIR/$PATCH_NAME" "$PM_PATCH_BACKUP_DIR" > /dev/null 2>&1
log "Created backup: $PM_PATCH_BACKUP_DIR"
log
}
#
# Bunch of safety checks and required initializations
#
mkdir -p "$PM_VAR_DIR" > /dev/null 2>&1
if [ ! -f "$PM_LOG_FILE" ]; then
log "*** Patch-log created by pm_apply script $(date -Iseconds) ***"
fi
log
log "----------------------------------"
log "pm_apply $(date -Iseconds)"
log "----------------------------------"
log
if [ -z "$PARAMETER" ]; then
log "ERROR: A parameter must be provided for the pm_apply script!"
failure
fi
if [ ! -d "$PATCH_DIR" ]; then
log "ERROR: $PATCH_DIR is not a directory or does not exist!"
failure
fi
log "$(basename "$PATCH_DIR")"
log
if [ ! -x "$PATCH_EXEC" ]; then
log "ERROR: Cannot use patch executable ${PATCH_EXEC}!"
failure
fi
log "Using patch file: $PATCH_PATH"
if [ ! -f "$PATCH_PATH" ]; then
log "ERROR: Cannot find patch file!"
failure
fi
#
# The main function that controls all the magic stuff
#
[ -n "$MANGLE_CANDIDATES" ] && mangle_libpath
test_if_applied
verify_text_patch
install_text_patch
create_backup_patch
success
exit 0