Skip to content

Commit 4660877

Browse files
committed
backport v2/client.Remove; return PathError
1 parent a2172dc commit 4660877

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

client.go

+37-14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717

1818
"github.com/kr/fs"
1919
"golang.org/x/crypto/ssh"
20+
21+
"github.com/pkg/sftp/internal/encoding/ssh/filexfer/openssh"
2022
)
2123

2224
var (
@@ -758,20 +760,33 @@ func (c *Client) Join(elem ...string) string { return path.Join(elem...) }
758760
// file or directory with the specified path exists, or if the specified directory
759761
// is not empty.
760762
func (c *Client) Remove(path string) error {
761-
err := c.removeFile(path)
762-
// some servers, *cough* osx *cough*, return EPERM, not ENODIR.
763-
// serv-u returns ssh_FX_FILE_IS_A_DIRECTORY
764-
// EPERM is converted to os.ErrPermission so it is not a StatusError
765-
if err, ok := err.(*StatusError); ok {
766-
switch err.Code {
767-
case sshFxFailure, sshFxFileIsADirectory:
768-
return c.RemoveDirectory(path)
769-
}
763+
errF := c.removeFile(path)
764+
if errF == nil {
765+
return nil
766+
}
767+
768+
errD := c.RemoveDirectory(path)
769+
if errD == nil {
770+
return nil
771+
}
772+
773+
// Both failed: figure out which error to return.
774+
775+
if errF == errD {
776+
// If they are the same error, then just return that.
777+
return errF
778+
}
779+
780+
fi, err := c.Stat(path)
781+
if err != nil {
782+
return err
770783
}
771-
if os.IsPermission(err) {
772-
return c.RemoveDirectory(path)
784+
785+
if fi.IsDir() {
786+
return errD
773787
}
774-
return err
788+
789+
return errF
775790
}
776791

777792
func (c *Client) removeFile(path string) error {
@@ -785,7 +800,11 @@ func (c *Client) removeFile(path string) error {
785800
}
786801
switch typ {
787802
case sshFxpStatus:
788-
return normaliseError(unmarshalStatus(id, data))
803+
return &PathError{
804+
Op: "remove",
805+
Path: path,
806+
Err: normaliseError(unmarshalStatus(id, data)),
807+
}
789808
default:
790809
return unimplementedPacketErr(typ)
791810
}
@@ -803,7 +822,11 @@ func (c *Client) RemoveDirectory(path string) error {
803822
}
804823
switch typ {
805824
case sshFxpStatus:
806-
return normaliseError(unmarshalStatus(id, data))
825+
return &PathError{
826+
Op: "rmdir",
827+
Path: path,
828+
Err: normaliseError(unmarshalStatus(id, data)),
829+
}
807830
default:
808831
return unimplementedPacketErr(typ)
809832
}

0 commit comments

Comments
 (0)