-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEsfsFile.cs
178 lines (136 loc) · 5.03 KB
/
EsfsFile.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
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
using System;
using System.IO;
namespace EsfsLite
{
public class EsfsFile
{
private const int AllocationChunkSectors = 8;
private readonly EsfsFreespace _freespace;
private readonly EsfsFileInfo _fileInfo;
private Int64 _pointer;
private readonly EsfsChain _chain;
public EsfsFile(EsfsSal salMount, EsfsFileInfo fileInfo, EsfsFreespace freespace)
{
_fileInfo = fileInfo;
_freespace = freespace;
_pointer = 0;
_chain = new EsfsChain(salMount, fileInfo.StartSector);
}
public Int64 Size => _fileInfo.Size;
public byte[] GetMeta()
{
return _fileInfo.Meta;
}
public void SetMeta(byte[] metaData)
{
Array.Copy(metaData, _fileInfo.Meta, Math.Min(metaData.Length, _fileInfo.Meta.Length));
StoreFileInfo();
}
public void Seek(Int64 pointer, SeekOrigin seekOrigin)
{
if (seekOrigin == SeekOrigin.Begin)
{
_pointer = pointer;
}
else if (seekOrigin == SeekOrigin.Current)
{
_pointer += pointer;
}
else
{
_pointer = _fileInfo.Size - pointer;
}
if (_pointer < 0)
{
_pointer = 0;
}
}
public long ReadBytes(byte[] buffer, int offset, int length)
{
if (_pointer >= _fileInfo.Size)
{
return 0;
}
var bytesToRead = (long) length;
var bytesRemainingInFile = _fileInfo.Size - _pointer;
if (bytesToRead > bytesRemainingInFile)
{
bytesToRead = bytesRemainingInFile;
}
var sectorIndex = _pointer/Esfs.SectorSizeContentBytes;
var bytesIndex = _pointer%Esfs.SectorSizeContentBytes;
var bytesRemainingToRead = bytesToRead;
var targetOffset = offset;
while (bytesRemainingToRead > 0)
{
_chain.Seek((int) sectorIndex + 1);
_chain.Read();
var bytesAvailable = _chain.Data.Length - bytesIndex;
if (bytesAvailable > bytesRemainingToRead)
{
bytesAvailable = bytesRemainingToRead;
}
Array.Copy(_chain.Data, bytesIndex, buffer, targetOffset, bytesAvailable);
bytesIndex = 0;
sectorIndex++;
bytesRemainingToRead -= bytesAvailable;
targetOffset += (int) bytesAvailable;
}
_pointer += bytesToRead;
return bytesToRead;
}
public void WriteBytes(byte[] buffer, int offset, int length)
{
var fileInfoNeed2BeUpdated = false;
var needMoreBytes = (_pointer + length) - (_chain.Length() - 1) * Esfs.SectorSizeContentBytes;
if (needMoreBytes > 0)
{
var needMoreSectors = (1 + needMoreBytes / Esfs.SectorSizeContentBytes) + AllocationChunkSectors;
var appendix = _freespace.GetChunk((int) needMoreSectors);
_chain.Glue(appendix);
_fileInfo.EndSector = _chain.End();
fileInfoNeed2BeUpdated = true;
}
var sectorIndex = _pointer / Esfs.SectorSizeContentBytes;
var bytesIndex = _pointer % Esfs.SectorSizeContentBytes;
var bytesToWrite = length;
var sourceOffset = offset;
while (bytesToWrite > 0)
{
_chain.Seek((int)sectorIndex + 1);
var bytesAvailable = _chain.Data.Length - bytesIndex;
if (bytesAvailable > bytesToWrite)
{
bytesAvailable = bytesToWrite;
}
if (bytesIndex != 0 || bytesAvailable != _chain.Data.Length)
{
_chain.Read();
}
Array.Copy(buffer, sourceOffset, _chain.Data, bytesIndex, bytesAvailable);
_chain.Store();
bytesIndex = 0;
sectorIndex++;
sourceOffset += (int) bytesAvailable;
bytesToWrite -= (int) bytesAvailable;
}
_pointer += length;
if (_pointer > _fileInfo.Size)
{
_fileInfo.Size = _pointer;
fileInfoNeed2BeUpdated = true;
}
if (fileInfoNeed2BeUpdated)
{
StoreFileInfo();
}
}
private void StoreFileInfo()
{
_chain.Seek(0);
var fileInfo = _fileInfo.ToBytes();
Array.Copy(fileInfo, _chain.Data, fileInfo.Length);
_chain.Store();
}
}
}