@@ -17,6 +17,8 @@ import (
17
17
18
18
"github.com/kr/fs"
19
19
"golang.org/x/crypto/ssh"
20
+
21
+ "github.com/pkg/sftp/internal/encoding/ssh/filexfer/openssh"
20
22
)
21
23
22
24
var (
@@ -758,20 +760,33 @@ func (c *Client) Join(elem ...string) string { return path.Join(elem...) }
758
760
// file or directory with the specified path exists, or if the specified directory
759
761
// is not empty.
760
762
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
770
783
}
771
- if os .IsPermission (err ) {
772
- return c .RemoveDirectory (path )
784
+
785
+ if fi .IsDir () {
786
+ return errD
773
787
}
774
- return err
788
+
789
+ return errF
775
790
}
776
791
777
792
func (c * Client ) removeFile (path string ) error {
@@ -785,7 +800,11 @@ func (c *Client) removeFile(path string) error {
785
800
}
786
801
switch typ {
787
802
case sshFxpStatus :
788
- return normaliseError (unmarshalStatus (id , data ))
803
+ return & PathError {
804
+ Op : "remove" ,
805
+ Path : path ,
806
+ Err : normaliseError (unmarshalStatus (id , data )),
807
+ }
789
808
default :
790
809
return unimplementedPacketErr (typ )
791
810
}
@@ -803,7 +822,11 @@ func (c *Client) RemoveDirectory(path string) error {
803
822
}
804
823
switch typ {
805
824
case sshFxpStatus :
806
- return normaliseError (unmarshalStatus (id , data ))
825
+ return & PathError {
826
+ Op : "rmdir" ,
827
+ Path : path ,
828
+ Err : normaliseError (unmarshalStatus (id , data )),
829
+ }
807
830
default :
808
831
return unimplementedPacketErr (typ )
809
832
}
0 commit comments