Skip to content

Commit

Permalink
Keep file mode on Unix (#1622)
Browse files Browse the repository at this point in the history
  • Loading branch information
messense authored May 20, 2023
1 parent f66a0c8 commit c28e90c
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/module_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ use std::collections::{HashMap, HashSet};
use std::env;
use std::ffi::OsStr;
use std::fmt::Write as _;
#[cfg(target_family = "unix")]
#[cfg(unix)]
use std::fs::OpenOptions;
use std::io;
use std::io::{Read, Write};
#[cfg(target_family = "unix")]
use std::os::unix::fs::OpenOptionsExt;
#[cfg(unix)]
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use std::str;
Expand Down Expand Up @@ -1160,7 +1160,12 @@ pub fn write_python_part(
if source.is_dir() {
writer.add_directory(target)?;
} else {
writer.add_file(target, source)?;
let permissions = source.metadata()?.permissions();
#[cfg(unix)]
let mode = permissions.mode();
#[cfg(not(unix))]
let mode = 0o644;
writer.add_file_with_permissions(target, source, mode)?;
}
}
}
Expand Down Expand Up @@ -1247,15 +1252,24 @@ pub fn add_data(writer: &mut impl ModuleWriter, data: Option<&Path>) -> Result<(
.build()
{
let file = file?;
let permissions = file.metadata()?.permissions();
#[cfg(unix)]
let mode = permissions.mode();
#[cfg(not(unix))]
let mode = 0o644;
let relative = file.path().strip_prefix(data.parent().unwrap()).unwrap();

if file.path_is_symlink() {
// Copy the actual file contents, not the link, so that you can create a
// data directory by joining different data sources
let source = fs::read_link(file.path())?;
writer.add_file(relative, source.parent().unwrap())?;
writer.add_file_with_permissions(
relative,
source.parent().unwrap(),
mode,
)?;
} else if file.path().is_file() {
writer.add_file(relative, file.path())?;
writer.add_file_with_permissions(relative, file.path(), mode)?;
} else if file.path().is_dir() {
writer.add_directory(relative)?;
} else {
Expand Down

0 comments on commit c28e90c

Please sign in to comment.