Mark Wielaard a11f22
commit 038bfe01796f751001e02de41c5d8678f511f366
Mark Wielaard a11f22
Author: Michal Marek <mmarek@suse.com>
Mark Wielaard a11f22
Date:   Sat Sep 10 23:13:24 2016 +0200
Mark Wielaard a11f22
Mark Wielaard a11f22
    find-debuginfo.sh: Split directory traversal and debuginfo extraction
Mark Wielaard a11f22
    
Mark Wielaard a11f22
    This siplifies the handling of hardlinks a bit and allows a later patch
Mark Wielaard a11f22
    to parallelize the debuginfo extraction.
Mark Wielaard a11f22
    
Mark Wielaard a11f22
    Signed-off-by: Michal Marek <mmarek@suse.com>
Mark Wielaard a11f22
Mark Wielaard a11f22
diff --git a/scripts/find-debuginfo.sh b/scripts/find-debuginfo.sh
Mark Wielaard a11f22
index d83c3e2..6dcd5a4 100644
Mark Wielaard a11f22
--- a/scripts/find-debuginfo.sh
Mark Wielaard a11f22
+++ b/scripts/find-debuginfo.sh
Mark Wielaard a11f22
@@ -283,32 +283,36 @@
Mark Wielaard a11f22
 strict_error=ERROR
Mark Wielaard a11f22
 $strict || strict_error=WARNING
Mark Wielaard a11f22
 
Mark Wielaard a11f22
-# Strip ELF binaries
Mark Wielaard a11f22
+temp=$(mktemp -d ${TMPDIR:-/tmp}/find-debuginfo.XXXXXX)
Mark Wielaard a11f22
+trap 'rm -rf "$temp"' EXIT
Mark Wielaard a11f22
+
Mark Wielaard a11f22
+# Build a list of unstripped ELF files and their hardlinks
Mark Wielaard a11f22
+touch "$temp/primary"
Mark Wielaard a11f22
 find "$RPM_BUILD_ROOT" ! -path "${debugdir}/*.debug" -type f \
Mark Wielaard a11f22
      		     \( -perm -0100 -or -perm -0010 -or -perm -0001 \) \
Mark Wielaard a11f22
 		     -print |
Mark Wielaard a11f22
 file -N -f - | sed -n -e 's/^\(.*\):[ 	]*.*ELF.*, not stripped.*/\1/p' |
Mark Wielaard a11f22
 xargs --no-run-if-empty stat -c '%h %D_%i %n' |
Mark Wielaard a11f22
 while read nlinks inum f; do
Mark Wielaard a11f22
-  get_debugfn "$f"
Mark Wielaard a11f22
-  [ -f "${debugfn}" ] && continue
Mark Wielaard a11f22
-
Mark Wielaard a11f22
-  # If this file has multiple links, keep track and make
Mark Wielaard a11f22
-  # the corresponding .debug files all links to one file too.
Mark Wielaard a11f22
   if [ $nlinks -gt 1 ]; then
Mark Wielaard a11f22
-    eval linked=\$linked_$inum
Mark Wielaard a11f22
-    if [ -n "$linked" ]; then
Mark Wielaard a11f22
-      eval id=\$linkedid_$inum
Mark Wielaard a11f22
-      link=$debugfn
Mark Wielaard a11f22
-      get_debugfn "$linked"
Mark Wielaard a11f22
-      echo "hard linked $link to $debugfn"
Mark Wielaard a11f22
-      mkdir -p "$(dirname "$link")" && ln -nf "$debugfn" "$link"
Mark Wielaard a11f22
+    var=seen_$inum
Mark Wielaard a11f22
+    if test -n "${!var}"; then
Mark Wielaard a11f22
+      echo "$inum $f" >>"$temp/linked"
Mark Wielaard a11f22
       continue
Mark Wielaard a11f22
     else
Mark Wielaard a11f22
-      eval linked_$inum=\$f
Mark Wielaard a11f22
-      echo "file $f has $[$nlinks - 1] other hard links"
Mark Wielaard a11f22
+      read "$var" < <(echo 1)
Mark Wielaard a11f22
     fi
Mark Wielaard a11f22
   fi
Mark Wielaard a11f22
+  echo "$nlinks $inum $f" >>"$temp/primary"
Mark Wielaard a11f22
+done
Mark Wielaard a11f22
+
Mark Wielaard a11f22
+# Strip ELF binaries
Mark Wielaard a11f22
+do_file()
Mark Wielaard a11f22
+{
Mark Wielaard a11f22
+  local nlinks=$1 inum=$2 f=$3 id link linked
Mark Wielaard a11f22
+
Mark Wielaard a11f22
+  get_debugfn "$f"
Mark Wielaard a11f22
+  [ -f "${debugfn}" ] && return
Mark Wielaard a11f22
 
Mark Wielaard a11f22
   echo "extracting debug info from $f"
Mark Wielaard a11f22
   build_id_seed=
Mark Wielaard a11f22
@@ -328,9 +332,6 @@
Mark Wielaard a11f22
   fi
Mark Wielaard a11f22
   id=$(${lib_rpm_dir}/debugedit -b $debug_base_name -d $debug_dest_name \
Mark Wielaard a11f22
 	       $no_recompute -i $build_id_seed -l "$SOURCEFILE" "$f") || exit
Mark Wielaard a11f22
-  if [ $nlinks -gt 1 ]; then
Mark Wielaard a11f22
-    eval linkedid_$inum=\$id
Mark Wielaard a11f22
-  fi
Mark Wielaard a11f22
   if [ -z "$id" ]; then
Mark Wielaard a11f22
     echo >&2 "*** ${strict_error}: No build ID note found in $f"
Mark Wielaard a11f22
     $strict && exit 2
Mark Wielaard a11f22
@@ -376,7 +377,21 @@
Mark Wielaard a11f22
 
Mark Wielaard a11f22
   echo "./${f#$RPM_BUILD_ROOT}" >> "$ELFBINSFILE"
Mark Wielaard a11f22
 
Mark Wielaard a11f22
-done || exit
Mark Wielaard a11f22
+  # If this file has multiple links, make the corresponding .debug files
Mark Wielaard a11f22
+  # all links to one file too.
Mark Wielaard a11f22
+  if [ $nlinks -gt 1 ]; then
Mark Wielaard a11f22
+    grep "^$inum " "$temp/linked" | while read inum linked; do
Mark Wielaard a11f22
+      link=$debugfn
Mark Wielaard a11f22
+      get_debugfn "$linked"
Mark Wielaard a11f22
+      echo "hard linked $link to $debugfn"
Mark Wielaard a11f22
+      mkdir -p "$(dirname "$debugfn")" && ln -nf "$link" "$debugfn"
Mark Wielaard a11f22
+    done
Mark Wielaard a11f22
+  fi
Mark Wielaard a11f22
+}
Mark Wielaard a11f22
+
Mark Wielaard a11f22
+while read nlinks inum f; do
Mark Wielaard a11f22
+  do_file "$nlinks" "$inum" "$f"
Mark Wielaard a11f22
+done <"$temp/primary"
Mark Wielaard a11f22
 
Mark Wielaard a11f22
 # Invoke the DWARF Compressor utility.
Mark Wielaard a11f22
 if $run_dwz \