summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/src/process/chdir.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/rustix/src/process/chdir.rs')
-rw-r--r--vendor/rustix/src/process/chdir.rs52
1 files changed, 36 insertions, 16 deletions
diff --git a/vendor/rustix/src/process/chdir.rs b/vendor/rustix/src/process/chdir.rs
index 4951e5670..0da0b5c2e 100644
--- a/vendor/rustix/src/process/chdir.rs
+++ b/vendor/rustix/src/process/chdir.rs
@@ -1,9 +1,13 @@
-use crate::ffi::CString;
-use crate::path::SMALL_PATH_BUFFER_SIZE;
-use crate::{backend, io, path};
-use alloc::vec::Vec;
#[cfg(not(target_os = "fuchsia"))]
-use backend::fd::AsFd;
+use crate::backend::fd::AsFd;
+#[cfg(any(feature = "fs", not(target_os = "fuchsia")))]
+use crate::{backend, io};
+#[cfg(feature = "fs")]
+use {
+ crate::ffi::{CStr, CString},
+ crate::path::{self, SMALL_PATH_BUFFER_SIZE},
+ alloc::vec::Vec,
+};
/// `chdir(path)`—Change the current working directory.
///
@@ -14,6 +18,8 @@ use backend::fd::AsFd;
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/chdir.html
/// [Linux]: https://man7.org/linux/man-pages/man2/chdir.2.html
#[inline]
+#[cfg(feature = "fs")]
+#[cfg_attr(doc_cfg, doc(cfg(feature = "fs")))]
pub fn chdir<P: path::Arg>(path: P) -> io::Result<()> {
path.into_with_c_str(backend::process::syscalls::chdir)
}
@@ -32,9 +38,9 @@ pub fn fchdir<Fd: AsFd>(fd: Fd) -> io::Result<()> {
backend::process::syscalls::fchdir(fd.as_fd())
}
-/// `getcwd()`—Return the current working directory.
+/// `getCWD`—Return the current working directory.
///
-/// If `reuse` is non-empty, reuse its buffer to store the result if possible.
+/// If `reuse` already has available capacity, reuse it if possible.
///
/// # References
/// - [POSIX]
@@ -42,29 +48,43 @@ pub fn fchdir<Fd: AsFd>(fd: Fd) -> io::Result<()> {
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html
/// [Linux]: https://man7.org/linux/man-pages/man3/getcwd.3.html
+#[cfg(feature = "fs")]
#[cfg(not(target_os = "wasi"))]
+#[cfg_attr(doc_cfg, doc(cfg(feature = "fs")))]
#[inline]
pub fn getcwd<B: Into<Vec<u8>>>(reuse: B) -> io::Result<CString> {
_getcwd(reuse.into())
}
+#[cfg(feature = "fs")]
+#[allow(unsafe_code)]
fn _getcwd(mut buffer: Vec<u8>) -> io::Result<CString> {
- // This code would benefit from having a better way to read into
- // uninitialized memory, but that requires `unsafe`.
buffer.clear();
buffer.reserve(SMALL_PATH_BUFFER_SIZE);
- buffer.resize(buffer.capacity(), 0_u8);
loop {
- match backend::process::syscalls::getcwd(&mut buffer) {
+ match backend::process::syscalls::getcwd(buffer.spare_capacity_mut()) {
Err(io::Errno::RANGE) => {
- buffer.reserve(1); // use `Vec` reallocation strategy to grow capacity exponentially
- buffer.resize(buffer.capacity(), 0_u8);
+ buffer.reserve(buffer.capacity() + 1); // use `Vec` reallocation strategy to grow capacity exponentially
}
Ok(_) => {
- let len = buffer.iter().position(|x| *x == b'\0').unwrap();
- buffer.resize(len, 0_u8);
- return Ok(CString::new(buffer).unwrap());
+ // SAFETY:
+ // - "These functions return a null-terminated string"
+ // - [POSIX definition 3.375: String]: "A contiguous sequence of bytes
+ // terminated by and including the first null byte."
+ //
+ // Thus, there will be a single NUL byte at the end of the string.
+ //
+ // [POSIX definition 3.375: String]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_375
+ unsafe {
+ buffer.set_len(
+ CStr::from_ptr(buffer.as_ptr().cast())
+ .to_bytes_with_nul()
+ .len(),
+ );
+
+ return Ok(CString::from_vec_with_nul_unchecked(buffer));
+ }
}
Err(errno) => return Err(errno),
}