Re: CreateFile() and FILE_FLAG_WRITE_THROUGH

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Try:

CreateFile"testfile.txt",APPEND|SYNCHRONISE,
0,NULL,CREATE_NEW,FILE_FLAG_WRITE_THROUGH,NULL);


"Nick Tucker" <nick.tucker@xxxxxxxxxxxxxxxxx> wrote in message
news:CEAA587B-08B5-4C88-9A7F-E85857B1B4A0@xxxxxxxxxxxxxxxx
Hi Alexander

Thanks for repling.

The code looks like this

HANDLE h
=CreateFile("testfile.txt",GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_FLAG_WRITE_THROUGH,NULL);
BYTE b[1024]={0};
DWORD w;
WriteFile(h,b,sizeof(b),&w,NULL);

POWER OFF!!!

On restart, the file testfile.txt exists but is zero bytes long because
although
the file data has been written to disk, the metadata has not.

So our so-called safe write to disk, is in this situation completely
useless!

I think I've now grasped why this is so - you MUST call FileFlushBuffers()
if
the data you're writing has not previously been written out and fully
flushed by
FileFlushBuffers() (or by the file being closed).

So if we add the steps:

FlushFileBuffers();
LONG l;
SetFilePointer(h,0,&l,SEEK_SET);
memset(b,1,sizeof(b));

We can then happily cut the power, confident that the data will have been
overwritten with 1's, even though we haven't flushed the file.

I hope you're of my opinion that Microsoft could do better in spelling out
this behaviour!

Thanks

Nick

"Alexander Grigoriev" <alegr@xxxxxxxxxxxxx> wrote in message
news:u9N%23Fx7EJHA.4104@xxxxxxxxxxxxxxxxxxxxxxx
What does your CreateFile call look like?

"Nick Tucker" <nick.tucker@xxxxxxxxxxxxxxxxx> wrote in message
news:A1959885-3F61-4C46-A742-4AA2FB55FEB2@xxxxxxxxxxxxxxxx
Hi everyone

If I create a file using CreateFile(), specifying the
FILE_FLAG_WRITE_THROUGH flag, then write to that file, the file still
shows
as zero bytes long in the directory and in Windows Explorer, and if I
then
turn the power off, that file is still zero bytes long on restart, so
the
data I wrote is unavailable. I have tried this test on a variety of
operating systems including Vista, XP and Windows Server 2003/2008 using
a
variety of hardware platforms with both SATA and SCSI disk systems, and
they
all behave the same way.

Surely the whole point of specifying that flag is that I want the data
to be
written to disk, together with it's corresponding metadata, so if a
catastrophic failure occurs, the data is available to me on restart.

My researches of newsgroup articles on this subject suggest that if I
want
this, then I also have to use FlushFileBuffers(), but if that is so then
surely I don't need to specify the write through flag since all file and
folder data will be committed to disk when I call that function.

So, what is the point of specifying the write-through flag in this
scenario?

I think the documentation of CreateFile() could do with a little
clarification!

Thanks in advance for responses.

Nick






.



Relevant Pages

  • Re: CreateFile() and FILE_FLAG_WRITE_THROUGH
    ... On restart, the file testfile.txt exists but is zero bytes long because although ... So our so-called safe write to disk, is in this situation completely useless! ... We can then happily cut the power, confident that the data will have been ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Rebooting
    ... > Device Manager) expand Disk Drives> right click your hard disk and ... Restart in Safe Mode, do a disk clean up and defrag> restart in safe mode ... > HDDs and then I have to power off and then power on to restart my system. ...
    (microsoft.public.windowsxp.general)
  • Re: Restart on AC
    ... power disconnect? ... Restart automatically after a power failure. ... What I'd like to do is have it shutdown cleanly, ... If you are concerned about this, you could have it run a script to dismount all drives except the boot disk. ...
    (comp.sys.mac.system)
  • Re: Windows XP Pro is FUBARd!!! Help!
    ... Get your friend to mail back your xp cd ... Free up a gig or so of some hard disk space. ... New HD and reinstall windows preferably with a slipstreamed SP2 XP CD. ... Upon restart, the blue screen of death came back. ...
    (microsoft.public.windowsxp.perform_maintain)
  • Re: Ext4 and the "30 second window of death"
    ... A - Writing data to disk immediately and lose no work at all, ... application uses fsync, right?). ... The problem comes when you have lots of applications open on the ... in order to save power. ...
    (Linux-Kernel)