Re: [hackers] [sbase] [PATCH 09/10] tail: Use fstat in case file is removed

From: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Mon, 5 Dec 2016 10:20:49 +0100

On Sun, Dec 04, 2016 at 09:55:11PM -0800, Michael Forney wrote:
> ---
> tail.c | 16 +++++++---------
> 1 file changed, 7 insertions(+), 9 deletions(-)
>
> diff --git a/tail.c b/tail.c
> index ad97308..000be62 100644
> --- a/tail.c
> +++ b/tail.c
> _AT_@ -133,7 +133,7 @@ main(int argc, char *argv[])
> }
> if (many)
> printf("%s==> %s <==\n", newline ? "\n" : "", *argv);
> - if (stat(*argv, &st1) < 0)
> + if (fstat(fileno(fp), &st1) < 0)
> eprintf("stat %s:", *argv);

The error message should be "fstat" here instead of "stat".

> if (!(S_ISFIFO(st1.st_mode) || S_ISREG(st1.st_mode)))
> fflag = 0;
> _AT_@ -153,15 +153,13 @@ main(int argc, char *argv[])
> if (ferror(fp))
> eprintf("fgetc %s:", *argv);
> clearerr(fp);
> - /* ignore error in case file was removed, we continue
> - * tracking the existing open file descriptor */
> - if (!stat(*argv, &st2)) {
> - if (st2.st_size < st1.st_size) {
> - fprintf(stderr, "%s: file truncated\n", *argv);
> - rewind(fp);
> - }
> - st1 = st2;
> + if (fstat(fileno(fp), &st2) < 0)
> + eprintf("fstat:");
> + if (st2.st_size < st1.st_size) {
> + fprintf(stderr, "%s: file truncated\n", *argv);
> + rewind(fp);
> }
> + st1 = st2;
> sleep(1);
> }
> }
> --
> 2.11.0
>
>

-- 
Kind regards,
Hiltjo
Received on Mon Dec 05 2016 - 10:20:49 CET

This archive was generated by hypermail 2.3.0 : Mon Dec 05 2016 - 10:24:43 CET