Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor sshfx encoding, fix link rot, go fmt #545

Merged
merged 2 commits into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion attrs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sftp

// ssh_FXP_ATTRS support
// see http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02#section-5
// see https://filezilla-project.org/specs/draft-ietf-secsh-filexfer-02.txt#section-5

import (
"os"
Expand Down
1 change: 1 addition & 0 deletions attrs_stubs.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build plan9 || windows || android
// +build plan9 windows android

package sftp
Expand Down
1 change: 1 addition & 0 deletions attrs_unix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build darwin || dragonfly || freebsd || (!android && linux) || netbsd || openbsd || solaris || aix || js
// +build darwin dragonfly freebsd !android,linux netbsd openbsd solaris aix js

package sftp
Expand Down
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,11 @@ func (c *Client) Create(path string) (*File, error) {
return c.open(path, flags(os.O_RDWR|os.O_CREATE|os.O_TRUNC))
}

const sftpProtocolVersion = 3 // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02
const sftpProtocolVersion = 3 // https://filezilla-project.org/specs/draft-ietf-secsh-filexfer-02.txt

func (c *Client) sendInit() error {
return c.clientConn.conn.sendPacket(&sshFxInitPacket{
Version: sftpProtocolVersion, // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02
Version: sftpProtocolVersion, // https://filezilla-project.org/specs/draft-ietf-secsh-filexfer-02.txt
})
}

Expand Down
1 change: 1 addition & 0 deletions debug.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build debug
// +build debug

package sftp
Expand Down
1 change: 1 addition & 0 deletions fuzz.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build gofuzz
// +build gofuzz

package sftp
Expand Down
117 changes: 44 additions & 73 deletions internal/encoding/ssh/filexfer/attrs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package filexfer
package sshfx
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the expected name of this package, so that importers know the preferred name it should be renamed to.


// Attributes related flags.
const (
Expand All @@ -12,7 +12,7 @@ const (

// Attributes defines the file attributes type defined in draft-ietf-secsh-filexfer-02
//
// Defined in: https://tools.ietf.org/html/draft-ietf-secsh-filexfer-02#section-5
// Defined in: https://filezilla-project.org/specs/draft-ietf-secsh-filexfer-02.txt#section-5
type Attributes struct {
Flags uint32

Expand Down Expand Up @@ -116,32 +116,32 @@ func (a *Attributes) Len() int {
}

// MarshalInto marshals e onto the end of the given Buffer.
func (a *Attributes) MarshalInto(b *Buffer) {
b.AppendUint32(a.Flags)
func (a *Attributes) MarshalInto(buf *Buffer) {
buf.AppendUint32(a.Flags)

if a.Flags&AttrSize != 0 {
b.AppendUint64(a.Size)
buf.AppendUint64(a.Size)
}

if a.Flags&AttrUIDGID != 0 {
b.AppendUint32(a.UID)
b.AppendUint32(a.GID)
buf.AppendUint32(a.UID)
buf.AppendUint32(a.GID)
}

if a.Flags&AttrPermissions != 0 {
b.AppendUint32(uint32(a.Permissions))
buf.AppendUint32(uint32(a.Permissions))
}

if a.Flags&AttrACModTime != 0 {
b.AppendUint32(a.ATime)
b.AppendUint32(a.MTime)
buf.AppendUint32(a.ATime)
buf.AppendUint32(a.MTime)
}

if a.Flags&AttrExtended != 0 {
b.AppendUint32(uint32(len(a.ExtendedAttributes)))
buf.AppendUint32(uint32(len(a.ExtendedAttributes)))

for _, ext := range a.ExtendedAttributes {
ext.MarshalInto(b)
ext.MarshalInto(buf)
}
}
}
Expand All @@ -156,74 +156,51 @@ func (a *Attributes) MarshalBinary() ([]byte, error) {
// UnmarshalFrom unmarshals an Attributes from the given Buffer into e.
//
// NOTE: The values of fields not covered in the a.Flags are explicitly undefined.
func (a *Attributes) UnmarshalFrom(b *Buffer) (err error) {
flags, err := b.ConsumeUint32()
if err != nil {
return err
}
func (a *Attributes) UnmarshalFrom(buf *Buffer) (err error) {
flags := buf.ConsumeUint32()

return a.XXX_UnmarshalByFlags(flags, b)
return a.XXX_UnmarshalByFlags(flags, buf)
}

// XXX_UnmarshalByFlags uses the pre-existing a.Flags field to determine which fields to decode.
// DO NOT USE THIS: it is an anti-corruption function to implement existing internal usage in pkg/sftp.
// This function is not a part of any compatibility promise.
func (a *Attributes) XXX_UnmarshalByFlags(flags uint32, b *Buffer) (err error) {
func (a *Attributes) XXX_UnmarshalByFlags(flags uint32, buf *Buffer) (err error) {
a.Flags = flags

// Short-circuit dummy attributes.
if a.Flags == 0 {
return nil
return buf.Err
}

if a.Flags&AttrSize != 0 {
if a.Size, err = b.ConsumeUint64(); err != nil {
return err
}
a.Size = buf.ConsumeUint64()
}

if a.Flags&AttrUIDGID != 0 {
if a.UID, err = b.ConsumeUint32(); err != nil {
return err
}

if a.GID, err = b.ConsumeUint32(); err != nil {
return err
}
a.UID = buf.ConsumeUint32()
a.GID = buf.ConsumeUint32()
}

if a.Flags&AttrPermissions != 0 {
m, err := b.ConsumeUint32()
if err != nil {
return err
}

a.Permissions = FileMode(m)
a.Permissions = FileMode(buf.ConsumeUint32())
}

if a.Flags&AttrACModTime != 0 {
if a.ATime, err = b.ConsumeUint32(); err != nil {
return err
}

if a.MTime, err = b.ConsumeUint32(); err != nil {
return err
}
a.ATime = buf.ConsumeUint32()
a.MTime = buf.ConsumeUint32()
}

if a.Flags&AttrExtended != 0 {
count, err := b.ConsumeUint32()
if err != nil {
return err
}
count := buf.ConsumeCount()

a.ExtendedAttributes = make([]ExtendedAttribute, count)
for i := range a.ExtendedAttributes {
a.ExtendedAttributes[i].UnmarshalFrom(b)
a.ExtendedAttributes[i].UnmarshalFrom(buf)
}
}

return nil
return buf.Err
}

// UnmarshalBinary decodes the binary encoding of Attributes into e.
Expand All @@ -233,7 +210,7 @@ func (a *Attributes) UnmarshalBinary(data []byte) error {

// ExtendedAttribute defines the extended file attribute type defined in draft-ietf-secsh-filexfer-02
//
// Defined in: https://tools.ietf.org/html/draft-ietf-secsh-filexfer-02#section-5
// Defined in: https://filezilla-project.org/specs/draft-ietf-secsh-filexfer-02.txt#section-5
type ExtendedAttribute struct {
Type string
Data string
Expand All @@ -245,9 +222,9 @@ func (e *ExtendedAttribute) Len() int {
}

// MarshalInto marshals e onto the end of the given Buffer.
func (e *ExtendedAttribute) MarshalInto(b *Buffer) {
b.AppendString(e.Type)
b.AppendString(e.Data)
func (e *ExtendedAttribute) MarshalInto(buf *Buffer) {
buf.AppendString(e.Type)
buf.AppendString(e.Data)
}

// MarshalBinary returns e as the binary encoding of e.
Expand All @@ -258,16 +235,13 @@ func (e *ExtendedAttribute) MarshalBinary() ([]byte, error) {
}

// UnmarshalFrom unmarshals an ExtendedAattribute from the given Buffer into e.
func (e *ExtendedAttribute) UnmarshalFrom(b *Buffer) (err error) {
if e.Type, err = b.ConsumeString(); err != nil {
return err
func (e *ExtendedAttribute) UnmarshalFrom(buf *Buffer) (err error) {
*e = ExtendedAttribute{
Type: buf.ConsumeString(),
Data: buf.ConsumeString(),
}

if e.Data, err = b.ConsumeString(); err != nil {
return err
}

return nil
return buf.Err
}

// UnmarshalBinary decodes the binary encoding of ExtendedAttribute into e.
Expand All @@ -290,11 +264,11 @@ func (e *NameEntry) Len() int {
}

// MarshalInto marshals e onto the end of the given Buffer.
func (e *NameEntry) MarshalInto(b *Buffer) {
b.AppendString(e.Filename)
b.AppendString(e.Longname)
func (e *NameEntry) MarshalInto(buf *Buffer) {
buf.AppendString(e.Filename)
buf.AppendString(e.Longname)

e.Attrs.MarshalInto(b)
e.Attrs.MarshalInto(buf)
}

// MarshalBinary returns e as the binary encoding of e.
Expand All @@ -307,16 +281,13 @@ func (e *NameEntry) MarshalBinary() ([]byte, error) {
// UnmarshalFrom unmarshals an NameEntry from the given Buffer into e.
//
// NOTE: The values of fields not covered in the a.Flags are explicitly undefined.
func (e *NameEntry) UnmarshalFrom(b *Buffer) (err error) {
if e.Filename, err = b.ConsumeString(); err != nil {
return err
}

if e.Longname, err = b.ConsumeString(); err != nil {
return err
func (e *NameEntry) UnmarshalFrom(buf *Buffer) (err error) {
*e = NameEntry{
Filename: buf.ConsumeString(),
Longname: buf.ConsumeString(),
}

return e.Attrs.UnmarshalFrom(b)
return e.Attrs.UnmarshalFrom(buf)
}

// UnmarshalBinary decodes the binary encoding of NameEntry into e.
Expand Down
2 changes: 1 addition & 1 deletion internal/encoding/ssh/filexfer/attrs_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package filexfer
package sshfx

import (
"bytes"
Expand Down
Loading