1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++98, c++03
12 // <experimental/filesystem>
14 // class directory_entry
16 // uintmax_t hard_link_count() const;
17 // uintmax_t hard_link_count(error_code const&) const noexcept;
19 #include "filesystem_include.hpp"
20 #include <type_traits>
23 #include "filesystem_test_helper.hpp"
24 #include "rapid-cxx-test.hpp"
26 TEST_SUITE(directory_entry_obs_testsuite)
28 TEST_CASE(signatures) {
31 const directory_entry e = {};
33 static_assert(std::is_same<decltype(e.hard_link_count()), uintmax_t>::value, "");
34 static_assert(std::is_same<decltype(e.hard_link_count(ec)), uintmax_t>::value,
36 static_assert(noexcept(e.hard_link_count()) == false, "");
37 static_assert(noexcept(e.hard_link_count(ec)) == true, "");
45 const path file = env.create_file("file", 42);
46 const path dir = env.create_dir("dir");
47 const path sym = env.create_symlink("file", "sym");
50 directory_entry ent(file);
51 uintmax_t expect = hard_link_count(ent);
53 // Remove the file to show that the results were already in the cache.
54 LIBCPP_ONLY(remove(file));
56 std::error_code ec = GetTestEC();
57 TEST_CHECK(ent.hard_link_count(ec) == expect);
61 directory_entry ent(dir);
62 uintmax_t expect = hard_link_count(ent);
64 LIBCPP_ONLY(remove(dir));
66 std::error_code ec = GetTestEC();
67 TEST_CHECK(ent.hard_link_count(ec) == expect);
70 env.create_file("file", 99);
71 env.create_hardlink("file", "hl");
73 directory_entry ent(sym);
74 std::error_code ec = GetTestEC();
75 TEST_CHECK(ent.hard_link_count(ec) == 2);
80 TEST_CASE(not_regular_file) {
84 const path dir = env.create_dir("dir");
85 const path dir2 = env.create_dir("dir/dir2");
86 const path fifo = env.create_fifo("dir/fifo");
87 const path sym_to_fifo = env.create_symlink("dir/fifo", "dir/sym");
89 const perms old_perms = status(dir).permissions();
91 for (auto p : {dir2, fifo, sym_to_fifo}) {
92 permissions(dir, old_perms);
93 std::error_code dummy_ec = GetTestEC();
94 directory_entry ent(p, dummy_ec);
95 TEST_CHECK(!dummy_ec);
97 uintmax_t expect = hard_link_count(p);
99 LIBCPP_ONLY(permissions(dir, perms::none));
101 std::error_code ec = GetTestEC();
102 TEST_CHECK(ent.hard_link_count(ec) == expect);
104 TEST_CHECK_NO_THROW(ent.hard_link_count());
108 TEST_CASE(error_reporting) {
113 const path dir = env.create_dir("dir");
114 const path file = env.create_file("dir/file", 42);
115 const path file_out_of_dir = env.create_file("file2", 101);
116 const path sym_out_of_dir = env.create_symlink("dir/file", "sym");
117 const path sym_in_dir = env.create_symlink("file2", "dir/sym2");
119 const perms old_perms = status(dir).permissions();
121 // test a file which doesn't exist
125 std::error_code ec = GetTestEC();
126 ent.assign(StaticEnv::DNE, ec);
128 TEST_REQUIRE(ent.path() == StaticEnv::DNE);
129 TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));
132 TEST_CHECK(ent.hard_link_count(ec) == uintmax_t(-1));
133 TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));
135 ExceptionChecker Checker(StaticEnv::DNE,
136 std::errc::no_such_file_or_directory,
137 "directory_entry::hard_link_count");
138 TEST_CHECK_THROW_RESULT(filesystem_error, Checker, ent.hard_link_count());
140 // test a dead symlink
144 std::error_code ec = GetTestEC();
145 uintmax_t expect_bad = hard_link_count(StaticEnv::BadSymlink, ec);
146 TEST_CHECK(expect_bad == uintmax_t(-1));
147 TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));
150 ent.assign(StaticEnv::BadSymlink, ec);
151 TEST_REQUIRE(ent.path() == StaticEnv::BadSymlink);
155 TEST_CHECK(ent.hard_link_count(ec) == expect_bad);
156 TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));
158 ExceptionChecker Checker(StaticEnv::BadSymlink,
159 std::errc::no_such_file_or_directory,
160 "directory_entry::hard_link_count");
161 TEST_CHECK_THROW_RESULT(filesystem_error, Checker, ent.hard_link_count());
163 // test a file w/o appropriate permissions.
166 uintmax_t expect_good = hard_link_count(file);
167 permissions(dir, perms::none);
169 std::error_code ec = GetTestEC();
170 ent.assign(file, ec);
171 TEST_REQUIRE(ent.path() == file);
172 TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
175 TEST_CHECK(ent.hard_link_count(ec) == uintmax_t(-1));
176 TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
178 ExceptionChecker Checker(file, std::errc::permission_denied,
180 TEST_CHECK_THROW_RESULT(filesystem_error, Checker, ent.hard_link_count());
182 permissions(dir, old_perms);
184 TEST_CHECK(ent.hard_link_count(ec) == expect_good);
186 TEST_CHECK_NO_THROW(ent.hard_link_count());
188 permissions(dir, old_perms);
189 // test a symlink w/o appropriate permissions.
192 uintmax_t expect_good = hard_link_count(sym_in_dir);
193 permissions(dir, perms::none);
195 std::error_code ec = GetTestEC();
196 ent.assign(sym_in_dir, ec);
197 TEST_REQUIRE(ent.path() == sym_in_dir);
198 TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
201 TEST_CHECK(ent.hard_link_count(ec) == uintmax_t(-1));
202 TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
204 ExceptionChecker Checker(sym_in_dir, std::errc::permission_denied,
206 TEST_CHECK_THROW_RESULT(filesystem_error, Checker, ent.hard_link_count());
208 permissions(dir, old_perms);
210 TEST_CHECK(ent.hard_link_count(ec) == expect_good);
212 TEST_CHECK_NO_THROW(ent.hard_link_count());
214 permissions(dir, old_perms);
215 // test a symlink to a file w/o appropriate permissions
218 uintmax_t expect_good = hard_link_count(sym_out_of_dir);
219 permissions(dir, perms::none);
221 std::error_code ec = GetTestEC();
222 ent.assign(sym_out_of_dir, ec);
223 TEST_REQUIRE(ent.path() == sym_out_of_dir);
227 TEST_CHECK(ent.hard_link_count(ec) == uintmax_t(-1));
228 TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
230 ExceptionChecker Checker(sym_out_of_dir, std::errc::permission_denied,
232 TEST_CHECK_THROW_RESULT(filesystem_error, Checker, ent.hard_link_count());
234 permissions(dir, old_perms);
236 TEST_CHECK(ent.hard_link_count(ec) == expect_good);
238 TEST_CHECK_NO_THROW(ent.hard_link_count());