• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //! It contains common utils shared within sound_card_init.
5 #![deny(missing_docs)]
6 
7 use std::fs::File;
8 use std::io::{prelude::*, BufReader, BufWriter};
9 use std::path::PathBuf;
10 use std::time::Duration;
11 
12 use crate::datastore::Datastore;
13 use crate::error::{Error, Result};
14 
duration_from_file(path: &PathBuf) -> Result<Duration>15 fn duration_from_file(path: &PathBuf) -> Result<Duration> {
16     let reader =
17         BufReader::new(File::open(&path).map_err(|e| Error::FileIOFailed(path.clone(), e))?);
18     serde_yaml::from_reader(reader).map_err(|e| Error::SerdeError(path.clone(), e))
19 }
20 
21 /// The utils to parse CRAS shutdown time file.
22 pub mod shutdown_time {
23     use super::*;
24     // The path of CRAS shutdown time file.
25     const SHUTDOWN_TIME_FILE: &str = "/var/lib/cras/stop";
26 
27     /// Reads the unix time from CRAS shutdown time file.
from_file() -> Result<Duration>28     pub fn from_file() -> Result<Duration> {
29         duration_from_file(&PathBuf::from(SHUTDOWN_TIME_FILE))
30     }
31 }
32 
33 /// The utils to create and parse sound_card_init run time file.
34 pub mod run_time {
35     use std::time::SystemTime;
36 
37     use super::*;
38     // The filename of sound_card_init run time file.
39     const RUN_TIME_FILE: &str = "run";
40 
41     /// Returns the sound_card_init run time file existence.
exists(snd_card: &str) -> bool42     pub fn exists(snd_card: &str) -> bool {
43         run_time_file(snd_card).exists()
44     }
45 
46     /// Reads the unix time from sound_card_init run time file.
from_file(snd_card: &str) -> Result<Duration>47     pub fn from_file(snd_card: &str) -> Result<Duration> {
48         duration_from_file(&run_time_file(snd_card))
49     }
50 
51     /// Saves the current unix time to sound_card_init run time file.
now_to_file(snd_card: &str) -> Result<()>52     pub fn now_to_file(snd_card: &str) -> Result<()> {
53         match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
54             Ok(t) => to_file(snd_card, t),
55             Err(e) => Err(Error::SystemTimeError(e)),
56         }
57     }
58 
59     /// Saves the unix time to sound_card_init run time file.
to_file(snd_card: &str, duration: Duration) -> Result<()>60     pub fn to_file(snd_card: &str, duration: Duration) -> Result<()> {
61         let path = run_time_file(snd_card);
62         let mut writer =
63             BufWriter::new(File::create(&path).map_err(|e| Error::FileIOFailed(path.clone(), e))?);
64         writer
65             .write_all(
66                 serde_yaml::to_string(&duration)
67                     .map_err(|e| Error::SerdeError(path.clone(), e))?
68                     .as_bytes(),
69             )
70             .map_err(|e| Error::FileIOFailed(path.clone(), e))?;
71         writer
72             .flush()
73             .map_err(|e| Error::FileIOFailed(path.clone(), e))?;
74         Ok(())
75     }
76 
run_time_file(snd_card: &str) -> PathBuf77     fn run_time_file(snd_card: &str) -> PathBuf {
78         PathBuf::from(Datastore::DATASTORE_DIR)
79             .join(snd_card)
80             .join(RUN_TIME_FILE)
81     }
82 }
83