goftp

package module
v0.0.0-...-e1554a2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2017 License: MIT Imports: 12 Imported by: 1

README

goftp

Golang FTP library with Walk support.

This page only contains a sample. For a more detailed documentation please refer to the MANUAL.

This fork contains unstable code

Features

  • AUTH TLS support
  • Walk

Sample

	package main

    import (
        "crypto/sha256"
        "crypto/tls"
        "fmt"
        "io"
        "os"

        "github.com/VincenzoLaSpesa/goftp"
    )

    func main() {
        var err error
        var ftp *goftp.FTP

        // For debug messages: goftp.ConnectDbg("ftp.server.com:21")
        if ftp, err = goftp.Connect("ftp.server.com:21"); err != nil {
            panic(err)
        }

        defer ftp.Close()

        config := tls.Config{
            InsecureSkipVerify: true,
            ClientAuth:         tls.RequestClientCert,
        }

        if err = ftp.AuthTLS(config); err != nil {
            panic(err)
        }

        if err = ftp.Login("username", "password"); err != nil {
            panic(err)
        }

        if err = ftp.Cwd("/"); err != nil {
            panic(err)
        }

        var curpath string
        if curpath, err = ftp.Pwd(); err != nil {
            panic(err)
        }

        fmt.Printf("Current path: %s", curpath)

        var files []string
        if files, err = ftp.List(""); err != nil {
            panic(err)
        }

        fmt.Println(files)

        var file *os.File
        if file, err = os.Open("/tmp/test.txt"); err != nil {
            panic(err)
        }

        if err := ftp.Stor("/test.txt", file); err != nil {
            panic(err)
        }

        err = ftp.Walk("/", func(path string, info os.FileMode, err error) error {
            _, err = ftp.Retr(path, func(r io.Reader) error {
                var hasher = sha256.New()
                if _, err = io.Copy(hasher, r); err != nil {
                    return err
                }

                hash := fmt.Sprintf("%s %x", path, sha256.Sum256(nil))
                fmt.Println(hash)

                return err
            })

            return nil
        })
    }

Contributions

Contributions are welcome.

  • Sourav Datta: for his work on the anonymous user login and multiline return status.
  • Vincenzo La Spesa: for his work on resolving login issues with specific ftp servers.
  • Dmitriy Denisov: for his work on resolving parsing issues with non-standard multiline response.

Creators

Remco Verhoef

Code and documentation copyright 2011-2014 Remco Verhoef. Code released under the MIT license.

Documentation

Index

Constants

View Source
const (
	//MLSD server can return directory listing in machine readable format.
	//Supported
	MLSD = 1
	//NLST server can return a list of filenames in the given directory, with no other information.
	//Not supported
	NLST = 2
	//EPLF server can return directory listing in "Easily Parsed LIST Format"
	//Not supported
	EPLF = 4
	//BAD this server only supports LIST output format
	BAD = 1 >> 32

	//ModeASCII transfer mode ASCII
	ModeASCII TransferMode = "A"
	//ModeBinary transfer mode binary
	ModeBinary TransferMode = "I"
)
View Source
const (
	CodeRestartMarker             = 110
	CodeServiceReadyNNNminutes    = 120
	CodeDataConnectionAlreadyOpen = 125
	CodeFileStatusOk              = 150

	CodeCommandOk                              = 200
	CodeCommandNotImplementedInfo              = 202
	CodeSystemStatus                           = 211
	CodeDirectoryStatus                        = 212
	CodeFileStatus                             = 213
	CodeHelpMessage                            = 214
	CodeSystemType                             = 215
	CodeServiceReadyForNewUser                 = 220
	CodeServiceClosingControlConnection        = 221
	CodeDataConnectionOpenNoTransferInProgress = 225
	CodeClosingDataConnection                  = 226
	CodeEnteringPassiveMode                    = 227
	CodeEnteringLongPassiveMode                = 228
	CodeEnteringExtendedPassiveMode            = 229
	CodeUserLoggedIn                           = 230
	CodeUserLoggedOut                          = 231
	CodeLogoutNoted                            = 232
	CodeAuthMechanismAccepted                  = 234
	CodeRequestedFileActionOk                  = 250
	CodePathnameCreated                        = 257

	CodeUserNameOkNeedPassword     = 331
	CodeNeedAccountForLogin        = 332
	CodeRequestedFileActionPending = 350

	CodeServiceNotAvaliable         = 421
	CodeCantOpenDataConnection      = 425
	CodeConnectionClosed            = 426
	CodeInvalidUsernameOrPassword   = 430
	CodeRequstedHostUnavaliable     = 434
	CodeRequestedFileActionNotTaken = 450
	CodeRequestedActionAborted      = 451
	CodeRequestedActionNotTaken     = 452

	CodeSyntaxError                        = 501
	CodeCommandNotImplementedError         = 502
	CodeBadSequenceOfCommandsError         = 503
	CodeCommandNotImplementedForParamError = 504
	CodeNotLoggedInError                   = 530
	CodeNeedAccountForStoringError         = 532
	CodeFileUnavailableError               = 550
	CodePageTypeunknownError               = 551
	CodeExceededStorageAllocationError     = 552
	CodeFileNameNotAllowedError            = 553

	CodeIntegrityProtectedReply                   = 631
	CodeConfidentialityAndIntegrityProtectedReply = 632
	CodeConfidentialityProtectedReply             = 633
)

