debian/changelog: new version
[gnulib.git] / tests / test-utimens.h
1 /* Test of file timestamp modification functions.
2    Copyright (C) 2009-2011 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 3 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   if (check_ctime)
41     ASSERT (st1.st_ctime < st2.st_ctime
42             || (st1.st_ctime == st2.st_ctime
43                 && get_stat_ctime_ns (&st1) < get_stat_ctime_ns (&st2)));
44   {
45     /* On some NFS systems, the 'now' timestamp of creat or a NULL
46        timespec is determined by the server, but the 'now' timestamp
47        determined by gettime() (as is done when using UTIME_NOW) is
48        determined by the client; since the two machines are not
49        necessarily on the same clock, this is another case where time
50        can appear to go backwards.  The rest of this test cares about
51        client time, so manually use gettime() to set both times.  */
52     struct timespec ts[2];
53     gettime (&ts[0]);
54     ts[1] = ts[0];
55     ASSERT (func (BASE "file", ts) == 0);
56     ASSERT (stat (BASE "file", &st1) == 0);
57     nap ();
58   }
59
60   /* Invalid arguments.  */
61   errno = 0;
62   ASSERT (func ("no_such", NULL) == -1);
63   ASSERT (errno == ENOENT);
64   errno = 0;
65   ASSERT (func ("no_such/", NULL) == -1);
66   ASSERT (errno == ENOENT || errno == ENOTDIR);
67   errno = 0;
68   ASSERT (func ("", NULL) == -1);
69   ASSERT (errno == ENOENT);
70   {
71     struct timespec ts[2] = { { Y2K, UTIME_BOGUS_POS }, { Y2K, 0 } };
72     errno = 0;
73     ASSERT (func (BASE "file", ts) == -1);
74     ASSERT (errno == EINVAL);
75   }
76   {
77     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, UTIME_BOGUS_NEG } };
78     errno = 0;
79     ASSERT (func (BASE "file", ts) == -1);
80     ASSERT (errno == EINVAL);
81   }
82   {
83     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
84     errno = 0;
85     ASSERT (func (BASE "file/", ts) == -1);
86     ASSERT (errno == ENOTDIR || errno == EINVAL);
87   }
88   ASSERT (stat (BASE "file", &st2) == 0);
89   ASSERT (st1.st_atime == st2.st_atime);
90   ASSERT (get_stat_atime_ns (&st1) == get_stat_atime_ns (&st2));
91   ASSERT (utimecmp (BASE "file", &st1, &st2, 0) == 0);
92
93   /* Set both times.  */
94   {
95     struct timespec ts[2] = { { Y2K, BILLION / 2 - 1 }, { Y2K, BILLION - 1 } };
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     ASSERT (st2.st_mtime == Y2K);
102     ASSERT (0 <= get_stat_mtime_ns (&st2));
103     ASSERT (get_stat_mtime_ns (&st2) < BILLION);
104     if (check_ctime)
105       ASSERT (st1.st_ctime < st2.st_ctime
106               || (st1.st_ctime == st2.st_ctime
107                   && get_stat_ctime_ns (&st1) < get_stat_ctime_ns (&st2)));
108   }
109
110   /* Play with UTIME_OMIT, UTIME_NOW.  */
111   {
112     struct stat st3;
113     struct timespec ts[2] = { { BILLION, UTIME_OMIT }, { 0, UTIME_NOW } };
114     nap ();
115     ASSERT (func (BASE "file", ts) == 0);
116     ASSERT (stat (BASE "file", &st3) == 0);
117     ASSERT (st3.st_atime == Y2K);
118     ASSERT (0 <= get_stat_atime_ns (&st3));
119     ASSERT (get_stat_atime_ns (&st3) < BILLION / 2);
120     /* See comment above about this utimecmp call.  */
121     ASSERT (0 <= utimecmp (BASE "file", &st3, &st1, UTIMECMP_TRUNCATE_SOURCE));
122     if (check_ctime)
123       ASSERT (st2.st_ctime < st3.st_ctime
124               || (st2.st_ctime == st3.st_ctime
125                   && get_stat_ctime_ns (&st2) < get_stat_ctime_ns (&st3)));
126     nap ();
127     ts[0].tv_nsec = 0;
128     ts[1].tv_nsec = UTIME_OMIT;
129     ASSERT (func (BASE "file", ts) == 0);
130     ASSERT (stat (BASE "file", &st2) == 0);
131     ASSERT (st2.st_atime == BILLION);
132     ASSERT (get_stat_atime_ns (&st2) == 0);
133     ASSERT (st3.st_mtime == st2.st_mtime);
134     ASSERT (get_stat_mtime_ns (&st3) == get_stat_mtime_ns (&st2));
135     if (check_ctime)
136       ASSERT (st3.st_ctime < st2.st_ctime
137               || (st3.st_ctime == st2.st_ctime
138                   && get_stat_ctime_ns (&st3) < get_stat_ctime_ns (&st2)));
139   }
140
141   /* Make sure this dereferences symlinks.  */
142   if (symlink (BASE "file", BASE "link"))
143     {
144       ASSERT (unlink (BASE "file") == 0);
145       if (print)
146         fputs ("skipping test: symlinks not supported on this file system\n",
147                stderr);
148       return 77;
149     }
150   ASSERT (lstat (BASE "link", &st1) == 0);
151   ASSERT (st1.st_mtime != Y2K);
152   errno = 0;
153   ASSERT (func (BASE "link/", NULL) == -1);
154   ASSERT (errno == ENOTDIR);
155   {
156     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
157     ASSERT (func (BASE "link", ts) == 0);
158     ASSERT (lstat (BASE "link", &st2) == 0);
159     /* Can't compare atimes, since lstat() changes symlink atime on cygwin.  */
160     ASSERT (st1.st_mtime == st2.st_mtime);
161     ASSERT (stat (BASE "link", &st2) == 0);
162     ASSERT (st2.st_mtime == Y2K);
163     ASSERT (get_stat_mtime_ns (&st2) == 0);
164   }
165
166   /* Cleanup.  */
167   ASSERT (unlink (BASE "link") == 0);
168   ASSERT (unlink (BASE "file") == 0);
169   return 0;
170 }