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