-
Notifications
You must be signed in to change notification settings - Fork 4
/
file_shredder.cpp
313 lines (276 loc) · 7.71 KB
/
file_shredder.cpp
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
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include <boost/filesystem.hpp>
using namespace std;
using namespace boost::filesystem;
// File handles for log files
static std::ofstream logError, logInfo;
typedef unsigned long long ULL;
// Returns the number of files in the input directory
// The progress status is printed after every `milestone` files are visited.
// Here "file" refers to regular files
ULL GetNumberOfFiles ( const path& inputPath = ".",
const ULL& milestone = 1000 )
{
if ( !is_directory ( inputPath ) )
{
return 1;
}
ULL numberOfFiles = 0;
if ( milestone == 0 )
{
numberOfFiles = std::distance ( recursive_directory_iterator ( inputPath ),
recursive_directory_iterator() );
}
else
{
ULL filesVisitedIterator = 0;
for ( recursive_directory_iterator it ( inputPath );
it != recursive_directory_iterator(); ++it )
{
if ( is_regular_file ( *it ) )
{
filesVisitedIterator++;
if ( filesVisitedIterator == milestone )
{
numberOfFiles += filesVisitedIterator;
filesVisitedIterator = 0;
cout << "\rFiles visited = " << numberOfFiles << std::flush;
}
}
}
numberOfFiles += filesVisitedIterator;
if ( filesVisitedIterator )
{
cout << "\rFiles visited = " << numberOfFiles << std::flush;
}
cout << endl;
}
return numberOfFiles;
}
// Iterate through a directory and store everything found ( regular files, directories or any other special files ) in the input container
static void DirectoryIterate ( const path& dirPath, vector<path>& dirContents )
{
if ( is_directory ( dirPath ) )
{
copy ( directory_iterator ( dirPath ), directory_iterator(),
back_inserter ( dirContents ) );
}
}
// Generates a random file name
static string GenerateRandomFileName()
{
return unique_path().string();
}
// Renames the input file to a random string
static path RandomRename ( const path& inputPath )
{
const size_t maxRenameAttempts =
10; // Max number of attempts to make in renaming a file.
// It is possible that the random filename generator returns a name
// which is same as that of a file in the current directory,
// or is a reserved filename ( like "com1" on windows )
size_t attempts = 0;
while ( attempts < maxRenameAttempts )
{
const path newPath = inputPath.parent_path() / GenerateRandomFileName();
if ( !exists ( newPath ) )
{
boost::system::error_code ec;
rename ( inputPath, newPath, ec );
if ( ec )
{
// Maybe a reserved filename was generated
logError << "Failure in renaming " << absolute ( inputPath ) << " to " <<
absolute ( newPath ) << " : " << ec.message() << endl;
}
else // No error
{
return newPath;
}
}
attempts++;
}
return inputPath;
}
// Overwrite the input file with random data
// Returns true if successful, false otherwise
static bool WriteRandomData ( const path& inputPath )
{
boost::system::error_code ec;
const auto inputFileSize = file_size ( inputPath, ec );
if ( ec )
{
logError << "Failure in determining the size of " << absolute (
inputPath ) << " : " << ec.message() << endl;
return false;
}
std::ofstream fout ( inputPath.string(), ios::binary );
if ( !fout )
{
logError << "Failure in opening " << absolute ( inputPath ) << " : " <<
strerror ( errno ) << endl;
return false;
}
vector<unsigned char> buffer ( inputFileSize );
////////////////////////////////////////////////////////////////////
// Overwrite file with ASCII 255 and ASCII 0 multiple times alternately
int iterations = 5;
while ( iterations -- )
{
const unsigned char c = ( iterations & 1 ) ? 255 : 0;
for ( auto& bufferElement : buffer )
{
bufferElement = c;
}
fout.seekp ( 0, ios::beg );
fout.write ( ( char* ) &buffer[0], buffer.size() );
fout.flush();
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Overwrite file with random characters
for ( auto& bufferElement : buffer )
{
bufferElement = rand() % 128;
}
fout.seekp ( 0, ios::beg );
fout.write ( ( char* ) &buffer[0], buffer.size() );
fout.flush();
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Overwrite file with null characters
for ( auto& bufferElement : buffer )
{
bufferElement = 0;
}
fout.seekp ( 0, ios::beg );
fout.write ( ( char* ) &buffer[0], buffer.size() );
fout.flush();
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Change file size to 0
fout.close();
fout.open ( inputPath.string(), ios::binary );
////////////////////////////////////////////////////////////////////
fout.close();
return true;
}
// Shred a file
static void ShredFile ( const path& inputPath )
{
if ( !WriteRandomData ( inputPath ) )
{
return;
}
const path newPath = RandomRename ( inputPath );
boost::system::error_code ec;
remove ( newPath, ec );
if ( ec )
{
logError << "Failure in removing " << newPath << " : " << ec.message() << endl;
}
}
// Confirm shredding a file
// Returns true if the user confirms, false otherwise
static bool ConfirmShred ( const path& inputPath )
{
static ULL fileCount = 1;
cout << "\n" << fileCount++ << ". Shred " << absolute ( inputPath ) <<
" ?\n( y/n ) ";
const bool confirm = ( cin.get() == 'y' );
// Clear any trailing input
cin.clear();
cin.ignore ( numeric_limits<std::streamsize>::max(), '\n' );
return confirm;
}
// Shred a file/folder
static void Shred ( const path& inputPath )
{
logInfo << absolute ( inputPath ) << endl;
boost::system::error_code ec;
if ( is_directory ( inputPath, ec ) )
{
vector<path> dirContents;
try
{
DirectoryIterate ( inputPath, dirContents );
}
catch ( const filesystem_error& ex )
{
logError << ex.what() << endl;
return; // Not able to list the directory's contents. No point in proceeding further.
}
for ( const auto& item : dirContents )
{
Shred ( item );
}
ec.clear();
// Remove the input directory itself
// Will fail if not empty
remove ( inputPath, ec );
if ( ec )
{
logError << "Failure in removing " << absolute ( inputPath ) << " : " <<
ec.message() << endl;
}
}
// input is a file
else if ( !ec )
{
if ( ConfirmShred ( inputPath ) )
{
logInfo << "Shredding " << absolute ( inputPath ) << endl;
ShredFile ( inputPath );
}
else
{
logInfo << "Skipping " << absolute ( inputPath ) << endl;
}
}
else
{
logError << "Failure in determining if " << absolute ( inputPath ) <<
" is a directory or not : " << ec.message() << endl;
}
}
int main ( int argc, char** argv )
{
path logFolderPath = "./logs";
if ( argc < 2 )
{
cout << "Usage : " << argv[0] << " <input_path> [log_file_folder=" <<
logFolderPath << "]\n";
return -1;
}
if ( argc > 2 )
{
logFolderPath = path ( argv[2] );
}
try
{
const path inputPath = canonical ( argv[1] );
create_directories ( logFolderPath );
const path errorLogPath = logFolderPath / "errors.log";
logError.open ( errorLogPath.string() );
const path infoLogPath = logFolderPath / "info.log";
logInfo.open ( infoLogPath.string() );
cout << "Calculating number of files to shred ...\n";
const ULL filesInInputPath = GetNumberOfFiles ( inputPath );
cout << "Files to shred = " << filesInInputPath << "\n";
cout << "\nInitiating the shredding process ...\n\n";
Shred ( inputPath );
cout << "\n\n";
}
catch ( const exception& ex )
{
cerr << ex.what() << endl;
logError << ex.what() << endl;
}
}