shell_rs/
rm.rs

1// Copyright (c) 2021 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
2// Use of this source is governed by Apache-2.0 License that can be found
3// in the LICENSE file.
4
5use glob::glob;
6use nc::Errno;
7use std::path::PathBuf;
8use std::result::Result;
9
10use crate::core::env::expand_env;
11
12pub struct RmOptions {
13    /// Remove directories and their contenst recursively.
14    pub recursive: bool,
15
16    /// Treat `/` specially.
17    pub preserve_root: bool,
18
19    /// When removing a hierarchy recursively, skip any directory that is on a file system
20    /// different from that of the corresponding argument.
21    pub one_file_system: bool,
22
23    /// Remove empty directories.
24    pub empty_dir: bool,
25
26    /// Expand environment variables in path with `expand_env()`.
27    pub expand_env: bool,
28
29    /// Match shell style patterns in path.
30    pub use_glob: bool,
31}
32
33impl RmOptions {
34    pub fn new() -> Self {
35        RmOptions::default()
36    }
37}
38
39impl Default for RmOptions {
40    fn default() -> Self {
41        RmOptions {
42            recursive: false,
43            preserve_root: true,
44            one_file_system: false,
45            empty_dir: false,
46            expand_env: true,
47            use_glob: true,
48        }
49    }
50}
51
52pub fn rm<T: AsRef<str>>(path: T, options: &RmOptions) -> Result<(), Errno> {
53    let path = if options.expand_env {
54        expand_env(path)
55    } else {
56        path.as_ref().to_string()
57    };
58
59    let _path_list: Vec<PathBuf> = if options.use_glob {
60        glob(&path).unwrap().filter_map(Result::ok).collect()
61    } else {
62        vec![PathBuf::from(path)]
63    };
64
65    return Ok(());
66}
67
68fn do_rm() {}