Disambiguate symlink argument names

Mirrors https://github.com/rust-lang/rust/pull/79060.
This commit is contained in:
David Tolnay 2020-11-14 15:16:53 -08:00
parent 2757ca805c
commit 36731f8f53
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
4 changed files with 41 additions and 38 deletions

View File

@ -28,19 +28,19 @@ pub(crate) fn write(path: impl AsRef<Path>, content: &[u8]) -> Result<()> {
}
}
pub(crate) fn symlink_file(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
let src = src.as_ref();
let dst = dst.as_ref();
pub(crate) fn symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
let original = original.as_ref();
let link = link.as_ref();
let mut create_dir_error = None;
if dst.exists() {
let _ = fs::remove_file(dst).unwrap();
if link.exists() {
let _ = fs::remove_file(link).unwrap();
} else {
let parent = dst.parent().unwrap();
let parent = link.parent().unwrap();
create_dir_error = fs::create_dir_all(parent).err();
}
match paths::symlink_or_copy(src, dst) {
match paths::symlink_or_copy(original, link) {
// As long as symlink_or_copy succeeded, ignore any create_dir_all error.
Ok(()) => Ok(()),
// If create_dir_all and symlink_or_copy both failed, prefer the first error.
@ -48,19 +48,19 @@ pub(crate) fn symlink_file(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Resu
}
}
pub(crate) fn symlink_dir(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
let src = src.as_ref();
let dst = dst.as_ref();
pub(crate) fn symlink_dir(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
let original = original.as_ref();
let link = link.as_ref();
let mut create_dir_error = None;
if dst.exists() {
let _ = paths::remove_symlink_dir(dst).unwrap();
if link.exists() {
let _ = paths::remove_symlink_dir(link).unwrap();
} else {
let parent = dst.parent().unwrap();
let parent = link.parent().unwrap();
create_dir_error = fs::create_dir_all(parent).err();
}
match paths::symlink_dir(src, dst) {
match paths::symlink_dir(original, link) {
// As long as symlink_dir succeeded, ignore any create_dir_all error.
Ok(()) => Ok(()),
// If create_dir_all and symlink_dir both failed, prefer the first error.

View File

@ -43,13 +43,16 @@ impl PathExt for Path {
pub(crate) use self::fs::symlink_file as symlink_or_copy;
#[cfg(windows)]
pub(crate) fn symlink_or_copy(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> fs::Result<()> {
pub(crate) fn symlink_or_copy(
original: impl AsRef<Path>,
link: impl AsRef<Path>,
) -> fs::Result<()> {
// Pre-Windows 10, symlinks require admin privileges. Since Windows 10, they
// require Developer Mode. If it fails, fall back to copying the file.
let src = src.as_ref();
let dst = dst.as_ref();
if fs::symlink_file(src, dst).is_err() {
fs::copy(src, dst)?;
let original = original.as_ref();
let link = link.as_ref();
if fs::symlink_file(original, link).is_err() {
fs::copy(original, link)?;
}
Ok(())
}
@ -61,7 +64,7 @@ pub(crate) use self::fs::copy as symlink_or_copy;
pub(crate) use self::fs::symlink_dir;
#[cfg(not(any(unix, windows)))]
pub(crate) fn symlink_dir(_src: impl AsRef<Path>, _dst: impl AsRef<Path>) -> fs::Result<()> {
pub(crate) fn symlink_dir(_original: impl AsRef<Path>, _link: impl AsRef<Path>) -> fs::Result<()> {
Ok(())
}

View File

@ -91,17 +91,17 @@ pub(crate) fn remove_dir(path: impl AsRef<Path>) -> Result<()> {
}
fn symlink<'a>(
src: &'a Path,
dst: &'a Path,
original: &'a Path,
link: &'a Path,
fun: fn(&'a Path, &'a Path) -> io::Result<()>,
) -> Result<()> {
match fun(src, dst) {
match fun(original, link) {
Ok(()) => Ok(()),
Err(e) => err!(
e,
"Failed to create symlink `{}` pointing to `{}`",
dst,
src,
link,
original,
),
}
}
@ -111,24 +111,24 @@ fn symlink<'a>(
pub(crate) use self::symlink_file as symlink_dir;
#[cfg(unix)]
pub(crate) fn symlink_file(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
symlink(src.as_ref(), dst.as_ref(), std::os::unix::fs::symlink)
pub(crate) fn symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
symlink(original.as_ref(), link.as_ref(), std::os::unix::fs::symlink)
}
#[cfg(windows)]
pub(crate) fn symlink_file(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
pub(crate) fn symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
symlink(
src.as_ref(),
dst.as_ref(),
original.as_ref(),
link.as_ref(),
std::os::windows::fs::symlink_file,
)
}
#[cfg(windows)]
pub(crate) fn symlink_dir(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
pub(crate) fn symlink_dir(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
symlink(
src.as_ref(),
dst.as_ref(),
original.as_ref(),
link.as_ref(),
std::os::windows::fs::symlink_dir,
)
}

View File

@ -58,11 +58,11 @@ fn main() {
#[cfg(windows)]
if let Some(out_dir) = env::var_os("OUT_DIR") {
let parent_dir = Path::new(&out_dir).join("symlink");
let from_dir = parent_dir.join("from");
let to_dir = parent_dir.join("to");
if fs::create_dir_all(&from_dir).is_ok()
&& (!to_dir.exists() || fs::remove_dir(&to_dir).is_ok())
&& windows::symlink_dir(&from_dir, &to_dir).is_err()
let original_dir = parent_dir.join("original");
let link_dir = parent_dir.join("link");
if fs::create_dir_all(&original_dir).is_ok()
&& (!link_dir.exists() || fs::remove_dir(&link_dir).is_ok())
&& windows::symlink_dir(&original_dir, &link_dir).is_err()
{
message = DENIED;
}