-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathProgram.cs
76 lines (70 loc) · 2.34 KB
/
Program.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
using System;
using System.IO;
namespace BadBlocksPlaceholder
{
class Program
{
static void Main(string[] args)
{
string targetDir;
if (args[0] == "clean")
{
if (!args[1].ToUpper().Contains("BADBLOCKPLACEHOLDERS"))
{
Console.WriteLine("Only BadBlockPlaceholders folder is allowed to be cleaned");
return;
}
targetDir = args[1];
}
else
{
var drive = new DriveInfo(args[0]);
int blockSize = int.Parse(args[1]) * 1024;
targetDir = CreateBlocks(drive, blockSize);
}
Validate(targetDir);
Console.WriteLine("Done!");
}
private static string CreateBlocks(DriveInfo drive, int blockSize)
{
var block = new byte[blockSize];
var bbDir = Path.Combine(drive.RootDirectory.FullName, "BadBlockPlaceholders");
if (!Directory.Exists(bbDir))
Directory.CreateDirectory(bbDir);
var targetDir = Path.Combine(bbDir, DateTime.Now.ToString("yyyyMMdd"));
if (!Directory.Exists(targetDir))
Directory.CreateDirectory(targetDir);
int index = 0;
while (drive.AvailableFreeSpace > blockSize)
{
Console.WriteLine("Creating block #" + index);
var filename = Path.Combine(targetDir, index + ".bad");
++index;
using (var filestream = File.OpenWrite(filename))
{
filestream.Write(block, 0, blockSize);
filestream.Flush();
filestream.Close();
}
}
return targetDir;
}
private static void Validate(string targetDir)
{
Console.WriteLine("Validating blocks");
foreach (var file in Directory.GetFiles(targetDir))
{
try
{
var res = File.ReadAllBytes(file);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
continue;
}
File.Delete(file);
}
}
}
}