diff options
| author | Kunal Mohan <44079328+kunalmohan@users.noreply.github.com> | 2021-05-06 22:11:07 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-06 22:11:07 +0530 |
| commit | d5433f8f8995c155f231c823d49813882b6212bf (patch) | |
| tree | 529d5ee706ded18cd24f26a3b0372bf17ecb81ed /src/common/utils | |
| parent | docs(changelog): Add Manpage #455 (diff) | |
| parent | explicitly set file and directory permissions (diff) | |
| download | sl0ppij-d5433f8f8995c155f231c823d49813882b6212bf.tar.gz sl0ppij-d5433f8f8995c155f231c823d49813882b6212bf.tar.xz sl0ppij-d5433f8f8995c155f231c823d49813882b6212bf.zip | |
Merge pull request #223 from zellij-org/isolate-pty
Psuedo Client-Server model
Diffstat (limited to 'src/common/utils')
| -rw-r--r-- | src/common/utils/consts.rs | 37 | ||||
| -rw-r--r-- | src/common/utils/logging.rs | 29 |
2 files changed, 51 insertions, 15 deletions
diff --git a/src/common/utils/consts.rs b/src/common/utils/consts.rs index 4bd1b5b1..a46f8243 100644 --- a/src/common/utils/consts.rs +++ b/src/common/utils/consts.rs @@ -1,12 +1,41 @@ //! Zellij program-wide constants. -pub const ZELLIJ_TMP_DIR: &str = "/tmp/zellij"; -pub const ZELLIJ_TMP_LOG_DIR: &str = "/tmp/zellij/zellij-log"; -pub const ZELLIJ_TMP_LOG_FILE: &str = "/tmp/zellij/zellij-log/log.txt"; -pub const ZELLIJ_IPC_PIPE: &str = "/tmp/zellij/ipc"; +use crate::os_input_output::set_permissions; +use directories_next::ProjectDirs; +use lazy_static::lazy_static; +use nix::unistd::Uid; +use std::path::PathBuf; +use std::{env, fs}; pub const ZELLIJ_CONFIG_FILE_ENV: &str = "ZELLIJ_CONFIG_FILE"; pub const ZELLIJ_CONFIG_DIR_ENV: &str = "ZELLIJ_CONFIG_DIR"; +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); // TODO: ${PREFIX} argument in makefile pub const SYSTEM_DEFAULT_CONFIG_DIR: &str = "/etc/zellij"; + +lazy_static! { + static ref UID: Uid = Uid::current(); + pub static ref SESSION_NAME: String = names::Generator::default().next().unwrap(); + pub static ref ZELLIJ_PROJ_DIR: ProjectDirs = + ProjectDirs::from("org", "Zellij Contributors", "Zellij").unwrap(); + pub static ref ZELLIJ_IPC_PIPE: PathBuf = { + let mut ipc_dir = env::var("ZELLIJ_SOCKET_DIR").map_or_else( + |_| { + ZELLIJ_PROJ_DIR + .runtime_dir() + .map_or_else(|| ZELLIJ_TMP_DIR.clone(), |p| p.to_owned()) + }, + PathBuf::from, + ); + ipc_dir.push(VERSION); + fs::create_dir_all(&ipc_dir).unwrap(); + set_permissions(&ipc_dir); + ipc_dir.push(&*SESSION_NAME); + ipc_dir + }; + pub static ref ZELLIJ_TMP_DIR: PathBuf = + PathBuf::from("/tmp/zellij-".to_string() + &format!("{}", *UID)); + pub static ref ZELLIJ_TMP_LOG_DIR: PathBuf = ZELLIJ_TMP_DIR.join("zellij-log"); + pub static ref ZELLIJ_TMP_LOG_FILE: PathBuf = ZELLIJ_TMP_LOG_DIR.join("log.txt"); +} diff --git a/src/common/utils/logging.rs b/src/common/utils/logging.rs index 06827bfc..ff3fb44c 100644 --- a/src/common/utils/logging.rs +++ b/src/common/utils/logging.rs @@ -4,17 +4,20 @@ use std::{ fs, io::{self, prelude::*}, os::unix::io::RawFd, - path::PathBuf, + path::{Path, PathBuf}, }; +use crate::os_input_output::set_permissions; use crate::utils::consts::{ZELLIJ_TMP_LOG_DIR, ZELLIJ_TMP_LOG_FILE}; -pub fn atomic_create_file(file_name: &str) { +pub fn atomic_create_file(file_name: &Path) { let _ = fs::OpenOptions::new().create(true).open(file_name); + #[cfg(not(test))] + set_permissions(file_name); } -pub fn atomic_create_dir(dir_name: &str) -> io::Result<()> { - if let Err(e) = fs::create_dir(dir_name) { +pub fn atomic_create_dir(dir_name: &Path) -> io::Result<()> { + let result = if let Err(e) = fs::create_dir(dir_name) { if e.kind() == std::io::ErrorKind::AlreadyExists { Ok(()) } else { @@ -22,7 +25,11 @@ pub fn atomic_create_dir(dir_name: &str) -> io::Result<()> { } } else { Ok(()) + }; + if result.is_ok() { + set_permissions(dir_name); } + result } pub fn debug_log_to_file(mut message: String) -> io::Result<()> { @@ -31,11 +38,11 @@ pub fn debug_log_to_file(mut message: String) -> io::Result<()> { } pub fn debug_log_to_file_without_newline(message: String) -> io::Result<()> { - atomic_create_file(ZELLIJ_TMP_LOG_FILE); + atomic_create_file(&*ZELLIJ_TMP_LOG_FILE); let mut file = fs::OpenOptions::new() .append(true) .create(true) - .open(ZELLIJ_TMP_LOG_FILE)?; + .open(&*ZELLIJ_TMP_LOG_FILE)?; file.write_all(message.as_bytes()) } @@ -48,16 +55,16 @@ pub fn _debug_log_to_file_pid_3(message: String, pid: RawFd) -> io::Result<()> { } pub fn _delete_log_file() -> io::Result<()> { - if fs::metadata(ZELLIJ_TMP_LOG_FILE).is_ok() { - fs::remove_file(ZELLIJ_TMP_LOG_FILE) + if fs::metadata(&*ZELLIJ_TMP_LOG_FILE).is_ok() { + fs::remove_file(&*ZELLIJ_TMP_LOG_FILE) } else { Ok(()) } } pub fn _delete_log_dir() -> io::Result<()> { - if fs::metadata(ZELLIJ_TMP_LOG_DIR).is_ok() { - fs::remove_dir_all(ZELLIJ_TMP_LOG_DIR) + if fs::metadata(&*ZELLIJ_TMP_LOG_DIR).is_ok() { + fs::remove_dir_all(&*ZELLIJ_TMP_LOG_DIR) } else { Ok(()) } @@ -65,7 +72,7 @@ pub fn _delete_log_dir() -> io::Result<()> { pub fn debug_to_file(message: u8, pid: RawFd) -> io::Result<()> { let mut path = PathBuf::new(); - path.push(ZELLIJ_TMP_LOG_DIR); + path.push(&*ZELLIJ_TMP_LOG_DIR); path.push(format!("zellij-{}.log", pid.to_string())); let mut file = fs::OpenOptions::new() |
