summaryrefslogtreecommitdiff
path: root/sound_card_init/utils
diff options
context:
space:
mode:
authorJorge E. Moreira <jemoreira@google.com>2021-02-08 17:59:31 -0800
committerJorge E. Moreira <jemoreira@google.com>2021-02-08 17:59:31 -0800
commitdc2b74a983c1182f9f4e05a8e6e91beb04792c51 (patch)
tree8c44572911d479dc6dca87dd1cc2e9eca46500ba /sound_card_init/utils
parentc8f8b3cd94220d03fcdee753d8fc762224c3d177 (diff)
parent9668bb7e705f4102e0558c522f1488a966a51826 (diff)
downloadadhd-dc2b74a983c1182f9f4e05a8e6e91beb04792c51.tar.gz
Merge branch 'upstream-master'
Change-Id: I25af576b68bcd95ee401067ee798e963f9a96264
Diffstat (limited to 'sound_card_init/utils')
-rw-r--r--sound_card_init/utils/Cargo.lock109
-rw-r--r--sound_card_init/utils/Cargo.toml11
-rw-r--r--sound_card_init/utils/src/error.rs39
-rw-r--r--sound_card_init/utils/src/lib.rs87
4 files changed, 246 insertions, 0 deletions
diff --git a/sound_card_init/utils/Cargo.lock b/sound_card_init/utils/Cargo.lock
new file mode 100644
index 00000000..fd53fb0f
--- /dev/null
+++ b/sound_card_init/utils/Cargo.lock
@@ -0,0 +1,109 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "dtoa"
+version = "0.4.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "134951f4028bdadb9b84baf4232681efbf277da25144b9b0ad65df75946c422b"
+
+[[package]]
+name = "linked-hash-map"
+version = "0.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a"
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.24"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
+dependencies = [
+ "unicode-xid",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "remain"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "70ba1e78fa68412cb93ef642fd4d20b9a941be49ee9333875ebaf13112673ea7"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "serde"
+version = "1.0.116"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "96fe57af81d28386a513cbc6858332abc6117cfdb5999647c6444b8f43a370a5"
+dependencies = [
+ "serde_derive",
+]
+
+[[package]]
+name = "serde_derive"
+version = "1.0.116"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f630a6370fd8e457873b4bd2ffdae75408bc291ba72be773772a4c2a065d9ae8"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "serde_yaml"
+version = "0.8.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ae3e2dd40a7cdc18ca80db804b7f461a39bb721160a85c9a1fa30134bf3c02a5"
+dependencies = [
+ "dtoa",
+ "linked-hash-map",
+ "serde",
+ "yaml-rust",
+]
+
+[[package]]
+name = "syn"
+version = "1.0.44"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e03e57e4fcbfe7749842d53e24ccb9aa12b7252dbe5e91d2acad31834c8b8fdd"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-xid",
+]
+
+[[package]]
+name = "unicode-xid"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
+
+[[package]]
+name = "utils"
+version = "0.1.0"
+dependencies = [
+ "remain",
+ "serde",
+ "serde_yaml",
+]
+
+[[package]]
+name = "yaml-rust"
+version = "0.4.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "39f0c922f1a334134dc2f7a8b67dc5d25f0735263feec974345ff706bcf20b0d"
+dependencies = [
+ "linked-hash-map",
+]
diff --git a/sound_card_init/utils/Cargo.toml b/sound_card_init/utils/Cargo.toml
new file mode 100644
index 00000000..8c688de3
--- /dev/null
+++ b/sound_card_init/utils/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "utils"
+version = "0.1.0"
+authors = ["The Chromium OS Authors"]
+edition = "2018"
+description = "Utils for sound_card_init"
+
+[dependencies]
+remain = "0.2.1"
+serde = { version = "1.0", features = ["derive"]}
+serde_yaml = "0.8.11"
diff --git a/sound_card_init/utils/src/error.rs b/sound_card_init/utils/src/error.rs
new file mode 100644
index 00000000..93d53b9b
--- /dev/null
+++ b/sound_card_init/utils/src/error.rs
@@ -0,0 +1,39 @@
+// 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.
+
+//! This mod contains all possible errors that can occur within utils.
+use std::fmt;
+use std::io;
+use std::time;
+use std::{error, path::PathBuf};
+
+use remain::sorted;
+
+/// Alias for a `Result` with the error type `utils::Error`.
+pub type Result<T> = std::result::Result<T, Error>;
+
+/// This type represents all possible errors that can occur within utils.
+#[sorted]
+#[derive(Debug)]
+pub enum Error {
+ /// It wraps file path with the io::Error.
+ FileIOFailed(PathBuf, io::Error),
+ /// It wraps file path with the serde_yaml::Error.
+ SerdeError(PathBuf, serde_yaml::Error),
+ /// It wraps time::SystemTimeError.
+ SystemTimeError(time::SystemTimeError),
+}
+
+impl error::Error for Error {}
+
+impl fmt::Display for Error {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ use Error::*;
+ match self {
+ FileIOFailed(file, e) => write!(f, "{:?}: {}", file, e),
+ SerdeError(file, e) => write!(f, "{:?}: {}", file, e),
+ SystemTimeError(e) => write!(f, "{}", e),
+ }
+ }
+}
diff --git a/sound_card_init/utils/src/lib.rs b/sound_card_init/utils/src/lib.rs
new file mode 100644
index 00000000..e8edce77
--- /dev/null
+++ b/sound_card_init/utils/src/lib.rs
@@ -0,0 +1,87 @@
+// 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)]
+
+//! The error definitions for utils.
+pub mod error;
+
+use std::fs::File;
+use std::io::{prelude::*, BufReader, BufWriter};
+use std::path::PathBuf;
+use std::time::Duration;
+
+use crate::error::{Error, Result};
+
+/// The path of datastore.
+pub const DATASTORE_DIR: &str = "/var/lib/sound_card_init";
+
+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_DIR)
+ .join(snd_card)
+ .join(RUN_TIME_FILE)
+ }
+}