Skip to content

Commit

Permalink
address review set 2
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin-ward committed Apr 5, 2024
1 parent 96b29c5 commit 8411183
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 156 deletions.
3 changes: 1 addition & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
iofs "io/fs"
"math"
"os"
"path"
Expand Down Expand Up @@ -2143,7 +2142,7 @@ func toPflags(f int) uint32 {
// setuid, setgid and sticky in m, because we've historically supported those
// bits, and we mask off any non-permission bits.
func toChmodPerm(m os.FileMode) (perm uint32) {
const mask = os.ModePerm | iofs.FileMode(s_ISUID|s_ISGID|s_ISVTX)
const mask = os.ModePerm | os.FileMode(s_ISUID|s_ISGID|s_ISVTX)
perm = uint32(m & mask)

if m&os.ModeSetuid != 0 {
Expand Down
3 changes: 1 addition & 2 deletions client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"fmt"
"io"
iofs "io/fs"
"io/ioutil"
"math/rand"
"net"
Expand Down Expand Up @@ -975,7 +974,7 @@ func TestClientSetuid(t *testing.T) {
f.Close()

const allPerm = os.ModePerm | os.ModeSetuid | os.ModeSetgid | os.ModeSticky |
iofs.FileMode(s_ISUID|s_ISGID|s_ISVTX)
os.FileMode(s_ISUID|s_ISGID|s_ISVTX)

for _, c := range []struct {
goPerm os.FileMode
Expand Down
42 changes: 42 additions & 0 deletions errno_plan9.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package sftp

import (
"os"
"syscall"
)

var EBADF = syscall.NewError("fd out of range or not open")

func wrapPathError(filepath string, err error) error {
if errno, ok := err.(syscall.ErrorString); ok {
return &os.PathError{Path: filepath, Err: errno}
}
return err
}

// translateErrno translates a syscall error number to a SFTP error code.
func translateErrno(errno syscall.ErrorString) uint32 {
switch errno {
case "":
return sshFxOk
case syscall.ENOENT:
return sshFxNoSuchFile
case syscall.EPERM:
return sshFxPermissionDenied
}

return sshFxFailure
}

func translateSyscallError(err error) (uint32, bool) {
switch e := err.(type) {
case syscall.ErrorString:
return translateErrno(e), true
case *os.PathError:
debug("statusFromError,pathError: error is %T %#v", e.Err, e.Err)
if errno, ok := e.Err.(syscall.ErrorString); ok {
return translateErrno(errno), true
}
}
return 0, false
}
45 changes: 45 additions & 0 deletions errno_posix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build !plan9
// +build !plan9

package sftp

import (
"os"
"syscall"
)

const EBADF = syscall.EBADF

func wrapPathError(filepath string, err error) error {
if errno, ok := err.(syscall.Errno); ok {
return &os.PathError{Path: filepath, Err: errno}
}
return err
}

// translateErrno translates a syscall error number to a SFTP error code.
func translateErrno(errno syscall.Errno) uint32 {
switch errno {
case 0:
return sshFxOk
case syscall.ENOENT:
return sshFxNoSuchFile
case syscall.EACCES, syscall.EPERM:
return sshFxPermissionDenied
}

return sshFxFailure
}

func translateSyscallError(err error) (uint32, bool) {
switch e := err.(type) {
case syscall.Errno:
return translateErrno(e), true
case *os.PathError:
debug("statusFromError,pathError: error is %T %#v", e.Err, e.Err)
if errno, ok := e.Err.(syscall.Errno); ok {
return translateErrno(errno), true
}
}
return 0, false
}
47 changes: 7 additions & 40 deletions stat_posix.go → stat.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,11 @@
//go:build !plan9
// +build !plan9

package sftp

import (
"os"
"syscall"

sshfx "github.com/pkg/sftp/internal/encoding/ssh/filexfer"
)

const EBADF = syscall.EBADF

func wrapPathError(filepath string, err error) error {
if errno, ok := err.(syscall.Errno); ok {
return &os.PathError{Path: filepath, Err: errno}
}
return err
}

// translateErrno translates a syscall error number to a SFTP error code.
func translateErrno(errno syscall.Errno) uint32 {
switch errno {
case 0:
return sshFxOk
case syscall.ENOENT:
return sshFxNoSuchFile
case syscall.EACCES, syscall.EPERM:
return sshFxPermissionDenied
}

return sshFxFailure
}

func translateSyscallError(err error) (uint32, bool) {
switch e := err.(type) {
case syscall.Errno:
return translateErrno(e), true
case *os.PathError:
debug("statusFromError,pathError: error is %T %#v", e.Err, e.Err)
if errno, ok := e.Err.(syscall.Errno); ok {
return translateErrno(errno), true
}
}
return 0, false
}

// isRegular returns true if the mode describes a regular file.
func isRegular(mode uint32) bool {
return sshfx.FileMode(mode)&sshfx.ModeType == sshfx.ModeRegular
Expand Down Expand Up @@ -124,3 +84,10 @@ const (
s_ISGID = uint32(sshfx.ModeSetGID)
s_ISVTX = uint32(sshfx.ModeSticky)
)

// Legacy export:
//
// Go defines S_IFMT on windows, plan9 and js/wasm as 0x1f000 instead of
// 0xf000. None of the the other S_IFxyz values include the "1" (in 0x1f000)
// which prevents them from matching the bitmask.
const S_IFMT = uint32(sshfx.ModeType)
103 changes: 0 additions & 103 deletions stat_plan9.go

This file was deleted.

9 changes: 0 additions & 9 deletions syscall_fixed.go

This file was deleted.

0 comments on commit 8411183

Please sign in to comment.