forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tar.go
118 lines (101 loc) · 2.49 KB
/
tar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package commands
import (
"io"
"strings"
cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreunix"
dag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"
tar "github.com/ipfs/go-ipfs/tar"
)
var TarCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Utility functions for tar files in ipfs.",
},
Subcommands: map[string]*cmds.Command{
"add": tarAddCmd,
"cat": tarCatCmd,
},
}
var tarAddCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Import a tar file into ipfs.",
ShortDescription: `
'ipfs tar add' will parse a tar file and create a merkledag structure to
represent it.
`,
},
Arguments: []cmds.Argument{
cmds.FileArg("file", true, false, "Tar file to add.").EnableStdin(),
},
Run: func(req cmds.Request, res cmds.Response) {
nd, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
fi, err := req.Files().NextFile()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
node, err := tar.ImportTar(fi, nd.DAG)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
c := node.Cid()
fi.FileName()
res.SetOutput(&coreunix.AddedObject{
Name: fi.FileName(),
Hash: c.String(),
})
},
Type: coreunix.AddedObject{},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
o := res.Output().(*coreunix.AddedObject)
return strings.NewReader(o.Hash + "\n"), nil
},
},
}
var tarCatCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Export a tar file from IPFS.",
ShortDescription: `
'ipfs tar cat' will export a tar file from a previously imported one in IPFS.
`,
},
Arguments: []cmds.Argument{
cmds.StringArg("path", true, false, "ipfs path of archive to export.").EnableStdin(),
},
Run: func(req cmds.Request, res cmds.Response) {
nd, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
p, err := path.ParsePath(req.Arguments()[0])
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
root, err := core.Resolve(req.Context(), nd.Namesys, nd.Resolver, p)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
rootpb, ok := root.(*dag.ProtoNode)
if !ok {
res.SetError(dag.ErrNotProtobuf, cmds.ErrNormal)
return
}
r, err := tar.ExportTar(req.Context(), rootpb, nd.DAG)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(r)
},
}