]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
test: add -N unary operator
authorBernhard Voelker <mail@bernhard-voelker.de>
Sun, 21 Oct 2018 22:54:51 +0000 (00:54 +0200)
committerBernhard Voelker <mail@bernhard-voelker.de>
Fri, 26 Oct 2018 09:05:25 +0000 (11:05 +0200)
Bash knows 'test -N FILE'.  Add it to GNU 'test' as well.

* src/test.c (unary_operator): Add a case for 'N'.
(usage): Document it.
* doc/coreutils.texi (node File characteristic tests): Likewise.
* NEWS (New features): Likewise.
* tests/misc/test-N.sh: Add a test.
* tests/local.mk (all_tests): Reference it.

NEWS
doc/coreutils.texi
src/test.c
tests/local.mk
tests/misc/test-N.sh [new file with mode: 0755]

diff --git a/NEWS b/NEWS
index a2b733d387b141daa2aa04e37e4f199491e42754..a8fa5f2e43acb9aac26fbb4bb779d843ff5dd73d 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -27,6 +27,9 @@ GNU coreutils NEWS                                    -*- outline -*-
 
   id now supports specifying multiple users.
 
+  test now supports the '-N FILE' unary operator (like e.g. bash) to check
+  whether FILE exists and has been modified since it was last read.
+
 
 * Noteworthy changes in release 8.30 (2018-07-01) [stable]
 
index 512443aa68b6bd6b52dd2527b84e06c995bf7a6b..30155bb86a348fba7825ce34edd1619dc79fa726 100644 (file)
@@ -13099,6 +13099,12 @@ True if @var{file1} is older (according to modification date) than
 True if @var{file1} and @var{file2} have the same device and inode
 numbers, i.e., if they are hard links to each other.
 
+@item -N @var{file}
+@opindex -N
+@cindex mtime-greater-atime file check
+True if @var{file} exists and has been modified (mtime) since it was
+last read (atime).
+
 @end table
 
 
index 9005b196223abe50e08c9f5854cd4f6f29bd65b0..7ed797f1791908bd5c120044ef79733b545ea960 100644 (file)
@@ -417,6 +417,16 @@ unary_operator (void)
       unary_advance ();
       return euidaccess (argv[pos - 1], X_OK) == 0;
 
+    case 'N':  /* File exists and has been modified since it was last read? */
+      {
+        unary_advance ();
+        if (stat (argv[pos - 1], &stat_buf) != 0)
+          return false;
+        struct timespec atime = get_stat_atime (&stat_buf);
+        struct timespec mtime = get_stat_mtime (&stat_buf);
+        return (timespec_cmp (mtime, atime) > 0);
+      }
+
     case 'O':                  /* File is owned by you? */
       {
         unary_advance ();
@@ -741,6 +751,7 @@ EXPRESSION is true or false and sets exit status.  It is one of:\n\
 "), stdout);
       fputs (_("\
   -L FILE     FILE exists and is a symbolic link (same as -h)\n\
+  -N FILE     FILE exists and has been modified since it was last read\n\
   -O FILE     FILE exists and is owned by the effective user ID\n\
   -p FILE     FILE exists and is a named pipe\n\
   -r FILE     FILE exists and read permission is granted\n\
index 47c6b95373c2d1ab8d36342083c24aab158c2113..84d346979781f88434d47bc697d1bdd8a8e6fe26 100644 (file)
@@ -408,6 +408,7 @@ all_tests =                                 \
   tests/misc/tac-2-nonseekable.sh              \
   tests/misc/tail.pl                           \
   tests/misc/tee.sh                            \
+  tests/misc/test-N.sh                         \
   tests/misc/test-diag.pl                      \
   tests/misc/time-style.sh                     \
   tests/misc/timeout.sh                                \
diff --git a/tests/misc/test-N.sh b/tests/misc/test-N.sh
new file mode 100755 (executable)
index 0000000..468182b
--- /dev/null
@@ -0,0 +1,42 @@
+#!/bin/sh
+# Test 'test -N file'.
+
+# Copyright (C) 2018 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ test
+
+# For a freshly touched file, atime should equal mtime: 'test -N' returns 1.
+touch file || framework_failure_
+stat file
+returns_ 1 env test -N file || fail=1
+
+# Set access time to 2 days ago: 'test -N' returns 0.
+touch -a -d "12:00 today -2 days" file || framework_failure_
+stat file
+env test -N file || fail=1
+
+# Set mtime to 2 days before atime: 'test -N' returns 1;
+touch -m -d "12:00 today -4 days" file || framework_failure_
+stat file
+returns_ 1 env test -N file || fail=1
+
+# Now modify the file: 'test -N' returns 0.
+> file || framework_failure_
+stat file
+env test -N file || fail=1
+
+Exit $fail