aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/utils
diff options
context:
space:
mode:
authorKunal Mohan <kunalmohan99@gmail.com>2021-05-04 13:48:19 +0530
committerKunal Mohan <kunalmohan99@gmail.com>2021-05-04 20:48:18 +0530
commit585b225290df3e44a86555490dc9170f88148fa6 (patch)
tree3709b0cb99ea5a32d1ebc7d4fb3cae6c47dfe0c0 /src/common/utils
parentAdd version to socket path to avoid problems after version update (diff)
downloadsl0ppij-585b225290df3e44a86555490dc9170f88148fa6.tar.gz
sl0ppij-585b225290df3e44a86555490dc9170f88148fa6.tar.xz
sl0ppij-585b225290df3e44a86555490dc9170f88148fa6.zip
Use ZELLIJ_SOCKET_DIR env variable and make user specific tmp dir
Diffstat (limited to 'src/common/utils')
-rw-r--r--src/common/utils/consts.rs25
-rw-r--r--src/common/utils/logging.rs20
2 files changed, 25 insertions, 20 deletions
diff --git a/src/common/utils/consts.rs b/src/common/utils/consts.rs
index 244845da..4c219fed 100644
--- a/src/common/utils/consts.rs
+++ b/src/common/utils/consts.rs
@@ -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");
}
diff --git a/src/common/utils/logging.rs b/src/common/utils/logging.rs
index 06827bfc..d99a2428 100644
--- a/src/common/utils/logging.rs
+++ b/src/common/utils/logging.rs
@@ -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()