patterncppMinor
A program to shred files
Viewed 0 times
shredfilesprogram
Problem
The following is a program to shred files securely:
```
#include
#include
#include
#include
#include
#include
#include
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include
using namespace std;
using namespace boost::filesystem;
static ofstream Log;
static stringstream LogErrorStream; // Buffer soft errors to output them separately after the informational messages in the log file.
// 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& dirContents )
{
if ( is_directory( dirPath ) )
{
copy( directory_iterator( dirPath ), directory_iterator(), back_inserter( dirContents ) );
}
}
// Generates a random file name
static string GenerateRandomFileName()
{
const size_t maxFileNameLength = 19;
const size_t fileNameLength = ( rand() % maxFileNameLength ) + 1;
const string acceptedFileNameCharacters = "0123456789abcdefghijklmnopqrstuvwxyz";
string fileName( fileNameLength, '*' );
for ( auto& c : fileName )
{
c = acceptedFileNameCharacters[rand() % acceptedFileNameCharacters.length()];
}
return fileName;
}
// 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 buffer( inputFileSize );
////////////////////////////////////////////////////////////////////
// Overwrite file with ASCII 255 and ASCII 0 multiple times alternately
int iterations = 5;
while ( iterations -- )
```
#include
#include
#include
#include
#include
#include
#include
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include
using namespace std;
using namespace boost::filesystem;
static ofstream Log;
static stringstream LogErrorStream; // Buffer soft errors to output them separately after the informational messages in the log file.
// 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& dirContents )
{
if ( is_directory( dirPath ) )
{
copy( directory_iterator( dirPath ), directory_iterator(), back_inserter( dirContents ) );
}
}
// Generates a random file name
static string GenerateRandomFileName()
{
const size_t maxFileNameLength = 19;
const size_t fileNameLength = ( rand() % maxFileNameLength ) + 1;
const string acceptedFileNameCharacters = "0123456789abcdefghijklmnopqrstuvwxyz";
string fileName( fileNameLength, '*' );
for ( auto& c : fileName )
{
c = acceptedFileNameCharacters[rand() % acceptedFileNameCharacters.length()];
}
return fileName;
}
// 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 buffer( inputFileSize );
////////////////////////////////////////////////////////////////////
// Overwrite file with ASCII 255 and ASCII 0 multiple times alternately
int iterations = 5;
while ( iterations -- )
Solution
const size_t fileNameLength = ( rand() % maxFileNameLength ) + 1;So 1 out of ever 19 times, you get a 1-character filename. This means that you can expect to repeat filenames once every 684 times. (Actually, slightly more frequently than that: you also get a 2-character filename 1-out-of-19 times which contributes another 1-in-24624)
Don't most OSes have a routine that generates a guaranteed-to-be-unique filename for temporary file purposes? And if not, Boost has
boost::filesystem::unique_path().Code Snippets
const size_t fileNameLength = ( rand() % maxFileNameLength ) + 1;Context
StackExchange Code Review Q#85573, answer score: 4
Revisions (0)
No revisions yet.