Skip to content

Commit 3752124

Browse files
author
Hongzhen Luo
committed
[EROFS] test: enhanced testing for same-name file or dir
Add tests for cases where file and directory have the same name. Signed-off-by: Hongzhen Luo <hongzhen@linux.alibaba.com>
1 parent fa5d2fb commit 3752124

2 files changed

Lines changed: 55 additions & 28 deletions

File tree

src/overlaybd/tar/erofs/test/erofs_stress_base.cpp

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,57 @@ static bool is_substring(const std::string& str, const std::string& substring) {
4343
return str.find(substring) != std::string::npos;
4444
}
4545

46+
static bool str_n_equal(std::string s1, std::string s2, long unsigned int n) {
47+
if (s1.length() < n || s2.length() < n)
48+
return false;
49+
return s1.substr(0, n) == s2.substr(0, n);
50+
}
51+
52+
bool StressFsTree::add_node(StressNode *node) {
53+
if (!node || !node->path.size() || node->type >= NODE_TYPE_MAX)
54+
LOG_ERRNO_RETURN(-1, false, "invalid node");
55+
56+
if (node->type != NODE_WHITEOUT) {
57+
/* the upper regular file should remove the lower dir */
58+
std::map<std::string, StressNode*>::iterator dir_it;
59+
if (node->type == NODE_REGULAR && (dir_it = tree.find(node->path)) != tree.end() &&
60+
dir_it->second->type == NODE_DIR)
61+
{
62+
tree.erase(dir_it);
63+
std::string rm_prefix = node->path + "/";
64+
for (auto it = tree.begin(); it != tree.end(); ) {
65+
if (str_n_equal(rm_prefix, it->first, rm_prefix.length())) {
66+
it = tree.erase(it);
67+
} else {
68+
++it;
69+
}
70+
71+
}
72+
}
73+
tree[node->path] = node;
74+
} else {
75+
auto it = tree.find(node->path);
76+
if (it == tree.end() || it->second->type == NODE_WHITEOUT)
77+
LOG_ERROR_RETURN(-1, false, "whiteout a invalid object");
78+
if (it->second->type == NODE_REGULAR)
79+
tree.erase(it);
80+
else {
81+
std::string prefix = it->first;
82+
for (auto p = tree.begin(); p != tree.end();) {
83+
if (prefix.compare(0, prefix.size(), p->first) == 0)
84+
p = tree.erase(p);
85+
else
86+
p ++;
87+
}
88+
}
89+
}
90+
return true;
91+
}
92+
4693
std::string StressFsTree::get_same_name(int idx, int depth, std::string root_path, NODE_TYPE type) {
4794
std::vector<std::string> vec;
4895
for (const auto& pair : tree) {
49-
if (pair.first == "/" || pair.second->type != type ||
50-
!is_substring(pair.first, root_path) ||
96+
if (pair.first == "/" || !is_substring(pair.first, root_path) ||
5197
pair.first.length() == root_path.length())
5298
continue;
5399
std::string last_component = pair.first.substr(root_path.length() + 1);

src/overlaybd/tar/erofs/test/erofs_stress_base.h

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -166,36 +166,17 @@ class StressFsTree {
166166
}
167167

168168
// build process
169-
bool add_node(StressNode *node) {
170-
if (!node || !node->path.size() || node->type >= NODE_TYPE_MAX)
171-
LOG_ERRNO_RETURN(-1, false, "invalid node");
172-
173-
if (node->type != NODE_WHITEOUT) {
174-
tree[node->path] = node;
175-
} else {
176-
auto it = tree.find(node->path);
177-
if (it == tree.end() || it->second->type == NODE_WHITEOUT)
178-
LOG_ERROR_RETURN(-1, false, "whiteout a invalid object");
179-
if (it->second->type == NODE_REGULAR)
180-
tree.erase(it);
181-
else {
182-
std::string prefix = it->first;
183-
for (auto p = tree.begin(); p != tree.end();) {
184-
if (prefix.compare(0, prefix.size(), p->first) == 0)
185-
p = tree.erase(p);
186-
else
187-
p ++;
188-
}
189-
}
190-
}
191-
return true;
192-
}
169+
bool add_node(StressNode *node);
193170

194171
// verify process
195172
bool query_delete_node(StressNode *node) {
196173
auto it = tree.find(node->path);
197-
if (it == tree.end() || !it->second || !it->second->equal(node))
198-
return false;
174+
if (it == tree.end())
175+
LOG_ERROR_RETURN(-1,false, "path ` does not exist in in-mem tree", node->path);
176+
if (!it->second)
177+
LOG_ERROR_RETURN(-1, false, "NULL in-mem info (`)", node->path);
178+
if (!it->second->equal(node))
179+
LOG_ERROR_RETURN(-1, false, "node contents mismatch");
199180
tree.erase(it);
200181
return true;
201182
}

0 commit comments

Comments
 (0)