diff options
| author | Shulhan <ms@kilabit.info> | 2023-09-10 23:21:24 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-09-11 02:26:33 +0700 |
| commit | 08c0fd19b75ea4a4b1cb75c72bfca43d49b4d555 (patch) | |
| tree | 2789ad30c8593a18cd8ac5d4d804443d638c88c9 /lib/ssh | |
| parent | 19abc9b968eef3a2ded89009dfb647f927729b16 (diff) | |
| download | pakakeh.go-08c0fd19b75ea4a4b1cb75c72bfca43d49b4d555.tar.xz | |
ssh/sftp: realign struct for better size allocation
The realignment reduce the cost of the following struct,
* Client: from 88 to 80 bytes (-8)
* dirEntry: from 40 to 32 bytes (-8)
* FileAttrs: from 72 to 64 bytes (-8)
* packet: from 128 to 88 bytes (-40)
While at it, add missing comment to FileHandle type.
Diffstat (limited to 'lib/ssh')
| -rw-r--r-- | lib/ssh/sftp/client.go | 9 | ||||
| -rw-r--r-- | lib/ssh/sftp/dir_entry.go | 3 | ||||
| -rw-r--r-- | lib/ssh/sftp/file_attrs.go | 23 | ||||
| -rw-r--r-- | lib/ssh/sftp/file_handle.go | 1 | ||||
| -rw-r--r-- | lib/ssh/sftp/packet.go | 27 |
5 files changed, 36 insertions, 27 deletions
diff --git a/lib/ssh/sftp/client.go b/lib/ssh/sftp/client.go index 6d5da1ab..dbad2f0b 100644 --- a/lib/ssh/sftp/client.go +++ b/lib/ssh/sftp/client.go @@ -18,8 +18,8 @@ import ( // Client for SFTP. type Client struct { - sess *ssh.Session - version uint32 + sess *ssh.Session + exts extensions pipeIn io.WriteCloser pipeOut io.Reader @@ -27,8 +27,11 @@ type Client struct { // The requestId is unique number that will be incremented by client, // to prevent the same ID generated on concurrent operations. - mtxId sync.Mutex requestId uint32 + + version uint32 + + mtxId sync.Mutex } // NewClient create and initialize new client for SSH file transfer protocol. diff --git a/lib/ssh/sftp/dir_entry.go b/lib/ssh/sftp/dir_entry.go index dae071c0..dd3ef4e3 100644 --- a/lib/ssh/sftp/dir_entry.go +++ b/lib/ssh/sftp/dir_entry.go @@ -9,9 +9,10 @@ import "os" // dirEntry represent the internal data returned from Readdir, Readlink, or // Realpath. type dirEntry struct { + attrs *FileAttrs + fileName string longName string - attrs *FileAttrs } func (de *dirEntry) Name() string { diff --git a/lib/ssh/sftp/file_attrs.go b/lib/ssh/sftp/file_attrs.go index ab93c114..a5e8c870 100644 --- a/lib/ssh/sftp/file_attrs.go +++ b/lib/ssh/sftp/file_attrs.go @@ -36,16 +36,20 @@ const ( // FileAttrs define the attributes for opening or creating file on the remote. type FileAttrs struct { - name string + exts extensions // attr_EXTENDED + + name string + + fsMode fs.FileMode + + size uint64 // attr_SIZE + flags uint32 - size uint64 // attr_SIZE - uid uint32 // attr_UIDGID - gid uint32 // attr_UIDGID - permissions uint32 // attr_PERMISSIONS - atime uint32 // attr_ACMODTIME - mtime uint32 // attr_ACMODTIME - exts extensions // attr_EXTENDED - fsMode fs.FileMode + uid uint32 // attr_UIDGID + gid uint32 // attr_UIDGID + permissions uint32 // attr_PERMISSIONS + atime uint32 // attr_ACMODTIME + mtime uint32 // attr_ACMODTIME } // NewFileAttrs create and initialize FileAttrs from FileInfo. @@ -191,6 +195,7 @@ func (fa *FileAttrs) Mode() fs.FileMode { return fa.fsMode } +// Name return the name of file. func (fa *FileAttrs) Name() string { return fa.name } diff --git a/lib/ssh/sftp/file_handle.go b/lib/ssh/sftp/file_handle.go index 71c4ed81..d20b01b9 100644 --- a/lib/ssh/sftp/file_handle.go +++ b/lib/ssh/sftp/file_handle.go @@ -4,6 +4,7 @@ package sftp +// FileHandle define the container to store remote file. type FileHandle struct { remotePath string // The remote path. v []byte // The handle value returned from open(). diff --git a/lib/ssh/sftp/packet.go b/lib/ssh/sftp/packet.go index 373281e2..4f7b24f3 100644 --- a/lib/ssh/sftp/packet.go +++ b/lib/ssh/sftp/packet.go @@ -47,21 +47,16 @@ const ( // ) type packet struct { - length uint32 - kind byte - requestId uint32 + // FxpHandle + fh *FileHandle - // FxpVersion. - version uint32 - exts extensions + // FxpAttrs + fa *FileAttrs - // FxpStatus - code uint32 - message string - languageTag string + exts extensions // from FxpVersion. - // FxpHandle - fh *FileHandle + message string // from FxpStatus + languageTag string // from FxpStatus // FxpData data []byte @@ -69,8 +64,12 @@ type packet struct { // FxpName nodes []*dirEntry - // FxpAttrs - fa *FileAttrs + version uint32 // from FxpVersion. + code uint32 // from FxpStatus + length uint32 + requestId uint32 + + kind byte } func unpackPacket(payload []byte) (pac *packet, err error) { |
