Skip to content

Commit

Permalink
fix: do not mutate read-only files
Browse files Browse the repository at this point in the history
fixes #1071
related #700
  • Loading branch information
iloveitaly committed Sep 2, 2024
1 parent 2df97bb commit 6b96215
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/nixpacks/files.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use anyhow::Result;
use ignore::WalkBuilder;
use std::{fs, io, path::Path};
use std::{fs, io, os::unix::fs::PermissionsExt, path::Path};

Check failure on line 3 in src/nixpacks/files.rs

View workflow job for this annotation

GitHub Actions / Test Suite (MSRV) (windows-latest)

failed to resolve: could not find `unix` in `os`

Check failure on line 3 in src/nixpacks/files.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest)

failed to resolve: could not find `unix` in `os`

fn is_writable<P: AsRef<Path>>(path: P) -> io::Result<bool> {
let metadata = fs::metadata(path)?;
let permissions = metadata.permissions();
Ok(permissions.mode() & 0o200 != 0)

Check failure on line 8 in src/nixpacks/files.rs

View workflow job for this annotation

GitHub Actions / Test Suite (MSRV) (windows-latest)

no method named `mode` found for struct `Permissions` in the current scope

Check failure on line 8 in src/nixpacks/files.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest)

no method named `mode` found for struct `Permissions` in the current scope
}

/// Copies a directory and all its contents to the destination path, recursively.
pub fn recursive_copy_dir<T: AsRef<Path>, Q: AsRef<Path>>(source: T, dest: Q) -> Result<()> {
Expand Down Expand Up @@ -30,9 +36,12 @@ pub fn recursive_copy_dir<T: AsRef<Path>, Q: AsRef<Path>>(source: T, dest: Q) ->
// copy files
else if file_type.is_file() {
fs::copy(from, &to)?;
// replace CRLF with LF
if let Ok(data) = fs::read_to_string(from) {
fs::write(&to, data.replace("\r\n", "\n"))?;

if is_writable(&to)? {
// replace CRLF with LF
if let Ok(data) = fs::read_to_string(from) {
fs::write(&to, data.replace("\r\n", "\n"))?;
}
}
}
}
Expand Down

0 comments on commit 6b96215

Please sign in to comment.