update for HEAD-2003021201
[reactos.git] / lib / kernel32 / file / file.c
index e4c7163..79061ad 100644 (file)
 
 /* INCLUDES *****************************************************************/
 
-#include <ddk/ntddk.h>
-#include <windows.h>
-#include <wchar.h>
-#include <string.h>
+#include <k32.h>
 
 #define NDEBUG
 #include <kernel32/kernel32.h>
-#include <kernel32/error.h>
-
-#define LPPROGRESS_ROUTINE void*
 
 
 /* GLOBALS ******************************************************************/
@@ -259,15 +253,18 @@ GetFileType(HANDLE hFile)
    switch ((ULONG)hFile)
      {
        case STD_INPUT_HANDLE:
-         hFile = NtCurrentPeb()->ProcessParameters->hStdInput;\r
+         hFile = NtCurrentPeb()->ProcessParameters->hStdInput;
+
          break;
 
        case STD_OUTPUT_HANDLE:
-         hFile = NtCurrentPeb()->ProcessParameters->hStdOutput;\r
+         hFile = NtCurrentPeb()->ProcessParameters->hStdOutput;
+
          break;
 
        case STD_ERROR_HANDLE:
-         hFile = NtCurrentPeb()->ProcessParameters->hStdError;\r
+         hFile = NtCurrentPeb()->ProcessParameters->hStdError;
+
          break;
      }
 
@@ -408,10 +405,14 @@ GetCompressedFileSizeW(LPCWSTR lpFileName,
      {
        CloseHandle(hFile);
        SetLastErrorByStatus(errCode);
-       return 0;
+       return INVALID_FILE_SIZE;
      }
    CloseHandle(hFile);
-   return 0;
+
+   if(lpFileSizeHigh)
+    *lpFileSizeHigh = FileCompression.CompressedFileSize.u.HighPart;
+
+   return FileCompression.CompressedFileSize.u.LowPart;
 }
 
 
@@ -604,6 +605,11 @@ SetFileAttributesW(LPCWSTR lpFileName,
                       OPEN_EXISTING,
                       FILE_ATTRIBUTE_NORMAL,
                       NULL);
+   if (INVALID_HANDLE_VALUE == hFile)
+     {
+       DPRINT("SetFileAttributes CreateFileW failed with code %d\n", GetLastError());
+       return FALSE;
+     }
 
    errCode = NtQueryInformationFile(hFile,
                                    &IoStatusBlock,
@@ -613,6 +619,7 @@ SetFileAttributesW(LPCWSTR lpFileName,
    if (!NT_SUCCESS(errCode))
      {
        CloseHandle(hFile);
+       DPRINT("SetFileAttributes NtQueryInformationFile failed with status 0x%08x\n", errCode);
        SetLastErrorByStatus(errCode);
        return FALSE;
      }
@@ -625,6 +632,7 @@ SetFileAttributesW(LPCWSTR lpFileName,
    if (!NT_SUCCESS(errCode))
      {
        CloseHandle(hFile);
+       DPRINT("SetFileAttributes NtSetInformationFile failed with status 0x%08x\n", errCode);
        SetLastErrorByStatus(errCode);
        return FALSE;
      }
@@ -800,12 +808,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 */