summaryrefslogtreecommitdiff
path: root/zip/zip.go
diff options
context:
space:
mode:
Diffstat (limited to 'zip/zip.go')
-rw-r--r--zip/zip.go90
1 files changed, 66 insertions, 24 deletions
diff --git a/zip/zip.go b/zip/zip.go
index e4e9585d5..22b770474 100644
--- a/zip/zip.go
+++ b/zip/zip.go
@@ -272,7 +272,6 @@ type ZipArgs struct {
FileArgs []FileArg
OutputFilePath string
EmulateJar bool
- SortEntries bool
SrcJar bool
AddDirectoryEntriesToZip bool
CompressionLevel int
@@ -395,7 +394,7 @@ func zipTo(args ZipArgs, w io.Writer) error {
}
}
- return z.write(w, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.SortEntries, args.SrcJar, args.NumParallelJobs)
+ return z.write(w, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.SrcJar, args.NumParallelJobs)
}
// Zip creates an output zip archive from given sources.
@@ -476,6 +475,42 @@ func fillPathPairs(fa FileArg, src string, pathMappings *[]pathMapping,
return nil
}
+func (z *ZipWriter) moveJavaFileBasedOnPackage(mapping *pathMapping) error {
+ src := mapping.src
+ var s os.FileInfo
+ var err error
+ if z.followSymlinks {
+ s, err = z.fs.Stat(src)
+ } else {
+ s, err = z.fs.Lstat(src)
+ }
+ if err != nil {
+ if os.IsNotExist(err) && z.ignoreMissingFiles {
+ return nil
+ }
+ return err
+ }
+ if !s.Mode().IsRegular() {
+ return nil
+ }
+ r, err := z.fs.Open(src)
+ if err != nil {
+ return err
+ }
+ // rewrite the destination using the package path if it can be determined
+ pkg, err := jar.JavaPackage(r, src)
+ err2 := r.Close()
+ if err2 != nil {
+ return err2
+ }
+ if err != nil {
+ // ignore errors for now, leaving the file at in its original location in the zip
+ } else {
+ mapping.dest = filepath.Join(filepath.Join(strings.Split(pkg, ".")...), filepath.Base(src))
+ }
+ return nil
+}
+
func jarSort(mappings []pathMapping) {
sort.SliceStable(mappings, func(i int, j int) bool {
return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest)
@@ -483,7 +518,7 @@ func jarSort(mappings []pathMapping) {
}
func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest string,
- emulateJar, sortEntries, srcJar bool,
+ emulateJar, srcJar bool,
parallelJobs int) error {
z.errors = make(chan error)
@@ -513,16 +548,38 @@ 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)")
+ // move java source files to the correct folder based on the package statement inside of them.
+ // This is done before the entry sorting so that they're still in the right order.
+ if srcJar {
+ var javaMoveErrors []error
+ var javaMoveErrorsLock sync.Mutex
+ var wg sync.WaitGroup
+ for i := range pathMappings {
+ if filepath.Ext(pathMappings[i].src) == ".java" {
+ wg.Add(1)
+ go func() {
+ err := z.moveJavaFileBasedOnPackage(&pathMappings[i])
+ if err != nil {
+ javaMoveErrorsLock.Lock()
+ javaMoveErrors = append(javaMoveErrors, err)
+ javaMoveErrorsLock.Unlock()
+ }
+ wg.Done()
+ }()
+ }
+ }
+ wg.Wait()
+ if len(javaMoveErrors) > 0 {
+ return errors.Join(javaMoveErrors...)
+ }
}
+
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 {
+ } else {
sort.SliceStable(pathMappings, func(i int, j int) bool {
return pathMappings[i].dest < pathMappings[j].dest
})
@@ -536,7 +593,7 @@ func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest stri
if emulateJar && ele.dest == jar.ManifestFile {
err = z.addManifest(ele.dest, ele.src, ele.zipMethod)
} else {
- err = z.addFile(ele.dest, ele.src, ele.zipMethod, emulateJar, srcJar)
+ err = z.addFile(ele.dest, ele.src, ele.zipMethod, emulateJar)
}
if err != nil {
z.errors <- err
@@ -635,7 +692,7 @@ func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest stri
}
// imports (possibly with compression) <src> into the zip at sub-path <dest>
-func (z *ZipWriter) addFile(dest, src string, method uint16, emulateJar, srcJar bool) error {
+func (z *ZipWriter) addFile(dest, src string, method uint16, emulateJar bool) error {
var fileSize int64
var executable bool
@@ -709,21 +766,6 @@ func (z *ZipWriter) addFile(dest, src string, method uint16, emulateJar, srcJar
return err
}
- if srcJar && filepath.Ext(src) == ".java" {
- // rewrite the destination using the package path if it can be determined
- pkg, err := jar.JavaPackage(r, src)
- if err != nil {
- // ignore errors for now, leaving the file at in its original location in the zip
- } else {
- dest = filepath.Join(filepath.Join(strings.Split(pkg, ".")...), filepath.Base(src))
- }
-
- _, err = r.Seek(0, io.SeekStart)
- if err != nil {
- return err
- }
- }
-
fileSize = s.Size()
executable = s.Mode()&0100 != 0