8000 Make request attribute structs public for docs · etherscan-io/sftp@0ef30ab · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ef30ab

Browse files
committed
Make request attribute structs public for docs
Make the structures used to hold the file flags/attributes public so they can be documented. Improve the names to make their use clearer and make the docs more obvious. Fixes pkg#245
1 parent 2d92cdc commit 0ef30ab

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

attrs.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ func (fi *fileInfo) IsDir() bool { return fi.Mode().IsDir() }
4343

4444
func (fi *fileInfo) Sys() interface{} { return fi.sys }
4545

46-
// FileStat holds the original unmarshalled values from a call to READDIR or *STAT.
47-
// It is exported for the purposes of accessing the raw values via os.FileInfo.Sys()
46+
// FileStat holds the original unmarshalled values from a call to READDIR or
47+
// *STAT. It is exported for the purposes of accessing the raw values via
48+
// os.FileInfo.Sys(). It is also used server side to store the unmarshalled
49+
// values for SetStat.
4850
type FileStat struct {
4951
Size uint64
5052
Mode uint32

request-attrs.go

Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ package sftp
33
// Methods on the Request object to make working with the Flags bitmasks and
44
// Attr(ibutes) byte blob easier. Use Pflags() when working with an Open/Write
55
// request and AttrFlags() and Attributes() when working with SetStat requests.
6-
76
import "os"
87

9-
// Open packet pflags
10-
type pflags struct {
8+
// File Open and Write Flags. Correlate directly with with os.OpenFile flags
9+
// (https://golang.org/pkg/os/#pkg-constants).
10+
type FileOpenFlags struct {
1111
Read, Write, Append, Creat, Trunc, Excl bool
1212
}
1313

14-
// testable constructor
15-
func newPflags(flags uint32) pflags {
16-
return pflags{
14+
func newFileOpenFlags(flags uint32) FileOpenFlags {
15+
return FileOpenFlags{
1716
Read: flags&ssh_FXF_READ != 0,
1817
Write: flags&ssh_FXF_WRITE != 0,
1918
Append: flags&ssh_FXF_APPEND != 0,
@@ -23,41 +22,42 @@ func newPflags(flags uint32) pflags {
2322
}
2423
}
2524

26-
// Check bitmap/uint32 for Open packet pflag values
27-
func (r *Request) Pflags() pflags {
28-
return newPflags(r.Flags)
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.
27+
func (r *Request) Pflags() FileOpenFlags {
28+
return newFileOpenFlags(r.Flags)
2929
}
3030

31-
// File attribute flags
32-
type aflags struct {
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.
34+
type FileAttrFlags struct {
3335
Size, UidGid, Permissions, Acmodtime bool
3436
}
3537

36-
// testable constructor
37-
func newAflags(flags uint32) aflags {
38-
return aflags{
38+
func newFileAttrFlags(flags uint32) FileAttrFlags {
39+
return FileAttrFlags{
3940
Size: (flags & ssh_FILEXFER_ATTR_SIZE) != 0,
4041
UidGid: (flags & ssh_FILEXFER_ATTR_UIDGID) != 0,
4142
Permissions: (flags & ssh_FILEXFER_ATTR_PERMISSIONS) != 0,
4243
Acmodtime: (flags & ssh_FILEXFER_ATTR_ACMODTIME) != 0,
4344
}
4445
}
4546

46-
// Check bitmap/uint32 for file attribute flags
47-
func (r *Request) AttrFlags(flags uint32) aflags {
48-
return newAflags(r.Flags)
47+
// FileAttrFlags returns a FileAttrFlags boolean struct based on the
48+
// bitmap/uint32 file attribute flags from the SFTP packaet.
49+
func (r *Request) AttrFlags() FileAttrFlags {
50+
return newFileAttrFlags(r.Flags)
4951
}
5052

51-
// File attributes
52-
type fileattrs FileStat
53-
54-
// Return Mode wrapped in os.FileMode
55-
func (a fileattrs) FileMode() os.FileMode {
53+
// FileMode returns the Mode SFTP file attributes wrapped as os.FileMode
54+
func (a FileStat) FileMode() os.FileMode {
5655
return os.FileMode(a.Mode)
5756
}
5857

59-
// Parse file attributes byte blob and return them in object
60-
func (r *Request) Attributes() fileattrs {
61-
fa, _ := getFileStat(r.Flags, r.Attrs)
62-
return fileattrs(*fa)
58+
// Attributres parses file attributes byte blob and return them in a
59+
// FileStat object.
60+
func (r *Request) Attributes() *FileStat {
61+
fs, _ := getFileStat(r.Flags, r.Attrs)
62+
return fs
6363
}

request-attrs_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func TestRequestPflags(t *testing.T) {
12-
pflags := newPflags(ssh_FXF_READ | ssh_FXF_WRITE | ssh_FXF_APPEND)
12+
pflags := newFileOpenFlags(ssh_FXF_READ | ssh_FXF_WRITE | ssh_FXF_APPEND)
1313
assert.True(t, pflags.Read)
1414
assert.True(t, pflags.Write)
1515
assert.True(t, pflags.Append)
@@ -19,7 +19,8 @@ func TestRequestPflags(t *testing.T) {
1919
}
2020

2121
func TestRequestAflags(t *testing.T) {
22-
aflags := newAflags(ssh_FILEXFER_ATTR_SIZE | ssh_FILEXFER_ATTR_UIDGID)
22+
aflags := newFileAttrFlags(
23+
ssh_FILEXFER_ATTR_SIZE | ssh_FILEXFER_ATTR_UIDGID)
2324
assert.True(t, aflags.Size)
2425
assert.True(t, aflags.UidGid)
2526
assert.False(t, aflags.Acmodtime)
@@ -28,24 +29,23 @@ func TestRequestAflags(t *testing.T) {
2829

2930
func TestRequestAttributes(t *testing.T) {
3031
// UID/GID
31-
fa := fileattrs{UID: 1, GID: 2}
32+
fa := FileStat{UID: 1, GID: 2}
3233
fl := uint32(ssh_FILEXFER_ATTR_UIDGID)
3334
at := []byte{}
3435
at = marshalUint32(at, 1)
3536
at = marshalUint32(at, 2)
3637
test_fs, _ := getFileStat(fl, at)
37-
assert.Equal(t, fa, fileattrs(*test_fs))
38+
assert.Equal(t, fa, *test_fs)
3839
// Size and Mode
39-
fa = fileattrs{Mode: 700, Size: 99}
40+
fa = FileStat{Mode: 700, Size: 99}
4041
fl = uint32(ssh_FILEXFER_ATTR_SIZE | ssh_FILEXFER_ATTR_PERMISSIONS)
4142
at = []byte{}
4243
at = marshalUint64(at, 99)
4344
at = marshalUint32(at, 700)
4445
test_fs, _ = getFileStat(fl, at)
45-
test_fa := fileattrs(*test_fs)
46-
assert.Equal(t, fa, test_fa)
46+
assert.Equal(t, fa, *test_fs)
4747
// FileMode
48-
assert.True(t, test_fa.FileMode().IsRegular())
49-
assert.False(t, test_fa.FileMode().IsDir())
50-
assert.Equal(t, test_fa.FileMode().Perm(), os.FileMode(700).Perm())
48+
assert.True(t, test_fs.FileMode().IsRegular())
49+
assert.False(t, test_fs.FileMode().IsDir())
50+
assert.Equal(t, test_fs.FileMode().Perm(), os.FileMode(700).Perm())
5151
}

0 commit comments

Comments
 (0)
0