-
Notifications
You must be signed in to change notification settings - Fork 0
/
inode.cpp
248 lines (229 loc) · 5.46 KB
/
inode.cpp
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
//
// Created by 旧城筱雨 on 2021/5/28.
//
#include "inode.h"
using namespace std;
INode cache;
string cacheFilename;
//模拟i结点的位示图,用来分配的时候查看i结点的空闲情况
bool iNodeDistributeList[INODE_NUM];
INode::INode(){}
//复制构造函数,方便将i节点表copy到内存的i结点表中
INode::INode(const INode &A)
{
type = A.type;
dir = A.dir;
fileLen = A.fileLen;
diskSize = A.diskSize;
setTime = A.setTime;
username = A.username;
updateTime = A.updateTime;
i_Nlink = A.i_Nlink;
content = A.content;
indexT = A.indexT;
}
// 赋值构造函数
INode::INode(int type, string setTime, string updateTime, string username, int fileLen, int diskSize, int i_Nlink, string content)
: type(type)
, setTime(setTime)
, updateTime(updateTime)
, username(username)
, fileLen(fileLen)
, diskSize(diskSize)
, i_Nlink(i_Nlink)
, content(content)
{
}
//重载等号运算符,返回一个引用对象
INode& INode::operator=(const INode& B)
{
this->dir = B.dir;
this->type = B.type;
this->updateTime = B.updateTime;
this->setTime = B.setTime;
this->diskSize = B.diskSize;
this->fileLen = B.fileLen;
this->username = B.username;
this->i_Nlink = B.i_Nlink;
this->content = B.content;
this->indexT = B.indexT;
return *this;
}
//更新i结点的相关信息
void INode::updateFileSize()
{
if(type == 1)
fileLen = sizeof(dir);
else
fileLen = sizeof(content);
}
// 返回文件大小
int INode::size() const
{
return fileLen;
}
int INode::disksize()
{
return diskSize;
}
// 返回文件变化情况
int INode::differ()
{
int t;
if(type == 1)
{
// cout << "sizeof dir = " << sizeof(dir) << " filelen = " << fileLen << endl;
t = sizeof(dir) - fileLen;
}
else
t = sizeof(content) - fileLen;
fileLen = t;
return (int)ceil((double)t / (double)BLOCK_SIZE);
}
// 移除一个块
int INode::freeBlock()
{
return indexT.dropIndex();
}
// 添加一个块
bool INode::addBlock(int id)
{
return (indexT.addIndex(id)) ? (diskSize++, true):(false);
}
// 获取块数
int INode::num()
{
return diskSize;
}
// 获取用户名
string INode::getUser()
{
return username;
}
// 清空inode
void INode::clear()
{
type = -1;
dir.clear();
fileLen = 0;
diskSize = 0;
// setTime = ;
username = "";
i_Nlink = 0;
content = "";
indexT.clear();
}
//更新i结点
void INodeList::UpdateInode(int id, INode ano)
{
inodeList[id] = ano;
}
//计算i结点对应文件内容的大小
int INode::calculateFileSize(const string& filename)
{
ifstream file(filename);
if(!file.is_open())
return -1;
string t;
file >> noskipws >> t;
return t.size();
}
//获取硬连接数
int INode::check()
{
return i_Nlink;
}
// 获取i节点表的空闲项编号
int INodeList::getFreeInodeNum()
{
for(int i=0; i<INODE_NUM; i++)
{
if(!iNodeDistributeList[i])
return i;
}
return -1;
}
// 添加新的i结点
bool INodeList::addNewINode(INode A, int i) {
inodeList[i] = A;
iNodeSize++;
return true;
}
void INodeList::FreeInvalidInode(int pos) {
iNodeDistributeList[pos] = false;
inodeList[pos].clear();
iNodeSize--;
}
INode& INodeList::getInode(int id)
{
return inodeList[id];
}
void INode::show()
{
indexT.show();
}
//显示所有i结点的信息
void INodeList::getSpecificInodeInfo(int pos) {
//type == 0表示该i结点为一个文件, type == 1表示该i结点为一个目录
cout << "the relevant info are as below:" << endl;
cout << "the size of inode is " << inodeList[pos].size() << endl;
cout << "the type of inode is" << inodeList[pos].getType() << endl;
// if(inodeList[pos].getType() == 0){
// cout << "the inode is stored as a file" << endl;
// }
// else if (inodeList[pos].getType() == 1){
// cout << "the inode is stored as a directory" << endl;
// }
cout << "the inode belongs to " << inodeList[pos].getUser() << endl;
cout << "the length of inode is" << inodeList[pos].size() << endl;
cout << "the block num of inode is" << inodeList[pos].num() << endl;
if(inodeList[pos].getType() == 0){
cout << "the content of the file is: " << inodeList[pos].content << endl;
}
else if(inodeList[pos].getType() == 1){
cout << "the component of the directory is: " << endl;
inodeList[pos].dir.show();
}
cout << endl;
}
string INode::save_as_string() {
string ans = "";
ans = username + "\n" + to_string(type) + "\n" +
to_string(i_Nlink) + "\n" + to_string(fileLen) + "\n"
+ to_string(diskSize) + "\n" + setTime + updateTime;
//ans += "aaa:" + (to_string(dir.size()) + '\n');
// vector<int> tmp = indexT.getIndexes();
// ans += tmp.size() + "\n";
// ans += "\n---------------------\n";
// for(int i : tmp){
// ans += to_string(i);
// ans += '\n';
// }
// ans += "\n---------------------\n";
return ans;
}
int INode::getType() {
return type;
}
void INode::addLink()
{
i_Nlink++;
}
bool INode::delLink()
{
i_Nlink--;
if(i_Nlink == 0)
return true;
else
return false;
}
void INode::saveDiskNumber(const vector<int>& numberSet) {
for(int i : numberSet){
indexT.addIndex(i);
}
}
bool INode::inodeIsAuthor(string username) {
if(getUser() == username)
return true;
return false;
}