how to append data to file(ZwWriteFile call)?

BlankHi,

I’m trying to append some data to a file, someone said zwwritefile could
be used for that. My file is openned with no_intermedia_buffer flag, and
the buffer size is also aligned to sector size, the call to write data will
be failed on NTFS volume with err code 0xc0000002(STATUS_NOT_IMPLEMENTED),
but the same code will success on FAT volume.

Why the NTFS reports such a error ? I can’t believe the error code is
correct.

Thanks in advance for any hit,

Regards,

Xinwei

Check your parameters again:

"The CreateOptions FILE_NO_INTERMEDIATE_BUFFERING flag prevents the file
system from performing any intermediate buffering on behalf of the
caller. Specifying this value places certain restrictions on the
caller’s parameters to other Zw…File routines, including the following:

Any optional ByteOffset passed to ZwReadFile or ZwWriteFile must be an
integral of the sector size.

The Length passed to ZwReadFile or ZwWriteFile, must be an integral of
the sector size. Note that specifying a read operation to a buffer whose
length is exactly the sector size might result in a lesser number of
significant bytes being transferred to that buffer if the end of the
file was reached during the transfer.

Buffers must be aligned in accordance with the alignment requirement of
the underlying device. This information can be obtained by calling
ZwCreateFile to get a handle for the file object that represents the
physical device, and then calling ZwQueryInformationFile with that
handle. For a list of the system FILE_XXX_ALIGNMENT values, see
DEVICE_OBJECT.

Calls to ZwSetInformationFile with the FileInformationClass parameter
set to FilePositionInformation must specify an offset that is an
integral of the sector size."

Make sure you’re asking for GENERIC_WRITE access to remove any question
of file permissions.

SXW wrote:

BlankHi,

I’m trying to append some data to a file, someone said zwwritefile could
be used for that. My file is openned with no_intermedia_buffer flag, and
the buffer size is also aligned to sector size, the call to write data will
be failed on NTFS volume with err code 0xc0000002(STATUS_NOT_IMPLEMENTED),
but the same code will success on FAT volume.

Why the NTFS reports such a error ? I can’t believe the error code is
correct.

Thanks in advance for any hit,

Regards,

Xinwei


Nick Ryan (MVP for DDK)

Thanks Nick, the buffer size and the offset are integral of 512 bytes.
Is there other size for the sector? If so,how to get them?

Thanks,

----- Original Message -----
From: “Nick Ryan”
Newsgroups: ntfsd
To: “File Systems Developers”
Sent: Saturday, August 23, 2003 1:31 AM
Subject: [ntfsd] Re: how to append data to file(ZwWriteFile call)?

> Check your parameters again:
>
> “The CreateOptions FILE_NO_INTERMEDIATE_BUFFERING flag prevents the file
> system from performing any intermediate buffering on behalf of the
> caller. Specifying this value places certain restrictions on the
> caller’s parameters to other Zw…File routines, including the following:
>
> Any optional ByteOffset passed to ZwReadFile or ZwWriteFile must be an
> integral of the sector size.
>
> The Length passed to ZwReadFile or ZwWriteFile, must be an integral of
> the sector size. Note that specifying a read operation to a buffer whose
> length is exactly the sector size might result in a lesser number of
> significant bytes being transferred to that buffer if the end of the
> file was reached during the transfer.
>
> Buffers must be aligned in accordance with the alignment requirement of
> the underlying device. This information can be obtained by calling
> ZwCreateFile to get a handle for the file object that represents the
> physical device, and then calling ZwQueryInformationFile with that
> handle. For a list of the system FILE_XXX_ALIGNMENT values, see
> DEVICE_OBJECT.
>
> Calls to ZwSetInformationFile with the FileInformationClass parameter
> set to FilePositionInformation must specify an offset that is an
> integral of the sector size.”
>
> Make sure you’re asking for GENERIC_WRITE access to remove any question
> of file permissions.
>
> SXW wrote:
>
> > BlankHi,
> >
> > I’m trying to append some data to a file, someone said zwwritefile could
> > be used for that. My file is openned with no_intermedia_buffer flag, and
> > the buffer size is also aligned to sector size, the call to write data
will
> > be failed on NTFS volume with err code
0xc0000002(STATUS_NOT_IMPLEMENTED),
> > but the same code will success on FAT volume.
> >
> > Why the NTFS reports such a error ? I can’t believe the error code is
> > correct.
> >
> > Thanks in advance for any hit,
> >
> > Regards,
> >
> > Xinwei
> >
> >
> >
> >
>
> –
> Nick Ryan (MVP for DDK)
>
>
> —
> You are currently subscribed to ntfsd as: xxxxx@yahoo.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com

