request-attrs.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package sftp
  2. // Methods on the Request object to make working with the Flags bitmasks and
  3. // Attr(ibutes) byte blob easier. Use Pflags() when working with an Open/Write
  4. // request and AttrFlags() and Attributes() when working with SetStat requests.
  5. import "os"
  6. // FileOpenFlags defines Open and Write Flags. Correlate directly with with os.OpenFile flags
  7. // (https://golang.org/pkg/os/#pkg-constants).
  8. type FileOpenFlags struct {
  9. Read, Write, Append, Creat, Trunc, Excl bool
  10. }
  11. func newFileOpenFlags(flags uint32) FileOpenFlags {
  12. return FileOpenFlags{
  13. Read: flags&sshFxfRead != 0,
  14. Write: flags&sshFxfWrite != 0,
  15. Append: flags&sshFxfAppend != 0,
  16. Creat: flags&sshFxfCreat != 0,
  17. Trunc: flags&sshFxfTrunc != 0,
  18. Excl: flags&sshFxfExcl != 0,
  19. }
  20. }
  21. // Pflags converts the bitmap/uint32 from SFTP Open packet pflag values,
  22. // into a FileOpenFlags struct with booleans set for flags set in bitmap.
  23. func (r *Request) Pflags() FileOpenFlags {
  24. return newFileOpenFlags(r.Flags)
  25. }
  26. // FileAttrFlags that indicate whether SFTP file attributes were passed. When a flag is
  27. // true the corresponding attribute should be available from the FileStat
  28. // object returned by Attributes method. Used with SetStat.
  29. type FileAttrFlags struct {
  30. Size, UidGid, Permissions, Acmodtime bool
  31. }
  32. func newFileAttrFlags(flags uint32) FileAttrFlags {
  33. return FileAttrFlags{
  34. Size: (flags & sshFileXferAttrSize) != 0,
  35. UidGid: (flags & sshFileXferAttrUIDGID) != 0,
  36. Permissions: (flags & sshFileXferAttrPermissions) != 0,
  37. Acmodtime: (flags & sshFileXferAttrACmodTime) != 0,
  38. }
  39. }
  40. // AttrFlags returns a FileAttrFlags boolean struct based on the
  41. // bitmap/uint32 file attribute flags from the SFTP packaet.
  42. func (r *Request) AttrFlags() FileAttrFlags {
  43. return newFileAttrFlags(r.Flags)
  44. }
  45. // FileMode returns the Mode SFTP file attributes wrapped as os.FileMode
  46. func (a FileStat) FileMode() os.FileMode {
  47. return os.FileMode(a.Mode)
  48. }
  49. // Attributes parses file attributes byte blob and return them in a
  50. // FileStat object.
  51. func (r *Request) Attributes() *FileStat {
  52. fs, _ := unmarshalFileStat(r.Flags, r.Attrs)
  53. return fs
  54. }