Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Stat based on MLST command #1

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions ftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const (
// Time format used by the MDTM and MFMT commands
const timeFormat = "20060102150405"

var ErrNotSupported = errors.New("Not supported by remote FTP server")

// ServerConn represents the connection to a remote FTP server.
// A single connection only supports one in-flight data connection.
// It is not safe to be called concurrently.
Expand Down Expand Up @@ -654,6 +656,35 @@ func (c *ServerConn) List(path string) (entries []*Entry, err error) {
return entries, err
}

// Stat issues an MLST command which returns info for a single entry
func (c *ServerConn) Stat(path string) (entry *Entry, err error) {
if !c.mlstSupported {
return nil, ErrNotSupported
}

cmd := "MLST"
parser := parseRFC3659ListLine

space := " "
if path == "" {
space = ""
}

_, msg, errCmd := c.cmd(StatusRequestedFileActionOK, "%s%s%s", cmd, space, path)
if errCmd != nil {
return nil, errCmd
}

scanner := bufio.NewScanner(strings.NewReader(msg))
for scanner.Scan() {
parseEntry, parseErr := parser(strings.TrimLeft(scanner.Text(), " "), time.Now(), c.options.location)
if parseErr == nil {
return parseEntry, parseErr
}
}
return nil, scanner.Err()
}

// IsTimePreciseInList returns true if client and server support the MLSD
// command so List can return time with 1-second precision for all files.
func (c *ServerConn) IsTimePreciseInList() bool {
Expand Down