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.
Showing the changes to scripts/livepatch (1 of 1 file). Show all
Unified · Split
| @@ -296,15 +296,33 @@ set_kernelversion() { · 295 unmodified lines | ||
| 296 | 296 | sed -i "2i echo $localversion; exit 0" scripts/setlocalversion |
| 297 | 297 | } |
| 298 | 298 | |
| 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() { | |
| 300 | 310 | local patch="$1" |
| 301 | 311 | |
| 302 | − grep0 -E '^(--- |\+\+\+ )' "$patch" \ | |
| 312 | + grep0 -E '^\+\+\+ ' "$patch" \ | |
| 303 | 313 | | gawk '{print $2}' \ |
| 314 | + | grep0 -v '^/dev/null$' \ | |
| 304 | 315 | | sed 's|^[^/]*/||' \ |
| 305 | 316 | | sort -u |
| 306 | 317 | } |
| 307 | 318 | |
| 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 | + | |
| 308 | 326 | # Make sure git re-stats the changed files |
| 309 | 327 | git_refresh() { |
| 310 | 328 | local patch="$1" |
| @@ -312,7 +330,7 @@ git_refresh() { · 1 unmodified line | ||
| 312 | 330 | |
| 313 | 331 | [[ ! -e "$SRC/.git" ]] && return |
| 314 | 332 | |
| 315 | − get_patch_files "$patch" | mapfile -t files | |
| 333 | + get_patch_input_files "$patch" | mapfile -t files | |
| 316 | 334 | |
| 317 | 335 | ( |
| 318 | 336 | cd "$SRC" |
| @@ -426,21 +444,23 @@ do_init() { · 107 unmodified lines | ||
| 426 | 444 | refresh_patch() { |
| 427 | 445 | local patch="$1" |
| 428 | 446 | local tmpdir="$PATCH_TMP_DIR" |
| 429 | − local files=() | |
| 447 | + local input_files=() | |
| 448 | + local output_files=() | |
| 430 | 449 | |
| 431 | 450 | rm -rf "$tmpdir" |
| 432 | 451 | mkdir -p "$tmpdir/a" |
| 433 | 452 | mkdir -p "$tmpdir/b" |
| 434 | 453 | |
| 435 | 454 | # 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 | |
| 437 | 457 | |
| 438 | 458 | # 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" ) | |
| 440 | 460 | |
| 441 | 461 | # Copy patched source files to 'b' |
| 442 | 462 | 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" ) | |
| 444 | 464 | revert_patch "$patch" --recount |
| 445 | 465 | |
| 446 | 466 | # Diff 'a' and 'b' to make a clean patch |