with a module for wrapping apt repository functions. Signed-off-by: Fabian Ebner <f.eb...@proxmox.com> ---
Changes from v5: * Start with a fresh repository. * Add comments to exported functions. * Make exported functions pub. * Use Option<&str> for the digest parameter. .cargo/config | 5 +++ .gitignore | 3 ++ Cargo.toml | 22 +++++++++++++ rustfmt.toml | 1 + src/apt/mod.rs | 1 + src/apt/repositories.rs | 69 +++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 7 files changed, 102 insertions(+) create mode 100644 .cargo/config create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 rustfmt.toml create mode 100644 src/apt/mod.rs create mode 100644 src/apt/repositories.rs create mode 100644 src/lib.rs diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 0000000..3b5b6e4 --- /dev/null +++ b/.cargo/config @@ -0,0 +1,5 @@ +[source] +[source.debian-packages] +directory = "/usr/share/cargo/registry" +[source.crates-io] +replace-with = "debian-packages" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..52e46cb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +Cargo.lock +/PVE diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d3b9955 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "pve-rs" +version = "0.1.0" +authors = ["Proxmox Support Team <supp...@proxmox.com>"] +edition = "2018" +license = "AGPL-3" +description = "PVE parts which have been ported to Rust" +homepage = "https://www.proxmox.com" +exclude = [ + "build", + "debian", + "PVE", +] + +[lib] +crate-type = [ "cdylib" ] + +[dependencies] +anyhow = "1.0" +proxmox = { version = "0.11.5" } +proxmox-apt = "0.1.1" +perlmod = { version = "0.4.3", features = [ "exporter" ] } diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..32a9786 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1 @@ +edition = "2018" diff --git a/src/apt/mod.rs b/src/apt/mod.rs new file mode 100644 index 0000000..574c1a7 --- /dev/null +++ b/src/apt/mod.rs @@ -0,0 +1 @@ +mod repositories; diff --git a/src/apt/repositories.rs b/src/apt/repositories.rs new file mode 100644 index 0000000..1643a23 --- /dev/null +++ b/src/apt/repositories.rs @@ -0,0 +1,69 @@ +#[perlmod::package(name = "PVE::RS::APT::Repositories", lib = "pve_rs")] +mod export { + use anyhow::{bail, Error}; + + use perlmod::{to_value, Value}; + + /// Returns all APT repositories configured in `/etc/apt/sources.list` and + /// in `/etc/apt/sources.list.d` including disabled repositories. + /// + /// The first return value is the list of successfully parsed files of type `APTRepositoryFile`. + /// The second return value is the list of problems of type `APTRepositoryFileError`. + /// The third return value is a common digest of all successfully parsed files as a hexadecimal + /// string. + #[export(raw_return)] + pub fn repositories() -> Result<(Value, Value, Value), Error> { + let (files, errors) = proxmox_apt::repositories::repositories()?; + + if files.is_empty() { + bail!("no APT repository files could be parsed!"); + } + + let common_digest = proxmox_apt::repositories::common_digest(&files); + + let hex_digest = proxmox::tools::digest_to_hex(&common_digest); + + Ok(( + to_value(&files)?, + to_value(&errors)?, + to_value(&hex_digest)?, + )) + } + + /// Provides additional information about the repositories. + /// + /// If `digest` is specified and doesn't match the current one, an error is returned. + /// + /// The first return value is the list of informations of type `APTRepositoryInfo`. + /// The second (resp. third) return values is a boolean indicating whether the enterprise + /// (resp. no-subscription) repository is configured. + #[export(raw_return)] + pub fn check_repositories(digest: Option<&str>) -> Result<(Value, Value, Value), Error> { + let (files, _) = proxmox_apt::repositories::repositories()?; + + if files.is_empty() { + bail!("no APT repository files could be parsed!"); + } + + if let Some(digest) = digest { + let expected_digest = proxmox::tools::hex_to_digest(digest)?; + let current_digest = proxmox_apt::repositories::common_digest(&files); + if current_digest != expected_digest { + bail!("detected modified configuration - file changed by other user? Try again."); + } + } + + let infos = proxmox_apt::repositories::check_repositories(&files); + + let enterprise_enabled = + proxmox_apt::repositories::enterprise_repository_enabled(&files, "pve"); + let no_subscription_enabled = + proxmox_apt::repositories::no_subscription_repository_enabled(&files, "pve"); + + Ok(( + to_value(&infos)?, + to_value(&enterprise_enabled)?, + to_value(&no_subscription_enabled)?, + )) + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..10b3376 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod apt; -- 2.20.1 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel