soong_zip: support globs in -f and -D arguments
-f and -D arguments can now take globs in the Soong format.
Also update the use of soong_zip that jars resources to escape the
globs in the arguments, and then shell-escape them when writing to
the rsp file so the glob escape are not intepreted by ReadRespFile.
Also remove an unused argument to the buildAAR rule that could
have contained values that needed escaping.
Relands I7f20bb169dc01f952d2a7681ec6ee9c05737ed37 with a fix for
trailing "\n" in list files, which causes a call to pathtools.Glob("")
that returns "./", which could then get incorrectly translated to
"../../../" in the zip file. Also adds tests.
Test: m checkbuild
Test: zip_test.go
Change-Id: I54b8eef9231875e6042a32c9f8bcc5c2f779922a
diff --git a/zip/zip.go b/zip/zip.go
index e7de6f8..7eebf06 100644
--- a/zip/zip.go
+++ b/zip/zip.go
@@ -27,6 +27,7 @@
"sort"
"strings"
"sync"
+ "syscall"
"time"
"unicode"
@@ -163,6 +164,15 @@
return b.fileArgs
}
+type IncorrectRelativeRootError struct {
+ RelativeRoot string
+ Path string
+}
+
+func (x IncorrectRelativeRootError) Error() string {
+ return fmt.Sprintf("path %q is outside relative root %q", x.Path, x.RelativeRoot)
+}
+
type ZipWriter struct {
time time.Time
createdFiles map[string]string
@@ -271,9 +281,47 @@
noCompression := args.CompressionLevel == 0
for _, fa := range args.FileArgs {
- srcs := fa.SourceFiles
+ var srcs []string
+ for _, s := range fa.SourceFiles {
+ s = strings.TrimSpace(s)
+ if s == "" {
+ continue
+ }
+
+ globbed, _, err := z.fs.Glob(s, nil, pathtools.DontFollowSymlinks)
+ if err != nil {
+ return err
+ }
+ if len(globbed) == 0 {
+ return &os.PathError{
+ Op: "stat",
+ Path: s,
+ Err: os.ErrNotExist,
+ }
+ }
+ srcs = append(srcs, globbed...)
+ }
if fa.GlobDir != "" {
- srcs = append(srcs, recursiveGlobFiles(fa.GlobDir)...)
+ if exists, isDir, err := z.fs.Exists(fa.GlobDir); err != nil {
+ return err
+ } else if !exists {
+ return &os.PathError{
+ Op: "stat",
+ Path: fa.GlobDir,
+ Err: os.ErrNotExist,
+ }
+ } else if !isDir {
+ return &os.PathError{
+ Op: "stat",
+ Path: fa.GlobDir,
+ Err: syscall.ENOTDIR,
+ }
+ }
+ globbed, _, err := z.fs.Glob(filepath.Join(fa.GlobDir, "**/*"), nil, pathtools.DontFollowSymlinks)
+ if err != nil {
+ return err
+ }
+ srcs = append(srcs, globbed...)
}
for _, src := range srcs {
err := fillPathPairs(fa, src, &pathMappings, args.NonDeflatedFiles, noCompression)
@@ -328,11 +376,6 @@
func fillPathPairs(fa FileArg, src string, pathMappings *[]pathMapping,
nonDeflatedFiles map[string]bool, noCompression bool) error {
- src = strings.TrimSpace(src)
- if src == "" {
- return nil
- }
- src = filepath.Clean(src)
var dest string
if fa.JunkPaths {
@@ -343,6 +386,13 @@
if err != nil {
return err
}
+ if strings.HasPrefix(dest, "../") {
+ return IncorrectRelativeRootError{
+ Path: src,
+ RelativeRoot: fa.SourcePrefixToStrip,
+ }
+ }
+
}
dest = filepath.Join(fa.PathPrefixInZip, dest)
@@ -889,15 +939,3 @@
return nil
}
-
-func recursiveGlobFiles(path string) []string {
- var files []string
- filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
- if !info.IsDir() {
- files = append(files, path)
- }
- return nil
- })
-
- return files
-}