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

From: Michael Forney <mforney_AT_mforney.org>
Date: Sun, 4 Dec 2016 21:55:11 -0800

---
 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);
 			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
Received on Mon Dec 05 2016 - 06:55:11 CET

This archive was generated by hypermail 2.3.0 : Mon Dec 05 2016 - 07:00:51 CET