1b8a62b91Sopenharmony_ciuse crate::ffi::CString; 2b8a62b91Sopenharmony_ciuse crate::path::SMALL_PATH_BUFFER_SIZE; 3b8a62b91Sopenharmony_ciuse crate::{backend, io, path}; 4b8a62b91Sopenharmony_ciuse alloc::vec::Vec; 5b8a62b91Sopenharmony_ci#[cfg(not(target_os = "fuchsia"))] 6b8a62b91Sopenharmony_ciuse backend::fd::AsFd; 7b8a62b91Sopenharmony_ci 8b8a62b91Sopenharmony_ci/// `chdir(path)`—Change the current working directory. 9b8a62b91Sopenharmony_ci/// 10b8a62b91Sopenharmony_ci/// # References 11b8a62b91Sopenharmony_ci/// - [POSIX] 12b8a62b91Sopenharmony_ci/// - [Linux] 13b8a62b91Sopenharmony_ci/// 14b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/chdir.html 15b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man2/chdir.2.html 16b8a62b91Sopenharmony_ci#[inline] 17b8a62b91Sopenharmony_cipub fn chdir<P: path::Arg>(path: P) -> io::Result<()> { 18b8a62b91Sopenharmony_ci path.into_with_c_str(backend::process::syscalls::chdir) 19b8a62b91Sopenharmony_ci} 20b8a62b91Sopenharmony_ci 21b8a62b91Sopenharmony_ci/// `fchdir(fd)`—Change the current working directory. 22b8a62b91Sopenharmony_ci/// 23b8a62b91Sopenharmony_ci/// # References 24b8a62b91Sopenharmony_ci/// - [POSIX] 25b8a62b91Sopenharmony_ci/// - [Linux] 26b8a62b91Sopenharmony_ci/// 27b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchdir.html 28b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man2/fchdir.2.html 29b8a62b91Sopenharmony_ci#[cfg(not(target_os = "fuchsia"))] 30b8a62b91Sopenharmony_ci#[inline] 31b8a62b91Sopenharmony_cipub fn fchdir<Fd: AsFd>(fd: Fd) -> io::Result<()> { 32b8a62b91Sopenharmony_ci backend::process::syscalls::fchdir(fd.as_fd()) 33b8a62b91Sopenharmony_ci} 34b8a62b91Sopenharmony_ci 35b8a62b91Sopenharmony_ci/// `getcwd()`—Return the current working directory. 36b8a62b91Sopenharmony_ci/// 37b8a62b91Sopenharmony_ci/// If `reuse` is non-empty, reuse its buffer to store the result if possible. 38b8a62b91Sopenharmony_ci/// 39b8a62b91Sopenharmony_ci/// # References 40b8a62b91Sopenharmony_ci/// - [POSIX] 41b8a62b91Sopenharmony_ci/// - [Linux] 42b8a62b91Sopenharmony_ci/// 43b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html 44b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man3/getcwd.3.html 45b8a62b91Sopenharmony_ci#[cfg(not(target_os = "wasi"))] 46b8a62b91Sopenharmony_ci#[inline] 47b8a62b91Sopenharmony_cipub fn getcwd<B: Into<Vec<u8>>>(reuse: B) -> io::Result<CString> { 48b8a62b91Sopenharmony_ci _getcwd(reuse.into()) 49b8a62b91Sopenharmony_ci} 50b8a62b91Sopenharmony_ci 51b8a62b91Sopenharmony_cifn _getcwd(mut buffer: Vec<u8>) -> io::Result<CString> { 52b8a62b91Sopenharmony_ci // This code would benefit from having a better way to read into 53b8a62b91Sopenharmony_ci // uninitialized memory, but that requires `unsafe`. 54b8a62b91Sopenharmony_ci buffer.clear(); 55b8a62b91Sopenharmony_ci buffer.reserve(SMALL_PATH_BUFFER_SIZE); 56b8a62b91Sopenharmony_ci buffer.resize(buffer.capacity(), 0_u8); 57b8a62b91Sopenharmony_ci 58b8a62b91Sopenharmony_ci loop { 59b8a62b91Sopenharmony_ci match backend::process::syscalls::getcwd(&mut buffer) { 60b8a62b91Sopenharmony_ci Err(io::Errno::RANGE) => { 61b8a62b91Sopenharmony_ci buffer.reserve(1); // use `Vec` reallocation strategy to grow capacity exponentially 62b8a62b91Sopenharmony_ci buffer.resize(buffer.capacity(), 0_u8); 63b8a62b91Sopenharmony_ci } 64b8a62b91Sopenharmony_ci Ok(_) => { 65b8a62b91Sopenharmony_ci let len = buffer.iter().position(|x| *x == b'\0').unwrap(); 66b8a62b91Sopenharmony_ci buffer.resize(len, 0_u8); 67b8a62b91Sopenharmony_ci return Ok(CString::new(buffer).unwrap()); 68b8a62b91Sopenharmony_ci } 69b8a62b91Sopenharmony_ci Err(errno) => return Err(errno), 70b8a62b91Sopenharmony_ci } 71b8a62b91Sopenharmony_ci } 72b8a62b91Sopenharmony_ci} 73