question

noname-1014 avatar image
0 Votes"
noname-1014 asked noname-1014 commented

WriteFile and partial writes

Is it possible for WriteFile() to perform a partial write? Let's say we want to write 100 bytes to a file and only 50 bytes can be written (for example, the disk is full). Would WriteFile() return success and *lpNumberOfBytesWritten would be 50? Would WriteFile() return error and *lpNumberOfBytesWritten would be 50?

When using asynchronous I/O (opening the file with FILE_FLAG_OVERLAPPED and passing a pointer to an overlapped struct in WriteFile()): can the data be partially written to disk (for example, 50 bytes can be immediately written to the file but not the remainder 50 bytes)? would WriteFile() return success and *lpNumberOfBytesWritten would be 50? would WriteFile() return error (ERROR_IO_PENDING) and *lpNumberOfBytesWritten would be 50?

windows-api
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks

0 Votes 0 ·

1 Answer

RLWA32-6355 avatar image
1 Vote"
RLWA32-6355 answered noname-1014 commented

Whether using synchronous or asynchronous I/O if a call to WriteFile is unable to write the number of bytes specified because the disk is full then the write fails, 0 bytes are reported as written, and the last error code is set to 112 (There is not enough space on the disk).

· 10
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much for your answer.
When using asynchronous I/O, are partial writes possible (for example, the driver is not fast enough)?
Asked another way, when WriteFile() returns TRUE, do I have to check the number of bytes written (*lpNumberOfBytesWritten) or is it not needed because *lpNumberOfBytesWritten == nNumberOfBytesToWrite? When WriteFile() returns FALSE, do I have to check the number of bytes written or is it always 0?

0 Votes 0 ·

The documentation for WriteFile makes some points that you should note. For asynchronous operation the documentation of the lpNumberOfBytesWritten parameter states "Use NULL for this parameter if this is an asynchronous operation to avoid potentially erroneous results." Also, it says "The lpNumberOfBytesWritten parameter should be set to NULL. To get the number of bytes written, use the GetOverlappedResult function. If the hFile parameter is associated with an I/O completion port, you can also get the number of bytes written by calling the GetQueuedCompletionStatus function." When the lpNumberOfBytesWritten parameter is non-NULL for synchronous operations the documentation also points out that "WriteFile sets this value to zero before doing any work or error checking."

When WriteFile returns TRUE it means that the operation has succeeded. The documentation does not gurarantee that the number of bytes written will always be equal to the amount requested. However, in the context of a successful file I/O operation, in my experience this has been the case.

When WriteFile is used for asynchronous operations a return of FALSE indicates that GetLastError() should be called to determine if the I/O operation is pending or if an error has occurred.

I believe my earlier response has already addressed your questions about partial writes, and the reported number of bytes written when an I/O operation has failed.




0 Votes 0 ·

I have also read that I should use NULL for lpNumberOfBytesWritten when performing asynchronous I/O operations.

My file is associated with an I/O completion port (CreateThreadpoolIo() / StartThreadpoolIo()). I am using SetFileCompletionNotificationModes() with the flag FILE_SKIP_COMPLETION_PORT_ON_SUCCESS. If the operation can complete immediately, the completion callback doesn't have to be called, I just have to cancel the I/O operation (CancelThreadpoolIo()) right afterwards.

In your first response you said what happened when the disk is full, that's why I wanted to know if this behavior "either everything can be written at once or nothing" is always expected.

0 Votes 0 ·
Show more comments