Commit 7c2a42b390bb9dccc89dbe3666b944c79a30002f

Thomas de Grivel 2023-02-22T17:28:08

sha256

diff --git a/lib/rbpkg.rb b/lib/rbpkg.rb
index 0d5f574..9c39a6a 100644
--- a/lib/rbpkg.rb
+++ b/lib/rbpkg.rb
@@ -133,9 +133,10 @@ module Rbpkg
 
   def self.inspect
     """%Rbpkg{
- dir: #{dir.inspect},
  CC: #{cc.inspect},
+ dir: #{dir.inspect},
  ncpu: #{ncpu.inspect},
+ sha256: #{sha256.inspect},
  target: #{target.inspect}
 }"""
   end
@@ -238,11 +239,28 @@ module Rbpkg
     end
   end
 
-  def self.sha256(path)
-    c = "sha256 #{sh_quote(path)}"
+  @@sha256 = nil
+  def self.sha256
+    @@sha256 || @@sha256 = if cmd "which sha256"
+                             "sha256"
+                           elsif cmd "which sha256sum"
+                             "sha256sum"
+                           else
+                             raise "sha256 executable not found"
+                           end
+  end
+
+  def self.sha256_file(path)
+    if File.basename(sha256).match?(/sum/)
+      c = "#{sh_quote(sha256)} #{sh_quote(path)} | cut -d ' ' -f 1"
+      r = `#{c}`
+      raise "#{sha256} #{path.inspect} failed" unless $?.success?
+      return "SHA256 (#{path}) = #{r}\n"
+    end
+    c = "#{sh_quote(sha256)} #{sh_quote(path)}"
     verbose(1, c)
     r = `#{c}`
-    raise "sha256(#{path.inspect}) failed" unless $?.success?
+    raise "#{sha256} #{path.inspect} failed" unless $?.success?
     r
   end
 
diff --git a/lib/rbpkg/repo.rb b/lib/rbpkg/repo.rb
index 792056b..83d10c5 100644
--- a/lib/rbpkg/repo.rb
+++ b/lib/rbpkg/repo.rb
@@ -261,7 +261,7 @@ class Rbpkg::Repo
       if File.directory?(path_f)
         package_checksum_rec(path_f, Dir.new(path_f).children, output)
       else
-        output.write(Rbpkg.sha256(path_f))
+        output.write(Rbpkg.sha256_file(path_f))
       end
     end
   end