[PATCH] grep: fix -r following file symlinks
Sertonix
sertonix at posteo.net
Mon Apr 14 15:33:42 UTC 2025
On Mon Apr 14, 2025 at 4:05 PM CEST, Csókás Bence wrote:
> Hi,
>
> On 2025. 04. 14. 12:16, Sertonix wrote:
>>
>> GNU grep -r doesn't search in files that are linked to by symlinks.
>>
>> function old new delta
>> file_action_grep 219 228 +9
>> ------------------------------------------------------------------------------
>> (add/remove: 0/0 grow/shrink: 1/0 up/down: 9/0) Total: 9 bytes
>> ---
>> findutils/grep.c | 2 ++
>> 1 file changed, 2 insertions(+)
>>
>> diff --git a/findutils/grep.c b/findutils/grep.c
>> index f6d0a73f4..0bd4898bc 100644
>> --- a/findutils/grep.c
>> +++ b/findutils/grep.c
>> @@ -666,6 +666,8 @@ static int FAST_FUNC file_action_grep(struct recursive_state *state UNUSED_PARAM
>> * example will return the raw directory contents). */
>> if (S_ISLNK(statbuf->st_mode)) {
>> struct stat sb;
>> + if (!(option_mask32 & OPT_R))
>
> You mixed it up, -R *does* follow links, -r doesn't (which would be
> OPT_r here).
Since I used a "not" here it is correct. I wanted to make it easier to
see that it is related to the code below:
((option_mask32 & OPT_R) ? ACTION_FOLLOWLINKS : 0)
(And I checked that the binary size doesn't differ)
> But the whole patch is not needed anyway, because
> recursive_action() (which calls file_action_grep()) only follows
> symlinks if -R is given: [1]
recursive_action() won't recursivly search directories pointed to by
symlinks but it will still pass the symlinks to file_action_grep().
recursive_action() then uses stat (not lstat) and when the symlink
target is a file it will always search the file regardless of -r or -R.
Just try for yourself:
$ mkdir dir
$ echo test > dir/foo
$ ln -s foo dir/bar
$ busybox grep test dir -r
dir/bar:test
dir/foo:test
$ grep test dir -r
dir/foo:test
> [1] https://git.busybox.net/busybox/tree/findutils/grep.c#n696
>
>> + return 0;
>> if (stat(filename, &sb) != 0) {
>> if (!SUPPRESS_ERR_MSGS)
>> bb_simple_perror_msg(filename);
>
> Bence
>
> _______________________________________________
> busybox mailing list
> busybox at busybox.net
> https://lists.busybox.net/mailman/listinfo/busybox
More information about the busybox
mailing list