test-stat-time, test-utimens: improve portability
[pspp] / tests / test-lutimens.h
1 /* Test of file timestamp modification functions.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #include "test-utimens-common.h"
18
19 /* This file is designed to test both lutimens(a,b) and
20    utimensat(AT_FDCWD,a,b,AT_SYMLINK_NOFOLLOW).  FUNC is the function
21    to test.  Assumes that BASE and ASSERT are already defined.  If
22    PRINT, warn before skipping tests with status 77.  */
23 static int
24 test_lutimens (int (*func) (char const *, struct timespec const *), bool print)
25 {
26   int result;
27   struct stat st1;
28   struct stat st2;
29   bool atime_supported = true;
30
31   if (symlink ("nowhere", BASE "link"))
32     {
33       if (print)
34         fputs ("skipping test: symlinks not supported on this file system\n",
35                stderr);
36       return 77;
37     }
38   errno = 0;
39   result = func (BASE "link", NULL);
40   if (result == -1 && errno == ENOSYS)
41     {
42       ASSERT (unlink (BASE "link") == 0);
43       if (print)
44         fputs ("skipping test: "
45                "setting symlink time not supported on this file system\n",
46                stderr);
47       return 77;
48     }
49   ASSERT (!result);
50   ASSERT (lstat (BASE "link", &st1) == 0);
51   /* On cygwin, lstat() changes atime of symlinks, so that lutimens
52      can only effectively modify mtime.  */
53   nap ();
54   ASSERT (lstat (BASE "link", &st2) == 0);
55   if (st1.st_atime != st2.st_atime
56       || get_stat_atime_ns (&st1) != get_stat_atime_ns (&st2))
57     atime_supported = false;
58
59   /* Invalid arguments.  */
60   errno = 0;
61   ASSERT (func ("no_such", NULL) == -1);
62   ASSERT (errno == ENOENT);
63   errno = 0;
64   ASSERT (func ("", NULL) == -1);
65   ASSERT (errno == ENOENT);
66   {
67     struct timespec ts[2] = { { Y2K, UTIME_BOGUS_POS }, { Y2K, 0 } };
68     errno = 0;
69     ASSERT (func (BASE "link", ts) == -1);
70     ASSERT (errno == EINVAL);
71   }
72   {
73     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, UTIME_BOGUS_NEG } };
74     errno = 0;
75     ASSERT (func (BASE "link", ts) == -1);
76     ASSERT (errno == EINVAL);
77   }
78   ASSERT (lstat (BASE "link", &st2) == 0);
79   if (atime_supported)
80     {
81       ASSERT (st1.st_atime == st2.st_atime);
82       ASSERT (get_stat_atime_ns (&st1) == get_stat_atime_ns (&st2));
83     }
84   ASSERT (utimecmp (BASE "link", &st1, &st2, 0) == 0);
85
86   /* Set both times.  */
87   {
88     struct timespec ts[2] = { { Y2K, BILLION / 2 - 1 }, { Y2K, BILLION - 1 } };
89     ASSERT (func (BASE "link", ts) == 0);
90     ASSERT (lstat (BASE "link", &st2) == 0);
91     if (atime_supported)
92       {
93         ASSERT (st2.st_atime == Y2K);
94         ASSERT (0 <= get_stat_atime_ns (&st2));
95         ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
96       }
97     ASSERT (st2.st_mtime == Y2K);
98     ASSERT (0 <= get_stat_mtime_ns (&st2));
99     ASSERT (get_stat_mtime_ns (&st2) < BILLION);
100   }
101
102   /* Play with UTIME_OMIT, UTIME_NOW.  */
103   {
104     struct timespec ts[2] = { { BILLION, UTIME_OMIT }, { 0, UTIME_NOW } };
105     ASSERT (func (BASE "link", ts) == 0);
106     ASSERT (lstat (BASE "link", &st2) == 0);
107     if (atime_supported)
108       {
109         ASSERT (st2.st_atime == Y2K);
110         ASSERT (0 <= get_stat_atime_ns (&st2));
111         ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
112       }
113     ASSERT (utimecmp (BASE "link", &st1, &st2, 0) <= 0);
114   }
115
116   /* Cleanup.  */
117   ASSERT (unlink (BASE "link") == 0);
118   return 0;
119 }