diff options
author | 2025-02-10 15:23:12 -0800 | |
---|---|---|
committer | 2025-02-10 15:23:12 -0800 | |
commit | aee4d3e145361b0b434aac85d9693a8f6a28fa13 (patch) | |
tree | 76b914aaeaac8ee94f7bf5a889a8c060028f29aa /zip | |
parent | d5a846331c73817b78abdf8e3a7f3b5b857ee42f (diff) |
Add a --sort_entries flag to soong_zip
I want to build zips that can be diffed against each other easily.
Add a --sort_entries flag so that you don't need to write the flags
in the same order each time.
Bug: 376539388
Test: Manually
Change-Id: Ic7b6b9d1755abffdb609068712d68dc6c1bb6761
Diffstat (limited to 'zip')
-rw-r--r-- | zip/cmd/main.go | 2 | ||||
-rw-r--r-- | zip/zip.go | 14 |
2 files changed, 14 insertions, 2 deletions
diff --git a/zip/cmd/main.go b/zip/cmd/main.go index 37537ab8b..831f6d421 100644 --- a/zip/cmd/main.go +++ b/zip/cmd/main.go @@ -164,6 +164,7 @@ func main() { directories := flags.Bool("d", false, "include directories in zip") compLevel := flags.Int("L", 5, "deflate compression level (0-9)") emulateJar := flags.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'") + sortEntries := flags.Bool("sort_entries", false, "sort the zip entries") writeIfChanged := flags.Bool("write_if_changed", false, "only update resultant .zip if it has changed") ignoreMissingFiles := flags.Bool("ignore_missing_files", false, "continue if a requested file does not exist") symlinks := flags.Bool("symlinks", true, "store symbolic links in zip instead of following them") @@ -228,6 +229,7 @@ func main() { FileArgs: fileArgsBuilder.FileArgs(), OutputFilePath: *out, EmulateJar: *emulateJar, + SortEntries: *sortEntries, SrcJar: *srcJar, AddDirectoryEntriesToZip: *directories, CompressionLevel: *compLevel, diff --git a/zip/zip.go b/zip/zip.go index f91a5f2cb..e4e9585d5 100644 --- a/zip/zip.go +++ b/zip/zip.go @@ -272,6 +272,7 @@ type ZipArgs struct { FileArgs []FileArg OutputFilePath string EmulateJar bool + SortEntries bool SrcJar bool AddDirectoryEntriesToZip bool CompressionLevel int @@ -394,7 +395,7 @@ func zipTo(args ZipArgs, w io.Writer) error { } } - return z.write(w, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.SrcJar, args.NumParallelJobs) + return z.write(w, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.SortEntries, args.SrcJar, args.NumParallelJobs) } // Zip creates an output zip archive from given sources. @@ -481,7 +482,8 @@ func jarSort(mappings []pathMapping) { }) } -func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest string, emulateJar, srcJar bool, +func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest string, + emulateJar, sortEntries, srcJar bool, parallelJobs int) error { z.errors = make(chan error) @@ -511,12 +513,20 @@ func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest stri return errors.New("must specify --jar when specifying a manifest via -m") } + if emulateJar && sortEntries { + return errors.New("Cannot specify both --jar and --sort_entries (--jar implies sorting with a different algorithm)") + } if emulateJar { // manifest may be empty, in which case addManifest will fill in a default pathMappings = append(pathMappings, pathMapping{jar.ManifestFile, manifest, zip.Deflate}) jarSort(pathMappings) } + if sortEntries { + sort.SliceStable(pathMappings, func(i int, j int) bool { + return pathMappings[i].dest < pathMappings[j].dest + }) + } go func() { var err error |