Skip to content

Commit

Permalink
Merge #1127
Browse files Browse the repository at this point in the history
1127: unistd: getcwd: Double the buffer when need, up to PATH_MAX as limit r=asomers a=otavio

We now have a `reserve_double_buffer_size` method which reserves the
double of buffer, up to a limit, allowing it to be reused on other
methods in future.

Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>

Co-authored-by: Otavio Salvador <otavio@ossystems.com.br>
  • Loading branch information
bors[bot] and otavio authored Sep 22, 2019
2 parents ac5de6b + 035c69f commit 2fc246c
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {Error, Result, NixPath};
use fcntl::{AtFlags, at_rawfd, fcntl, FdFlag, OFlag};
use fcntl::FcntlArg::F_SETFD;
use libc::{self, c_char, c_void, c_int, c_long, c_uint, size_t, pid_t, off_t,
uid_t, gid_t, mode_t};
uid_t, gid_t, mode_t, PATH_MAX};
use std::{fmt, mem, ptr};
use std::ffi::{CString, CStr, OsString, OsStr};
use std::os::unix::ffi::{OsStringExt, OsStrExt};
Expand Down Expand Up @@ -534,6 +534,21 @@ pub fn symlinkat<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(
Errno::result(res).map(drop)
}

// Double the buffer capacity up to limit. In case it already has
// reached the limit, return Errno::ERANGE.
fn reserve_double_buffer_size<T>(buf: &mut Vec<T>, limit: usize) -> Result<()> {
use std::cmp::min;

if buf.len() >= limit {
return Err(Error::Sys(Errno::ERANGE))
}

let capacity = min(buf.capacity() * 2, limit);
buf.reserve(capacity);

Ok(())
}

/// Returns the current directory as a `PathBuf`
///
/// Err is returned if the current user doesn't have the permission to read or search a component
Expand Down Expand Up @@ -576,11 +591,8 @@ pub fn getcwd() -> Result<PathBuf> {
}
}

// Trigger the internal buffer resizing logic of `Vec` by requiring
// more space than the current capacity.
let cap = buf.capacity();
buf.set_len(cap);
buf.reserve(1);
// Trigger the internal buffer resizing logic.
reserve_double_buffer_size(&mut buf, PATH_MAX as usize)?;
}
}
}
Expand Down

0 comments on commit 2fc246c

Please sign in to comment.