Skip to content

Clarify behavior of std::fs::hard_link when second path already exists. #130117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
geeklint opened this issue Sep 8, 2024 · 2 comments · Fixed by #135415
Closed

Clarify behavior of std::fs::hard_link when second path already exists. #130117

geeklint opened this issue Sep 8, 2024 · 2 comments · Fixed by #135415
Assignees
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-filesystem Area: `std::fs` C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@geeklint
Copy link
Contributor

geeklint commented Sep 8, 2024

Location

Documentation of the std::fs::hard_link function, e.g. https://doc.rust-lang.org/std/fs/fn.hard_link.html

Summary

The function currently doesn't specify what happens if the second parameter (link / "the destination") already exists.

The note for unix systems and linux's man pages lead me to believe it will return an error in this case, but I don't think one should have to read linux man pages to determine the behavior of a rust function and it's not obvious if this is the intended rust behavior across platforms or if the behavior would be platform-specific.

Assuming it's not platform-specific, I think it'd be sufficient to add a bullet point to the # Errors section as a second documented error case.

@geeklint geeklint added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Sep 8, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 8, 2024
@workingjubilee workingjubilee added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. A-filesystem Area: `std::fs` labels Sep 8, 2024
@saethlin saethlin added C-bug Category: This is a bug. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 10, 2024
@Harshit933
Copy link
Contributor

@rustbot claim

@hkBst
Copy link
Member

hkBst commented Oct 19, 2024

The code responsible for this redirects to fs_imp::link which is defined up top as use crate::sys::fs as fs_imp;. If we look at the sys mod we find pub use pal::*;, so we look at the pal mod where it basically says that depending on the target_os use an implementation specific for that target_os, so for unix we look in pal::unix::fs, for windows we look in pal::windows::fs. In this way you can drill down to the actual kernel calls being made, but I'm not sure this is viable way to proceed.

Perhaps a better way is to write a small program to do the hard link creation with an existing file at the link location and run that on a few platforms and see what happens. Report back with your findings and maybe it will then be clear what behavior to document.

@bors bors closed this as completed in 0741cc0 Jan 25, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 25, 2025
Rollup merge of rust-lang#135415 - Harshit933:hard-link-error, r=ChrisDenton

Add `File already exists` error doc to `hard_link` function

## Description
If the link path already exists, the error `AlreadyExists` is returned. This commit adds this error to the docs.

I tested it with the current rust master version, this error was returned when there is already a link for the file is present.
This was the error returned:
```
[harshit:../Desktop/rust_compiler_testing/hard_link (master|…5)] cargo +stage1 run
   Compiling hard_link v0.1.0 (/home/harshit/Desktop/rust_compiler_testing/hard_link)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.12s
     Running `target/debug/hard_link`
Err(Os { code: 17, kind: AlreadyExists, message: "File exists" })
```
This is my first PR on rust, any suggestions on which issue I can take next are most welcome 😄

Fixes rust-lang#130117
github-actions bot pushed a commit to tautschnig/verify-rust-std that referenced this issue Mar 11, 2025
…sDenton

Add `File already exists` error doc to `hard_link` function

## Description
If the link path already exists, the error `AlreadyExists` is returned. This commit adds this error to the docs.

I tested it with the current rust master version, this error was returned when there is already a link for the file is present.
This was the error returned:
```
[harshit:../Desktop/rust_compiler_testing/hard_link (master|…5)] cargo +stage1 run
   Compiling hard_link v0.1.0 (/home/harshit/Desktop/rust_compiler_testing/hard_link)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.12s
     Running `target/debug/hard_link`
Err(Os { code: 17, kind: AlreadyExists, message: "File exists" })
```
This is my first PR on rust, any suggestions on which issue I can take next are most welcome 😄

Fixes rust-lang#130117
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-filesystem Area: `std::fs` C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants