-
Notifications
You must be signed in to change notification settings - Fork 837
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
Fix LocalFileSystem with range request that ends beyond end of file #6751
Draft
kylebarron
wants to merge
6
commits into
apache:main
Choose a base branch
from
kylebarron:kyle/local-file-range-request
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+51
−10
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
65b50ed
Fix LocalFileSystem with range request that ends beyond end of file
kylebarron b1dd9b3
fix windows
kylebarron 5f73204
add comment
kylebarron f1ff1cd
Seek error
kylebarron 7c9aed7
fix seek check
kylebarron 03de68d
remove windows flag
kylebarron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -897,23 +897,26 @@ pub(crate) fn chunked_stream( | |
|
||
pub(crate) fn read_range(file: &mut File, path: &PathBuf, range: Range<usize>) -> Result<Bytes> { | ||
let to_read = range.end - range.start; | ||
file.seek(SeekFrom::Start(range.start as u64)) | ||
let seek_idx = file | ||
.seek(SeekFrom::Start(range.start as u64)) | ||
.context(SeekSnafu { path })?; | ||
|
||
let mut buf = Vec::with_capacity(to_read); | ||
let read = file | ||
.take(to_read as u64) | ||
.read_to_end(&mut buf) | ||
.context(UnableToReadBytesSnafu { path })?; | ||
|
||
ensure!( | ||
read == to_read, | ||
seek_idx == range.start as u64, | ||
OutOfRangeSnafu { | ||
path, | ||
expected: to_read, | ||
actual: read | ||
expected: range.start, | ||
actual: seek_idx as usize | ||
} | ||
); | ||
|
||
let mut buf = Vec::with_capacity(to_read); | ||
file.take(to_read as u64) | ||
.read_to_end(&mut buf) | ||
.context(UnableToReadBytesSnafu { path })?; | ||
Comment on lines
+914
to
+916
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise, this read will simply return 0 when reading beyond EOF. |
||
|
||
// The output buffer could be smaller than `to_read` bytes if the end of the range is beyond | ||
// the end of the file. | ||
Ok(buf.into()) | ||
} | ||
|
||
|
@@ -1155,6 +1158,44 @@ mod tests { | |
assert_eq!(&*read_data, data); | ||
} | ||
|
||
#[tokio::test] | ||
async fn range_request_start_beyond_end_of_file() { | ||
let root = TempDir::new().unwrap(); | ||
let integration = LocalFileSystem::new_with_prefix(root.path()).unwrap(); | ||
|
||
let location = Path::from("some_file"); | ||
|
||
let data = Bytes::from("arbitrary data"); | ||
|
||
integration | ||
.put(&location, data.clone().into()) | ||
.await | ||
.unwrap(); | ||
|
||
integration | ||
.get_range(&location, 100..200) | ||
.await | ||
.expect_err("Should error with start range beyond end of file"); | ||
} | ||
|
||
#[tokio::test] | ||
async fn range_request_beyond_end_of_file() { | ||
let root = TempDir::new().unwrap(); | ||
let integration = LocalFileSystem::new_with_prefix(root.path()).unwrap(); | ||
|
||
let location = Path::from("some_file"); | ||
|
||
let data = Bytes::from("arbitrary data"); | ||
|
||
integration | ||
.put(&location, data.clone().into()) | ||
.await | ||
.unwrap(); | ||
|
||
let read_data = integration.get_range(&location, 0..100).await.unwrap(); | ||
assert_eq!(&*read_data, data); | ||
} | ||
|
||
#[tokio::test] | ||
#[cfg(target_family = "unix")] | ||
// Fails on github actions runner (which runs the tests as root) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears this will always be true, even if seeking beyond EOF.