-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Detect infinite loop when input and output are the same #1197
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
Conversation
|
Right now, this is just sketched up based on @sharkdp's proposal in the issue thread and has some unwraps and stuff everywhere. I encountered some questions so far:
$ cat --version
cat (GNU coreutils) 8.32
...
$ echo A > a
$ echo B > b
$ cat a b > a
$ cat a
$
$ echo A > a
$ echo B > b
$ cat a b > b
cat: b: input file is output file
$ cat b
A
$In my opinion, it would be best if no files are changed if a loop is detected, but that would mean to be inconsistent with the behavior of
|
e6747d9 to
f4e0d05
Compare
|
Okay, I found more weird stuff. The files Behavior on both version 0.15.4 (my normal installation) and the master branch (e4df564):
|
|
I did some testing on my windows machine. Lines with $ echo A > a
$ echo B > b
$ echo C > c
(PS) .\target\debug\bat.exe a b c > abc
(PS) .\target\debug\bat.exe a b c > b
$ xxd abc
00000000: fffe 4100 0d00 0a00 4200 0d00 0a00 4300 ..A.....B.....C.
00000010: 0d00 0a00
$ xxd b
00000000: fffe 4100 0d00 0a00 4100 0d00 0a00 430a ..A.....A.....C.
00000010: 0d00 0a00As you can see, first the file I was a bit confused about the other stuff: The trailing As a site note: When concatenating multiple files that were created on Windows (therefore having the leading [Powershell]
$ echo A > a
$ echo B > b
$ echo C > c
$ .\target\debug\bat.exe a b c > abc
[Linux]
$ xxd a
00000000: fffe 4100 0d00 0a00 ..A.....
$ xxd abc
00000000: fffe 4100 0d00 0a00 fffe 4200 0d00 0a00 ..A.......B.....
00000010: fffe 4300 0d00 0a00 ..C.....I don't know whether this is a problem, but according to the Wikipedia article from above, these chararcters are never valid. In VS Code, this file looks like this: The Powershell tool (PS) cat a, b, c > abc
$ xxd abc
00000000: fffe 4100 0d00 0a00 4200 0d00 0a00 4300 ..A.....B.....C.
00000010: 0d00 0a00 ....ConclusionRunning @sharkdp I am not sure what to do with the rest of my findings. Should I open a tracking issue for the weird behaviors, or is none of this really a problem? |
914c373 to
8de7936
Compare
|
I spent some time with the Windows implementation. Firstly, I found that it is impossible to find out to which file stdout is being redirected to in PowerShell, since In the command prompt however, it works as aspected and all direct conflicts are detectable ( The code has some Could someone take an early look at the code, even though I don't mark the PR as ready yet @sharkdp @eth-p? I am going to mark it ready when everything is settled and I squashed the commits into useful snippets. |
When a shell (at least most shells on linux) sees a redirection ('>' redirection, not '>>', which only appends to the file), it first clears the file that the output is being redirected to.
On my linux machine When I'm not sure why |
8cccf70 to
2ea8ed7
Compare
|
Alright, I tidied up everything and rebased. Like I said, I am gonna squash the commits into 2 or 3 once reviews are done. @TheHardew Yup, that explanation checks out, nice 👍 For the remaining TODOs from the first post, I could use some input from you guys. |
|
@niklasmohrin Thank you very much for your work on this! I haven't forgotten it, but didn't have the time to dig into this myself, so far. There is quite a lot if platform-specific (and |
|
@sharkdp I guess I could move this code to another repo and publish it on crates.io. By the way, I only recently saw that there is Should I start a crate on my personal account or should it be hosted in this repo? |
If you would be up to that, I think that would be great. It sounds like something that could be used in other tools as well. But please note that I haven't reviewed the changes here so far, so I cannot "promise" that we are actually going to integrate that new crate. |
|
Alright, I moved all the code into a crate on my profile, clircle. This makes the diff here pretty short, let me know what you think! PS: Also tell me if you have suggestions for the |
|
Now everything is cross-platform and Rust 1.40 safe, please go ahead 😄 |
|
Awesome! Thank you very much for creating a Before we integrate this, I would like to see a new integration test that checks the basic functionality (and also ensures that it works cross-platform). This could be added to I hope that this would be possible with |
|
It doesn't look like |
|
Sounds great - thank you for looking into this. |
9fd1fe7 to
dbb0c46
Compare
|
Alright, I released another minor version of |
|
@niklasmohrin Thank you very much! I just tested this locally, and it seems like the output file will still be overwritten - even if I'm not sure if this has to be fixed (after all, the user definitely asked for |
|
@sharkdp Yeah, that cannot be mitigated, because the file is cleared before Overall, there is nothing that can be done to save the contents of the overwritten file when redirecting. We could introduce a |
dbb0c46 to
ec60d84
Compare
Oh, right. Of course. Thanks for the explanation!
Yes, definitely. I think this new functionality here is already a great improvement. |
|
@niklasmohrin Awesome work on this PR. Thank you very much for the dedication! |

Fixes #1193
TODO:
cfg(target = linux)where neededwinapi::*GetFileInformationByHandlevsGetFinalPathNameByHandleW(+ Remove the TODO in the function)nixsfstateprintln!