power: Make sure to flush writes before dropping the files

This commit is contained in:
Vicki Pfau 2024-04-16 18:14:18 -07:00
parent e3122059e5
commit f004035f7f
2 changed files with 18 additions and 16 deletions

View file

@ -6,7 +6,9 @@
*/
use anyhow::{anyhow, bail, Error, Result};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use tokio::fs::File;
use tokio::io::AsyncWriteExt;
use tokio::signal::unix::{signal, SignalKind};
use tokio::task::JoinSet;
use tokio_util::sync::CancellationToken;
@ -76,6 +78,12 @@ pub fn path<S: AsRef<str>>(path: S) -> PathBuf {
.join(path.as_ref().trim_start_matches('/'))
}
pub async fn write_synced<P: AsRef<Path>>(path: P, bytes: &[u8]) -> Result<()> {
let mut file = File::create(path.as_ref()).await?;
file.write_all(bytes).await?;
Ok(file.sync_data().await?)
}
pub fn read_comm(pid: u32) -> Result<String> {
let comm = std::fs::read_to_string(path(format!("/proc/{}/comm", pid)))?;
Ok(comm.trim_end().to_string())