From hburch@lumeta.com  Tue Jun 11 11:53:55 2002
Return-Path: <hburch@lumeta.com>
Received: from exgw2.lumeta.com (exgw2.lumeta.com [65.198.68.66])
	by hub.freebsd.org (Postfix) with ESMTP id 0042337B40F
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 11 Jun 2002 11:53:53 -0700 (PDT)
Received: from lucy.corp.lumeta.com (h65-198-68-133.lumeta.com [65.198.68.133])
	by exgw2.lumeta.com (Postfix) with ESMTP id 682D8373834
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 11 Jun 2002 14:53:53 -0400 (EDT)
Received: from localhost (localhost [127.0.0.1])
	by lucy.corp.lumeta.com (Postfix) with ESMTP id F03601084A
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 11 Jun 2002 14:53:52 -0400 (EDT)
Received: from lumeta.com (hburch.corp.lumeta.com [65.198.68.240])
	by lucy.corp.lumeta.com (Postfix) with ESMTP id 6704A10849
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 11 Jun 2002 14:53:49 -0400 (EDT)
Received: by lumeta.com (Postfix, from userid 1000)
	id A46459340B; Tue, 11 Jun 2002 14:53:55 -0400 (EDT)
Message-Id: <20020611185355.A46459340B@lumeta.com>
Date: Tue, 11 Jun 2002 14:53:55 -0400 (EDT)
From: Hal Burch <hburch@lumeta.com>
Reply-To: Hal Burch <hburch@lumeta.com>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: -nt/-ot in test(1) does not detect if tv_nsec are different
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         39163
>Category:       bin
>Synopsis:       -nt/-ot in test(1) does not detect if tv_nsec are different
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    dwmalone
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Jun 11 12:00:08 PDT 2002
>Closed-Date:    Sun Aug 11 06:41:19 PDT 2002
>Last-Modified:  Sun Aug 11 06:41:19 PDT 2002
>Originator:     Hal Burch
>Release:        FreeBSD 5.0-CURRENT i386
>Organization:
Lumeta Corporation
>Environment:
System: FreeBSD hburch-lap.corp.lumeta.com 5.0-CURRENT FreeBSD 5.0-CURRENT #5: Tue May 7 18:46:05 EDT 2002 hburch@hburch-lap.corp.lumeta.com:/usr/src/sys/i386/compile/LOCAL i386
(Testing on 4.3-RELEASE, 4.5-RELEASE, 4.1.1-RELEASE all had the same behavior)
>Description:
Because newerf only checks st_mtime, if the files have the same value for
st_mtimespec.tv_sec but not for st_mtimespec.tv_nsec, test will improperly
say that the files have the same modification time.

This may be required for POSIX conformance, but that seems unlikely.
>How-To-Repeat:
 $ touch a ; echo pass time; touch b
Expect: pass time
Received: pass time
 $ test b -nt a && echo NEWER
Expect: NEWER
Received: [blank] (if the second does not increment, which is ~99.7%
                   of the time on my system)
>Fix:
Add code to newerf to check both st_mtimespec.tv_sec and
st_mtimespec.tv_nsec.  The old way was using the st_mtime hook, so
switched it to st_mtimespec.tv_sec for consistency.  Change olderf
to just a call to newerf with the parameters reversed to reduce
code duplication.

Diff from current head (src/bin/test/test.c 1.47)

diff -c /usr/src/bin/test/test.c ./test.c
*** /usr/src/bin/test/test.c	Mon Jun 10 21:43:24 2002
--- ./test.c	Mon Jun 10 21:56:13 2002
***************
*** 520,540 ****
  static int
  newerf (const char *f1, const char *f2)
  {
! 	struct stat b1, b2;
! 
! 	return (stat (f1, &b1) == 0 &&
! 		stat (f2, &b2) == 0 &&
! 		b1.st_mtime > b2.st_mtime);
  }
  
  static int
  olderf (const char *f1, const char *f2)
  {
! 	struct stat b1, b2;
! 
! 	return (stat (f1, &b1) == 0 &&
! 		stat (f2, &b2) == 0 &&
! 		b1.st_mtime < b2.st_mtime);
  }
  
  static int
--- 520,540 ----
  static int
  newerf (const char *f1, const char *f2)
  {
!  	if (stat(f1, &b1) != 0) return 0;
!  	if (stat(f2, &b2) != 0) return 0;
!  
!  	if (b1.st_mtimespec.tv_sec > b2.st_mtimespec.tv_sec)
!  		return 1;
!  	if (b1.st_mtimespec.tv_sec < b2.st_mtimespec.tv_sec)
!  		return 0;
!  
!  	return (b1.st_mtimespec.tv_nsec > b2.st_mtimespec.tv_nsec);
  }
  
  static int
  olderf (const char *f1, const char *f2)
  {
! 	return newerf(f2, f1);
  }
  
  static int
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->dwmalone 
Responsible-Changed-By: dwmalone 
Responsible-Changed-When: Fri Jun 14 15:22:55 PDT 2002 
Responsible-Changed-Why:  
Patch looks sensible - I'll have a look at it. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=39163 
State-Changed-From-To: open->closed 
State-Changed-By: dwmalone 
State-Changed-When: Sun Aug 11 06:40:53 PDT 2002 
State-Changed-Why:  
Patch applied to -current and -stable. Thanks. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=39163 
>Unformatted:
