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

Add Client.Sync method #386

Merged
merged 1 commit into from
Oct 23, 2020
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
20 changes: 20 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,26 @@ func (f *File) Chmod(mode os.FileMode) error {
return f.c.Chmod(f.path, mode)
}

// Sync requests a flush of the contents of a File to stable storage.
//
// Sync requires the server to support the [email protected] extension.
func (f *File) Sync() error {
id := f.c.nextID()
typ, data, err := f.c.sendPacket(sshFxpFsyncPacket{
ID: id,
Handle: f.handle,
})

switch {
case err != nil:
return err
case typ == sshFxpStatus:
return normaliseError(unmarshalStatus(id, data))
default:
return &unexpectedPacketErr{want: sshFxpStatus, got: typ}
}
}

// Truncate sets the size of the current file. Although it may be safely assumed
// that if the size is less than its current size it will be truncated to fit,
// the SFTP protocol does not specify what behavior the server should do when setting
Expand Down
45 changes: 45 additions & 0 deletions client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"time"

"github.com/kr/fs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -1434,6 +1436,49 @@ func clientReadDeadlock(t *testing.T, N int, badfunc func(*File)) {
badfunc(r)
}

func TestClientSyncGo(t *testing.T) {
if !*testServerImpl {
t.Skipf("skipping without -testserver")
}
err := testClientSync(t)

// Since Server does not support the fsync extension, we can only
// check that we get the right error.
require.NotNil(t, err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oof, I didn’t check the usage of require and assert as closely as I should have. 🤦‍♀️


switch err := err.(type) {
case *StatusError:
assert.Equal(t, ErrSSHFxOpUnsupported, err.FxCode())
default:
t.Error(err)
}
}

func TestClientSyncSFTP(t *testing.T) {
if *testServerImpl {
t.Skipf("skipping with -testserver")
}
err := testClientSync(t)
assert.Nil(t, err)
}

func testClientSync(t *testing.T) error {
sftp, cmd := testClient(t, READWRITE, NODELAY)
defer cmd.Wait()
defer sftp.Close()

d, err := ioutil.TempDir("", "sftptest.sync")
require.Nil(t, err)
defer os.RemoveAll(d)

f := path.Join(d, "syncTest")
w, err := sftp.Create(f)
require.Nil(t, err)
defer w.Close()

return w.Sync()
}

// taken from github.com/kr/fs/walk_test.go

type Node struct {
Expand Down
20 changes: 20 additions & 0 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,26 @@ func (p *StatVFS) MarshalBinary() ([]byte, error) {
return buf.Bytes(), err
}

type sshFxpFsyncPacket struct {
ID uint32
Handle string
}

func (p sshFxpFsyncPacket) id() uint32 { return p.ID }

func (p sshFxpFsyncPacket) MarshalBinary() ([]byte, error) {
puellanivis marked this conversation as resolved.
Show resolved Hide resolved
l := 1 + 4 + // type (byte) + ID (uint32)
4 + len("[email protected]") +
4 + len(p.Handle)

b := make([]byte, 0, l)
b = append(b, sshFxpExtended)
b = marshalUint32(b, p.ID)
b = marshalString(b, "[email protected]")
b = marshalString(b, p.Handle)
return b, nil
}

type sshFxpExtendedPacket struct {
ID uint32
ExtendedRequest string
Expand Down