Skip to content

Commit

Permalink
Improve the FileMode API
Browse files Browse the repository at this point in the history
Avoid users needing to deal with raw u16 numbers and return errors on
invalid values rather than making "invalid" part of the enum.
  • Loading branch information
dralley committed Aug 25, 2023
1 parent 90f55d0 commit 52d1055
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 194 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,31 @@ This library does not build software like rpmbuild. It is meant for finished art
use rpm::signature::pgp::{Signer, Verifier};

let raw_secret_key = std::fs::read("./test_assets/secret_key.asc")?;
// It's recommended to use timestamp of last commit in your VCS
// It's recommended to use timestamp of last commit in your VCS rather than a constant
let source_date = 1_600_000_000;
let pkg = rpm::PackageBuilder::new("test", "1.0.0", "MIT", "x86_64", "some awesome package")
.compression(rpm::CompressionType::Gzip)
.with_file(
"./test_assets/awesome.toml",
rpm::FileOptions::new("/etc/awesome/config.toml").is_config(),
rpm::FileOptions::regular("/etc/awesome/config.toml").is_config(),
)?
// file mode is inherited from source file
.with_file(
"./test_assets/awesome.py",
rpm::FileOptions::new("/usr/bin/awesome"),
rpm::FileOptions::regular("/usr/bin/awesome"),
)?
.with_file(
"./test_assets/awesome.toml",
// you can set a custom mode and custom user too
rpm::FileOptions::new("/etc/awesome/second.toml")
.mode(rpm::FileMode::regular(0o644))
rpm::FileOptions::regular("/etc/awesome/second.toml")
.permissions(0o644)
.caps("cap_sys_admin,cap_net_admin=pe")?
.user("hugo"),
)?
.with_file(
"./test_assets/empty_file_for_symlink_create",
rpm::FileOptions::new("/usr/bin/awesome_link")
.mode(0o120644)
.symlink("/usr/bin/awesome"),
rpm::FileOptions::symlink("/usr/bin/awesome_link", "/usr/bin/awesome")
.permissions(0o644)
.pre_install_script("echo preinst")
// If you don't need reproducible builds,
// you can remove the following line
Expand Down
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ bitflags! {
// const SPECFILE = 1 << 5; // first file in SRPM?
const GHOST = 1 << 6; // %%ghost
const LICENSE = 1 << 7; // %%license
const README = 1 << 8; // %%readme
const README = 1 << 8; // %%readme // obsolete?
// bits 9-10 unused
const PUBKEY = 1 << 11; // %%pubkey
const ARTIFACT = 1 << 12; // %%artifact
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
//! .compression(rpm::CompressionType::Gzip)
//! .with_file(
//! "./test_assets/awesome.toml",
//! rpm::FileOptions::new("/etc/awesome/config.toml").is_config(),
//! rpm::FileOptions::regular("/etc/awesome/config.toml").is_config(),
//! )?
//! // file mode is inherited from source file
//! .with_file(
//! "./test_assets/awesome.py",
//! rpm::FileOptions::new("/usr/bin/awesome"),
//! rpm::FileOptions::regular("/usr/bin/awesome"),
//! )?
//! .with_file(
//! "./test_assets/awesome.toml",
//! // you can set a custom mode and custom user too
//! rpm::FileOptions::new("/etc/awesome/second.toml")
//! .mode(rpm::FileMode::regular(0o644))
//! rpm::FileOptions::regular("/etc/awesome/second.toml")
//! .permissions(0o644)
//! .user("hugo"),
//! )?
//! .pre_install_script("echo preinst")
Expand Down
8 changes: 4 additions & 4 deletions src/rpm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,17 @@ impl PackageBuilder {
/// let pkg = rpm::PackageBuilder::new("foo", "1.0.0", "Apache-2.0", "x86_64", "some baz package")
/// .with_file(
/// "./awesome-config.toml",
/// rpm::FileOptions::new("/etc/awesome/config.toml").is_config(),
/// rpm::FileOptions::regular("/etc/awesome/config.toml").is_config(),
/// )?
/// // file mode is inherited from source file
/// .with_file(
/// "./awesome-bin",
/// rpm::FileOptions::new("/usr/bin/awesome"),
/// rpm::FileOptions::regular("/usr/bin/awesome"),
/// )?
/// .with_file(
/// "./awesome-config.toml",
/// // you can set a custom mode, capabilities and custom user too
/// rpm::FileOptions::new("/etc/awesome/second.toml").mode(0o100744).caps("cap_sys_admin=pe")?.user("hugo"),
/// rpm::FileOptions::regular("/etc/awesome/second.toml").permissions(0o744).caps("cap_sys_admin=pe")?.user("hugo"),
/// )?
/// .build()?;
/// # Ok(())
Expand All @@ -290,7 +290,7 @@ impl PackageBuilder {
input.read_to_end(&mut content)?;
let mut options = options.into();
if options.inherit_permissions {
options.mode = (file_mode(&input)? as i32).into();
options.mode = (file_mode(&input)? as i32).try_into()?;
}

let modified_at = input.metadata()?.modified()?.try_into()?;
Expand Down
Loading

0 comments on commit 52d1055

Please sign in to comment.