From 06706f26253d11db9561e01a59c75961246c678c Mon Sep 17 00:00:00 2001 From: jawr Date: Mon, 15 Jun 2020 08:36:46 +0900 Subject: [PATCH 1/2] Rework walker.Next to (hopefully) be clearer. Fix issues where it wouldn't correctly walk a directory with one file. Also prevent it trimming leading '/' from root. --- walker.go | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/walker.go b/walker.go index 5f16519..e6a0f3f 100644 --- a/walker.go +++ b/walker.go @@ -1,10 +1,7 @@ package ftp import ( - "fmt" - "os" "path" - "strings" ) //Walker traverses the directory tree of a remote FTP server @@ -26,38 +23,27 @@ type item struct { // 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. func (w *Walker) Next() bool { - var isRoot bool + // check if we need to init cur, maybe this should be inside Walk if w.cur == nil { w.cur = &item{ - path: strings.Trim(w.root, string(os.PathSeparator)), + path: w.root, + entry: &Entry{ + Type: EntryTypeFolder, + }, } - - isRoot = true } - entries, err := w.serverConn.List(w.cur.path) - w.cur.err = err - if err == nil { - if len(entries) == 0 { - w.cur.err = fmt.Errorf("no such file or directory: %s", w.cur.path) + if w.descend && w.cur.entry.Type == EntryTypeFolder { + entries, err := w.serverConn.List(w.cur.path) + // an error occured, drop out and stop walking + if err != nil { + w.cur.err = err return false } - if isRoot && len(entries) == 1 && entries[0].Type == EntryTypeFile { - w.cur.err = fmt.Errorf("root is not a directory: %s", w.cur.path) - - return false - } - - 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 { + for _, entry := range entries { + if entry.Name == "." || entry.Name == ".." { continue } @@ -74,9 +60,12 @@ func (w *Walker) Next() bool { return false } + // update cur i := len(w.stack) - 1 w.cur = w.stack[i] w.stack = w.stack[:i] + + // reset SkipDir w.descend = true return true From 226f00fd00848f692e3767394ce0f148e191caf3 Mon Sep 17 00:00:00 2001 From: jawr Date: Mon, 15 Jun 2020 09:53:47 +0900 Subject: [PATCH 2/2] try and improve coveralls --- walker_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/walker_test.go b/walker_test.go index d557553..9cf6276 100644 --- a/walker_test.go +++ b/walker_test.go @@ -187,3 +187,26 @@ func TestCurAndStackSetCorrectly(t *testing.T) { assert.Equal(t, 0, len(w.stack)) assert.Equal(t, "file", w.cur.entry.Name) } + +func TestCurInit(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 := c.Walk("/root") + + result := w.Next() + + // mock fs has one file 'lo' + + assert.Equal(t, true, result, "Result should return false") + assert.Equal(t, 0, len(w.stack)) + assert.Equal(t, "/root/lo", w.Path()) +}