[debhelper-devel] [debhelper] 08/09: dh_strip: Cache file(1) output

Niels Thykier nthykier at moszumanska.debian.org
Wed Jun 15 20:29:17 UTC 2016


This is an automated email from the git hooks/post-receive script.

nthykier pushed a commit to branch master
in repository debhelper.

commit d98cb4705e8dbd69c501bda54bf318916b331776
Author: Niels Thykier <niels at thykier.net>
Date:   Wed Jun 15 18:38:53 2016 +0000

    dh_strip: Cache file(1) output
    
    Signed-off-by: Niels Thykier <niels at thykier.net>
---
 debian/changelog |  2 ++
 dh_strip         | 48 +++++++++++++++++++++++++++---------------------
 2 files changed, 29 insertions(+), 21 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 950f0b3..1cb62b6 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -44,6 +44,8 @@ debhelper (9.20160403+unreleased) UNRELEASED; urgency=medium
   * dh_strip: Use file(1) to determine the build-id when
     available.  This saves an readelf call for every binary in
     the package.
+  * dh_strip: Cache file(1) output to avoid calling file(1)
+    twice on all ELF binaries in the package.
 
  -- Niels Thykier <niels at thykier.net>  Sat, 09 Apr 2016 09:20:32 +0000
 
diff --git a/dh_strip b/dh_strip
index 4f561ff..a2c9dcb 100755
--- a/dh_strip
+++ b/dh_strip
@@ -176,27 +176,16 @@ my $strip = cross_command("strip");
 my $no_auto_dbgsym = 0;
 $no_auto_dbgsym = 1 if get_buildoption('noautodbgsym') or get_buildoption('noddebs');
 
-# I could just use `file $_[0]`, but this is safer
-sub get_file_type {
-	my $file=shift;
-	open (FILE, '-|') # handle all filenames safely
-		|| exec('file', $file)
-		|| die "can't exec file: $!";
-	my $type=<FILE>;
-	close FILE;
-	return $type;
-}
-
 # Check if a file is an elf binary, shared library, or static library,
 # for use by File::Find. It'll fill the 3 first arrays with anything
 # it finds.  The @build_ids will be the collected build-ids (if any)
-my (@shared_libs, @executables, @static_libs, @build_ids);
+my (@shared_libs, @executables, @static_libs, @build_ids, %file_output);
 sub testfile {
-	return if -l $_ or -d $_; # Skip directories and symlinks always.
+	my $fn = $_;
+	return if -l $fn or -d _; # Skip directories and symlinks always.
 	
 	# See if we were asked to exclude this file.
 	# Note that we have to test on the full filename, including directory.
-	my $fn="$File::Find::dir/$_";
 	foreach my $f (@{$dh{EXCLUDE}}) {
 		return if ($fn=~m/\Q$f\E/);
 	}
@@ -207,9 +196,9 @@ sub testfile {
 	# Does its filename look like a shared library?
 	#  - *.cmxs are OCaml native code shared libraries
 	#  - *.node are also native ELF binaries (for node-js)
-	if (m/\.(?:so.*?|cmxs|node)$/) {
+	if ($fn =~ m/\.(?:so.*?|cmxs|node)$/) {
 		# Ok, do the expensive test.
-		my $type=get_file_type($_);
+		my $type=get_file_type($fn);
 		if ($type=~m/ELF.*shared/) {
 			push @shared_libs, $fn;
 			return;
@@ -220,7 +209,7 @@ sub testfile {
 	my (undef,undef,$mode,undef)=stat(_);
 	if ($mode & 0111) {
 		# Ok, expensive test.
-		my $type=get_file_type($_);
+		my $type=get_file_type($fn);
 		if ($type=~m/ELF.*(executable|shared)/) {
 			push @executables, $fn;
 			return;
@@ -228,17 +217,31 @@ sub testfile {
 	}
 	
 	# Is it a static library, and not a debug library?
-	if (m/\/lib[^\/]*\.a$/ && ! m/.*_g\.a$/) {
+	if ($fn =~ m/\/lib[^\/]*\.a$/ && $fn !~ m/.*_g\.a$/) {
 		# Is it a binary file, or something else (maybe a linker
 		# script on Hurd, for example? I don't use file, because
 		# file returns a variety of things on static libraries.
-		if (-B $_) {
+		if (-B $fn) {
 			push @static_libs, $fn;
 			return;
 		}
 	}
 }
 
+# I could just use `file $_[0]`, but this is safer
+sub get_file_type {
+	my ($file) = @_;
+	if ($file_output{$file}) {
+		return $file_output{$file};
+	}
+	open (FILE, '-|') # handle all filenames safely
+		|| exec('file', $file)
+		|| die "can't exec file: $!";
+	my $type=<FILE>;
+	close FILE;
+	return $file_output{$file} = $type;
+}
+
 sub make_debug {
 	my ($file, $tmp, $desttmp, $use_build_id) = @_;
 	my ($debug_path, $debug_build_id);
@@ -317,8 +320,11 @@ foreach my $package (@{$dh{DOPACKAGES}}) {
 			$use_build_id = 2;
 		}
 	}
-	@shared_libs=@executables=@static_libs=@build_ids=();
-	find(\&testfile,$tmp);
+	%file_output=@shared_libs=@executables=@static_libs=@build_ids=();
+	find({
+		wanted => \&testfile,
+		no_chdir => 1,
+	}, $tmp);
 
 	foreach (@shared_libs) {
 		my $debug_path = make_debug($_, $tmp, $debugtmp, $use_build_id) if $keep_debug;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debhelper/debhelper.git




More information about the debhelper-devel mailing list