History

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.

Joe Lawrence committed 8 months ago

Commit 757bd101 parent 4b57e971 file · +27 −7Browse files

Showing the changes to scripts/livepatch (1 of 1 file). Show all

Unified · Split

scripts/livepatch/klp-build+27−7
@@ -296,15 +296,33 @@ set_kernelversion() { · 295 unmodified lines
296296 sed -i "2i echo $localversion; exit 0" scripts/setlocalversion
297297 }
298298
299−get_patch_files() {
299+get_patch_input_files() {
300+ local patch="$1"
301+
302+ grep0 -E '^--- ' "$patch" \
303+ | gawk '{print $2}' \
304+ | grep0 -v '^/dev/null$' \
305+ | sed 's|^[^/]*/||' \
306+ | sort -u
307+}
308+
309+get_patch_output_files() {
300310 local patch="$1"
301311
302− grep0 -E '^(--- |\+\+\+ )' "$patch" \
312+ grep0 -E '^\+\+\+ ' "$patch" \
303313 | gawk '{print $2}' \
314+ | grep0 -v '^/dev/null$' \
304315 | sed 's|^[^/]*/||' \
305316 | sort -u
306317 }
307318
319+get_patch_files() {
320+ local patch="$1"
321+
322+ { get_patch_input_files "$patch"; get_patch_output_files "$patch"; } \
323+ | sort -u
324+}
325+
308326 # Make sure git re-stats the changed files
309327 git_refresh() {
310328 local patch="$1"
@@ -312,7 +330,7 @@ git_refresh() { · 1 unmodified line
312330
313331 [[ ! -e "$SRC/.git" ]] && return
314332
315− get_patch_files "$patch" | mapfile -t files
333+ get_patch_input_files "$patch" | mapfile -t files
316334
317335 (
318336 cd "$SRC"
@@ -426,21 +444,23 @@ do_init() { · 107 unmodified lines
426444 refresh_patch() {
427445 local patch="$1"
428446 local tmpdir="$PATCH_TMP_DIR"
429− local files=()
447+ local input_files=()
448+ local output_files=()
430449
431450 rm -rf "$tmpdir"
432451 mkdir -p "$tmpdir/a"
433452 mkdir -p "$tmpdir/b"
434453
435454 # Get all source files affected by the patch
436− get_patch_files "$patch" | mapfile -t files
455+ get_patch_input_files "$patch" | mapfile -t input_files
456+ get_patch_output_files "$patch" | mapfile -t output_files
437457
438458 # Copy orig source files to 'a'
439− ( cd "$SRC" && echo "${files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" )
459+ ( cd "$SRC" && echo "${input_files[@]}" | xargs cp --parents --target-directory="$tmpdir/a" )
440460
441461 # Copy patched source files to 'b'
442462 apply_patch "$patch" --recount
443− ( cd "$SRC" && echo "${files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" )
463+ ( cd "$SRC" && echo "${output_files[@]}" | xargs cp --parents --target-directory="$tmpdir/b" )
444464 revert_patch "$patch" --recount
445465
446466 # Diff 'a' and 'b' to make a clean patch