Callback function for failed disk write?

Callback function for failed disk write?

Post by S2VpdGggUH » Sun, 13 Nov 2005 02:49:10


I need to detect a failed disk write through any of the following mechanisms
and am hoping that there is a Win32 API call that allows registration of a
callback function for this kind of event.

Writing to a file stream (fstream)
WriteFile()
WriteFileEx()
fprintf()
fwrite()
printf() (directed to a filestream)

Does anyone know of such a facility?
 
 
 

Callback function for failed disk write?

Post by Jochen Kal » Sun, 13 Nov 2005 03:21:35

Hi Keith!

Why not use the return value of "WriteFile" ???
Or do you mean the "physical write"?
In this case you either need to implement the ACID fundamentals by
yourself, or you can simply use a (real) database like MS-SQL/Oracle.

YOu also can wait until Vista. Here the operating system supports
transactions on NTFS-file-system.

--
Greetings
Jochen

My blog about Win32 and .NET
http://www.yqcomputer.com/

 
 
 

Callback function for failed disk write?

Post by S2VpdGggUH » Sun, 13 Nov 2005 03:37:07

I accidentally left out one important tidbit and that is I need to detect
that the write failed as a result of the disk being full. AFAIK, most of
these "file writing" functions don't return that kind of error code, but I
thought Windows may provide a callback for this sort of thing.

Waiting for Vista and using a DBMS are not viable alternatives for my
situation. Thanks for the feedback.
 
 
 

Callback function for failed disk write?

Post by Jochen Kal » Sun, 13 Nov 2005 04:31:30

Hi Keith!

???

If the disk is full, then WriteFille returns FALSE... (or at least
dwBytesWritten is less than the bytesCount you had requested).


--
Greetings
Jochen

My blog about Win32 and .NET
http://www.yqcomputer.com/
 
 
 

Callback function for failed disk write?

Post by Vincent Fa » Sun, 13 Nov 2005 04:47:54

On Fri, 11 Nov 2005 10:37:07 -0800, Keith Pulfer



A quick test (of actually filling up a disk) shows that when appropriate,
WriteFile() fails and GetLastError() returns 112 ("There is not enough space
on the disk."). Smilarly, fwrite() fails to write the specified number of
items and GetLastError() returns 112.
--
- Vince
 
 
 

Callback function for failed disk write?

Post by Hector San » Sun, 13 Nov 2005 19:13:50

Keith,

As Jochen pointed out, the disk full error number is 122.

But why wait until disaster strikes?

If you are going to detect somthing, detect low disk space, maybe as some
fix number or percent. These days, low disk space is around 20 gigs. <g>

Its the same concept. The difference being only a human perception of whats
consider a bad state of operations. To me, low space is BAD! <g>

Its simple to do. Write a wrapper function for your Writefile() function.

BOOL kpWriteFile(,,,, const dwMinSpace = 0);

This makes it optional when dwMinSpace = 0. but if its non-zero, then your
wrapper would call GetDiskFreeSpaceEx() first to see if you have enough
space. If you don't, set an error ERROR_DISK_FULL and return FALSE. That
would be a lie, of course, but I don't see a ERROR_DISK_LOW_SPACE error
available. Let see if there is a near error that says "low" or
"insufficient".. Nah, nothing close that I can see. You could always just
create your own ERROR_LOW_DISK_SPACE number, just return the ERROR_DISK_FULL
(112) error.

Anyway, its just an idea.

Good luck









mechanisms
 
 
 

Callback function for failed disk write?

Post by Pavel A » Mon, 14 Nov 2005 00:24:55


Detecting failed disk writes is very easy: disk failures are logged in the event log.
But receiving calback in your app is not easy because the write is
actually asynchronous.
The data goes to the memory cache, only much later it gets
written to a physical disk or over network. This can occur even
after the program exits, so if there is a failure, the original program
may be unable to handle this.
Some win32 API give you more control of the physical write.
Read on CreateFile and WriteFile in MSDN.
Detection of disk full is a different case because this is allocation error,
not a physical write error. This error is immediately returned to your app
so you can handle it.

Regards,
--PA
 
 
 

Callback function for failed disk write?

Post by Hector San » Mon, 14 Nov 2005 13:01:22

I seem to recall in the past seeing Windows throwing up a "Low Disk Space"
dialog.

Do you know if Windows will broadcast this as a message event? I wouldn't
mind adding this to our high end disk intensive package.
 
 
 

Callback function for failed disk write?

Post by Arkady Fre » Mon, 14 Nov 2005 13:55:08

Hector , check http://www.yqcomputer.com/ , I really don't
know about notification ( seems not ) , but IMHO wmi may be useful to read
it
Arkady
 
 
 

Callback function for failed disk write?

Post by UGF2ZWwgQS » Mon, 14 Nov 2005 19:29:04


AFAIK everybody who wants to know free disk space does polling on their own.
For example:

http://www.yqcomputer.com/ ;en-us;299921&sd=tech
http://www.yqcomputer.com/

So you can roll your own as well. Query for free space on local disks is
cheap.

Regards,
--PA
 
 
 

Callback function for failed disk write?

Post by Hector San » Mon, 14 Nov 2005 20:08:50

You should read the entire thread :-) I already suggested a wrapper
solution.

