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// File 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&ssh_FXF_READ != 0,
17		Write:  flags&ssh_FXF_WRITE != 0,
18		Append: flags&ssh_FXF_APPEND != 0,
19		Creat:  flags&ssh_FXF_CREAT != 0,
20		Trunc:  flags&ssh_FXF_TRUNC != 0,
21		Excl:   flags&ssh_FXF_EXCL != 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// Flags 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 & ssh_FILEXFER_ATTR_SIZE) != 0,
41		UidGid:      (flags & ssh_FILEXFER_ATTR_UIDGID) != 0,
42		Permissions: (flags & ssh_FILEXFER_ATTR_PERMISSIONS) != 0,
43		Acmodtime:   (flags & ssh_FILEXFER_ATTR_ACMODTIME) != 0,
44	}
45}
46
47// FileAttrFlags 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// Attributres 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