use std::path::PathBuf;
struct WorkDir {
session: PathBuf,
current: PathBuf,
}
thread_local! {
static WORKDIR: std::cell::RefCell<Option<WorkDir>> = const { std::cell::RefCell::new(None) };
}
pub fn set_session_working_directory(path: PathBuf) {
if let Some(session_id) = crate::session::context::current_session_id() {
crate::session::context::set_session_workdir(&session_id, path);
return;
}
WORKDIR.with(|w| {
*w.borrow_mut() = Some(WorkDir {
session: path.clone(),
current: path,
});
});
}
pub fn set_thread_working_directory(path: PathBuf) {
if let Some(session_id) = crate::session::context::current_session_id() {
crate::session::context::set_current_workdir(&session_id, path);
return;
}
WORKDIR.with(|w| {
let mut w = w.borrow_mut();
if let Some(ref mut wd) = *w {
wd.current = path;
}
});
}
pub fn get_thread_working_directory() -> PathBuf {
if let Some(session_id) = crate::session::context::current_session_id() {
if let Some(path) = crate::session::context::get_current_workdir(&session_id) {
return path;
}
}
WORKDIR.with(|w| {
w.borrow()
.as_ref()
.map(|wd| wd.current.clone())
.unwrap_or_else(|| std::env::current_dir().unwrap_or_default())
})
}
pub fn get_thread_original_working_directory() -> PathBuf {
if let Some(session_id) = crate::session::context::current_session_id() {
if let Some(path) = crate::session::context::get_session_workdir_anchor(&session_id) {
return path;
}
}
WORKDIR.with(|w| {
w.borrow()
.as_ref()
.map(|wd| wd.session.clone())
.unwrap_or_else(|| std::env::current_dir().unwrap_or_default())
})
}