Skip to content

Commit

Permalink
Remove thirdparty/tar/extractor
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: rht <rhtbot@gmail.com>
  • Loading branch information
rht committed Aug 10, 2015
1 parent d1366cd commit 75e353e
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 202 deletions.
102 changes: 40 additions & 62 deletions core/commands/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
path "github.com/ipfs/go-ipfs/path"
tar "github.com/ipfs/go-ipfs/thirdparty/tar"
uio "github.com/ipfs/go-ipfs/unixfs/io"
utar "github.com/ipfs/go-ipfs/unixfs/tar"
)
Expand Down Expand Up @@ -64,14 +63,34 @@ may also specify the level of compression by specifying '-l=<1-9>'.
res.SetError(err, cmds.ErrNormal)
return
}

p := path.Path(req.Arguments()[0])
ctx := req.Context()
dn, err := core.Resolve(ctx, node, p)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

var reader io.Reader
if archive, _, _ := req.Option("archive").Bool(); !archive && cmplvl != gzip.NoCompression {
// only use this when the flag is '-C' without '-a'
reader, err = getZip(req.Context(), node, p, cmplvl)
if archive, _, _ := req.Option("archive").Bool(); !archive {
if cmplvl != gzip.NoCompression {
// only use this when the flag is '-C' without '-a'
reader, err = utar.DagZip(ctx, dn, node.DAG, cmplvl)
} else {
outPath, _, _ := req.Option("output").String()
if len(outPath) == 0 {
_, outPath = gopath.Split(p.String())
outPath = gopath.Clean(outPath)
}

if err := utar.WriteNodeFS(ctx, dn, node.DAG, outPath); err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
return
}
} else {
reader, err = get(req.Context(), node, p, cmplvl)
reader, err = utar.DagArchive(ctx, dn, p.String(), node.DAG, cmplvl)
}
if err != nil {
res.SetError(err, cmds.ErrNormal)
Expand All @@ -80,12 +99,6 @@ may also specify the level of compression by specifying '-l=<1-9>'.
res.SetOutput(reader)
},
PostRun: func(req cmds.Request, res cmds.Response) {
if res.Output() == nil {
return
}
outReader := res.Output().(io.Reader)
res.SetOutput(nil)

outPath, _, _ := req.Option("output").String()
if len(outPath) == 0 {
_, outPath = gopath.Split(req.Arguments()[0])
Expand All @@ -99,6 +112,17 @@ may also specify the level of compression by specifying '-l=<1-9>'.
}

archive, _, _ := req.Option("archive").Bool()
if !archive && cmplvl == gzip.NoCompression {
fmt.Fprintf(os.Stdout, "Saving file(s) to %s\n", outPath)
return
}

if res.Output() == nil {
return
}

outReader := res.Output().(io.Reader)
res.SetOutput(nil)

gw := getWriter{
Out: os.Stdout,
Expand Down Expand Up @@ -135,7 +159,10 @@ func (gw *getWriter) Write(r io.Reader, fpath string) error {
if gw.Archive || gw.Compression != gzip.NoCompression {
return gw.writeArchive(r, fpath)
}
return gw.writeExtracted(r, fpath)
//bar, barR := progressBarForReader(gw.Err, r)
//bar.Start()
//defer bar.Finish()
return nil
}

func (gw *getWriter) writeArchive(r io.Reader, fpath string) error {
Expand Down Expand Up @@ -169,16 +196,6 @@ func (gw *getWriter) writeArchive(r io.Reader, fpath string) error {
return err
}

func (gw *getWriter) writeExtracted(r io.Reader, fpath string) error {
fmt.Fprintf(gw.Out, "Saving file(s) to %s\n", fpath)
bar, barR := progressBarForReader(gw.Err, r)
bar.Start()
defer bar.Finish()

extractor := &tar.Extractor{fpath}
return extractor.Extract(barR)
}

func getCompressOptions(req cmds.Request) (int, error) {
cmprs, _, _ := req.Option("compress").Bool()
cmplvl, cmplvlFound, _ := req.Option("compression-level").Int()
Expand All @@ -192,42 +209,3 @@ func getCompressOptions(req cmds.Request) (int, error) {
}
return gzip.NoCompression, nil
}

func get(ctx context.Context, node *core.IpfsNode, p path.Path, compression int) (io.Reader, error) {
dn, err := core.Resolve(ctx, node, p)
if err != nil {
return nil, err
}

return utar.DagArchive(ctx, dn, p.String(), node.DAG, compression)
}

// getZip is equivalent to `ipfs getdag $hash | gzip`
func getZip(ctx context.Context, node *core.IpfsNode, p path.Path, compression int) (io.Reader, error) {
dagnode, err := core.Resolve(ctx, node, p)
if err != nil {
return nil, err
}

reader, err := uio.NewDagReader(ctx, dagnode, node.DAG)
if err != nil {
return nil, err
}

pr, pw := io.Pipe()
gw, err := gzip.NewWriterLevel(pw, compression)
if err != nil {
return nil, err
}
bufin := bufio.NewReader(reader)
go func() {
_, err := bufin.WriteTo(gw)
if err != nil {
log.Error("Fail to compress the stream")
}
gw.Close()
pw.Close()
}()

return pr, nil
}
109 changes: 0 additions & 109 deletions thirdparty/tar/extractor.go

This file was deleted.

Loading

0 comments on commit 75e353e

Please sign in to comment.