From ae8a71d543cd9d2cb7a59cdeb2b35c720a7b18e1 Mon Sep 17 00:00:00 2001 From: Ivan Andreev Date: Sun, 22 Aug 2021 02:48:50 +0300 Subject: [PATCH] Debug directory listings --- debug.go | 16 ++++++++++++++++ ftp.go | 12 ++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/debug.go b/debug.go index 864566c..5973064 100644 --- a/debug.go +++ b/debug.go @@ -19,3 +19,19 @@ func newDebugWrapper(conn io.ReadWriteCloser, w io.Writer) io.ReadWriteCloser { func (w *debugWrapper) Close() error { return w.conn.Close() } + +type streamDebugWrapper struct { + io.Reader + closer io.ReadCloser +} + +func newStreamDebugWrapper(rd io.ReadCloser, w io.Writer) io.ReadCloser { + return &streamDebugWrapper{ + Reader: io.TeeReader(rd, w), + closer: rd, + } +} + +func (w *streamDebugWrapper) Close() error { + return w.closer.Close() +} diff --git a/ftp.go b/ftp.go index 0a96169..9f09422 100644 --- a/ftp.go +++ b/ftp.go @@ -251,6 +251,14 @@ func (o *dialOptions) wrapConn(netConn net.Conn) io.ReadWriteCloser { return newDebugWrapper(netConn, o.debugOutput) } +func (o *dialOptions) wrapStream(rd io.ReadCloser) io.ReadCloser { + if o.debugOutput == nil { + return rd + } + + return newStreamDebugWrapper(rd, o.debugOutput) +} + // Connect is an alias to Dial, for backward compatibility func Connect(addr string) (*ServerConn, error) { return Dial(addr) @@ -561,7 +569,7 @@ func (c *ServerConn) NameList(path string) (entries []string, err error) { } }() - scanner := bufio.NewScanner(r) + scanner := bufio.NewScanner(c.options.wrapStream(r)) for scanner.Scan() { entries = append(entries, scanner.Text()) } @@ -600,7 +608,7 @@ func (c *ServerConn) List(path string) (entries []*Entry, err error) { } }() - scanner := bufio.NewScanner(r) + scanner := bufio.NewScanner(c.options.wrapStream(r)) now := time.Now() for scanner.Scan() { entry, errParse := parser(scanner.Text(), now, c.options.location)