From 9bbd9b061d6a53faab8c8e5f52ea80888caaf2d9 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Sun, 30 Aug 2015 22:14:31 -0700 Subject: [PATCH] give ipfs get symlink support License: MIT Signed-off-by: Jeromy --- core/commands/add.go | 4 ---- thirdparty/tar/extractor.go | 23 +++++++++++++++++------ unixfs/archive/tar/writer.go | 11 +++++++++++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/core/commands/add.go b/core/commands/add.go index ecc9ad42dba..a25e8051bfe 100644 --- a/core/commands/add.go +++ b/core/commands/add.go @@ -143,7 +143,6 @@ remains to be implemented. return nil // done } - log.Errorf("FILE: %#v", file) if _, err := fileAdder.addFile(file); err != nil { return err } @@ -361,9 +360,6 @@ func (params *adder) addFile(file files.File) (*dag.Node, error) { } if s, ok := file.(*files.Symlink); ok { - log.Error("SYMLINK: ", s) - log.Error(s.Target) - log.Error(s.FileName()) dagnode := &dag.Node{ Data: ft.SymlinkData(s.Target), } diff --git a/thirdparty/tar/extractor.go b/thirdparty/tar/extractor.go index 7a526f508d2..eeeb7415ad0 100644 --- a/thirdparty/tar/extractor.go +++ b/thirdparty/tar/extractor.go @@ -2,6 +2,7 @@ package tar import ( "archive/tar" + "fmt" "io" "os" gopath "path" @@ -39,15 +40,21 @@ func (te *Extractor) Extract(reader io.Reader) error { break } - if header.Typeflag == tar.TypeDir { + switch header.Typeflag { + case tar.TypeDir: if err := te.extractDir(header, i); err != nil { return err } - continue - } - - if err := te.extractFile(header, tarReader, i, rootExists, rootIsDir); err != nil { - return err + case tar.TypeReg: + if err := te.extractFile(header, tarReader, i, rootExists, rootIsDir); err != nil { + return err + } + case tar.TypeSymlink: + if err := te.extractSymlink(header); err != nil { + return err + } + default: + return fmt.Errorf("unrecognized tar header type: %d", header.Typeflag) } } return nil @@ -79,6 +86,10 @@ func (te *Extractor) extractDir(h *tar.Header, depth int) error { return nil } +func (te *Extractor) extractSymlink(h *tar.Header) error { + return os.Symlink(h.Linkname, te.outputPath(h.Name)) +} + func (te *Extractor) extractFile(h *tar.Header, r *tar.Reader, depth int, rootExists bool, rootIsDir bool) error { path := te.outputPath(h.Name) diff --git a/unixfs/archive/tar/writer.go b/unixfs/archive/tar/writer.go index 73aeafa4b62..4953be90e7d 100644 --- a/unixfs/archive/tar/writer.go +++ b/unixfs/archive/tar/writer.go @@ -79,6 +79,8 @@ func (w *Writer) WriteNode(nd *mdag.Node, fpath string) error { fallthrough case upb.Data_File: return w.writeFile(nd, pb, fpath) + case upb.Data_Symlink: + return writeSymlinkHeader(w.TarW, string(pb.GetData()), fpath) default: return ft.ErrUnrecognizedType } @@ -108,3 +110,12 @@ func writeFileHeader(w *tar.Writer, fpath string, size uint64) error { // TODO: set mode, dates, etc. when added to unixFS }) } + +func writeSymlinkHeader(w *tar.Writer, target, fpath string) error { + return w.WriteHeader(&tar.Header{ + Name: fpath, + Linkname: target, + Mode: 0777, + Typeflag: tar.TypeSymlink, + }) +}