diff options
author | Colin Cross <ccross@android.com> | 2017-11-10 13:11:02 -0800 |
---|---|---|
committer | Colin Cross <ccross@android.com> | 2017-11-13 15:11:08 -0800 |
commit | f83c150269c933ab6b384cf5e28cbf021a6ba9e1 (patch) | |
tree | 4dd3bfdc537d90f340badafce5233327ee7480b3 /zip/zip.go | |
parent | 5e6cfbead9f4a24b5807e35a9783a8630ede0d35 (diff) | |
download | build_soong-f83c150269c933ab6b384cf5e28cbf021a6ba9e1.tar.gz build_soong-f83c150269c933ab6b384cf5e28cbf021a6ba9e1.tar.bz2 build_soong-f83c150269c933ab6b384cf5e28cbf021a6ba9e1.zip |
Add --write_if_changed argument to soong_zip
Simplify making restat rules by adding a --write_if_changed argument
to soong_zip that generates the output file into memory and then
compares it to the version on disk before writing it out.
Test: builds
Change-Id: I5059a1e3a11e79b0ca538b3b829bc7479c126ce6
Diffstat (limited to 'zip/zip.go')
-rw-r--r-- | zip/zip.go | 56 |
1 files changed, 39 insertions, 17 deletions
@@ -32,6 +32,8 @@ import ( "sync" "time" + "github.com/google/blueprint/pathtools" + "android/soong/jar" "android/soong/third_party/zip" ) @@ -127,6 +129,7 @@ type ZipArgs struct { ManifestSourcePath string NumParallelJobs int NonDeflatedFiles map[string]bool + WriteIfChanged bool } func Run(args ZipArgs) (err error) { @@ -186,8 +189,38 @@ func Run(args ZipArgs) (err error) { } } - return w.write(args.OutputFilePath, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.NumParallelJobs) + buf := &bytes.Buffer{} + var out io.Writer = buf + + if !args.WriteIfChanged { + f, err := os.Create(args.OutputFilePath) + if err != nil { + return err + } + + defer f.Close() + defer func() { + if err != nil { + os.Remove(args.OutputFilePath) + } + }() + + out = f + } + + err = w.write(out, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.NumParallelJobs) + if err != nil { + return err + } + if args.WriteIfChanged { + err := pathtools.WriteFileIfChanged(args.OutputFilePath, buf.Bytes(), 0666) + if err != nil { + return err + } + } + + return nil } func fillPathPairs(prefix, rel, src string, pathMappings *[]pathMapping, nonDeflatedFiles map[string]bool) error { @@ -226,19 +259,7 @@ type readerSeekerCloser interface { io.Seeker } -func (z *ZipWriter) write(out string, pathMappings []pathMapping, manifest string, emulateJar bool, parallelJobs int) error { - f, err := os.Create(out) - if err != nil { - return err - } - - defer f.Close() - defer func() { - if err != nil { - os.Remove(out) - } - }() - +func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest string, emulateJar bool, parallelJobs int) error { z.errors = make(chan error) defer close(z.errors) @@ -324,6 +345,7 @@ func (z *ZipWriter) write(out string, pathMappings []pathMapping, manifest strin case op := <-writeOpChan: currentWriteOpChan = nil + var err error if op.fh.Method == zip.Deflate { currentWriter, err = zipw.CreateCompressedHeader(op.fh) } else { @@ -356,21 +378,21 @@ func (z *ZipWriter) write(out string, pathMappings []pathMapping, manifest strin currentReader = futureReader case reader := <-currentReader: - _, err = io.Copy(currentWriter, reader) + _, err := io.Copy(currentWriter, reader) if err != nil { return err } currentReader = nil - case err = <-z.errors: + case err := <-z.errors: return err } } // One last chance to catch an error select { - case err = <-z.errors: + case err := <-z.errors: return err default: zipw.Close() |