]> git.proxmox.com Git - proxmox-backup.git/blob - src/tools/mod.rs
more clippy fixes and annotations
[proxmox-backup.git] / src / tools / mod.rs
1 //! Tools and utilities
2 //!
3 //! This is a collection of small and useful tools.
4 use std::any::Any;
5
6 use anyhow::{bail, Error};
7
8 use proxmox_http::{client::SimpleHttp, client::SimpleHttpOptions, ProxyConfig};
9
10 pub mod apt;
11 pub mod config;
12 pub mod disks;
13 pub mod fs;
14
15 mod shared_rate_limiter;
16 pub use shared_rate_limiter::SharedRateLimiter;
17
18 pub mod statistics;
19 pub mod systemd;
20 pub mod ticket;
21
22 pub mod parallel_handler;
23
24 pub fn assert_if_modified(digest1: &str, digest2: &str) -> Result<(), Error> {
25 if digest1 != digest2 {
26 bail!("detected modified configuration - file changed by other user? Try again.");
27 }
28 Ok(())
29 }
30
31 /// Detect modified configuration files
32 ///
33 /// This function fails with a reasonable error message if checksums do not match.
34 pub fn detect_modified_configuration_file(
35 digest1: &[u8; 32],
36 digest2: &[u8; 32],
37 ) -> Result<(), Error> {
38 if digest1 != digest2 {
39 bail!("detected modified configuration - file changed by other user? Try again.");
40 }
41 Ok(())
42 }
43
44 /// An easy way to convert types to Any
45 ///
46 /// Mostly useful to downcast trait objects (see RpcEnvironment).
47 pub trait AsAny {
48 fn as_any(&self) -> &dyn Any;
49 }
50
51 impl<T: Any> AsAny for T {
52 fn as_any(&self) -> &dyn Any {
53 self
54 }
55 }
56
57 /// The default 2 hours are far too long for PBS
58 pub const PROXMOX_BACKUP_TCP_KEEPALIVE_TIME: u32 = 120;
59 pub const DEFAULT_USER_AGENT_STRING: &str = "proxmox-backup-client/1.0";
60
61 /// Returns a new instance of `SimpleHttp` configured for PBS usage.
62 pub fn pbs_simple_http(proxy_config: Option<ProxyConfig>) -> SimpleHttp {
63 let options = SimpleHttpOptions {
64 proxy_config,
65 user_agent: Some(DEFAULT_USER_AGENT_STRING.to_string()),
66 tcp_keepalive: Some(PROXMOX_BACKUP_TCP_KEEPALIVE_TIME),
67 };
68
69 SimpleHttp::with_options(options)
70 }
71
72 pub fn setup_safe_path_env() {
73 std::env::set_var("PATH", "/sbin:/bin:/usr/sbin:/usr/bin");
74 // Make %ENV safer - as suggested by https://perldoc.perl.org/perlsec.html
75 for name in &["IFS", "CDPATH", "ENV", "BASH_ENV"] {
76 std::env::remove_var(name);
77 }
78 }