Skip to content

Commit bbcee05

Browse files
nephrosOlf0
andauthored
[pm_apply] improve mangling by patching outside the loop (#241)
* [pm_apply] patch outside the loop * pm_apply: Use sed streamingly & some beautifying Co-authored-by: olf <[email protected]>
1 parent cb6495b commit bbcee05

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

src/tools/pm_apply

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,44 +98,48 @@ mangle_libpath() {
9898
log
9999

100100
found=0
101+
sedcmd=""
102+
# look for lines to convert, if some are found add pattern to sed scriptlet
101103
for p in $candidates; do
102-
totl_lines=$(grep -c "^\+\+\+" "$PATCH_PATH")
103104
cand_lines=$(grep -c "^\+\+\+ [^/]*$p" "$PATCH_PATH")
104105
if [ $cand_lines -eq 0 ]; then
105106
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"
111-
fi
112107
fi
113-
log "found: ${p}, replacing libpath references"
114-
# prepare the replacement pattern
108+
found=$(( $found + $cand_lines ))
109+
log "Converting library path reference $p $cand_lines times"
110+
111+
# prepare the path replacement pattern
115112
pr=""
116113
if [ $SYS_BITNESS -eq 32 ]; then
117114
pr=$(printf '%s' "$p" | sed 's@/usr/lib64/@/usr/lib/@')
118115
elif [ $SYS_BITNESS -eq 64 ]; then
119116
pr=$(printf '%s' "$p" | sed 's@/usr/lib/@/usr/lib64/@')
117+
else
118+
failure
120119
fi
121-
# doit
122-
if [ $found -eq $cand_lines ]; then # first run in loop
123-
mkdir -p "$PM_PATCH_BACKUP_DIR"
124-
patch_edited_path="$PM_PATCH_BACKUP_DIR"/"$PATCH_EDITED_NAME"
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"
127-
fi
128-
sed -i "s@^\+\+\+ \([^/]*\)$p@+++ \1$pr@;s@^--- \([^/]*\)$p@--- \1$pr@" "$patch_edited_path" || failure || true # patch the Patch
120+
# append at the front so ';' can be easily used for separation
121+
sedcmd="s@^\+\+\+ \([^/]*\)$p@+++ \1$pr@;s@^--- \([^/]*\)$p@--- \1$pr@;$sedcmd"
129122
done
123+
130124
if [ $found -eq 0 ]; then
131125
log
132126
log "OK, found nothing to convert."
133127
log
134128
else
129+
mkdir -p "$PM_PATCH_BACKUP_DIR"
130+
patch_edited_path="$PM_PATCH_BACKUP_DIR"/"$PATCH_EDITED_NAME"
131+
132+
# create mangled patch file and add a note to it
133+
printf '#\n# Patch converted to %sbit library paths from its original by Patchmanager > 3.1\n# Date: %s\n#\n' $SYS_BITNESS $(date -Iseconds) \
134+
| cat - "$PATCH_PATH" | sed "$sedcmd" > "$patch_edited_path" # patch the Patch
135+
if [ $? -ne 0 ]; then
136+
failure
137+
fi
138+
135139
log
136-
log "OK, replaced $found libpath references and created: $patch_edited_path"
140+
log "OK, converted $found library path references and created: $patch_edited_path"
137141
log
138-
# set the patch to apply to the new one:
142+
# set the patch path to the new one:
139143
PATCH_PATH="$patch_edited_path"
140144
fi
141145
fi
@@ -150,7 +154,6 @@ verify_text_patch() {
150154
log
151155

152156
$PATCH_EXEC -p 1 -d "$ROOT_DIR" --dry-run < "$PATCH_PATH" 2>&1 | tee -a "$PM_LOG_FILE"
153-
154157
if [ $? -ne 0 ]; then
155158
failure
156159
fi

0 commit comments

Comments
 (0)