| 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 |
// This test requires the dylib support introduced in e4ed349c7658. |
| 15 |
// XFAIL: using-built-library-before-llvm-12 |
| 16 |
|
| 17 |
// <filesystem> |
| 18 |
|
| 19 |
// bool create_directory(const path& p); |
| 20 |
// bool create_directory(const path& p, error_code& ec) noexcept; |
| 21 |
// bool create_directory(const path& p, const path& attr); |
| 22 |
// bool create_directory(const path& p, const path& attr, error_code& ec) noexcept; |
| 23 |
|
| 24 |
#include <filesystem> |
| 25 |
#include <type_traits> |
| 26 |
#include <cassert> |
| 27 |
|
| 28 |
#include "test_macros.h" |
| 29 |
#include "filesystem_test_helper.h" |
| 30 |
|
| 31 |
#include <sys/types.h> |
| 32 |
#include <sys/stat.h> |
| 33 |
namespace fs = std::filesystem; |
| 34 |
using namespace fs; |
| 35 |
|
| 36 |
fs::perms read_umask() { |
| 37 |
auto old_mask = umask(0); // int on Windows, mode_t on POSIX. |
| 38 |
umask(old_mask); // reset the mask to the old value. |
| 39 |
return static_cast<fs::perms>(old_mask); |
| 40 |
} |
| 41 |
|
| 42 |
static void test_signatures() |
| 43 |
{ |
| 44 |
const path p; ((void)p); |
| 45 |
std::error_code ec; ((void)ec); |
| 46 |
ASSERT_SAME_TYPE(decltype(fs::create_directory(p)), bool); |
| 47 |
ASSERT_SAME_TYPE(decltype(fs::create_directory(p, ec)), bool); |
| 48 |
ASSERT_SAME_TYPE(decltype(fs::create_directory(p, p)), bool); |
| 49 |
ASSERT_SAME_TYPE(decltype(fs::create_directory(p, p, ec)), bool); |
| 50 |
ASSERT_NOT_NOEXCEPT(fs::create_directory(p)); |
| 51 |
ASSERT_NOEXCEPT(fs::create_directory(p, ec)); |
| 52 |
ASSERT_NOT_NOEXCEPT(fs::create_directory(p, p)); |
| 53 |
ASSERT_NOEXCEPT(fs::create_directory(p, p, ec)); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
static void create_existing_directory() |
| 58 |
{ |
| 59 |
scoped_test_env env; |
| 60 |
const path dir = env.create_dir("dir1"); |
| 61 |
std::error_code ec; |
| 62 |
assert(fs::create_directory(dir, ec) == false); |
| 63 |
assert(!ec); |
| 64 |
assert(is_directory(dir)); |
| 65 |
// Test throwing version |
| 66 |
assert(fs::create_directory(dir) == false); |
| 67 |
} |
| 68 |
|
| 69 |
static void create_directory_one_level() |
| 70 |
{ |
| 71 |
scoped_test_env env; |
| 72 |
const path dir = env.make_env_path("dir1"); |
| 73 |
std::error_code ec; |
| 74 |
assert(fs::create_directory(dir, ec) == true); |
| 75 |
assert(!ec); |
| 76 |
assert(is_directory(dir)); |
| 77 |
|
| 78 |
auto st = status(dir); |
| 79 |
const perms expect_perms = perms::all & ~(read_umask()); |
| 80 |
assert((st.permissions() & perms::all) == expect_perms); |
| 81 |
} |
| 82 |
|
| 83 |
static void create_directory_multi_level() |
| 84 |
{ |
| 85 |
scoped_test_env env; |
| 86 |
const path dir = env.make_env_path("dir1/dir2"); |
| 87 |
const path dir1 = env.make_env_path("dir1"); |
| 88 |
std::error_code ec; |
| 89 |
assert(fs::create_directory(dir, ec) == false); |
| 90 |
assert(ec); |
| 91 |
assert(!is_directory(dir)); |
| 92 |
assert(!is_directory(dir1)); |
| 93 |
} |
| 94 |
|
| 95 |
static void dest_is_file() |
| 96 |
{ |
| 97 |
scoped_test_env env; |
| 98 |
const path file = env.create_file("file", 42); |
| 99 |
std::error_code ec = GetTestEC(); |
| 100 |
assert(fs::create_directory(file, ec) == false); |
| 101 |
assert(ec); |
| 102 |
assert(is_regular_file(file)); |
| 103 |
} |
| 104 |
|
| 105 |
static void dest_part_is_file() |
| 106 |
{ |
| 107 |
scoped_test_env env; |
| 108 |
const path file = env.create_file("file"); |
| 109 |
const path dir = env.make_env_path("file/dir1"); |
| 110 |
std::error_code ec = GetTestEC(); |
| 111 |
assert(fs::create_directory(dir, ec) == false); |
| 112 |
assert(ec); |
| 113 |
assert(is_regular_file(file)); |
| 114 |
assert(!exists(dir)); |
| 115 |
} |
| 116 |
|
| 117 |
static void dest_is_symlink_to_dir() |
| 118 |
{ |
| 119 |
scoped_test_env env; |
| 120 |
const path dir = env.create_dir("dir"); |
| 121 |
const path sym = env.create_directory_symlink(dir, "sym_name"); |
| 122 |
std::error_code ec = GetTestEC(); |
| 123 |
assert(create_directory(sym, ec) == false); |
| 124 |
assert(!ec); |
| 125 |
} |
| 126 |
|
| 127 |
static void dest_is_symlink_to_file() |
| 128 |
{ |
| 129 |
scoped_test_env env; |
| 130 |
const path file = env.create_file("file"); |
| 131 |
const path sym = env.create_symlink(file, "sym_name"); |
| 132 |
std::error_code ec = GetTestEC(); |
| 133 |
assert(create_directory(sym, ec) == false); |
| 134 |
assert(ec); |
| 135 |
} |
| 136 |
|
| 137 |
int main(int, char**) { |
| 138 |
test_signatures(); |
| 139 |
create_existing_directory(); |
| 140 |
create_directory_one_level(); |
| 141 |
create_directory_multi_level(); |
| 142 |
dest_is_file(); |
| 143 |
dest_part_is_file(); |
| 144 |
dest_is_symlink_to_dir(); |
| 145 |
dest_is_symlink_to_file(); |
| 146 |
|
| 147 |
return 0; |
| 148 |
} |