Skip to content

Don't flush files if they weren't written to #364

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

Merged
merged 1 commit into from Oct 28, 2019
Merged
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
35 changes: 20 additions & 15 deletions src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ pub struct File {
}

impl File {
/// Creates an async file handle.
pub(crate) fn new(file: std::fs::File, is_flushed: bool) -> File {
let file = Arc::new(file);

File {
file: file.clone(),
lock: Lock::new(State {
file,
mode: Mode::Idle,
cache: Vec::new(),
is_flushed,
last_read_err: None,
last_write_err: None,
}),
}
}

/// Opens a file in read-only mode.
///
/// See the [`OpenOptions::open`] function for more options.
Expand Down Expand Up @@ -96,7 +113,7 @@ impl File {
pub async fn open<P: AsRef<Path>>(path: P) -> io::Result<File> {
let path = path.as_ref().to_owned();
let file = blocking::spawn(move || std::fs::File::open(&path)).await?;
Ok(file.into())
Ok(File::new(file, true))
}

/// Opens a file in write-only mode.
Expand Down Expand Up @@ -131,7 +148,7 @@ impl File {
pub async fn create<P: AsRef<Path>>(path: P) -> io::Result<File> {
let path = path.as_ref().to_owned();
let file = blocking::spawn(move || std::fs::File::create(&path)).await?;
Ok(file.into())
Ok(File::new(file, true))
}

/// Synchronizes OS-internal buffered contents and metadata to disk.
Expand Down Expand Up @@ -383,19 +400,7 @@ impl Seek for &File {

impl From<std::fs::File> for File {
fn from(file: std::fs::File) -> File {
let file = Arc::new(file);

File {
file: file.clone(),
lock: Lock::new(State {
file,
mode: Mode::Idle,
cache: Vec::new(),
is_flushed: false,
last_read_err: None,
last_write_err: None,
}),
}
File::new(file, false)
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/fs/open_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ impl OpenOptions {
pub fn open<P: AsRef<Path>>(&self, path: P) -> impl Future<Output = io::Result<File>> {
let path = path.as_ref().to_owned();
let options = self.0.clone();
async move { blocking::spawn(move || options.open(path).map(|f| f.into())).await }
async move {
let file = blocking::spawn(move || options.open(path)).await?;
Ok(File::new(file, true))
}
}
}

Expand Down