Skip to content

Commit 52de9a1

Browse files
plietarSergioBenitez
authored andcommitted
Ensure 'TempFile' flushes when persisted.
Tokio's `File::write_all()` method has an unexpected quirk: it doesn't actually write all the requested content to the file when the returned future resolves. Instead, the write is attempted and queued. This means that the `persist()` method can resolve without the data being persisted to the file system. Subsequent reads of the ostensibly written-to file can thus fail to contain the expected data. An call to `flush()` following `write_all()` would circumvent the issue. Alternatively, calling `fs::write()` actually writes to the file system before returning and requires fewer lines of code. This commit thus swaps the call to `write_all()` with `fs::write()`.
1 parent 61e77c5 commit 52de9a1

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

core/lib/src/fs/temp_file.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::fs::FileName;
1010

1111
use tokio::task;
1212
use tokio::fs::{self, File};
13-
use tokio::io::{AsyncWriteExt, AsyncBufRead, BufReader};
13+
use tokio::io::{AsyncBufRead, BufReader};
1414
use tempfile::{NamedTempFile, TempPath};
1515
use either::Either;
1616

@@ -189,8 +189,7 @@ impl<'v> TempFile<'v> {
189189
}
190190
}
191191
TempFile::Buffered { content } => {
192-
let mut file = File::create(&new_path).await?;
193-
file.write_all(content).await?;
192+
fs::write(&new_path, &content).await?;
194193
*self = TempFile::File {
195194
file_name: None,
196195
content_type: None,
@@ -228,10 +227,12 @@ impl<'v> TempFile<'v> {
228227
/// # let some_other_path = std::env::temp_dir().join("some-other.txt");
229228
/// file.copy_to(&some_other_path).await?;
230229
/// assert_eq!(file.path(), Some(&*some_path));
230+
/// # assert_eq!(std::fs::read(some_path).unwrap(), b"hi");
231+
/// # assert_eq!(std::fs::read(some_other_path).unwrap(), b"hi");
231232
///
232233
/// Ok(())
233234
/// }
234-
/// # let file = TempFile::Buffered { content: "hi".as_bytes() };
235+
/// # let file = TempFile::Buffered { content: b"hi" };
235236
/// # rocket::async_test(handle(file)).unwrap();
236237
/// ```
237238
pub async fn copy_to<P>(&mut self, path: P) -> io::Result<()>
@@ -257,8 +258,7 @@ impl<'v> TempFile<'v> {
257258
}
258259
TempFile::Buffered { content } => {
259260
let path = path.as_ref();
260-
let mut file = File::create(path).await?;
261-
file.write_all(content).await?;
261+
fs::write(&path, &content).await?;
262262
*self = TempFile::File {
263263
file_name: None,
264264
content_type: None,
@@ -393,10 +393,11 @@ impl<'v> TempFile<'v> {
393393
/// # let some_path = std::env::temp_dir().join("some-path.txt");
394394
/// file.persist_to(&some_path).await?;
395395
/// assert_eq!(file.path(), Some(&*some_path));
396+
/// # assert_eq!(std::fs::read(some_path).unwrap(), b"hi");
396397
///
397398
/// Ok(())
398399
/// }
399-
/// # let file = TempFile::Buffered { content: "hi".as_bytes() };
400+
/// # let file = TempFile::Buffered { content: b"hi" };
400401
/// # rocket::async_test(handle(file)).unwrap();
401402
/// ```
402403
pub fn path(&self) -> Option<&Path> {

0 commit comments

Comments
 (0)