Skip to content

Commit

Permalink
Set executable bit on shared library
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Feb 17, 2021
1 parent 8a06678 commit 5c652c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

* Escape version in wheel metadata by messense in [#420](https://github.com/PyO3/maturin/pull/420)
* Set executable bit on shared library by messense in [#421](https://github.com/PyO3/maturin/pull/421)

## 0.9.1 - 2021-01-13

Expand Down
32 changes: 25 additions & 7 deletions src/module_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ pub trait ModuleWriter {
.context(format!("Failed to write to {}", target.as_ref().display()))?;
Ok(())
}

/// Copies the source file the the target path relative to the module base path while setting
/// the given unix permissions
fn add_file_with_permissions(
&mut self,
target: impl AsRef<Path>,
source: impl AsRef<Path>,
permissions: u32,
) -> Result<()> {
let read_failed_context = format!("Failed to read {}", source.as_ref().display());
let mut file = File::open(source.as_ref()).context(read_failed_context.clone())?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).context(read_failed_context)?;
self.add_bytes_with_permissions(target.as_ref(), &buffer, permissions)
.context(format!("Failed to write to {}", target.as_ref().display()))?;
Ok(())
}
}

/// A [ModuleWriter] that adds the module somewhere in the filesystem, e.g. in a virtualenv
Expand Down Expand Up @@ -588,10 +605,14 @@ pub fn write_bindings_module(
))?;
}

writer.add_file(Path::new(&module_name).join(&so_filename), &artifact)?;
writer.add_file_with_permissions(
Path::new(&module_name).join(&so_filename),
&artifact,
0o755,
)?;
}
ProjectLayout::PureRust => {
writer.add_file(so_filename, &artifact)?;
writer.add_file_with_permissions(so_filename, &artifact, 0o755)?;
}
}

Expand Down Expand Up @@ -639,7 +660,7 @@ pub fn write_cffi_module(
writer.add_directory(&module)?;
writer.add_bytes(&module.join("__init__.py"), cffi_init_file().as_bytes())?;
writer.add_bytes(&module.join("ffi.py"), cffi_declarations.as_bytes())?;
writer.add_file(&module.join("native.so"), &artifact)?;
writer.add_file_with_permissions(&module.join("native.so"), &artifact, 0o755)?;

Ok(())
}
Expand All @@ -661,10 +682,7 @@ pub fn write_bin(
writer.add_directory(&data_dir)?;

// We can't use add_file since we need to mark the file as executable
let mut file = File::open(artifact)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
writer.add_bytes_with_permissions(&data_dir.join(bin_name), &buffer, 0o755)?;
writer.add_file_with_permissions(&data_dir.join(bin_name), artifact, 0o755)?;
Ok(())
}

Expand Down

0 comments on commit 5c652c4

Please sign in to comment.