fix: don't make cas files read-only on windows

This commit is contained in:
daimond113 2024-10-26 17:45:18 +02:00
parent 2e62d07265
commit b1ae6aebda
No known key found for this signature in database
GPG key ID: 3A8ECE51328B513C
2 changed files with 18 additions and 15 deletions

View file

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Use updated aliases when reusing lockfile dependencies by @daimond113
- Listen for device flow completion without requiring pressing enter by @daimond113
- Sync scripts repo in background by @daimond113
- Don't make CAS files read-only on Windows (file removal is disallowed if the file is read-only) by @daimond113
### Performance
- Clone dependency repos shallowly by @daimond113

View file

@ -35,6 +35,19 @@ pub enum PackageFS {
Copy(PathBuf, TargetKind),
}
fn make_readonly(_file: &std::fs::File) -> std::io::Result<()> {
// on Windows, file deletion is disallowed if the file is read-only which breaks patching
#[cfg(not(windows))]
{
let mut permissions = _file.metadata()?.permissions();
permissions.set_readonly(true);
_file.set_permissions(permissions)
}
#[cfg(windows)]
Ok(())
}
pub(crate) fn store_in_cas<P: AsRef<Path>>(
cas_dir: P,
contents: &[u8],
@ -50,10 +63,7 @@ pub(crate) fn store_in_cas<P: AsRef<Path>>(
let mut file = std::fs::File::create(&cas_path)?;
file.write_all(contents)?;
// prevent the CAS from being corrupted due to accidental modifications
let mut permissions = file.metadata()?.permissions();
permissions.set_readonly(true);
file.set_permissions(permissions)?;
make_readonly(&file)?;
}
Ok((hash, cas_path))
@ -89,10 +99,7 @@ pub(crate) fn store_reader_in_cas<P: AsRef<Path>>(
let cas_path = folder.join(rest);
match file_writer.into_inner()?.persist_noclobber(cas_path) {
Ok(f) => {
// prevent the CAS from being corrupted due to accidental modifications
let mut permissions = f.metadata()?.permissions();
permissions.set_readonly(true);
f.set_permissions(permissions)?;
make_readonly(&f)?;
}
Err(e) if e.error.kind() == std::io::ErrorKind::AlreadyExists => {}
Err(e) => return Err(e.error),
@ -163,18 +170,13 @@ impl PackageFS {
let mut f = std::fs::File::create(&path)?;
f.write_all(&std::fs::read(cas_file_path)?)?;
let mut permissions = f.metadata()?.permissions();
#[cfg(windows)]
{
#[allow(clippy::permissions_set_readonly_false)]
permissions.set_readonly(false);
}
#[cfg(unix)]
{
let mut permissions = f.metadata()?.permissions();
use std::os::unix::fs::PermissionsExt;
permissions.set_mode(permissions.mode() | 0o644);
f.set_permissions(permissions)?;
}
f.set_permissions(permissions)?;
}
}
FSEntry::Directory => {