]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/27_io/filesystem/operations/create_directories.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / operations / create_directories.cc
CommitLineData
7adcbafe 1// Copyright (C) 2015-2022 Free Software Foundation, Inc.
641cb5a6
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
641cb5a6
JW
18// { dg-do run { target c++17 } }
19// { dg-require-filesystem-ts "" }
20
21#include <filesystem>
22#include <testsuite_hooks.h>
23#include <testsuite_fs.h>
24
25namespace fs = std::filesystem;
26
27void
28test01()
29{
30 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
31 std::error_code ec;
32
33 // Test empty path.
34 bool b = fs::create_directories( "", ec );
35 VERIFY( ec );
36 VERIFY( !b );
37
38 // Test existing path.
39 ec = bad_ec;
40 b = fs::create_directories( fs::current_path(), ec );
41 VERIFY( !ec );
42 VERIFY( !b );
43
44 // Test non-existent path.
45 const auto p = __gnu_test::nonexistent_path();
46 ec = bad_ec;
47 b = fs::create_directories( p, ec );
48 VERIFY( !ec );
49 VERIFY( b );
50 VERIFY( is_directory(p) );
51
52 ec = bad_ec;
53 b = fs::create_directories( p/".", ec );
54 VERIFY( !ec );
55 VERIFY( !b );
56
57 ec = bad_ec;
58 b = fs::create_directories( p/"..", ec );
59 VERIFY( !ec );
60 VERIFY( !b );
61
62 ec = bad_ec;
63 b = fs::create_directories( p/"d1/d2/d3", ec );
64 VERIFY( !ec );
65 VERIFY( b );
66 VERIFY( is_directory(p/"d1/d2/d3") );
67
68 ec = bad_ec;
69 b = fs::create_directories( p/"./d4/../d5", ec );
70 VERIFY( !ec );
71 VERIFY( b );
dd0f7ba2
JW
72#if defined(__MINGW32__) || defined(__MINGW64__)
73 // create_directories("./d4/..") is a no-op, does not create "d4"
74#else
edfe833a 75 VERIFY( is_directory(p/"d4") );
dd0f7ba2 76#endif
edfe833a 77 VERIFY( is_directory(p/"d5") );
641cb5a6
JW
78 VERIFY( is_directory(p/"./d4/../d5") );
79
80 std::uintmax_t count = remove_all(p, ec);
dd0f7ba2
JW
81#if defined(__MINGW32__) || defined(__MINGW64__)
82 VERIFY( count == 5 );
83#else
641cb5a6 84 VERIFY( count == 6 );
dd0f7ba2 85#endif
641cb5a6
JW
86}
87
ffe2c055
JW
88void
89test02()
90{
91 // PR libstdc++/86910
92 const auto p = __gnu_test::nonexistent_path();
93 std::error_code ec;
94 bool result;
95
96 {
97 __gnu_test::scoped_file file;
98
99 result = create_directories(file.path, ec);
100 VERIFY( !result );
101 VERIFY( ec == std::errc::not_a_directory );
dd0f7ba2 102 ec.clear();
ffe2c055
JW
103 result = create_directories(file.path / "foo", ec);
104 VERIFY( !result );
105 VERIFY( ec == std::errc::not_a_directory );
dd0f7ba2 106 ec.clear();
ffe2c055
JW
107 }
108
109 create_directories(p);
110 {
111 __gnu_test::scoped_file dir(p, __gnu_test::scoped_file::adopt_file);
112 __gnu_test::scoped_file file(dir.path/"file");
113
114 result = create_directories(file.path, ec);
115 VERIFY( !result );
116 VERIFY( ec == std::errc::not_a_directory );
dd0f7ba2 117 ec.clear();
ffe2c055 118 result = create_directories(file.path/"../bar", ec);
dd0f7ba2
JW
119#if defined(__MINGW32__) || defined(__MINGW64__)
120 VERIFY( result );
121 VERIFY( !ec );
122 VERIFY( is_directory(dir.path/"bar") );
123 remove(dir.path/"bar");
124#else
ffe2c055
JW
125 VERIFY( !result );
126 VERIFY( ec == std::errc::not_a_directory );
dd0f7ba2
JW
127 VERIFY( !is_directory(dir.path/"bar") );
128#endif
ffe2c055
JW
129 }
130}
131
132void
133test03()
134{
135 // PR libstdc++/87846
136 const auto p = __gnu_test::nonexistent_path() / "";
137 bool result = create_directories(p);
138 VERIFY( result );
139 VERIFY( exists(p) );
140 remove(p);
141 result = create_directories(p/"foo/");
142 VERIFY( result );
143 VERIFY( exists(p) );
144 VERIFY( exists(p/"foo") );
145 remove_all(p);
146}
147
124eaa50
JW
148void
149test04()
150{
151#if defined(__MINGW32__) || defined(__MINGW64__)
152 // no symlinks
153#else
154 // PR libstdc++/101510
155 // create_directories reports an error if the path is a symlink to a dir
156 std::error_code ec = make_error_code(std::errc::invalid_argument);
157 const auto p = __gnu_test::nonexistent_path() / "";
158 fs::create_directories(p/"dir");
159 auto link = p/"link";
160 fs::create_directory_symlink("dir", link);
161 bool created = fs::create_directories(link, ec);
162 VERIFY( !created );
163 VERIFY( !ec );
164 created = fs::create_directories(link);
165 VERIFY( !created );
166 remove_all(p);
167#endif
168}
169
641cb5a6
JW
170int
171main()
172{
173 test01();
ffe2c055
JW
174 test02();
175 test03();
124eaa50 176 test04();
641cb5a6 177}