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

cp: Updated prompt logic to handle update with and without interactive mode enabled #6069

Closed
wants to merge 0 commits into from

Conversation

Vikrant2691
Copy link
Contributor

@Vikrant2691 Vikrant2691 commented Mar 14, 2024

Fixes #6019
The issue was that the condition preemptively returned Ok when the status was UpdateMode::ReplaceIfOlder.
But if the condition is removed, it will prompt for coreutils cp -i -u old new.
This is a very peculiar condition where we don't want to prompt where we know time(old)>time(new). (update only overwrites when a new file is copied to an old file.)
So, the prompt needs to be closer to the condition where the time for the folders is checked.

@cakebaker cakebaker changed the title Fixes #6019: Updated prompt logic to handle update with and without interactive mode enabled cp: Updated prompt logic to handle update with and without interactive mode enabled Mar 14, 2024
@cakebaker
Copy link
Contributor

Can you please add a test to ensure we don't regress in the future?

Copy link

GNU testsuite comparison:

GNU test failed: tests/rm/rm2. tests/rm/rm2 is passing on 'main'. Maybe you have to rebase?

@Vikrant2691
Copy link
Contributor Author

Can you please add a test to ensure we don't regress in the future?

Hi @cakebaker, there was already a test case written for the current scenario. and for coreutils cp -i -u old new we already have a test case. According to me, all tests are covered.

@cakebaker
Copy link
Contributor

@Vikrant2691 all the better if there is already a test :) I don't know if you have seen that it fails?

@Vikrant2691
Copy link
Contributor Author

Vikrant2691 commented Mar 15, 2024 via email

Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/tail/inotify-dir-recreate (passes in this run but fails in the 'main' branch)

Copy link

GNU testsuite comparison:

Skip an intermittent issue tests/tail/inotify-dir-recreate (fails in this run but passes in the 'main' branch)

Copy link
Collaborator

@BenWiederhake BenWiederhake left a comment

Choose a reason for hiding this comment

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

test_cp_arg_update_interactive failed. Congratulations, this fix reveals the reason for a flaky test, because now it depends on which file is newer; and because the test fixture copies file in "random" order (typically inode order, which is "random"), this will fail 50% of the time.

Please fix this test; perhaps remove it entirely because this is now covered by the new tests.

Additional comments:

  • I don't quite understand how cp is structured, and it seems surprising to me that handle_existing_dest() checks the update mode, even though handle_copy_mode() checks it again a bit later. Is there a scenario where it ends up asking for confirmation twice?
  • If you feel like writing more tests, I think it's a good idea to test test_cp_arg_interactive_update_overwrite_older() but with the input Y.
  • Although this project seems to pay not much attention to it, I would like to recommend git rebase to you. This is a great opportunity to get started! In this case, maybe a single or perhaps two commits would be easier to understand than 11 fix-fixing-fix-commits and pointless merge-commits.

// -u -i *WILL* show the prompt to validate the override.
// Therefore, the error code depends on the prompt response.
let (at, mut ucmd) = at_and_ucmd!();
at.touch("b");
std::thread::sleep(Duration::from_secs(1));
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 add a comment why sleep ?
and can we make it shorter ?
thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, this issue is what I posted in the chat. The test itself was failing initially. The reason is that the two create calls are so close to each other that the files get at the same time. To mitigate that we have two options. 1) add a delay and 2) set time explicitly.
I tried to set time explicitly initially, but the libraries I used were unstable (FileTimes). Please let me know if you have some option to set time while creating the file. I had no other option than to use 1.
Sorry, I did not get what you meant by shorter...

Copy link
Contributor

@cakebaker cakebaker Mar 28, 2024

Choose a reason for hiding this comment

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

For 2) you could use something like:

let _ = at.make_file("b").set_modified(std::time::SystemTime::UNIX_EPOCH);

Update: Please ignore it, I just realized this snippet requires Rust 1.75 :|

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For 2) you could use something like:

