diff --git a/zip/cmd/main.go b/zip/cmd/main.go index d603586e6..fc976f689 100644 --- a/zip/cmd/main.go +++ b/zip/cmd/main.go @@ -12,6 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +// soong_zip is a utility used during the build to create a zip archive by pulling the entries from +// various sources: +// * explicitly specified files +// * files whose paths are read from a file +// * directories traversed recursively +// It can optionally change the recorded path of an entry. + package main import ( diff --git a/zip/zip.go b/zip/zip.go index e27432cbb..cb85f5ced 100644 --- a/zip/zip.go +++ b/zip/zip.go @@ -126,6 +126,7 @@ func (b *FileArgsBuilder) Dir(name string) *FileArgsBuilder { return b } +// List reads the file names from the given file and adds them to the source files list. func (b *FileArgsBuilder) List(name string) *FileArgsBuilder { if b.err != nil { return b @@ -150,6 +151,7 @@ func (b *FileArgsBuilder) List(name string) *FileArgsBuilder { return b } +// RspFile reads the file names from given .rsp file and adds them to the source files list. func (b *FileArgsBuilder) RspFile(name string) *FileArgsBuilder { if b.err != nil { return b @@ -291,7 +293,7 @@ func ReadRespFile(bytes []byte) []string { return args } -func ZipTo(args ZipArgs, w io.Writer) error { +func zipTo(args ZipArgs, w io.Writer) error { if args.EmulateJar { args.AddDirectoryEntriesToZip = true } @@ -392,6 +394,7 @@ func ZipTo(args ZipArgs, w io.Writer) error { return z.write(w, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.SrcJar, args.NumParallelJobs) } +// Zip creates an output zip archive from given sources. func Zip(args ZipArgs) error { if args.OutputFilePath == "" { return fmt.Errorf("output file path must be nonempty") @@ -416,7 +419,7 @@ func Zip(args ZipArgs) error { out = f } - err := ZipTo(args, out) + err := zipTo(args, out) if err != nil { return err } @@ -450,7 +453,6 @@ func fillPathPairs(fa FileArg, src string, pathMappings *[]pathMapping, RelativeRoot: fa.SourcePrefixToStrip, } } - } dest = filepath.Join(fa.PathPrefixInZip, dest) @@ -465,10 +467,9 @@ func fillPathPairs(fa FileArg, src string, pathMappings *[]pathMapping, } func jarSort(mappings []pathMapping) { - less := func(i int, j int) (smaller bool) { + sort.SliceStable(mappings, func(i int, j int) bool { return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest) - } - sort.SliceStable(mappings, less) + }) } func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest string, emulateJar, srcJar bool, @@ -709,7 +710,7 @@ func (z *ZipWriter) addFile(dest, src string, method uint16, emulateJar, srcJar } } -func (z *ZipWriter) addManifest(dest string, src string, method uint16) error { +func (z *ZipWriter) addManifest(dest string, src string, _ uint16) error { if prev, exists := z.createdDirs[dest]; exists { return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src) } @@ -963,7 +964,7 @@ func (z *ZipWriter) writeDirectory(dir string, src string, emulateJar bool) erro dir = filepath.Clean(dir) // discover any uncreated directories in the path - zipDirs := []string{} + var zipDirs []string for dir != "" && dir != "." { if _, exists := z.createdDirs[dir]; exists { break diff --git a/zip/zip_test.go b/zip/zip_test.go index 302a749a3..a16e09286 100644 --- a/zip/zip_test.go +++ b/zip/zip_test.go @@ -442,7 +442,7 @@ func TestZip(t *testing.T) { args.Stderr = &bytes.Buffer{} buf := &bytes.Buffer{} - err := ZipTo(args, buf) + err := zipTo(args, buf) if (err != nil) != (test.err != nil) { t.Fatalf("want error %v, got %v", test.err, err) @@ -627,7 +627,7 @@ func TestSrcJar(t *testing.T) { args.Stderr = &bytes.Buffer{} buf := &bytes.Buffer{} - err := ZipTo(args, buf) + err := zipTo(args, buf) if err != nil { t.Fatalf("got error %v", err) }