fix: use new path when setting file read-only

This commit is contained in:
daimond113 2024-11-11 18:59:34 +01:00
parent 9f3017742e
commit 72eb48de07
No known key found for this signature in database
GPG key ID: 3A8ECE51328B513C
2 changed files with 26 additions and 19 deletions

View file

@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Correctly (de)serialize workspace specifiers by @daimond113 - Correctly (de)serialize workspace specifiers by @daimond113
- Fix CAS finder algorithm issues with Windows by @daimond113 - Fix CAS finder algorithm issues with Windows by @daimond113
- Fix CAS finder algorithm's AlreadyExists error by @daimond113 - Fix CAS finder algorithm's AlreadyExists error by @daimond113
- Use moved path when setting file to read-only by @daimond113
### Changed ### Changed
- Switched to fs-err for better errors with file system operations by @daimond113 - Switched to fs-err for better errors with file system operations by @daimond113

View file

@ -40,17 +40,31 @@ pub enum PackageFS {
Copy(PathBuf, TargetKind), Copy(PathBuf, TargetKind),
} }
async fn make_readonly(_file: &fs::File) -> std::io::Result<()> { async fn set_readonly(path: &Path, readonly: bool) -> std::io::Result<()> {
// on Windows, file deletion is disallowed if the file is read-only which breaks patching // on Windows, file deletion is disallowed if the file is read-only which breaks multiple features
#[cfg(not(windows))] #[cfg(windows)]
{ if readonly {
let mut permissions = _file.metadata().await?.permissions(); return Ok(());
permissions.set_readonly(true);
_file.set_permissions(permissions).await
} }
#[cfg(windows)] let mut permissions = fs::metadata(path).await?.permissions();
Ok(()) if readonly {
permissions.set_readonly(true);
} else {
#[cfg(windows)]
#[allow(clippy::permissions_set_readonly_false)]
{
permissions.set_readonly(false);
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
permissions.set_mode(permissions.mode() | 0o644);
}
}
fs::set_permissions(path, permissions).await
} }
pub(crate) fn cas_path(hash: &str, cas_dir: &Path) -> PathBuf { pub(crate) fn cas_path(hash: &str, cas_dir: &Path) -> PathBuf {
@ -100,7 +114,7 @@ pub(crate) async fn store_in_cas<
match temp_path.persist_noclobber(&cas_path) { match temp_path.persist_noclobber(&cas_path) {
Ok(_) => { Ok(_) => {
make_readonly(&file_writer).await?; set_readonly(&cas_path, true).await?;
} }
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),
@ -139,15 +153,7 @@ impl PackageFS {
fs::hard_link(cas_file_path, path).await?; fs::hard_link(cas_file_path, path).await?;
} else { } else {
fs::copy(cas_file_path, &path).await?; fs::copy(cas_file_path, &path).await?;
set_readonly(&path, false).await?;
#[cfg(unix)]
{
let f = fs::File::open(&path).await?;
let mut permissions = f.metadata().await?.permissions();
use std::os::unix::fs::PermissionsExt;
permissions.set_mode(permissions.mode() | 0o644);
f.set_permissions(permissions).await?;
}
} }
} }
FSEntry::Directory => { FSEntry::Directory => {