Skip to content

Commit

Permalink
fix(ftp): to handle ftp which not support MLST
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Jun 29, 2024
1 parent 584fbf1 commit 475a54b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
47 changes: 41 additions & 6 deletions pkg/filesystem/ftp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package ftp
import (
"context"
"crypto/tls"
"github.com/pkg/errors"
"io"
"net/textproto"
"net/url"
"time"

Expand Down Expand Up @@ -35,7 +37,9 @@ type Pool struct {
Auth *url.Userinfo
MaxConnections int32
ConnectTimeout time.Duration
TLSConfig *tls.Config

ExplicitTLS bool
TLSConfig *tls.Config

count int64
}
Expand All @@ -52,9 +56,16 @@ func (p *Pool) Conn(ctx context.Context, args ...any) (Conn, error) {
}

if p.TLSConfig != nil {
options = append(options,
ftp.DialWithTLS(p.TLSConfig),
)
if p.ExplicitTLS {
options = append(options,
ftp.DialWithExplicitTLS(p.TLSConfig),
)
} else {
options = append(options,
ftp.DialWithTLS(p.TLSConfig),
)
}

}

c, err := ftp.Dial(p.Addr, options...)
Expand All @@ -73,7 +84,9 @@ func (p *Pool) Conn(ctx context.Context, args ...any) (Conn, error) {
}
}

return &conn{conn: c}, nil
return &conn{
conn: c,
}, nil
}

type conn struct {
Expand All @@ -89,7 +102,29 @@ func (c *conn) MakeDir(path string) error {
}

func (c *conn) GetEntry(path string) (*ftp.Entry, error) {
return c.conn.GetEntry(path)
e, err := c.conn.GetEntry(path)
if err != nil {
// to handle ftp MLST not support
terr := &textproto.Error{}
if errors.As(err, &terr) {
if terr.Code == ftp.StatusNotImplemented {
list, err := c.List(path)
if err != nil {
return nil, err
}
if len(list) > 0 {
return list[0], nil
}
return nil, &textproto.Error{
Code: ftp.StatusBadFileName,
Msg: "NotFound",
}
}
}

return nil, err
}
return e, nil
}

func (c *conn) Delete(name string) error {
Expand Down
8 changes: 8 additions & 0 deletions pkg/filesystem/ftp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ func (c *Config) Conn(ctx context.Context, args ...any) (Conn, error) {
}
p.TLSConfig.InsecureSkipVerify = d
}

if t := c.Endpoint.Extra.Get("explicitTLS"); t != "" {
d, err := strconv.ParseBool(t)
if err != nil {
return nil, err
}
p.ExplicitTLS = d
}
}

c.p = p
Expand Down

0 comments on commit 475a54b

Please sign in to comment.