Ugh..added dependency for gopkg.in/cheggaaa/pb.v1: github.com/matn/go-runewidth. Also added github.com/jlaffaye/ftp for ftp support.
This commit is contained in:
parent
c29e3915be
commit
e42a23ecb5
|
@ -0,0 +1,13 @@
|
||||||
|
Copyright (c) 2011-2013, Julien Laffaye <jlaffaye@FreeBSD.org>
|
||||||
|
|
||||||
|
Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
purpose with or without fee is hereby granted, provided that the above
|
||||||
|
copyright notice and this permission notice appear in all copies.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
@ -0,0 +1,17 @@
|
||||||
|
# goftp #
|
||||||
|
|
||||||
|
[![Build Status](https://travis-ci.org/jlaffaye/ftp.svg?branch=master)](https://travis-ci.org/jlaffaye/ftp)
|
||||||
|
[![Coverage Status](https://coveralls.io/repos/jlaffaye/ftp/badge.svg?branch=master&service=github)](https://coveralls.io/github/jlaffaye/ftp?branch=master)
|
||||||
|
[![Go ReportCard](http://goreportcard.com/badge/jlaffaye/ftp)](http://goreportcard.com/report/jlaffaye/ftp)
|
||||||
|
|
||||||
|
A FTP client package for Go
|
||||||
|
|
||||||
|
## Install ##
|
||||||
|
|
||||||
|
```
|
||||||
|
go get -u github.com/jlaffaye/ftp
|
||||||
|
```
|
||||||
|
|
||||||
|
## Documentation ##
|
||||||
|
|
||||||
|
http://godoc.org/github.com/jlaffaye/ftp
|
|
@ -0,0 +1,671 @@
|
||||||
|
// Package ftp implements a FTP client as described in RFC 959.
|
||||||
|
package ftp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/textproto"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// EntryType describes the different types of an Entry.
|
||||||
|
type EntryType int
|
||||||
|
|
||||||
|
// The differents types of an Entry
|
||||||
|
const (
|
||||||
|
EntryTypeFile EntryType = iota
|
||||||
|
EntryTypeFolder
|
||||||
|
EntryTypeLink
|
||||||
|
)
|
||||||
|
|
||||||
|
// ServerConn represents the connection to a remote FTP server.
|
||||||
|
type ServerConn struct {
|
||||||
|
conn *textproto.Conn
|
||||||
|
host string
|
||||||
|
timeout time.Duration
|
||||||
|
features map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Entry describes a file and is returned by List().
|
||||||
|
type Entry struct {
|
||||||
|
Name string
|
||||||
|
Type EntryType
|
||||||
|
Size uint64
|
||||||
|
Time time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// response represent a data-connection
|
||||||
|
type response struct {
|
||||||
|
conn net.Conn
|
||||||
|
c *ServerConn
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connect is an alias to Dial, for backward compatibility
|
||||||
|
func Connect(addr string) (*ServerConn, error) {
|
||||||
|
return Dial(addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dial is like DialTimeout with no timeout
|
||||||
|
func Dial(addr string) (*ServerConn, error) {
|
||||||
|
return DialTimeout(addr, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialTimeout initializes the connection to the specified ftp server address.
|
||||||
|
//
|
||||||
|
// It is generally followed by a call to Login() as most FTP commands require
|
||||||
|
// an authenticated user.
|
||||||
|
func DialTimeout(addr string, timeout time.Duration) (*ServerConn, error) {
|
||||||
|
tconn, err := net.DialTimeout("tcp", addr, timeout)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the resolved IP address in case addr contains a domain name
|
||||||
|
// If we use the domain name, we might not resolve to the same IP.
|
||||||
|
remoteAddr := tconn.RemoteAddr().String()
|
||||||
|
host, _, err := net.SplitHostPort(remoteAddr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
conn := textproto.NewConn(tconn)
|
||||||
|
|
||||||
|
c := &ServerConn{
|
||||||
|
conn: conn,
|
||||||
|
host: host,
|
||||||
|
timeout: timeout,
|
||||||
|
features: make(map[string]string),
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, err = c.conn.ReadResponse(StatusReady)
|
||||||
|
if err != nil {
|
||||||
|
c.Quit()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = c.feat()
|
||||||
|
if err != nil {
|
||||||
|
c.Quit()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Login authenticates the client with specified user and password.
|
||||||
|
//
|
||||||
|
// "anonymous"/"anonymous" is a common user/password scheme for FTP servers
|
||||||
|
// that allows anonymous read-only accounts.
|
||||||
|
func (c *ServerConn) Login(user, password string) error {
|
||||||
|
code, message, err := c.cmd(-1, "USER %s", user)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch code {
|
||||||
|
case StatusLoggedIn:
|
||||||
|
case StatusUserOK:
|
||||||
|
_, _, err = c.cmd(StatusLoggedIn, "PASS %s", password)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return errors.New(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Switch to binary mode
|
||||||
|
_, _, err = c.cmd(StatusCommandOK, "TYPE I")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// feat issues a FEAT FTP command to list the additional commands supported by
|
||||||
|
// the remote FTP server.
|
||||||
|
// FEAT is described in RFC 2389
|
||||||
|
func (c *ServerConn) feat() error {
|
||||||
|
code, message, err := c.cmd(-1, "FEAT")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if code != StatusSystem {
|
||||||
|
// The server does not support the FEAT command. This is not an
|
||||||
|
// error: we consider that there is no additional feature.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
lines := strings.Split(message, "\n")
|
||||||
|
for _, line := range lines {
|
||||||
|
if !strings.HasPrefix(line, " ") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
featureElements := strings.SplitN(line, " ", 2)
|
||||||
|
|
||||||
|
command := featureElements[0]
|
||||||
|
|
||||||
|
var commandDesc string
|
||||||
|
if len(featureElements) == 2 {
|
||||||
|
commandDesc = featureElements[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
c.features[command] = commandDesc
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// epsv issues an "EPSV" command to get a port number for a data connection.
|
||||||
|
func (c *ServerConn) epsv() (port int, err error) {
|
||||||
|
_, line, err := c.cmd(StatusExtendedPassiveMode, "EPSV")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
start := strings.Index(line, "|||")
|
||||||
|
end := strings.LastIndex(line, "|")
|
||||||
|
if start == -1 || end == -1 {
|
||||||
|
err = errors.New("Invalid EPSV response format")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
port, err = strconv.Atoi(line[start+3 : end])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// pasv issues a "PASV" command to get a port number for a data connection.
|
||||||
|
func (c *ServerConn) pasv() (port int, err error) {
|
||||||
|
_, line, err := c.cmd(StatusPassiveMode, "PASV")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// PASV response format : 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).
|
||||||
|
start := strings.Index(line, "(")
|
||||||
|
end := strings.LastIndex(line, ")")
|
||||||
|
if start == -1 || end == -1 {
|
||||||
|
return 0, errors.New("Invalid PASV response format")
|
||||||
|
}
|
||||||
|
|
||||||
|
// We have to split the response string
|
||||||
|
pasvData := strings.Split(line[start+1:end], ",")
|
||||||
|
|
||||||
|
if len(pasvData) < 6 {
|
||||||
|
return 0, errors.New("Invalid PASV response format")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Let's compute the port number
|
||||||
|
portPart1, err1 := strconv.Atoi(pasvData[4])
|
||||||
|
if err1 != nil {
|
||||||
|
err = err1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
portPart2, err2 := strconv.Atoi(pasvData[5])
|
||||||
|
if err2 != nil {
|
||||||
|
err = err2
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recompose port
|
||||||
|
port = portPart1*256 + portPart2
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// openDataConn creates a new FTP data connection.
|
||||||
|
func (c *ServerConn) openDataConn() (net.Conn, error) {
|
||||||
|
var (
|
||||||
|
port int
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
if port, err = c.epsv(); err != nil {
|
||||||
|
if port, err = c.pasv(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return net.DialTimeout("tcp", net.JoinHostPort(c.host, strconv.Itoa(port)), c.timeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
// cmd is a helper function to execute a command and check for the expected FTP
|
||||||
|
// return code
|
||||||
|
func (c *ServerConn) cmd(expected int, format string, args ...interface{}) (int, string, error) {
|
||||||
|
_, err := c.conn.Cmd(format, args...)
|
||||||
|
if err != nil {
|
||||||
|
return 0, "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.conn.ReadResponse(expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
// cmdDataConnFrom executes a command which require a FTP data connection.
|
||||||
|
// Issues a REST FTP command to specify the number of bytes to skip for the transfer.
|
||||||
|
func (c *ServerConn) cmdDataConnFrom(offset uint64, format string, args ...interface{}) (net.Conn, error) {
|
||||||
|
conn, err := c.openDataConn()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if offset != 0 {
|
||||||
|
_, _, err := c.cmd(StatusRequestFilePending, "REST %d", offset)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = c.conn.Cmd(format, args...)
|
||||||
|
if err != nil {
|
||||||
|
conn.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
code, msg, err := c.conn.ReadResponse(-1)
|
||||||
|
if err != nil {
|
||||||
|
conn.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if code != StatusAlreadyOpen && code != StatusAboutToSend {
|
||||||
|
conn.Close()
|
||||||
|
return nil, &textproto.Error{Code: code, Msg: msg}
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var errUnsupportedListLine = errors.New("Unsupported LIST line")
|
||||||
|
|
||||||
|
// parseRFC3659ListLine parses the style of directory line defined in RFC 3659.
|
||||||
|
func parseRFC3659ListLine(line string) (*Entry, error) {
|
||||||
|
iSemicolon := strings.Index(line, ";")
|
||||||
|
iWhitespace := strings.Index(line, " ")
|
||||||
|
|
||||||
|
if iSemicolon < 0 || iSemicolon > iWhitespace {
|
||||||
|
return nil, errUnsupportedListLine
|
||||||
|
}
|
||||||
|
|
||||||
|
e := &Entry{
|
||||||
|
Name: line[iWhitespace+1:],
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, field := range strings.Split(line[:iWhitespace-1], ";") {
|
||||||
|
i := strings.Index(field, "=")
|
||||||
|
if i < 1 {
|
||||||
|
return nil, errUnsupportedListLine
|
||||||
|
}
|
||||||
|
|
||||||
|
key := field[:i]
|
||||||
|
value := field[i+1:]
|
||||||
|
|
||||||
|
switch key {
|
||||||
|
case "modify":
|
||||||
|
var err error
|
||||||
|
e.Time, err = time.Parse("20060102150405", value)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
case "type":
|
||||||
|
switch value {
|
||||||
|
case "dir", "cdir", "pdir":
|
||||||
|
e.Type = EntryTypeFolder
|
||||||
|
case "file":
|
||||||
|
e.Type = EntryTypeFile
|
||||||
|
}
|
||||||
|
case "size":
|
||||||
|
e.setSize(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseLsListLine parses a directory line in a format based on the output of
|
||||||
|
// the UNIX ls command.
|
||||||
|
func parseLsListLine(line string) (*Entry, error) {
|
||||||
|
fields := strings.Fields(line)
|
||||||
|
if len(fields) >= 7 && fields[1] == "folder" && fields[2] == "0" {
|
||||||
|
e := &Entry{
|
||||||
|
Type: EntryTypeFolder,
|
||||||
|
Name: strings.Join(fields[6:], " "),
|
||||||
|
}
|
||||||
|
if err := e.setTime(fields[3:6]); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(fields) < 8 {
|
||||||
|
return nil, errUnsupportedListLine
|
||||||
|
}
|
||||||
|
|
||||||
|
if fields[1] == "0" {
|
||||||
|
e := &Entry{
|
||||||
|
Type: EntryTypeFile,
|
||||||
|
Name: strings.Join(fields[7:], " "),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := e.setSize(fields[2]); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := e.setTime(fields[4:7]); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(fields) < 9 {
|
||||||
|
return nil, errUnsupportedListLine
|
||||||
|
}
|
||||||
|
|
||||||
|
e := &Entry{}
|
||||||
|
switch fields[0][0] {
|
||||||
|
case '-':
|
||||||
|
e.Type = EntryTypeFile
|
||||||
|
if err := e.setSize(fields[4]); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
case 'd':
|
||||||
|
e.Type = EntryTypeFolder
|
||||||
|
case 'l':
|
||||||
|
e.Type = EntryTypeLink
|
||||||
|
default:
|
||||||
|
return nil, errors.New("Unknown entry type")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := e.setTime(fields[5:8]); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
e.Name = strings.Join(fields[8:], " ")
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var dirTimeFormats = []string{
|
||||||
|
"01-02-06 03:04PM",
|
||||||
|
"2006-01-02 15:04",
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseDirListLine parses a directory line in a format based on the output of
|
||||||
|
// the MS-DOS DIR command.
|
||||||
|
func parseDirListLine(line string) (*Entry, error) {
|
||||||
|
e := &Entry{}
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// Try various time formats that DIR might use, and stop when one works.
|
||||||
|
for _, format := range dirTimeFormats {
|
||||||
|
if len(line) > len(format) {
|
||||||
|
e.Time, err = time.Parse(format, line[:len(format)])
|
||||||
|
if err == nil {
|
||||||
|
line = line[len(format):]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
// None of the time formats worked.
|
||||||
|
return nil, errUnsupportedListLine
|
||||||
|
}
|
||||||
|
|
||||||
|
line = strings.TrimLeft(line, " ")
|
||||||
|
if strings.HasPrefix(line, "<DIR>") {
|
||||||
|
e.Type = EntryTypeFolder
|
||||||
|
line = strings.TrimPrefix(line, "<DIR>")
|
||||||
|
} else {
|
||||||
|
space := strings.Index(line, " ")
|
||||||
|
if space == -1 {
|
||||||
|
return nil, errUnsupportedListLine
|
||||||
|
}
|
||||||
|
e.Size, err = strconv.ParseUint(line[:space], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errUnsupportedListLine
|
||||||
|
}
|
||||||
|
e.Type = EntryTypeFile
|
||||||
|
line = line[space:]
|
||||||
|
}
|
||||||
|
|
||||||
|
e.Name = strings.TrimLeft(line, " ")
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var listLineParsers = []func(line string) (*Entry, error){
|
||||||
|
parseRFC3659ListLine,
|
||||||
|
parseLsListLine,
|
||||||
|
parseDirListLine,
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseListLine parses the various non-standard format returned by the LIST
|
||||||
|
// FTP command.
|
||||||
|
func parseListLine(line string) (*Entry, error) {
|
||||||
|
for _, f := range listLineParsers {
|
||||||
|
e, err := f(line)
|
||||||
|
if err == errUnsupportedListLine {
|
||||||
|
// Try another format.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return e, err
|
||||||
|
}
|
||||||
|
return nil, errUnsupportedListLine
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Entry) setSize(str string) (err error) {
|
||||||
|
e.Size, err = strconv.ParseUint(str, 0, 64)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Entry) setTime(fields []string) (err error) {
|
||||||
|
var timeStr string
|
||||||
|
if strings.Contains(fields[2], ":") { // this year
|
||||||
|
thisYear, _, _ := time.Now().Date()
|
||||||
|
timeStr = fields[1] + " " + fields[0] + " " + strconv.Itoa(thisYear)[2:4] + " " + fields[2] + " GMT"
|
||||||
|
} else { // not this year
|
||||||
|
if len(fields[2]) != 4 {
|
||||||
|
return errors.New("Invalid year format in time string")
|
||||||
|
}
|
||||||
|
timeStr = fields[1] + " " + fields[0] + " " + fields[2][2:4] + " 00:00 GMT"
|
||||||
|
}
|
||||||
|
e.Time, err = time.Parse("_2 Jan 06 15:04 MST", timeStr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// NameList issues an NLST FTP command.
|
||||||
|
func (c *ServerConn) NameList(path string) (entries []string, err error) {
|
||||||
|
conn, err := c.cmdDataConnFrom(0, "NLST %s", path)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
r := &response{conn, c}
|
||||||
|
defer r.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(r)
|
||||||
|
for scanner.Scan() {
|
||||||
|
entries = append(entries, scanner.Text())
|
||||||
|
}
|
||||||
|
if err = scanner.Err(); err != nil {
|
||||||
|
return entries, err
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// List issues a LIST FTP command.
|
||||||
|
func (c *ServerConn) List(path string) (entries []*Entry, err error) {
|
||||||
|
conn, err := c.cmdDataConnFrom(0, "LIST %s", path)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
r := &response{conn, c}
|
||||||
|
defer r.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(r)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
entry, err := parseListLine(line)
|
||||||
|
if err == nil {
|
||||||
|
entries = append(entries, entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChangeDir issues a CWD FTP command, which changes the current directory to
|
||||||
|
// the specified path.
|
||||||
|
func (c *ServerConn) ChangeDir(path string) error {
|
||||||
|
_, _, err := c.cmd(StatusRequestedFileActionOK, "CWD %s", path)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChangeDirToParent issues a CDUP FTP command, which changes the current
|
||||||
|
// directory to the parent directory. This is similar to a call to ChangeDir
|
||||||
|
// with a path set to "..".
|
||||||
|
func (c *ServerConn) ChangeDirToParent() error {
|
||||||
|
_, _, err := c.cmd(StatusRequestedFileActionOK, "CDUP")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// CurrentDir issues a PWD FTP command, which Returns the path of the current
|
||||||
|
// directory.
|
||||||
|
func (c *ServerConn) CurrentDir() (string, error) {
|
||||||
|
_, msg, err := c.cmd(StatusPathCreated, "PWD")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
start := strings.Index(msg, "\"")
|
||||||
|
end := strings.LastIndex(msg, "\"")
|
||||||
|
|
||||||
|
if start == -1 || end == -1 {
|
||||||
|
return "", errors.New("Unsuported PWD response format")
|
||||||
|
}
|
||||||
|
|
||||||
|
return msg[start+1 : end], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retr issues a RETR FTP command to fetch the specified file from the remote
|
||||||
|
// FTP server.
|
||||||
|
//
|
||||||
|
// The returned ReadCloser must be closed to cleanup the FTP data connection.
|
||||||
|
func (c *ServerConn) Retr(path string) (io.ReadCloser, error) {
|
||||||
|
return c.RetrFrom(path, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RetrFrom issues a RETR FTP command to fetch the specified file from the remote
|
||||||
|
// FTP server, the server will not send the offset first bytes of the file.
|
||||||
|
//
|
||||||
|
// The returned ReadCloser must be closed to cleanup the FTP data connection.
|
||||||
|
func (c *ServerConn) RetrFrom(path string, offset uint64) (io.ReadCloser, error) {
|
||||||
|
conn, err := c.cmdDataConnFrom(offset, "RETR %s", path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &response{conn, c}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stor issues a STOR FTP command to store a file to the remote FTP server.
|
||||||
|
// Stor creates the specified file with the content of the io.Reader.
|
||||||
|
//
|
||||||
|
// Hint: io.Pipe() can be used if an io.Writer is required.
|
||||||
|
func (c *ServerConn) Stor(path string, r io.Reader) error {
|
||||||
|
return c.StorFrom(path, r, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StorFrom issues a STOR FTP command to store a file to the remote FTP server.
|
||||||
|
// Stor creates the specified file with the content of the io.Reader, writing
|
||||||
|
// on the server will start at the given file offset.
|
||||||
|
//
|
||||||
|
// Hint: io.Pipe() can be used if an io.Writer is required.
|
||||||
|
func (c *ServerConn) StorFrom(path string, r io.Reader, offset uint64) error {
|
||||||
|
conn, err := c.cmdDataConnFrom(offset, "STOR %s", path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = io.Copy(conn, r)
|
||||||
|
conn.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, err = c.conn.ReadResponse(StatusClosingDataConnection)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rename renames a file on the remote FTP server.
|
||||||
|
func (c *ServerConn) Rename(from, to string) error {
|
||||||
|
_, _, err := c.cmd(StatusRequestFilePending, "RNFR %s", from)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, err = c.cmd(StatusRequestedFileActionOK, "RNTO %s", to)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete issues a DELE FTP command to delete the specified file from the
|
||||||
|
// remote FTP server.
|
||||||
|
func (c *ServerConn) Delete(path string) error {
|
||||||
|
_, _, err := c.cmd(StatusRequestedFileActionOK, "DELE %s", path)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MakeDir issues a MKD FTP command to create the specified directory on the
|
||||||
|
// remote FTP server.
|
||||||
|
func (c *ServerConn) MakeDir(path string) error {
|
||||||
|
_, _, err := c.cmd(StatusPathCreated, "MKD %s", path)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveDir issues a RMD FTP command to remove the specified directory from
|
||||||
|
// the remote FTP server.
|
||||||
|
func (c *ServerConn) RemoveDir(path string) error {
|
||||||
|
_, _, err := c.cmd(StatusRequestedFileActionOK, "RMD %s", path)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// NoOp issues a NOOP FTP command.
|
||||||
|
// NOOP has no effects and is usually used to prevent the remote FTP server to
|
||||||
|
// close the otherwise idle connection.
|
||||||
|
func (c *ServerConn) NoOp() error {
|
||||||
|
_, _, err := c.cmd(StatusCommandOK, "NOOP")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logout issues a REIN FTP command to logout the current user.
|
||||||
|
func (c *ServerConn) Logout() error {
|
||||||
|
_, _, err := c.cmd(StatusReady, "REIN")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Quit issues a QUIT FTP command to properly close the connection from the
|
||||||
|
// remote FTP server.
|
||||||
|
func (c *ServerConn) Quit() error {
|
||||||
|
c.conn.Cmd("QUIT")
|
||||||
|
return c.conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read implements the io.Reader interface on a FTP data connection.
|
||||||
|
func (r *response) Read(buf []byte) (int, error) {
|
||||||
|
return r.conn.Read(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close implements the io.Closer interface on a FTP data connection.
|
||||||
|
func (r *response) Close() error {
|
||||||
|
err := r.conn.Close()
|
||||||
|
_, _, err2 := r.c.conn.ReadResponse(StatusClosingDataConnection)
|
||||||
|
if err2 != nil {
|
||||||
|
err = err2
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
|
@ -0,0 +1,106 @@
|
||||||
|
package ftp
|
||||||
|
|
||||||
|
// FTP status codes, defined in RFC 959
|
||||||
|
const (
|
||||||
|
StatusInitiating = 100
|
||||||
|
StatusRestartMarker = 110
|
||||||
|
StatusReadyMinute = 120
|
||||||
|
StatusAlreadyOpen = 125
|
||||||
|
StatusAboutToSend = 150
|
||||||
|
|
||||||
|
StatusCommandOK = 200
|
||||||
|
StatusCommandNotImplemented = 202
|
||||||
|
StatusSystem = 211
|
||||||
|
StatusDirectory = 212
|
||||||
|
StatusFile = 213
|
||||||
|
StatusHelp = 214
|
||||||
|
StatusName = 215
|
||||||
|
StatusReady = 220
|
||||||
|
StatusClosing = 221
|
||||||
|
StatusDataConnectionOpen = 225
|
||||||
|
StatusClosingDataConnection = 226
|
||||||
|
StatusPassiveMode = 227
|
||||||
|
StatusLongPassiveMode = 228
|
||||||
|
StatusExtendedPassiveMode = 229
|
||||||
|
StatusLoggedIn = 230
|
||||||
|
StatusLoggedOut = 231
|
||||||
|
StatusLogoutAck = 232
|
||||||
|
StatusRequestedFileActionOK = 250
|
||||||
|
StatusPathCreated = 257
|
||||||
|
|
||||||
|
StatusUserOK = 331
|
||||||
|
StatusLoginNeedAccount = 332
|
||||||
|
StatusRequestFilePending = 350
|
||||||
|
|
||||||
|
StatusNotAvailable = 421
|
||||||
|
StatusCanNotOpenDataConnection = 425
|
||||||
|
StatusTransfertAborted = 426
|
||||||
|
StatusInvalidCredentials = 430
|
||||||
|
StatusHostUnavailable = 434
|
||||||
|
StatusFileActionIgnored = 450
|
||||||
|
StatusActionAborted = 451
|
||||||
|
Status452 = 452
|
||||||
|
|
||||||
|
StatusBadCommand = 500
|
||||||
|
StatusBadArguments = 501
|
||||||
|
StatusNotImplemented = 502
|
||||||
|
StatusBadSequence = 503
|
||||||
|
StatusNotImplementedParameter = 504
|
||||||
|
StatusNotLoggedIn = 530
|
||||||
|
StatusStorNeedAccount = 532
|
||||||
|
StatusFileUnavailable = 550
|
||||||
|
StatusPageTypeUnknown = 551
|
||||||
|
StatusExceededStorage = 552
|
||||||
|
StatusBadFileName = 553
|
||||||
|
)
|
||||||
|
|
||||||
|
var statusText = map[int]string{
|
||||||
|
// 200
|
||||||
|
StatusCommandOK: "Command okay.",
|
||||||
|
StatusCommandNotImplemented: "Command not implemented, superfluous at this site.",
|
||||||
|
StatusSystem: "System status, or system help reply.",
|
||||||
|
StatusDirectory: "Directory status.",
|
||||||
|
StatusFile: "File status.",
|
||||||
|
StatusHelp: "Help message.",
|
||||||
|
StatusName: "",
|
||||||
|
StatusReady: "Service ready for new user.",
|
||||||
|
StatusClosing: "Service closing control connection.",
|
||||||
|
StatusDataConnectionOpen: "Data connection open; no transfer in progress.",
|
||||||
|
StatusClosingDataConnection: "Closing data connection. Requested file action successful.",
|
||||||
|
StatusPassiveMode: "Entering Passive Mode.",
|
||||||
|
StatusLongPassiveMode: "Entering Long Passive Mode.",
|
||||||
|
StatusExtendedPassiveMode: "Entering Extended Passive Mode.",
|
||||||
|
StatusLoggedIn: "User logged in, proceed.",
|
||||||
|
StatusLoggedOut: "User logged out; service terminated.",
|
||||||
|
StatusLogoutAck: "Logout command noted, will complete when transfer done.",
|
||||||
|
StatusRequestedFileActionOK: "Requested file action okay, completed.",
|
||||||
|
StatusPathCreated: "Path created.",
|
||||||
|
|
||||||
|
// 300
|
||||||
|
StatusUserOK: "User name okay, need password.",
|
||||||
|
StatusLoginNeedAccount: "Need account for login.",
|
||||||
|
StatusRequestFilePending: "Requested file action pending further information.",
|
||||||
|
|
||||||
|
// 400
|
||||||
|
StatusNotAvailable: "Service not available, closing control connection.",
|
||||||
|
StatusCanNotOpenDataConnection: "Can't open data connection.",
|
||||||
|
StatusTransfertAborted: "Connection closed; transfer aborted.",
|
||||||
|
StatusInvalidCredentials: "Invalid username or password.",
|
||||||
|
StatusHostUnavailable: "Requested host unavailable.",
|
||||||
|
StatusFileActionIgnored: "Requested file action not taken.",
|
||||||
|
StatusActionAborted: "Requested action aborted. Local error in processing.",
|
||||||
|
Status452: "Insufficient storage space in system.",
|
||||||
|
|
||||||
|
// 500
|
||||||
|
StatusBadCommand: "Command unrecognized.",
|
||||||
|
StatusBadArguments: "Syntax error in parameters or arguments.",
|
||||||
|
StatusNotImplemented: "Command not implemented.",
|
||||||
|
StatusBadSequence: "Bad sequence of commands.",
|
||||||
|
StatusNotImplementedParameter: "Command not implemented for that parameter.",
|
||||||
|
StatusNotLoggedIn: "Not logged in.",
|
||||||
|
StatusStorNeedAccount: "Need account for storing files.",
|
||||||
|
StatusFileUnavailable: "File unavailable.",
|
||||||
|
StatusPageTypeUnknown: "Page type unknown.",
|
||||||
|
StatusExceededStorage: "Exceeded storage allocation.",
|
||||||
|
StatusBadFileName: "File name not allowed.",
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Yasuhiro Matsumoto
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
|
@ -0,0 +1,27 @@
|
||||||
|
go-runewidth
|
||||||
|
============
|
||||||
|
|
||||||
|
[![Build Status](https://travis-ci.org/mattn/go-runewidth.png?branch=master)](https://travis-ci.org/mattn/go-runewidth)
|
||||||
|
[![Coverage Status](https://coveralls.io/repos/mattn/go-runewidth/badge.png?branch=HEAD)](https://coveralls.io/r/mattn/go-runewidth?branch=HEAD)
|
||||||
|
[![GoDoc](https://godoc.org/github.com/mattn/go-runewidth?status.svg)](http://godoc.org/github.com/mattn/go-runewidth)
|
||||||
|
[![Go Report Card](https://goreportcard.com/badge/github.com/mattn/go-runewidth)](https://goreportcard.com/report/github.com/mattn/go-runewidth)
|
||||||
|
|
||||||
|
Provides functions to get fixed width of the character or string.
|
||||||
|
|
||||||
|
Usage
|
||||||
|
-----
|
||||||
|
|
||||||
|
```go
|
||||||
|
runewidth.StringWidth("つのだ☆HIRO") == 12
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Author
|
||||||
|
------
|
||||||
|
|
||||||
|
Yasuhiro Matsumoto
|
||||||
|
|
||||||
|
License
|
||||||
|
-------
|
||||||
|
|
||||||
|
under the MIT License: http://mattn.mit-license.org/2013
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,8 @@
|
||||||
|
// +build js
|
||||||
|
|
||||||
|
package runewidth
|
||||||
|
|
||||||
|
func IsEastAsian() bool {
|
||||||
|
// TODO: Implement this for the web. Detect east asian in a compatible way, and return true.
|
||||||
|
return false
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
// +build !windows,!js
|
||||||
|
|
||||||
|
package runewidth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var reLoc = regexp.MustCompile(`^[a-z][a-z][a-z]?(?:_[A-Z][A-Z])?\.(.+)`)
|
||||||
|
|
||||||
|
var mblenTable = map[string]int{
|
||||||
|
"utf-8": 6,
|
||||||
|
"utf8": 6,
|
||||||
|
"jis": 8,
|
||||||
|
"eucjp": 3,
|
||||||
|
"euckr": 2,
|
||||||
|
"euccn": 2,
|
||||||
|
"sjis": 2,
|
||||||
|
"cp932": 2,
|
||||||
|
"cp51932": 2,
|
||||||
|
"cp936": 2,
|
||||||
|
"cp949": 2,
|
||||||
|
"cp950": 2,
|
||||||
|
"big5": 2,
|
||||||
|
"gbk": 2,
|
||||||
|
"gb2312": 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
func isEastAsian(locale string) bool {
|
||||||
|
charset := strings.ToLower(locale)
|
||||||
|
r := reLoc.FindStringSubmatch(locale)
|
||||||
|
if len(r) == 2 {
|
||||||
|
charset = strings.ToLower(r[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasSuffix(charset, "@cjk_narrow") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for pos, b := range []byte(charset) {
|
||||||
|
if b == '@' {
|
||||||
|
charset = charset[:pos]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
max := 1
|
||||||
|
if m, ok := mblenTable[charset]; ok {
|
||||||
|
max = m
|
||||||
|
}
|
||||||
|
if max > 1 && (charset[0] != 'u' ||
|
||||||
|
strings.HasPrefix(locale, "ja") ||
|
||||||
|
strings.HasPrefix(locale, "ko") ||
|
||||||
|
strings.HasPrefix(locale, "zh")) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsEastAsian return true if the current locale is CJK
|
||||||
|
func IsEastAsian() bool {
|
||||||
|
locale := os.Getenv("LC_CTYPE")
|
||||||
|
if locale == "" {
|
||||||
|
locale = os.Getenv("LANG")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ignore C locale
|
||||||
|
if locale == "POSIX" || locale == "C" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if len(locale) > 1 && locale[0] == 'C' && (locale[1] == '.' || locale[1] == '-') {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return isEastAsian(locale)
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package runewidth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
kernel32 = syscall.NewLazyDLL("kernel32")
|
||||||
|
procGetConsoleOutputCP = kernel32.NewProc("GetConsoleOutputCP")
|
||||||
|
)
|
||||||
|
|
||||||
|
// IsEastAsian return true if the current locale is CJK
|
||||||
|
func IsEastAsian() bool {
|
||||||
|
r1, _, _ := procGetConsoleOutputCP.Call()
|
||||||
|
if r1 == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch int(r1) {
|
||||||
|
case 932, 51932, 936, 949, 950:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
|
@ -816,6 +816,12 @@
|
||||||
"path": "github.com/hashicorp/yamux",
|
"path": "github.com/hashicorp/yamux",
|
||||||
"revision": "df949784da9ed028ee76df44652e42d37a09d7e4"
|
"revision": "df949784da9ed028ee76df44652e42d37a09d7e4"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"checksumSHA1": "28nznojcl6Ejtm6I1tKozgy0isg=",
|
||||||
|
"path": "github.com/jlaffaye/ftp",
|
||||||
|
"revision": "025815df64030c144b86e368fedf80ee195fe464",
|
||||||
|
"revisionTime": "2016-02-29T21:52:38Z"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "3/Bhy+ua/DCv2ElMD5GzOYSGN6g=",
|
"checksumSHA1": "3/Bhy+ua/DCv2ElMD5GzOYSGN6g=",
|
||||||
"comment": "0.2.2-2-gc01cf91",
|
"comment": "0.2.2-2-gc01cf91",
|
||||||
|
@ -917,6 +923,12 @@
|
||||||
"path": "github.com/mattn/go-isatty",
|
"path": "github.com/mattn/go-isatty",
|
||||||
"revision": "56b76bdf51f7708750eac80fa38b952bb9f32639"
|
"revision": "56b76bdf51f7708750eac80fa38b952bb9f32639"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"checksumSHA1": "MNkKJyk2TazKMJYbal5wFHybpyA=",
|
||||||
|
"path": "github.com/mattn/go-runewidth",
|
||||||
|
"revision": "14207d285c6c197daabb5c9793d63e7af9ab2d50",
|
||||||
|
"revisionTime": "2017-02-01T02:35:40Z"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "UP+pXl+ic9y6qrpZA5MqDIAuGfw=",
|
"checksumSHA1": "UP+pXl+ic9y6qrpZA5MqDIAuGfw=",
|
||||||
"path": "github.com/mitchellh/cli",
|
"path": "github.com/mitchellh/cli",
|
||||||
|
|
Loading…
Reference in New Issue