Add a --symlinks argument to soong_zip

Add a --symlinks argument that defaults to true to soong_zip.
Passing --symlinks=false will cause it to follow symlinks instead
of storing them in the zip file.

Bug: 112843624
Test: glob_test.go
Test: m checkbuild
Change-Id: I4deb98daa9d4ba9f94e3d7670c117fe00381d2ba
This commit is contained in:
Colin Cross
2018-09-21 15:12:39 -07:00
parent 08e28abc4e
commit d59dab94c4
2 changed files with 26 additions and 9 deletions

View File

@@ -186,6 +186,8 @@ func main() {
emulateJar := flags.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'")
writeIfChanged := flags.Bool("write_if_changed", false, "only update resultant .zip if it has changed")
symlinks := flags.Bool("symlinks", true, "store symbolic links in zip instead of following them")
parallelJobs := flags.Int("parallel", runtime.NumCPU(), "number of parallel threads to use")
cpuProfile := flags.String("cpuprofile", "", "write cpu profile to file")
traceFile := flags.String("trace", "", "write trace to file")
@@ -216,9 +218,10 @@ func main() {
NumParallelJobs: *parallelJobs,
NonDeflatedFiles: nonDeflatedFiles,
WriteIfChanged: *writeIfChanged,
StoreSymlinks: *symlinks,
})
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
fmt.Fprintln(os.Stderr, "error:", err.Error())
os.Exit(1)
}
}

View File

@@ -107,6 +107,7 @@ type ZipWriter struct {
compressorPool sync.Pool
compLevel int
followSymlinks pathtools.ShouldFollowSymlinks
}
type zipEntry struct {
@@ -132,6 +133,7 @@ type ZipArgs struct {
NumParallelJobs int
NonDeflatedFiles map[string]bool
WriteIfChanged bool
StoreSymlinks bool
}
const NOQUOTE = '\x00'
@@ -212,12 +214,16 @@ func Run(args ZipArgs) (err error) {
args.AddDirectoryEntriesToZip = true
}
// Have Glob follow symlinks if they are not being stored as symlinks in the zip file.
followSymlinks := pathtools.ShouldFollowSymlinks(!args.StoreSymlinks)
w := &ZipWriter{
time: jar.DefaultTime,
createdDirs: make(map[string]string),
createdFiles: make(map[string]string),
directories: args.AddDirectoryEntriesToZip,
compLevel: args.CompressionLevel,
time: jar.DefaultTime,
createdDirs: make(map[string]string),
createdFiles: make(map[string]string),
directories: args.AddDirectoryEntriesToZip,
compLevel: args.CompressionLevel,
followSymlinks: followSymlinks,
}
pathMappings := []pathMapping{}
@@ -226,14 +232,14 @@ func Run(args ZipArgs) (err error) {
for _, fa := range args.FileArgs {
var srcs []string
for _, s := range fa.SourceFiles {
globbed, _, err := pathtools.Glob(s, nil, pathtools.DontFollowSymlinks)
globbed, _, err := pathtools.Glob(s, nil, followSymlinks)
if err != nil {
return err
}
srcs = append(srcs, globbed...)
}
if fa.GlobDir != "" {
globbed, _, err := pathtools.Glob(filepath.Join(fa.GlobDir, "**/*"), nil, pathtools.DontFollowSymlinks)
globbed, _, err := pathtools.Glob(filepath.Join(fa.GlobDir, "**/*"), nil, followSymlinks)
if err != nil {
return err
}
@@ -472,7 +478,15 @@ func (z *ZipWriter) addFile(dest, src string, method uint16, emulateJar bool) er
var fileSize int64
var executable bool
if s, err := os.Lstat(src); err != nil {
var s os.FileInfo
var err error
if z.followSymlinks {
s, err = os.Stat(src)
} else {
s, err = os.Lstat(src)
}
if err != nil {
return err
} else if s.IsDir() {
if z.directories {