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

Reduce amount of blocking in blocks #2093

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 9 additions & 6 deletions src/blocks/amd_gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
};

let device = match &config.device {
Some(name) => Device::new(name)?,
Some(name) => Device::new(name).await?,
None => Device::default_card()
.await
.error("failed to get default GPU")?
Expand Down Expand Up @@ -121,10 +121,13 @@ struct GpuInfo {
}

impl Device {
fn new(name: &str) -> Result<Self, Error> {
async fn new(name: &str) -> Result<Self, Error> {
let path = PathBuf::from(format!("/sys/class/drm/{name}/device"));

if !path.exists() {
if !tokio::fs::try_exists(&path)
.await
.error("Unable to stat file")?
{
Err(Error::new(format!("Device {name} not found")))
} else {
Ok(Self { path })
Expand Down Expand Up @@ -185,9 +188,9 @@ impl Device {
mod tests {
use super::*;

#[test]
fn test_non_existing_gpu_device() {
let device = Device::new("/nope");
#[tokio::test]
async fn test_non_existing_gpu_device() {
let device = Device::new("/nope").await;
assert!(device.is_err());
}
}
8 changes: 5 additions & 3 deletions src/blocks/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let warning_threshold = Duration::try_seconds(config.warning_threshold.into())
.error("Invalid warning threshold configuration")?;

let mut source = Source::new(source_config.clone()).await?;
let mut source = Source::new(source_config.to_owned()).await?;

let mut timer = config.fetch_interval.timer();

Expand Down Expand Up @@ -424,7 +424,8 @@ impl Source {
credentials_path,
}) => {
let credentials = if let Some(path) = credentials_path {
util::deserialize_toml_file(path.expand()?.to_string())
util::async_deserialize_toml_file(path.expand()?.to_string())
.await
.error("Failed to read basic credentials file")?
} else {
credentials.clone()
Expand All @@ -440,7 +441,8 @@ impl Source {
}
AuthConfig::OAuth2(oauth2) => {
let credentials = if let Some(path) = &oauth2.credentials_path {
util::deserialize_toml_file(path.expand()?.to_string())
util::async_deserialize_toml_file(path.expand()?.to_string())
.await
.error("Failed to read oauth2 credentials file")?
} else {
oauth2.credentials.clone()
Expand Down
5 changes: 4 additions & 1 deletion src/blocks/packages/apt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ impl Apt {
async fn setup(&mut self) -> Result<()> {
let mut cache_dir = env::temp_dir();
cache_dir.push("i3rs-apt");
if !cache_dir.exists() {
if !tokio::fs::try_exists(&cache_dir)
.await
.error("Unable to stat file")?
{
create_dir_all(&cache_dir)
.await
.error("Failed to create temp dir")?;
Expand Down
5 changes: 4 additions & 1 deletion src/blocks/packages/pacman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ impl Backend for Pacman {

// Create symlink to local cache in `checkup-db` if required
let local_cache = PACMAN_UPDATES_DB.join("local");
if !local_cache.exists() {
if !tokio::fs::try_exists(&local_cache)
.await
.error("Unable to stat file")?
{
symlink(PACMAN_DB.join("local"), local_cache)
.await
.error("Failed to created required symlink")?;
Expand Down
2 changes: 1 addition & 1 deletion src/icons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Icons {
if file == "none" {
Ok(Icons::default())
} else {
let file = util::find_file(file, Some("icons"), Some("toml"))
let file = util::find_file(file, Some("icons"), Some("toml"))?
.or_error(|| format!("Icon set '{file}' not found"))?;
Ok(Icons(util::deserialize_toml_file(file)?))
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() {
.build()
.unwrap()
.block_on(async move {
let config_path = util::find_file(&args.config, None, Some("toml"))
let config_path = util::find_file(&args.config, None, Some("toml"))?
.or_error(|| format!("Configuration file '{}' not found", args.config))?;
let mut config: Config = util::deserialize_toml_file(&config_path)?;
let blocks = std::mem::take(&mut config.blocks);
Expand Down
4 changes: 3 additions & 1 deletion src/netlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ impl NetDevice {
let path = Path::new("/sys/class/net").join(&iface.name);
let tun = iface.name.starts_with("tun")
|| iface.name.starts_with("tap")
|| path.join("tun_flags").exists();
|| tokio::fs::try_exists(path.join("tun_flags"))
.await
.error("Unable to stat file")?;
let (wg, ppp) = util::read_file(path.join("uevent"))
.await
.map_or((false, false), |c| {
Expand Down
2 changes: 1 addition & 1 deletion src/themes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl TryFrom<ThemeUserConfig> for Theme {

fn try_from(user_config: ThemeUserConfig) -> Result<Self, Self::Error> {
let name = user_config.theme.as_deref().unwrap_or("plain");
let file = util::find_file(name, Some("themes"), Some("toml"))
let file = util::find_file(name, Some("themes"), Some("toml"))?
.or_error(|| format!("Theme '{name}' not found"))?;
let theme: ThemeInner = util::deserialize_toml_file(file)?;
let mut theme = Theme(theme);
Expand Down
57 changes: 41 additions & 16 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ use crate::errors::*;
/// - Then try `/usr/share/`
///
/// Automatically append an extension if not presented.
pub fn find_file(file: &str, subdir: Option<&str>, extension: Option<&str>) -> Option<PathBuf> {
pub fn find_file(
file: &str,
subdir: Option<&str>,
extension: Option<&str>,
) -> Result<Option<PathBuf>> {
let file = Path::new(file);

if file.is_absolute() && file.exists() {
return Some(file.to_path_buf());
if file.is_absolute() && file.try_exists().error("Unable to stat file")? {
return Ok(Some(file.to_path_buf()));
}

// Try XDG_CONFIG_HOME (e.g. `~/.config`)
Expand All @@ -28,8 +32,8 @@ pub fn find_file(file: &str, subdir: Option<&str>, extension: Option<&str>) -> O
xdg_config.push(subdir);
}
xdg_config.push(file);
if let Some(file) = exists_with_opt_extension(&xdg_config, extension) {
return Some(file);
if let Some(file) = exists_with_opt_extension(&xdg_config, extension)? {
return Ok(Some(file));
}
}

Expand All @@ -40,8 +44,8 @@ pub fn find_file(file: &str, subdir: Option<&str>, extension: Option<&str>) -> O
xdg_data.push(subdir);
}
xdg_data.push(file);
if let Some(file) = exists_with_opt_extension(&xdg_data, extension) {
return Some(file);
if let Some(file) = exists_with_opt_extension(&xdg_data, extension)? {
return Ok(Some(file));
}
}

Expand All @@ -51,26 +55,26 @@ pub fn find_file(file: &str, subdir: Option<&str>, extension: Option<&str>) -> O
usr_share_path.push(subdir);
}
usr_share_path.push(file);
if let Some(file) = exists_with_opt_extension(&usr_share_path, extension) {
return Some(file);
if let Some(file) = exists_with_opt_extension(&usr_share_path, extension)? {
return Ok(Some(file));
}

None
Ok(None)
}

fn exists_with_opt_extension(file: &Path, extension: Option<&str>) -> Option<PathBuf> {
if file.exists() {
return Some(file.into());
fn exists_with_opt_extension(file: &Path, extension: Option<&str>) -> Result<Option<PathBuf>> {
if file.try_exists().error("Unable to stat file")? {
return Ok(Some(file.into()));
}
// If file has no extension, test with given extension
if let (None, Some(extension)) = (file.extension(), extension) {
let file = file.with_extension(extension);
// Check again with extension added
if file.exists() {
return Some(file);
if file.try_exists().error("Unable to stat file")? {
return Ok(Some(file));
}
}
None
Ok(None)
}

pub async fn new_dbus_connection() -> Result<zbus::Connection> {
Expand All @@ -95,6 +99,27 @@ where
let contents = std::fs::read_to_string(path)
.or_error(|| format!("Failed to read file: {}", path.display()))?;

deserialize_toml_file_string(contents, path)
}

pub async fn async_deserialize_toml_file<T, P>(path: P) -> Result<T>
where
T: DeserializeOwned,
P: AsRef<Path>,
{
let path = path.as_ref();

let contents = read_file(path)
.await
.or_error(|| format!("Failed to read file: {}", path.display()))?;

deserialize_toml_file_string(contents, path)
}

fn deserialize_toml_file_string<T>(contents: String, path: &Path) -> Result<T>
where
T: DeserializeOwned,
{
toml::from_str(&contents).map_err(|err| {
let location_msg = err
.span()
Expand Down
Loading