Skip to content

Commit

Permalink
server: use os.IsNotExist to map sshFxNoSuchFile
Browse files Browse the repository at this point in the history
Always use os.IsNotExist to identify any OS specific error types that
represent missing files or directories.  This resolves an issue on
Windows where some system errors (ENOTDIR) were not being identified as
'not found' errors and mapped to sshFxNoSuchFile.

fixes pkg#381
  • Loading branch information
willnorris committed Sep 14, 2020
1 parent ec5b80d commit 28f6fd9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion server.go
Expand Up @@ -594,7 +594,9 @@ func statusFromError(p ider, err error) sshFxpStatusPacket {
ret.StatusError.Code = translateErrno(e)
case *os.PathError:
debug("statusFromError,pathError: error is %T %#v", e.Err, e.Err)
if errno, ok := e.Err.(syscall.Errno); ok {
if os.IsNotExist(err) {
ret.StatusError.Code = sshFxNoSuchFile
} else if errno, ok := e.Err.(syscall.Errno); ok {
ret.StatusError.Code = translateErrno(errno)
}
case fxerr:
Expand Down
15 changes: 15 additions & 0 deletions server_test.go
Expand Up @@ -344,3 +344,18 @@ func TestOpenStatRace(t *testing.T) {
os.Remove(tmppath)
checkServerAllocator(t, server)
}

// Ensure that proper error codes are returned for non existent files, such
// that they are mapped back to a 'not exists' error on the client side.
func TestStatNonExistent(t *testing.T) {
client, server := clientServerPair(t)
defer client.Close()
defer server.Close()

for _, file := range []string{"/doesnotexist", "/doesnotexist/a/b"} {
_, err := client.Stat(file)
if err == nil || !os.IsNotExist(err) {
t.Errorf("expected 'does not exist' err for file %q. got: %v", file, err)
}
}
}

0 comments on commit 28f6fd9

Please sign in to comment.