ls.go 3.63 KB
Newer Older
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
package unixfs

import (
	"bytes"
	"fmt"
	"io"
	"text/tabwriter"
	"time"

	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"

	cmds "github.com/ipfs/go-ipfs/commands"
	core "github.com/ipfs/go-ipfs/core"
	path "github.com/ipfs/go-ipfs/path"
	unixfs "github.com/ipfs/go-ipfs/unixfs"
	unixfspb "github.com/ipfs/go-ipfs/unixfs/pb"
)

type LsLink struct {
	Name, Hash string
	Size       uint64
	Type       unixfspb.Data_DataType
}

type LsObject struct {
	Argument string
	Links    []LsLink
}

type LsOutput struct {
	Objects []*LsObject
}

var LsCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "List directory contents for Unix-filesystem objects",
		ShortDescription: `
Retrieves the object named by <ipfs-or-ipns-path> and displays the
contents with the following format:

  <hash> <type> <size> <name>

For files, the child size is the total size of the file contents.  For
directories, the child size is the IPFS link size.
`,
	},

	Arguments: []cmds.Argument{
		cmds.StringArg("ipfs-path", true, true, "The path to the IPFS object(s) to list links from").EnableStdin(),
	},
	Run: func(req cmds.Request, res cmds.Response) {
		node, err := req.Context().GetNode()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		paths := req.Arguments()

		output := make([]*LsObject, len(paths))
		for i, fpath := range paths {
62 63
			ctx := req.Context().Context
			merkleNode, err := core.Resolve(ctx, node, path.Path(fpath))
64 65 66 67 68
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}

69 70 71 72
			unixFSNode, err := unixfs.FromBytes(merkleNode.Data)
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
73
			}
74 75 76 77 78 79 80 81 82 83

			output[i] = &LsObject{}

			t := unixFSNode.GetType()
			switch t {
			default:
				res.SetError(fmt.Errorf("unrecognized type: %s", t), cmds.ErrImplementation)
				return
			case unixfspb.Data_File:
				key, err := merkleNode.Key()
84 85 86 87
				if err != nil {
					res.SetError(err, cmds.ErrNormal)
					return
				}
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 119 120
				output[i].Links = []LsLink{LsLink{
					Name: fpath,
					Hash: key.String(),
					Type: t,
					Size: unixFSNode.GetFilesize(),
				}}
			case unixfspb.Data_Directory:
				output[i].Argument = fpath
				output[i].Links = make([]LsLink, len(merkleNode.Links))
				for j, link := range merkleNode.Links {
					getCtx, cancel := context.WithTimeout(context.TODO(), time.Minute)
					defer cancel()
					link.Node, err = link.GetNode(getCtx, node.DAG)
					if err != nil {
						res.SetError(err, cmds.ErrNormal)
						return
					}
					d, err := unixfs.FromBytes(link.Node.Data)
					if err != nil {
						res.SetError(err, cmds.ErrNormal)
						return
					}
					lsLink := LsLink{
						Name: link.Name,
						Hash: link.Hash.B58String(),
						Type: d.GetType(),
					}
					if lsLink.Type == unixfspb.Data_File {
						lsLink.Size = d.GetFilesize()
					} else {
						lsLink.Size = link.Size
					}
					output[i].Links[j] = lsLink
121 122 123 124 125 126 127 128 129 130 131 132
				}
			}
		}

		res.SetOutput(&LsOutput{Objects: output})
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {

			output := res.Output().(*LsOutput)
			buf := new(bytes.Buffer)
			w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
133 134 135 136 137 138
			lastObjectDirHeader := false
			for i, object := range output.Objects {
				if len(output.Objects) > 1 && object.Argument != "" {
					if i > 0 {
						fmt.Fprintln(w)
					}
139
					fmt.Fprintf(w, "%s:\n", object.Argument)
140 141 142 143 144 145
					lastObjectDirHeader = true
				} else {
					if lastObjectDirHeader {
						fmt.Fprintln(w)
					}
					lastObjectDirHeader = false
146 147 148 149 150 151 152 153 154 155 156 157
				}
				for _, link := range object.Links {
					fmt.Fprintf(w, "%s\n", link.Name)
				}
			}
			w.Flush()

			return buf, nil
		},
	},
	Type: LsOutput{},
}