attrs.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package sftp
  2. // ssh_FXP_ATTRS support
  3. // see https://filezilla-project.org/specs/draft-ietf-secsh-filexfer-02.txt#section-5
  4. import (
  5. "os"
  6. "time"
  7. )
  8. const (
  9. sshFileXferAttrSize = 0x00000001
  10. sshFileXferAttrUIDGID = 0x00000002
  11. sshFileXferAttrPermissions = 0x00000004
  12. sshFileXferAttrACmodTime = 0x00000008
  13. sshFileXferAttrExtended = 0x80000000
  14. sshFileXferAttrAll = sshFileXferAttrSize | sshFileXferAttrUIDGID | sshFileXferAttrPermissions |
  15. sshFileXferAttrACmodTime | sshFileXferAttrExtended
  16. )
  17. // fileInfo is an artificial type designed to satisfy os.FileInfo.
  18. type fileInfo struct {
  19. name string
  20. stat *FileStat
  21. }
  22. // Name returns the base name of the file.
  23. func (fi *fileInfo) Name() string { return fi.name }
  24. // Size returns the length in bytes for regular files; system-dependent for others.
  25. func (fi *fileInfo) Size() int64 { return int64(fi.stat.Size) }
  26. // Mode returns file mode bits.
  27. func (fi *fileInfo) Mode() os.FileMode { return toFileMode(fi.stat.Mode) }
  28. // ModTime returns the last modification time of the file.
  29. func (fi *fileInfo) ModTime() time.Time { return time.Unix(int64(fi.stat.Mtime), 0) }
  30. // IsDir returns true if the file is a directory.
  31. func (fi *fileInfo) IsDir() bool { return fi.Mode().IsDir() }
  32. func (fi *fileInfo) Sys() interface{} { return fi.stat }
  33. // FileStat holds the original unmarshalled values from a call to READDIR or
  34. // *STAT. It is exported for the purposes of accessing the raw values via
  35. // os.FileInfo.Sys(). It is also used server side to store the unmarshalled
  36. // values for SetStat.
  37. type FileStat struct {
  38. Size uint64
  39. Mode uint32
  40. Mtime uint32
  41. Atime uint32
  42. UID uint32
  43. GID uint32
  44. Extended []StatExtended
  45. }
  46. // StatExtended contains additional, extended information for a FileStat.
  47. type StatExtended struct {
  48. ExtType string
  49. ExtData string
  50. }
  51. func fileInfoFromStat(stat *FileStat, name string) os.FileInfo {
  52. return &fileInfo{
  53. name: name,
  54. stat: stat,
  55. }
  56. }
  57. // FileInfoUidGid extends os.FileInfo and adds callbacks for Uid and Gid retrieval,
  58. // as an alternative to *syscall.Stat_t objects on unix systems.
  59. type FileInfoUidGid interface {
  60. os.FileInfo
  61. Uid() uint32
  62. Gid() uint32
  63. }
  64. // FileInfoUidGid extends os.FileInfo and adds a callbacks for extended data retrieval.
  65. type FileInfoExtendedData interface {
  66. os.FileInfo
  67. Extended() []StatExtended
  68. }
  69. func fileStatFromInfo(fi os.FileInfo) (uint32, *FileStat) {
  70. mtime := fi.ModTime().Unix()
  71. atime := mtime
  72. var flags uint32 = sshFileXferAttrSize |
  73. sshFileXferAttrPermissions |
  74. sshFileXferAttrACmodTime
  75. fileStat := &FileStat{
  76. Size: uint64(fi.Size()),
  77. Mode: fromFileMode(fi.Mode()),
  78. Mtime: uint32(mtime),
  79. Atime: uint32(atime),
  80. }
  81. // os specific file stat decoding
  82. fileStatFromInfoOs(fi, &flags, fileStat)
  83. // The call above will include the sshFileXferAttrUIDGID in case
  84. // the os.FileInfo can be casted to *syscall.Stat_t on unix.
  85. // If fi implements FileInfoUidGid, retrieve Uid, Gid from it instead.
  86. if fiExt, ok := fi.(FileInfoUidGid); ok {
  87. flags |= sshFileXferAttrUIDGID
  88. fileStat.UID = fiExt.Uid()
  89. fileStat.GID = fiExt.Gid()
  90. }
  91. // if fi implements FileInfoExtendedData, retrieve extended data from it
  92. if fiExt, ok := fi.(FileInfoExtendedData); ok {
  93. fileStat.Extended = fiExt.Extended()
  94. if len(fileStat.Extended) > 0 {
  95. flags |= sshFileXferAttrExtended
  96. }
  97. }
  98. return flags, fileStat
  99. }