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