Skip to content

Commit

Permalink
[azblob] Fix file offset post sequential write in downloadFile (#22357)
Browse files Browse the repository at this point in the history
* fix file offset post download

* changelog update

* lint error fix

* error verifications add

* file pointer check in test case

* check error from seek function call
  • Loading branch information
tanyasethi-msft authored Feb 8, 2024
1 parent f7748e7 commit 9964cfc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions sdk/storage/azblob/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Bugs Fixed
* Fix concurrency issue while Downloading File. Fixes [#22156](https://github.com/Azure/azure-sdk-for-go/issues/22156).
* Fix file offset update after Download file. Fixes [#22297](https://github.com/Azure/azure-sdk-for-go/issues/22297).

### Other Changes

Expand Down
15 changes: 14 additions & 1 deletion sdk/storage/azblob/blob/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,11 @@ func (b *Client) DownloadFile(ctx context.Context, file *os.File, o *DownloadFil
}
do := (*downloadOptions)(o)

filePointer, err := file.Seek(0, io.SeekCurrent)
if err != nil {
return 0, err
}

// 1. Calculate the size of the destination file
var size int64

Expand Down Expand Up @@ -624,7 +629,15 @@ func (b *Client) DownloadFile(ctx context.Context, file *os.File, o *DownloadFil
}

if size > 0 {
return b.downloadFile(ctx, file, *do)
writeSize, err := b.downloadFile(ctx, file, *do)
if err != nil {
return 0, err
}
_, err = file.Seek(filePointer, io.SeekStart)
if err != nil {
return 0, err
}
return writeSize, nil
} else { // if the blob's size is 0, there is no need in downloading it
return 0, nil
}
Expand Down
9 changes: 7 additions & 2 deletions sdk/storage/azblob/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ func performUploadAndDownloadFileTest(t *testing.T, _require *require.Assertions
})
assert.NoError(t, errTransferred)
_require.NoError(err)
//_require.Equal(response.StatusCode, 201)

// Set up file to download the blob to
destFileName := "BigFile-downloaded.bin"
Expand All @@ -411,6 +410,9 @@ func performUploadAndDownloadFileTest(t *testing.T, _require *require.Assertions

}(destFileName)

filePointer, err := file.Seek(0, io.SeekCurrent)
_require.NoError(err)

// Perform download
_, err = client.DownloadFile(context.Background(),
containerName,
Expand All @@ -435,6 +437,10 @@ func performUploadAndDownloadFileTest(t *testing.T, _require *require.Assertions
assert.NoError(t, errTransferred)
_require.NoError(err)

currPointer, err := destFile.Seek(0, io.SeekCurrent)
_require.Equal(filePointer, currPointer)
_require.NoError(err)

// Assert downloaded data is consistent
var destBuffer []byte
if downloadCount == blob.CountToEnd {
Expand All @@ -443,7 +449,6 @@ func performUploadAndDownloadFileTest(t *testing.T, _require *require.Assertions
destBuffer = make([]byte, downloadCount)
}

_, err = destFile.Seek(0, 0)
_require.NoError(err)
n, err := destFile.Read(destBuffer)
_require.NoError(err)
Expand Down

0 comments on commit 9964cfc

Please sign in to comment.