-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSystem.cs
144 lines (122 loc) · 3.56 KB
/
FileSystem.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OneBigBox
{
class FileSystem
{
public Loc loc;
public HashSet<Block> blockPool;
public List<File> ToC;
public FileSystem()
{
blockPool = new HashSet<Block>();
ToC = new List<File>();
}
public void add(Block block)
{
blockPool.Add(block);
}
public void open(Block block)
{
loc = new Loc(block, 0);
blockPool.Add(block);
}
public File GetFile(string name)
{
return ToC.FirstOrDefault(x => x.name == name);
}
public byte[] readFileBytes(File f)
{
byte[] o = new byte[f.length];
int m = 0;
foreach(Region r in f.regions)
{
for (int i = r.a; i < r.b; i ++)
o[m++] = r.block.get(i);
}
return o;
}
public void writeNext()
{
}
List<Block> getFreeBlocks()
{
return blockPool.Where((x) => !x.isFull).OrderByDescending((x) => x.usedSpace).ToList();
}
void ensureFreeSpace()
{
if (loc.block.isFull)
{
var free = getFreeBlocks();
if (free.Count < 1) throw new Exception("All full!");
loc.block = free[0];
loc.pos = 0;
}
}
public Region findSmallestFreeSpace()
{
List<Region> regs = new List<Region>();
foreach (var block in getFreeBlocks())
{
regs.Add(block.GetSmallestRegion());
}
return regs.Min();
}
public File writeFile(string name, byte[] bytes)
{
int ii = 0;
Region currentReg;
List<Region> regions = new List<Region>();
while (ii < bytes.Length)
{
Loc oldLoc = loc;
ensureFreeSpace();
currentReg = findSmallestFreeSpace();
loc.pos = currentReg.a;
loc.block = currentReg.block;
if (ii == 0)
{
// done here after we find an initial writing pos
// loc.block.ToC.Add((File)receipt);
}
else
{
// oldLoc.block.FileSystem.addJump(oldLoc, loc);
}
int z = 0;
while (loc.pos < currentReg.b && ii < bytes.Length)
{
loc.block.writeByte(loc.pos++, bytes[ii++]);
z++;
}
regions.Add((loc.pos < currentReg.b) ? new Region(loc.block, currentReg.a, currentReg.a + z) : currentReg);
}
File f = new File(regions, bytes.Length, name);
ToC.Add(f);
return f;
}
public void delete(File f)
{
int z = 0;
ToC.Remove(f);
HashSet<Block> blocks = new HashSet<Block>();
foreach (Region r in f.regions)
{
r.block.freeRegion(r);
blocks.Add(r.block);
}
foreach (Block b in blocks)
{
b.MergeRegions();
}
}
private void deleteRegion(Region region)
{
// HELP
}
}
}