pkg / sftp

SFTP support for the go.crypto/ssh package
BSD 2-Clause "Simplified" License
1.52k stars 380 forks source link

sftp.Glob returns incorrect path of found file or dir #531

Open alexey-sderzhikov opened 2 years ago

alexey-sderzhikov commented 2 years ago

If we use pattern with trailing slash, like "/home/" and such object exist, than sftp.Glob duplicates object's name, and returns "/home/home". It happens because c.Lstat(pattern) finds existing object with trailing slash, but Split(pattern) doesn't split dir and object, and then Join(dir, file.Name()) joins duplicates.

func (c *Client) Glob(pattern string) (matches []string, err error) {
    if !hasMeta(pattern) {
        file, err := c.Lstat(pattern)
        if err != nil {
            return nil, nil
        }
        dir, _ := Split(pattern)
        dir = cleanGlobPath(dir)
        return []string{Join(dir, file.Name())}, nil
    }
....

I'am not sure how it should to work, but i think current logic not properly right. May i help?

puellanivis commented 2 years ago

Hm… yeah, this splitting and cleaning path code doesn’t seem to be in the original. In this specific path, it just returns []string{pattern}, nil.

Not sure what we can update by porting the current code over again. 🤔 But this at least is something we should probably fix.