ishell/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use std::env;
use std::io::BufRead;
use std::io::BufReader;
use std::path::Path;
use std::process::ExitStatus;
use std::process::{Command, Output, Stdio};
use std::sync::{Arc, Mutex};
use std::thread;

#[cfg(target_os = "linux")]
use std::os::unix::process::ExitStatusExt;

#[cfg(target_os = "windows")]
use std::os::windows::process::ExitStatusExt;

#[cfg(feature = "logging")]
use log::{error, info, warn};

// Leech output from stdout/stderr while also storing the resulting output
macro_rules! leech_output {
    ($out:ident, $out_buf:ident, $log_method:ident) => {
        thread::spawn({
            let output_buffer_clone = Arc::clone($out_buf);
            move || {
                if let Some(output) = $out {
                    let reader = BufReader::new(output);
                    for line in reader.lines() {
                        if let Ok(line) = line {
                            #[cfg(feature = "logging")]
                            $log_method!("{}", line);
                            match output_buffer_clone.lock() {
                                Err(_err) => {
                                    #[cfg(feature = "logging")]
                                    error!("Failed to lock {} buffer! {}", stringify!($out), _err);
                                    return;
                                }
                                Ok(mut vec) => {
                                    vec.push(line);
                                }
                            }
                        }
                    }
                }
            }
        })
    };
}

pub struct IShell {
    initial_dir: String,
    current_dir: Arc<Mutex<String>>,
}

impl IShell {
    pub fn new(initial_dir: Option<&str>) -> Self {
        let current_dir = env::current_dir().expect(
            "Failed to get current directory; it may not exist or you may not have permissions.",
        );

        let current_dir = current_dir
            .to_str()
            .expect("Current directory contains invalid UTF-8.")
            .to_string();

        let initial_dir = initial_dir.map_or_else(|| current_dir.clone(), |dir| dir.to_string());

        IShell {
            initial_dir: initial_dir.clone(),
            current_dir: Arc::new(Mutex::new(initial_dir)),
        }
    }

    pub fn run_command(&self, command: &str) -> Output {
        #[cfg(feature = "logging")]
        info!("Running: `{}`", command);

        if command.starts_with("cd") {
            let new_dir = command[2..].trim();
            let mut current_dir = self.current_dir.lock().unwrap();

            // First check if the cd was intended to change to a relative dir
            // i.e. `cd test` is called at `/home/user/Desktop/`,
            // and there is an actual dir named `test` there.
            let wanted_dir = Path::new(current_dir.as_str()).join(new_dir);

            let new_dir = if wanted_dir.exists() && wanted_dir.is_dir() {
                Path::new(&wanted_dir)
            } else {
                // Maybe the specified path wasn't relative?
                Path::new(new_dir)
            };

            if let Err(e) = env::set_current_dir(new_dir) {
                #[cfg(feature = "logging")]
                {
                    error!(
                        "Failed to change directory to either of {:?} or \"{}/{}\": {}",
                        wanted_dir,
                        current_dir,
                        new_dir.to_str().unwrap_or(""),
                        e
                    );
                    error!("Current directory: '{}'", current_dir);
                }

                return self.create_output(
                    ExitStatus::from_raw(1),
                    Vec::new(),
                    Vec::from(format!("Error: {}", e)),
                );
            } else {
                let new_current_dir = env::current_dir()
                    .map(|path| path.to_string_lossy().into_owned())
                    .unwrap_or_else(|_err| {
                        #[cfg(feature = "logging")]
                        error!("Failed to get current directory: {}", _err);
                        current_dir.clone() // Keep the old dir if getting the new one fails
                    });

                *current_dir = new_current_dir;
            }
            return self.create_output(ExitStatus::from_raw(0), Vec::new(), Vec::new());
        }

        let child_process = self.spawn_process(command);
        match child_process {
            Ok(mut process) => {
                let (stdout_buffer, stderr_buffer) = (
                    Arc::new(Mutex::new(Vec::new())),
                    Arc::new(Mutex::new(Vec::new())),
                );

                let (stdout_handle, stderr_handle) = self.spawn_output_threads(
                    process.stdout.take(),
                    process.stderr.take(),
                    &stdout_buffer,
                    &stderr_buffer,
                );

                let status = match process.wait() {
                    Ok(status) => status,
                    Err(_err) => {
                        #[cfg(feature = "logging")]
                        error!("Failed to wait for process: {}", _err);
                        ExitStatus::default()
                    }
                };

                if let Err(_err) = stdout_handle.join() {
                    #[cfg(feature = "logging")]
                    error!("Failed to join stdout thread: {:?}", _err);
                }
                if let Err(_err) = stderr_handle.join() {
                    #[cfg(feature = "logging")]
                    error!("Failed to join stderr thread: {:?}", _err);
                }

                let stdout = self.collect_output(&stdout_buffer);
                let stderr = self.collect_output(&stderr_buffer);

                Output {
                    status,
                    stdout,
                    stderr,
                }
            }
            Err(e) => {
                #[cfg(feature = "logging")]
                error!("Couldn't spawn child process! {}", e);

                self.create_output(
                    ExitStatus::from_raw(-1),
                    Vec::new(),
                    Vec::from(format!("Error: {}", e)),
                )
            }
        }
    }

