]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/filesystem/operations/last_write_time.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / filesystem / operations / last_write_time.cc
1 // Copyright (C) 2016-2021 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library 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 along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-options "-DUSE_FILESYSTEM_TS -lstdc++fs" }
19 // { dg-do run { target c++11 } }
20 // { dg-require-filesystem-ts "" }
21
22 // 15.25 Permissions [fs.op.last_write_time]
23
24 #include <experimental/filesystem>
25 #include <limits>
26 #include <testsuite_fs.h>
27 #include <testsuite_hooks.h>
28
29 #ifdef _GLIBCXX_HAVE_FCNTL_H
30 # include <fcntl.h>
31 #endif
32 #if _GLIBCXX_HAVE_UTIME_H
33 # include <utime.h>
34 #endif
35 #include <stdio.h>
36
37 using time_type = std::experimental::filesystem::file_time_type;
38
39 namespace chrono = std::chrono;
40
41 void
42 test01()
43 {
44 // read times
45
46 auto p = __gnu_test::nonexistent_path();
47 std::error_code ec;
48 time_type mtime = last_write_time(p, ec);
49 VERIFY( ec );
50 VERIFY( ec == std::make_error_code(std::errc::no_such_file_or_directory) );
51 #if __cpp_exceptions
52 bool caught = false;
53 try {
54 mtime = last_write_time(p);
55 } catch (std::system_error const& e) {
56 caught = true;
57 ec = e.code();
58 }
59 VERIFY( caught );
60 VERIFY( ec );
61 VERIFY( ec == std::make_error_code(std::errc::no_such_file_or_directory) );
62 #endif
63
64 __gnu_test::scoped_file file(p);
65 VERIFY( exists(p) );
66 mtime = last_write_time(p, ec);
67 VERIFY( !ec );
68 VERIFY( mtime <= time_type::clock::now() );
69 VERIFY( mtime == last_write_time(p) );
70
71 auto end_of_time = time_type::duration::max();
72 auto last_second
73 = chrono::duration_cast<chrono::seconds>(end_of_time).count();
74 if (last_second > std::numeric_limits<std::time_t>::max())
75 {
76 puts("Range of time_t is smaller than range of chrono::file_clock, "
77 "can't test for overflow on this target.");
78 return;
79 }
80
81 // Set mtime to a date past the maximum possible file_time_type:
82 #if _GLIBCXX_USE_UTIMENSAT
83 struct ::timespec ts[2];
84 ts[0].tv_sec = 0;
85 ts[0].tv_nsec = UTIME_NOW;
86 ts[1].tv_sec = std::numeric_limits<std::time_t>::max() - 1;
87 ts[1].tv_nsec = 0;
88 VERIFY( !::utimensat(AT_FDCWD, p.c_str(), ts, 0) );
89 #elif _GLIBCXX_HAVE_UTIME_H
90 ::utimbuf times;
91 times.modtime = std::numeric_limits<std::time_t>::max() - 1;
92 times.actime = std::numeric_limits<std::time_t>::max() - 1;
93 VERIFY( !::utime(p.string().c_str(), &times) );
94 #else
95 puts("No utimensat or utime, giving up.");
96 return;
97 #endif
98
99 // Try to read back the impossibly-large mtime:
100 mtime = last_write_time(p, ec);
101 // Some filesystems (e.g. XFS) silently truncate distant times to
102 // the time_t epochalypse, Jan 19 2038, so we won't get an error when
103 // reading it back:
104 if (ec)
105 {
106 VERIFY( ec == std::make_error_code(std::errc::value_too_large) );
107 VERIFY( mtime == time_type::min() );
108 }
109 else
110 puts("No overflow error, filesystem may not support 64-bit time_t.");
111
112 #if __cpp_exceptions
113 // Once more, with exceptions:
114 try {
115 auto mtime2 = last_write_time(p);
116 // If it didn't throw, expect to have read back the same value:
117 VERIFY( mtime2 == mtime );
118 } catch (std::experimental::filesystem::filesystem_error const& e) {
119 // If it did throw, expect the error_code to be the same:
120 VERIFY( e.code() == ec );
121 VERIFY( e.path1() == p );
122 }
123 #endif
124 }
125
126 bool approx_equal(time_type file_time, time_type expected)
127 {
128 auto delta = expected - file_time;
129 if (delta < delta.zero())
130 delta = -delta;
131 return delta < chrono::seconds(1);
132 }
133
134 void
135 test02()
136 {
137 // write times
138
139 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
140 __gnu_test::scoped_file f;
141 std::error_code ec;
142 time_type time;
143
144 ec = bad_ec;
145 time = last_write_time(f.path);
146 last_write_time(f.path, time, ec);
147 VERIFY( !ec );
148 VERIFY( approx_equal(last_write_time(f.path), time) );
149
150 ec = bad_ec;
151 time -= chrono::milliseconds(1000 * 60 * 10 + 15);
152 last_write_time(f.path, time, ec);
153 VERIFY( !ec );
154 VERIFY( approx_equal(last_write_time(f.path), time) );
155
156 ec = bad_ec;
157 time += chrono::milliseconds(1000 * 60 * 20 + 15);
158 last_write_time(f.path, time, ec);
159 VERIFY( !ec );
160 VERIFY( approx_equal(last_write_time(f.path), time) );
161
162 ec = bad_ec;
163 time = time_type();
164 last_write_time(f.path, time, ec);
165 VERIFY( !ec );
166 VERIFY( approx_equal(last_write_time(f.path), time) );
167
168 ec = bad_ec;
169 time -= chrono::milliseconds(1000 * 60 * 10 + 15);
170 last_write_time(f.path, time, ec);
171 VERIFY( !ec );
172 VERIFY( approx_equal(last_write_time(f.path), time) );
173 }
174
175 int
176 main()
177 {
178 test01();
179 test02();
180 }