diff options
author | Jake Champlin <jake.champlin.27@gmail.com> | 2017-06-06 12:40:07 -0400 |
---|---|---|
committer | Jake Champlin <jake.champlin.27@gmail.com> | 2017-06-06 12:40:07 -0400 |
commit | bae9f6d2fd5eb5bc80929bd393932b23f14d7c93 (patch) | |
tree | ca9ab12a7d78b1fc27a8f734729081357ce6d252 /vendor/github.com/hashicorp/go-getter/decompress_tbz2.go | |
parent | 254c495b6bebab3fb72a243c4bce858d79e6ee99 (diff) | |
download | terraform-provider-statuscake-bae9f6d2fd5eb5bc80929bd393932b23f14d7c93.tar.gz terraform-provider-statuscake-bae9f6d2fd5eb5bc80929bd393932b23f14d7c93.tar.zst terraform-provider-statuscake-bae9f6d2fd5eb5bc80929bd393932b23f14d7c93.zip |
Initial transfer of provider code
Diffstat (limited to 'vendor/github.com/hashicorp/go-getter/decompress_tbz2.go')
-rw-r--r-- | vendor/github.com/hashicorp/go-getter/decompress_tbz2.go | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/go-getter/decompress_tbz2.go b/vendor/github.com/hashicorp/go-getter/decompress_tbz2.go new file mode 100644 index 0000000..c46ed44 --- /dev/null +++ b/vendor/github.com/hashicorp/go-getter/decompress_tbz2.go | |||
@@ -0,0 +1,95 @@ | |||
1 | package getter | ||
2 | |||
3 | import ( | ||
4 | "archive/tar" | ||
5 | "compress/bzip2" | ||
6 | "fmt" | ||
7 | "io" | ||
8 | "os" | ||
9 | "path/filepath" | ||
10 | ) | ||
11 | |||
12 | // TarBzip2Decompressor is an implementation of Decompressor that can | ||
13 | // decompress tar.bz2 files. | ||
14 | type TarBzip2Decompressor struct{} | ||
15 | |||
16 | func (d *TarBzip2Decompressor) Decompress(dst, src string, dir bool) error { | ||
17 | // If we're going into a directory we should make that first | ||
18 | mkdir := dst | ||
19 | if !dir { | ||
20 | mkdir = filepath.Dir(dst) | ||
21 | } | ||
22 | if err := os.MkdirAll(mkdir, 0755); err != nil { | ||
23 | return err | ||
24 | } | ||
25 | |||
26 | // File first | ||
27 | f, err := os.Open(src) | ||
28 | if err != nil { | ||
29 | return err | ||
30 | } | ||
31 | defer f.Close() | ||
32 | |||
33 | // Bzip2 compression is second | ||
34 | bzipR := bzip2.NewReader(f) | ||
35 | |||
36 | // Once bzip decompressed we have a tar format | ||
37 | tarR := tar.NewReader(bzipR) | ||
38 | done := false | ||
39 | for { | ||
40 | hdr, err := tarR.Next() | ||
41 | if err == io.EOF { | ||
42 | if !done { | ||
43 | // Empty archive | ||
44 | return fmt.Errorf("empty archive: %s", src) | ||
45 | } | ||
46 | |||
47 | return nil | ||
48 | } | ||
49 | if err != nil { | ||
50 | return err | ||
51 | } | ||
52 | |||
53 | path := dst | ||
54 | if dir { | ||
55 | path = filepath.Join(path, hdr.Name) | ||
56 | } | ||
57 | |||
58 | if hdr.FileInfo().IsDir() { | ||
59 | if dir { | ||
60 | return fmt.Errorf("expected a single file: %s", src) | ||
61 | } | ||
62 | |||
63 | // A directory, just make the directory and continue unarchiving... | ||
64 | if err := os.MkdirAll(path, 0755); err != nil { | ||
65 | return err | ||
66 | } | ||
67 | |||
68 | continue | ||
69 | } | ||
70 | |||
71 | // We have a file. If we already decoded, then it is an error | ||
72 | if !dir && done { | ||
73 | return fmt.Errorf("expected a single file, got multiple: %s", src) | ||
74 | } | ||
75 | |||
76 | // Mark that we're done so future in single file mode errors | ||
77 | done = true | ||
78 | |||
79 | // Open the file for writing | ||
80 | dstF, err := os.Create(path) | ||
81 | if err != nil { | ||
82 | return err | ||
83 | } | ||
84 | _, err = io.Copy(dstF, tarR) | ||
85 | dstF.Close() | ||
86 | if err != nil { | ||
87 | return err | ||
88 | } | ||
89 | |||
90 | // Chmod the file | ||
91 | if err := os.Chmod(path, hdr.FileInfo().Mode()); err != nil { | ||
92 | return err | ||
93 | } | ||
94 | } | ||
95 | } | ||