[hackers] [sbase] [PATCH v4 3/4] tail: Use fstat in case file is removed

From: Michael Forney <mforney_AT_mforney.org>
Date: Mon, 3 Jul 2017 14:58:51 -0700

---
 tail.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
diff --git a/tail.c b/tail.c
index 1ab9d18..ce65a01 100644
--- a/tail.c
+++ b/tail.c
_AT_@ -175,8 +175,8 @@ main(int argc, char *argv[])
 			}
 			if (many)
 				printf("%s==> %s <==\n", newline ? "\n" : "", *argv);
-			if (stat(*argv, &st1) < 0)
-				eprintf("stat %s:", *argv);
+			if (fstat(fd, &st1) < 0)
+				eprintf("fstat %s:", *argv);
 			if (!(S_ISFIFO(st1.st_mode) || S_ISREG(st1.st_mode)))
 				fflag = 0;
 			newline = 1;
_AT_@ -193,16 +193,14 @@ main(int argc, char *argv[])
 			for (;;) {
 				if (concat(fd, *argv, 1, "<stdout>") < 0)
 					exit(1);
-				/* 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);
-						if (lseek(fd, SEEK_SET, 0) < 0)
-							eprintf("lseek:");
-					}
-					st1 = st2;
+				if (fstat(fd, &st2) < 0)
+					eprintf("fstat %s:", *argv);
+				if (st2.st_size < st1.st_size) {
+					fprintf(stderr, "%s: file truncated\n", *argv);
+					if (lseek(fd, SEEK_SET, 0) < 0)
+						eprintf("lseek:");
 				}
+				st1 = st2;
 				sleep(1);
 			}
 		}
-- 
2.13.2
Received on Mon Jul 03 2017 - 23:58:51 CEST

This archive was generated by hypermail 2.3.0 : Tue Jul 04 2017 - 00:13:19 CEST