提交 f4609845 作者: Juan Batiz-Benet

thirdparty/tar: clearer var names for recursion

License: MIT
Signed-off-by: 's avatarJuan Batiz-Benet <juan@benet.ai>
上级 b8589286
......@@ -18,14 +18,14 @@ func (te *Extractor) Extract(reader io.Reader) error {
// Check if the output path already exists, so we know whether we should
// create our output with that name, or if we should put the output inside
// a preexisting directory
exists := true
pathIsDir := false
rootExists := true
rootIsDir := false
if stat, err := os.Stat(te.Path); err != nil && os.IsNotExist(err) {
exists = false
rootExists = false
} else if err != nil {
return err
} else if stat.IsDir() {
pathIsDir = true
rootIsDir = true
}
// files come recursively in order (i == 0 is root directory)
......@@ -39,22 +39,22 @@ func (te *Extractor) Extract(reader io.Reader) error {
}
if header.Typeflag == tar.TypeDir {
if err := te.extractDir(header, i, exists); err != nil {
if err := te.extractDir(header, i, rootExists); err != nil {
return err
}
continue
}
if err := te.extractFile(header, tarReader, i, exists, pathIsDir); err != nil {
if err := te.extractFile(header, tarReader, i, rootExists, rootIsDir); err != nil {
return err
}
}
return nil
}
func (te *Extractor) extractDir(h *tar.Header, depth int, exists bool) error {
func (te *Extractor) extractDir(h *tar.Header, depth int, rootExists bool) error {
pathElements := strings.Split(h.Name, "/")
if !exists {
if !rootExists {
pathElements = pathElements[1:]
}
path := fp.Join(pathElements...)
......@@ -72,18 +72,14 @@ func (te *Extractor) extractDir(h *tar.Header, depth int, exists bool) error {
return nil
}
func (te *Extractor) extractFile(h *tar.Header, r *tar.Reader, depth int, exists bool, pathIsDir bool) error {
var path string
if depth == 0 {
func (te *Extractor) extractFile(h *tar.Header, r *tar.Reader, depth int, rootExists bool, rootIsDir bool) error {
path := te.Path
if depth == 0 && rootExists {
// if depth is 0, this is the only file (we aren't 'ipfs get'ing a directory)
switch {
case exists && !pathIsDir:
return os.ErrExist
case exists && pathIsDir:
if rootIsDir { // putting file inside of a root dir.
path = fp.Join(te.Path, h.Name)
case !exists:
path = te.Path
}
// else if the file exists, just overwrite it.
} else {
// we are outputting a directory, this file is inside of it
pathElements := strings.Split(h.Name, "/")[1:]
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论