1#!/bin/sh
2# Test 'test -N file'.
3
4# Copyright (C) 2018-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ test stat
21
22stat_test_N() {
23  mtime=$(env stat -c '%.Y' "$1")
24  atime=$(env stat -c '%.X' "$1")
25  test "$mtime" = "$atime" && return 1
26  latest=$(printf '%s\n' "$mtime" "$atime" | sort -g | tail -n1)
27  test "$mtime" = "$latest"
28}
29
30# For a freshly touched file, atime should equal mtime: 'test -N' returns 1.
31touch file || framework_failure_
32if ! stat_test_N file; then
33  returns_ 1 env test -N file || fail=1
34fi
35
36# Set access time to 2 days ago: 'test -N' returns 0.
37touch -a -d '12:00 today -2 days' file || framework_failure_
38if stat_test_N file; then
39  env test -N file || fail=1
40fi
41
42# Set mtime to 2 days before atime: 'test -N' returns 1;
43touch -m -d '12:00 today -4 days' file || framework_failure_
44if ! stat_test_N file; then
45  returns_ 1 env test -N file || fail=1
46fi
47
48# Now modify the file: 'test -N' returns 0.
49echo 'data' > file || framework_failure_
50if stat_test_N file; then
51  env test -N file || fail=1
52fi
53
54Exit $fail
55