Sort files in cpio archive
As discussed on Matrix, in order to produce a reproducible archive that can be compared with e.g. diffoscope it would be great if the list of files in the archive would be sorted deterministically.
A WIP patch that actually doesn't work can be found here:
diff --git a/pkgs/archive/archive.go b/pkgs/archive/archive.go
index c47c2e2..d7a6caf 100644
--- a/pkgs/archive/archive.go
+++ b/pkgs/archive/archive.go
@@ -13,6 +13,7 @@ import (
"log"
"os"
"path/filepath"
+ "sort"
"strings"
)
@@ -181,8 +182,16 @@ func (archive *Archive) writeCpio() error {
archive.addDir(dir)
}
+ // Sort files, so they have a deterministic order in the archive
+ keys := make([]string, 0, len(archive.Files))
+ for k := range archive.Files {
+ keys = append(keys, k)
+ }
+ sort.Strings(keys)
+
// Write files and any missing parent dirs
- for file, imported := range archive.Files {
+ for _, file := range keys {
+ imported := archive.Files[file]
if imported {
continue
}
I assume that also in func (archive *Archive) writeCpio()
and func (archive *Archive) addDir(dir string)
this sorting would need to be done.