FTP status codes https://en.wikipedia.org/wiki/List_of_FTP_server_return_codes

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorHandlerFunc

type ErrorHandlerFunc func(pwd string, errorCode int, errorStr string, shouldBeSkippable bool) (skippable bool, err error)

ErrorHandlerFunc is the type of the function called on error in FTP.WalkCustom

type FTP

type FTP struct {
	// contains filtered or unexported fields
}

FTP is ftp client

func Connect

func Connect(addr string) (*FTP, error)

Connect connect to server, debug is OFF

func ConnectDbg

func ConnectDbg(addr string) (*FTP, error)

ConnectDbg connect to server, debug is ON

func (*FTP) AuthTLS

func (ftp *FTP) AuthTLS(config *tls.Config) error

AuthTLS secures the ftp connection by using TLS

func (*FTP) Close

func (ftp *FTP) Close()

Close connection, disconnect from server

func (*FTP) Cwd

func (ftp *FTP) Cwd(path string) (err error)

Cwd change current path

func (*FTP) Dele

func (ftp *FTP) Dele(path string) (err error)

Dele delete file

func (*FTP) GetFilesList

func (ftp *FTP) GetFilesList(path string) (files []string, directories []string, links []string, err error)

GetFilesList list the path (or current directory) and parse it. Return an array with the files, one with the directories and one with the links

func (*FTP) HasCode

func (ftp *FTP) HasCode(line string, code int) bool

HasCode check if ftp status code in line

func (*FTP) List

func (ftp *FTP) List(path string) (files []string, err error)

List list the path (or current directory). return raw listing, do not parse it.

func (*FTP) Login

func (ftp *FTP) Login(username string, password string) (err error)

Login login to the server

func (*FTP) Mkd

func (ftp *FTP) Mkd(path string) error

Mkd make directory

func (*FTP) NewPassiveConnection

func (ftp *FTP) NewPassiveConnection() (conn net.Conn, err error)

NewPassiveConnection enables passive data connection and connect to server

func (*FTP) Noop

func (ftp *FTP) Noop() (err error)

Noop send a NOOP (no operation) to the server

func (*FTP) Pasv

func (ftp *FTP) Pasv() (port int, err error)

Pasv enables passive data connection and returns port number

func (*FTP) Pwd

func (ftp *FTP) Pwd() (path string, err error)

Pwd get current path

func (*FTP) Quit

func (ftp *FTP) Quit() (err error)

Quit send quit to the server and close the connection

func (*FTP) RawCmd

func (ftp *FTP) RawCmd(command string, args ...interface{}) (code int, line string)

RawCmd send raw commands, return response as string and response code as int

func (*FTP) RawPassiveCmd

func (ftp *FTP) RawPassiveCmd(command string) (code int, response []string)

RawPassiveCmd open a passive connection with pasv, send a raw command, retrieve the response, close the connection, return the response

func (*FTP) Rename

func (ftp *FTP) Rename(from string, to string) (err error)

Rename rename file

func (*FTP) Retr

func (ftp *FTP) Retr(path string, retrFn RetrFunc) (s string, err error)

Retr retrieves file from server

func (*FTP) Stor

func (ftp *FTP) Stor(path string, r io.Reader) (err error)

Stor upload file to server

func (*FTP) Type

func (ftp *FTP) Type(t TransferMode) error

Type sets the transfer mode (ASCII/Binary).

func (*FTP) Walk

func (ftp *FTP) Walk(path string, walkFn WalkFunc, deepLimit ...int) (err error)

Walk walks recursively through path and call walkfunc for each file. - links are ignored. - the optional parameter deepLimit controls the max level of recursion. - recursion stops on first error , *always*. - Directories are traversed in pre-order

func (*FTP) WalkCustom

func (ftp *FTP) WalkCustom(path string, walkFn WalkFunc, errHandler ErrorHandlerFunc, deepLimit ...int) (err error)

WalkCustom walks recursively through path and call walkFunc for each file. - links are ignored. - the optional parameter deepLimit controls the max level of recursion. - recursion stops only if errHandler returns false ( or if it's not defined ) - Directories are traversed in pre-order

type RetrFunc

type RetrFunc func(r io.Reader) error

RetrFunc is type of the function called in FTP.Retr

type TransferMode

type TransferMode string

TransferMode ftp transfer mode

type WalkFunc

type WalkFunc func(path string, info os.FileMode, err error) error

WalkFunc is the type of the function called for each file or directory visited by FTP.Walk

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL