]> 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-2019 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 <testsuite_fs.h>
26 #include <testsuite_hooks.h>
27
28 #ifdef _GLIBCXX_HAVE_FCNTL_H
29 # include <fcntl.h>
30 #endif
31 #if _GLIBCXX_HAVE_UTIME_H
32 # include <utime.h>
33 #endif
34
35 using time_type = std::experimental::filesystem::file_time_type;
36
37 void
38 test01()
39 {
40 // read times
41
42 auto p = __gnu_test::nonexistent_path();
43 std::error_code ec;
44 time_type mtime = last_write_time(p, ec);
45 VERIFY( ec );
46 VERIFY( ec == std::make_error_code(std::errc::no_such_file_or_directory) );
47 #if __cpp_exceptions
48 bool caught = false;
49 try {
50 mtime = last_write_time(p);
51 } catch (std::system_error const& e) {
52 caught = true;
53 ec = e.code();
54 }
55 VERIFY( caught );
56 VERIFY( ec );
57 VERIFY( ec == std::make_error_code(std::errc::no_such_file_or_directory) );
58 #endif
59
60 __gnu_test::scoped_file file(p);
61 VERIFY( exists(p) );
62 mtime = last_write_time(p, ec);
63 VERIFY( !ec );
64 VERIFY( mtime <= time_type::clock::now() );
65 VERIFY( mtime == last_write_time(p) );
66
67 auto end_of_time = time_type::duration::max();
68 auto last_second
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
72
73 #if _GLIBCXX_USE_UTIMENSAT
74 struct ::timespec ts[2];
75 ts[0].tv_sec = 0;
76 ts[0].tv_nsec = UTIME_NOW;
77 ts[1].tv_sec = std::numeric_limits<std::time_t>::max() - 1;
78 ts[1].tv_nsec = 0;
79 VERIFY( !::utimensat(AT_FDCWD, p.c_str(), ts, 0) );
80 #elif _GLIBCXX_HAVE_UTIME_H
81 ::utimbuf times;
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.string().c_str(), &times) );
85 #else
86 return;
87 #endif
88
89 mtime = last_write_time(p, ec);
90 VERIFY( ec );
91 VERIFY( ec == std::make_error_code(std::errc::value_too_large) );
92 VERIFY( mtime == time_type::min() );
93
94 #if __cpp_exceptions
95 caught = false;
96 try {
97 mtime = last_write_time(p);
98 } catch (std::system_error const& e) {
99 caught = true;
100 ec = e.code();
101 }
102 VERIFY( caught );
103 VERIFY( ec );
104 VERIFY( ec == std::make_error_code(std::errc::value_too_large) );
105 #endif
106 }
107
108 bool approx_equal(time_type file_time, time_type expected)
109 {
110 auto delta = expected - file_time;
111 if (delta < delta.zero())
112 delta = -delta;
113 return delta < std::chrono::seconds(1);
114 }
115
116 void
117 test02()
118 {
119 // write times
120
121 __gnu_test::scoped_file f;
122 std::error_code ec;
123 time_type time;
124
125 time = last_write_time(f.path);
126 last_write_time(f.path, time, ec);
127 VERIFY( !ec );
128 VERIFY( approx_equal(last_write_time(f.path), time) );
129
130 time -= std::chrono::milliseconds(1000 * 60 * 10 + 15);
131 last_write_time(f.path, time, ec);
132 VERIFY( !ec );
133 VERIFY( approx_equal(last_write_time(f.path), time) );
134
135 time += std::chrono::milliseconds(1000 * 60 * 20 + 15);
136 last_write_time(f.path, time, ec);
137 VERIFY( !ec );
138 VERIFY( approx_equal(last_write_time(f.path), time) );
139
140 time = time_type();
141 last_write_time(f.path, time, ec);
142 VERIFY( !ec );
143 VERIFY( approx_equal(last_write_time(f.path), time) );
144
145 time -= std::chrono::milliseconds(1000 * 60 * 10 + 15);
146 last_write_time(f.path, time, ec);
147 VERIFY( !ec );
148 VERIFY( approx_equal(last_write_time(f.path), time) );
149 }
150
151 int
152 main()
153 {
154 test01();
155 test02();
156 }