8000 split: Don't overwrite files by andrewbaptist · Pull Request #3719 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

split: Don't overwrite files #3719

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

Merged
merged 1 commit into from
Jul 23, 2022
Merged

split: Don't overwrite files #3719

merged 1 commit into from
Jul 23, 2022

Conversation

andrewbaptist
Copy link
Contributor

Check that a file exists by calling create_new and changing the
interface of instantiate_current_writer to return a Result rather
than calling unwrap.

@andrewbaptist
Copy link
Contributor Author

This addresses #3627

@sylvestre
Copy link
Contributor

Could you please add a test ? :)

Copy link
Contributor
@sylvestre sylvestre left a comment

Choose a reason for hiding this comment

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

need a test

@andrewbaptist
Copy link
Contributor Author

Working on it now - I realized I misunderstood the contract first and changed it, so it mostly works, except for the case of a pipe input. Should have something ready shortly

@andrewbaptist andrewbaptist force-pushed the main branch 4 times, most recently from 09f466d to 9f5eb3a Compare July 18, 2022 15:13
@andrewbaptist
Copy link
Contributor Author

Test added - it now seems to exactly match the behavior of gnu split for the error cases. I have not been able to test on windows, but this should not make things any worse.

@andrewbaptist andrewbaptist force-pushed the main branch 2 times, most recently from f75436e to 3c460f2 Compare July 18, 2022 19:01
@andrewbaptist andrewbaptist requested a review from sylvestre July 18, 2022 19:19
@andrewbaptist
Copy link
Contributor Author

This PR should be good to go now and fix the issue #3627. The review comment is old, but please review now as I think its ready.

}
}

pub fn paths_refer_to_same_file(p1: &str, p2: &str) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

paths_refer_to_same_file seems to be the same, no ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, that doesn't handle the "-" case where we are trying to tell if something like
split < xaa is run. This is the reason it needs to compare to /dev/fd/0. There doesn't seem to be any other way to handle this case correctly (which is part of the gnu test suite).

$ echo 12345 > xaa && split < xaa
split: 'xaa' would overwrite input; aborting

It might be worth extending that method to handle this case, but I'm not sure if this is something we generally want to do. With the code as I have it here it handles that case correctly.

Copy link
Contributor

Choose a reason for hiding this comment

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

or you keep the "-" mgmt and plug the rest of the function on paths_referr_to_same_file ?

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks

@andrewbaptist
Copy link
Contributor Author

So after a lot of weirdness understanding how macos/bsd processes /dev/fd/0. I finally have a workable solution. On linux "fd(0) == /dev/fd/0" however on macos/Darwin they are not. Specifically, see this:

% stat -f "Inode %i Device (%Hd / %Ld) Special Device (%Hr / %Lr)" xaa
Inode 48835005 Device (1 / 17) Special Device (0 / 0)
% stat -f "Inode %i Device (%Hd / %Ld) Special Device (%Hr / %Lr)" < xaa
Inode 48835005 Device (1 / 17) Special Device (0 / 0)
% stat -f "Inode %i Device (%Hd / %Ld) Special Device (%Hr / %Lr)" /dev/fd/0 <  xaa
Inode 48835005 Device (201 / 4189594) Special Device (0 / 0)
% stat -f "Inode %i Device (%Hd / %Ld) Special Device (%Hr / %Lr)" /dev/fd/0
Inode 2691 Device (201 / 4189594) Special Device (16 / 1)

The first 3 lines should have been identical (the Inode is), but the Device (st_dev) is different on the third one. The workaround that I added seems to be the only way to get this to work as there is no "path" that will have the same inode and st_dev.

@andrewbaptist
Copy link
Contributor Author

As reference, here are the equivalent commands on linux:

$ stat -L -c "Inode %i Device %T %t" xaa
Inode 9175043 Device 0 0
$ stat -L -c "Inode %i Device %T %t" - < xaa
Inode 9175043 Device 0 0
$ stat -L -c "Inode %i Device %T %t" /dev/fd/0 < xaa
Inode 9175043 Device 0 0
$ stat -L -c "Inode %i Device %T %t" /dev/fd/0
Inode 3 Device 0 88

Note that indeed the first three are the same.

Check that a file exists by calling create_new and changing the
interface of instantiate_current_writer to return a Result rather
than calling unwrap.
@andrewbaptist
Copy link
Contributor Author

I added the same call for windows too (note that I don't have a way to test it though)

@sylvestre
Copy link
Contributor

Nice:
Congrats! The gnu test tests/split/guard-input is no longer failing!

@andrewbaptist
Copy link
Contributor Author

Let me know if you need anything more done on this. Thanks!

Copy link
Contributor
@sylvestre sylvestre left a comment

Choose a reason for hiding this comment

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

LGTM
thanks

@sylvestre sylvestre merged commit 62305e6 into uutils:main Jul 23, 2022
@jfinkels jfinkels linked an issue Aug 12, 2022 that may be closed by this pull request
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.

split should refuse to overwrite the input file
2 participants
0