1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
// Copyright (C) 2016-2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++17 -lstdc++fs" }
// { dg-do run { target c++17 } }
// { dg-require-filesystem-ts "" }
// 15.25 Permissions [fs.op.last_write_time]
#include <filesystem>
#include <testsuite_fs.h>
#include <testsuite_hooks.h>
#ifdef _GLIBCXX_HAVE_FCNTL_H
# include <fcntl.h>
#endif
#if _GLIBCXX_HAVE_UTIME_H
# include <utime.h>
#endif
using time_type = std::filesystem::file_time_type;
void
test01()
{
// read times
auto p = __gnu_test::nonexistent_path();
std::error_code ec;
time_type mtime = last_write_time(p, ec);
VERIFY( ec );
VERIFY( ec == std::make_error_code(std::errc::no_such_file_or_directory) );
#if __cpp_exceptions
bool caught = false;
try {
mtime = last_write_time(p);
} catch (std::system_error const& e) {
caught = true;
ec = e.code();
}
VERIFY( caught );
VERIFY( ec );
VERIFY( ec == std::make_error_code(std::errc::no_such_file_or_directory) );
#endif
__gnu_test::scoped_file file(p);
VERIFY( exists(p) );
mtime = last_write_time(p, ec);
VERIFY( !ec );
VERIFY( mtime <= time_type::clock::now() );
VERIFY( mtime == last_write_time(p) );
auto end_of_time = time_type::duration::max();
auto last_second
= std::chrono::duration_cast<std::chrono::seconds>(end_of_time).count();
if (last_second > std::numeric_limits<std::time_t>::max())
return; // can't test overflow
#if _GLIBCXX_USE_UTIMENSAT
struct ::timespec ts[2];
ts[0].tv_sec = 0;
ts[0].tv_nsec = UTIME_NOW;
ts[1].tv_sec = std::numeric_limits<std::time_t>::max() - 1;
ts[1].tv_nsec = 0;
VERIFY( !::utimensat(AT_FDCWD, p.c_str(), ts, 0) );
#elif _GLIBCXX_HAVE_UTIME_H
::utimbuf times;
times.modtime = std::numeric_limits<std::time_t>::max() - 1;
times.actime = std::numeric_limits<std::time_t>::max() - 1;
VERIFY( !::utime(p.string().c_str(), ×) );
#else
return;
#endif
mtime = last_write_time(p, ec);
VERIFY( ec );
VERIFY( ec == std::make_error_code(std::errc::value_too_large) );
VERIFY( mtime == time_type::min() );
#if __cpp_exceptions
caught = false;
try {
mtime = last_write_time(p);
} catch (std::system_error const& e) {
caught = true;
ec = e.code();
}
VERIFY( caught );
VERIFY( ec );
VERIFY( ec == std::make_error_code(std::errc::value_too_large) );
#endif
}
bool approx_equal(time_type file_time, time_type expected)
{
auto delta = expected - file_time;
if (delta < delta.zero())
delta = -delta;
return delta < std::chrono::seconds(1);
}
void
test02()
{
// write times
const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
__gnu_test::scoped_file f;
std::error_code ec;
time_type time;
time = last_write_time(f.path);
ec = bad_ec;
last_write_time(f.path, time, ec);
VERIFY( !ec );
VERIFY( approx_equal(last_write_time(f.path), time) );
ec = bad_ec;
time -= std::chrono::milliseconds(1000 * 60 * 10 + 15);
last_write_time(f.path, time, ec);
VERIFY( !ec );
VERIFY( approx_equal(last_write_time(f.path), time) );
ec = bad_ec;
time += std::chrono::milliseconds(1000 * 60 * 20 + 15);
last_write_time(f.path, time, ec);
VERIFY( !ec );
VERIFY( approx_equal(last_write_time(f.path), time) );
ec = bad_ec;
time = time_type();
last_write_time(f.path, time, ec);
VERIFY( !ec );
VERIFY( approx_equal(last_write_time(f.path), time) );
ec = bad_ec;
time -= std::chrono::milliseconds(1000 * 60 * 10 + 15);
last_write_time(f.path, time, ec);
VERIFY( !ec );
VERIFY( approx_equal(last_write_time(f.path), time) );
}
int
main()
{
test01();
test02();
}
|