Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 68 additions & 75 deletions src/tools/pm_apply
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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"
Expand Down Expand Up @@ -45,11 +46,6 @@ log() {
echo "$@" | tee -a "$PM_LOG_FILE"
}

log "Mangle candidates"
log $MANGLE_CANDIDATES
log "Disable mangling"
log $DISABLE_MANGLING

failure() {
log
log "*** FAILED ***"
Expand Down Expand Up @@ -84,65 +80,65 @@ test_if_applied() {

# 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->64bit conversion"
[ $SYS_BITNESS -eq 64 ] && log "Checking paths for 64->32bit conversion"
log "----------------------------------"
log
if [ -f "$PATCH_PATH" ]; then
log
log "----------------------------------"
[ $SYS_BITNESS -eq 32 ] && log "Checking paths for 32->64bit conversion"
[ $SYS_BITNESS -eq 64 ] && log "Checking paths for 64->32bit conversion"
log "----------------------------------"
log

found=0
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/ash/busybox
candidates=$(printf '%s' "$MANGLE_CANDIDATES" | sed 's@/usr/lib/@/usr/lib64/@g' )
fi
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

for p in $candidates; do
totl_lines=$(grep -c "^+++" "$PATCH_PATH")
cand_lines=$(grep -c "^+++ $p" "$PATCH_PATH")
if [ $cand_lines -eq 0 ]; then
continue # nothing found, try next
else
found=$(( $found + $cand_lines ))
if [ $totl_lines -ne $cand_lines ]; then
# if there are several references in the patch file our mangling might do too much and cause the patch to fail.
log "WARNING: mixed patch, conversion might not work"
fi
fi
log "found: ${p}, replacing libpath references"

# prepare the 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/@')
found=0
for p in $candidates; do
totl_lines=$(grep -c "^\+\+\+" "$PATCH_PATH")
cand_lines=$(grep -c "^\+\+\+ [^/]*$p" "$PATCH_PATH")
if [ $cand_lines -eq 0 ]; then
continue # nothing found, try next
else
found=$(( $found + $cand_lines ))
if [ $totl_lines -ne $cand_lines ]; then
# if there are several references in the patch file our mangling might do too much and cause the patch to fail.
log "WARNING: mixed patch, conversion might not work"
fi
# doit
fi
log "found: ${p}, replacing libpath references"
# prepare the 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/@')
fi
# doit
if [ $found -eq $cand_lines ]; then # first run in loop
mkdir -p "$PM_PATCH_BACKUP_DIR"
patch_edited_path="$PM_PATCH_BACKUP_DIR"/"$PATCH_EDITED_NAME"
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"
sed "s@^+++ $p@+++ $pr@;s@^--- $p@--- $pr@" "$PATCH_PATH" >> "$patch_edited_path"
if [ $? -ne 0 ]; then
failure
fi
break
done
if [ $found -eq 0 ]; then
log
log "OK, found nothing to convert."
log
else
log
log "OK, replaced $found libpath references and created: $patch_edited_path"
log
# set the patch to apply to the new one:
PATCH_PATH="$patch_edited_path"
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" > "$patch_edited_path"
fi
sed -i "s@^\+\+\+ \([^/]*\)$p@+++ \1$pr@;s@^--- \([^/]*\)$p@--- \1$pr@" "$patch_edited_path" || failure || true # patch the Patch
done
if [ $found -eq 0 ]; then
log
log "OK, found nothing to convert."
log
else
log
log "OK, replaced $found libpath references and created: $patch_edited_path"
log
# set the patch to apply to the new one:
PATCH_PATH="$patch_edited_path"
fi
fi
}

verify_text_patch() {
Expand Down Expand Up @@ -182,8 +178,8 @@ create_backup_patch() {

mkdir -p "$PM_PATCH_BACKUP_DIR" > /dev/null 2>&1
cp -f "$PATCH_DIR/$PATCH_NAME" "$PM_PATCH_BACKUP_DIR" > /dev/null 2>&1
log "Created backup"
log " backup: $PM_PATCH_BACKUP_DIR"
log "Created backup: $PM_PATCH_BACKUP_DIR"
log
}

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

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

log
log "----------------------------------"
log "PM APPLY $(date)"
log "pm_apply $(date -Iseconds)"
log "----------------------------------"
log

log "$(basename "$PATCH_DIR")"
if [ -z "$PARAMETER" ]; then
log "ERROR: A parameter must be provided for applying!"
failure
fi

if [ -f "$PATCH_PATH" ]; then
log " contains text patch"
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 find $PATCH_EXEC"
failure
fi

if [ -z "$PATCH_DIR" ]; then
log "ERROR: PATCH_DIR must be given for installing"
log "ERROR: Cannot use patch executable ${PATCH_EXEC}!"
failure
fi

if [ ! -d "$PATCH_DIR" ]; then
log "ERROR: $PATCH_DIR is not a directory or does not exist!"
failure
fi
log "Using patch file: $PATCH_PATH"

if [ ! -f "$PATCH_PATH" ]; then
log "ERROR: Cannot find patch file !"
log "ERROR: Cannot find patch file!"
failure
fi

Expand Down