From e42a3db8aafb8bf431024c542a0c884059b97675 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Tue, 7 Jun 2011 16:01:33 -0700
Subject: [PATCH] stat-time: get_stat_birthtime failure is better-defined

* lib/stat-time.h (get_stat_birthtime): If the time is not available,
return a timestamp whose tv_sec and tv_nsec values are both -1.
Previously, the spec said only that the tv_nsec value was negative.
This upward-compatible change simplifies GNU tar a bit.
---
 ChangeLog       |  8 ++++++++
 lib/stat-time.h | 14 ++++++++------
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index af211551b5..149049044c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2011-06-07  Paul Eggert  <eggert@cs.ucla.edu>
+
+	stat-time: get_stat_birthtime failure is better-defined
+	* lib/stat-time.h (get_stat_birthtime): If the time is not available,
+	return a timestamp whose tv_sec and tv_nsec values are both -1.
+	Previously, the spec said only that the tv_nsec value was negative.
+	This upward-compatible change simplifies GNU tar a bit.
+
 2011-06-07  Eric Blake  <eblake@redhat.com>
 
 	strerror_r-posix: work around cygwin 1.7.9
diff --git a/lib/stat-time.h b/lib/stat-time.h
index d36a78ec54..86d9d4b8f7 100644
--- a/lib/stat-time.h
+++ b/lib/stat-time.h
@@ -142,7 +142,7 @@ get_stat_mtime (struct stat const *st)
 }
 
 /* Return *ST's birth time, if available; otherwise return a value
-   with negative tv_nsec.  */
+   with tv_sec and tv_nsec both equal to -1.  */
 static inline struct timespec
 get_stat_birthtime (struct stat const *st)
 {
@@ -161,7 +161,7 @@ get_stat_birthtime (struct stat const *st)
   t.tv_sec = st->st_ctime;
   t.tv_nsec = 0;
 #else
-  /* Birth time is not supported.  Set tv_sec to avoid undefined behavior.  */
+  /* Birth time is not supported.  */
   t.tv_sec = -1;
   t.tv_nsec = -1;
   /* Avoid a "parameter unused" warning.  */
@@ -175,10 +175,12 @@ get_stat_birthtime (struct stat const *st)
      using zero.  Attempt to work around this problem.  Alas, this can
      report failure even for valid time stamps.  Also, NetBSD
      sometimes returns junk in the birth time fields; work around this
-     bug if it is detected.  There's no need to detect negative
-     tv_nsec junk as negative tv_nsec already indicates an error.  */
-  if (t.tv_sec == 0 || 1000000000 <= t.tv_nsec)
-    t.tv_nsec = -1;
+     bug if it is detected.  */
+  if (! (t.tv_sec && 0 <= t.tv_nsec && t.tv_nsec < 1000000000))
+    {
+      t.tv_sec = -1;
+      t.tv_nsec = -1;
+    }
 #endif
 
   return t;
-- 
2.30.2