sftp/attrs.go

98 lines
2.4 KiB
Go
Raw Normal View History

2013-11-05 13:28:45 +08:00
package sftp
2013-11-06 16:12:25 +08:00
// ssh_FXP_ATTRS support
2013-11-05 13:28:45 +08:00
// see http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02#section-5
import (
"os"
"time"
)
const (
sshFileXferAttrSize = 0x00000001
sshFileXferAttrUIDGID = 0x00000002
sshFileXferAttrPermissions = 0x00000004
sshFileXferAttrACmodTime = 0x00000008
2021-03-20 18:46:58 +08:00
sshFileXferAttrExtended = 0x80000000
sshFileXferAttrAll = sshFileXferAttrSize | sshFileXferAttrUIDGID | sshFileXferAttrPermissions |
2021-03-20 18:46:58 +08:00
sshFileXferAttrACmodTime | sshFileXferAttrExtended
2013-11-05 13:28:45 +08:00
)
// fileInfo is an artificial type designed to satisfy os.FileInfo.
type fileInfo struct {
2014-09-27 05:56:26 +08:00
name string
size int64
mode os.FileMode
2013-11-05 13:28:45 +08:00
mtime time.Time
2014-09-27 05:56:26 +08:00
sys interface{}
2013-11-05 13:28:45 +08:00
}
// Name returns the base name of the file.
func (fi *fileInfo) Name() string { return fi.name }
2013-11-05 13:28:45 +08:00
// Size returns the length in bytes for regular files; system-dependent for others.
func (fi *fileInfo) Size() int64 { return fi.size }
2013-11-05 13:28:45 +08:00
// Mode returns file mode bits.
func (fi *fileInfo) Mode() os.FileMode { return fi.mode }
2013-11-05 13:28:45 +08:00
// ModTime returns the last modification time of the file.
func (fi *fileInfo) ModTime() time.Time { return fi.mtime }
2013-11-05 13:28:45 +08:00
// IsDir returns true if the file is a directory.
func (fi *fileInfo) IsDir() bool { return fi.Mode().IsDir() }
2013-11-05 13:28:45 +08:00
func (fi *fileInfo) Sys() interface{} { return fi.sys }
2013-11-05 13:28:45 +08:00
// FileStat holds the original unmarshalled values from a call to READDIR or
// *STAT. It is exported for the purposes of accessing the raw values via
// os.FileInfo.Sys(). It is also used server side to store the unmarshalled
// values for SetStat.
type FileStat struct {
2014-09-27 05:56:26 +08:00
Size uint64
Mode uint32
Mtime uint32
Atime uint32
2016-01-05 05:15:21 +08:00
UID uint32
GID uint32
Extended []StatExtended
}
// StatExtended contains additional, extended information for a FileStat.
type StatExtended struct {
ExtType string
ExtData string
}
func fileInfoFromStat(st *FileStat, name string) os.FileInfo {
fs := &fileInfo{
2014-09-27 05:56:26 +08:00
name: name,
size: int64(st.Size),
mode: toFileMode(st.Mode),
mtime: time.Unix(int64(st.Mtime), 0),
2014-09-27 05:56:26 +08:00
sys: st,
}
return fs
}
2015-09-20 10:09:03 +08:00
func fileStatFromInfo(fi os.FileInfo) (uint32, FileStat) {
mtime := fi.ModTime().Unix()
atime := mtime
var flags uint32 = sshFileXferAttrSize |
sshFileXferAttrPermissions |
sshFileXferAttrACmodTime
2015-09-20 10:09:03 +08:00
fileStat := FileStat{
Size: uint64(fi.Size()),
Mode: fromFileMode(fi.Mode()),
Mtime: uint32(mtime),
Atime: uint32(atime),
}
// os specific file stat decoding
fileStatFromInfoOs(fi, &flags, &fileStat)
return flags, fileStat
}