Coverage Report

Created: 2026-05-30 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rustix-1.1.4/src/process/chdir.rs
Line
Count
Source
1
#[cfg(not(target_os = "fuchsia"))]
2
use crate::backend::fd::AsFd;
3
#[cfg(feature = "fs")]
4
use crate::path;
5
#[cfg(any(feature = "fs", not(target_os = "fuchsia")))]
6
use crate::{backend, io};
7
#[cfg(all(feature = "alloc", feature = "fs"))]
8
use {
9
    crate::ffi::{CStr, CString},
10
    crate::path::SMALL_PATH_BUFFER_SIZE,
11
    alloc::vec::Vec,
12
};
13
14
/// `chdir(path)`—Change the current working directory.
15
///
16
/// # References
17
///  - [POSIX]
18
///  - [Linux]
19
///
20
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/chdir.html
21
/// [Linux]: https://man7.org/linux/man-pages/man2/chdir.2.html
22
#[inline]
23
#[cfg(feature = "fs")]
24
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
25
0
pub fn chdir<P: path::Arg>(path: P) -> io::Result<()> {
26
0
    path.into_with_c_str(backend::process::syscalls::chdir)
27
0
}
28
29
/// `fchdir(fd)`—Change the current working directory.
30
///
31
/// # References
32
///  - [POSIX]
33
///  - [Linux]
34
///
35
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/fchdir.html
36
/// [Linux]: https://man7.org/linux/man-pages/man2/fchdir.2.html
37
#[cfg(not(target_os = "fuchsia"))]
38
#[inline]
39
0
pub fn fchdir<Fd: AsFd>(fd: Fd) -> io::Result<()> {
40
0
    backend::process::syscalls::fchdir(fd.as_fd())
41
0
}
42
43
/// `getcwd`—Return the current working directory.
44
///
45
/// If `reuse` already has available capacity, reuse it if possible.
46
///
47
/// # References
48
///  - [POSIX]
49
///  - [Linux]
50
///
51
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getcwd.html
52
/// [Linux]: https://man7.org/linux/man-pages/man3/getcwd.3.html
53
#[cfg(all(feature = "alloc", feature = "fs"))]
54
#[cfg(not(target_os = "wasi"))]
55
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
56
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
57
#[inline]
58
0
pub fn getcwd<B: Into<Vec<u8>>>(reuse: B) -> io::Result<CString> {
59
0
    _getcwd(reuse.into())
60
0
}
61
62
#[cfg(all(feature = "alloc", feature = "fs"))]
63
#[allow(unsafe_code)]
64
0
fn _getcwd(mut buffer: Vec<u8>) -> io::Result<CString> {
65
0
    buffer.clear();
66
0
    buffer.reserve(SMALL_PATH_BUFFER_SIZE);
67
68
    loop {
69
0
        match backend::process::syscalls::getcwd(buffer.spare_capacity_mut()) {
70
0
            Err(io::Errno::RANGE) => {
71
0
                // Use `Vec` reallocation strategy to grow capacity
72
0
                // exponentially.
73
0
                buffer.reserve(buffer.capacity() + 1);
74
0
            }
75
            Ok(_) => {
76
                // SAFETY:
77
                // - “These functions return a null-terminated string”
78
                // - [POSIX definition 3.375: String]: “A contiguous sequence
79
                //   of bytes terminated by and including the first null byte.”
80
                //
81
                // Thus, there will be a single NUL byte at the end of the
82
                // string.
83
                //
84
                // [POSIX definition 3.375: String]: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_375
85
                unsafe {
86
0
                    buffer.set_len(
87
0
                        CStr::from_ptr(buffer.as_ptr().cast())
88
0
                            .to_bytes_with_nul()
89
0
                            .len(),
90
                    );
91
92
0
                    return Ok(CString::from_vec_with_nul_unchecked(buffer));
93
                }
94
            }
95
0
            Err(errno) => return Err(errno),
96
        }
97
    }
98
0
}