raw Win32api

Ryan Malayter rmalayter@bai.org
Wed Oct 10 20:18:01 2001


Thanks for pointing me in the right direction, Janke.

I've now got a working shred utility (created in the MinGW environment so it
can be GPL'd) which does unbuffered writes and uses CryptoAPI to generate
secure random data for overwrites. I've got to do some sort of byte-by-byte
writing with small files (which are stored directly in the MFT on NTFS
systems), and add a bit more error checking, and I'll post it.

If anyone has suggestions as to how to force overwrites on small files
stored directly in the MFT, please let me know. FILE_FLAG_WRITE_THROUGH will
work in this case, but FILE_FLAG_NO_BUFFERING will not, since it is not a
full-sector write. I'm thinking a byte-by-byte overwrite of the small file,
followed by a call to FlushFileBuffers() will do the trick, but I'm not
quite sure if NTFS will force the write out to the MFT as it does with a
standard file in a standard sector.

Thanks for any insights,
	-ryan-


:::Ryan Malayter, MCSE
:::Bank Administration Institute
:::Chicago, Illinois, USA
-----Original Message----- From: Janke van Aalst [mailto:janke1a@clerk.com] Sent: Monday, October 08, 2001 6:16 PM To: rmalayter@bai.org Subject: raw Win32api > How can I force non-cached file writes with the Win32 API? Piece of pie. Easy as cake. You may want to go to microsoft.com and look up their 'Platform SDK' help; there's a page for each function. #include <windows.h> static HANDLE FH; // not equiv to unix filehandle char *fName; FH = CreateFile( fName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, // or 0 NULL, // security attribs not relevant OPEN_ALWAYS, // or OPEN_EXISTING FILE_FLAG_NO_BUFFERING|FILE_FLAG_WRITE_THROUGH, NULL // 'templates' are rarely used... ); if (FH==INVALID_HANDLE_VALUE) return NULL; // or whatever // To get extended error information, call GetLastError. // You use the WriteFile() primitive to write data. // ... then, eventually ... CloseHandle(FH); // You may need to close and reopen it between passes, // or some such nonsense. NOTE: An application must meet certain requirements when using FILE_FLAG_NO_BUFFERING: *File access must begin at byte offsets on disk that are integer multiples of the volume's sector size. *File access must be for numbers of bytes that are integer multiples of the volume's sector size. For example, if the sector size is 512 bytes, an application can request reads and writes of 512, 1024, or 2048 bytes. You'll probably want a buffer of 4096 bytes. *Buffer addresses IN MEMORY for read and write operations should be sector aligned (aligned on addresses in memory that are integer multiples of the volume's sector size). One way to align buffers on integer multiples of the volume sector size is to use VirtualAlloc to allocate the buffers. -- JJK, jkane89@softhome.net -- _______________________________________________ Sign-up for your own FREE Personalized E-mail at Mail.com http://www.mail.com/?sr=signup Have you downloaded the latest calling software from Net2Phone? Click here to get it now! http://www.net2phone.com/cgi-bin/adforward.cgi?p_key=NH211JK&url=http://comm center.net2phone.com/