i3status_rs/blocks/packages/
pacman.rs
1use std::env;
2use std::path::PathBuf;
3use std::process::Stdio;
4
5use tokio::fs::{create_dir_all, symlink};
6use tokio::process::Command;
7
8use super::*;
9use crate::util::has_command;
10
11make_log_macro!(debug, "pacman");
12
13pub static PACMAN_UPDATES_DB: LazyLock<PathBuf> = LazyLock::new(|| {
14 let path = match env::var_os("CHECKUPDATES_DB") {
15 Some(val) => val.into(),
16 None => {
17 let mut path = env::temp_dir();
18 let user = env::var("USER");
19 path.push(format!(
20 "checkup-db-i3statusrs-{}",
21 user.as_deref().unwrap_or("no-user")
22 ));
23 path
24 }
25 };
26 debug!("Using {} as updates DB path", path.display());
27 path
28});
29
30pub static PACMAN_DB: LazyLock<PathBuf> = LazyLock::new(|| {
31 let path = env::var_os("DBPath")
32 .map(Into::into)
33 .unwrap_or_else(|| PathBuf::from("/var/lib/pacman/"));
34 debug!("Using {} as pacman DB path", path.display());
35 path
36});
37
38pub struct Pacman;
39
40pub struct Aur {
41 aur_command: String,
42}
43
44impl Pacman {
45 pub async fn new() -> Result<Self> {
46 check_fakeroot_command_exists().await?;
47
48 Ok(Self)
49 }
50}
51
52impl Aur {
53 pub fn new(aur_command: String) -> Self {
54 Aur { aur_command }
55 }
56}
57
58#[async_trait]
59impl Backend for Pacman {
60 fn name(&self) -> Cow<'static, str> {
61 "pacman".into()
62 }
63
64 async fn get_updates_list(&self) -> Result<Vec<String>> {
65 create_dir_all(&*PACMAN_UPDATES_DB).await.or_error(|| {
67 format!(
68 "Failed to create checkup-db directory at '{}'",
69 PACMAN_UPDATES_DB.display()
70 )
71 })?;
72
73 let local_cache = PACMAN_UPDATES_DB.join("local");
75 if !local_cache.exists() {
76 symlink(PACMAN_DB.join("local"), local_cache)
77 .await
78 .error("Failed to created required symlink")?;
79 }
80
81 let status = Command::new("fakeroot")
83 .env("LC_ALL", "C")
84 .args([
85 "--".as_ref(),
86 "pacman".as_ref(),
87 "-Sy".as_ref(),
88 "--dbpath".as_ref(),
89 PACMAN_UPDATES_DB.as_os_str(),
90 "--logfile".as_ref(),
91 "/dev/null".as_ref(),
92 ])
93 .stdout(Stdio::null())
94 .status()
95 .await
96 .error("Failed to run command")?;
97 if !status.success() {
98 debug!("{}", status);
99 return Err(Error::new("pacman -Sy exited with non zero exit status"));
100 }
101
102 let stdout = Command::new("fakeroot")
103 .env("LC_ALL", "C")
104 .args([
105 "--".as_ref(),
106 "pacman".as_ref(),
107 "-Qu".as_ref(),
108 "--dbpath".as_ref(),
109 PACMAN_UPDATES_DB.as_os_str(),
110 ])
111 .output()
112 .await
113 .error("There was a problem running the pacman commands")?
114 .stdout;
115
116 let updates = String::from_utf8(stdout).error("Pacman produced non-UTF8 output")?;
117
118 let updates = updates
119 .lines()
120 .filter(|line| !line.contains("[ignored]"))
121 .map(|line| line.to_string())
122 .collect();
123
124 Ok(updates)
125 }
126}
127
128#[async_trait]
129impl Backend for Aur {
130 fn name(&self) -> Cow<'static, str> {
131 "aur".into()
132 }
133
134 async fn get_updates_list(&self) -> Result<Vec<String>> {
135 let stdout = Command::new("sh")
136 .args(["-c", &self.aur_command])
137 .output()
138 .await
139 .or_error(|| format!("aur command: {} failed", self.aur_command))?
140 .stdout;
141 let updates = String::from_utf8(stdout)
142 .error("There was a problem while converting the aur command output to a string")?;
143
144 let updates = updates
145 .lines()
146 .filter(|line| !line.contains("[ignored]"))
147 .map(|line| line.to_string())
148 .collect();
149
150 Ok(updates)
151 }
152}
153
154async fn check_fakeroot_command_exists() -> Result<()> {
155 if !has_command("fakeroot").await? {
156 Err(Error::new("fakeroot not found"))
157 } else {
158 Ok(())
159 }
160}