Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
166 lines (138 loc) · 4.8 KB
/
Copy pathProgram.cs
File metadata and controls
166 lines (138 loc) · 4.8 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
using System.Security.Cryptography;
using System.Diagnostics;
namespace SFill;
class Program
{
private const int BufferSize = 1024 * 1024; // 1 MB buffer
private const string FillFileName = "sfill.tmp";
private static volatile bool _cancelRequested;
static int Main(string[] args)
{
var stopwatch = Stopwatch.StartNew();
var workingDirectory = args.Length > 0 ? args[0] : Directory.GetCurrentDirectory();
if (!Directory.Exists(workingDirectory))
{
Console.Error.WriteLine($"Error: Directory does not exist: {workingDirectory}");
return 1;
}
var filePath = Path.Combine(workingDirectory, FillFileName);
var freeSpace = new DriveInfo(Path.GetPathRoot(filePath) ?? filePath).AvailableFreeSpace;
PrintTimestamp("Process started");
Console.WriteLine($"sfill - Filling free space with random data");
Console.WriteLine($"Target: {filePath}");
Console.WriteLine($"Free space: {FormatBytes(freeSpace)}");
Console.WriteLine("Press Ctrl+C to abort");
Console.WriteLine();
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true;
_cancelRequested = true;
Console.WriteLine("\nAborting...");
};
try
{
FillDrive(filePath, freeSpace, stopwatch);
CleanupFile(filePath);
if (_cancelRequested)
{
Console.WriteLine("\nAborted. Temporary file removed.");
PrintElapsedTime(stopwatch.Elapsed);
PrintTimestamp("Process finished");
return 1;
}
Console.WriteLine("\nDrive filled successfully. Temporary file removed.");
PrintElapsedTime(stopwatch.Elapsed);
PrintTimestamp("Process finished");
return 0;
}
catch (Exception ex)
{
Console.Error.WriteLine($"\nError: {ex.Message}");
CleanupFile(filePath);
PrintElapsedTime(stopwatch.Elapsed);
PrintTimestamp("Process finished");
return 1;
}
}
private static void FillDrive(string filePath, long freeSpace, Stopwatch stopwatch)
{
var buffer = new byte[BufferSize];
long totalWritten = 0;
using var file = new FileStream(
filePath,
FileMode.Create,
FileAccess.Write,
FileShare.None,
BufferSize,
FileOptions.WriteThrough);
while (!_cancelRequested)
{
RandomNumberGenerator.Fill(buffer);
try
{
file.Write(buffer);
totalWritten += BufferSize;
if (totalWritten % (100 * BufferSize) == 0)
{
Console.Write($"\rWritten: {FormatBytes(totalWritten)} - ETA: {FormatEta(totalWritten, freeSpace, stopwatch.Elapsed)}. ");
}
}
catch (IOException ex) when (IsDiskFullException(ex))
{
Console.WriteLine($"\rWritten: {FormatBytes(totalWritten)} - Disk full");
break;
}
}
}
private static bool IsDiskFullException(IOException ex)
{
const int ERROR_DISK_FULL = unchecked((int)0x80070070);
const int ERROR_HANDLE_DISK_FULL = unchecked((int)0x80070027);
return ex.HResult == ERROR_DISK_FULL || ex.HResult == ERROR_HANDLE_DISK_FULL;
}
private static void CleanupFile(string filePath)
{
try
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
catch
{
// Best effort cleanup
}
}
private static string FormatBytes(long bytes)
{
string[] suffixes = ["B", "KB", "MB", "GB", "TB"];
int suffixIndex = 0;
double size = bytes;
while (size >= 1024 && suffixIndex < suffixes.Length - 1)
{
size /= 1024;
suffixIndex++;
}
return $"{size:F2} {suffixes[suffixIndex]}";
}
private static string FormatEta(long written, long freeSpace, TimeSpan elapsed)
{
var remaining = freeSpace - written;
if (remaining <= 0 || elapsed.TotalSeconds <= 0 || written <= 0)
{
return "calculating...";
}
var bytesPerSecond = written / elapsed.TotalSeconds;
var etaMinutes = remaining / bytesPerSecond / 60.0;
return $"{etaMinutes:F1} min remaining";
}
private static void PrintElapsedTime(TimeSpan elapsed)
{
Console.WriteLine($"Elapsed time: {elapsed:hh\\:mm\\:ss}");
}
private static void PrintTimestamp(string message)
{
Console.WriteLine($"{message}: {DateTime.Now:yyyy-MM-dd HH:mm:ss zzz}");
}
}