open-nomad/vendor/github.com/hashicorp/go-getter/decompress.go

60 lines
1.8 KiB
Go
Raw Normal View History

2016-02-16 02:22:51 +00:00
package getter
2018-04-09 20:34:38 +00:00
import (
"os"
2018-04-09 20:34:38 +00:00
"strings"
)
2016-02-16 02:22:51 +00:00
// Decompressor defines the interface that must be implemented to add
// support for decompressing a type.
2018-04-09 20:34:38 +00:00
//
// Important: if you're implementing a decompressor, please use the
// containsDotDot helper in this file to ensure that files can't be
// decompressed outside of the specified directory.
2016-02-16 02:22:51 +00:00
type Decompressor interface {
// Decompress should decompress src to dst. dir specifies whether dst
// is a directory or single file. src is guaranteed to be a single file
// that exists. dst is not guaranteed to exist already.
Decompress(dst, src string, dir bool, umask os.FileMode) error
2016-02-16 02:22:51 +00:00
}
// Decompressors is the mapping of extension to the Decompressor implementation
// that will decompress that extension/type.
var Decompressors map[string]Decompressor
func init() {
tbzDecompressor := new(TarBzip2Decompressor)
tgzDecompressor := new(TarGzipDecompressor)
2017-07-14 17:31:21 +00:00
txzDecompressor := new(TarXzDecompressor)
2016-02-16 02:22:51 +00:00
Decompressors = map[string]Decompressor{
"bz2": new(Bzip2Decompressor),
"gz": new(GzipDecompressor),
2017-07-14 17:31:21 +00:00
"xz": new(XzDecompressor),
2016-02-16 02:22:51 +00:00
"tar.bz2": tbzDecompressor,
"tar.gz": tgzDecompressor,
2017-07-14 17:31:21 +00:00
"tar.xz": txzDecompressor,
2016-02-16 02:22:51 +00:00
"tbz2": tbzDecompressor,
"tgz": tgzDecompressor,
2017-07-14 17:31:21 +00:00
"txz": txzDecompressor,
2016-02-16 02:22:51 +00:00
"zip": new(ZipDecompressor),
}
}
2018-04-09 20:34:38 +00:00
// containsDotDot checks if the filepath value v contains a ".." entry.
// This will check filepath components by splitting along / or \. This
// function is copied directly from the Go net/http implementation.
func containsDotDot(v string) bool {
if !strings.Contains(v, "..") {
return false
}
for _, ent := range strings.FieldsFunc(v, isSlashRune) {
if ent == ".." {
return true
}
}
return false
}
func isSlashRune(r rune) bool { return r == '/' || r == '\\' }