Can you show us the code?

SXW wrote:

Thanks Nick, the buffer size and the offset are integral of 512 bytes.
Is there other size for the sector? If so,how to get them?

Thanks,

----- Original Message -----
From: “Nick Ryan”
> Newsgroups: ntfsd
> To: “File Systems Developers”
> Sent: Saturday, August 23, 2003 1:31 AM
> Subject: [ntfsd] Re: how to append data to file(ZwWriteFile call)?
>
>
>
>>Check your parameters again:
>>
>>“The CreateOptions FILE_NO_INTERMEDIATE_BUFFERING flag prevents the file
>>system from performing any intermediate buffering on behalf of the
>>caller. Specifying this value places certain restrictions on the
>>caller’s parameters to other Zw…File routines, including the following:
>>
>>Any optional ByteOffset passed to ZwReadFile or ZwWriteFile must be an
>>integral of the sector size.
>>
>>The Length passed to ZwReadFile or ZwWriteFile, must be an integral of
>>the sector size. Note that specifying a read operation to a buffer whose
>>length is exactly the sector size might result in a lesser number of
>>significant bytes being transferred to that buffer if the end of the
>>file was reached during the transfer.
>>
>>Buffers must be aligned in accordance with the alignment requirement of
>>the underlying device. This information can be obtained by calling
>>ZwCreateFile to get a handle for the file object that represents the
>>physical device, and then calling ZwQueryInformationFile with that
>>handle. For a list of the system FILE_XXX_ALIGNMENT values, see
>>DEVICE_OBJECT.
>>
>>Calls to ZwSetInformationFile with the FileInformationClass parameter
>>set to FilePositionInformation must specify an offset that is an
>>integral of the sector size.”
>>
>>Make sure you’re asking for GENERIC_WRITE access to remove any question
>>of file permissions.
>>
>>SXW wrote:
>>
>>
>>>BlankHi,
>>>
>>>I’m trying to append some data to a file, someone said zwwritefile could
>>>be used for that. My file is openned with no_intermedia_buffer flag, and
>>>the buffer size is also aligned to sector size, the call to write data
>
> will
>
>>>be failed on NTFS volume with err code
>
> 0xc0000002(STATUS_NOT_IMPLEMENTED),
>
>>>but the same code will success on FAT volume.
>>>
>>>Why the NTFS reports such a error ? I can’t believe the error code is
>>>correct.
>>>
>>>Thanks in advance for any hit,
>>>
>>>Regards,
>>>
>>>Xinwei
>>>
>>>
>>>
>>>
>>
>>–
>>Nick Ryan (MVP for DDK)
>>
>>
>>—
>>You are currently subscribed to ntfsd as: xxxxx@yahoo.com
>>To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>
>
>
>


Nick Ryan (MVP for DDK)

//file created as :
status = ZwCreateFile( &file_handle,
GENERIC_READ | GENERIC_WRITE,

&object_attributes,&IoStatus,NULL,FILE_ATTRIBUTE_NORMAL,0,
FILE_OPEN,
FILE_NON_DIRECTORY_FILE |
FILE_RANDOM_ACCESS |
FILE_NO_INTERMEDIATE_BUFFERING |
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,0 );

//offset & size
if( uWriteSize & (SECTOR_SIZE-1)){
uWriteSize += SECTOR_SIZE;
uWriteSize &= (~(SECTOR_SIZE-1));
//buffer is big enough
}
writeOffset = device_extension->file_information.EndOfFile;
DbgMsg((“Append - write @ %I64u - %08x\n”, writeOffset.QuadPart,
uWriteSize));
//the size dumped is 0x200
//write to the end of file
status = ZwWriteFile( file_handle,NULL,NULL,NULL,&iostatus,

pBuffer,uWriteSize,&writeOffset,NULL);

