Microsoft KB Archive/280718

{|
 * width="100%"|

BUG: Redirector Keeps Remote File's Original Size When CreateFile Is called with TRUNCATE_EXISTING

 * }

Q280718

-

The information in this article applies to:


 * Microsoft Win32 Application Programming Interface (API), included with:
 * the operating system: Microsoft Windows NT 4.0

-

SYMPTOMS
When applications open remote files by using the CreateFile function and specify TRUNCATE_EXISTING, subsequent file I/O functions such as WriteFile and GetFileInformationByHandle use the file's original size rather than the truncated size of zero bytes. However, immediately after the CreateFile call, the server does truncate the file to zero bytes. Therefore, the client seems to be confused.

CAUSE
The Windows NT 4.0 network redirector has a bug such that it correctly opens the remote file and truncates it on the server, but continues to remember the file's original size before truncation. This file size is then used in subsequent function calls to that file. Because the server successfully truncated the file, the client redirector should reset its internal copy of the file size to zero.

NOTE: This bug only affects Windows NT 4.0 clients.

RESOLUTION
To work around this problem, call the SetEndOfFile function immediately after calling CreateFile with TRUNCATE_EXISTING to open remote files. This method forces the client redirector internally to set the file size to zero. For example:

  hf = CreateFile (pszFileName, GENERIC_READ |GENERIC_WRITE,                    0,                    NULL, TRUNCATE_EXISTING,                    FILE_ATTRIBUTE_NORMAL, NULL); if (hf == INVALID_HANDLE_VALUE) {     // Handle error. printf (&quot;error %lu: could not open file %s\n&quot;,               GetLastError, pszFileName); return (-1); }

SetEndOfFile(hf);  // Force client redirector to set file size to zero. . ..

STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.

MORE INFORMATION
When an application calls CreateFile with TRUNCATE_EXISTING to open a remote file, the client computer's network redirector translates the request into two Server Message Block (SMB) commands: &quot;NT Create & X&quot; and &quot;transact2 Set File Info&quot;. The first SMB asks the server to open an existing file; the server does this, and then returns a response that includes the file's current size. The second SMB then asks the server to set the file's size to zero bytes; the server does this, and then responds that it was successful. The file on the server now has a size of zero bytes.

After these two SMBs are sent and the server responds, the Windows NT 4.0 client redirector internally keeps the size returned by the &quot;NT Create & X&quot; SMB instead of setting it to zero as it should. This size is used by the redirector in subsequent file I/O functions such as WriteFile and GetFileInformationByHandle.

Steps to Reproduce Behavior
Because this problem causes several file I/O functions to behave incorrectly, this article demonstrates two ways to reproduce the behavior and shows how to fix each.

CreateFile Followed by WriteFile
To see the problem when the CreateFile call is followed by WriteFile, compile the following code as a console application, and then run it from the command line with the name of a remote file as the single argument; for example, &quot;TRUNC I:\TEST&quot;. The program writes 10 bytes to the file and closes it. On the server, the file has the original size with the 10 bytes of new data followed by zeros for the remaining data.

To see the correct behavior, remove the comment in front of the line that contains SetEndOfFile, recompile the application, and then run it with the name of a remote file. It will now correctly write 10 bytes to the file and close the file, leaving a 10-byte file on the server.

/*- TRUNC.CPP

Demonstrates a problem where the Windows NT 4.0 network redirector opens a file with TRUNCATE_EXISTING but keeps the file's original size.

Compile from the command line with: cl trunc.cpp

Run from the command line with: trunc filename

where filename is the name of a remote file that already exists.

When the problem occurs, the file has its original size, but it will have 10 bytes of &quot;D&quot; followed by zeros.

To see a fix to the problem, remove the comment from the line that contains SetEndOfFile. The remote file will then have a size of 10 bytes. -*/
 * 1) include 
 * 2) include 

const int BUFF_LENGTH = 10;

int main (int argc, char **argv) {  DWORD  dwRetVal; HANDLE hf; BOOL  fResult; char * pszFileName; char  buffer[BUFF_LENGTH]; DWORD dwWritten;

if (argc != 2) {     printf(&quot;usage: cf \n&quot;); dwRetVal = -1; goto EXIT_DONE; }  pszFileName = argv[1];

hf = CreateFile (pszFileName, GENERIC_READ |GENERIC_WRITE,                   0,                    NULL, TRUNCATE_EXISTING,                    FILE_ATTRIBUTE_NORMAL, NULL); if (hf == INVALID_HANDLE_VALUE) {     printf (&quot;error %lu:  could not open file %s\n&quot;,              GetLastError, pszFileName); dwRetVal = -1; goto EXIT_DONE; }

// SetEndOfFile(hf); // Force redirector to set file size to zero.

FillMemory (buffer, BUFF_LENGTH, 'D'); fResult = WriteFile (hf, &buffer, BUFF_LENGTH, &dwWritten, NULL); if (0 == fResult) {     printf(&quot;error %lu:  couldn't write to %s\n&quot;,             GetLastError, pszFileName); }

CloseHandle (hf); dwRetVal = 0; printf(&quot;truncated %s, wrote %d bytes to it\n&quot;,         pszFileName, dwWritten);

EXIT_DONE: return (dwRetVal); }

CreateFile Followed by GetFileInformationByHandle
To see the problem when the CreateFile call is followed by GetFileInformationByHandle, compile the following code as a console application, and then run it from the command line with the name of a remote file as the single argument; for example, &quot;GFI I:\TEST&quot;. The program displays the file's original size, but it should be zero because it was truncated and the server shows a file size of zero.

To see the correct behavior, remove the comment in front of the line that contains SetEndOfFile, recompile the application, and then run it with the name of a remote file. It will show the file's correct size (zero).

/*- GFI.CPP

Demonstrates a problem where the Windows NT 4.0 network redirector opens a file with TRUNCATE_EXISTING but keeps the file's original size.

Compile from the command line with: cl gfi.cpp

Run from the command line with: gfi filename

where filename is the name of a remote file that already exists.

To see a fix to the problem, remove the comment from the line that contains SetEndOfFile. -*/
 * 1) include 
 * 2) include 

void main (int argc, char **argv) {  HANDLE hf; BY_HANDLE_FILE_INFORMATION bhi;

if (argc != 2) {     printf(&quot;usage: %s \n&quot;, argv[0]); return; }

hf = CreateFile (argv[1], GENERIC_READ|GENERIC_WRITE,                   FILE_SHARE_READ|FILE_SHARE_WRITE,                    NULL, TRUNCATE_EXISTING,                    FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hf) {     printf(&quot;couldn't open %s for truncation\n&quot;, argv[1]); return; }

// SetEndOfFile(hf); // Force redirector to set file size to zero.

if (GetFileInformationByHandle (hf, &bhi)) printf(&quot;size is %lu, should be 0\n&quot;, bhi.nFileSizeLow);

CloseHandle (hf); } Additional query words:

Keywords : kbAPI kbFileIO kbKernBase kbSDKWin32 kbDSupport kbGrpDSKernBase

Issue type : kbbug

Technology : kbAudDeveloper kbWin32sSearch kbWin32API