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