Skip to content

Commit e80cfa5

Browse files
authored
pm_apply: Do convert multiple libpath references etc.
[pm_apply] Do convert multiple libpath references [pm_apply]Simplify code [pm_apply] Let log output be more expressive and consistent [pm_apply] Enhance checks&balances and debug output pm_apply: improve regexp (#237) [pm_apply] (Re-)move misplaced log output and counter initialisation (#239) [pm_apply] De-indent the whole function mangle_libpath
2 parents 1d4ab44 + a0b7089 commit e80cfa5

File tree

1 file changed

+68
-75
lines changed

1 file changed

+68
-75
lines changed

src/tools/pm_apply

Lines changed: 68 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ PM_VAR_DIR="/tmp/patchmanager3"
99
PATCH_ROOT_DIR="/usr/share/patchmanager/patches"
1010

1111
# Paths / Files
12+
PARAMETER="$1"
1213
PATCH_DIR="$PATCH_ROOT_DIR/$1"
1314

1415
PM_LOG_FILE="$PM_VAR_DIR/patchmanager.log"
@@ -45,11 +46,6 @@ log() {
4546
echo "$@" | tee -a "$PM_LOG_FILE"
4647
}
4748

48-
log "Mangle candidates"
49-
log $MANGLE_CANDIDATES
50-
log "Disable mangling"
51-
log $DISABLE_MANGLING
52-
5349
failure() {
5450
log
5551
log "*** FAILED ***"
@@ -84,65 +80,65 @@ test_if_applied() {
8480

8581
# see issue #71: https://github.com/sailfishos-patches/patchmanager/issues/71
8682
mangle_libpath() {
87-
if [ -f "$PATCH_PATH" ]; then
88-
log
89-
log "----------------------------------"
90-
[ $SYS_BITNESS -eq 32 ] && log "Checking paths for 32->64bit conversion"
91-
[ $SYS_BITNESS -eq 64 ] && log "Checking paths for 64->32bit conversion"
92-
log "----------------------------------"
93-
log
83+
if [ -f "$PATCH_PATH" ]; then
84+
log
85+
log "----------------------------------"
86+
[ $SYS_BITNESS -eq 32 ] && log "Checking paths for 32->64bit conversion"
87+
[ $SYS_BITNESS -eq 64 ] && log "Checking paths for 64->32bit conversion"
88+
log "----------------------------------"
89+
log
9490

95-
found=0
96-
candidates="$MANGLE_CANDIDATES"
97-
if [ $SYS_BITNESS -eq 32 ]; then
98-
# first, convert the candidate list
99-
# variable expansion ${foo/lib/lib64} would work on bash, but not POSIX/ash/busybox
100-
candidates=$(printf '%s' "$MANGLE_CANDIDATES" | sed 's@/usr/lib/@/usr/lib64/@g' )
101-
fi
91+
candidates="$MANGLE_CANDIDATES"
92+
if [ $SYS_BITNESS -eq 32 ]; then
93+
# first, convert the candidate list
94+
# variable expansion ${foo/lib/lib64} would work on bash, but not POSIX sh or busybox's ash
95+
candidates=$(printf '%s' "$MANGLE_CANDIDATES" | sed 's@/usr/lib/@/usr/lib64/@g')
96+
fi
97+
log "Mangle candidates: $candidates"
98+
log
10299

103-
for p in $candidates; do
104-
totl_lines=$(grep -c "^+++" "$PATCH_PATH")
105-
cand_lines=$(grep -c "^+++ $p" "$PATCH_PATH")
106-
if [ $cand_lines -eq 0 ]; then
107-
continue # nothing found, try next
108-
else
109-
found=$(( $found + $cand_lines ))
110-
if [ $totl_lines -ne $cand_lines ]; then
111-
# if there are several references in the patch file our mangling might do too much and cause the patch to fail.
112-
log "WARNING: mixed patch, conversion might not work"
113-
fi
114-
fi
115-
log "found: ${p}, replacing libpath references"
116-
117-
# prepare the replacement pattern
118-
pr=""
119-
if [ $SYS_BITNESS -eq 32 ]; then
120-
pr=$(printf '%s' "$p" | sed 's@/usr/lib64/@/usr/lib/@')
121-
elif [ $SYS_BITNESS -eq 64 ]; then
122-
pr=$(printf '%s' "$p" | sed 's@/usr/lib/@/usr/lib64/@')
100+
found=0
101+
for p in $candidates; do
102+
totl_lines=$(grep -c "^\+\+\+" "$PATCH_PATH")
103+
cand_lines=$(grep -c "^\+\+\+ [^/]*$p" "$PATCH_PATH")
104+
if [ $cand_lines -eq 0 ]; then
105+
continue # nothing found, try next
106+
else
107+
found=$(( $found + $cand_lines ))
108+
if [ $totl_lines -ne $cand_lines ]; then
109+
# if there are several references in the patch file our mangling might do too much and cause the patch to fail.
110+
log "WARNING: mixed patch, conversion might not work"
123111
fi
124-
# doit
112+
fi
113+
log "found: ${p}, replacing libpath references"
114+
# prepare the replacement pattern
115+
pr=""
116+
if [ $SYS_BITNESS -eq 32 ]; then
117+
pr=$(printf '%s' "$p" | sed 's@/usr/lib64/@/usr/lib/@')
118+
elif [ $SYS_BITNESS -eq 64 ]; then
119+
pr=$(printf '%s' "$p" | sed 's@/usr/lib/@/usr/lib64/@')
120+
fi
121+
# doit
122+
if [ $found -eq $cand_lines ]; then # first run in loop
125123
mkdir -p "$PM_PATCH_BACKUP_DIR"
126124
patch_edited_path="$PM_PATCH_BACKUP_DIR"/"$PATCH_EDITED_NAME"
127-
printf '#\n# Patch converted to %sbit library paths from its original by Patchmanager > 3.1\n# Date: %s\n#\n' $SYS_BITNESS $(date -Iseconds) > "$patch_edited_path"
128-
sed "s@^+++ $p@+++ $pr@;s@^--- $p@--- $pr@" "$PATCH_PATH" >> "$patch_edited_path"
129-
if [ $? -ne 0 ]; then
130-
failure
131-
fi
132-
break
133-
done
134-
if [ $found -eq 0 ]; then
135-
log
136-
log "OK, found nothing to convert."
137-
log
138-
else
139-
log
140-
log "OK, replaced $found libpath references and created: $patch_edited_path"
141-
log
142-
# set the patch to apply to the new one:
143-
PATCH_PATH="$patch_edited_path"
125+
printf '#\n# Patch converted to %sbit library paths from its original by Patchmanager > 3.1\n# Date: %s\n#\n' $SYS_BITNESS $(date -Iseconds) \
126+
| cat - "$PATCH_PATH" > "$patch_edited_path"
144127
fi
128+
sed -i "s@^\+\+\+ \([^/]*\)$p@+++ \1$pr@;s@^--- \([^/]*\)$p@--- \1$pr@" "$patch_edited_path" || failure || true # patch the Patch
129+
done
130+
if [ $found -eq 0 ]; then
131+
log
132+
log "OK, found nothing to convert."
133+
log
134+
else
135+
log
136+
log "OK, replaced $found libpath references and created: $patch_edited_path"
137+
log
138+
# set the patch to apply to the new one:
139+
PATCH_PATH="$patch_edited_path"
145140
fi
141+
fi
146142
}
147143

148144
verify_text_patch() {
@@ -182,8 +178,8 @@ create_backup_patch() {
182178

183179
mkdir -p "$PM_PATCH_BACKUP_DIR" > /dev/null 2>&1
184180
cp -f "$PATCH_DIR/$PATCH_NAME" "$PM_PATCH_BACKUP_DIR" > /dev/null 2>&1
185-
log "Created backup"
186-
log " backup: $PM_PATCH_BACKUP_DIR"
181+
log "Created backup: $PM_PATCH_BACKUP_DIR"
182+
log
187183
}
188184

189185
#
@@ -193,40 +189,37 @@ create_backup_patch() {
193189
mkdir -p "$PM_VAR_DIR" > /dev/null 2>&1
194190

195191
if [ ! -f "$PM_LOG_FILE" ]; then
196-
log "*** Patch Log Created by Apply $(date) ***"
192+
log "*** Patch-log created by pm_apply script $(date -Iseconds) ***"
197193
fi
198194

199195
log
200196
log "----------------------------------"
201-
log "PM APPLY $(date)"
197+
log "pm_apply $(date -Iseconds)"
202198
log "----------------------------------"
203199
log
204200

205-
log "$(basename "$PATCH_DIR")"
201+
if [ -z "$PARAMETER" ]; then
202+
log "ERROR: A parameter must be provided for applying!"
203+
failure
204+
fi
206205

207-
if [ -f "$PATCH_PATH" ]; then
208-
log " contains text patch"
206+
if [ ! -d "$PATCH_DIR" ]; then
207+
log "ERROR: $PATCH_DIR is not a directory or does not exist!"
208+
failure
209209
fi
210210

211+
log "$(basename "$PATCH_DIR")"
211212
log
212213

213214
if [ ! -x "$PATCH_EXEC" ]; then
214-
log "ERROR: Cannot find $PATCH_EXEC"
215-
failure
216-
fi
217-
218-
if [ -z "$PATCH_DIR" ]; then
219-
log "ERROR: PATCH_DIR must be given for installing"
215+
log "ERROR: Cannot use patch executable ${PATCH_EXEC}!"
220216
failure
221217
fi
222218

223-
if [ ! -d "$PATCH_DIR" ]; then
224-
log "ERROR: $PATCH_DIR is not a directory or does not exist!"
225-
failure
226-
fi
219+
log "Using patch file: $PATCH_PATH"
227220

228221
if [ ! -f "$PATCH_PATH" ]; then
229-
log "ERROR: Cannot find patch file !"
222+
log "ERROR: Cannot find patch file!"
230223
failure
231224
fi
232225

0 commit comments

Comments
 (0)