-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
fs: add read_at/write_at/seek_read/seek_write for fs::File #6427
Open
SteveLauC
wants to merge
6
commits into
tokio-rs:master
Choose a base branch
from
SteveLauC:feat/pread_pwrite
base: master
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.
+272
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3d548d8
fs: read/write_at for unix & seek_read/write for win
SteveLauC 0274f3a
test: add test for seek_read/seek_write
SteveLauC ffe6451
style: fmt
SteveLauC dbb1b7e
test: fix windows test
SteveLauC 488d5f2
test: fix windows test
SteveLauC c98e22f
test: fix windows test
SteveLauC 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
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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#![warn(rust_2018_idioms)] | ||
#![cfg(all(feature = "full", not(target_os = "wasi")))] | ||
|
||
#[tokio::test] | ||
#[cfg(unix)] | ||
async fn read_at() { | ||
use tempfile::tempdir; | ||
use tokio::fs; | ||
use tokio::io::AsyncSeekExt; | ||
|
||
let temp_dir = tempdir().unwrap(); | ||
let file_path = temp_dir.path().join("a.txt"); | ||
fs::write(&file_path, b"HelloWorld").await.unwrap(); | ||
let mut file = fs::File::open(file_path.as_path()).await.unwrap(); | ||
|
||
let mut buf = [0_u8; 10]; | ||
assert_eq!(file.read_at(&mut buf, 5).await.unwrap(), 5); | ||
assert_eq!(&buf[..5], b"World"); | ||
|
||
assert_eq!(file.stream_position().await.unwrap(), 0); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#![warn(rust_2018_idioms)] | ||
#![cfg(all(feature = "full", not(target_os = "wasi")))] | ||
|
||
#[tokio::test] | ||
#[cfg(windows)] | ||
async fn seek_read() { | ||
use tempfile::tempdir; | ||
use tokio::fs; | ||
use tokio::io::AsyncSeekExt; | ||
|
||
let temp_dir = tempdir().unwrap(); | ||
let file_path = temp_dir.path().join("a.txt"); | ||
fs::write(&file_path, b"HelloWorld").await.unwrap(); | ||
let mut file = fs::File::open(file_path.as_path()).await.unwrap(); | ||
|
||
let mut buf = [0_u8; 10]; | ||
assert_eq!(file.seek_read(&mut buf, 5).await.unwrap(), 5); | ||
assert_eq!(&buf[..5], b"World"); | ||
|
||
assert_eq!(file.stream_position().await.unwrap(), 10); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#![warn(rust_2018_idioms)] | ||
#![cfg(all(feature = "full", not(target_os = "wasi")))] | ||
|
||
#[tokio::test] | ||
#[cfg(windows)] | ||
async fn seek_write() { | ||
use tempfile::tempdir; | ||
use tokio::fs; | ||
use tokio::fs::OpenOptions; | ||
use tokio::io::AsyncSeekExt; | ||
|
||
let temp_dir = tempdir().unwrap(); | ||
let file_path = temp_dir.path().join("a.txt"); | ||
fs::write(&file_path, b"Hello File").await.unwrap(); | ||
let mut file = OpenOptions::new() | ||
.write(true) | ||
.open(file_path.as_path()) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(file.seek_write(b"World", 5).await.unwrap(), 5); | ||
let contents = fs::read(file_path.as_path()).await.unwrap(); | ||
assert_eq!(contents, b"HelloWorld"); | ||
|
||
assert_eq!(file.stream_position().await.unwrap(), 10); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#![warn(rust_2018_idioms)] | ||
#![cfg(all(feature = "full", not(target_os = "wasi")))] | ||
|
||
#[tokio::test] | ||
#[cfg(unix)] | ||
async fn write_at() { | ||
use tempfile::tempdir; | ||
use tokio::fs; | ||
use tokio::fs::OpenOptions; | ||
use tokio::io::AsyncSeekExt; | ||
|
||
let temp_dir = tempdir().unwrap(); | ||
let file_path = temp_dir.path().join("a.txt"); | ||
fs::write(&file_path, b"Hello File").await.unwrap(); | ||
let mut file = OpenOptions::new() | ||
.write(true) | ||
.open(file_path.as_path()) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(file.write_at(b"World", 5).await.unwrap(), 5); | ||
let contents = fs::read(file_path.as_path()).await.unwrap(); | ||
assert_eq!(contents, b"HelloWorld"); | ||
|
||
assert_eq!(file.stream_position().await.unwrap(), 0); | ||
} |
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.
The Tokio file already has a bunch of logic for keeping track of and reusing a buffer, but none of these functions use that logic. Is there a reason you're doing it this way?
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.
Well, I am not aware of this, do you mean the
bytes
crate? If not, would you like to show me some examples?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.
No, I mean the stuff related to these types:
tokio/tokio/src/fs/file.rs
Lines 96 to 118 in c98e22f
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.
Thanks for showing me this!
I just took another look at other methods implemented with
spawn_blocking()
,seems that I should add:before involving the actual logic, right?Update: Well, seems more complicated than I thought, I originally thought we could simply implement it using
asyncify()
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.
One could probably argue either way about whether it's even desireable. One advantage of
read_at
is that you can have several calls in parallel, but if we use the state logic, then they will run one-after-the-other.I'm not sure what the best answer here is.
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.
Thanks for pointing it out! The capability of enabling concurrent access is indeed the reason why
pread/pwrite
are added, so I slightly tend to use separate buffersThere 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.
I have a tentative plan, can we use
RwLock
instead ofMutex
to achieve sharing among multiple readers and allowing them run in parallel?tokio/tokio/src/fs/file.rs
Line 92 in c98e22f
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.
@Chasing1020 A read/write lock doesn't help. Fundamentally, the shared buffer can only hold one piece of data at the time.