-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathSerialFile.cs
349 lines (295 loc) · 9.02 KB
/
SerialFile.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Waher.Runtime.IO;
using Waher.Runtime.Threading;
namespace Waher.Persistence.Files
{
/// <summary>
/// Serializes binary blocks into a file, possibly encrypted. Blocks are accessed in the order they were persisted.
/// </summary>
public class SerialFile : IDisposable
{
private const int MinBlockSize = 64;
private readonly MultiReadSingleWriteObject fileAccess;
private readonly FileStream file;
private readonly string fileName;
private readonly bool encrypted;
private readonly bool fileExists;
private readonly bool asyncFileIo;
private Aes aes;
private byte[] aesKey;
private byte[] ivSeed;
private int ivSeedLen;
private bool disposed = false;
/// <summary>
/// Collection Name
/// </summary>
protected readonly string collectionName;
/// <summary>
/// Serializes binary blocks into a file, possibly encrypted. Blocks are accessed in the order they were persisted.
/// </summary>
/// <param name="FileName">Name of file</param>
/// <param name="CollectionName">Collection Name</param>
/// <param name="Encrypted">If file is encrypted.</param>
protected SerialFile(string FileName, string CollectionName, bool Encrypted)
{
this.fileAccess = new MultiReadSingleWriteObject(this);
this.fileName = FileName;
this.collectionName = CollectionName;
this.encrypted = Encrypted;
this.fileExists = File.Exists(this.fileName);
this.asyncFileIo = FilesProvider.AsyncFileIo;
string Folder = Path.GetDirectoryName(this.fileName);
if (!string.IsNullOrEmpty(Folder) && !Directory.Exists(Folder))
Directory.CreateDirectory(Folder);
if (this.fileExists)
this.file = File.Open(this.fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
else
this.file = File.Open(this.fileName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None);
}
/// <summary>
/// Serializes binary blocks into a file, possibly encrypted. Blocks are accessed in the order they were persisted.
/// </summary>
/// <param name="FileName">Name of file</param>
/// <param name="CollectionName">Collection Name</param>
public static Task<SerialFile> Create(string FileName, string CollectionName)
{
return Create(FileName, CollectionName, false, null);
}
/// <summary>
/// Serializes binary blocks into a file, possibly encrypted. Blocks are accessed in the order they were persisted.
/// </summary>
/// <param name="FileName">Name of file</param>
/// <param name="CollectionName">Collection Name</param>
/// <param name="Encrypted">If file is encrypted.</param>
/// <param name="Provider">Provider of encryption keys.</param>
public static async Task<SerialFile> Create(string FileName, string CollectionName, bool Encrypted, FilesProvider Provider)
{
SerialFile Result = new SerialFile(FileName, CollectionName, Encrypted);
await GetKeys(Result, Provider);
return Result;
}
/// <summary>
/// Gets keys for the serial file, or decendant.
/// </summary>
/// <param name="SerialFile">SerialFile reference, or decendant.</param>
/// <param name="Provider">Provider of encryption keys.</param>
protected static async Task GetKeys(SerialFile SerialFile, FilesProvider Provider)
{
if (SerialFile.encrypted)
{
SerialFile.aes = Aes.Create();
SerialFile.aes.BlockSize = 128;
SerialFile.aes.KeySize = 256;
SerialFile.aes.Mode = CipherMode.CBC;
SerialFile.aes.Padding = PaddingMode.None;
KeyValuePair<byte[], byte[]> P = await Provider.GetKeys(SerialFile.fileName, SerialFile.fileExists);
SerialFile.aesKey = P.Key;
SerialFile.ivSeed = P.Value;
SerialFile.ivSeedLen = SerialFile.ivSeed.Length;
}
}
/// <summary>
/// File name.
/// </summary>
public string FileName => this.fileName;
/// <summary>
/// Collection name.
/// </summary>
public string CollectionName => this.collectionName;
/// <summary>
/// Gets the length of the file, in bytes.
/// </summary>
/// <returns>Length of file.</returns>
public async Task<long> GetLength()
{
await this.fileAccess.BeginRead();
try
{
return this.file.Length; // Can only change in write state
}
finally
{
await this.fileAccess.EndRead();
}
}
/// <summary>
/// Reads a binary block from the file, starting at a given position.
/// </summary>
/// <param name="Position">Position of block.</param>
/// <returns>Binary block (decrypted if file is encrypted), and the position of the following block.</returns>
public async Task<KeyValuePair<byte[], long>> ReadBlock(long Position)
{
await this.fileAccess.BeginWrite(); // Unique access required
try
{
byte[] Block = await this.ReadBlockLocked(Position, MinBlockSize);
int Pos = 0;
int c = 0;
int Offset = 0;
byte b;
do
{
b = Block[Pos++];
c |= (b & 0x7f) << Offset;
Offset += 7;
if (Offset > 31)
throw Database.FlagForRepair(this.collectionName, "Invalid block length. Possible corruption of file: " + this.fileName);
}
while ((b & 0x80) != 0);
if (c <= 0 || c > int.MaxValue)
throw Database.FlagForRepair(this.collectionName, "Invalid length. Possible corruption of file: " + this.fileName);
int BlockSize = c + Pos;
int Tail = BlockSize % MinBlockSize;
if (Tail > 0)
BlockSize += MinBlockSize - Tail;
if (BlockSize > MinBlockSize)
Block = await this.ReadBlockLocked(Position, BlockSize);
byte[] Data = new byte[c];
Array.Copy(Block, Pos, Data, 0, c);
return new KeyValuePair<byte[], long>(Data, Position + BlockSize);
}
finally
{
await this.fileAccess.EndWrite();
}
}
private async Task<byte[]> ReadBlockLocked(long Position, int NrBytes)
{
byte[] Result = new byte[NrBytes];
int NrRead;
this.file.Position = Position;
if (this.asyncFileIo)
NrRead = await this.file.TryReadAllAsync(Result, 0, NrBytes);
else
NrRead = this.file.TryReadAll(Result, 0, NrBytes);
if (NrRead < NrBytes)
throw Database.FlagForRepair(this.collectionName, "Unexpected end of file " + this.fileName + ".");
if (this.encrypted)
{
using (ICryptoTransform Aes = this.aes.CreateDecryptor(this.aesKey, this.GetIV(Position)))
{
Result = Aes.TransformFinalBlock(Result, 0, Result.Length);
}
}
return Result;
}
/// <summary>
/// Writes a binary block to the end of the file.
/// </summary>
/// <param name="Data">Binary data to write.</param>
/// <returns>Position of data block.</returns>
public async Task<long> WriteBlock(byte[] Data)
{
await this.fileAccess.BeginWrite();
try
{
return await this.WriteBlockLocked(Data);
}
finally
{
await this.fileAccess.EndWrite();
}
}
/// <summary>
/// Writes a binary block to the end of the file.
/// </summary>
/// <param name="Data">Binary data to write.</param>
/// <returns>Position of data block.</returns>
protected async Task<long> WriteBlockLocked(byte[] Data)
{
int c = 0;
int i = Data.Length;
if (i == 0)
throw new ArgumentException("Zero-length blocks not allowed.", nameof(Data));
while (i > 0)
{
i >>= 7;
c++;
}
int BlockSize = Data.Length + c;
int Tail = BlockSize % MinBlockSize;
if (Tail > 0)
BlockSize += MinBlockSize - Tail;
byte[] Block = new byte[BlockSize];
c = 0;
i = Data.Length;
while (i > 0)
{
Block[c] = (byte)(i & 127);
i >>= 7;
if (i > 0)
Block[c] |= 0x80;
c++;
}
Array.Copy(Data, 0, Block, c, Data.Length);
long Position;
Position = this.file.Length;
this.file.Position = Position;
if (this.encrypted)
{
using (ICryptoTransform Aes = this.aes.CreateEncryptor(this.aesKey, this.GetIV(Position)))
{
Block = Aes.TransformFinalBlock(Block, 0, Block.Length);
}
}
if (this.asyncFileIo)
{
await this.file.WriteAsync(Block, 0, Block.Length);
await this.file.FlushAsync();
}
else
{
this.file.Write(Block, 0, Block.Length);
this.file.Flush();
}
return Position;
}
private byte[] GetIV(long Position)
{
byte[] Input = new byte[this.ivSeedLen + 8];
Array.Copy(this.ivSeed, 0, Input, 0, this.ivSeedLen);
Array.Copy(BitConverter.GetBytes(Position), 0, Input, this.ivSeedLen, 8);
byte[] Hash;
using (SHA1 Sha1 = SHA1.Create())
{
Hash = Sha1.ComputeHash(Input);
}
Array.Resize(ref Hash, 16);
return Hash;
}
/// <summary>
/// Truncates the file.
/// </summary>
/// <param name="Length">Length at which the file will be truncated.</param>
protected async Task Truncate(long Length)
{
await this.fileAccess.BeginWrite();
try
{
this.file.SetLength(Length);
this.file.Position = Length;
}
finally
{
await this.fileAccess.EndWrite();
}
}
/// <summary>
/// <see cref="IDisposable.Dispose"/>
/// </summary>
public virtual void Dispose()
{
if (!this.disposed)
{
this.file.Dispose();
this.fileAccess.Dispose();
this.aes?.Dispose();
this.disposed = true;
}
}
}
}