Article View: borland.public.cppbuilder.nativeapi
Article #11044Re: FindFirstFile, FindNext Problem
From: Martin C
Date: Sun, 29 Feb 2004 22:10
Date: Sun, 29 Feb 2004 22:10
218 lines
6761 bytes
6761 bytes
Hi Remy,
First of all thanks Remy for the code, I've tried your code and it is
working fine now. Although I need to ask you another question related to
this function. I hope you can help me with it also.
As long as I use DeleteFile function there is no problem at all, all files
are deleted fast. But when I add some functions to securely overwrite and
delete files so it can't be recovered, I find that the DeleteAllFilesFolders
function delete some files and leave other, without any logical reason why
those files where left, in other words if I am testing on a folder that
contains 10 files, it can leave the first 2 onetime, then sometimes the last
2...etc.
Thanks so much for your help.
Looking forward for your reply..
BR
Here is what I do, please tell me what I am doing wrong in it, which in
return cause such errors in deleting files
//Inside the DeleteAllFilesFolders I changed your code to the following
if( MatchesMask(wfd.cFileName, MaskToFind) )
{
if(SecureDeleteOn)
{
Result = DeleteFile(sFullPath);
}
else
{ Result = SecureDeleteFiles(sFullPath);
}
}
void __fastcall TForm1::SecureDeleteFiles(AnsiString FileInformation)
{
HANDLE hFind;
WIN32_FIND_DATA FindFileData;
hFind = FindFirstFile((LPCTSTR)FileInformation.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{ //File not found...
}
else
{ //Start deletint File Securly...
SecureDelete1(FileName.c_str(), FindFileData.nFileSizeHigh,
FindFileData.nFileSizeLow);
}
}
//--------------------------------------------------------------------------
--
void __fastcall TForm1::SecureDelete1(PTCHAR FileName, DWORD FileLengthHi,
DWORD FileLengthLo)
{
HANDLE hFile;
ULONGLONG bytesToWrite, bytesWritten;
ULARGE_INTEGER fileLength;
TCHAR lastFileName[MAX_PATH];
hFile = CreateFile( FileName, GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_FLAG_WRITE_THROUGH, NULL );
if( hFile == INVALID_HANDLE_VALUE )
{
return;
}
if( FileLengthLo || FileLengthHi )
{
// Seek to the last byte of the file
FileLengthLo--;
if( FileLengthLo == (DWORD) -1 && FileLengthHi ) FileLengthHi--;
SetFilePointer( hFile, FileLengthLo, (PLONG)&FileLengthHi, FILE_BEGIN);
if( !SecureOverwriteFileWRandom(hFile, 1))
{ CloseHandle( hFile ); return;
}
SetFilePointer( hFile, 0, NULL, FILE_BEGIN);
fileLength.LowPart = FileLengthLo;
fileLength.HighPart = FileLengthHi;
bytesWritten = 0;
while(bytesWritten < fileLength.QuadPart)
{
bytesToWrite = min(fileLength.QuadPart - bytesWritten, 65536);
if(!SecureOverwriteFileWRandom(hFile, (DWORD)
bytesToWrite))
{ CloseHandle( hFile ); return;
}
bytesWritten += bytesToWrite;
}
}
CloseHandle( hFile );
// Now we can delete the file
if(!DeleteFile(lastFileName))
{
return;
}
}
//--------------------------------------------------------------------------
-
bool __fastcall TForm1::SecureOverwriteFileWRandom(HANDLE FileHandle, DWORD
Length)
{
#define CLEANBUFSIZE 65536
static PBYTE cleanBuffer[3];
static BOOLEAN buffersAlloced = FALSE;
DWORD i, j, passes;
DWORD bytesWritten, bytesToWrite, totalWritten;
LONG seekLength;
BOOLEAN status;
// Allocate our cleaning buffers if necessary (we just let program exit
free the buffers)...
if( !buffersAlloced )
{
// Seed the random number generator
srand( (unsigned)time( NULL ) );
for( i = 0; i < 3; i++ )
{
(LPVOID)cleanBuffer[i] = VirtualAlloc( NULL, CLEANBUFSIZE, MEM_COMMIT,
PAGE_READWRITE );
if( !cleanBuffer[i])
{
for( j = 0; j < i; j++ )
{ VirtualFree( cleanBuffer[j], 0,
MEM_RELEASE );
}
return FALSE;
}
// Fill each buffer with a different signature
switch(i){
case 0:
// do nothing - buffer is zero-filled by Windows
break;
case 1:
// fill with complement of 0 - 0xFF
memset( cleanBuffer[i], 0xFF, CLEANBUFSIZE );
break;
case 2:
// fill with a random value
for( j = 0; j < CLEANBUFSIZE; j++ ) cleanBuffer[i][j] = (BYTE) rand();
break;
}
}
buffersAlloced = TRUE;
}
// Do the overwrite
seekLength = (LONG) Length;
for( passes = 0; passes < 3; passes++ )
{
if( passes != 0 )
{ SetFilePointer( FileHandle, -seekLength, NULL,
FILE_CURRENT );
}
for( i = 0; i < 3; i++ )
{
//Move back to the start of where we're overwriting
if( i != 0 )
{ SetFilePointer( FileHandle, -seekLength, NULL,
FILE_CURRENT );
}
// Loop and overwrite
bytesToWrite = Length;
totalWritten = 0;
while( totalWritten < Length )
{
bytesToWrite = Length - totalWritten;
if( bytesToWrite > CLEANBUFSIZE ) bytesToWrite = CLEANBUFSIZE;
status = WriteFile( FileHandle, cleanBuffer[i], bytesToWrite,
&bytesWritten, NULL );
if( !status ) return FALSE;
totalWritten += bytesWritten;
}
}
}
return TRUE;
}
//--------------------------------------------------------------------
"Remy Lebeau (TeamB)" <gambit47.no.spam@no.spam.yahoo.com> wrote in message
news:4042b964$1@newsgroups.borland.com...
>
> Try this code instead:
Technical Details
Message-ID:
<4042c611@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.wirehub.nl!news2.euro.net!news.cambrium.nl!news.cambrium.nl!border1.nntp.ash.giganews.com!nntp.giganews.com!newshosting.com!nx01.iad01.newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<4042b3bc@newsgroups.borland.com> <4042b964$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,cd3253abe6cdd94c X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2004-02-29 21:17:26 PST From: "Martin C" <martinc@aol.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <4042b3bc@newsgroups.borland.com> <4042b964$1@newsgroups.borland.com> Subject: Re: FindFirstFile, FindNext Problem Date: Mon, 1 Mar 2004 00:10:21 +0200 Lines: 220 Organization: V Safe X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 81.10.4.238 Message-ID: <4042c611@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1078117905 81.10.4.238 (29 Feb 2004 21:11:45 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.wirehub.nl!news2.euro.net!news.cambrium.nl!news.cambrium.nl!border1.nntp.ash.giganews.com!nntp.giganews.com!newshosting.com!nx01.iad01.newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail