]>
Commit | Line | Data |
---|---|---|
99dee823 | 1 | // Copyright (C) 2015-2021 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 | ||
de4db54f | 18 | // { dg-options "-std=gnu++17" } |
641cb5a6 JW |
19 | // { dg-do run { target c++17 } } |
20 | // { dg-require-filesystem-ts "" } | |
21 | ||
22 | #include <filesystem> | |
23 | #include <stdlib.h> | |
24 | #include <testsuite_hooks.h> | |
25 | #include <testsuite_fs.h> | |
26 | ||
27 | void | |
28 | clean_env() | |
29 | { | |
9534a5e6 JW |
30 | #if defined(__MINGW32__) || defined(__MINGW64__) |
31 | ::_putenv("TMP="); | |
32 | ::_putenv("TEMP="); | |
33 | #else | |
641cb5a6 JW |
34 | ::unsetenv("TMPDIR"); |
35 | ::unsetenv("TMP"); | |
36 | ::unsetenv("TEMPDIR"); | |
37 | ::unsetenv("TEMP"); | |
9534a5e6 JW |
38 | #endif |
39 | } | |
40 | ||
41 | bool | |
42 | set_env(const char* name, std::string value) | |
43 | { | |
44 | #if defined(__MINGW32__) || defined(__MINGW64__) | |
45 | std::string s = name; | |
46 | s += '='; | |
47 | s += value; | |
48 | return !::_putenv(s.c_str()); | |
49 | #else | |
50 | return !::setenv(name, value.c_str(), 1); | |
51 | #endif | |
641cb5a6 JW |
52 | } |
53 | ||
54 | namespace fs = std::filesystem; | |
55 | ||
56 | void | |
57 | test01() | |
58 | { | |
59 | clean_env(); | |
60 | ||
61 | if (!fs::exists("/tmp")) | |
62 | return; // just give up | |
63 | ||
64 | std::error_code ec = make_error_code(std::errc::invalid_argument); | |
65 | fs::path p1 = fs::temp_directory_path(ec); | |
66 | VERIFY( !ec ); | |
67 | VERIFY( exists(p1) ); | |
68 | ||
69 | fs::path p2 = fs::temp_directory_path(); | |
70 | VERIFY( p1 == p2 ); | |
71 | } | |
72 | ||
73 | void | |
74 | test02() | |
75 | { | |
76 | clean_env(); | |
77 | ||
dd0f7ba2 | 78 | if (!set_env("TMP", __gnu_test::nonexistent_path().string())) |
641cb5a6 JW |
79 | return; // just give up |
80 | ||
81 | std::error_code ec; | |
82 | fs::path p = fs::temp_directory_path(ec); | |
83 | VERIFY( ec ); | |
84 | VERIFY( p == fs::path() ); | |
85 | ||
86 | std::error_code ec2; | |
87 | try { | |
88 | p = fs::temp_directory_path(); | |
89 | } catch (const fs::filesystem_error& e) { | |
90 | ec2 = e.code(); | |
91 | } | |
92 | VERIFY( ec2 == ec ); | |
93 | } | |
94 | ||
95 | void | |
96 | test03() | |
97 | { | |
dd0f7ba2 JW |
98 | #if defined(__MINGW32__) || defined(__MINGW64__) |
99 | // No permissions support | |
100 | return; | |
101 | #endif | |
102 | ||
103 | clean_env(); | |
104 | ||
641cb5a6 JW |
105 | auto p = __gnu_test::nonexistent_path(); |
106 | create_directories(p/"tmp"); | |
107 | permissions(p, fs::perms::none); | |
9534a5e6 | 108 | set_env("TMPDIR", (p/"tmp").string()); |
641cb5a6 JW |
109 | std::error_code ec; |
110 | auto r = fs::temp_directory_path(ec); // libstdc++/PR71337 | |
111 | VERIFY( ec == std::make_error_code(std::errc::permission_denied) ); | |
112 | VERIFY( r == fs::path() ); | |
113 | ||
114 | std::error_code ec2; | |
115 | try { | |
116 | fs::temp_directory_path(); | |
117 | } catch (const fs::filesystem_error& e) { | |
118 | ec2 = e.code(); | |
119 | } | |
120 | VERIFY( ec2 == ec ); | |
121 | ||
122 | permissions(p, fs::perms::owner_all, ec); | |
123 | remove_all(p, ec); | |
124 | } | |
125 | ||
126 | void | |
127 | test04() | |
128 | { | |
dd0f7ba2 JW |
129 | clean_env(); |
130 | ||
641cb5a6 | 131 | __gnu_test::scoped_file f; |
dd0f7ba2 | 132 | set_env("TMP", f.path.string()); |
641cb5a6 JW |
133 | std::error_code ec; |
134 | auto r = fs::temp_directory_path(ec); | |
135 | VERIFY( ec == std::make_error_code(std::errc::not_a_directory) ); | |
136 | VERIFY( r == fs::path() ); | |
137 | ||
138 | std::error_code ec2; | |
139 | try { | |
140 | fs::temp_directory_path(); | |
141 | } catch (const fs::filesystem_error& e) { | |
142 | ec2 = e.code(); | |
143 | } | |
144 | VERIFY( ec2 == ec ); | |
145 | } | |
146 | ||
147 | int | |
148 | main() | |
149 | { | |
150 | test01(); | |
151 | test02(); | |
152 | test03(); | |
153 | test04(); | |
154 | } |