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
|
#include <gtest/gtest.h>
#include "io.hh"
#include <cstdlib>
#include <cerrno>
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
namespace {
bool remove_recursive(int fd) {
auto* dir = fdopendir(fd);
if (!dir) return false;
while (auto* ent = readdir(dir)) {
if (ent->d_name[0] == '.') {
if (ent->d_name[1] == '\0') continue;
if (ent->d_name[1] == '.' && ent->d_name[2] == '\0') continue;
}
bool is_dir;
if (ent->d_type == DT_DIR) {
is_dir = true;
} else if (ent->d_type == DT_UNKNOWN) {
struct stat buf;
if (fstatat(dirfd(dir), ent->d_name, &buf, AT_SYMLINK_NOFOLLOW) == 0) {
is_dir = S_ISDIR(buf.st_mode);
} else {
if (errno != ENOENT) {
closedir(dir);
return false;
}
is_dir = false;
}
} else {
is_dir = false;
}
if (is_dir) {
int fd2 = openat(dirfd(dir), ent->d_name, O_RDONLY | O_DIRECTORY);
if (fd2 == -1) {
if (errno != ENOENT) {
closedir(dir);
return false;
}
} else {
if (!remove_recursive(fd2)) {
closedir(dir);
return false;
}
}
}
if (unlinkat(dirfd(dir), ent->d_name, is_dir ? AT_REMOVEDIR : 0)) {
if (errno != ENOENT) {
closedir(dir);
return false;
}
}
}
closedir(dir);
return true;
}
class IoTest : public testing::Test {
protected:
void SetUp() override {
// NOLINTNEXTLINE(misc-include-cleaner)
tmpdir_ = P_tmpdir "/jkc-test-io-XXXXXX";
// NOLINTNEXTLINE(misc-include-cleaner)
auto* ret = mkdtemp(tmpdir_.data());
ASSERT_EQ(ret, tmpdir_.data());
dirfd_ = open(tmpdir_.c_str(), O_PATH | O_DIRECTORY);
ASSERT_NE(-1, dirfd_);
}
void TearDown() override {
int fd = openat(dirfd_, ".", O_RDONLY | O_DIRECTORY);
EXPECT_NE(-1, fd);
if (fd != -1) {
EXPECT_TRUE(remove_recursive(fd));
}
close(dirfd_);
rmdir(tmpdir_.c_str());
}
[[nodiscard]] int dirfd() const {
return dirfd_;
}
void touch(const std::string& name, const std::string& value = "") {
auto fd = openat(dirfd(), name.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0700);
EXPECT_NE(-1, fd);
if (fd == -1) return;
size_t offset = 0;
while (offset < value.size()) {
auto ret = write(fd, value.data() + offset, value.size() - offset);
EXPECT_LT(0, ret);
if (ret <= 0) {
break;
}
offset += ret;
}
close(fd);
}
private:
int dirfd_;
std::string tmpdir_;
};
} // namespace
TEST_F(IoTest, no_such_file) {
auto ret = io::openat(dirfd(), "no-such-file");
ASSERT_FALSE(ret.has_value());
EXPECT_EQ(io::OpenError::NoSuchFile, ret.error());
}
TEST_F(IoTest, read_empty) {
touch("test");
auto ret = io::openat(dirfd(), "test");
ASSERT_TRUE(ret.has_value());
std::string tmp(10, ' ');
auto ret2 = ret.value()->read(tmp);
ASSERT_TRUE(ret2.has_value());
EXPECT_EQ(0, ret2.value());
}
TEST_F(IoTest, read) {
touch("test", "hello world");
auto ret = io::openat(dirfd(), "test");
ASSERT_TRUE(ret.has_value());
std::string tmp(12, ' ');
auto ret2 = ret.value()->repeat_read(tmp);
ASSERT_TRUE(ret2.has_value());
EXPECT_EQ(11, ret2.value());
tmp.resize(ret2.value());
EXPECT_EQ("hello world", tmp);
}
|