shell_rs/
mv.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 crate::core::env::expand_env;
6use crate::error::Error;
7
8#[derive(Debug, Default)]
9pub struct Options {
10    ///  Overwrite existing files.
11    pub force: bool,
12
13    /// Remove any trailing slashes from `src` argument.
14    pub strip_trailing_slashes: bool,
15
16    /// Treat `dest` as a normal file.
17    pub no_target_directory: bool,
18
19    /// Move only when the `src` file is newer than the destination file
20    /// or when the destination file is missing.
21    pub update: bool,
22}
23
24/// Rename `src` to `dest`, or move `src` to `dest` directory.
25pub fn mv(src: &str, dest: &str, options: &Options) -> Result<usize, Error> {
26    let src = expand_env(src);
27    let dest = expand_env(dest);
28
29    if options.no_target_directory {
30        unsafe { nc::renameat(nc::AT_FDCWD, src, nc::AT_FDCWD, dest)? };
31        return Ok(1);
32    }
33
34    Ok(0)
35}