Skip to content

Commit

Permalink
Fixed all lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fclairamb committed Mar 2, 2024
1 parent e202f9e commit 4e934cb
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 41 deletions.
4 changes: 2 additions & 2 deletions client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (c *clientHandler) Close() error {
// closing the connection from a different goroutine should be safe
err := c.conn.Close()
if err != nil {
err = NewNetworkError("error closing control connection", err)
err = newNetworkError("error closing control connection", err)
}

return err
Expand Down Expand Up @@ -650,7 +650,7 @@ func (c *clientHandler) TransferOpen(info string) (net.Conn, error) {

c.writeMessage(StatusCannotOpenDataConnection, err.Error())

err = NewNetworkError("Unable to open transfer", err)
err = newNetworkError("Unable to open transfer", err)

return nil, err
}
Expand Down
8 changes: 5 additions & 3 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type DriverError struct {
err error
}

func NewDriverError(str string, err error) DriverError {
func newDriverError(str string, err error) DriverError {
return DriverError{str: str, err: err}
}

Expand All @@ -43,12 +43,13 @@ func (e DriverError) Unwrap() error {
return e.err
}

// NetworkError is a wrapper for any error that occur while contacting the network
type NetworkError struct {
str string
err error
}

func NewNetworkError(str string, err error) NetworkError {
func newNetworkError(str string, err error) NetworkError {
return NetworkError{str: str, err: err}
}

Expand All @@ -60,12 +61,13 @@ func (e NetworkError) Unwrap() error {
return e.err
}

// FileAccessError is a wrapper for any error that occur while accessing the file system
type FileAccessError struct {
str string
err error
}

func NewFileAccessError(str string, err error) FileAccessError {
func newFileAccessError(str string, err error) FileAccessError {
return FileAccessError{str: str, err: err}
}

Expand Down
11 changes: 7 additions & 4 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ func TestTransferCloseStorageExceeded(t *testing.T) {
}

func TestErrorTypes(t *testing.T) {
a := assert.New(t)
// a := assert.New(t)
t.Run("DriverError", func(t *testing.T) {
var err error = NewDriverError("test", os.ErrPermission)
a := assert.New(t)
var err error = newDriverError("test", os.ErrPermission)
a.Equal("driver error: test: permission denied", err.Error())
a.ErrorIs(err, os.ErrPermission)

Expand All @@ -41,7 +42,8 @@ func TestErrorTypes(t *testing.T) {
})

t.Run("NetworkError", func(t *testing.T) {
var err error = NewNetworkError("test", os.ErrPermission)
a := assert.New(t)
var err error = newNetworkError("test", os.ErrPermission)
a.Equal("network error: test: permission denied", err.Error())
a.ErrorIs(err, os.ErrPermission)

Expand All @@ -51,7 +53,8 @@ func TestErrorTypes(t *testing.T) {
})

t.Run("FileAccessError", func(t *testing.T) {
var err error = NewFileAccessError("test", os.ErrPermission)
a := assert.New(t)
var err error = newFileAccessError("test", os.ErrPermission)
a.Equal("file access error: test: permission denied", err.Error())
a.ErrorIs(err, os.ErrPermission)
var specificError FileAccessError
Expand Down
12 changes: 6 additions & 6 deletions handle_dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (c *clientHandler) dirTransferNLST(w io.Writer, files []os.FileInfo, parent
_, err := w.Write([]byte(""))

if err != nil {
err = NewNetworkError("couldn't send NLST data", err)
err = newNetworkError("couldn't send NLST data", err)
}

return err
Expand All @@ -239,7 +239,7 @@ func (c *clientHandler) dirTransferNLST(w io.Writer, files []os.FileInfo, parent
// by a program to further process the files automatically.
// So we return paths relative to the current working directory
if _, err := fmt.Fprintf(w, "%s\r\n", path.Join(c.getRelativePath(parentDir), file.Name())); err != nil {
return NewNetworkError("couldn't send NLST data", err)
return newNetworkError("couldn't send NLST data", err)
}
}

Expand Down Expand Up @@ -304,7 +304,7 @@ func (c *clientHandler) dirTransferLIST(w io.Writer, files []os.FileInfo) error
_, err := w.Write([]byte(""))

if err != nil {
err = NewNetworkError("error writing LIST entry", err)
err = newNetworkError("error writing LIST entry", err)
}

return err
Expand All @@ -325,7 +325,7 @@ func (c *clientHandler) dirTransferMLSD(w io.Writer, files []os.FileInfo) error
_, err := w.Write([]byte(""))

if err != nil {
err = NewNetworkError("error writing MLSD entry", err)
err = newNetworkError("error writing MLSD entry", err)
}

return err
Expand Down Expand Up @@ -374,7 +374,7 @@ func (c *clientHandler) getFileList(param string, filePathAllowed bool) ([]os.Fi
// return list of single file if directoryPath points to file and filePathAllowed
info, err := c.driver.Stat(listPath)
if err != nil {
return nil, "", NewFileAccessError("couldn't stat", err)
return nil, "", newFileAccessError("couldn't stat", err)
}

if !info.IsDir() {
Expand All @@ -395,7 +395,7 @@ func (c *clientHandler) getFileList(param string, filePathAllowed bool) ([]os.Fi

directory, errOpenFile := c.driver.Open(listPath)
if errOpenFile != nil {
return nil, "", NewFileAccessError("couldn't open directory", errOpenFile)
return nil, "", newFileAccessError("couldn't open directory", errOpenFile)
}

defer c.closeDirectory(listPath, directory)
Expand Down
10 changes: 5 additions & 5 deletions handle_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (c *clientHandler) doFileTransfer(tr net.Conn, file io.ReadWriter, write bo
fileTransferError.TransferError(err)
}

err = NewNetworkError("error transferring data", err)
err = newNetworkError("error transferring data", err)
}

return err
Expand Down Expand Up @@ -700,14 +700,14 @@ func (c *clientHandler) computeHashForFile(filePath string, algo HASHAlgo, start
if start > 0 {
_, err = file.Seek(start, io.SeekStart)
if err != nil {
return "", NewFileAccessError("couldn't seek file", err)
return "", newFileAccessError("couldn't seek file", err)
}
}

_, err = io.CopyN(h, file, end-start)

if err != nil && err != io.EOF {
return "", NewFileAccessError("couldn't read file", err)
return "", newFileAccessError("couldn't read file", err)
}

return hex.EncodeToString(h.Sum(nil)), nil
Expand All @@ -717,7 +717,7 @@ func (c *clientHandler) getFileHandle(name string, flags int, offset int64) (Fil
if fileTransfer, ok := c.driver.(ClientDriverExtentionFileTransfer); ok {
ft, err := fileTransfer.GetHandle(name, flags, offset)
if err != nil {
err = NewDriverError("calling GetHandle", err)
err = newDriverError("calling GetHandle", err)
}

return ft, err
Expand All @@ -726,7 +726,7 @@ func (c *clientHandler) getFileHandle(name string, flags int, offset int64) (Fil
ft, err := c.driver.OpenFile(name, flags, os.ModePerm)

if err != nil {
err = NewDriverError("calling OpenFile", err)
err = newDriverError("calling OpenFile", err)
}

return ft, err
Expand Down
10 changes: 5 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (server *FtpServer) loadSettings() error {
s, err := server.driver.GetSettings()

if err != nil || s == nil {
return NewDriverError("couldn't load settings", err)
return newDriverError("couldn't load settings", err)
}

if s.PublicHost != "" {
Expand Down Expand Up @@ -172,7 +172,7 @@ func (server *FtpServer) Listen() error {
if err != nil {
server.Logger.Error("cannot listen on main port", "err", err, "listenAddr", server.settings.ListenAddr)

return NewNetworkError("cannot listen on main port", err)
return newNetworkError("cannot listen on main port", err)
}

if server.settings.TLSRequired == ImplicitEncryption {
Expand All @@ -183,7 +183,7 @@ func (server *FtpServer) Listen() error {
if err != nil || tlsConfig == nil {
server.Logger.Error("Cannot get tls config", "err", err)

return NewDriverError("cannot get tls config", err)
return newDriverError("cannot get tls config", err)
}

server.listener = tls.NewListener(server.listener, tlsConfig)
Expand Down Expand Up @@ -252,7 +252,7 @@ func (server *FtpServer) Serve() error {

server.Logger.Error("Listener accept error", "err", err)

return NewNetworkError("listener accept error", err)
return newNetworkError("listener accept error", err)
}

tempDelay = 0
Expand Down Expand Up @@ -301,7 +301,7 @@ func (server *FtpServer) Stop() error {
"err", err,
)

return NewNetworkError("couln't close listener", err)
return newNetworkError("couln't close listener", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestCannotListen(t *testing.T) {
portBlockerListener, err := net.Listen("tcp", "127.0.0.1:0")
a.NoError(err)

defer portBlockerListener.Close()
defer func() { a.NoError(portBlockerListener.Close()) }()

server := FtpServer{
Logger: lognoop.NewNoOpLogger(),
Expand All @@ -112,7 +112,7 @@ func TestListenWithBadTLSSettings(t *testing.T) {
portBlockerListener, err := net.Listen("tcp", "127.0.0.1:0")
a.NoError(err)

defer portBlockerListener.Close()
defer func() { a.NoError(portBlockerListener.Close()) }()

server := FtpServer{
Logger: lognoop.NewNoOpLogger(),
Expand Down
8 changes: 4 additions & 4 deletions transfer_active.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (a *activeTransferHandler) Open() (net.Conn, error) {
conn, err := dialer.Dial("tcp", a.raddr.String())

if err != nil {
return nil, NewNetworkError("could not establish active connection", err)
return nil, newNetworkError("could not establish active connection", err)
}

if a.tlsConfig != nil {
Expand All @@ -118,7 +118,7 @@ func (a *activeTransferHandler) Open() (net.Conn, error) {
func (a *activeTransferHandler) Close() error {
if a.conn != nil {
if err := a.conn.Close(); err != nil {
return NewNetworkError("could not close active connection", err)
return newNetworkError("could not close active connection", err)
}
}

Expand Down Expand Up @@ -161,7 +161,7 @@ func parsePORTAddr(param string) (*net.TCPAddr, error) {
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", ip, port))

if err != nil {
err = NewNetworkError(fmt.Sprintf("could not resolve %s", param), err)
err = newNetworkError(fmt.Sprintf("could not resolve %s", param), err)
}

return addr, err
Expand Down Expand Up @@ -203,7 +203,7 @@ func parseEPRTAddr(param string) (addr *net.TCPAddr, err error) {
addr, err = net.ResolveTCPAddr("tcp", net.JoinHostPort(ip.String(), strconv.Itoa(portI)))

if err != nil {
err = NewNetworkError(fmt.Sprintf("could not resolve addr %v:%v", ip, portI), err)
err = newNetworkError(fmt.Sprintf("could not resolve addr %v:%v", ip, portI), err)
}

return addr, err
Expand Down
20 changes: 10 additions & 10 deletions transfer_pasv.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ func (e *ipValidationError) Error() string {
return e.error
}

const (
PortSearchMinAttempts = 10
PortSearchMaxAttempts = 1000
)

func (c *clientHandler) getCurrentIP() ([]string, error) {
// Provide our external IP address so the ftp client can connect back to us
ip := c.server.settings.PublicHost
Expand Down Expand Up @@ -84,14 +79,19 @@ func (c *clientHandler) getCurrentIP() ([]string, error) {
// ErrNoAvailableListeningPort is returned when no port could be found to accept incoming connection
var ErrNoAvailableListeningPort = errors.New("could not find any port to listen to")

const (
portSearchMinAttempts = 10
portSearchMaxAttempts = 1000
)

func (c *clientHandler) findListenerWithinPortRange(portRange *PortRange) (*net.TCPListener, error) {
nbAttempts := portRange.End - portRange.Start

// Making sure we trying a reasonable amount of ports before giving up
if nbAttempts < PortSearchMinAttempts {
nbAttempts = PortSearchMinAttempts
} else if nbAttempts > PortSearchMaxAttempts {
nbAttempts = PortSearchMaxAttempts
if nbAttempts < portSearchMinAttempts {
nbAttempts = portSearchMinAttempts
} else if nbAttempts > portSearchMaxAttempts {
nbAttempts = portSearchMaxAttempts
}

for i := 0; i < nbAttempts; i++ {
Expand All @@ -102,7 +102,7 @@ func (c *clientHandler) findListenerWithinPortRange(portRange *PortRange) (*net.
if errResolve != nil {
c.logger.Error("Problem resolving local port", "err", errResolve, "port", port)

return nil, NewNetworkError(fmt.Sprintf("could not resolve port %d", port), errResolve)
return nil, newNetworkError(fmt.Sprintf("could not resolve port %d", port), errResolve)
}

tcpListener, errListen := net.ListenTCP("tcp", laddr)
Expand Down

0 comments on commit 4e934cb

Please sign in to comment.