commit
5563613968
1
ftp.go
1
ftp.go
@ -760,6 +760,7 @@ func (c *ServerConn) Walk(root string) *Walker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
w.root = root
|
w.root = root
|
||||||
|
w.descend = true
|
||||||
|
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
63
walker.go
63
walker.go
@ -1,7 +1,9 @@
|
|||||||
package ftp
|
package ftp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
pa "path"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -9,14 +11,14 @@ import (
|
|||||||
type Walker struct {
|
type Walker struct {
|
||||||
serverConn *ServerConn
|
serverConn *ServerConn
|
||||||
root string
|
root string
|
||||||
cur item
|
cur *item
|
||||||
stack []item
|
stack []*item
|
||||||
descend bool
|
descend bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type item struct {
|
type item struct {
|
||||||
path string
|
path string
|
||||||
entry Entry
|
entry *Entry
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,36 +26,59 @@ type item struct {
|
|||||||
// which will then be available through the Path, Stat, and Err methods.
|
// which will then be available through the Path, Stat, and Err methods.
|
||||||
// It returns false when the walk stops at the end of the tree.
|
// It returns false when the walk stops at the end of the tree.
|
||||||
func (w *Walker) Next() bool {
|
func (w *Walker) Next() bool {
|
||||||
if w.descend && w.cur.err == nil && w.cur.entry.Type == EntryTypeFolder {
|
var isRoot bool
|
||||||
list, err := w.serverConn.List(w.cur.path)
|
if w.cur == nil {
|
||||||
if err != nil {
|
w.cur = &item{
|
||||||
|
path: strings.Trim(w.root, string(os.PathSeparator)),
|
||||||
|
}
|
||||||
|
|
||||||
|
isRoot = true
|
||||||
|
}
|
||||||
|
|
||||||
|
entries, err := w.serverConn.List(w.cur.path)
|
||||||
w.cur.err = err
|
w.cur.err = err
|
||||||
w.stack = append(w.stack, w.cur)
|
if err == nil {
|
||||||
} else {
|
if len(entries) == 0 {
|
||||||
for i := len(list) - 1; i >= 0; i-- {
|
w.cur.err = fmt.Errorf("no such file or directory: %s", w.cur.path)
|
||||||
if !strings.HasSuffix(w.cur.path, "/") {
|
|
||||||
w.cur.path += "/"
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
var path string
|
if isRoot && len(entries) == 1 && entries[0].Type == EntryTypeFile {
|
||||||
if list[i].Type == EntryTypeFolder {
|
w.cur.err = fmt.Errorf("root is not a directory: %s", w.cur.path)
|
||||||
path = pa.Join(w.cur.path, list[i].Name)
|
|
||||||
} else {
|
return false
|
||||||
path = w.cur.path
|
|
||||||
}
|
}
|
||||||
|
|
||||||
w.stack = append(w.stack, item{path, *list[i], nil})
|
for i, entry := range entries {
|
||||||
|
if entry.Name == "." || (i == 0 && entry.Type == EntryTypeFile) {
|
||||||
|
entry.Name = path.Base(w.cur.path)
|
||||||
|
w.cur.entry = entry
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if entry.Name == ".." || !w.descend {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
item := &item{
|
||||||
|
path: path.Join(w.cur.path, entry.Name),
|
||||||
|
entry: entry,
|
||||||
|
}
|
||||||
|
|
||||||
|
w.stack = append(w.stack, item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(w.stack) == 0 {
|
if len(w.stack) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
i := len(w.stack) - 1
|
i := len(w.stack) - 1
|
||||||
w.cur = w.stack[i]
|
w.cur = w.stack[i]
|
||||||
w.stack = w.stack[:i]
|
w.stack = w.stack[:i]
|
||||||
w.descend = true
|
w.descend = true
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +96,7 @@ func (w *Walker) Err() error {
|
|||||||
|
|
||||||
// Stat returns info for the most recent file or directory
|
// Stat returns info for the most recent file or directory
|
||||||
// visited by a call to Step.
|
// visited by a call to Step.
|
||||||
func (w *Walker) Stat() Entry {
|
func (w *Walker) Stat() *Entry {
|
||||||
return w.cur.entry
|
return w.cur.entry
|
||||||
}
|
}
|
||||||
|
|
||||||
|
114
walker_test.go
114
walker_test.go
@ -2,7 +2,6 @@ package ftp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -29,10 +28,10 @@ func TestWalkReturnsCorrectlyPopulatedWalker(t *testing.T) {
|
|||||||
|
|
||||||
func TestFieldsReturnCorrectData(t *testing.T) {
|
func TestFieldsReturnCorrectData(t *testing.T) {
|
||||||
w := Walker{
|
w := Walker{
|
||||||
cur: item{
|
cur: &item{
|
||||||
path: "/root/",
|
path: "/root/",
|
||||||
err: fmt.Errorf("This is an error"),
|
err: fmt.Errorf("this is an error"),
|
||||||
entry: Entry{
|
entry: &Entry{
|
||||||
Name: "root",
|
Name: "root",
|
||||||
Size: 123,
|
Size: 123,
|
||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
@ -41,7 +40,7 @@ func TestFieldsReturnCorrectData(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, "This is an error", w.Err().Error())
|
assert.Equal(t, "this is an error", w.Err().Error())
|
||||||
assert.Equal(t, "/root/", w.Path())
|
assert.Equal(t, "/root/", w.Path())
|
||||||
assert.Equal(t, EntryTypeFolder, w.Stat().Type)
|
assert.Equal(t, EntryTypeFolder, w.Stat().Type)
|
||||||
}
|
}
|
||||||
@ -55,11 +54,22 @@ func TestSkipDirIsCorrectlySet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNoDescendDoesNotAddToStack(t *testing.T) {
|
func TestNoDescendDoesNotAddToStack(t *testing.T) {
|
||||||
w := new(Walker)
|
mock, err := newFtpMock(t, "127.0.0.1")
|
||||||
w.cur = item{
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer mock.Close()
|
||||||
|
|
||||||
|
c, cErr := Connect(mock.Addr())
|
||||||
|
if cErr != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w := c.Walk("/root")
|
||||||
|
w.cur = &item{
|
||||||
path: "/root/",
|
path: "/root/",
|
||||||
err: nil,
|
err: nil,
|
||||||
entry: Entry{
|
entry: &Entry{
|
||||||
Name: "root",
|
Name: "root",
|
||||||
Size: 123,
|
Size: 123,
|
||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
@ -67,11 +77,11 @@ func TestNoDescendDoesNotAddToStack(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
w.stack = []item{
|
w.stack = []*item{
|
||||||
item{
|
&item{
|
||||||
path: "file",
|
path: "file",
|
||||||
err: nil,
|
err: nil,
|
||||||
entry: Entry{
|
entry: &Entry{
|
||||||
Name: "file",
|
Name: "file",
|
||||||
Size: 123,
|
Size: 123,
|
||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
@ -90,11 +100,23 @@ func TestNoDescendDoesNotAddToStack(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEmptyStackReturnsFalse(t *testing.T) {
|
func TestEmptyStackReturnsFalse(t *testing.T) {
|
||||||
w := new(Walker)
|
mock, err := newFtpMock(t, "127.0.0.1")
|
||||||
w.cur = item{
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer mock.Close()
|
||||||
|
|
||||||
|
c, cErr := Connect(mock.Addr())
|
||||||
|
if cErr != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w := c.Walk("/root")
|
||||||
|
|
||||||
|
w.cur = &item{
|
||||||
path: "/root/",
|
path: "/root/",
|
||||||
err: nil,
|
err: nil,
|
||||||
entry: Entry{
|
entry: &Entry{
|
||||||
Name: "root",
|
Name: "root",
|
||||||
Size: 123,
|
Size: 123,
|
||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
@ -102,7 +124,7 @@ func TestEmptyStackReturnsFalse(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
w.stack = []item{}
|
w.stack = []*item{}
|
||||||
|
|
||||||
w.SkipDir()
|
w.SkipDir()
|
||||||
|
|
||||||
@ -112,11 +134,22 @@ func TestEmptyStackReturnsFalse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCurAndStackSetCorrectly(t *testing.T) {
|
func TestCurAndStackSetCorrectly(t *testing.T) {
|
||||||
w := new(Walker)
|
mock, err := newFtpMock(t, "127.0.0.1")
|
||||||
w.cur = item{
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer mock.Close()
|
||||||
|
|
||||||
|
c, cErr := Connect(mock.Addr())
|
||||||
|
if cErr != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w := c.Walk("/root")
|
||||||
|
w.cur = &item{
|
||||||
path: "root/file1",
|
path: "root/file1",
|
||||||
err: nil,
|
err: nil,
|
||||||
entry: Entry{
|
entry: &Entry{
|
||||||
Name: "file1",
|
Name: "file1",
|
||||||
Size: 123,
|
Size: 123,
|
||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
@ -124,21 +157,21 @@ func TestCurAndStackSetCorrectly(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
w.stack = []item{
|
w.stack = []*item{
|
||||||
item{
|
&item{
|
||||||
path: "file",
|
path: "file",
|
||||||
err: nil,
|
err: nil,
|
||||||
entry: Entry{
|
entry: &Entry{
|
||||||
Name: "file",
|
Name: "file",
|
||||||
Size: 123,
|
Size: 123,
|
||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
Type: EntryTypeFile,
|
Type: EntryTypeFile,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
item{
|
&item{
|
||||||
path: "root/file1",
|
path: "root/file1",
|
||||||
err: nil,
|
err: nil,
|
||||||
entry: Entry{
|
entry: &Entry{
|
||||||
Name: "file1",
|
Name: "file1",
|
||||||
Size: 123,
|
Size: 123,
|
||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
@ -154,38 +187,3 @@ func TestCurAndStackSetCorrectly(t *testing.T) {
|
|||||||
assert.Equal(t, 0, len(w.stack))
|
assert.Equal(t, 0, len(w.stack))
|
||||||
assert.Equal(t, "file", w.cur.entry.Name)
|
assert.Equal(t, "file", w.cur.entry.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStackIsPopulatedCorrectly(t *testing.T) {
|
|
||||||
|
|
||||||
mock, err := newFtpMock(t, "127.0.0.1")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer mock.Close()
|
|
||||||
|
|
||||||
c, cErr := Connect(mock.Addr())
|
|
||||||
if cErr != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
w := Walker{
|
|
||||||
cur: item{
|
|
||||||
path: "/root",
|
|
||||||
entry: Entry{
|
|
||||||
Name: "root",
|
|
||||||
Size: 123,
|
|
||||||
Time: time.Now(),
|
|
||||||
Type: EntryTypeFolder,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
serverConn: c,
|
|
||||||
}
|
|
||||||
|
|
||||||
w.descend = true
|
|
||||||
|
|
||||||
w.Next()
|
|
||||||
|
|
||||||
assert.Equal(t, 0, len(w.stack))
|
|
||||||
assert.Equal(t, "lo", w.cur.entry.Name)
|
|
||||||
assert.Equal(t, true, strings.HasSuffix(w.cur.path, "/"))
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user