hotfix(logging): atomically create tmp folders

This commit is contained in:
Aram Drevekenin 2020-11-25 17:08:43 +01:00
parent 761695fe9b
commit 489ade6eb8
3 changed files with 21 additions and 3 deletions

View file

@ -26,7 +26,10 @@ use crate::layout::Layout;
use crate::os_input_output::{get_os_input, OsApi}; use crate::os_input_output::{get_os_input, OsApi};
use crate::pty_bus::{PtyBus, PtyInstruction, VteEvent}; use crate::pty_bus::{PtyBus, PtyInstruction, VteEvent};
use crate::screen::{Screen, ScreenInstruction}; use crate::screen::{Screen, ScreenInstruction};
use crate::utils::{consts::MOSAIC_IPC_PIPE, logging::*}; use crate::utils::{
consts::{MOSAIC_IPC_PIPE, MOSAIC_TMP_DIR, MOSAIC_TMP_LOG_DIR},
logging::*,
};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
enum ApiCommand { enum ApiCommand {
@ -84,6 +87,8 @@ pub fn main() {
stream.write_all(&api_command).unwrap(); stream.write_all(&api_command).unwrap();
} else { } else {
let os_input = get_os_input(); let os_input = get_os_input();
atomic_create_dir(MOSAIC_TMP_DIR).unwrap();
atomic_create_dir(MOSAIC_TMP_LOG_DIR).unwrap();
start(Box::new(os_input), opts); start(Box::new(os_input), opts);
} }
} }

View file

@ -1,3 +1,4 @@
pub const MOSAIC_TMP_LOG_FILE: &str = "/tmp/mosaic/mosaic-log/log.txt"; pub const MOSAIC_TMP_DIR: &str = "/tmp/mosaic";
pub const MOSAIC_TMP_LOG_DIR: &str = "/tmp/mosaic/mosaic-log"; pub const MOSAIC_TMP_LOG_DIR: &str = "/tmp/mosaic/mosaic-log";
pub const MOSAIC_TMP_LOG_FILE: &str = "/tmp/mosaic/mosaic-log/log.txt";
pub const MOSAIC_IPC_PIPE: &str = "/tmp/mosaic/ipc"; pub const MOSAIC_IPC_PIPE: &str = "/tmp/mosaic/ipc";

View file

@ -7,10 +7,22 @@ use std::{
use crate::utils::consts::{MOSAIC_TMP_LOG_DIR, MOSAIC_TMP_LOG_FILE}; use crate::utils::consts::{MOSAIC_TMP_LOG_DIR, MOSAIC_TMP_LOG_FILE};
fn atomic_create_file(file_name: &str) { pub fn atomic_create_file(file_name: &str) {
let _ = fs::OpenOptions::new().create(true).open(file_name); let _ = fs::OpenOptions::new().create(true).open(file_name);
} }
pub fn atomic_create_dir(dir_name: &str) -> io::Result<()> {
if let Err(e) = fs::create_dir(dir_name) {
if e.kind() == std::io::ErrorKind::AlreadyExists {
Ok(())
} else {
Err(e)
}
} else {
Ok(())
}
}
pub fn debug_log_to_file(message: String) -> io::Result<()> { pub fn debug_log_to_file(message: String) -> io::Result<()> {
atomic_create_file(MOSAIC_TMP_LOG_FILE); atomic_create_file(MOSAIC_TMP_LOG_FILE);
let mut file = fs::OpenOptions::new() let mut file = fs::OpenOptions::new()