Manually applying Gunnars patch because it's easier this way - and a good chance...
[reactos.git] / reactos / lib / kernel32 / file / file.c
index e7857f0..ec70fca 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: file.c,v 1.36 2002/09/08 10:22:42 chorns Exp $
+/* $Id: file.c,v 1.38 2002/11/07 02:52:37 robd Exp $
  *
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS system libraries
@@ -259,15 +259,18 @@ GetFileType(HANDLE hFile)
    switch ((ULONG)hFile)
      {
        case STD_INPUT_HANDLE:
-         hFile = NtCurrentPeb()->ProcessParameters->InputHandle;
+         hFile = NtCurrentPeb()->ProcessParameters->hStdInput;
+
          break;
 
        case STD_OUTPUT_HANDLE:
-         hFile = NtCurrentPeb()->ProcessParameters->OutputHandle;
+         hFile = NtCurrentPeb()->ProcessParameters->hStdOutput;
+
          break;
 
        case STD_ERROR_HANDLE:
-         hFile = NtCurrentPeb()->ProcessParameters->ErrorHandle;
+         hFile = NtCurrentPeb()->ProcessParameters->hStdError;
+
          break;
      }
 
@@ -800,12 +803,73 @@ SetFileTime(HANDLE hFile,
 }
 
 
+/*
+The caller must have opened the file with the DesiredAccess FILE_WRITE_DATA flag set.
+*/
 WINBOOL STDCALL
 SetEndOfFile(HANDLE hFile)
 {
-   int x = -1;
-   DWORD Num;
-   return WriteFile(hFile,&x,1,&Num,NULL);
+       IO_STATUS_BLOCK  IoStatusBlock;
+       FILE_END_OF_FILE_INFORMATION    EndOfFileInfo;
+       FILE_ALLOCATION_INFORMATION             FileAllocationInfo;
+       FILE_POSITION_INFORMATION                FilePosInfo;
+       NTSTATUS Status;
+
+       //get current position
+       Status = NtQueryInformationFile(
+                                       hFile,
+                                       &IoStatusBlock,
+                                       &FilePosInfo,
+                                       sizeof(FILE_POSITION_INFORMATION),
+                                       FilePositionInformation
+                                       );
+
+       if (!NT_SUCCESS(Status)){
+               SetLastErrorByStatus(Status);
+               return FALSE;
+       }
+
+       EndOfFileInfo.EndOfFile.QuadPart = FilePosInfo.CurrentByteOffset.QuadPart;
+
+       /*
+       NOTE: 
+       This call is not supposed to free up any space after the eof marker
+       if the file gets truncated. We have to deallocate the space explicitly afterwards.
+       But...most file systems dispatch both FileEndOfFileInformation 
+       and FileAllocationInformation as they were the same     command.
+
+       */
+       Status = NtSetInformationFile(
+                                               hFile,
+                                               &IoStatusBlock,  //out
+                                               &EndOfFileInfo,
+                                               sizeof(FILE_END_OF_FILE_INFORMATION),
+                                               FileEndOfFileInformation
+                                               );
+
+       if (!NT_SUCCESS(Status)){
+               SetLastErrorByStatus(Status);
+               return FALSE;
+       }
+
+       FileAllocationInfo.AllocationSize.QuadPart = FilePosInfo.CurrentByteOffset.QuadPart;
+
+
+       Status = NtSetInformationFile(
+                                               hFile,
+                                               &IoStatusBlock,  //out
+                                               &FileAllocationInfo,
+                                               sizeof(FILE_ALLOCATION_INFORMATION),
+                                               FileAllocationInformation
+                                               );
+
+       if (!NT_SUCCESS(Status)){
+               SetLastErrorByStatus(Status);
+               return FALSE;
+       }
+       
+       return TRUE;
+
 }
 
 /* EOF */