Skip to content

Commit fb244a8

Browse files
committed
-z threads fix for unlikely file read errors
when a file read error occurs (unlikely, but may happen), option -z decompression threads will shut down (can't read input to decompress) and ugrep search ends, but must also make sure to close an open pipe from decompression threads that msy stay open under error conditions, and a minor change to move filter() invocation up above fcntl() non-blocking input
1 parent 48a2359 commit fb244a8

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

src/ugrep.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3446,31 +3446,32 @@ struct Grep {
34463446
return false;
34473447
}
34483448

3449+
// --filter: fork process to filter file, when applicable
3450+
if (!filter(file_in, pathname))
3451+
return false;
3452+
34493453
#if defined(__linux__)
34503454
if (file_in != stdin && file_in != Static::source)
34513455
{
34523456
// Linux has system "regular files" that can be set non-blocking to raise EAGAIN e.g. in /proc and /sys
34533457
if (flag_devices_action != Action::READ)
34543458
{
34553459
int fd = fileno(file_in);
3456-
int fl = fcntl(fd, F_GETFL);
3457-
if (fl >= 0)
3458-
fcntl(fd, F_SETFL, fl | O_NONBLOCK);
3460+
struct stat buf;
3461+
if (fstat(fd, &buf) == 0 && S_ISREG(buf.st_mode))
3462+
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
34593463
}
34603464
}
34613465
#endif
34623466

3463-
// --filter: fork process to filter file, when applicable
3464-
if (!filter(file_in, pathname))
3465-
return false;
3466-
34673467
#ifdef HAVE_LIBZ
34683468
if (flag_decompress)
34693469
{
34703470
#ifdef WITH_DECOMPRESSION_THREAD
34713471

34723472
// start decompression thread if not running, get pipe with decompressed input
34733473
FILE *pipe_in = zthread.start(flag_zmax, pathname, file_in, find);
3474+
34743475
if (pipe_in == NULL)
34753476
{
34763477
fclose(file_in);
@@ -3482,6 +3483,7 @@ struct Grep {
34823483
input = reflex::Input(pipe_in, flag_encoding_type);
34833484

34843485
#else
3486+
34853487
(void)find; // appease -Wunused
34863488

34873489
// create or open a new zstreambuf
@@ -3827,14 +3829,28 @@ struct Grep {
38273829

38283830
#ifdef HAVE_LIBZ
38293831
#ifdef WITH_DECOMPRESSION_THREAD
3832+
38303833
if (flag_decompress)
3834+
{
3835+
// close the input FILE* and its underlying pipe previously created with pipe() and fdopen()
3836+
if (input.file() != NULL)
3837+
{
3838+
// close and unassign input, i.e. input.file() == NULL, also closes pipe_fd[0] per fdopen()
3839+
fclose(input.file());
3840+
input.clear();
3841+
}
3842+
38313843
zthread.cancel();
3844+
}
3845+
38323846
#else
3847+
38333848
if (stream != NULL)
38343849
{
38353850
delete stream;
38363851
stream = NULL;
38373852
}
3853+
38383854
#endif
38393855
#endif
38403856

@@ -3954,7 +3970,7 @@ struct Grep {
39543970
#endif
39553971

39563972
#ifndef OS_WIN
3957-
if (!flag_quiet && !flag_files_with_matches && !flag_count)
3973+
if (!flag_decompress && !flag_quiet && !flag_files_with_matches && !flag_count)
39583974
{
39593975
int fd = fileno(file_in);
39603976
struct stat buf;

0 commit comments

Comments
 (0)