archive.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package build
  17. import (
  18. "archive/tar"
  19. "archive/zip"
  20. "compress/gzip"
  21. "fmt"
  22. "io"
  23. "os"
  24. "path/filepath"
  25. "strings"
  26. "time"
  27. )
  28. type Archive interface {
  29. // Directory adds a new directory entry to the archive and sets the
  30. // directory for subsequent calls to Header.
  31. Directory(name string) error
  32. // Header adds a new file to the archive. The file is added to the directory
  33. // set by Directory. The content of the file must be written to the returned
  34. // writer.
  35. Header(os.FileInfo) (io.Writer, error)
  36. // Close flushes the archive and closes the underlying file.
  37. Close() error
  38. }
  39. func NewArchive(file *os.File) (Archive, string) {
  40. switch {
  41. case strings.HasSuffix(file.Name(), ".zip"):
  42. return NewZipArchive(file), strings.TrimSuffix(file.Name(), ".zip")
  43. case strings.HasSuffix(file.Name(), ".tar.gz"):
  44. return NewTarballArchive(file), strings.TrimSuffix(file.Name(), ".tar.gz")
  45. default:
  46. return nil, ""
  47. }
  48. }
  49. // AddFile appends an existing file to an archive.
  50. func AddFile(a Archive, file string) error {
  51. fd, err := os.Open(file)
  52. if err != nil {
  53. return err
  54. }
  55. defer fd.Close()
  56. fi, err := fd.Stat()
  57. if err != nil {
  58. return err
  59. }
  60. w, err := a.Header(fi)
  61. if err != nil {
  62. return err
  63. }
  64. if _, err := io.Copy(w, fd); err != nil {
  65. return err
  66. }
  67. return nil
  68. }
  69. // WriteArchive creates an archive containing the given files.
  70. func WriteArchive(name string, files []string) (err error) {
  71. archfd, err := os.Create(name)
  72. if err != nil {
  73. return err
  74. }
  75. defer func() {
  76. archfd.Close()
  77. // Remove the half-written archive on failure.
  78. if err != nil {
  79. os.Remove(name)
  80. }
  81. }()
  82. archive, basename := NewArchive(archfd)
  83. if archive == nil {
  84. return fmt.Errorf("unknown archive extension")
  85. }
  86. fmt.Println(name)
  87. if err := archive.Directory(basename); err != nil {
  88. return err
  89. }
  90. for _, file := range files {
  91. fmt.Println(" +", filepath.Base(file))
  92. if err := AddFile(archive, file); err != nil {
  93. return err
  94. }
  95. }
  96. return archive.Close()
  97. }
  98. type ZipArchive struct {
  99. dir string
  100. zipw *zip.Writer
  101. file io.Closer
  102. }
  103. func NewZipArchive(w io.WriteCloser) Archive {
  104. return &ZipArchive{"", zip.NewWriter(w), w}
  105. }
  106. func (a *ZipArchive) Directory(name string) error {
  107. a.dir = name + "/"
  108. return nil
  109. }
  110. func (a *ZipArchive) Header(fi os.FileInfo) (io.Writer, error) {
  111. head, err := zip.FileInfoHeader(fi)
  112. if err != nil {
  113. return nil, fmt.Errorf("can't make zip header: %v", err)
  114. }
  115. head.Name = a.dir + head.Name
  116. head.Method = zip.Deflate
  117. w, err := a.zipw.CreateHeader(head)
  118. if err != nil {
  119. return nil, fmt.Errorf("can't add zip header: %v", err)
  120. }
  121. return w, nil
  122. }
  123. func (a *ZipArchive) Close() error {
  124. if err := a.zipw.Close(); err != nil {
  125. return err
  126. }
  127. return a.file.Close()
  128. }
  129. type TarballArchive struct {
  130. dir string
  131. tarw *tar.Writer
  132. gzw *gzip.Writer
  133. file io.Closer
  134. }
  135. func NewTarballArchive(w io.WriteCloser) Archive {
  136. gzw := gzip.NewWriter(w)
  137. tarw := tar.NewWriter(gzw)
  138. return &TarballArchive{"", tarw, gzw, w}
  139. }
  140. func (a *TarballArchive) Directory(name string) error {
  141. a.dir = name + "/"
  142. return a.tarw.WriteHeader(&tar.Header{
  143. Name: a.dir,
  144. Mode: 0755,
  145. Typeflag: tar.TypeDir,
  146. ModTime: time.Now(),
  147. })
  148. }
  149. func (a *TarballArchive) Header(fi os.FileInfo) (io.Writer, error) {
  150. head, err := tar.FileInfoHeader(fi, "")
  151. if err != nil {
  152. return nil, fmt.Errorf("can't make tar header: %v", err)
  153. }
  154. head.Name = a.dir + head.Name
  155. if err := a.tarw.WriteHeader(head); err != nil {
  156. return nil, fmt.Errorf("can't add tar header: %v", err)
  157. }
  158. return a.tarw, nil
  159. }
  160. func (a *TarballArchive) Close() error {
  161. if err := a.tarw.Close(); err != nil {
  162. return err
  163. }
  164. if err := a.gzw.Close(); err != nil {
  165. return err
  166. }
  167. return a.file.Close()
  168. }
  169. // ExtractArchive unpacks a .zip or .tar.gz archive to the destination directory.
  170. func ExtractArchive(archive string, dest string) error {
  171. ar, err := os.Open(archive)
  172. if err != nil {
  173. return err
  174. }
  175. defer ar.Close()
  176. switch {
  177. case strings.HasSuffix(archive, ".tar.gz"):
  178. return extractTarball(ar, dest)
  179. case strings.HasSuffix(archive, ".zip"):
  180. return extractZip(ar, dest)
  181. default:
  182. return fmt.Errorf("unhandled archive type %s", archive)
  183. }
  184. }
  185. // extractTarball unpacks a .tar.gz file.
  186. func extractTarball(ar io.Reader, dest string) error {
  187. gzr, err := gzip.NewReader(ar)
  188. if err != nil {
  189. return err
  190. }
  191. defer gzr.Close()
  192. tr := tar.NewReader(gzr)
  193. for {
  194. // Move to the next file header.
  195. header, err := tr.Next()
  196. if err != nil {
  197. if err == io.EOF {
  198. return nil
  199. }
  200. return err
  201. }
  202. // We only care about regular files, directory modes
  203. // and special file types are not supported.
  204. if header.Typeflag == tar.TypeReg {
  205. armode := header.FileInfo().Mode()
  206. err := extractFile(header.Name, armode, tr, dest)
  207. if err != nil {
  208. return fmt.Errorf("extract %s: %v", header.Name, err)
  209. }
  210. }
  211. }
  212. }
  213. // extractZip unpacks the given .zip file.
  214. func extractZip(ar *os.File, dest string) error {
  215. info, err := ar.Stat()
  216. if err != nil {
  217. return err
  218. }
  219. zr, err := zip.NewReader(ar, info.Size())
  220. if err != nil {
  221. return err
  222. }
  223. for _, zf := range zr.File {
  224. if !zf.Mode().IsRegular() {
  225. continue
  226. }
  227. data, err := zf.Open()
  228. if err != nil {
  229. return err
  230. }
  231. err = extractFile(zf.Name, zf.Mode(), data, dest)
  232. data.Close()
  233. if err != nil {
  234. return fmt.Errorf("extract %s: %v", zf.Name, err)
  235. }
  236. }
  237. return nil
  238. }
  239. // extractFile extracts a single file from an archive.
  240. func extractFile(arpath string, armode os.FileMode, data io.Reader, dest string) error {
  241. // Check that path is inside destination directory.
  242. target := filepath.Join(dest, filepath.FromSlash(arpath))
  243. if !strings.HasPrefix(target, filepath.Clean(dest)+string(os.PathSeparator)) {
  244. return fmt.Errorf("path %q escapes archive destination", target)
  245. }
  246. // Ensure the destination directory exists.
  247. if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
  248. return err
  249. }
  250. // Copy file data.
  251. file, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, armode)
  252. if err != nil {
  253. return err
  254. }
  255. if _, err := io.Copy(file, data); err != nil {
  256. file.Close()
  257. os.Remove(target)
  258. return err
  259. }
  260. return file.Close()
  261. }