Use ZELLIJ_SOCKET_DIR env variable and make user specific tmp dir

This commit is contained in:
Kunal Mohan 2021-05-04 13:48:19 +05:30
parent a05a12dbec
commit 585b225290
3 changed files with 27 additions and 22 deletions

View file

@ -4,10 +4,7 @@ use directories_next::ProjectDirs;
use lazy_static::lazy_static;
use nix::unistd::Uid;
use std::path::PathBuf;
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";
use std::{env, fs};
pub const ZELLIJ_CONFIG_FILE_ENV: &str = "ZELLIJ_CONFIG_FILE";
pub const ZELLIJ_CONFIG_DIR_ENV: &str = "ZELLIJ_CONFIG_DIR";
@ -21,14 +18,22 @@ lazy_static! {
pub static ref ZELLIJ_PROJ_DIR: ProjectDirs =
ProjectDirs::from("org", "Zellij Contributors", "Zellij").unwrap();
pub static ref ZELLIJ_IPC_PIPE: PathBuf = {
let version = std::env::var("CARGO_PKG_VERSION").unwrap();
let mut ipc_dir = ZELLIJ_PROJ_DIR
.runtime_dir()
.map(|p| p.to_owned())
.unwrap_or_else(|| PathBuf::from("/tmp/zellij-".to_string() + &format!("{}", *UID)));
let version = env::var("CARGO_PKG_VERSION").unwrap();
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);
std::fs::create_dir_all(&ipc_dir).unwrap();
fs::create_dir_all(&ipc_dir).unwrap();
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");
}

View file

@ -4,16 +4,16 @@ use std::{
fs,
io::{self, prelude::*},
os::unix::io::RawFd,
path::PathBuf,
path::{Path, PathBuf},
};
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);
}
pub fn atomic_create_dir(dir_name: &str) -> io::Result<()> {
pub fn atomic_create_dir(dir_name: &Path) -> io::Result<()> {
if let Err(e) = fs::create_dir(dir_name) {
if e.kind() == std::io::ErrorKind::AlreadyExists {
Ok(())
@ -31,11 +31,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 +48,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 +65,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()

View file

@ -49,8 +49,8 @@ pub fn main() {
setup::dump_default_config().expect("Failed to print to stdout");
std::process::exit(1);
} else {
atomic_create_dir(ZELLIJ_TMP_DIR).unwrap();
atomic_create_dir(ZELLIJ_TMP_LOG_DIR).unwrap();
atomic_create_dir(&*ZELLIJ_TMP_DIR).unwrap();
atomic_create_dir(&*ZELLIJ_TMP_LOG_DIR).unwrap();
let server_os_input = get_server_os_input();
let os_input = get_client_os_input();
start(Box::new(os_input), opts, Box::new(server_os_input), config);