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 RetrieveOffset Method #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 12 additions & 4 deletions transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (

// Retrieve file "path" from server and write bytes to "dest". If the
// server supports resuming stream transfers, Retrieve will continue
// resuming a failed download as long as it continues making progress.
// Retrieve will also verify the file's size after the transfer if the
// at the specified offset.
// RetrieveOffset will also verify the file's size after the transfer if the
// server supports the SIZE command.
func (c *Client) Retrieve(path string, dest io.Writer) error {
func (c *Client) RetrieveOffset(path string, dest io.Writer, bytesSoFar int64) error {
// fetch file size to check against how much we transferred
size, err := c.size(path)
if err != nil {
Expand All @@ -25,7 +25,6 @@ func (c *Client) Retrieve(path string, dest io.Writer) error {

canResume := c.canResume()

var bytesSoFar int64
for {
n, err := c.transferFromOffset(path, dest, nil, bytesSoFar)

Expand Down Expand Up @@ -53,6 +52,15 @@ func (c *Client) Retrieve(path string, dest io.Writer) error {
return nil
}

// Retrieve file "path" from server and write bytes to "dest". If the
// server supports resuming stream transfers, Retrieve will continue
// resuming a failed download as long as it continues making progress.
// Retrieve will also verify the file's size after the transfer if the
// server supports the SIZE command.
func (c *Client) Retrieve(path string, dest io.Writer) error {
return c.RetrieveOffset(path, dest, 0)
}

// Store bytes read from "src" into file "path" on the server. If the
// server supports resuming stream transfers and "src" is an io.Seeker
// (*os.File is an io.Seeker), Store will continue resuming a failed upload
Expand Down
29 changes: 29 additions & 0 deletions transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,35 @@ import (
"time"
)

func TestRetrieveOffset(t *testing.T) {
for _, addr := range ftpdAddrs {
c, err := DialConfig(goftpConfig, addr)

if err != nil {
t.Fatal(err)
}


buf := new(bytes.Buffer)

err = c.RetrieveOffset("subdir/1234.bin", buf, 1)

if err != nil {
t.Fatal(err)
}

if !bytes.Equal([]byte{2, 3, 4}, buf.Bytes()) {
t.Errorf("Got %v", buf.Bytes())
}

err = c.RetrieveOffset("subdir/1234.bin", buf, 5)

if err == nil {
t.Error("BytesSoFar is greater than the source File and should throw an error")
}
}
}

func TestRetrieve(t *testing.T) {
for _, addr := range ftpdAddrs {
c, err := DialConfig(goftpConfig, addr)
Expand Down