Skip to content

Commit 4d6d293

Browse files
committed
setup-hooks/strip: uniqify files by inode number before stripping
#246164 but for hardlinks. Mesa, among other packages, has binaries that are linked together and can end up corrupted when the same binary is stripped through two different names. To resolve this, print out the device and inode number before each file name, sort/uniq based on that, then cut it back out before stripping. The symlink resolution logic is removed as the same file accessed through two different links in `$paths` will necessarily have the same numbers. File/directory within the paths listed in `$paths` are correctly not (and were never) processed due to the `-type f` predicate and (implied) `-P` option to `find`.
1 parent 3254cac commit 4d6d293

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

pkgs/build-support/setup-hooks/strip.sh

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,17 @@ stripDirs() {
7474
echo "stripping (with command $cmd and flags $stripFlags) in $paths"
7575
local striperr
7676
striperr="$(mktemp --tmpdir="$TMPDIR" 'striperr.XXXXXX')"
77-
# Do not strip lib/debug. This is a directory used by setup-hooks/separate-debug-info.sh.
78-
find $paths -type f "${excludeFlags[@]}" -a '!' -path "$prefix/lib/debug/*" -print0 |
79-
# Make sure we process files under symlinks only once. Otherwise
80-
# 'strip` can corrupt files when writes to them in parallel:
81-
# https://github.com/NixOS/nixpkgs/issues/246147#issuecomment-1657072039
82-
xargs -r -0 -n1 -- realpath -z | sort -u -z |
77+
# Make sure we process files only once. `strip`ping the same file through different
78+
# links in parallel can corrupt it:
79+
# https://github.com/NixOS/nixpkgs/issues/246147#issuecomment-1657072039
8380

81+
# Do not strip lib/debug. This is a directory used by setup-hooks/separate-debug-info.sh.
82+
# Print out each file's device and inode (which will be the same if two files are hardlinked
83+
# or are the same file found through different symlinks), followed by its path...
84+
find $paths -type f "${excludeFlags[@]}" -a '!' -path "$prefix/lib/debug/*" -printf '%D-%i,%p\0' |
85+
# ... sort/uniq by device/inode, then cut them out and keep the path, ...
86+
sort -t, -k1,1 -u -z | cut -d, -f2- -z |
87+
# and finally strip each unique path in parallel.
8488
xargs -r -0 -n1 -P "$NIX_BUILD_CORES" -- $cmd $stripFlags 2>"$striperr" || exit_code=$?
8589
# xargs exits with status code 123 if some but not all of the
8690
# processes fail. We don't care if some of the files couldn't

0 commit comments

Comments
 (0)