Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compile error on windows #172

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
of a `description`. A new method `PackageBuilder::description` can be used to
set a detailed description for a package; if not set, the description defaults
to the `summary`.
- Fix compile error on Windows which introduced by file capabilities support feature.

## 0.12.1

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ itertools = "0.11"
hex = { version = "0.4", features = ["std"] }
zstd = "0.12"
xz2 = "0.1"
[target.'cfg(unix)'.dependencies]
capctl = "0.2.3"

[dev-dependencies]
Expand Down
4 changes: 4 additions & 0 deletions src/rpm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ impl PackageBuilder {
link: options.symlink,
modified_at,
dir: dir.clone(),
#[cfg(unix)]
// Convert the caps to a string, so that we can store it in the header.
// We do this so that it's possible to verify that caps are correct when provided
// and then later check if any were set
Expand Down Expand Up @@ -585,6 +586,7 @@ impl PackageBuilder {
let files_len = self.files.len();
let mut file_sizes = Vec::with_capacity(files_len);
let mut file_modes = Vec::with_capacity(files_len);
#[cfg(unix)]
let mut file_caps = Vec::with_capacity(files_len);
let mut file_rdevs = Vec::with_capacity(files_len);
let mut file_mtimes = Vec::with_capacity(files_len);
Expand All @@ -606,6 +608,7 @@ impl PackageBuilder {
combined_file_sizes += entry.size;
file_sizes.push(entry.size);
file_modes.push(entry.mode.into());
#[cfg(unix)]
file_caps.push(entry.caps.to_owned());
// I really do not know the difference. It seems like file_rdevice is always 0 and file_device number always 1.
// Who knows, who cares.
Expand Down Expand Up @@ -983,6 +986,7 @@ impl PackageBuilder {
IndexData::StringArray(self.directories.into_iter().collect()),
),
]);
#[cfg(unix)]
if file_caps.iter().any(|caps| caps.is_some()) {
actual_records.extend([IndexEntry::new(
IndexTag::RPMTAG_FILECAPS,
Expand Down
8 changes: 8 additions & 0 deletions src/rpm/headers/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! A collection of types used in various header records.
#[cfg(unix)]
use std::str::FromStr;

use crate::{constants::*, errors, Timestamp};
#[cfg(unix)]
use capctl::FileCaps;
use digest::Digest;

Expand All @@ -28,6 +30,7 @@ pub struct PackageFileEntry {
pub group: String,
pub base_name: String,
pub dir: String,
#[cfg(unix)]
pub caps: Option<FileCaps>,
pub(crate) content: Vec<u8>,
}
Expand Down Expand Up @@ -198,6 +201,7 @@ pub struct FileOptions {
pub(crate) mode: FileMode,
pub(crate) flag: FileFlags,
pub(crate) inherit_permissions: bool,
#[cfg(unix)]
pub(crate) caps: Option<FileCaps>,
}

Expand All @@ -213,6 +217,7 @@ impl FileOptions {
mode: FileMode::regular(0o664),
flag: FileFlags::empty(),
inherit_permissions: true,
#[cfg(unix)]
caps: None,
},
}
Expand Down Expand Up @@ -246,6 +251,7 @@ impl FileOptionsBuilder {
self
}

#[cfg(unix)]
pub fn caps(mut self, caps: impl Into<String>) -> Result<Self, errors::Error> {
// verify capabilities
self.inner.caps = match FileCaps::from_str(&caps.into()) {
Expand Down Expand Up @@ -490,12 +496,14 @@ mod test {
Ok(())
}

#[cfg(unix)]
#[test]
fn test_verify_capabilities_valid() {
let blank_file = crate::FileOptions::new("/usr/bin/awesome");
blank_file.caps("cap_net_admin,cap_net_raw+p").unwrap();
}

#[cfg(unix)]
#[test]
fn test_verify_capabilities_invalid() -> Result<(), crate::errors::Error> {
let blank_file = crate::FileOptions::new("/usr/bin/awesome");
Expand Down
27 changes: 15 additions & 12 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn cargo_manifest_dir() -> std::path::PathBuf {
fn test_rpm_builder() -> Result<(), Box<dyn std::error::Error>> {
let mut buff = std::io::Cursor::new(Vec::<u8>::new());

let pkg = PackageBuilder::new("test", "1.0.0", "MIT", "x86_64", "some awesome package")
let builder = PackageBuilder::new("test", "1.0.0", "MIT", "x86_64", "some awesome package")
.description(
"This is an awesome package.

Expand All @@ -26,14 +26,6 @@ However, it does nothing.",
)?
// file mode is inherited from source file
.with_file("Cargo.toml", FileOptions::new("/usr/bin/awesome"))?
.with_file(
"Cargo.toml",
// you can set a custom mode and custom user too
FileOptions::new("/etc/awesome/second.toml")
.mode(0o100744)
.caps("cap_sys_admin,cap_sys_ptrace=pe")?
.user("hugo"),
)?
.with_file(
"./test_assets/empty_file_for_symlink_create",
FileOptions::new("/usr/bin/awesome_link")
Expand All @@ -46,9 +38,17 @@ However, it does nothing.",
.requires(Dependency::any("wget"))
.vendor("dummy vendor")
.url("dummy url")
.vcs("dummy vcs")
.build()?;

.vcs("dummy vcs");
#[cfg(unix)]
let builder = builder.with_file(
"Cargo.toml",
// you can set a custom mode and custom user too
FileOptions::new("/etc/awesome/second.toml")
.mode(0o100744)
.caps("cap_sys_admin,cap_sys_ptrace=pe")?
.user("hugo"),
)?;
let pkg = builder.build()?;
pkg.write(&mut buff)?;

// check that generated packages has source rpm tag
Expand All @@ -60,14 +60,17 @@ However, it does nothing.",
// check various metadata on the files
pkg.metadata.get_file_entries()?.iter().for_each(|f| {
if f.path.as_os_str() == "/etc/awesome/second.toml" {
#[cfg(unix)]
assert_eq!(
f.clone().caps.unwrap(),
"cap_sys_ptrace,cap_sys_admin=ep".to_string()
);
assert_eq!(f.ownership.user, "hugo".to_string());
} else if f.path.as_os_str() == "/etc/awesome/config.toml" {
#[cfg(unix)]
assert_eq!(f.caps, Some("".to_string()));
} else if f.path.as_os_str() == "/usr/bin/awesome" {
#[cfg(unix)]
assert_eq!(f.mode, FileMode::from(0o100644));
} else if f.path.as_os_str() == "/usr/bin/awesome_link" {
assert_eq!(f.mode, FileMode::from(0o120644));
Expand Down