livepatch/klp-build: support patches that add/remove files

The klp-build script prepares a clean patch by populating two temporary
directories ('a' and 'b') with source files and diffing the result.
However, this process fails when a patch introduces a new source file,
as the script attempts to copy files that do not yet exist in the
original source tree.  Likewise, it fails when a patch removes a source
file and the script attempts to copy a file that no longer exists.

Refactor the file-gathering logic to distinguish between original input
files and patched output files:

- Split get_patch_files() into get_patch_input_files() and
  get_patch_output_files() to identify which files exist before and
  after patch application.
- Filter out "/dev/null" from both to handle file creation/deletion.
- Update refresh_patch() to only copy existing input files to the 'a'
  directory and the resulting output files to the 'b' directory.

Acked-by: Song Liu <song@kernel.org>
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
Link: https://patch.msgid.link/20260310203751.1479229-4-joe.lawrence@redhat.com
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
This commit is contained in:
Joe Lawrence
2026-03-10 16:37:42 -04:00
committed by Josh Poimboeuf
parent 4b57e97be2
commit 757bd10ff0

View File

@@ -296,12 +296,30 @@ set_kernelversion() {
sed -i "2i echo $localversion; exit 0" scripts/setlocalversion
}
get_patch_input_files() {
local patch="$1"
grep0 -E '^--- ' "$patch" \
| gawk '{print $2}' \
| grep0 -v '^/dev/null$' \
| sed 's|^[^/]*/||' \
| sort -u
}
get_patch_output_files() {
local patch="$1"
grep0 -E '^\+\+\+ ' "$patch" \
| gawk '{print $2}' \
| grep0 -v '^/dev/null$' \
| sed 's|^[^/]*/||' \
| sort -u
}
get_patch_files() {
local patch="$1"
grep0 -E '^(--- |\+\+\+ )' "$patch" \
| gawk '{print $2}' \
| sed 's|^[^/]*/||' \
{ get_patch_input_files "$patch"; get_patch_output_files "$patch"; } \
| sort -u
}
@@ -312,7 +330,7 @@ git_refresh() {
[[ ! -e "$SRC/.git" ]] && return
get_patch_files "$patch" | mapfile -t files
get_patch_input_files "$patch" | mapfile -t files
(
cd "$SRC"
@@ -426,21 +444,23 @@ do_init() {
refresh_patch() {
local patch="$1"
local tmpdir="$PATCH_TMP_DIR"
local files=()
local input_files=()
local output_files=()
rm -rf "$tmpdir"
mkdir -p "$tmpdir/a"
mkdir -p "$tmpdir/b"
# Get all source files affected by the patch
get_patch_files "$patch" | mapfile -t files
get_patch_input_files "$patch" | mapfile -t input_files
get_patch_output_files "$patch" | mapfile -t output_files
# Copy orig source files to 'a'
( cd "$SRC" && echo "${files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" )
( cd "$SRC" && echo "${input_files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" )
# Copy patched source files to 'b'
apply_patch "$patch" --recount
( cd "$SRC" && echo "${files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" )
( cd "$SRC" && echo "${output_files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" )
revert_patch "$patch" --recount
# Diff 'a' and 'b' to make a clean patch