2020-09-11 00:11:47 +08:00
package sftp
import (
"os"
2024-04-06 00:37:27 +08:00
sshfx "github.com/pkg/sftp/internal/encoding/ssh/filexfer"
2020-09-11 00:11:47 +08:00
)
2021-04-27 00:51:04 +08:00
// isRegular returns true if the mode describes a regular file.
func isRegular ( mode uint32 ) bool {
2024-04-06 00:37:27 +08:00
return sshfx . FileMode ( mode ) & sshfx . ModeType == sshfx . ModeRegular
2021-04-27 00:51:04 +08:00
}
2020-09-11 00:11:47 +08:00
// toFileMode converts sftp filemode bits to the os.FileMode specification
func toFileMode ( mode uint32 ) os . FileMode {
var fm = os . FileMode ( mode & 0777 )
2021-08-11 21:10:07 +08:00
2024-04-06 00:37:27 +08:00
switch sshfx . FileMode ( mode ) & sshfx . ModeType {
case sshfx . ModeDevice :
2020-09-11 00:11:47 +08:00
fm |= os . ModeDevice
2024-04-06 00:37:27 +08:00
case sshfx . ModeCharDevice :
2020-09-11 00:11:47 +08:00
fm |= os . ModeDevice | os . ModeCharDevice
2024-04-06 00:37:27 +08:00
case sshfx . ModeDir :
2020-09-11 00:11:47 +08:00
fm |= os . ModeDir
2024-04-06 00:37:27 +08:00
case sshfx . ModeNamedPipe :
2020-09-11 00:11:47 +08:00
fm |= os . ModeNamedPipe
2024-04-06 00:37:27 +08:00
case sshfx . ModeSymlink :
2020-09-11 00:11:47 +08:00
fm |= os . ModeSymlink
2024-04-06 00:37:27 +08:00
case sshfx . ModeRegular :
2020-09-11 00:11:47 +08:00
// nothing to do
2024-04-06 00:37:27 +08:00
case sshfx . ModeSocket :
2020-09-11 00:11:47 +08:00
fm |= os . ModeSocket
}
2021-08-11 21:10:07 +08:00
2024-04-06 00:37:27 +08:00
if sshfx . FileMode ( mode ) & sshfx . ModeSetUID != 0 {
2020-09-11 00:11:47 +08:00
fm |= os . ModeSetuid
}
2024-04-06 00:37:27 +08:00
if sshfx . FileMode ( mode ) & sshfx . ModeSetGID != 0 {
2021-08-11 21:10:07 +08:00
fm |= os . ModeSetgid
}
2024-04-06 00:37:27 +08:00
if sshfx . FileMode ( mode ) & sshfx . ModeSticky != 0 {
2020-09-11 00:11:47 +08:00
fm |= os . ModeSticky
}
2021-08-11 21:10:07 +08:00
2020-09-11 00:11:47 +08:00
return fm
}
// fromFileMode converts from the os.FileMode specification to sftp filemode bits
func fromFileMode ( mode os . FileMode ) uint32 {
2024-04-06 00:37:27 +08:00
ret := sshfx . FileMode ( mode & os . ModePerm )
2020-09-11 00:11:47 +08:00
2021-08-11 21:10:07 +08:00
switch mode & os . ModeType {
case os . ModeDevice | os . ModeCharDevice :
2024-04-06 00:37:27 +08:00
ret |= sshfx . ModeCharDevice
2021-08-11 21:10:07 +08:00
case os . ModeDevice :
2024-04-06 00:37:27 +08:00
ret |= sshfx . ModeDevice
2021-08-11 21:10:07 +08:00
case os . ModeDir :
2024-04-06 00:37:27 +08:00
ret |= sshfx . ModeDir
2021-08-11 21:10:07 +08:00
case os . ModeNamedPipe :
2024-04-06 00:37:27 +08:00
ret |= sshfx . ModeNamedPipe
2021-08-11 21:10:07 +08:00
case os . ModeSymlink :
2024-04-06 00:37:27 +08:00
ret |= sshfx . ModeSymlink
2021-08-11 21:10:07 +08:00
case 0 :
2024-04-06 00:37:27 +08:00
ret |= sshfx . ModeRegular
2021-08-11 21:10:07 +08:00
case os . ModeSocket :
2024-04-06 00:37:27 +08:00
ret |= sshfx . ModeSocket
2020-09-11 00:11:47 +08:00
}
2021-08-11 21:10:07 +08:00
if mode & os . ModeSetuid != 0 {
2024-04-06 00:37:27 +08:00
ret |= sshfx . ModeSetUID
2020-09-11 00:11:47 +08:00
}
if mode & os . ModeSetgid != 0 {
2024-04-06 00:37:27 +08:00
ret |= sshfx . ModeSetGID
2020-09-11 00:11:47 +08:00
}
if mode & os . ModeSticky != 0 {
2024-04-06 00:37:27 +08:00
ret |= sshfx . ModeSticky
2020-09-11 00:11:47 +08:00
}
2024-04-06 00:37:27 +08:00
return uint32 ( ret )
2020-09-11 00:11:47 +08:00
}
Support os.Mode{Setuid,Setgid,Sticky} in Chmod
Previously, these bits were ignored by Chmod, which sent the numerical
value of the mode argument as-is to the server. As a result, callers had
to supply POSIX values for setuid, setgid and sticky to Chmod.
The new version supports both the POSIX values and the Go values.
Also added a note to the docs to clarify that the umask is not
subtracted from the mode, and why that is. The only portable way to get
the umask is to set it, then reset it, but that's racy. On Linux, we
could parse /proc/self/status, but that doesn't work portably and will
fail where /proc is not available (some Docker containers, notably).
2021-03-12 00:37:08 +08:00
const (
2024-04-06 00:37:27 +08:00
s_ISUID = uint32 ( sshfx . ModeSetUID )
s_ISGID = uint32 ( sshfx . ModeSetGID )
s_ISVTX = uint32 ( sshfx . ModeSticky )
Support os.Mode{Setuid,Setgid,Sticky} in Chmod
Previously, these bits were ignored by Chmod, which sent the numerical
value of the mode argument as-is to the server. As a result, callers had
to supply POSIX values for setuid, setgid and sticky to Chmod.
The new version supports both the POSIX values and the Go values.
Also added a note to the docs to clarify that the umask is not
subtracted from the mode, and why that is. The only portable way to get
the umask is to set it, then reset it, but that's racy. On Linux, we
could parse /proc/self/status, but that doesn't work portably and will
fail where /proc is not available (some Docker containers, notably).
2021-03-12 00:37:08 +08:00
)
2024-04-06 04:07:40 +08:00
2024-04-09 02:34:16 +08:00
// S_IFMT is a legacy export, and was brought in to support GOOS environments whose sysconfig.S_IFMT may be different from the value used internally by SFTP standards.
// There should be no reason why you need to import it, or use it, but unexporting it could cause code to break in a way that cannot be readily fixed.
// As such, we continue to export this value as the value used in the SFTP standard.
//
// Deprecated: Remove use of this value, and avoid any future use as well.
// There is no alternative provided, you should never need to access this value.
2024-04-06 04:07:40 +08:00
const S_IFMT = uint32 ( sshfx . ModeType )