Since Windows does check for low disk space, I was just wondering if it was
or is capable of sharing that info by broadcasting it with a registered
message maybe.

Apparently not.

Thanks





Space"
wouldn't
own.
 
 
 

Callback function for failed disk write?

Post by UGF2ZWwgQS » Mon, 14 Nov 2005 23:24:04


Hector, I've read entire thread :)
Apology if my reply wasn't clear enough - what I mean, _Windows as
a whole_ does not check for low disk space - therefore no _global_ event.
I've done a quick check with object viewer and haven't noticed such event.
My guess is, every component does this alone, based on it's own needs
(one of reasons why the shell checks for low space may be emptying the
Recycle bin).

Regards,
--PA
 
 
 

Callback function for failed disk write?

Post by Hector San » Tue, 15 Nov 2005 01:43:29


I don't use the Recycle Bin (or allow the system to put anything in it, i.e.
"shift delete" via explorer, but 99% of the time I'm in the console and
delete this way, when needed) but that doesn't mean the Recycle Bin it is
isn't checking. Isn't it even running as a process or service?

Anyway, I think your are right. I always thought it would be the virtual
memory manager that is doing the checking since that would be more global to
the system, and it does log it in the event logs. So if the OS is checking,
its only for the paging drives. I suspect it won't check for other
non-paging drives or partitions - no criteria to work with. So a custom
solution is the way to do this.
 
 
 

Callback function for failed disk write?

Post by Vincent Fa » Tue, 15 Nov 2005 03:44:55

On Sun, 13 Nov 2005 11:43:29 -0500, "Hector Santos"



That must depend on having some service running or on properties of the
drive itself or on something; it's not automatic. Here, XP SP2, I made a
test drive (8MB primary partition on second hard drive) and filled it (and
tried to over-fill it) many times. As I mentioned before, when my test
program tries to write beyond the disk's capacity, the *only* indication is
GetLastError()'s 112. When I try with Explorer, I get (only) a "Copy Error"
message box. In my testing, I have not seen any "low disk" warnings and
there are no relevant Event Log entries.
--
- Vince
 
 
 

Callback function for failed disk write?

Post by Hector San » Tue, 15 Nov 2005 07:53:12


Vincent,

Did you try it as the virutal memory paging drive?

Right click:

My Computer | Advanced | Performance Options | Virtual Memory.

I know for sure that NT 4.0 has an event log for low disk space because back
then I had few NT machines with low space disk on the page drive and I use
to see many Low Space event entry warnings. I'll be surprise if MS remove
this for 2000, XP or newer. But for non-paging drives, I wouldn't expect it
and as Pavel said, you have to roll your own on this.

In fact, I googled the words: Low disk space event log

and found this:

Tip #225: Low Disk Space Warning
http://www.yqcomputer.com/

showing you the registry option for changing the default 90% threshold and
according to the tip, it applies to all volumes, which if true, would be a
surprise to me.

In the same search result, I found this interesting too:

"How To Add Error Logging for Low Disk Space in System Monitor in Windows
2000"

http://www.yqcomputer.com/ ;en-us;299921&sd=tech

and there is a bunch of other useful links on the subject.