From f731ded6ab0b80d48845ec6b853e83a768a47d14 Mon Sep 17 00:00:00 2001 From: messense Date: Wed, 17 Feb 2021 10:43:18 +0800 Subject: [PATCH] Set executable bit on shared library --- Changelog.md | 1 + src/module_writer.rs | 32 +++++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Changelog.md b/Changelog.md index d4c7d7e61..13a6e05d9 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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 diff --git a/src/module_writer.rs b/src/module_writer.rs index 71e2cac3b..a42b5e923 100644 --- a/src/module_writer.rs +++ b/src/module_writer.rs @@ -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, + source: impl AsRef, + 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 @@ -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)?; } } @@ -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(()) } @@ -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(()) }