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
|
/*
* libfud
* Copyright 2024 Dominick Allen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "fud_array.hpp"
#include "fud_c_file.hpp"
#include "fud_directory.hpp"
#include "fud_memory.hpp"
#include "fud_string.hpp"
#include "gtest/gtest.h"
#include <algorithm>
#include <cerrno>
#include <fcntl.h>
#include <ftw.h>
#include <ranges>
namespace fud {
int unlink_cb(const char* fpath, const struct stat* sb_unused, int typeflag, struct FTW* ftwbuf)
{
static_cast<void>(sb_unused);
int retValue = remove(fpath);
EXPECT_EQ(retValue, 0);
if (retValue != 0) {
perror(fpath);
}
return retValue;
}
FudStatus removeRecursive(const String& path)
{
if (!path.utf8Valid()) {
return FudStatus::Utf8Invalid;
}
if (path.length() < 5) {
return FudStatus::InvalidInput;
}
auto prefix{String::makeFromCString("/tmp/").takeOkay()};
auto diffResult = compareMem(path.data(), path.length(), prefix.data(), prefix.length());
if (diffResult.isError()) {
return FudStatus::InvalidInput;
}
auto diff = diffResult.getOkay();
if (diff != 0) {
return FudStatus::InvalidInput;
}
constexpr int maxOpenFd = 64;
auto status = nftw(path.c_str(), unlink_cb, maxOpenFd, FTW_DEPTH | FTW_PHYS);
if (status == 0) {
return FudStatus::Success;
}
if (errno == ENOENT) {
return FudStatus::Success;
}
return FudStatus::Failure;
}
TEST(FudDirectory, Basic)
{
const auto testDirName{String::makeFromCString("/tmp/fud_directory_test").takeOkay()};
ASSERT_TRUE(testDirName.utf8Valid());
constexpr mode_t pathMode = 0777;
const Array<String, 2> files{
String::makeFromCString("file1").takeOkay(),
String::makeFromCString("file2").takeOkay()};
ASSERT_TRUE(files[0].utf8Valid());
ASSERT_TRUE(files[1].utf8Valid());
ASSERT_EQ(removeRecursive(testDirName), FudStatus::Success);
auto mkdirResult = mkdir(testDirName.c_str(), pathMode);
EXPECT_EQ(mkdirResult, 0);
if (mkdirResult != 0) {
ASSERT_EQ(removeRecursive(testDirName), FudStatus::Success);
return;
}
const String testDirNamePrefix{testDirName.catenate("/").takeOkay()};
ASSERT_TRUE(testDirNamePrefix.utf8Valid());
for (const auto& fnameBase : files) {
const auto fname{testDirNamePrefix.catenate(fnameBase).takeOkay()};
ASSERT_TRUE(fname.utf8Valid());
auto fileResult{CBinaryFile::make(fname, CFileMode::ReadWriteTruncate)};
ASSERT_TRUE(fileResult.isOkay());
CBinaryFile file{std::move(fileResult).takeOkay()};
ASSERT_EQ(file.open(), FudStatus::Success);
Array<utf8, 5> data{"test"};
WriteResult expected{data.size(), FudStatus::Success};
auto writeResult = file.write(data);
ASSERT_EQ(writeResult.bytesWritten, expected.bytesWritten);
ASSERT_EQ(writeResult.status, expected.status);
}
Directory directory{testDirName};
ASSERT_EQ(directory.status(), FudStatus::Success);
ASSERT_EQ(directory.errorCode(), 0);
const Array<DirectoryEntry, 4> expectedFiles{
DirectoryEntry{String::makeFromCString(".").takeOkay(), 0, 0, 2, 0, DirectoryEntryType::Directory},
DirectoryEntry{String::makeFromCString("..").takeOkay(), 0, 0, 1, 0, DirectoryEntryType::Directory},
DirectoryEntry{files[0], 0, files[0].size(), 1, 0, DirectoryEntryType::RegularFile},
DirectoryEntry{files[1], 0, files[1].size(), 1, 0, DirectoryEntryType::RegularFile},
};
ASSERT_TRUE(expectedFiles[0].name.compare(expectedFiles[0].name));
for (auto idx = 0; idx < expectedFiles.size(); ++idx) {
auto dirEntryResult = directory.getNextEntry();
EXPECT_TRUE(dirEntryResult.isOkay());
const auto dirEntryOpt = dirEntryResult.getOkay();
if (dirEntryOpt == std::nullopt) {
break;
}
const auto dirEntry = *dirEntryOpt;
const auto* expected = std::find_if(
expectedFiles.begin(),
expectedFiles.end(),
[&dirEntry](const DirectoryEntry& entry) {
return entry.name.compare(dirEntry.name) && entry.entryType == dirEntry.entryType;
});
EXPECT_NE(expected, nullptr);
EXPECT_NE(expected, expectedFiles.end());
}
auto finalDirEntryResult = directory.getNextEntry();
EXPECT_TRUE(finalDirEntryResult.isOkay());
EXPECT_EQ(finalDirEntryResult.getOkay(), std::nullopt);
// ASSERT_EQ(removeRecursive(testDirName), FudStatus::Success);
}
} // namespace fud
|