if( !NT_SUCCESS( status ) ){
DbgMsg((“Append - write err %08x\n”, status));
return status ;
}

the write operation will get 0xc0000002(STATUS_NOT_IMPLEMENTED)

I’d tested these codes under usermode, write ok. :frowning:
Thanks,

“Nick Ryan” wrote into:xxxxx@ntfsd…
>
> Can you show us the code?

I tried my own version of your code and it works on my machine (Server
2k3, NTFS drive). I tried with a pre-existing file of 0 length and then
again with 0x200 length. My code writes 0x200 bytes at offset 0x200. I
suggest you try to run my function on your computer and go from there:

#define DA_PATH L"\??\C:\foobar.txt"

NTSTATUS
TestFile()
{
PBYTE buf = NULL;
HANDLE hFile = NULL;
IO_STATUS_BLOCK ioStatus;
LARGE_INTEGER liOffset;
NTSTATUS status = STATUS_SUCCESS;
OBJECT_ATTRIBUTES objAttribs;
UNICODE_STRING uPath;

buf = (PBYTE) ExAllocatePool(NonPagedPool, 0x200);

if (NULL == buf)
{
status = STATUS_INSUFFICIENT_RESOURCES;
goto try_exit;
}

memset(buf, 0x42, 0x200);

RtlInitUnicodeString(&uPath, DA_PATH);
InitializeObjectAttributes(&objAttribs, &uPath, OBJ_KERNEL_HANDLE,
NULL,
NULL);

status = ZwCreateFile(&hFile, GENERIC_READ | GENERIC_WRITE,
&objAttribs, &ioStatus, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN,
FILE_NON_DIRECTORY_FILE | FILE_RANDOM_ACCESS |
FILE_NO_INTERMEDIATE_BUFFERING | FILE_SYNCHRONOUS_IO_NONALERT,
NULL, 0);

if (!NT_SUCCESS(status))
goto try_exit;

liOffset.QuadPart = 0x200;

status = ZwWriteFile(hFile, NULL, NULL, NULL, &ioStatus, buf, 0x200,
&liOffset, NULL);

if (!NT_SUCCESS(status))
goto try_exit;

try_exit:
if (NULL != buf)
ExFreePool(buf);

if (NULL != hFile)
ZwClose(hFile);

return status;
}

SXW wrote:

//file created as :
status = ZwCreateFile( &file_handle,
GENERIC_READ | GENERIC_WRITE,

&object_attributes,&IoStatus,NULL,FILE_ATTRIBUTE_NORMAL,0,
FILE_OPEN,
FILE_NON_DIRECTORY_FILE |
FILE_RANDOM_ACCESS |
FILE_NO_INTERMEDIATE_BUFFERING |
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,0 );

//offset & size
if( uWriteSize & (SECTOR_SIZE-1)){
uWriteSize += SECTOR_SIZE;
uWriteSize &= (~(SECTOR_SIZE-1));
//buffer is big enough
}
writeOffset = device_extension->file_information.EndOfFile;
DbgMsg((“Append - write @ %I64u - %08x\n”, writeOffset.QuadPart,
uWriteSize));
//the size dumped is 0x200
//write to the end of file
status = ZwWriteFile( file_handle,NULL,NULL,NULL,&iostatus,

pBuffer,uWriteSize,&writeOffset,NULL);

if( !NT_SUCCESS( status ) ){
DbgMsg((“Append - write err %08x\n”, status));
return status ;
}

the write operation will get 0xc0000002(STATUS_NOT_IMPLEMENTED)

I’d tested these codes under usermode, write ok. :frowning:
Thanks,

“Nick Ryan” wrote into:xxxxx@ntfsd…
>
>>Can you show us the code?
>
>
>
>
>
>


Nick Ryan (MVP for DDK)

Thank you so much, Nick.

----- Original Message -----
From: “Nick Ryan”
Newsgroups: ntfsd
To: “File Systems Developers”
Sent: Tuesday, August 26, 2003 2:18 PM
Subject: [ntfsd] Re: how to append data to file(ZwWriteFile call)?

