fix: make sensor file world-readable, create parent directories

This commit is contained in:
Markus Zehnder
2025-08-31 14:50:34 +02:00
parent 21bf19ce31
commit 1ba9ccfd61
2 changed files with 24 additions and 8 deletions
+6 -5
View File
@@ -10,13 +10,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
_Changes in the next release_ _Changes in the next release_
### Fixed ### Fixed
- Misplaced text sensors in custom panels (#11). - Misplaced text sensors in custom panels ([#11](https://github.com/zehnm/aoostar-rs/issues/11)).
- Wrong start position for circular progress (fan) sensor using a counter-clockwise direction (#12). - Wrong start position for circular progress (fan) sensor using a counter-clockwise direction ([#12](https://github.com/zehnm/aoostar-rs/issues/12)).
- sysinfo tool: make sensor file world-readable, create all parent directories.
### Added ### Added
- Simple sensor panel with a file-based data source (#6). - Simple sensor panel with a file-based data source ([#6](https://github.com/zehnm/aoostar-rs/issues/6)).
- Initial support for fan-, progress-, & pointer-sensors (#8). - Initial support for fan-, progress-, & pointer-sensors ([#8](https://github.com/zehnm/aoostar-rs/pull/8)).
- Use [mdBook](https://rust-lang.github.io/mdBook/) for documentation and publish user guide to GitHub pages (#10). - Use [mdBook](https://rust-lang.github.io/mdBook/) for documentation and publish user guide to GitHub pages ([#10](https://github.com/zehnm/aoostar-rs/pull/10)).
### Changed ### Changed
- Project structure using a Cargo workspace. - Project structure using a Cargo workspace.
+18 -3
View File
@@ -14,12 +14,13 @@ use std::collections::HashMap;
use std::fmt::Display; use std::fmt::Display;
use std::fs; use std::fs;
use std::io::{BufWriter, Write}; use std::io::{BufWriter, Write};
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::{Command, exit}; use std::process::{Command, exit};
use std::thread::sleep; use std::thread::sleep;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use sysinfo::{Components, DiskKind, Disks, Networks, System}; use sysinfo::{Components, DiskKind, Disks, Networks, System};
use tempfile::NamedTempFile; use tempfile::{Builder, NamedTempFile};
/// Proof of concept sensor value collection for the asterctl screen control tool. /// Proof of concept sensor value collection for the asterctl screen control tool.
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@@ -65,6 +66,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(not(target_os = "linux"))] #[cfg(not(target_os = "linux"))]
let use_smartctl = false; let use_smartctl = false;
if let Some(out_file) = &args.out && let Some(parent) = out_file.parent() {
fs::create_dir_all(parent)?;
}
let mut sensors = HashMap::with_capacity(64); let mut sensors = HashMap::with_capacity(64);
let mut sysinfo_source = SysinfoSource::new(); let mut sysinfo_source = SysinfoSource::new();
@@ -76,6 +80,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
update_linux_storage_sensors(&mut sensors, use_smartctl)?; update_linux_storage_sensors(&mut sensors, use_smartctl)?;
} }
if !refresh.is_zero() {
info!("Starting sysinfo with refresh={}ms", refresh.as_millis());
}
loop { loop {
let upd_start_time = Instant::now(); let upd_start_time = Instant::now();
@@ -123,13 +131,19 @@ fn write_sensor_file(
exit(1); exit(1);
} }
// make sure our sensor file can be read by everyone
let all_read_perm = fs::Permissions::from_mode(0o664);
let tmp_file = if let Some(temp_path) = temp_dir { let tmp_file = if let Some(temp_path) = temp_dir {
fs::create_dir_all(temp_path)?; fs::create_dir_all(temp_path)?;
NamedTempFile::new_in(temp_path)?
debug!("Creating a new named temp file in {temp_path:?}");
Builder::new().permissions(all_read_perm).tempfile_in(temp_path)?
} else { } else {
NamedTempFile::new()? debug!("Creating a new named temp file");
Builder::new().permissions(all_read_perm).tempfile()?
}; };
debug!("Writing sensor temp file...");
let mut stream = BufWriter::new(&tmp_file); let mut stream = BufWriter::new(&tmp_file);
for (label, value) in sensors.iter() { for (label, value) in sensors.iter() {
@@ -138,6 +152,7 @@ fn write_sensor_file(
stream.flush()?; stream.flush()?;
drop(stream); drop(stream);
debug!("Renaming temp file to: {out_file:?}");
tmp_file.persist(out_file)?; tmp_file.persist(out_file)?;
Ok(()) Ok(())