ftp
ftp copied to clipboard
Can't parse LIST response line with parseDirListLine
Describe the bug
I'm using the List
function with a FTP server that returns a line like this:
08-03-2021 09:59AM <DIR> SomeDirectory
When debugging the parseDirListLine
function, it doesn't recognize the dirTimeFormat.
To Reproduce
A program like this:
func main() {
c, err := ftp.Dial("ftp.example.com:21", ftp.DialWithTimeout(5*time.Second))
if err != nil {
log.Fatal(err)
}
err = c.Login("user", "pass")
if err != nil {
log.Fatal(err)
}
entries, err := c.List("/")
if err != nil {
log.Fatal(err)
}
for _, entry := range entries {
log.Println(entry.Name)
}
if err := c.Quit(); err != nil {
log.Fatal(err)
}
}
Expected behavior
It returns and prints the directory as entry.
FTP server
- Name and version:
- Public URL if applicable
> quote SYST
215 Windows_NT
> quote FEAT
211-Extended features supported:
LANG EN*
UTF8
AUTH TLS;TLS-C;SSL;TLS-P;
PBSZ
PROT C;P;
CCC
HOST
SIZE
MDTM
REST STREAM
211 END
Suggested solution
I think it could be fixed by adding 01-02-2006 03:04PM
to the dirTimeFormats
in parse.go
.
I also encountered similar problems on Kylin OS. Kylin OS return dirTimeFormat is "02 01月 2006 15:04" when using LIST function So I added a new time parse in parse.go file
func (e *Entry) setTime(fields []string, now time.Time, loc *time.Location) (err error) {
if strings.Contains(fields[2], ":") { // contains time
thisYear, _, _ := now.Date()
timeStr := fmt.Sprintf("%s %s %d %s", fields[1], fields[0], thisYear, fields[2])
e.Time, err = time.ParseInLocation("_2 Jan 2006 15:04", timeStr, loc)
// added code
if err != nil{
layout := "02 01月 2006 15:04"
e.Time, err = time.ParseInLocation(layout, timeStr, loc)
}
// xxxxx
}
}
Same problem here with a Windows_NT FTP server that returns files and directories with a "01-02-2006 03:04PM"
format.
I modified the parse.go
source with the adequate formats :
var dirTimeFormats = []string{
"01-02-06 03:04PM",
"01-02-2006 03:04PM",
"2006-01-02 15:04",
"2006-01-02 03:04PM",
"2006-01-02 03:04PM",
}
Problem is that the list func
returns now the file and dir names but without the time stamps.
This appears to have been fixed in https://github.com/jlaffaye/ftp/pull/354 but no new release has been created. Should work with pseudo modules by doing go get github.com/jlaffaye/ftp@master
to pull in the latest master commit.