@@ -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+
4693std::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 );
0 commit comments