blob: 294e5ef0a7238160739f92a56531521cb852c0cc [file] [log] [blame]
Colin Cross436b7652018-03-15 16:24:10 -07001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
18 "archive/zip"
19 "flag"
20 "fmt"
21 "io"
22 "io/ioutil"
23 "log"
24 "os"
25 "path/filepath"
26 "strings"
27)
28
29var (
30 outputDir = flag.String("d", "", "output dir")
31 outputFile = flag.String("l", "", "output list file")
32 filter = flag.String("f", "", "optional filter pattern")
Dan Willemsen304cfec2019-05-28 14:49:06 -070033 zipPrefix = flag.String("zip-prefix", "", "optional prefix within the zip file to extract, stripping the prefix")
Colin Cross436b7652018-03-15 16:24:10 -070034)
35
36func must(err error) {
37 if err != nil {
38 log.Fatal(err)
39 }
40}
41
42func writeFile(filename string, in io.Reader, perm os.FileMode) error {
43 out, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
44 if err != nil {
45 return err
46 }
47 _, err = io.Copy(out, in)
48 if err != nil {
49 out.Close()
50 return err
51 }
52
53 return out.Close()
54}
55
56func main() {
57 flag.Usage = func() {
58 fmt.Fprintln(os.Stderr, "usage: zipsync -d <output dir> [-l <output file>] [-f <pattern>] [zip]...")
59 flag.PrintDefaults()
60 }
61
62 flag.Parse()
63
64 if *outputDir == "" {
65 flag.Usage()
66 os.Exit(1)
67 }
68
69 inputs := flag.Args()
70
71 // For now, just wipe the output directory and replace its contents with the zip files
72 // Eventually this could only modify the directory contents as necessary to bring it up
73 // to date with the zip files.
74 must(os.RemoveAll(*outputDir))
75
76 must(os.MkdirAll(*outputDir, 0777))
77
78 var files []string
79 seen := make(map[string]string)
80
Dan Willemsen304cfec2019-05-28 14:49:06 -070081 if *zipPrefix != "" {
82 *zipPrefix = filepath.Clean(*zipPrefix) + "/"
83 }
84
Colin Cross436b7652018-03-15 16:24:10 -070085 for _, input := range inputs {
86 reader, err := zip.OpenReader(input)
87 if err != nil {
88 log.Fatal(err)
89 }
90 defer reader.Close()
91
92 for _, f := range reader.File {
Dan Willemsen304cfec2019-05-28 14:49:06 -070093 name := f.Name
94 if *zipPrefix != "" {
95 if !strings.HasPrefix(name, *zipPrefix) {
96 continue
97 }
98 name = strings.TrimPrefix(name, *zipPrefix)
99 }
Colin Cross436b7652018-03-15 16:24:10 -0700100 if *filter != "" {
Dan Willemsen304cfec2019-05-28 14:49:06 -0700101 if match, err := filepath.Match(*filter, filepath.Base(name)); err != nil {
Colin Cross436b7652018-03-15 16:24:10 -0700102 log.Fatal(err)
103 } else if !match {
104 continue
105 }
106 }
Dan Willemsen304cfec2019-05-28 14:49:06 -0700107 if filepath.IsAbs(name) {
108 log.Fatalf("%q in %q is an absolute path", name, input)
Colin Cross436b7652018-03-15 16:24:10 -0700109 }
110
Dan Willemsen304cfec2019-05-28 14:49:06 -0700111 if prev, exists := seen[name]; exists {
112 log.Fatalf("%q found in both %q and %q", name, prev, input)
Colin Cross436b7652018-03-15 16:24:10 -0700113 }
Dan Willemsen304cfec2019-05-28 14:49:06 -0700114 seen[name] = input
Colin Cross436b7652018-03-15 16:24:10 -0700115
Dan Willemsen304cfec2019-05-28 14:49:06 -0700116 filename := filepath.Join(*outputDir, name)
Colin Cross436b7652018-03-15 16:24:10 -0700117 if f.FileInfo().IsDir() {
Jaewoong Junga86983d2020-04-03 18:35:38 -0700118 must(os.MkdirAll(filename, 0777))
Colin Cross436b7652018-03-15 16:24:10 -0700119 } else {
120 must(os.MkdirAll(filepath.Dir(filename), 0777))
121 in, err := f.Open()
122 if err != nil {
123 log.Fatal(err)
124 }
125 must(writeFile(filename, in, f.FileInfo().Mode()))
126 in.Close()
127 files = append(files, filename)
128 }
129 }
130 }
131
132 if *outputFile != "" {
Colin Cross628d55d2018-03-22 12:38:05 -0700133 data := strings.Join(files, "\n")
134 if len(files) > 0 {
135 data += "\n"
136 }
Colin Cross436b7652018-03-15 16:24:10 -0700137 must(ioutil.WriteFile(*outputFile, []byte(data), 0666))
138 }
139}