rustix/process/chdir.rs
1#[cfg(not(target_os = "fuchsia"))]
2use crate::backend::fd::AsFd;
3#[cfg(feature = "fs")]
4use crate::path;
5#[cfg(any(feature = "fs", not(target_os = "fuchsia")))]
6use crate::{backend, io};
7#[cfg(all(feature = "alloc", feature = "fs"))]
8use {
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")))]
25pub fn chdir<P: path::Arg>(path: P) -> io::Result<()> {
26 path.into_with_c_str(backend::process::syscalls::chdir)
27}
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]
39pub fn fchdir<Fd: AsFd>(fd: Fd) -> io::Result<()> {
40 backend::process::syscalls::fchdir(fd.as_fd())
41}
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]
58pub fn getcwd<B: Into<Vec<u8>>>(reuse: B) -> io::Result<CString> {
59 _getcwd(reuse.into())
60}
61
62#[cfg(all(feature = "alloc", feature = "fs"))]
63#[allow(unsafe_code)]
64fn _getcwd(mut buffer: Vec<u8>) -> io::Result<CString> {
65 buffer.clear();
66 buffer.reserve(SMALL_PATH_BUFFER_SIZE);
67
68 loop {
69 match backend::process::syscalls::getcwd(buffer.spare_capacity_mut()) {
70 Err(io::Errno::RANGE) => {
71 // Use `Vec` reallocation strategy to grow capacity
72 // exponentially.
73 buffer.reserve(buffer.capacity() + 1);
74 }
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 buffer.set_len(
87 CStr::from_ptr(buffer.as_ptr().cast())
88 .to_bytes_with_nul()
89 .len(),
90 );
91
92 return Ok(CString::from_vec_with_nul_unchecked(buffer));
93 }
94 }
95 Err(errno) => return Err(errno),
96 }
97 }
98}