cap_primitives/fs/via_parent/
rename.rs

1use super::open_parent;
2#[cfg(unix)]
3use crate::fs::{append_dir_suffix, path_has_trailing_slash};
4use crate::fs::{rename_unchecked, strip_dir_suffix, MaybeOwnedFile};
5use std::path::Path;
6use std::{fs, io};
7
8/// Implement `rename` by `open`ing up the parent component of the path and
9/// then calling `rename_unchecked` on the last component.
10pub(crate) fn rename(
11    old_start: &fs::File,
12    old_path: &Path,
13    new_start: &fs::File,
14    new_path: &Path,
15) -> io::Result<()> {
16    let old_start = MaybeOwnedFile::borrowed(old_start);
17    let new_start = MaybeOwnedFile::borrowed(new_start);
18
19    // As a special case, `rename` ignores a trailing slash rather than treating
20    // it as equivalent to a trailing slash-dot, so strip any trailing slashes
21    // for the purposes of `open_parent`.
22    //
23    // And on Unix, remember whether the source started with a slash so that we
24    // can still fail if it is and the source is a regular file.
25    #[cfg(unix)]
26    let old_starts_with_slash = path_has_trailing_slash(old_path);
27    let old_path = strip_dir_suffix(old_path);
28    let new_path = strip_dir_suffix(new_path);
29
30    let (old_dir, old_basename) = open_parent(old_start, &old_path)?;
31    let (new_dir, new_basename) = open_parent(new_start, &new_path)?;
32
33    // On Unix, re-append a slash if needed.
34    #[cfg(unix)]
35    let concat;
36    #[cfg(unix)]
37    let old_basename = if old_starts_with_slash {
38        concat = append_dir_suffix(old_basename.to_owned().into());
39        concat.as_os_str()
40    } else {
41        old_basename
42    };
43
44    rename_unchecked(
45        &old_dir,
46        old_basename.as_ref(),
47        &new_dir,
48        new_basename.as_ref(),
49    )
50}