Skip to content

Commit 09c9c9f

Browse files
committed
Auto merge of #79060 - dtolnay:symlinkarg, r=Mark-Simulacrum
Disambiguate symlink argument names The current argument naming in the following standard library functions is horribly ambiguous. - std::os::unix::fs::symlink: https://doc.rust-lang.org/1.47.0/std/os/unix/fs/fn.symlink.html - std::os::windows::fs::symlink_file: https://doc.rust-lang.org/1.47.0/std/os/windows/fs/fn.symlink_file.html - std::os::windows::fs::symlink_dir: https://doc.rust-lang.org/1.47.0/std/os/windows/fs/fn.symlink_dir.html **Notice that Swift uses one of the same names we do (`dst`) to refer to the opposite thing.** <br> | | the&nbsp;one&nbsp;that&nbsp;exists | the&nbsp;one&nbsp;that&nbsp;is<br>being&nbsp;created | reference | | --- | --- | --- | --- | | Rust | `src` | `dst` | | | Swift | `withDestinationPath`<br>`destPath` | `atPath`<br>`path` | <sub>https://developer.apple.com/documentation/foundation/filemanager/1411007-createsymboliclink</sub> | | D | `original` | `link` | <sub>https://dlang.org/library/std/file/symlink.html</sub> | | Go | `oldname` | `newname` | <sub>https://golang.org/pkg/os/#Symlink</sub> | | C++| `target` | `link` | <sub>https://en.cppreference.com/w/cpp/filesystem/create_symlink</sub> | | POSIX | `path1` | `path2` | <sub>https://pubs.opengroup.org/onlinepubs/9699919799/functions/symlink.html</sub> | | Linux | `target` | `linkpath` | <sub>https://man7.org/linux/man-pages/man2/symlink.2.html</sub> | Out of these I happen to like D's argument names and am proposing that we adopt them.
2 parents fe98231 + 29128a5 commit 09c9c9f

File tree

9 files changed

+67
-60
lines changed

9 files changed

+67
-60
lines changed

library/std/src/fs.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -1698,12 +1698,14 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
16981698

16991699
/// Creates a new hard link on the filesystem.
17001700
///
1701-
/// The `dst` path will be a link pointing to the `src` path. Note that systems
1702-
/// often require these two paths to both be located on the same filesystem.
1701+
/// The `link` path will be a link pointing to the `original` path. Note that
1702+
/// systems often require these two paths to both be located on the same
1703+
/// filesystem.
17031704
///
1704-
/// If `src` names a symbolic link, it is platform-specific whether the symbolic
1705-
/// link is followed. On platforms where it's possible to not follow it, it is
1706-
/// not followed, and the created hard link points to the symbolic link itself.
1705+
/// If `original` names a symbolic link, it is platform-specific whether the
1706+
/// symbolic link is followed. On platforms where it's possible to not follow
1707+
/// it, it is not followed, and the created hard link points to the symbolic
1708+
/// link itself.
17071709
///
17081710
/// # Platform-specific behavior
17091711
///
@@ -1718,7 +1720,7 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
17181720
/// This function will return an error in the following situations, but is not
17191721
/// limited to just these cases:
17201722
///
1721-
/// * The `src` path is not a file or doesn't exist.
1723+
/// * The `original` path is not a file or doesn't exist.
17221724
///
17231725
/// # Examples
17241726
///
@@ -1731,13 +1733,13 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
17311733
/// }
17321734
/// ```
17331735
#[stable(feature = "rust1", since = "1.0.0")]
1734-
pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
1735-
fs_imp::link(src.as_ref(), dst.as_ref())
1736+
pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
1737+
fs_imp::link(original.as_ref(), link.as_ref())
17361738
}
17371739

17381740
/// Creates a new symbolic link on the filesystem.
17391741
///
1740-
/// The `dst` path will be a symbolic link pointing to the `src` path.
1742+
/// The `link` path will be a symbolic link pointing to the `original` path.
17411743
/// On Windows, this will be a file symlink, not a directory symlink;
17421744
/// for this reason, the platform-specific [`std::os::unix::fs::symlink`]
17431745
/// and [`std::os::windows::fs::symlink_file`] or [`symlink_dir`] should be
@@ -1763,8 +1765,8 @@ pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<(
17631765
reason = "replaced with std::os::unix::fs::symlink and \
17641766
std::os::windows::fs::{symlink_file, symlink_dir}"
17651767
)]
1766-
pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
1767-
fs_imp::symlink(src.as_ref(), dst.as_ref())
1768+
pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
1769+
fs_imp::symlink(original.as_ref(), link.as_ref())
17681770
}
17691771

