Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Wipe a file (Multi-platform)


This question is answered.


Permlink Replies: 3 - Last Post: Mar 30, 2015 2:56 PM Last Post By: Linden ROTH
Todd Frankson

Posts: 39
Registered: 12/19/14
Wipe a file (Multi-platform)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 30, 2015 9:47 AM
All the code I can find regarding wiping a file, includes FlushFileBuffers which is a Windows only API.

How can I flush a TFilestream on other platforms?

procedure WipeFile(FileName: string);
var
  buffer: array [0..4095] of Byte;
  max, n: LongInt;
  i: Integer;
  fs: TFileStream;
 
  procedure RandomizeBuffer;
  var
    i: Integer;
  begin
    for i := Low(buffer) to High(buffer) do
      buffer[i] := Random(256);
  end;
begin
  fs := TFilestream.Create(FileName, fmOpenReadWrite or fmShareExclusive);
  try
    for i := 1 to 3 do
    begin
      RandomizeBuffer;
      max := fs.Size;
      fs.Position := 0;
      while max > 0 do
      begin
        if max > SizeOf(buffer) then
          n := SizeOf(buffer)
        else
          n := max;
        fs.Write(Buffer, n);
        max := max - n;
      end;
      FlushFileBuffers(fs.Handle); <---- HERE
    end;
  finally
    fs.Free;
  end;
  Deletefile(FileName);
end;
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Wipe a file (Multi-platform)
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 30, 2015 10:28 AM   in response to: Todd Frankson in response to: Todd Frankson
Todd wrote:

How can I flush a TFilestream on other platforms?

Have a look at the POSIX fsync() function.

BTW, your code does not support files that are > 2GB in size. Your max variable
needs to be Int64 instead of Longint. TStream.Size has been Int64 since
Delphi 6. Also, you should use fs.WriteBuffer() instead of fs.Write() to
ensure the entire buffer is written (you are ignoring Write()'s return value).

--
Remy Lebeau (TeamB)
Todd Frankson

Posts: 39
Registered: 12/19/14
Re: Wipe a file (Multi-platform)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 30, 2015 11:28 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Todd wrote:

How can I flush a TFilestream on other platforms?

Have a look at the POSIX fsync() function.

BTW, your code does not support files that are > 2GB in size. Your max variable
needs to be Int64 instead of Longint. TStream.Size has been Int64 since
Delphi 6. Also, you should use fs.WriteBuffer() instead of fs.Write() to
ensure the entire buffer is written (you are ignoring Write()'s return value).

--
Remy Lebeau (TeamB)

Thanks Remy.

I haven't used this code. It popped up on a search.
Linden ROTH

Posts: 467
Registered: 11/3/11
Re: Wipe a file (Multi-platform)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 30, 2015 2:56 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Todd wrote:

How can I flush a TFilestream on other platforms?

Have a look at the POSIX fsync() function.

BTW, your code does not support files that are > 2GB in size. Your max variable
needs to be Int64 instead of Longint. TStream.Size has been Int64 since
Delphi 6. Also, you should use fs.WriteBuffer() instead of fs.Write() to
ensure the entire buffer is written (you are ignoring Write()'s return value).

--
Remy Lebeau (TeamB)

He should also be looking at the cluster size and filling slack space as well ...

But in general the need to do multi writes has long been deemed to be unnecessary - but if you are going to (or need to - due to specification ) do multi random is not the correct style but rather alternate patterns

Also from a forensics point of view this does not handle moved files (and other OS considerations)

--
Linden
"Mango" was Cool but "Wasabi" was Hotter but remember it's all in the "source"
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02