Skip to content
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
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,15 +1134,15 @@ impl StateFile {
if !utils::is_file(&self.path) {
return Ok(State::default());
}
let content = utils::read_file("state", &self.path)?;
let content = utils::read_locked_file("state", &self.path)?;
State::parse(&content).with_context(|| RustupError::ParsingFile {
name: "state",
path: self.path.clone(),
})
}

fn store(&self, state: &State) -> Result<()> {
utils::write_file("state", &self.path, &state.stringify()?)?;
utils::write_locked_file("state", &self.path, &state.stringify()?)?;
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl SettingsFile {

fn write_settings(&self) -> Result<()> {
let settings = self.cache.borrow();
utils::write_file(
utils::write_locked_file(
"settings",
&self.path,
&settings.as_ref().unwrap().stringify()?,
Expand All @@ -44,7 +44,7 @@ impl SettingsFile {
if b.is_none() {
drop(b);
*self.cache.borrow_mut() = Some(if utils::is_file(&self.path) {
let content = utils::read_file("settings", &self.path)?;
let content = utils::read_locked_file("settings", &self.path)?;
Settings::parse(&content).with_context(|| RustupError::ParsingFile {
name: "settings",
path: self.path.clone(),
Expand Down
14 changes: 14 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,27 @@ pub fn read_file(name: &'static str, path: &Path) -> Result<String> {
})
}

pub(crate) fn read_locked_file(name: &'static str, path: &Path) -> Result<String> {
raw::read_locked_file(path).with_context(|| RustupError::ReadingFile {
name,
path: PathBuf::from(path),
})
}

pub fn write_file(name: &'static str, path: &Path, contents: &str) -> Result<()> {
raw::write_file(path, contents).with_context(|| RustupError::WritingFile {
name,
path: PathBuf::from(path),
})
}

pub(crate) fn write_locked_file(name: &'static str, path: &Path, contents: &str) -> Result<()> {
raw::write_locked_file(path, contents).with_context(|| RustupError::WritingFile {
name,
path: PathBuf::from(path),
})
}

pub(crate) fn append_file(name: &'static str, path: &Path, line: &str) -> Result<()> {
raw::append_file(path, line).with_context(|| RustupError::WritingFile {
name,
Expand Down
52 changes: 51 additions & 1 deletion src/utils/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::env;
use std::fs;
use std::fs::File;
use std::io;
use std::io::Write;
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::Path;
use std::str;

Expand Down Expand Up @@ -60,6 +60,21 @@ pub fn path_exists<P: AsRef<Path>>(path: P) -> bool {
fs::metadata(path).is_ok()
}

pub(crate) fn read_locked_file(path: &Path) -> io::Result<String> {
let mut file = File::open(path)?;
file.lock_shared()?;

let size = file
.metadata()
.map(|metadata| usize::try_from(metadata.len()).unwrap_or(usize::MAX))
.ok();
let mut contents = String::new();
Comment thread
ychampion marked this conversation as resolved.
contents.try_reserve_exact(size.unwrap_or(0))?;
file.read_to_string(&mut contents)?;

Ok(contents)
}

pub(crate) fn random_string(length: usize) -> String {
const CHARSET: &[u8] = b"abcdefghijklmnopqrstuvwxyz0123456789_";
let mut rng = rand::rng();
Expand All @@ -82,6 +97,24 @@ pub fn write_file(path: &Path, contents: &str) -> io::Result<()> {
Ok(())
}

pub(crate) fn write_locked_file(path: &Path, contents: &str) -> io::Result<()> {
let mut file = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
// Truncation must happen after the exclusive lock is held.
.truncate(false)
.open(path)?;

file.lock()?;
file.set_len(0)?;
file.seek(SeekFrom::Start(0))?;
Write::write_all(&mut file, contents.as_bytes())?;
file.sync_data()?;

Ok(())
}

pub(crate) fn filter_file<F: FnMut(&str) -> bool>(
src: &Path,
dest: &Path,
Expand Down Expand Up @@ -350,3 +383,20 @@ pub(crate) mod windows {
inner(s.as_ref())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn locked_file_round_trip_truncates_previous_contents() -> io::Result<()> {
let temp_dir = tempfile::tempdir()?;
let path = temp_dir.path().join("state.toml");

write_locked_file(&path, "last_release_notified_secs = 123456\n")?;
write_locked_file(&path, "version = \"12\"\n")?;

assert_eq!(read_locked_file(&path)?, "version = \"12\"\n");
Ok(())
}
}
Loading