Skip to content
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

mv: hard link check #4697

Closed
wants to merge 4 commits into from
Closed

mv: hard link check #4697

wants to merge 4 commits into from

Conversation

haciek
Copy link
Contributor

@haciek haciek commented Mar 31, 2023

Related to issue #4613
This pull request checks if files are hard linked.

Current behavior:

$ rm -f link
$ touch origin
$ echo content > origin
$ ln origin link
$ ./target/debug/coreutils mv origin link
mv: 'origin' and 'link' are the same file

Previously this didn't generate an error.
Let me know if there is anything I can change/improve. Thanks!

@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/mv/force is no longer failing!
Congrats! The gnu test tests/mv/i-4 is no longer failing!

@sylvestre
Copy link
Contributor

nice, could you please add a test ? thanks

@haciek
Copy link
Contributor Author

haciek commented Mar 31, 2023

nice, could you please add a test ? thanks

Sure, I'll try to work on that soon.

let mut is_hard_link = false;

if target.symlink_metadata().is_ok() {
let source_inode = source.symlink_metadata().unwrap().ino();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please avoid an unwrap here? Maybe manage the error ?

@github-actions
Copy link

github-actions bot commented Apr 1, 2023

GNU testsuite comparison:

Congrats! The gnu test tests/mv/force is no longer failing!
Congrats! The gnu test tests/mv/i-4 is no longer failing!

// Hard links are not allowed for directories
if !target_metadata.is_dir() {
let target_inode = target_metadata.ino();
let source_inode = match source.symlink_metadata() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about something like ?

    let source_inode = source.symlink_metadata().map_err(|_| MvError::NoSuchFile(source.quote().to_string()).into())?;

Comment on lines 275 to 278
if source_inode.eq(&target_inode) {
is_hard_link = true;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if source_inode.eq(&target_inode) {
is_hard_link = true;
}
is_hard_link = source_inode.ino() == target_inode;

@sylvestre
Copy link
Contributor

fails on windows:


error[E0599]: no method named `ino` found for struct `Metadata` in the current scope
   --> src\uu\mv\src/mv.rs:267:56
    |
267 |                     let target_inode = target_metadata.ino();
    |                                                        ^^^ method not found in `Metadata`

@haciek
Copy link
Contributor Author

haciek commented Apr 1, 2023

fails on windows:


error[E0599]: no method named `ino` found for struct `Metadata` in the current scope
   --> src\uu\mv\src/mv.rs:267:56
    |
267 |                     let target_inode = target_metadata.ino();
    |                                                        ^^^ method not found in `Metadata`

I'm not really good with windows, should I make the check for hard links run on Unix only or is there a better way to handle this error?

@haciek haciek marked this pull request as draft April 1, 2023 12:31
@haciek haciek marked this pull request as ready for review April 5, 2023 18:43
@github-actions
Copy link

github-actions bot commented Apr 5, 2023

GNU testsuite comparison:

Congrats! The gnu test tests/mv/force is no longer failing!
Congrats! The gnu test tests/mv/i-4 is no longer failing!
GNU test failed: tests/mv/i-1. tests/mv/i-1 is passing on 'main'. Maybe you have to rebase?

@github-actions
Copy link

github-actions bot commented Apr 5, 2023

GNU testsuite comparison:

Congrats! The gnu test tests/mv/force is no longer failing!
Congrats! The gnu test tests/mv/i-4 is no longer failing!
GNU test failed: tests/mv/i-1. tests/mv/i-1 is passing on 'main'. Maybe you have to rebase?
GNU test failed: tests/rm/rm1. tests/rm/rm1 is passing on 'main'. Maybe you have to rebase?
GNU test failed: tests/tail-2/inotify-dir-recreate. tests/tail-2/inotify-dir-recreate is passing on 'main'. Maybe you have to rebase?

@github-actions
Copy link

github-actions bot commented Apr 6, 2023

GNU testsuite comparison:

Congrats! The gnu test tests/mv/force is no longer failing!
Congrats! The gnu test tests/mv/i-4 is no longer failing!
GNU test failed: tests/mv/i-1. tests/mv/i-1 is passing on 'main'. Maybe you have to rebase?

@@ -420,7 +445,7 @@ fn rename(
OverwriteMode::NoClobber => return Ok(()),
OverwriteMode::Interactive => {
if !prompt_yes!("overwrite {}?", to.quote()) {
return Err(io::Error::new(io::ErrorKind::Other, ""));
return Ok(());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the change that makes the CI fail. We have a mechanism to turn a Result into an exit code. Ok will be 0 and an error usually 1. You can get around this with set_exit_code maybe. Or you could add a new variant Skipped (or something similar) to the MvError enum and return that here.

@@ -1,4 +1,3 @@
// This file is part of the uutils coreutils package.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't remove this

@cakebaker
Copy link
Contributor

Closing this PR because the hard link check has been implemented as part of #4831 in the meantime.

Thanks anyway for your PR :)

@cakebaker cakebaker closed this May 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants