1 // Copyright (C) 2016-2019 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 "-std=gnu++17 -lstdc++fs" }
19 // { dg-do run { target c++17 } }
20 // { dg-require-filesystem-ts "" }
22 // C++17 30.10.14.26 Permissions [fs.op.permissions]
25 #include <testsuite_fs.h>
26 #include <testsuite_hooks.h>
31 using std::filesystem::perms
;
32 using std::filesystem::perm_options
;
34 auto p
= __gnu_test::nonexistent_path();
36 __gnu_test::scoped_file
f(p
);
38 permissions(p
, perms::owner_all
);
39 VERIFY( status(p
).permissions() == perms::owner_all
);
40 permissions(p
, perms::group_read
, perm_options::add
);
41 VERIFY( status(p
).permissions() == (perms::owner_all
| perms::group_read
) );
42 permissions(p
, perms::group_read
, perm_options::remove
);
43 VERIFY( status(p
).permissions() == perms::owner_all
);
49 const std::error_code bad_ec
= make_error_code(std::errc::invalid_argument
);
50 using std::filesystem::perms
;
51 using std::filesystem::perm_options
;
53 auto p
= __gnu_test::nonexistent_path();
56 permissions(p
, perms::owner_all
, ec
);
59 __gnu_test::scoped_file
f(p
);
63 permissions(p
, perms::owner_all
, ec
);
65 VERIFY( status(p
).permissions() == perms::owner_all
);
67 permissions(p
, perms::group_read
, perm_options::add
, ec
);
69 VERIFY( status(p
).permissions() == (perms::owner_all
| perms::group_read
) );
71 permissions(p
, perms::group_read
, perm_options::remove
, ec
);
73 VERIFY( status(p
).permissions() == perms::owner_all
);
79 using std::filesystem::perms
;
80 using std::filesystem::perm_options
;
82 __gnu_test::scoped_file f
;
83 VERIFY( exists(f
.path
) );
85 auto p
= __gnu_test::nonexistent_path();
86 create_symlink(f
.path
, p
);
88 std::error_code ec
= make_error_code(std::errc::no_such_file_or_directory
);
89 permissions(p
, perms::owner_all
,
90 perm_options::replace
|perm_options::nofollow
, ec
);
95 permissions(p
, perms::owner_all
,
96 perm_options::replace
|perm_options::nofollow
);
98 catch (const std::filesystem::filesystem_error
& ex
)
102 VERIFY( ex
.path1() == p
);
104 // Both calls should succeed, or both should fail with same error:
119 using perms
= std::filesystem::perms
;
121 auto p
= __gnu_test::nonexistent_path();
122 create_symlink(__gnu_test::nonexistent_path(), p
);
124 std::error_code ec
= make_error_code(std::errc::no_such_file_or_directory
);
125 permissions(p
, perms::owner_all
, ec
);
130 permissions(p
, perms::owner_all
);
132 catch (const std::filesystem::filesystem_error
& ex
)
135 VERIFY( ex
.path1() == p
);
145 const std::error_code bad_ec
= make_error_code(std::errc::invalid_argument
);
146 using std::filesystem::perms
;
147 using std::filesystem::perm_options
;
150 __gnu_test::scoped_file f
;
151 auto p
= perms::owner_write
;
153 // symlink_nofollow should not give an error for non-symlinks
155 permissions(f
.path
, p
, perm_options::replace
|perm_options::nofollow
, ec
);
157 auto st
= status(f
.path
);
158 VERIFY( st
.permissions() == p
);
159 p
|= perms::owner_read
;
161 permissions(f
.path
, p
, perm_options::replace
|perm_options::nofollow
, ec
);
164 VERIFY( st
.permissions() == p
);