diff options
author | Jorge E. Moreira <jemoreira@google.com> | 2021-04-09 16:07:03 -0700 |
---|---|---|
committer | Jorge E. Moreira <jemoreira@google.com> | 2021-04-09 16:07:03 -0700 |
commit | 072dce8ca50bdf87b113490cdf14bde14724914f (patch) | |
tree | 7e0e07b9d8d50fc5dd352d5b36d74fa3cb3427db /sound_card_init/dsm/src/utils.rs | |
parent | 0aaab56eba431583969e73f177a0efdcebf46cac (diff) | |
parent | 3624a4296888926a21462bfd499d4ba91268046b (diff) | |
download | adhd-android-s-beta-2.tar.gz |
Merge remote-tracking branch 'aosp/upstream-main' into masterandroid-s-beta-2android-s-beta-1main-cg-testing-release
This merge is necessary to update crosvm to the latest version
Bug: 163867676
Test: locally
Change-Id: Ia0681ee12911ac60f44b04008c5a0590032733a3
Diffstat (limited to 'sound_card_init/dsm/src/utils.rs')
-rw-r--r-- | sound_card_init/dsm/src/utils.rs | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/sound_card_init/dsm/src/utils.rs b/sound_card_init/dsm/src/utils.rs new file mode 100644 index 00000000..64f6c972 --- /dev/null +++ b/sound_card_init/dsm/src/utils.rs @@ -0,0 +1,82 @@ +// Copyright 2020 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +//! It contains common utils shared within sound_card_init. +#![deny(missing_docs)] + +use std::fs::File; +use std::io::{prelude::*, BufReader, BufWriter}; +use std::path::PathBuf; +use std::time::Duration; + +use crate::datastore::Datastore; +use crate::error::{Error, Result}; + +fn duration_from_file(path: &PathBuf) -> Result<Duration> { + let reader = + BufReader::new(File::open(&path).map_err(|e| Error::FileIOFailed(path.clone(), e))?); + serde_yaml::from_reader(reader).map_err(|e| Error::SerdeError(path.clone(), e)) +} + +/// The utils to parse CRAS shutdown time file. +pub mod shutdown_time { + use super::*; + // The path of CRAS shutdown time file. + const SHUTDOWN_TIME_FILE: &str = "/var/lib/cras/stop"; + + /// Reads the unix time from CRAS shutdown time file. + pub fn from_file() -> Result<Duration> { + duration_from_file(&PathBuf::from(SHUTDOWN_TIME_FILE)) + } +} + +/// The utils to create and parse sound_card_init run time file. +pub mod run_time { + use std::time::SystemTime; + + use super::*; + // The filename of sound_card_init run time file. + const RUN_TIME_FILE: &str = "run"; + + /// Returns the sound_card_init run time file existence. + pub fn exists(snd_card: &str) -> bool { + run_time_file(snd_card).exists() + } + + /// Reads the unix time from sound_card_init run time file. + pub fn from_file(snd_card: &str) -> Result<Duration> { + duration_from_file(&run_time_file(snd_card)) + } + + /// Saves the current unix time to sound_card_init run time file. + pub fn now_to_file(snd_card: &str) -> Result<()> { + match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) { + Ok(t) => to_file(snd_card, t), + Err(e) => Err(Error::SystemTimeError(e)), + } + } + + /// Saves the unix time to sound_card_init run time file. + pub fn to_file(snd_card: &str, duration: Duration) -> Result<()> { + let path = run_time_file(snd_card); + let mut writer = + BufWriter::new(File::create(&path).map_err(|e| Error::FileIOFailed(path.clone(), e))?); + writer + .write_all( + serde_yaml::to_string(&duration) + .map_err(|e| Error::SerdeError(path.clone(), e))? + .as_bytes(), + ) + .map_err(|e| Error::FileIOFailed(path.clone(), e))?; + writer + .flush() + .map_err(|e| Error::FileIOFailed(path.clone(), e))?; + Ok(()) + } + + fn run_time_file(snd_card: &str) -> PathBuf { + PathBuf::from(Datastore::DATASTORE_DIR) + .join(snd_card) + .join(RUN_TIME_FILE) + } +} |