17701772
/// Reads a symbolic link, returning the file that the link points to.

library/std/src/sys/cloudabi/shims/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ pub fn readlink(_p: &Path) -> io::Result<PathBuf> {
283283
unsupported()
284284
}
285285

286-
pub fn symlink(_src: &Path, _dst: &Path) -> io::Result<()> {
286+
pub fn symlink(_original: &Path, _link: &Path) -> io::Result<()> {
287287
unsupported()
288288
}
289289

library/std/src/sys/hermit/fs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -377,11 +377,11 @@ pub fn readlink(_p: &Path) -> io::Result<PathBuf> {
377377
unsupported()
378378
}
379379

380-
pub fn symlink(_src: &Path, _dst: &Path) -> io::Result<()> {
380+
pub fn symlink(_original: &Path, _link: &Path) -> io::Result<()> {
381381
unsupported()
382382
}
383383

384-
pub fn link(_src: &Path, _dst: &Path) -> io::Result<()> {
384+
pub fn link(_original: &Path, _link: &Path) -> io::Result<()> {
385385
unsupported()
386386
}
387387

library/std/src/sys/unix/ext/fs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ impl DirEntryExt for fs::DirEntry {
841841

842842
/// Creates a new symbolic link on the filesystem.
843843
///
844-
/// The `dst` path will be a symbolic link pointing to the `src` path.
844+
/// The `link` path will be a symbolic link pointing to the `original` path.
845845
///
846846
/// # Examples
847847
///
@@ -854,8 +854,8 @@ impl DirEntryExt for fs::DirEntry {
854854
/// }
855855
/// ```
856856
#[stable(feature = "symlink", since = "1.1.0")]
857-
pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
858-
sys::fs::symlink(src.as_ref(), dst.as_ref())
857+
pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
858+
sys::fs::symlink(original.as_ref(), link.as_ref())
859859
}
860860

861861
/// Unix-specific extensions to [`fs::DirBuilder`].

library/std/src/sys/unix/fs.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1071,28 +1071,28 @@ pub fn readlink(p: &Path) -> io::Result<PathBuf> {
10711071
}
10721072
}
10731073

1074-
pub fn symlink(src: &Path, dst: &Path) -> io::Result<()> {
1075-
let src = cstr(src)?;
1076-
let dst = cstr(dst)?;
1077-
cvt(unsafe { libc::symlink(src.as_ptr(), dst.as_ptr()) })?;
1074+
pub fn symlink(original: &Path, link: &Path) -> io::Result<()> {
1075+
let original = cstr(original)?;
1076+
let link = cstr(link)?;
1077+
cvt(unsafe { libc::symlink(original.as_ptr(), link.as_ptr()) })?;
10781078
Ok(())
10791079
}
10801080

1081-
pub fn link(src: &Path, dst: &Path) -> io::Result<()> {
1082-
let src = cstr(src)?;
1083-
let dst = cstr(dst)?;
1081+
pub fn link(original: &Path, link: &Path) -> io::Result<()> {
1082+
let original = cstr(original)?;
1083+
let link = cstr(link)?;
10841084
cfg_if::cfg_if! {
10851085
if #[cfg(any(target_os = "vxworks", target_os = "redox", target_os = "android"))] {
10861086
// VxWorks, Redox, and old versions of Android lack `linkat`, so use
10871087
// `link` instead. POSIX leaves it implementation-defined whether
10881088
// `link` follows symlinks, so rely on the `symlink_hard_link` test
10891089
// in library/std/src/fs/tests.rs to check the behavior.
1090-
cvt(unsafe { libc::link(src.as_ptr(), dst.as_ptr()) })?;
1090+
cvt(unsafe { libc::link(original.as_ptr(), link.as_ptr()) })?;
10911091
} else {
10921092
// Use `linkat` with `AT_FDCWD` instead of `link` as `linkat` gives
10931093
// us a flag to specify how symlinks should be handled. Pass 0 as
10941094
// the flags argument, meaning don't follow symlinks.
1095-
cvt(unsafe { libc::linkat(libc::AT_FDCWD, src.as_ptr(), libc::AT_FDCWD, dst.as_ptr(), 0) })?;
1095+
cvt(unsafe { libc::linkat(libc::AT_FDCWD, original.as_ptr(), libc::AT_FDCWD, link.as_ptr(), 0) })?;
10961096
}
10971097
}
10981098
Ok(())

library/std/src/sys/unsupported/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ pub fn readlink(_p: &Path) -> io::Result<PathBuf> {
279279
unsupported()
280280
}
281281

282-
pub fn symlink(_src: &Path, _dst: &Path) -> io::Result<()> {
282+
pub fn symlink(_original: &Path, _link: &Path) -> io::Result<()> {
283283
unsupported()
284284
}
285285

library/std/src/sys/wasi/fs.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -549,19 +549,19 @@ fn read_link(fd: &WasiFd, file: &Path) -> io::Result<PathBuf> {
549549
}
550550
}
551551

552-
pub fn symlink(src: &Path, dst: &Path) -> io::Result<()> {
553-
let (dst, dst_file) = open_parent(dst)?;
554-
dst.symlink(osstr2str(src.as_ref())?, osstr2str(dst_file.as_ref())?)
552+
pub fn symlink(original: &Path, link: &Path) -> io::Result<()> {
553+
let (link, link_file) = open_parent(link)?;
554+
link.symlink(osstr2str(original.as_ref())?, osstr2str(link_file.as_ref())?)
555555
}
556556

557-
pub fn link(src: &Path, dst: &Path) -> io::Result<()> {
558-
let (src, src_file) = open_parent(src)?;
559-
let (dst, dst_file) = open_parent(dst)?;
560-
src.link(
557+
pub fn link(original: &Path, link: &Path) -> io::Result<()> {
558+
let (original, original_file) = open_parent(original)?;
559+
let (link, link_file) = open_parent(link)?;
560+
original.link(
561561
wasi::LOOKUPFLAGS_SYMLINK_FOLLOW,
562-
osstr2str(src_file.as_ref())?,
563-
&dst,
564-
osstr2str(dst_file.as_ref())?,
562+
osstr2str(original_file.as_ref())?,
563+
&link,
564+
osstr2str(link_file.as_ref())?,
565565
)
566566
}
567567

library/std/src/sys/windows/ext/fs.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl FileTypeExt for fs::FileType {
519519

520520
/// Creates a new file symbolic link on the filesystem.
521521
///
522-
/// The `dst` path will be a file symbolic link pointing to the `src`
522+
/// The `link` path will be a file symbolic link pointing to the `original`
523523
/// path.
524524
///
525525
/// # Examples
@@ -533,13 +533,13 @@ impl FileTypeExt for fs::FileType {
533533
/// }
534534
/// ```
535535
#[stable(feature = "symlink", since = "1.1.0")]
536-
pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
537-
sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), false)
536+
pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
537+
sys::fs::symlink_inner(original.as_ref(), link.as_ref(), false)
538538
}
539539

540540
/// Creates a new directory symlink on the filesystem.
541541
///
542-
/// The `dst` path will be a directory symbolic link pointing to the `src`
542+
/// The `link` path will be a directory symbolic link pointing to the `original`
543543
/// path.
544544
///
545545
/// # Examples
@@ -553,6 +553,6 @@ pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Resul
553553
/// }
554554
/// ```
555555
#[stable(feature = "symlink", since = "1.1.0")]
556-
pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
557-
sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), true)
556+
pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
557+
sys::fs::symlink_inner(original.as_ref(), link.as_ref(), true)
558558
}

library/std/src/sys/windows/fs.rs

+22-17
Original file line numberDiff line numberDiff line change
@@ -759,30 +759,32 @@ pub fn readlink(path: &Path) -> io::Result<PathBuf> {
759759
file.readlink()
760760
}
761761

762-
pub fn symlink(src: &Path, dst: &Path) -> io::Result<()> {
763-
symlink_inner(src, dst, false)
762+
pub fn symlink(original: &Path, link: &Path) -> io::Result<()> {
763+
symlink_inner(original, link, false)
764764
}
765765

766-
pub fn symlink_inner(src: &Path, dst: &Path, dir: bool) -> io::Result<()> {
767-
let src = to_u16s(src)?;
768-
let dst = to_u16s(dst)?;
766+
pub fn symlink_inner(original: &Path, link: &Path, dir: bool) -> io::Result<()> {
767+
let original = to_u16s(original)?;
768+
let link = to_u16s(link)?;
769769
let flags = if dir { c::SYMBOLIC_LINK_FLAG_DIRECTORY } else { 0 };
770770
// Formerly, symlink creation required the SeCreateSymbolicLink privilege. For the Windows 10
771771
// Creators Update, Microsoft loosened this to allow unprivileged symlink creation if the
772772
// computer is in Developer Mode, but SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE must be
773773
// added to dwFlags to opt into this behaviour.
774774
let result = cvt(unsafe {
775775
c::CreateSymbolicLinkW(
776-
dst.as_ptr(),
777-
src.as_ptr(),
776+
link.as_ptr(),
777+
original.as_ptr(),
778778
flags | c::SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE,
779779
) as c::BOOL
780780
});
781781
if let Err(err) = result {
782782
if err.raw_os_error() == Some(c::ERROR_INVALID_PARAMETER as i32) {
783783
// Older Windows objects to SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE,
784784
// so if we encounter ERROR_INVALID_PARAMETER, retry without that flag.
785-
cvt(unsafe { c::CreateSymbolicLinkW(dst.as_ptr(), src.as_ptr(), flags) as c::BOOL })?;
785+
cvt(unsafe {
786+
c::CreateSymbolicLinkW(link.as_ptr(), original.as_ptr(), flags) as c::BOOL
787+
})?;
786788
} else {
787789
return Err(err);
788790
}
@@ -791,15 +793,15 @@ pub fn symlink_inner(src: &Path, dst: &Path, dir: bool) -> io::Result<()> {
791793
}
792794

793795
#[cfg(not(target_vendor = "uwp"))]
794-
pub fn link(src: &Path, dst: &Path) -> io::Result<()> {
795-
let src = to_u16s(src)?;
796-
let dst = to_u16s(dst)?;
797-
cvt(unsafe { c::CreateHardLinkW(dst.as_ptr(), src.as_ptr(), ptr::null_mut()) })?;
796+
pub fn link(original: &Path, link: &Path) -> io::Result<()> {
797+
let original = to_u16s(original)?;
798+
let link = to_u16s(link)?;
799+
cvt(unsafe { c::CreateHardLinkW(link.as_ptr(), original.as_ptr(), ptr::null_mut()) })?;
798800
Ok(())
799801
}
800802

801803
#[cfg(target_vendor = "uwp")]
802-
pub fn link(_src: &Path, _dst: &Path) -> io::Result<()> {
804+
pub fn link(_original: &Path, _link: &Path) -> io::Result<()> {
803805
return Err(io::Error::new(io::ErrorKind::Other, "hard link are not supported on UWP"));
804806
}
805807

@@ -883,8 +885,11 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
883885
}
884886

885887
#[allow(dead_code)]
886-
pub fn symlink_junction<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
887-
symlink_junction_inner(src.as_ref(), dst.as_ref())
888+
pub fn symlink_junction<P: AsRef<Path>, Q: AsRef<Path>>(
889+
original: P,
890+
junction: Q,
891+
) -> io::Result<()> {
892+
symlink_junction_inner(original.as_ref(), junction.as_ref())
888893
}
889894

890895
// Creating a directory junction on windows involves dealing with reparse
@@ -893,7 +898,7 @@ pub fn symlink_junction<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::R
893898
//
894899
// http://www.flexhex.com/docs/articles/hard-links.phtml
895900
#[allow(dead_code)]
896-
fn symlink_junction_inner(target: &Path, junction: &Path) -> io::Result<()> {
901+
fn symlink_junction_inner(original: &Path, junction: &Path) -> io::Result<()> {
897902
let d = DirBuilder::new();
898903
d.mkdir(&junction)?;
899904

@@ -911,7 +916,7 @@ fn symlink_junction_inner(target: &Path, junction: &Path) -> io::Result<()> {
911916
// FIXME: this conversion is very hacky
912917
let v = br"\??\";
913918
let v = v.iter().map(|x| *x as u16);
914-
for c in v.chain(target.as_os_str().encode_wide()) {
919+
for c in v.chain(original.as_os_str().encode_wide()) {
915920
*buf.offset(i) = c;
916921
i += 1;
917922
}

0 commit comments

Comments
 (0)