Don't search stdout redirected file.

When running ripgrep like this:

    rg foo > output

we must be careful not to search `output` since ripgrep is actively writing
to it. Searching it can cause massive blowups where the file grows without
bound.

While this is conceptually easy to fix (check the inode of the redirection
and the inode of the file you're about to search), there are a few problems
with it.

First, inodes are a Unix thing, so we need a Windows specific solution to
this as well. To resolve this concern, I created a new crate, `same-file`,
which provides a cross platform abstraction.

Second, stat'ing every file is costly. This is not avoidable on Windows,
but on Unix, we can get the inode number directly from directory traversal.
However, this information wasn't exposed, but now it is (through both the
ignore and walkdir crates).

Fixes #286
This commit is contained in:
Andrew Gallant
2017-01-08 10:27:30 -05:00
parent 82df3b7685
commit 461e0c4e33
7 changed files with 242 additions and 70 deletions

View File

@@ -37,6 +37,7 @@ memchr = "1"
memmap = "0.5"
num_cpus = "1"
regex = "0.2.0"
same-file = "0.1.1"
termcolor = { version = "0.1.0", path = "termcolor" }
[target.'cfg(windows)'.dependencies]