| 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 |
// void create_hard_link(const path& existing_symlink, const path& new_symlink); |
| 21 |
// void create_hard_link(const path& existing_symlink, const path& new_symlink, |
| 22 |
// error_code& ec) noexcept; |
| 23 |
|
| 24 |
#include <filesystem> |
| 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 test_signatures() |
| 32 |
{ |
| 33 |
const path p; ((void)p); |
| 34 |
std::error_code ec; ((void)ec); |
| 35 |
ASSERT_NOT_NOEXCEPT(fs::create_hard_link(p, p)); |
| 36 |
ASSERT_NOEXCEPT(fs::create_hard_link(p, p, ec)); |
| 37 |
} |
| 38 |
|
| 39 |
static void test_error_reporting() |
| 40 |
{ |
| 41 |
scoped_test_env env; |
| 42 |
const path file = env.create_file("file1", 42); |
| 43 |
const path file2 = env.create_file("file2", 55); |
| 44 |
const path sym = env.create_symlink(file, "sym"); |
| 45 |
{ // destination exists |
| 46 |
std::error_code ec; |
| 47 |
fs::create_hard_link(sym, file2, ec); |
| 48 |
assert(ec); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
static void create_file_hard_link() |
| 53 |
{ |
| 54 |
scoped_test_env env; |
| 55 |
const path file = env.create_file("file"); |
| 56 |
const path dest = env.make_env_path("dest1"); |
| 57 |
std::error_code ec; |
| 58 |
assert(hard_link_count(file) == 1); |
| 59 |
fs::create_hard_link(file, dest, ec); |
| 60 |
assert(!ec); |
| 61 |
assert(exists(dest)); |
| 62 |
assert(equivalent(dest, file)); |
| 63 |
assert(hard_link_count(file) == 2); |
| 64 |
} |
| 65 |
|
| 66 |
static void create_directory_hard_link_fails() |
| 67 |
{ |
| 68 |
scoped_test_env env; |
| 69 |
const path dir = env.create_dir("dir"); |
| 70 |
const path dest = env.make_env_path("dest2"); |
| 71 |
std::error_code ec; |
| 72 |
|
| 73 |
fs::create_hard_link(dir, dest, ec); |
| 74 |
assert(ec); |
| 75 |
} |
| 76 |
|
| 77 |
int main(int, char**) { |
| 78 |
test_signatures(); |
| 79 |
test_error_reporting(); |
| 80 |
create_file_hard_link(); |
| 81 |
create_directory_hard_link_fails(); |
| 82 |
|
| 83 |
return 0; |
| 84 |
} |