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 - Use updated aliases when reusing lockfile dependencies by @daimond113
- Listen for device flow completion without requiring pressing enter by @daimond113 - Listen for device flow completion without requiring pressing enter by @daimond113
- Sync scripts repo in background 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 ### Performance
- Clone dependency repos shallowly by @daimond113 - Clone dependency repos shallowly by @daimond113

View file

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