0

Consider this snippet (handle was opened with FILE_SYNCHRONOUS_IO_NONALERT flag):

IO_STATUS_BLOCK io;
NTSTATUS r = NtWriteFile(h, NULL, NULL, NULL, &io, data, data_len, &pos, NULL);

if NT_SUCCESS(r)
    assert(io.Information == data_len);      // can we succeed with short write?
else
    // is IO_STATUS_BLOCK filled?
    assert(io.Information == 0);             // can we fail and yet write some data?

Is it possible for NtWriteFile() to succeed and yet write less than requested?

Is it possible for NtWriteFile() to fail and yet write some of data? If yes -- how to determine amount of data written?

C.M.
  • 3,071
  • 1
  • 14
  • 33
  • for both questions - must not. if of course disk stack not fail during write. device must or write exactly requested size or return error. this is by design. but need understand - that is driver implemented. depend from concrete driver. ms file system drivers (ntfs,fat,refs) of course try implement logic - or write all data or return error and not write any data – RbMm Jun 17 '20 at 22:23
  • @RbMm Hmm... It looks like the best approach would be to add checks for short write and fail with "bad driver behaviour" error. Does `IO_STATUS_BLOCK` get filled in second case (i.e. in case of `not NT_SUCCESS`)? – C.M. Jun 17 '20 at 22:35
  • 1
    if returned *status* in range `[0, 0x80000000)` *iosb* will be filled by io manager. if returned *status* in range `[0xC0000000, 0xFFFFFFFF)` *iosb* will be not filed. if returned status in rage - `[0x80000000, 0xC0000000)` - no general answer - in absolute most case must be filled. exist several exceptions, but not for read/write file. so *iosb* will be filled is *status* in range `[0, 0xC0000000)` – RbMm Jun 17 '20 at 23:01
  • I see... Better not to use `NT_SUCCESS()` then. *iosb* is **not** filled if `NT_ERROR(status)` and filled -- otherwise. Thanks a lot, @RbMm. – C.M. Jun 17 '20 at 23:07
  • yes, exactly by `NT_ERROR(status)` we detect are *iosb* filled. exist several exceptions, but not for read/write file. but anyway for concrete read/write api logic *Information* can be not 0 only in case `0 <= status` – RbMm Jun 17 '20 at 23:46

0 Answers0