-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDirectory.cpp
More file actions
272 lines (241 loc) · 6.61 KB
/
Directory.cpp
File metadata and controls
272 lines (241 loc) · 6.61 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "Directory.h"
Dirent::Dirent()
{
this->name = "";
this->fhandle = "";
}
Dirent::Dirent(std::string arg_name, std::string arg_fhandle)
{
this->name = arg_name;
this->fhandle = arg_fhandle;
}
Json::Value *
Dirent::dumpJ()
{
Json::Value * result_ptr = new Json::Value();
if (this->name != "")
{
(*result_ptr)["name"] = this->name;
}
if (this->fhandle != "")
{
(*result_ptr)["fhandle"] = this->fhandle;
}
return result_ptr;
}
bool
Dirent::Jdump(Json::Value *input_json_ptr)
{
if ((input_json_ptr == NULL) ||
((*input_json_ptr).isNull() == true) ||
((*input_json_ptr).isObject() != true))
{
return false;
}
if ((((*input_json_ptr)["name"]).isNull() == true) ||
(((*input_json_ptr)["fhandle"]).isNull() == true) ||
(((*input_json_ptr)["name"]).isString() != true) ||
(((*input_json_ptr)["fhandle"]).isString() != true))
{
return false;
}
this->name = ((*input_json_ptr)["name"]).asString();
this->fhandle = ((*input_json_ptr)["fhandle"]).asString();
return true;
}
Directory::Directory
(std::string core_arg_host_url, std::string core_arg_owner_vsID,
std::string core_arg_class_id, std::string core_arg_object_id)
: Core { core_arg_host_url, core_arg_owner_vsID,
core_arg_class_id, core_arg_object_id }
{
std::cout << "a shadow has been created" << std::endl;
this->dirent_vector_ptr = NULL;
}
Directory::Directory
(std::string core_arg_host_url, std::string core_arg_owner_vsID,
std::string core_arg_class_id, std::string core_arg_object_id,
std::string arg_name, std::string arg_fhandle)
: Core { core_arg_host_url, core_arg_owner_vsID,
core_arg_class_id, core_arg_object_id }
{
this->dirent_vector_ptr = new std::vector<Dirent *>();
this->name = arg_name;
this->fhandle = arg_fhandle;
}
Json::Value
Directory::ObtainChunkURL
(std::string arg_name, std::string arg_fhandle, std::string arg_chunk_index)
{
Json::Value result;
result["primary"] = (this->the_chunk).chunk_url_primary;
result["secondary_A"] = (this->the_chunk).chunk_url_secondary_A;
result["secondary_B"] = (this->the_chunk).chunk_url_secondary_B;
return result;
}
Json::Value
Directory::LookUp
(std::string arg_dir_fhandle, std::string arg_name)
{
int i;
int flag = 0;
Json::Value result;
if (arg_dir_fhandle == this->fhandle)
{
for (i = 0; i < (this->dirent_vector_ptr)->size(); i++)
{
if (((*(this->dirent_vector_ptr))[i])->name == arg_name)
{
result["fhandle"] = ((*(this->dirent_vector_ptr))[i])->fhandle;
flag = 1;
}
}
if (flag == 0)
{
result["status"] = "NFSERR_NOENT";
}
else
{
result["status"] = "NFS_OK";
}
}
else
{
result["status"] = "NFSERR_STALE";
}
return result;
}
Json::Value
Directory::Create
(std::string arg_dir_fhandle, std::string arg_name, std::string arg_sattr)
{
int i;
Json::Value result;
if (arg_dir_fhandle == this->fhandle)
{
int flag = 0;
for (i = 0; i < (this->dirent_vector_ptr)->size(); i++)
{
if (((*(this->dirent_vector_ptr))[i])->name == arg_name)
{
flag = 1;
}
}
if (flag == 0)
{
char fhandle_buf[256];
bzero(fhandle_buf, 256);
sprintf(fhandle_buf, "%ld%ld", random(), random());
std::string new_fhandle { fhandle_buf };
Dirent *dirent_ptr = new Dirent(arg_name, new_fhandle);
(*(this->dirent_vector_ptr)).push_back(dirent_ptr);
result["status"] = "NFS_OK";
}
else
{
result["status"] = "NFSERR_ACCES";
}
}
else
{
result["status"] = "NFSERR_STALE";
}
return result;
}
Json::Value *
Directory::dumpJ()
{
Json::Value * result_ptr = new Json::Value();
if (this->name != "")
{
(*result_ptr)["name"] = this->name;
}
if (this->fhandle != "")
{
(*result_ptr)["fhandle"] = this->fhandle;
}
int i;
if ((this->dirent_vector_ptr != NULL) &&
((this->dirent_vector_ptr)->size() > 0))
{
Json::Value json_dirents;
Json::Value json_dirents_array;
for (i = 0; i < (this->dirent_vector_ptr)->size(); i++)
{
Json::Value * dumpjv_ptr = ((*(this->dirent_vector_ptr))[i])->dumpJ();
json_dirents[i] = (*dumpjv_ptr);
delete dumpjv_ptr;
}
json_dirents_array["data"] = json_dirents;
json_dirents_array["count"] = ((int) (*(this->dirent_vector_ptr)).size());
(*result_ptr)["dirents"] = json_dirents_array;
}
std::cout << (*result_ptr).toStyledString() << std::endl;
return result_ptr;
}
bool
Directory::Jdump(Json::Value *input_json_ptr)
{
if ((input_json_ptr == NULL) ||
((*input_json_ptr).isNull() == true) ||
((*input_json_ptr).isObject() != true))
{
return false;
}
if ((((*input_json_ptr)["name"]).isNull() == true) ||
(((*input_json_ptr)["fhandle"]).isNull() == true) ||
(((*input_json_ptr)["name"]).isString() != true) ||
(((*input_json_ptr)["fhandle"]).isString() != true))
{
return false;
}
this->name = ((*input_json_ptr)["name"]).asString();
this->fhandle = ((*input_json_ptr)["fhandle"]).asString();
// check if there is any valid directory entry
if ((((*input_json_ptr)["dirents"]).isNull() == false) &&
(((*input_json_ptr)["dirents"]).isObject() == true) &&
(((*input_json_ptr)["dirents"]["data"]).isNull() == false) &&
(((*input_json_ptr)["dirents"]["data"]).isArray() == true) &&
(((*input_json_ptr)["dirents"]["data"]).size() > 0))
{
int i;
for (i = 0; i < ((*input_json_ptr)["dirents"]["data"]).size(); i++)
{
Json::Value l_jv_dirent = (*input_json_ptr)["dirents"]["data"][i];
Dirent *l_dirent_ptr = new Dirent();
bool rc = l_dirent_ptr->Jdump(&(l_jv_dirent));
if (rc == true)
{
// now check if the dirent already exist
int flag_dr = 0;
if (this->dirent_vector_ptr != NULL)
{
std::vector<Dirent *>::iterator my_it_dr;
for (my_it_dr = (*(this->dirent_vector_ptr)).begin();
my_it_dr < (*(this->dirent_vector_ptr)).end(); my_it_dr++)
{
if (((*my_it_dr)->name) == (l_dirent_ptr->name))
{
// this is a duplicated dirent (name must be different even inode the same
flag_dr = 1;
}
}
}
else
{
// create this->dirent_vector_ptr
this->dirent_vector_ptr = new std::vector<Dirent *>();
}
if (flag_dr == 0)
{
(*(this->dirent_vector_ptr)).push_back(l_dirent_ptr);
}
else
{
delete l_dirent_ptr;
}
}
}
}
return true;
}