goftp

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

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

Go to latest
Published: Apr 4, 2018 License: MIT Imports: 13 Imported by: 0

README

Forked from https://github.com/dutchcoders/goftp and added a ListParsed function to return parsed structs of files in a specific path. But be aware that this is only works with modern FTP servers running on Linux and may fail with others.

goftp

Golang FTP library with Walk support.

Features

  • AUTH TLS support
  • Walk

Sample

package main

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

    "encoding/hex"
    "github.com/pteich/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()
    fmt.Println("Successfully connected to", server)

    // TLS client authentication
    config := tls.Config{
        InsecureSkipVerify: true,
        ClientAuth:         tls.RequestClientCert,
    }

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

    // Username / password authentication
    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)

    // Get directory listing
    var files []string
    if files, err = ftp.List(""); err != nil {
        panic(err)
    }
    fmt.Println("Directory listing:", files)

    // Upload a file
    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)
    }

    // Download each file into local memory, and calculate it's sha256 hash
    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, hex.EncodeToString(hasher.Sum(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

Creators

Remco Verhoef

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

Documentation

Overview

Package goftp upload helper

Index

Constants

View Source
const (
	StatusFileOK                = "150"
	StatusOK                    = "200"
	StatusSystemStatus          = "211"
	StatusDirectoryStatus       = "212"
	StatusFileStatus            = "213"
	StatusConnectionClosing     = "221"
	StatusSystemType            = "215"
	StatusClosingDataConnection = "226"
	StatusActionOK              = "250"
	StatusPathCreated           = "257"
	StatusActionPending         = "350"
)

FTP Status codes, defined in RFC 959

View Source
const (
	// TypeASCII for ASCII
	TypeASCII = "A"
	// TypeEBCDIC for EBCDIC
	TypeEBCDIC = "E"
	// TypeImage for an Image
	TypeImage = "I"
	// TypeLocal for local byte size
	TypeLocal = "L"
)

Variables

View Source
var (
	SystemTypeUnixL8    = "UNIX Type: L8"
	SystemTypeWindowsNT = "Windows_NT"
)

System types from Syst

View Source
var RePwdPath = regexp.MustCompile(`\"(.*)\"`)

RePwdPath is the default expression for matching files in the current working directory

Functions

func StatusText

func StatusText(code string) string

StatusText returns a text for the FTP status code. It returns the empty string if the code is unknown.

Types

type FTP

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

FTP is a session for File Transfer Protocol

func Connect

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

Connect to server at addr (format "host:port"). debug is OFF

func ConnectDbg

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

ConnectDbg to server at addr (format "host:port"). 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() error

Close ends the FTP connection

func (*FTP) Cwd

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

Cwd changes current working directory on remote host to path

func (*FTP) Dele

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

Dele deletes path on remote host

func (*FTP) List

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

List returns a slice of all files in the path

func (*FTP) ListParsed

func (ftp *FTP) ListParsed(path string) (info []FtpFileInfo, err error)

ListParsed returns a slice of FtpFileInfo structs for every file

func (*FTP) Login

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

Login to the server with provided username and password. Typical default may be ("anonymous","").

func (*FTP) Mkd

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

Mkd makes a directory on the remote host

func (*FTP) Noop

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

Noop will 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 gets current path on the remote host

func (*FTP) Quit

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

Quit sends quit to the server and close the connection. No need to Close after this.

func (*FTP) RawCmd

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

RawCmd sends raw commands to the remote server. Returns response code as int and response as string.

func (*FTP) ReadAndDiscard

func (ftp *FTP) ReadAndDiscard() (int, error)

ReadAndDiscard reads all the buffered bytes and returns the number of bytes that cleared from the buffer

func (*FTP) Rename

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

Rename file on the remote host

func (*FTP) Retr

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

Retr retrieves file from remote host at path, using retrFn to read from the remote file.

func (*FTP) Rmd

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

Rmd remove directory

func (*FTP) Size

func (ftp *FTP) Size(path string) (size int, err error)

Size returns the size of a file.

func (*FTP) Stat

func (ftp *FTP) Stat(path string) ([]string, error)

Stat gets the status of path from the remote host

func (*FTP) Stor

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

Stor uploads file to remote host path, from r

func (*FTP) Syst

func (ftp *FTP) Syst() (line string, err error)

Syst returns the system type of the remote host

func (*FTP) Type

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

Type changes transfer type.

func (*FTP) Upload

func (ftp *FTP) Upload(localPath string) (err error)

Upload a file, or recursively upload a directory. Only normal files and directories are uploaded. Symlinks are not kept but treated as normal files/directories if targets are so.

func (*FTP) Walk

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

Walk walks recursively through path and call walkfunc for each file

type FileType

type FileType int
const (
	DIR FileType = iota
	FILE
)

type FtpFileInfo

type FtpFileInfo struct {
	Name  string
	Size  int64
	Type  FileType
	Mtime time.Time
	Ctime time.Time
	Raw   string
}

type RetrFunc

type RetrFunc func(r io.Reader) error

RetrFunc is passed to Retr and is the handler for the stream received for a given path

type TypeCode

type TypeCode string

TypeCode for the representation types

type WalkFunc

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

WalkFunc is called on each path in a Walk. Errors are filtered through WalkFunc

Jump to

Keyboard shortcuts

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