let _ = at.make_file("b").set_modified(std::time::SystemTime::UNIX_EPOCH);

Update: Please ignore it, I just realized this snippet requires Rust 1.75 :|

yes, I tried FileTimes but it did not work as you said...

@Vikrant2691
Copy link
Contributor Author

test_cp_arg_update_interactive failed. Congratulations, this fix reveals the reason for a flaky test, because now it depends on which file is newer; and because the test fixture copies file in "random" order (typically inode order, which is "random"), this will fail 50% of the time.

Please fix this test; perhaps remove it entirely because this is now covered by the new tests.

Additional comments:

  • I don't quite understand how cp is structured, and it seems surprising to me that handle_existing_dest() checks the update mode, even though handle_copy_mode() checks it again a bit later. Is there a scenario where it ends up asking for confirmation twice?
  • If you feel like writing more tests, I think it's a good idea to test test_cp_arg_interactive_update_overwrite_older() but with the input Y.
  • Although this project seems to pay not much attention to it, I would like to recommend git rebase to you. This is a great opportunity to get started! In this case, maybe a single or perhaps two commits would be easier to understand than 11 fix-fixing-fix-commits and pointless merge-commits.

Actions on me:

  • I will remove the flaky test
  • Add a test for the 'Y' option.

Answering additional comments

  • you are right. The structure and flag management need to be looked at for cp. handle_existing_dest() handles "overwrite" and handle_copy_mode() checks the "update" modes. I tried not to rock the boat too much. I just changed where we are "prompting" the user. -i -u is specific with the times. So I had to route the prompt where the time is checked. we can move the check as well. Please let me know if we can do it better.
  • I will follow the rebase suggestion .. thank you

@Vikrant2691
Copy link
Contributor Author

I am not sure why the checks in the pipeline failed.. could we please run it again

@cakebaker
Copy link
Contributor

@Vikrant2691 The failure of the windows-gnu checks are unrelated to your PR, they currently fail for all PRs :|

Copy link
Collaborator

@BenWiederhake BenWiederhake left a comment

Choose a reason for hiding this comment

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

Looks good to me!

EDIT: Looks like this needs a slightly more involved rebase. I currently don't have the capacity to do it, sorry. Your options are:

  • Some other maintainer steps in and rebases it
  • You rebase it (highly recommended!), and make it trivial to merge
  • A long time passes, I have enough time, and then I'll rebase it.

@Vikrant2691
Copy link
Contributor Author

Looks good to me!

EDIT: Looks like this needs a slightly more involved rebase. I currently don't have the capacity to do it, sorry. Your options are:

  • Some other maintainer steps in and rebases it
  • You rebase it (highly recommended!), and make it trivial to merge
  • A long time passes, I have enough time, and then I'll rebase it.

okk let me check

@Vikrant2691
Copy link
Contributor Author

Looks good to me!

EDIT: Looks like this needs a slightly more involved rebase. I currently don't have the capacity to do it, sorry. Your options are:

  • Some other maintainer steps in and rebases it
  • You rebase it (highly recommended!), and make it trivial to merge
  • A long time passes, I have enough time, and then I'll rebase it.

Could you please check if the rebase is successful?

@BenWiederhake
Copy link
Collaborator

Sorry, my bad, let me try to fix this.

@BenWiederhake
Copy link
Collaborator

I messed up, I didn't want to close this PR, and because of reasons that I understand but cannot fix, this PR cannot be re-opened easily. Instead, please see #6207. I promise to learn from my mistake!

Please sanity-check whether this looks correct. git diff says there is no difference in content between c94809b (your original final commit) and 9b9c0cc (this commit).

If you agree that there is no difference, I'll just merge #6207 instead.

@Vikrant2691
Copy link
Contributor Author

Vikrant2691 commented Apr 10, 2024 via email

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.

cp: Wrong handling of -i -u
4 participants