Extract: thirdparty/tar
This module was sort of extracted to whyrusleeping/tar-utils long ago. I gxed that, imported commits from go-ipfs to the current state, and extracted it. License: MIT Signed-off-by: Hector Sanjuan <hector@protocol.ai>
Hector Sanjuan committed
Mar 21, 2018 at 18:02 UTC
5bbaa75bea7a025e24df4eb67725a6e81d7175fe
3 files changed
+7
-133
core/commands/get.go
+1
-1
@@ -13,9 +13,9 @@ import (
13
e "github.com/ipfs/go-ipfs/core/commands/e"
14
dag "github.com/ipfs/go-ipfs/merkledag"
15
path "github.com/ipfs/go-ipfs/path"
16
- tar "github.com/ipfs/go-ipfs/thirdparty/tar"
16
uarchive "github.com/ipfs/go-ipfs/unixfs/archive"
17
18
+ tar "gx/ipfs/QmWeQHAnxPZkRDUez5ANmexbHWhzfNMKN8NXimvAUZeAbZ/tar-utils"
19
"gx/ipfs/QmceUdzxkimdYsgtX733uNgzf1DLHyBKN6ehGSp85ayppM/go-ipfs-cmdkit"
20
"gx/ipfs/QmeWjRodbcZFKe5tMN7poEx3izym6osrLSnTLf9UjJZBbs/pb"
21
"gx/ipfs/QmfAkMSt9Fwzk48QDJecPcwCUjnf2uG7MLnmCGTp4C6ouL/go-ipfs-cmds"
package.json
+6
@@ -575,6 +575,12 @@
575
"hash": "QmdbxjQWogRCHRaxhhGnYdT1oQJzL9GdqSKzCdqWr85AP2",
576
"name": "pubsub",
577
"version": "1.0.0"
578
+ },
579
+ {
580
+ "author": "whyrusleeping",
581
+ "hash": "QmWeQHAnxPZkRDUez5ANmexbHWhzfNMKN8NXimvAUZeAbZ",
582
+ "name": "tar-utils",
583
+ "version": "0.0.1"
584
}
585
],
586
"gxVersion": "0.10.0",
thirdparty/tar/extractor.go
deleted
-132
@@ -1,132 +0,0 @@
1
-package tar
2
-
3
-import (
4
- "archive/tar"
5
- "fmt"
6
- "io"
7
- "os"
8
- gopath "path"
9
- fp "path/filepath"
10
- "strings"
11
-)
12
-
13
-type Extractor struct {
14
- Path string
15
- Progress func(int64) int64
16
-}
17
-
18
-func (te *Extractor) Extract(reader io.Reader) error {
19
- tarReader := tar.NewReader(reader)
20
-
21
- // Check if the output path already exists, so we know whether we should
22
- // create our output with that name, or if we should put the output inside
23
- // a preexisting directory
24
- rootExists := true
25
- rootIsDir := false
26
- if stat, err := os.Stat(te.Path); err != nil && os.IsNotExist(err) {
27
- rootExists = false
28
- } else if err != nil {
29
- return err
30
- } else if stat.IsDir() {
31
- rootIsDir = true
32
- }
33
-
34
- // files come recursively in order (i == 0 is root directory)
35
- for i := 0; ; i++ {
36
- header, err := tarReader.Next()
37
- if err != nil && err != io.EOF {
38
- return err
39
- }
40
- if header == nil || err == io.EOF {
41
- break
42
- }
43
-
44
- switch header.Typeflag {
45
- case tar.TypeDir:
46
- if err := te.extractDir(header, i); err != nil {
47
- return err
48
- }
49
- case tar.TypeReg:
50
- if err := te.extractFile(header, tarReader, i, rootExists, rootIsDir); err != nil {
51
- return err
52
- }
53
- case tar.TypeSymlink:
54
- if err := te.extractSymlink(header); err != nil {
55
- return err
56
- }
57
- default:
58
- return fmt.Errorf("unrecognized tar header type: %d", header.Typeflag)
59
- }
60
- }
61
- return nil
62
-}
63
-
64
-// outputPath returns the path at whicht o place tarPath
65
-func (te *Extractor) outputPath(tarPath string) string {
66
- elems := strings.Split(tarPath, "/") // break into elems
67
- elems = elems[1:] // remove original root
68
-
69
- path := fp.Join(elems...) // join elems
70
- path = fp.Join(te.Path, path) // rebase on extractor root
71
- return path
72
-}
73
-
74
-func (te *Extractor) extractDir(h *tar.Header, depth int) error {
75
- path := te.outputPath(h.Name)
76
-
77
- if depth == 0 {
78
- // if this is the root root directory, use it as the output path for remaining files
79
- te.Path = path
80
- }
81
-
82
- return os.MkdirAll(path, 0755)
83
-}
84
-
85
-func (te *Extractor) extractSymlink(h *tar.Header) error {
86
- return os.Symlink(h.Linkname, te.outputPath(h.Name))
87
-}
88
-
89
-func (te *Extractor) extractFile(h *tar.Header, r *tar.Reader, depth int, rootExists bool, rootIsDir bool) error {
90
- path := te.outputPath(h.Name)
91
-
92
- if depth == 0 { // if depth is 0, this is the only file (we aren't 'ipfs get'ing a directory)
93
- if rootExists && rootIsDir {
94
- // putting file inside of a root dir.
95
- fnameo := gopath.Base(h.Name)
96
- fnamen := fp.Base(path)
97
- // add back original name if lost.
98
- if fnameo != fnamen {
99
- path = fp.Join(path, fnameo)
100
- }
101
- } // else if old file exists, just overwrite it.
102
- }
103
-
104
- file, err := os.Create(path)
105
- if err != nil {
106
- return err
107
- }
108
- defer file.Close()
109
-
110
- return copyWithProgress(file, r, te.Progress)
111
-}
112
-
113
-func copyWithProgress(to io.Writer, from io.Reader, cb func(int64) int64) error {
114
- buf := make([]byte, 4096)
115
- for {
116
- n, err := from.Read(buf)
117
- if n != 0 {
118
- cb(int64(n))
119
- _, err2 := to.Write(buf[:n])
120
- if err2 != nil {
121
- return err2
122
- }
123
- }
124
- if err != nil {
125
- if err == io.EOF {
126
- return nil
127
- }
128
- return err
129
- }
130
- }
131
-
132
-}