Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0580639

Browse files
committedAug 9, 2015
code cleanup and comments
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent 643400a commit 0580639

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed
 

‎core/commands/mfs.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,12 @@ var MfsLsCmd = &cmds.Command{
243243
Tagline: "List directories inside a filesystem",
244244
ShortDescription: ``,
245245
},
246-
247246
Arguments: []cmds.Argument{
248247
cmds.StringArg("path", true, false, "path to show listing for"),
249248
},
249+
Options: []cmds.Option{
250+
cmds.BoolOption("l", "use long listing format"),
251+
},
250252
Run: func(req cmds.Request, res cmds.Response) {
251253
node, err := req.InvocContext().GetNode()
252254
if err != nil {
@@ -323,8 +325,14 @@ var MfsLsCmd = &cmds.Command{
323325
cmds.Text: func(res cmds.Response) (io.Reader, error) {
324326
out := res.Output().(*MfsLsOutput)
325327
buf := new(bytes.Buffer)
328+
long, _, _ := res.Request().Option("l").Bool()
329+
326330
for _, o := range out.Entries {
327-
fmt.Fprintf(buf, "%s\n", o.Name)
331+
if long {
332+
fmt.Fprintf(buf, "%s\t%s\t%d\n", o.Name, o.Hash, o.Size)
333+
} else {
334+
fmt.Fprintf(buf, "%s\n", o.Name)
335+
}
328336
}
329337
return buf, nil
330338
},

‎mfs/dir.go

+2-27
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"os"
7-
"strings"
87
"sync"
98
"time"
109

@@ -17,6 +16,7 @@ import (
1716

1817
var ErrNotYetImplemented = errors.New("not yet implemented")
1918
var ErrInvalidChild = errors.New("invalid child node")
19+
var ErrDirExists = errors.New("directory already has entry by that name")
2020

2121
type Directory struct {
2222
fs *Filesystem
@@ -277,7 +277,7 @@ func (d *Directory) AddChild(name string, nd *dag.Node) error {
277277

278278
_, err = d.childUnsync(name)
279279
if err == nil {
280-
return errors.New("directory already has entry by that name")
280+
return ErrDirExists
281281
}
282282

283283
err = d.node.AddNodeLinkClean(name, nd)
@@ -311,28 +311,3 @@ func (d *Directory) Lock() {
311311
func (d *Directory) Unlock() {
312312
d.lock.Unlock()
313313
}
314-
315-
func DirLookup(d *Directory, path string) (FSNode, error) {
316-
path = strings.Trim(path, "/")
317-
parts := strings.Split(path, "/")
318-
if len(parts) == 1 && parts[0] == "" {
319-
return d, nil
320-
}
321-
322-
var cur FSNode
323-
cur = d
324-
for i, p := range parts {
325-
chdir, ok := cur.(*Directory)
326-
if !ok {
327-
return nil, fmt.Errorf("cannot access %s: Not a directory", strings.Join(parts[:i+1], "/"))
328-
}
329-
330-
child, err := chdir.Child(p)
331-
if err != nil {
332-
return nil, err
333-
}
334-
335-
cur = child
336-
}
337-
return cur, nil
338-
}

‎mfs/ops.go

+31
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
dag "github.com/ipfs/go-ipfs/merkledag"
1111
)
1212

13+
// Mv moves the file or directory at 'src' to 'dst'
1314
func Mv(rootd *Directory, src, dst string) error {
1415
srcDir, srcFname := gopath.Split(src)
1516

@@ -57,6 +58,7 @@ func Mv(rootd *Directory, src, dst string) error {
5758
return nil
5859
}
5960

61+
// PutNodeUnderRoot inserts 'nd' at 'ipath' under the given filesystem root
6062
func PutNodeUnderRoot(root *Root, ipath string, nd *dag.Node) error {
6163
dir, ok := root.GetValue().(*Directory)
6264
if !ok {
@@ -77,6 +79,8 @@ func PutNodeUnderRoot(root *Root, ipath string, nd *dag.Node) error {
7779
return pdir.AddChild(filename, nd)
7880
}
7981

82+
// Mkdir creates a directory at 'path' under the directory 'd', creating
83+
// intermediary directories as needed if 'parents' is set to true
8084
func Mkdir(rootd *Directory, path string, parents bool) error {
8185
parts := strings.Split(path, "/")
8286
if parts[0] == "" {
@@ -112,3 +116,30 @@ func Mkdir(rootd *Directory, path string, parents bool) error {
112116

113117
return nil
114118
}
119+
120+
// DirLookup will look up a file or directory at the given path
121+
// under the directory 'd'
122+
func DirLookup(d *Directory, path string) (FSNode, error) {
123+
path = strings.Trim(path, "/")
124+
parts := strings.Split(path, "/")
125+
if len(parts) == 1 && parts[0] == "" {
126+
return d, nil
127+
}
128+
129+
var cur FSNode
130+
cur = d
131+
for i, p := range parts {
132+
chdir, ok := cur.(*Directory)
133+
if !ok {
134+
return nil, fmt.Errorf("cannot access %s: Not a directory", strings.Join(parts[:i+1], "/"))
135+
}
136+
137+
child, err := chdir.Child(p)
138+
if err != nil {
139+
return nil, err
140+
}
141+
142+
cur = child
143+
}
144+
return cur, nil
145+
}

0 commit comments

Comments
 (0)
Please sign in to comment.