From 92a4ac79283e20a8922b6715d506aa8a04f15937 Mon Sep 17 00:00:00 2001
From: Marcus Comstedt <marcus@mc.pp.se>
Date: Sun, 10 Jan 2021 10:42:31 +0100
Subject: [PATCH] Installer: Optimize hash update on macOS

---
 bin/install.pike | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/bin/install.pike b/bin/install.pike
index 76f8aa2902..98fa6d9276 100644
--- a/bin/install.pike
+++ b/bin/install.pike
@@ -2659,7 +2659,7 @@ array(int) find_code_directory_hash_layout(Stdio.File f, int sb_offs, int sb_len
 
 void update_signature_hashes(Stdio.File f, int hash_offs, int hash_type,
                              int hash_size, int n_code_slots, int page_size,
-                             int code_limit)
+                             int code_limit, int|void offset, int|void length)
 {
   Crypto.Hash hash;
   switch(hash_type) {
@@ -2685,6 +2685,11 @@ void update_signature_hashes(Stdio.File f, int hash_offs, int hash_type,
       int sz = 1 << page_size;
       if (pos + sz > code_limit)
         sz = code_limit - pos;
+      if (length &&
+          (pos + sz <= offset || pos >= offset + length)) {
+        pos += sz;
+        continue;
+      }
       f->seek(pos);
       data = f->read(sz);
       pos += sizeof(data);
@@ -2697,7 +2702,7 @@ void update_signature_hashes(Stdio.File f, int hash_offs, int hash_type,
   f->write(hashes * "");
 }
 
-void fix_macos_adhoc_signature(Stdio.File f)
+void fix_macos_adhoc_signature(Stdio.File f, int|void offset, int|void length)
 {
   array(int) macho_sig = find_macho_signature(f);
   if (macho_sig) {
@@ -2706,7 +2711,7 @@ void fix_macos_adhoc_signature(Stdio.File f)
       array(int) hash_layout = find_code_directory_hash_layout(f, @macho_sig,
                                                                sb_code_dir);
       if (hash_layout)
-        update_signature_hashes(f, @hash_layout);
+        update_signature_hashes(f, @hash_layout, offset, length);
     }
   }
 }
@@ -2764,9 +2769,11 @@ void finalize_pike()
       pike_bin_file=combine_path(vars->TMP_BUILDDIR,"pike.tmp");
       Stdio.write_file(pike_bin_file, pike_bin);
       Stdio.File f=Stdio.File(pike_bin_file,"rw");
-      f->seek(pos+sizeof(MASTER_COOKIE));
-      f->write(combine_path(lib_prefix,"master.pike"));
-      fix_macos_adhoc_signature(f);
+      int master_pos = pos+sizeof(MASTER_COOKIE);
+      string master_value = combine_path(lib_prefix,"master.pike");
+      f->seek(master_pos);
+      f->write(master_value);
+      fix_macos_adhoc_signature(f, master_pos, sizeof(master_value));
       f->close();
       status("Finalizing",pike_bin_file,"done");
       if(install_file(pike_bin_file,pike,0755)) {
-- 
GitLab