forked from watkipet/fuse-cpp-ramfs
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcr_util.cpp
More file actions
200 lines (175 loc) · 5.07 KB
/
cr_util.cpp
File metadata and controls
200 lines (175 loc) · 5.07 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <unordered_map>
#include <vector>
#include <cstdint>
#include <cerrno>
#include "cr_util.hpp"
#ifdef DUMP_TESTING
#define PRINT_VAL(x) std::cout << #x" : " << x << std::endl
bool isExistInDeleted(fuse_ino_t curr_ino, std::queue<fuse_ino_t> DeletedInodes);
void print_ino_queue(std::queue<fuse_ino_t> DeletedInodes);
#endif
std::unordered_map<uint64_t, verifs2_state> state_pool;
int insert_state(uint64_t key,
const std::tuple<std::vector<Inode *>, std::queue<fuse_ino_t>,
struct statvfs> &fs_states_vec) {
auto it = state_pool.find(key);
if (it != state_pool.end()) {
return -EEXIST;
}
state_pool.insert({key, fs_states_vec});
return 0;
}
verifs2_state find_state(uint64_t key) {
auto it = state_pool.find(key);
if (it == state_pool.end()) {
std::queue<fuse_ino_t> empty_queue;
struct statvfs empty_statvfs = {};
return verifs2_state{std::vector<Inode *>(), empty_queue, empty_statvfs};
} else {
return it->second;
}
}
int remove_state(uint64_t key) {
auto it = state_pool.find(key);
if (it == state_pool.end()) {
return -ENOENT;
}
state_pool.erase(it);
return 0;
}
std::unordered_map<uint64_t, verifs2_state> get_state_pool() {
return state_pool;
}
void clear_states() {
state_pool.clear();
}
#ifdef DUMP_TESTING
/* Dump functionalites to verify Checkpoint/Restore APIs */
void dump_File(File* file)
{
PRINT_VAL((char*)(file->m_buf));
}
void dump_Directory(Directory* dir)
{
for (auto& t : dir->m_children){
std::cout << "Child name: " << t.first << " | "
<< "Child inode number: " << t.second << "\n";
}
}
void dump_SpecialInode(SpecialInode* sinode)
{
PRINT_VAL(sinode->m_type);
}
void dump_SymLink(SymLink* symlink)
{
PRINT_VAL(symlink->m_link);
}
static int dump_each_inode_type(std::vector<Inode *>::iterator it)
{
int ret = 0;
if (S_ISREG((*it)->GetMode())){
std::cout << "---Dump File" << std::endl;
File *file = dynamic_cast<File *>(*it);
dump_File(file);
}
else if (S_ISDIR((*it)->GetMode())){
std::cout << "---Dump Directory" << std::endl;
Directory *dir = dynamic_cast<Directory *>(*it);
dump_Directory(dir);
}
else if (S_ISCHR((*it)->GetMode()) || S_ISBLK((*it)->GetMode()) || S_ISFIFO((*it)->GetMode())
|| S_ISSOCK((*it)->GetMode()))
{
std::cout << "---Dump SpecialInode" << std::endl;
SpecialInode *sinode = dynamic_cast<SpecialInode *>(*it);
dump_SpecialInode(sinode);
}
else if (S_ISLNK((*it)->GetMode())){
std::cout << "---Dump SymLink" << std::endl;
SymLink *symlink = dynamic_cast<SymLink *>(*it);
dump_SymLink(symlink);
}
else if ((*it)->GetMode() == 0){
return ret;
}
else{
std::cerr << "dumping inode has incorrect inode type " << (*it)->GetMode() << "\n";
ret = -EINVAL;
}
return ret;
}
static int _dump_inodes_verifs2(std::vector<Inode *> Inodes,
std::queue<fuse_ino_t> DeletedInodes)
{
int ret;
for (std::vector<Inode *>::iterator it = Inodes.begin(); it != Inodes.end(); ++it){
int dist = std::distance(Inodes.begin(), it);
// If this inode is not in DeletedInodes
fuse_ino_t curr_ino = (fuse_ino_t)dist;
if (!isExistInDeleted(curr_ino, DeletedInodes)){
ret = dump_each_inode_type(it);
if (ret != 0){
return ret;
}
}
else{
std::cout << "\033[1;36m***Inode number " << curr_ino
<< " had been deleted\033[0m\n";
}
}
return ret;
}
bool isExistInDeleted(fuse_ino_t curr_ino, std::queue<fuse_ino_t> DeletedInodes)
{
bool ret = false;
while(!DeletedInodes.empty()){
if (DeletedInodes.front() == curr_ino){
ret = true;
break;
}
DeletedInodes.pop();
}
return ret;
}
void print_ino_queue(std::queue<fuse_ino_t> DeletedInodes)
{
std::cout << "Print out Queue: ";
while (!DeletedInodes.empty())
{
std::cout << DeletedInodes.front() << " ";
DeletedInodes.pop();
}
std::cout << std::endl;
}
int dump_inodes_verifs2(std::vector<Inode *> Inodes, std::queue<fuse_ino_t> DeletedInodes,
std::string info)
{
std::cout << "\033[1;35m" + info + "\033[0m" << std::endl;
std::cout << "Size of Inodes: " << Inodes.size() << std::endl;
int ret = _dump_inodes_verifs2(Inodes, DeletedInodes);
std::cout << "\033[1;32mdump_inodes_verifs2 Finished!\033[0m" << std::endl;
return ret;
}
int dump_state_pool()
{
uint64_t key;
int ret = 0;
std::vector <Inode *> value_inode;
int state_cnt = 0;
for (const auto &each_state : state_pool) {
std::cout << "\033[1;35mDump the "<< state_cnt <<"-th state\033[0m\n";
state_cnt++;
key = each_state.first;
value_inode = std::get<0>(each_state.second);
std::cout << "Key: " << each_state.first << std::endl;
std::cout << "value_inode.size(): " << value_inode.size() << std::endl;
for (std::vector<Inode *>::iterator it = value_inode.begin(); it != value_inode.end(); ++it){
ret = dump_each_inode_type(it);
if (ret != 0){
return ret;
}
}
}
return ret;
}
#endif