diff --git a/transfer.go b/transfer.go index b3ff8d3..265000a 100644 --- a/transfer.go +++ b/transfer.go @@ -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 { @@ -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) @@ -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 diff --git a/transfer_test.go b/transfer_test.go index 27201c4..f22e3a3 100644 --- a/transfer_test.go +++ b/transfer_test.go @@ -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)