aboutsummaryrefslogtreecommitdiff
path: root/libcxx/test/std/input.output/filesystems/class.directory_entry/directory_entry.mods/replace_filename.pass.cpp
blob: 03e9de882ab0ab8c2f7fd1e78af0fcf811971f80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// REQUIRES: can-create-symlinks
// UNSUPPORTED: c++03, c++11, c++14

// <filesystem>

// class directory_entry

// directory_entry& operator=(directory_entry const&) = default;
// directory_entry& operator=(directory_entry&&) noexcept = default;
// void assign(path const&);
// void replace_filename(path const&);

#include <filesystem>
#include <type_traits>
#include <cassert>

#include "test_macros.h"
#include "filesystem_test_helper.h"
namespace fs = std::filesystem;

static void test_replace_filename_method() {
  using namespace fs;

  {
    directory_entry e;
    path replace;
    static_assert(noexcept(e.replace_filename(replace)) == false,
                  "operation cannot be noexcept");
    static_assert(
        std::is_same<decltype(e.replace_filename(replace)), void>::value,
        "operation must return void");
  }
  {
    const path p("/path/to/foo.exe");
    const path replace("bar.out");
    const path expect("/path/to/bar.out");
    directory_entry e(p);
    assert(e.path() == p);
    e.replace_filename(replace);
    assert(e.path() == expect);
  }
}

static void test_replace_filename_ec_method() {
  using namespace fs;

  static_test_env static_env;
  {
    directory_entry e;
    path replace;
    std::error_code ec;
    static_assert(noexcept(e.replace_filename(replace, ec)) == false,
                  "operation cannot be noexcept");
    static_assert(
        std::is_same<decltype(e.replace_filename(replace, ec)), void>::value,
        "operation must return void");
  }
  {
    const path p("/path/to/foo.exe");
    const path replace("bar.out");
    const path expect("/path/to/bar.out");
    directory_entry e(p);
    assert(e.path() == p);
    std::error_code ec = GetTestEC();
    e.replace_filename(replace, ec);
    assert(e.path() == expect);
    assert(ErrorIs(ec, std::errc::no_such_file_or_directory));
  }
  {
    const path p = static_env.EmptyFile;
    const path expect = static_env.NonEmptyFile;
    const path replace = static_env.NonEmptyFile.filename();
    assert(expect.parent_path() == p.parent_path());
    directory_entry e(p);
    assert(e.path() == p);
    std::error_code ec = GetTestEC();
    e.replace_filename(replace, ec);
    assert(e.path() == expect);
    assert(!ec);
  }
}

static void test_replace_filename_calls_refresh() {
  using namespace fs;
  scoped_test_env env;
  const path dir = env.create_dir("dir");
  const path file = env.create_file("dir/file", 42);
  const path file_two = env.create_file("dir/file_two", 101);
  const path sym = env.create_symlink("dir/file", "sym");
  const path sym_two = env.create_symlink("dir/file_two", "sym_two");

  {
    directory_entry ent(file);
    ent.replace_filename(file_two.filename());
    assert(ent.path() == file_two);

    // removing the file demonstrates that the values where cached previously.
    LIBCPP_ONLY(remove(file_two));

    assert(ent.file_size() == 101);
  }
  env.create_file("dir/file_two", 99);
  {
    directory_entry ent(sym);
    ent.replace_filename(sym_two.filename());
    assert(ent.path() == sym_two);

    LIBCPP_ONLY(remove(file_two));
    LIBCPP_ONLY(remove(sym_two));

    assert(ent.is_symlink());
    assert(ent.is_regular_file());
    assert(ent.file_size() == 99);
  }
}

#ifndef TEST_WIN_NO_FILESYSTEM_PERMS_NONE
// Windows doesn't support setting perms::none to trigger failures
// reading directories.
static void test_replace_filename_propagates_error() {
  using namespace fs;
  scoped_test_env env;
  const path dir = env.create_dir("dir");
  const path file = env.create_file("dir/file", 42);
  const path file_two = env.create_file("dir/file_two", 99);
  const path file_out_of_dir = env.create_file("file_three", 101);
  const path sym_out_of_dir = env.create_symlink("dir/file", "sym");
  const path sym_out_of_dir_two = env.create_symlink("dir/file", "sym_two");
  const path sym_in_dir = env.create_symlink("file_two", "dir/sym_three");
  const path sym_in_dir_two = env.create_symlink("file_two", "dir/sym_four");

  const perms old_perms = status(dir).permissions();

  {
    directory_entry ent(file);
    permissions(dir, perms::none);
    std::error_code ec = GetTestEC();
    ent.replace_filename(file_two.filename(), ec);
    assert(ErrorIs(ec, std::errc::permission_denied));
  }
  permissions(dir, old_perms);
  {
    directory_entry ent(sym_in_dir);
    permissions(dir, perms::none);
    std::error_code ec = GetTestEC();
    ent.replace_filename(sym_in_dir_two.filename(), ec);
    assert(ErrorIs(ec, std::errc::permission_denied));
  }
  permissions(dir, old_perms);
  {
    directory_entry ent(sym_out_of_dir);
    permissions(dir, perms::none);
    std::error_code ec = GetTestEC();
    ent.replace_filename(sym_out_of_dir_two.filename(), ec);
    assert(!ec);
    assert(ent.is_symlink());
    ec = GetTestEC();
    assert(!ent.exists(ec));
    assert(ErrorIs(ec, std::errc::permission_denied));
  }
}
#endif // TEST_WIN_NO_FILESYSTEM_PERMS_NONE

int main(int, char**) {
  test_replace_filename_method();
  test_replace_filename_ec_method();
  test_replace_filename_calls_refresh();
#ifndef TEST_WIN_NO_FILESYSTEM_PERMS_NONE
  test_replace_filename_propagates_error();
#endif

  return 0;
}