Debian Codegraph @

libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.hard_lk_ct/hard_link_count.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // REQUIRES: can-create-symlinks
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: no-filesystem
12
13 // Starting in Android N (API 24), SELinux policy prevents the shell user from
14 // creating a hard link.
15 // XFAIL: LIBCXX-ANDROID-FIXME && !android-device-api={{21|22|23}}
16
17 // <filesystem>
18
19 // uintmax_t hard_link_count(const path& p);
20 // uintmax_t hard_link_count(const path& p, std::error_code& ec) noexcept;
21
22 #include <filesystem>
23 #include <type_traits>
24 #include <cassert>
25
26 #include "test_macros.h"
27 #include "filesystem_test_helper.h"
28 namespace fs = std::filesystem;
29 using namespace fs;
30
31 static void signature_test()
32 {
33 const path p; ((void)p);
34 std::error_code ec; ((void)ec);
35 ASSERT_SAME_TYPE(decltype(hard_link_count(p)), std::uintmax_t);
36 ASSERT_SAME_TYPE(decltype(hard_link_count(p, ec)), std::uintmax_t);
37 ASSERT_NOT_NOEXCEPT(hard_link_count(p));
38 ASSERT_NOEXCEPT(hard_link_count(p, ec));
39 }
40
41 static void hard_link_count_for_file()
42 {
43 static_test_env static_env;
44 assert(hard_link_count(static_env.File) == 1);
45 std::error_code ec;
46 assert(hard_link_count(static_env.File, ec) == 1);
47 }
48
49 static void hard_link_count_for_directory()
50 {
51 static_test_env static_env;
52 std::uintmax_t DirExpect = 3; // hard link from . .. and Dir2
53 std::uintmax_t Dir3Expect = 2; // hard link from . ..
54 std::uintmax_t DirExpectAlt = DirExpect;
55 std::uintmax_t Dir3ExpectAlt = Dir3Expect;
56 #if defined(__APPLE__)
57 // Filesystems formatted with case sensitive hfs+ behave unixish as
58 // expected. Normal hfs+ filesystems report the number of directory
59 // entries instead.
60 DirExpectAlt = 5; // . .. Dir2 file1 file2
61 Dir3Expect = 3; // . .. file5
62 #endif
63 assert(hard_link_count(static_env.Dir) == DirExpect ||
64 hard_link_count(static_env.Dir) == DirExpectAlt ||
65 hard_link_count(static_env.Dir) == 1);
66 assert(hard_link_count(static_env.Dir3) == Dir3Expect ||
67 hard_link_count(static_env.Dir3) == Dir3ExpectAlt ||
68 hard_link_count(static_env.Dir3) == 1);
69
70 std::error_code ec;
71 assert(hard_link_count(static_env.Dir, ec) == DirExpect ||
72 hard_link_count(static_env.Dir, ec) == DirExpectAlt ||
73 hard_link_count(static_env.Dir) == 1);
74 assert(hard_link_count(static_env.Dir3, ec) == Dir3Expect ||
75 hard_link_count(static_env.Dir3, ec) == Dir3ExpectAlt ||
76 hard_link_count(static_env.Dir3) == 1);
77 }
78
79 static void hard_link_count_increments_test()
80 {
81 scoped_test_env env;
82 const path file = env.create_file("file", 42);
83 assert(hard_link_count(file) == 1);
84
85 env.create_hardlink(file, "file_hl");
86 assert(hard_link_count(file) == 2);
87 }
88
89
90 static void hard_link_count_error_cases()
91 {
92 static_test_env static_env;
93 const path testCases[] = {
94 static_env.BadSymlink,
95 static_env.DNE
96 };
97 const std::uintmax_t expect = static_cast<std::uintmax_t>(-1);
98 for (auto& TC : testCases) {
99 std::error_code ec;
100 assert(hard_link_count(TC, ec) == expect);
101 assert(ec);
102 }
103 }
104
105 int main(int, char**) {
106 signature_test();
107 hard_link_count_for_file();
108 hard_link_count_for_directory();
109 hard_link_count_increments_test();
110 hard_link_count_error_cases();
111
112 return 0;
113 }