Skip to content

Commit 097281e

Browse files
committed
#431: Don't modify source file's permissions
1 parent 439438d commit 097281e

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

src/path.rs

+30-28
Original file line numberDiff line numberDiff line change
@@ -724,14 +724,7 @@ impl StrictPath {
724724
return Err(e);
725725
}
726726

727-
if let Err(e) = self.unset_readonly(context) {
728-
log::warn!("[{context}] failed to unset read-only on source: {:?} | {e}", &self);
729-
return Err(std::io::Error::new(
730-
std::io::ErrorKind::Other,
731-
"Failed to unset read-only",
732-
));
733-
}
734-
if let Err(e) = target_file.unset_readonly(context) {
727+
if let Err(e) = target_file.unset_readonly() {
735728
log::warn!(
736729
"[{context}] failed to unset read-only on target: {:?} | {e}",
737730
&target_file
@@ -747,6 +740,18 @@ impl StrictPath {
747740
return Err(e);
748741
}
749742

743+
// We do this again in case the source file was also read-only.
744+
if let Err(e) = target_file.unset_readonly() {
745+
log::warn!(
746+
"[{context}] failed to unset read-only on target after copy: {:?} | {e}",
747+
&target_file
748+
);
749+
return Err(std::io::Error::new(
750+
std::io::ErrorKind::Other,
751+
"Failed to unset read-only",
752+
));
753+
}
754+
750755
let mtime = match self.get_mtime() {
751756
Ok(x) => x,
752757
Err(e) => {
@@ -790,32 +795,29 @@ impl StrictPath {
790795
}
791796
}
792797

793-
pub fn unset_readonly(&self, context: &str) -> Result<(), AnyError> {
798+
pub fn unset_readonly(&self) -> Result<(), AnyError> {
799+
if !self.is_file() {
800+
return Ok(());
801+
}
802+
794803
let subject = self.as_std_path_buf()?;
795-
if self.is_file() {
796-
let mut perms = std::fs::metadata(&subject)?.permissions();
797-
if perms.readonly() {
804+
let mut perms = std::fs::metadata(&subject)?.permissions();
805+
806+
if perms.readonly() {
807+
#[cfg(windows)]
808+
{
798809
#[allow(clippy::permissions_set_readonly_false)]
799810
perms.set_readonly(false);
800-
std::fs::set_permissions(&subject, perms)?;
801811
}
802-
} else {
803-
for entry in walkdir::WalkDir::new(subject)
804-
.max_depth(100)
805-
.follow_links(false)
806-
.into_iter()
807-
.skip(1) // the base path itself
808-
.filter_map(|x| crate::prelude::filter_map_walkdir(context, x))
809-
.filter(|x| x.file_type().is_file())
812+
813+
#[cfg(unix)]
810814
{
811-
let file = entry.path().display().to_string();
812-
let mut perms = std::fs::metadata(&file)?.permissions();
813-
if perms.readonly() {
814-
#[allow(clippy::permissions_set_readonly_false)]
815-
perms.set_readonly(false);
816-
std::fs::set_permissions(&file, perms)?;
817-
}
815+
use std::os::unix::fs::PermissionsExt;
816+
817+
perms.set_mode(perms.mode() | 0b110_000_000);
818818
}
819+
820+
std::fs::set_permissions(&subject, perms)?;
819821
}
820822

821823
Ok(())

src/scan/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1826,7 +1826,7 @@ impl GameLayout {
18261826
);
18271827
return Err(Box::new(e));
18281828
}
1829-
if let Err(e) = target.unset_readonly(&self.mapping.name) {
1829+
if let Err(e) = target.unset_readonly() {
18301830
log::warn!(
18311831
"[{}] failed to unset read-only on target: {:?} | {e}",
18321832
self.mapping.name,

0 commit comments

Comments
 (0)