-
Notifications
You must be signed in to change notification settings - Fork 2
/
File.cc
205 lines (181 loc) · 6.75 KB
/
File.cc
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
/*
* File: File.cpp
* Author: Tobias Fleig <tobifleig@gmail.com>
*
* Created on September 16, 2015, 11:26 PM
*/
#include "File.h"
#include <cmath>
#include <cstdint>
#include <iostream>
#include <list>
#include <memory>
#include <vector>
#include "DataBlockList.h"
#include "Directory.h"
#include "FileINode.h"
#include "IDataBlockListCreator.h"
#include "DataBlock.h"
namespace SDI4FS {
File::File(IDataBlockListCreator *blockListCreator, std::unique_ptr<FileINode> primary, std::list<uint32_t> *blockListIDs) : blockListCreator(blockListCreator), inode(std::move(primary)), blockLists(), numberOfDataBlocks(0) {
if (!inode->isInlined()) {
numberOfDataBlocks = ceil(inode->getInternalSize_b() / ((float) SDI4FS_MAX_BYTES_PER_DATABLOCK));
// copy list of DataBlockLists
size_t numberOfDataBlockLists = ceil(numberOfDataBlocks / ((float) SDI4FS_MAX_DATABLOCKS_PER_DATABLOCKLIST));
for (size_t i = 0; i < numberOfDataBlockLists; ++i) {
blockListIDs->push_back(inode->getDataBlockList(i));
}
// copying the ids requests them to be loaded. caller will (=must) call init before using this object to complete construction
}
}
File::File(IDataBlockListCreator *blockListCreator, std::unique_ptr<FileINode> empty) : blockListCreator(blockListCreator), inode(std::move(empty)), blockLists(), numberOfDataBlocks(0) {
// nothing else to do, file has size 0 and is empty
}
void File::init(std::vector<std::unique_ptr<DataBlockList>> &blockLists) {
// sanity check
if (inode->isInlined()) {
std::cout << "fs: error - wrong initialization of file object" << std::endl;
}
// copy to internal list
for (auto &list : blockLists) {
this->blockLists.push_back(list.release());
}
}
FileINode& File::getPrimaryINode() {
return *inode.get();
}
uint32_t File::getNumberOfDataBlocks() {
return numberOfDataBlocks;
}
std::list<Block*> File::convertToNonInline(std::unique_ptr<DataBlock> dataBlock) {
std::list<Block*> changedBlocks;
// sanity check
if (!inode->isInlined()) {
std::cout << "fs: error - cannot convert non-inlined inode to non-inlined mode again" << std::endl;
return changedBlocks;
}
// create first DataBlockList (caller guarantees this will never return null)
DataBlockList *newList = blockListCreator->alloc();
// set first DataBlock, then save list
newList->pushDataBlock(dataBlock->getId());
inode->convertToNonInline(newList, *dataBlock.get());
blockLists.push_back(newList);
++numberOfDataBlocks;
// caller must save inode, new list, datablock
changedBlocks.push_back(newList);
changedBlocks.push_back(&getPrimaryINode());
changedBlocks.push_back(dataBlock.get());
// set cached
setCachedDataBlock(std::move(dataBlock));
return changedBlocks;
}
std::list<Block*> File::addDataBlock(std::unique_ptr<DataBlock> dataBlock) {
std::list<Block*> changedBlocks;
// sanity check
if (inode->isInlined()) {
std::cout << "fs: error - cannot add DataBlock to inline-mode file " << inode->getId() << std::endl;
return changedBlocks;
}
// new DataBlockList required?
if (numberOfDataBlocks % SDI4FS_MAX_DATABLOCKS_PER_DATABLOCKLIST == 0) {
// alloc new list, put in inode (caller guarantees this will never return null)
DataBlockList *newList = blockListCreator->alloc();
if (!inode->pushDataBlockList(newList->getId())) {
std::cout << "fs: error - cannot save new DataBlockList, file " << inode->getId() << " is full" << std::endl;
return changedBlocks;
}
blockLists.push_back(newList);
changedBlocks.push_back(&getPrimaryINode());
}
// free DataBlockList slot is guaranteed now
DataBlockList *last = blockLists[blockLists.size() - 1];
last->pushDataBlock(dataBlock->getId());
changedBlocks.push_back(last);
++numberOfDataBlocks;
setCachedDataBlock(std::move(dataBlock));
return changedBlocks;
}
std::list<Block*> File::removeDataBlock() {
std::list<Block*> changedBlocks;
// sanity check
if (inode->isInlined()) {
std::cout << "fs: error - cannot remove a DataBlock from inline-mode file " << inode->getId() << std::endl;
return changedBlocks;
}
// remove last DataBlock first
DataBlockList *last = blockLists[blockLists.size() - 1];
last->popDataBlock();
--numberOfDataBlocks;
// last DataBlockList now empty?
if (numberOfDataBlocks % SDI4FS_MAX_DATABLOCKS_PER_DATABLOCKLIST == 0 && numberOfDataBlocks > SDI4FS_MAX_DATABLOCKS_PER_DATABLOCKLIST) { // not if only 1 list left
// yes, remove now-empty DataBlockList
blockListCreator->dealloc(last);
blockLists.pop_back();
inode->popDataBlockList();
// inode was modified
changedBlocks.push_back(&getPrimaryINode());
} else {
// modified DataBlockList still contains elements, save
changedBlocks.push_back(last);
}
return changedBlocks;
}
uint32_t File::getDataBlockID(uint32_t blockNo) {
if (inode->isInlined()) {
std::cout << "fs: error - cannot retrieve a DataBlock from inline-mode file " << inode->getId() << std::endl;
return 0;
}
if (blockNo >= numberOfDataBlocks) {
return 0;
}
// get list index, then ask the list
uint32_t listNo = blockNo / SDI4FS_MAX_DATABLOCKS_PER_DATABLOCKLIST;
DataBlockList *blockList = blockLists[listNo];
return blockList->getDataBlock(blockNo % SDI4FS_MAX_DATABLOCKS_PER_DATABLOCKLIST);
}
void File::blocks(std::list<uint32_t> &result) {
result.push_back(inode->getId());
if (!inode->isInlined()) {
for (DataBlockList *list : blockLists) {
result.push_back(list->getId());
list->blocks(result);
}
}
}
void File::setCachedDataBlock(std::unique_ptr<DataBlock> dataBlock) {
cachedDataBlock = std::move(dataBlock);
}
uint32_t File::getCachedDataBlockID() {
if (cachedDataBlock) {
return cachedDataBlock->getId();
} else {
return 0;
}
}
bool File::cachedDataBlockIsDirty() {
if (cachedDataBlock) {
return cachedDataBlock->isDirty();
}
return false;
}
std::unique_ptr<DataBlock> File::releaseCachedDataBlock() {
return std::move(cachedDataBlock);
}
bool File::readFromCachedDataBlock(char *target, uint32_t pos, std::size_t n) {
if (!cachedDataBlock) {
return false;
}
return cachedDataBlock->read(target, pos, n);
}
bool File::writeToCachedDataBlock(const char *source, uint32_t pos, std::size_t n) {
if (!cachedDataBlock) {
return false;
}
return cachedDataBlock->write(source, pos, n);
}
File::~File() {
for (auto iter = blockLists.begin(); iter != blockLists.end(); ++iter) {
delete *iter;
}
}
} // SDI4FS