Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change dependency for process_cwd #1001

Merged
merged 5 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/tests/e2e/cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ pub fn detach_and_attach_session() {
name: "Wait for session to be attached",
instruction: |remote_terminal: RemoteTerminal| -> bool {
let mut step_is_complete = false;
if remote_terminal.cursor_position_is(77, 2) {
if remote_terminal.cursor_position_is(3, 2) {
// we're back inside the session
step_is_complete = true;
}
Expand Down
4 changes: 1 addition & 3 deletions zellij-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ log = "0.4.14"
typetag = "0.1.7"
chrono = "0.4.19"
close_fds = "0.3.2"

[target.'cfg(target_os = "macos")'.dependencies]
darwin-libproc = "0.2.0"
sysinfo = "0.22.5"

[dev-dependencies]
insta = "1.6.0"
Expand Down
29 changes: 14 additions & 15 deletions zellij-server/src/os_input_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ use std::collections::HashMap;

use crate::panes::PaneId;

#[cfg(target_os = "macos")]
use darwin_libproc;

#[cfg(target_os = "linux")]
use std::fs;

use std::env;
use std::os::unix::io::RawFd;
use std::os::unix::process::CommandExt;
Expand All @@ -21,6 +15,8 @@ use async_std::fs::File as AsyncFile;
use async_std::os::unix::io::FromRawFd;
use interprocess::local_socket::LocalSocketStream;

use sysinfo::{ProcessExt, ProcessRefreshKind, System, SystemExt};

use nix::pty::{openpty, OpenptyResult, Winsize};
use nix::sys::signal::{kill, Signal};
use nix::sys::termios;
Expand Down Expand Up @@ -338,16 +334,15 @@ impl ServerOsApi for ServerOsInputOutput {
fn load_palette(&self) -> Palette {
default_palette()
}
#[cfg(target_os = "macos")]
fn get_cwd(&self, pid: Pid) -> Option<PathBuf> {
darwin_libproc::pid_cwd(pid.as_raw()).ok()
}
#[cfg(target_os = "linux")]
fn get_cwd(&self, pid: Pid) -> Option<PathBuf> {
fs::read_link(format!("/proc/{}/cwd", pid)).ok()
}
#[cfg(all(not(target_os = "linux"), not(target_os = "macos")))]
fn get_cwd(&self, _pid: Pid) -> Option<PathBuf> {
let mut system_info = System::new();
// Update by minimizing information.
// See https://docs.rs/sysinfo/0.22.5/sysinfo/struct.ProcessRefreshKind.html#
system_info.refresh_processes_specifics(ProcessRefreshKind::default());

if let Some(process) = system_info.process(pid.into()) {
return Some(process.cwd().to_path_buf());
}
None
}
}
Expand Down Expand Up @@ -376,3 +371,7 @@ pub struct ChildId {
/// field is it's parent process id.
pub shell: Option<Pid>,
}

#[cfg(test)]
#[path = "./unit/os_input_output_tests.rs"]
mod os_input_output_tests;
49 changes: 49 additions & 0 deletions zellij-server/src/unit/os_input_output_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use super::*;

use nix::{pty::openpty, unistd::close};

struct TestTerminal {
openpty: OpenptyResult,
}

impl TestTerminal {
pub fn new() -> TestTerminal {
let openpty = openpty(None, None).expect("Could not create openpty");
TestTerminal { openpty }
}

#[allow(dead_code)]
pub fn master(&self) -> RawFd {
self.openpty.master
}

pub fn slave(&self) -> RawFd {
self.openpty.slave
}
}

impl Drop for TestTerminal {
fn drop(&mut self) {
close(self.openpty.master).expect("Failed to close the master");
close(self.openpty.slave).expect("Failed to close the slave");
}
}

#[test]
fn get_cwd() {
let test_terminal = TestTerminal::new();
let test_termios =
termios::tcgetattr(test_terminal.slave()).expect("Could not configure the termios");

let server = ServerOsInputOutput {
orig_termios: Arc::new(Mutex::new(test_termios)),
client_senders: Arc::default(),
};

let pid = nix::unistd::getpid();
assert!(
server.get_cwd(pid).is_some(),
"Get current working directory from PID {}",
pid
);
}