|
| 1 | +package unixfs |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "sort" |
| 8 | + "text/tabwriter" |
| 9 | + "time" |
| 10 | + |
| 11 | + context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" |
| 12 | + |
| 13 | + cmds "github.com/ipfs/go-ipfs/commands" |
| 14 | + core "github.com/ipfs/go-ipfs/core" |
| 15 | + path "github.com/ipfs/go-ipfs/path" |
| 16 | + unixfs "github.com/ipfs/go-ipfs/unixfs" |
| 17 | + unixfspb "github.com/ipfs/go-ipfs/unixfs/pb" |
| 18 | +) |
| 19 | + |
| 20 | +type LsLink struct { |
| 21 | + Name, Hash string |
| 22 | + Size uint64 |
| 23 | + Type string |
| 24 | +} |
| 25 | + |
| 26 | +type LsObject struct { |
| 27 | + Hash string |
| 28 | + Size uint64 |
| 29 | + Type string |
| 30 | + Links []LsLink |
| 31 | +} |
| 32 | + |
| 33 | +type LsOutput struct { |
| 34 | + Arguments map[string]string |
| 35 | + Objects map[string]*LsObject |
| 36 | +} |
| 37 | + |
| 38 | +var LsCmd = &cmds.Command{ |
| 39 | + Helptext: cmds.HelpText{ |
| 40 | + Tagline: "List directory contents for Unix-filesystem objects", |
| 41 | + ShortDescription: ` |
| 42 | +Retrieves the object named by <ipfs-or-ipns-path> and displays the |
| 43 | +contents with the following format: |
| 44 | +
|
| 45 | + <hash> <type> <size> <name> |
| 46 | +
|
| 47 | +For files, the child size is the total size of the file contents. For |
| 48 | +directories, the child size is the IPFS link size. |
| 49 | +`, |
| 50 | + }, |
| 51 | + |
| 52 | + Arguments: []cmds.Argument{ |
| 53 | + cmds.StringArg("ipfs-path", true, true, "The path to the IPFS object(s) to list links from").EnableStdin(), |
| 54 | + }, |
| 55 | + Run: func(req cmds.Request, res cmds.Response) { |
| 56 | + node, err := req.Context().GetNode() |
| 57 | + if err != nil { |
| 58 | + res.SetError(err, cmds.ErrNormal) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + paths := req.Arguments() |
| 63 | + |
| 64 | + output := LsOutput{ |
| 65 | + Arguments: map[string]string{}, |
| 66 | + Objects: map[string]*LsObject{}, |
| 67 | + } |
| 68 | + |
| 69 | + for _, fpath := range paths { |
| 70 | + ctx := req.Context().Context |
| 71 | + merkleNode, err := core.Resolve(ctx, node, path.Path(fpath)) |
| 72 | + if err != nil { |
| 73 | + res.SetError(err, cmds.ErrNormal) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + key, err := merkleNode.Key() |
| 78 | + if err != nil { |
| 79 | + res.SetError(err, cmds.ErrNormal) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + hash := key.B58String() |
| 84 | + output.Arguments[fpath] = hash |
| 85 | + |
| 86 | + if _, ok := output.Objects[hash]; ok { |
| 87 | + // duplicate argument for an already-listed node |
| 88 | + continue |
| 89 | + } |
| 90 | + |
| 91 | + unixFSNode, err := unixfs.FromBytes(merkleNode.Data) |
| 92 | + if err != nil { |
| 93 | + res.SetError(err, cmds.ErrNormal) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + t := unixFSNode.GetType() |
| 98 | + |
| 99 | + output.Objects[hash] = &LsObject{ |
| 100 | + Hash: key.String(), |
| 101 | + Type: t.String(), |
| 102 | + Size: unixFSNode.GetFilesize(), |
| 103 | + } |
| 104 | + |
| 105 | + switch t { |
| 106 | + default: |
| 107 | + res.SetError(fmt.Errorf("unrecognized type: %s", t), cmds.ErrImplementation) |
| 108 | + return |
| 109 | + case unixfspb.Data_File: |
| 110 | + break |
| 111 | + case unixfspb.Data_Directory: |
| 112 | + links := make([]LsLink, len(merkleNode.Links)) |
| 113 | + output.Objects[hash].Links = links |
| 114 | + for i, link := range merkleNode.Links { |
| 115 | + getCtx, cancel := context.WithTimeout(ctx, time.Minute) |
| 116 | + defer cancel() |
| 117 | + link.Node, err = link.GetNode(getCtx, node.DAG) |
| 118 | + if err != nil { |
| 119 | + res.SetError(err, cmds.ErrNormal) |
| 120 | + return |
| 121 | + } |
| 122 | + d, err := unixfs.FromBytes(link.Node.Data) |
| 123 | + if err != nil { |
| 124 | + res.SetError(err, cmds.ErrNormal) |
| 125 | + return |
| 126 | + } |
| 127 | + t := d.GetType() |
| 128 | + lsLink := LsLink{ |
| 129 | + Name: link.Name, |
| 130 | + Hash: link.Hash.B58String(), |
| 131 | + Type: t.String(), |
| 132 | + } |
| 133 | + if t == unixfspb.Data_File { |
| 134 | + lsLink.Size = d.GetFilesize() |
| 135 | + } else { |
| 136 | + lsLink.Size = link.Size |
| 137 | + } |
| 138 | + links[i] = lsLink |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + res.SetOutput(&output) |
| 144 | + }, |
| 145 | + Marshalers: cmds.MarshalerMap{ |
| 146 | + cmds.Text: func(res cmds.Response) (io.Reader, error) { |
| 147 | + |
| 148 | + output := res.Output().(*LsOutput) |
| 149 | + buf := new(bytes.Buffer) |
| 150 | + w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0) |
| 151 | + |
| 152 | + nonDirectories := []string{} |
| 153 | + directories := []string{} |
| 154 | + for argument, hash := range output.Arguments { |
| 155 | + object, ok := output.Objects[hash] |
| 156 | + if !ok { |
| 157 | + return nil, fmt.Errorf("unresolved hash: %s", hash) |
| 158 | + } |
| 159 | + |
| 160 | + if object.Type == "Directory" { |
| 161 | + directories = append(directories, argument) |
| 162 | + } else { |
| 163 | + nonDirectories = append(nonDirectories, argument) |
| 164 | + } |
| 165 | + } |
| 166 | + sort.Strings(nonDirectories) |
| 167 | + sort.Strings(directories) |
| 168 | + |
| 169 | + for _, argument := range nonDirectories { |
| 170 | + fmt.Fprintf(w, "%s\n", argument) |
| 171 | + } |
| 172 | + |
| 173 | + seen := map[string]bool{} |
| 174 | + for i, argument := range directories { |
| 175 | + hash := output.Arguments[argument] |
| 176 | + if _, ok := seen[hash]; ok { |
| 177 | + continue |
| 178 | + } |
| 179 | + seen[hash] = true |
| 180 | + |
| 181 | + object := output.Objects[hash] |
| 182 | + if i > 0 || len(nonDirectories) > 0 { |
| 183 | + fmt.Fprintln(w) |
| 184 | + } |
| 185 | + if len(output.Arguments) > 1 { |
| 186 | + for _, arg := range directories[i:] { |
| 187 | + if output.Arguments[arg] == hash { |
| 188 | + fmt.Fprintf(w, "%s:\n", arg) |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + for _, link := range object.Links { |
| 193 | + fmt.Fprintf(w, "%s\n", link.Name) |
| 194 | + } |
| 195 | + } |
| 196 | + w.Flush() |
| 197 | + |
| 198 | + return buf, nil |
| 199 | + }, |
| 200 | + }, |
| 201 | + Type: LsOutput{}, |
| 202 | +} |
0 commit comments