diff options
author | 2018-09-18 17:05:15 -0700 | |
---|---|---|
committer | 2018-09-28 14:01:29 -0700 | |
commit | 1d98ee23a322a997d28ef5dd07412db4b9564d2a (patch) | |
tree | 6c7de5a9a7da78ae634fd693768912bb5b2cd8fc /zip | |
parent | 05518bc13b3cd31e6cb7158a41aa124b224c3bc4 (diff) |
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
Diffstat (limited to 'zip')
-rw-r--r-- | zip/zip.go | 76 | ||||
-rw-r--r-- | zip/zip_test.go | 45 |
2 files changed, 102 insertions, 19 deletions
diff --git a/zip/zip.go b/zip/zip.go index e7de6f8cc..7eebf06ed 100644 --- a/zip/zip.go +++ b/zip/zip.go @@ -27,6 +27,7 @@ import ( "sort" "strings" "sync" + "syscall" "time" "unicode" @@ -163,6 +164,15 @@ func (b *FileArgsBuilder) FileArgs() []FileArg { 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 @@ func ZipTo(args ZipArgs, w io.Writer) error { 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 Zip(args ZipArgs) error { 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 @@ func fillPathPairs(fa FileArg, src string, pathMappings *[]pathMapping, 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 @@ func (z *ZipWriter) writeSymlink(rel, file string) error { 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 -} diff --git a/zip/zip_test.go b/zip/zip_test.go index 0c2105c67..e77801b91 100644 --- a/zip/zip_test.go +++ b/zip/zip_test.go @@ -130,6 +130,34 @@ func TestZip(t *testing.T) { }, }, { + name: "files glob", + args: fileArgsBuilder(). + SourcePrefixToStrip("a"). + File("a/**/*"), + compressionLevel: 9, + + files: []zip.FileHeader{ + fh("a/a", fileA, zip.Deflate), + fh("a/b", fileB, zip.Deflate), + fhLink("a/c", "../../c"), + fhLink("a/d", "b"), + }, + }, + { + name: "dir", + args: fileArgsBuilder(). + SourcePrefixToStrip("a"). + Dir("a"), + compressionLevel: 9, + + files: []zip.FileHeader{ + fh("a/a", fileA, zip.Deflate), + fh("a/b", fileB, zip.Deflate), + fhLink("a/c", "../../c"), + fhLink("a/d", "b"), + }, + }, + { name: "stored files", args: fileArgsBuilder(). File("a/a/a"). @@ -298,11 +326,24 @@ func TestZip(t *testing.T) { err: os.ErrNotExist, }, { + name: "error missing dir", + args: fileArgsBuilder(). + Dir("missing"), + err: os.ErrNotExist, + }, + { name: "error missing file in list", args: fileArgsBuilder(). List("l2"), err: os.ErrNotExist, }, + { + name: "error incorrect relative root", + args: fileArgsBuilder(). + SourcePrefixToStrip("b"). + File("a/a/a"), + err: IncorrectRelativeRootError{}, + }, } for _, test := range testCases { @@ -330,6 +371,10 @@ func TestZip(t *testing.T) { if !os.IsNotExist(test.err) { t.Fatalf("want error %v, got %v", test.err, err) } + } else if _, wantRelativeRootErr := test.err.(IncorrectRelativeRootError); wantRelativeRootErr { + if _, gotRelativeRootErr := err.(IncorrectRelativeRootError); !gotRelativeRootErr { + t.Fatalf("want error %v, got %v", test.err, err) + } } else { t.Fatalf("want error %v, got %v", test.err, err) } |