utimens: let lutimens work on non-symlinks
[gnulib.git] / tests / test-utimens.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 utimens(a,b) and
20    utimensat(AT_FDCWD,a,b,0).  FUNC is the function to test.  Assumes
21    that BASE and ASSERT are already defined.  If PRINT, warn before
22    skipping tests with status 77.  */
23 static int
24 test_utimens (int (*func) (char const *, struct timespec const *), bool print)
25 {
26   struct stat st1;
27   struct stat st2;
28
29   ASSERT (close (creat (BASE "file", 0600)) == 0);
30   /* If utimens truncates to less resolution than the file system
31      supports, then time can appear to go backwards between now and a
32      follow-up utimens with UTIME_NOW or a NULL timespec.  Use
33      UTIMECMP_TRUNCATE_SOURCE to compensate, with st1 as the
34      source.  */
35   ASSERT (stat (BASE "file", &st1) == 0);
36   nap ();
37   ASSERT (func (BASE "file", NULL) == 0);
38   ASSERT (stat (BASE "file", &st2) == 0);
39   ASSERT (0 <= utimecmp (BASE "file", &st2, &st1, UTIMECMP_TRUNCATE_SOURCE));
40   {
41     /* On some NFS systems, the 'now' timestamp of creat or a NULL
42        timespec is determined by the server, but the 'now' timestamp
43        determined by gettime() (as is done when using UTIME_NOW) is
44        determined by the client; since the two machines are not
45        necessarily on the same clock, this is another case where time
46        can appear to go backwards.  The rest of this test cares about
47        client time, so manually use gettime() to set both times.  */
48     struct timespec ts[2];
49     gettime (&ts[0]);
50     ts[1] = ts[0];
51     ASSERT (func (BASE "file", ts) == 0);
52     ASSERT (stat (BASE "file", &st1) == 0);
53     nap ();
54   }
55
56   /* Invalid arguments.  */
57   errno = 0;
58   ASSERT (func ("no_such", NULL) == -1);
59   ASSERT (errno == ENOENT);
60   errno = 0;
61   ASSERT (func ("", NULL) == -1);
62   ASSERT (errno == ENOENT);
63   {
64     struct timespec ts[2] = { { Y2K, UTIME_BOGUS_POS }, { Y2K, 0 } };
65     errno = 0;
66     ASSERT (func (BASE "file", ts) == -1);
67     ASSERT (errno == EINVAL);
68   }
69   {
70     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, UTIME_BOGUS_NEG } };
71     errno = 0;
72     ASSERT (func (BASE "file", ts) == -1);
73     ASSERT (errno == EINVAL);
74   }
75   ASSERT (stat (BASE "file", &st2) == 0);
76   ASSERT (st1.st_atime == st2.st_atime);
77   ASSERT (get_stat_atime_ns (&st1) == get_stat_atime_ns (&st2));
78   ASSERT (utimecmp (BASE "file", &st1, &st2, 0) == 0);
79
80   /* Set both times.  */
81   {
82     struct timespec ts[2] = { { Y2K, BILLION / 2 - 1 }, { Y2K, BILLION - 1 } };
83     ASSERT (func (BASE "file", ts) == 0);
84     ASSERT (stat (BASE "file", &st2) == 0);
85     ASSERT (st2.st_atime == Y2K);
86     ASSERT (0 <= get_stat_atime_ns (&st2));
87     ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
88     ASSERT (st2.st_mtime == Y2K);
89     ASSERT (0 <= get_stat_mtime_ns (&st2));
90     ASSERT (get_stat_mtime_ns (&st2) < BILLION);
91   }
92
93   /* Play with UTIME_OMIT, UTIME_NOW.  */
94   {
95     struct timespec ts[2] = { { BILLION, UTIME_OMIT }, { 0, UTIME_NOW } };
96     ASSERT (func (BASE "file", ts) == 0);
97     ASSERT (stat (BASE "file", &st2) == 0);
98     ASSERT (st2.st_atime == Y2K);
99     ASSERT (0 <= get_stat_atime_ns (&st2));
100     ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
101     /* See comment above about this utimecmp call.  */
102     ASSERT (0 <= utimecmp (BASE "file", &st2, &st1, UTIMECMP_TRUNCATE_SOURCE));
103   }
104
105   /* Make sure this dereferences symlinks.  */
106   if (symlink (BASE "file", BASE "link"))
107     {
108        ASSERT (unlink (BASE "file") == 0);
109       if (print)
110         fputs ("skipping test: symlinks not supported on this file system\n",
111                stderr);
112       return 77;
113     }
114   ASSERT (lstat (BASE "link", &st1) == 0);
115   ASSERT (st1.st_mtime != Y2K);
116   {
117     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
118     ASSERT (func (BASE "link", ts) == 0);
119     ASSERT (lstat (BASE "link", &st2) == 0);
120     /* Can't compare atimes, since lstat() changes symlink atime on cygwin.  */
121     ASSERT (st1.st_mtime == st2.st_mtime);
122     ASSERT (stat (BASE "link", &st2) == 0);
123     ASSERT (st2.st_mtime == Y2K);
124     ASSERT (get_stat_mtime_ns (&st2) == 0);
125   }
126
127   /* Cleanup.  */
128   ASSERT (unlink (BASE "link") == 0);
129   ASSERT (unlink (BASE "file") == 0);
130   return 0;
131 }