    pub fn forget_current_directory(&self) {
        let mut current_dir = self.current_dir.lock().unwrap();
        *current_dir = self.initial_dir.clone();
    }

    fn create_output(&self, status: ExitStatus, stdout: Vec<u8>, stderr: Vec<u8>) -> Output {
        Output {
            status,
            stdout,
            stderr,
        }
    }

    fn spawn_process(&self, command: &str) -> std::io::Result<std::process::Child> {
        let current_dir = self.current_dir.lock().unwrap().clone();
        if cfg!(target_os = "windows") {
            Command::new("cmd")
                .args(["/C", command])
                .current_dir(current_dir)
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .spawn()
        } else {
            Command::new("sh")
                .arg("-c")
                .arg(command)
                .current_dir(current_dir)
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .spawn()
        }
    }

    fn spawn_output_threads(
        &self,
        stdout: Option<std::process::ChildStdout>,
        stderr: Option<std::process::ChildStderr>,
        stdout_buffer: &Arc<Mutex<Vec<String>>>,
        stderr_buffer: &Arc<Mutex<Vec<String>>>,
    ) -> (thread::JoinHandle<()>, thread::JoinHandle<()>) {
        let stdout_handle = leech_output!(stdout, stdout_buffer, info);
        let stderr_handle = leech_output!(stderr, stderr_buffer, warn);

        (stdout_handle, stderr_handle)
    }

    fn collect_output(&self, buffer: &Arc<Mutex<Vec<String>>>) -> Vec<u8> {
        match buffer.lock() {
            Ok(buffer) => buffer.join("\n").into_bytes(),
            Err(_err) => {
                #[cfg(feature = "logging")]
                error!("Couldn't lock buffer! {}", _err);
                // Need to return SOMETHING here.
                Vec::new()
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn true_command() {
        let shell = IShell::new(None);

        let result = shell.run_command("true");
        assert_eq!(result.status.code().unwrap_or(1), 0);
    }

    #[test]
    fn false_command() {
        let shell = IShell::new(None);

        let result = shell.run_command("false");
        assert_eq!(result.status.code().unwrap_or(0), 1);
    }

    #[test]
    fn echo_command() {
        // Checking stdout capture
        let shell = IShell::new(None);

        let result = shell.run_command("echo \"Hello, World!\"");
        let stdout_res = unsafe { String::from_utf8_unchecked(result.stdout) };
        assert_eq!(stdout_res, "Hello, World!");
    }

    #[test]
    fn dir_memory() {
        // Check for whether CD is remembered

        // This test sometimes fails with "assertion `left == right` failed",
        // And sometimes it doesn't..
        let shell = IShell::new(None);

        let unique_dir_1 = format!("test_{}", rand::random::<u32>());
        let unique_dir_2 = format!("test2_{}", rand::random::<u32>());

        shell.run_command(&format!("mkdir {}", unique_dir_1));
        shell.run_command(&format!("cd {}", unique_dir_1));
        shell.run_command(&format!("mkdir {}", unique_dir_2));

        let result = shell.run_command("ls");
        let stdout_res = unsafe { String::from_utf8_unchecked(result.stdout) };
        assert_eq!(stdout_res.trim(), unique_dir_2);

        shell.run_command("cd ..");
        shell.run_command(&format!("rm -r {}", unique_dir_1));
    }

    #[test]
    fn forget_current_dir() {
        let shell = IShell::new(None);

        let result = shell.run_command("echo $PWD");
        let pwd = unsafe { String::from_utf8_unchecked(result.stdout) };

        let unique_dir = format!("test_{}", rand::random::<u32>());

        shell.run_command(&format!("mkdir {}", unique_dir));
        shell.run_command(&format!("cd {}", unique_dir));
        shell.forget_current_directory();

        let result = shell.run_command("echo $PWD");
        let forgotten_pwd = unsafe { String::from_utf8_unchecked(result.stdout) };

        assert_eq!(pwd, forgotten_pwd);

        shell.run_command(&format!("rm -r {}", unique_dir));
    }

    #[test]
    fn dir_doesnt_exist() {
        let shell = IShell::new(None);

        let current_dir = shell.current_dir.lock().unwrap().clone();
        shell.run_command("cd directory_that_doesnt_exist");
        let next_dir = shell.current_dir.lock().unwrap().clone();

        assert_eq!(current_dir, next_dir);
    }
}