utimens: let lutimens work on non-symlinks
[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   int saved_errno;
28   struct stat st1;
29   struct stat st2;
30   bool atime_supported = true;
31
32   /* Non-symlinks should be handled just like utimens.  */
33   errno = 0;
34   ASSERT (func ("no_such", NULL) == -1);
35   ASSERT (errno == ENOENT);
36   errno = 0;
37   ASSERT (func ("", NULL) == -1);
38   ASSERT (errno == ENOENT);
39   ASSERT (close (creat (BASE "file", 0600)) == 0);
40   ASSERT (stat (BASE "file", &st1) == 0);
41   ASSERT (st1.st_atime != Y2K);
42   ASSERT (st1.st_mtime != Y2K);
43   {
44     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
45     ASSERT (func (BASE "file", ts) == 0);
46   }
47   ASSERT (stat (BASE "file", &st1) == 0);
48   ASSERT (st1.st_atime == Y2K);
49   ASSERT (st1.st_mtime == Y2K);
50
51   /* Play with symlink timestamps.  */
52   if (symlink (BASE "file", BASE "link"))
53     {
54       ASSERT (unlink (BASE "file") == 0);
55       if (print)
56         fputs ("skipping test: symlinks not supported on this file system\n",
57                stderr);
58       return 77;
59     }
60   errno = 0;
61   result = func (BASE "link", NULL);
62   saved_errno = errno;
63   /* Make sure we did not reference through link by accident.  */
64   ASSERT (stat (BASE "file", &st1) == 0);
65   ASSERT (st1.st_atime == Y2K);
66   ASSERT (st1.st_mtime == Y2K);
67   ASSERT (lstat (BASE "link", &st1) == 0);
68   ASSERT (st1.st_atime != Y2K);
69   ASSERT (st1.st_mtime != Y2K);
70   ASSERT (unlink (BASE "file") == 0);
71   if (result == -1 && saved_errno == ENOSYS)
72     {
73       ASSERT (unlink (BASE "link") == 0);
74       if (print)
75         fputs ("skipping test: "
76                "setting symlink time not supported on this file system\n",
77                stderr);
78       return 77;
79     }
80   ASSERT (!result);
81   ASSERT (lstat (BASE "link", &st1) == 0);
82   /* On cygwin, lstat() changes atime of symlinks, so that lutimens
83      can only effectively modify mtime.  */
84   nap ();
85   ASSERT (lstat (BASE "link", &st2) == 0);
86   if (st1.st_atime != st2.st_atime
87       || get_stat_atime_ns (&st1) != get_stat_atime_ns (&st2))
88     atime_supported = false;
89
90   /* Invalid arguments.  */
91   {
92     struct timespec ts[2] = { { Y2K, UTIME_BOGUS_POS }, { Y2K, 0 } };
93     errno = 0;
94     ASSERT (func (BASE "link", ts) == -1);
95     ASSERT (errno == EINVAL);
96   }
97   {
98     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, UTIME_BOGUS_NEG } };
99     errno = 0;
100     ASSERT (func (BASE "link", ts) == -1);
101     ASSERT (errno == EINVAL);
102   }
103   ASSERT (lstat (BASE "link", &st2) == 0);
104   if (atime_supported)
105     {
106       ASSERT (st1.st_atime == st2.st_atime);
107       ASSERT (get_stat_atime_ns (&st1) == get_stat_atime_ns (&st2));
108     }
109   ASSERT (utimecmp (BASE "link", &st1, &st2, 0) == 0);
110
111   /* Set both times.  */
112   {
113     struct timespec ts[2] = { { Y2K, BILLION / 2 - 1 }, { Y2K, BILLION - 1 } };
114     ASSERT (func (BASE "link", ts) == 0);
115     ASSERT (lstat (BASE "link", &st2) == 0);
116     if (atime_supported)
117       {
118         ASSERT (st2.st_atime == Y2K);
119         ASSERT (0 <= get_stat_atime_ns (&st2));
120         ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
121       }
122     ASSERT (st2.st_mtime == Y2K);
123     ASSERT (0 <= get_stat_mtime_ns (&st2));
124     ASSERT (get_stat_mtime_ns (&st2) < BILLION);
125   }
126
127   /* Play with UTIME_OMIT, UTIME_NOW.  */
128   {
129     struct timespec ts[2] = { { BILLION, UTIME_OMIT }, { 0, UTIME_NOW } };
130     ASSERT (func (BASE "link", ts) == 0);
131     ASSERT (lstat (BASE "link", &st2) == 0);
132     if (atime_supported)
133       {
134         ASSERT (st2.st_atime == Y2K);
135         ASSERT (0 <= get_stat_atime_ns (&st2));
136         ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
137       }
138     ASSERT (utimecmp (BASE "link", &st1, &st2, 0) <= 0);
139   }
140
141   /* Cleanup.  */
142   ASSERT (unlink (BASE "link") == 0);
143   return 0;
144 }