forked from gcsjsd/fileStorageEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSFHeader.cpp
More file actions
172 lines (161 loc) · 5.49 KB
/
Copy pathSFHeader.cpp
File metadata and controls
172 lines (161 loc) · 5.49 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
#include "SFHeader.h"
#include <unordered_set>
#include <unordered_map>
std::vector<int> SFHeader::assignChunks(int chunkNum) {
std::vector<int> res;
std::unordered_set<int> used;
for (int i = 0; i < this->blocks.size(); i++) {
if (this->blocks[i].exist) {
for (int j = 0; j < this->blocks[i].chunks.size(); j++) {
used.insert(this->blocks[i].chunks[j]);
}
}
}
int cur_chunk = 0;
int assigned = 0;
while (assigned != chunkNum) {
if (used.find(cur_chunk) != used.end()) {
res.push_back(cur_chunk);
assigned++;
}
cur_chunk++;
}
return res;
}
std::vector<int> SFHeader::addFileHeader(block_i &block, std::ofstream archive) {
int size = block.size;
int chunkNum = size / chunk_size;
if (size % chunk_size != 0) {
chunkNum++;
}
std::vector<int> assignedChunks = this->assignChunks(chunkNum);
for (int i = 0; i < chunkNum; i++) {
block.chunks.push_back(assignedChunks[i]);
}
int blockIdx = 0;
for (int i = 0; i < this->blocks.size(); i++) {
if (!this->blocks[i].exist) {
blockIdx = i;
break;
}
}
block.exist = true;
int pos = blockIdx*block_size;
int writeSize = block_size;
archive.seekp(pos);
archive.write((char*)&block, writeSize);
return assignedChunks;
}
void SFHeader::delFileHeader(int atype, std::string aname, std::ofstream archive) {
int blockIdx = 0;
for (int i = 0; i < this->blocks.size(); i++) {
int type = this->blocks[i].type;
std::string name(this->blocks[i].name);
if (type == atype && name == aname) {
this->blocks[i].exist = false;
blockIdx = i;
break;
}
}
int pos = blockIdx*block_size;
int writeSize = block_size;
archive.seekp(pos);
archive.write((char*)&(this->blocks[i]), writeSize);
}
std::vector<int> SFHeader::getFile(int atype, std::string aname) {
for (int i = 0; i < this->blocks.size(); i++) {
int type = this->blocks[i].type;
std::string name(this->blocks[i].name);
if (type == atype && name == aname && this->blocks[i].exist) {
return this->blocks[i].chunks;
}
}
}
int SFHeader::getFileSize(int atype, std::string aname) {
for (int i = 0; i < this->blocks.size(); i++) {
int type = this->blocks[i].type;
std::string name(this->blocks[i].name);
if (type == atype && name == aname && this->blocks[i].exist) {
return this->blocks[i].size;
}
}
}
void SFHeader::readHeader(std::ifstream& archive) {
archive.seekg(0);
archive.read((char*)&(this->blocks), header_size);
}
void SFHeader::writeHeader(std::ofsream& archive) {
archive.seekp(0);
archive.write((char*)&(this->blocks), header_size);
}
void SFHeader::updateWholeHeader(std::fstream& archive) {
int usedChunks = 0;
int neededChunks = 0;
archive.seekg(0, std::ios::end);
int size = archive.tellg() - header_size;
usedChunks = size / chunk_size;
for (int i = 0; i < this->blocks.size(); i++) {
if (this->blocks[i].exist) {
neededChunks += (this->blocks[i].chunks.size());
}
}
if (usedChunks > neededChunks*2) {
std::vector<block_i> newBlocks;
for (int i = 0; i < maxFileNumber; i++) {
newBlocks[i].type = AVA;
for (int j = 0; j < name_size; j++) {
newBlocks[i].name[j] = 'x';
}
newBlocks[i].exist = false;
newBlocks[i].size = 0;
newBlocks[i].date = __DATE__;
}
int newPtr = 0;
int chunkPtr = 0;
std::unordered_map<int, int> newOldBlockMap;
for (int i = 0; i < this->blocks.size(); i++) {
if (this->blocks[i].exist) {
for (int j = 0; j < this->blocks[i].chunks.size(); j++) {
newBlocks[newPtr].chunks.push_back(chunkPtr);
chunkPtr++;
}
newBlocks[newPtr].exist = true;
for (int j = 0; j < name_size; j++) {
newBlocks[newPtr].name[j] = this->blocks[i].name[j];
}
for (int j = 0; j < 10; j++) {
newBlocks[newPtr].date[j] = this->blocks[i].date[j];
}
newOldBlockMap[newPtr] = i;
newBlocks[newPtr].size = this->blocks[i].size;
newBlocks[newPtr].type = this->blocks[i].type;
newPtr++;
}
}
//TODO
/* 0.create new archive, write header to new archive.
*
* 1.write content to new archive by calling SFile.writeWholeArchive()
* 2.delete old archive, rename new archive to archive.bin.
*/
}
}
bool contains(char* name, std::string s) { //TODO: check if the name contains string s.
}
void SFHeader::listFiles(std::string s) {
// go through this->blocks, check file name, list.
for (int i = 0; i < this->blocks.size(); i++) {
if (this->blocks[i].exist) {
if (contains(this->blocks[i].name, s)) {
std::cout << this->blocks[i].name << " " << this->blocks[i].size <<"byte " << this->blocks[i].date << endl;
}
}
}
}
void SFHeader::listFiles() {
for (int i = 0; i < this->blocks.size(); i++) {
if (this->blocks[i].exist) {
std::cout << this->blocks[i].name << " " << this->blocks[i].size <<"byte " << this->blocks[i].date << endl;
}
}
}