1 // Copyright (C) 2016-2018 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-options "-DUSE_FILESYSTEM_TS -lstdc++fs" }
19 // { dg-do run { target c++11 } }
20 // { dg-require-filesystem-ts "" }
22 // 15.25 Permissions [fs.op.last_write_time]
24 #include <experimental/filesystem>
25 #include <testsuite_fs.h>
26 #include <testsuite_hooks.h>
28 #ifdef _GLIBCXX_HAVE_FCNTL_H
31 #if _GLIBCXX_HAVE_UTIME_H
35 using time_type
= std::experimental::filesystem::file_time_type
;
42 auto p
= __gnu_test::nonexistent_path();
44 time_type mtime
= last_write_time(p
, ec
);
46 VERIFY( ec
== std::make_error_code(std::errc::no_such_file_or_directory
) );
50 mtime
= last_write_time(p
);
51 } catch (std::system_error
const& e
) {
57 VERIFY( ec
== std::make_error_code(std::errc::no_such_file_or_directory
) );
60 __gnu_test::scoped_file
file(p
);
62 mtime
= last_write_time(p
, ec
);
64 VERIFY( mtime
<= time_type::clock::now() );
65 VERIFY( mtime
== last_write_time(p
) );
67 auto end_of_time
= time_type::duration::max();
69 = std::chrono::duration_cast
<std::chrono::seconds
>(end_of_time
).count();
70 if (last_second
> std::numeric_limits
<std::time_t>::max())
71 return; // can't test overflow
73 #if _GLIBCXX_USE_UTIMENSAT
74 struct ::timespec ts
[2];
76 ts
[0].tv_nsec
= UTIME_NOW
;
77 ts
[1].tv_sec
= std::numeric_limits
<std::time_t>::max() - 1;
79 VERIFY( !::utimensat(AT_FDCWD
, p
.c_str(), ts
, 0) );
80 #elif _GLIBCXX_HAVE_UTIME_H
82 times
.modtime
= std::numeric_limits
<std::time_t>::max() - 1;
83 times
.actime
= std::numeric_limits
<std::time_t>::max() - 1;
84 VERIFY( !::utime(p
.c_str(), ×
) );
89 mtime
= last_write_time(p
, ec
);
91 VERIFY( ec
== std::make_error_code(std::errc::value_too_large
) );
92 VERIFY( mtime
== time_type::min() );
97 mtime
= last_write_time(p
);
98 } catch (std::system_error
const& e
) {
104 VERIFY( ec
== std::make_error_code(std::errc::value_too_large
) );
108 bool approx_equal(time_type file_time
, time_type expected
)
110 auto delta
= expected
- file_time
;
111 if (delta
< delta
.zero())
113 return delta
< std::chrono::seconds(1);
121 __gnu_test::scoped_file f
;
125 time
= last_write_time(f
.path
);
126 last_write_time(f
.path
, time
, ec
);
128 VERIFY( approx_equal(last_write_time(f
.path
), time
) );
130 time
-= std::chrono::milliseconds(1000 * 60 * 10 + 15);
131 last_write_time(f
.path
, time
, ec
);
133 VERIFY( approx_equal(last_write_time(f
.path
), time
) );
135 time
+= std::chrono::milliseconds(1000 * 60 * 20 + 15);
136 last_write_time(f
.path
, time
, ec
);
138 VERIFY( approx_equal(last_write_time(f
.path
), time
) );
141 last_write_time(f
.path
, time
, ec
);
143 VERIFY( approx_equal(last_write_time(f
.path
), time
) );
145 time
-= std::chrono::milliseconds(1000 * 60 * 10 + 15);
146 last_write_time(f
.path
, time
, ec
);
148 VERIFY( approx_equal(last_write_time(f
.path
), time
) );