Skip to content

Commit

Permalink
Do not use the system's tmp dir
Browse files Browse the repository at this point in the history
Renaming a file does not work across filesystems.

co-authored-by: Issac Kelly <[email protected]>
  • Loading branch information
teohhanhui and issackelly committed Oct 23, 2023
1 parent bb5f678 commit 9919db9
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/fs_store.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::{HashMap, HashSet},
fs::File,
io::Write,
path::{Path, PathBuf},
str,
Expand Down Expand Up @@ -192,15 +193,15 @@ fn write_chunk(
name: SavedChunkName,
) -> Result<(), Error> {
// Write to a temp file and then rename to avoid partial writes
let mut temp_save =
tempfile::NamedTempFile::new().map_err(|e| Error(ErrorKind::CreateTempFile(e)))?;
let temp_save_path = temp_save.path().to_owned();
temp_save
.as_file_mut()
let temp_dir =
tempfile::TempDir::new_in(root).map_err(|e| Error(ErrorKind::CreateTempFile(e)))?;
let temp_save_path = temp_dir.path().join(name.filename());
let mut temp_save_file =
File::create(&temp_save_path).map_err(|e| Error(ErrorKind::CreateTempFile(e)))?;
temp_save_file
.write_all(chunk)
.map_err(|e| Error(ErrorKind::WriteTempFile(temp_save_path.clone(), e)))?;
temp_save
.as_file_mut()
temp_save_file
.sync_all()
.map_err(|e| Error(ErrorKind::WriteTempFile(temp_save_path.clone(), e)))?;

Expand Down

0 comments on commit 9919db9

Please sign in to comment.