]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/experimental/filesystem/operations/last_write_time.cc
libstdc++: testsuite: skip fs space tests on dummy implementations
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / filesystem / operations / last_write_time.cc
CommitLineData
7adcbafe 1// Copyright (C) 2016-2022 Free Software Foundation, Inc.
fd5effb1
JW
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
ef0e80d2 18// { dg-options "-DUSE_FILESYSTEM_TS -lstdc++fs" }
fd5effb1
JW
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>
a51a546c 25#include <limits>
fd5effb1
JW
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
a51a546c 35#include <stdio.h>
fd5effb1 36
69af1c04
JW
37using time_type = std::experimental::filesystem::file_time_type;
38
a51a546c
JW
39namespace chrono = std::chrono;
40
fd5effb1
JW
41void
42test01()
43{
7195dfe9
JW
44 // read times
45
fd5effb1
JW
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
a51a546c 73 = chrono::duration_cast<chrono::seconds>(end_of_time).count();
fd5effb1 74 if (last_second > std::numeric_limits<std::time_t>::max())
a51a546c
JW
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 }
fd5effb1 80
a51a546c 81 // Set mtime to a date past the maximum possible file_time_type:
fd5effb1
JW
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;
9534a5e6 93 VERIFY( !::utime(p.string().c_str(), &times) );
fd5effb1 94#else
a51a546c 95 puts("No utimensat or utime, giving up.");
fd5effb1
JW
96 return;
97#endif
98
a51a546c 99 // Try to read back the impossibly-large mtime:
fd5effb1 100 mtime = last_write_time(p, ec);
a51a546c
JW
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.");
fd5effb1
JW
111
112#if __cpp_exceptions
a51a546c 113 // Once more, with exceptions:
fd5effb1 114 try {
a51a546c
JW
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 );
fd5effb1 122 }
fd5effb1
JW
123#endif
124}
125
69af1c04
JW
126bool approx_equal(time_type file_time, time_type expected)
127{
128 auto delta = expected - file_time;
129 if (delta < delta.zero())
130 delta = -delta;
a51a546c 131 return delta < chrono::seconds(1);
69af1c04
JW
132}
133
7195dfe9
JW
134void
135test02()
136{
137 // write times
138
a51a546c 139 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
7195dfe9
JW
140 __gnu_test::scoped_file f;
141 std::error_code ec;
142 time_type time;
143
a51a546c 144 ec = bad_ec;
7195dfe9
JW
145 time = last_write_time(f.path);
146 last_write_time(f.path, time, ec);
147 VERIFY( !ec );
69af1c04 148 VERIFY( approx_equal(last_write_time(f.path), time) );
7195dfe9 149
a51a546c
JW
150 ec = bad_ec;
151 time -= chrono::milliseconds(1000 * 60 * 10 + 15);
7195dfe9
JW
152 last_write_time(f.path, time, ec);
153 VERIFY( !ec );
69af1c04 154 VERIFY( approx_equal(last_write_time(f.path), time) );
7195dfe9 155
a51a546c
JW
156 ec = bad_ec;
157 time += chrono::milliseconds(1000 * 60 * 20 + 15);
7195dfe9
JW
158 last_write_time(f.path, time, ec);
159 VERIFY( !ec );
69af1c04 160 VERIFY( approx_equal(last_write_time(f.path), time) );
7195dfe9 161
a51a546c 162 ec = bad_ec;
7195dfe9
JW
163 time = time_type();
164 last_write_time(f.path, time, ec);
165 VERIFY( !ec );
69af1c04 166 VERIFY( approx_equal(last_write_time(f.path), time) );
7195dfe9 167
a51a546c
JW
168 ec = bad_ec;
169 time -= chrono::milliseconds(1000 * 60 * 10 + 15);
7195dfe9
JW
170 last_write_time(f.path, time, ec);
171 VERIFY( !ec );
69af1c04 172 VERIFY( approx_equal(last_write_time(f.path), time) );
7195dfe9
JW
173}
174
fd5effb1
JW
175int
176main()
177{
178 test01();
7195dfe9 179 test02();
fd5effb1 180}