> I tried my own version of your code and it works on my machine (Server
> 2k3, NTFS drive). I tried with a pre-existing file of 0 length and then
> again with 0x200 length. My code writes 0x200 bytes at offset 0x200. I
> suggest you try to run my function on your computer and go from there:
>
> #define DA_PATH L"\??\C:\foobar.txt"
>
> NTSTATUS
> TestFile()
> {
> PBYTE buf = NULL;
> HANDLE hFile = NULL;
> IO_STATUS_BLOCK ioStatus;
> LARGE_INTEGER liOffset;
> NTSTATUS status = STATUS_SUCCESS;
> OBJECT_ATTRIBUTES objAttribs;
> UNICODE_STRING uPath;
>
> buf = (PBYTE) ExAllocatePool(NonPagedPool, 0x200);
>
> if (NULL == buf)
> {
> status = STATUS_INSUFFICIENT_RESOURCES;
> goto try_exit;
> }
>
> memset(buf, 0x42, 0x200);
>
> RtlInitUnicodeString(&uPath, DA_PATH);
> InitializeObjectAttributes(&objAttribs, &uPath, OBJ_KERNEL_HANDLE,
> NULL,
> NULL);
>
> status = ZwCreateFile(&hFile, GENERIC_READ | GENERIC_WRITE,
> &objAttribs, &ioStatus, NULL, FILE_ATTRIBUTE_NORMAL, 0,
FILE_OPEN,
> FILE_NON_DIRECTORY_FILE | FILE_RANDOM_ACCESS |
> FILE_NO_INTERMEDIATE_BUFFERING | FILE_SYNCHRONOUS_IO_NONALERT,
> NULL, 0);
>
> if (!NT_SUCCESS(status))
> goto try_exit;
>
> liOffset.QuadPart = 0x200;
>
> status = ZwWriteFile(hFile, NULL, NULL, NULL, &ioStatus, buf, 0x200,
> &liOffset, NULL);
>
> if (!NT_SUCCESS(status))
> goto try_exit;
>
> try_exit:
> if (NULL != buf)
> ExFreePool(buf);
>
> if (NULL != hFile)
> ZwClose(hFile);
>
> return status;
> }
>
> SXW wrote:
>
> > //file created as :
> > status = ZwCreateFile( &file_handle,
> > GENERIC_READ |
GENERIC_WRITE,
> >
> > &object_attributes,&IoStatus,NULL,FILE_ATTRIBUTE_NORMAL,0,
> > FILE_OPEN,
> > FILE_NON_DIRECTORY_FILE |
> > FILE_RANDOM_ACCESS |
> >
FILE_NO_INTERMEDIATE_BUFFERING |
> >
FILE_SYNCHRONOUS_IO_NONALERT,
> > NULL,0 );
> >
> > //offset & size
> > if( uWriteSize & (SECTOR_SIZE-1)){
> > uWriteSize += SECTOR_SIZE;
> > uWriteSize &= (~(SECTOR_SIZE-1));
> > //buffer is big enough
> > }
> > writeOffset = device_extension->file_information.EndOfFile;
> > DbgMsg((“Append - write @ %I64u - %08x\n”, writeOffset.QuadPart,
> > uWriteSize));
> > //the size dumped is 0x200
> > //write to the end of file
> > status = ZwWriteFile( file_handle,NULL,NULL,NULL,&iostatus,
> >
> > pBuffer,uWriteSize,&writeOffset,NULL);
> >
> > if( !NT_SUCCESS( status ) ){
> > DbgMsg((“Append - write err %08x\n”, status));
> > return status ;
> > }
> >
> > the write operation will get 0xc0000002(STATUS_NOT_IMPLEMENTED)
> >
> > I’d tested these codes under usermode, write ok. :frowning:
> > Thanks,
> >
> > “Nick Ryan” wrote into:xxxxx@ntfsd…
> >
> >>Can you show us the code?
> >
> >
> >
> >
> >
> >
>
> –
> Nick Ryan (MVP for DDK)
>
>
> —
> You are currently subscribed to ntfsd as: xxxxx@yahoo.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com