[USETUP] Move Setup[Delete|Copy|Move]File() from usetup code into the setuplib's...
[reactos.git] / base / setup / usetup / filesup.c
index 2fe323a..261bfd9 100644 (file)
@@ -1,27 +1,9 @@
 /*
- *  ReactOS kernel
- *  Copyright (C) 2002 ReactOS Team
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License along
- *  with this program; if not, write to the Free Software Foundation, Inc.,
- *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-/* COPYRIGHT:       See COPYING in the top level directory
+ * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS text-mode setup
- * FILE:            subsys/system/usetup/filesup.c
+ * FILE:            base/setup/usetup/filesup.c
  * PURPOSE:         File support functions
- * PROGRAMMER:      Eric Kohl
- *                  Casper S. Hornstrup (chorns@users.sourceforge.net)
+ * PROGRAMMERS:     Casper S. Hornstrup (chorns@users.sourceforge.net)
  */
 
 /* INCLUDES *****************************************************************/
@@ -48,8 +30,9 @@ SetupCreateSingleDirectory(
     HANDLE DirectoryHandle;
     NTSTATUS Status;
 
-    RtlCreateUnicodeString(&PathName,
-                           DirectoryName);
+    if (!RtlCreateUnicodeString(&PathName, DirectoryName))
+        return STATUS_NO_MEMORY;
+
     if (PathName.Length > sizeof(WCHAR) &&
         PathName.Buffer[PathName.Length / sizeof(WCHAR) - 2] == L'\\' &&
         PathName.Buffer[PathName.Length / sizeof(WCHAR) - 1] == L'.')
@@ -72,14 +55,14 @@ SetupCreateSingleDirectory(
                                NULL);
 
     Status = NtCreateFile(&DirectoryHandle,
-                          DIRECTORY_ALL_ACCESS,
+                          FILE_LIST_DIRECTORY | SYNCHRONIZE,
                           &ObjectAttributes,
                           &IoStatusBlock,
                           NULL,
                           FILE_ATTRIBUTE_DIRECTORY,
                           FILE_SHARE_READ | FILE_SHARE_WRITE,
                           FILE_OPEN_IF,
-                          FILE_DIRECTORY_FILE,
+                          FILE_OPEN_FOR_BACKUP_INTENT | FILE_DIRECTORY_FILE,
                           NULL,
                           0);
     if (NT_SUCCESS(Status))
@@ -92,44 +75,6 @@ SetupCreateSingleDirectory(
     return Status;
 }
 
-
-static
-BOOLEAN
-DoesPathExist(
-    PWSTR PathName)
-{
-    OBJECT_ATTRIBUTES ObjectAttributes;
-    IO_STATUS_BLOCK IoStatusBlock;
-    UNICODE_STRING Name;
-    HANDLE FileHandle;
-    NTSTATUS Status;
-
-    RtlInitUnicodeString(&Name,
-                         PathName);
-
-    InitializeObjectAttributes(&ObjectAttributes,
-                               &Name,
-                               OBJ_CASE_INSENSITIVE,
-                               NULL,
-                               NULL);
-
-    Status = NtOpenFile(&FileHandle,
-                        GENERIC_READ | SYNCHRONIZE,
-                        &ObjectAttributes,
-                        &IoStatusBlock,
-                        0,
-                        FILE_SYNCHRONOUS_IO_NONALERT);
-    if (!NT_SUCCESS(Status))
-    {
-        return FALSE;
-    }
-
-    NtClose(FileHandle);
-
-    return TRUE;
-}
-
-
 NTSTATUS
 SetupCreateDirectory(
     PWCHAR PathName)
@@ -167,7 +112,7 @@ SetupCreateDirectory(
             *Ptr = 0;
 
             DPRINT("PathBuffer: %S\n", PathBuffer);
-            if (!DoesPathExist(PathBuffer))
+            if (!DoesPathExist(NULL, PathBuffer))
             {
                 DPRINT("Create: %S\n", PathBuffer);
                 Status = SetupCreateSingleDirectory(PathBuffer);
@@ -181,7 +126,7 @@ SetupCreateDirectory(
         Ptr++;
     }
 
-    if (!DoesPathExist(PathBuffer))
+    if (!DoesPathExist(NULL, PathBuffer))
     {
         DPRINT("Create: %S\n", PathBuffer);
         Status = SetupCreateSingleDirectory(PathBuffer);
@@ -197,198 +142,6 @@ done:
     return Status;
 }
 
-
-NTSTATUS
-SetupCopyFile(
-    PWCHAR SourceFileName,
-    PWCHAR DestinationFileName)
-{
-    OBJECT_ATTRIBUTES ObjectAttributes;
-    HANDLE FileHandleSource;
-    HANDLE FileHandleDest;
-    static IO_STATUS_BLOCK IoStatusBlock;
-    FILE_STANDARD_INFORMATION FileStandard;
-    FILE_BASIC_INFORMATION FileBasic;
-    ULONG RegionSize;
-    UNICODE_STRING FileName;
-    NTSTATUS Status;
-    PVOID SourceFileMap = 0;
-    HANDLE SourceFileSection;
-    SIZE_T SourceSectionSize = 0;
-    LARGE_INTEGER ByteOffset;
-
-#ifdef __REACTOS__
-    RtlInitUnicodeString(&FileName,
-                         SourceFileName);
-
-    InitializeObjectAttributes(&ObjectAttributes,
-                               &FileName,
-                               OBJ_CASE_INSENSITIVE,
-                               NULL,
-                               NULL);
-
-    Status = NtOpenFile(&FileHandleSource,
-                        GENERIC_READ,
-                        &ObjectAttributes,
-                        &IoStatusBlock,
-                        FILE_SHARE_READ,
-                        FILE_SEQUENTIAL_ONLY);
-    if (!NT_SUCCESS(Status))
-    {
-        DPRINT1("NtOpenFile failed: %x, %wZ\n", Status, &FileName);
-        goto done;
-    }
-#else
-    FileHandleSource = CreateFileW(SourceFileName,
-                                   GENERIC_READ,
-                                   FILE_SHARE_READ,
-                                   NULL,
-                                   OPEN_EXISTING,
-                                   0,
-                                   NULL);
-    if (FileHandleSource == INVALID_HANDLE_VALUE)
-    {
-        Status = STATUS_UNSUCCESSFUL;
-        goto done;
-    }
-#endif
-
-    Status = NtQueryInformationFile(FileHandleSource,
-                                    &IoStatusBlock,
-                                    &FileStandard,
-                                    sizeof(FILE_STANDARD_INFORMATION),
-                                    FileStandardInformation);
-    if (!NT_SUCCESS(Status))
-    {
-        DPRINT1("NtQueryInformationFile failed: %x\n", Status);
-        goto closesrc;
-    }
-
-    Status = NtQueryInformationFile(FileHandleSource,
-                                    &IoStatusBlock,&FileBasic,
-                                    sizeof(FILE_BASIC_INFORMATION),
-                                    FileBasicInformation);
-    if (!NT_SUCCESS(Status))
-    {
-        DPRINT1("NtQueryInformationFile failed: %x\n", Status);
-        goto closesrc;
-    }
-
-    Status = NtCreateSection(&SourceFileSection,
-                             SECTION_MAP_READ,
-                             NULL,
-                             NULL,
-                             PAGE_READONLY,
-                             SEC_COMMIT,
-                             FileHandleSource);
-    if (!NT_SUCCESS(Status))
-    {
-      DPRINT1("NtCreateSection failed: %x, %S\n", Status, SourceFileName);
-      goto closesrc;
-    }
-
-    Status = NtMapViewOfSection(SourceFileSection,
-                                NtCurrentProcess(),
-                                &SourceFileMap,
-                                0,
-                                0,
-                                NULL,
-                                &SourceSectionSize,
-                                ViewUnmap,
-                                0,
-                                PAGE_READONLY );
-    if (!NT_SUCCESS(Status))
-    {
-        DPRINT1("NtMapViewOfSection failed: %x, %S\n", Status, SourceFileName);
-        goto closesrcsec;
-    }
-
-    RtlInitUnicodeString(&FileName,
-                         DestinationFileName);
-
-    InitializeObjectAttributes(&ObjectAttributes,
-                               &FileName,
-                               OBJ_CASE_INSENSITIVE,
-                               NULL,
-                               NULL);
-
-    Status = NtCreateFile(&FileHandleDest,
-                          GENERIC_WRITE | SYNCHRONIZE,
-                          &ObjectAttributes,
-                          &IoStatusBlock,
-                          NULL,
-                          FILE_ATTRIBUTE_NORMAL,
-                          0,
-                          FILE_OVERWRITE_IF,
-                          FILE_NO_INTERMEDIATE_BUFFERING |
-                          FILE_SEQUENTIAL_ONLY |
-                          FILE_SYNCHRONOUS_IO_NONALERT,
-                          NULL,
-                          0);
-    if (!NT_SUCCESS(Status))
-    {
-        DPRINT1("NtCreateFile failed: %x\n", Status);
-        goto unmapsrcsec;
-    }
-
-    RegionSize = (ULONG)PAGE_ROUND_UP(FileStandard.EndOfFile.u.LowPart);
-    IoStatusBlock.Status = 0;
-    ByteOffset.QuadPart = 0;
-    Status = NtWriteFile(FileHandleDest,
-                         NULL,
-                         NULL,
-                         NULL,
-                         &IoStatusBlock,
-                         SourceFileMap,
-                         RegionSize,
-                         &ByteOffset,
-                         NULL);
-    if (!NT_SUCCESS(Status))
-    {
-        DPRINT1("NtWriteFile failed: %x:%x, iosb: %p src: %p, size: %x\n", Status, IoStatusBlock.Status, &IoStatusBlock, SourceFileMap, RegionSize);
-        goto closedest;
-    }
-
-    /* Copy file date/time from source file */
-    Status = NtSetInformationFile(FileHandleDest,
-                                  &IoStatusBlock,
-                                  &FileBasic,
-                                  sizeof(FILE_BASIC_INFORMATION),
-                                  FileBasicInformation);
-    if (!NT_SUCCESS(Status))
-    {
-        DPRINT1("NtSetInformationFile failed: %x\n", Status);
-        goto closedest;
-    }
-
-    /* shorten the file back to it's real size after completing the write */
-    Status = NtSetInformationFile(FileHandleDest,
-                                  &IoStatusBlock,
-                                  &FileStandard.EndOfFile,
-                                  sizeof(FILE_END_OF_FILE_INFORMATION),
-                                  FileEndOfFileInformation);
-    if (!NT_SUCCESS(Status))
-    {
-        DPRINT1("NtSetInformationFile failed: %x\n", Status);
-    }
-
-closedest:
-    NtClose(FileHandleDest);
-
-unmapsrcsec:
-    NtUnmapViewOfSection(NtCurrentProcess(), SourceFileMap);
-
-closesrcsec:
-    NtClose(SourceFileSection);
-
-closesrc:
-    NtClose(FileHandleSource);
-
-done:
-    return Status;
-}
-
-#ifdef __REACTOS__
 NTSTATUS
 SetupExtractFile(
     PWCHAR CabinetFileName,
@@ -466,49 +219,48 @@ SetupExtractFile(
 
     return STATUS_SUCCESS;
 }
-#endif
 
 BOOLEAN
-DoesFileExist(
-    PWSTR PathName,
-    PWSTR FileName)
+IsValidPath(
+    IN PCWSTR InstallDir)
 {
-    OBJECT_ATTRIBUTES ObjectAttributes;
-    IO_STATUS_BLOCK IoStatusBlock;
-    UNICODE_STRING Name;
-    WCHAR FullName[MAX_PATH];
-    HANDLE FileHandle;
-    NTSTATUS Status;
+    UINT i, Length;
 
-    wcscpy(FullName, PathName);
-    if (FileName != NULL)
-    {
-        if (FileName[0] != L'\\')
-            wcscat(FullName, L"\\");
-        wcscat(FullName, FileName);
-    }
+    Length = wcslen(InstallDir);
 
-    RtlInitUnicodeString(&Name,
-                         FullName);
+    // TODO: Add check for 8.3 too.
 
-    InitializeObjectAttributes(&ObjectAttributes,
-                               &Name,
-                               OBJ_CASE_INSENSITIVE,
-                               NULL,
-                               NULL);
+    /* Path must be at least 2 characters long */
+//    if (Length < 2)
+//        return FALSE;
+
+    /* Path must start with a backslash */
+//    if (InstallDir[0] != L'\\')
+//        return FALSE;
+
+    /* Path must not end with a backslash */
+    if (InstallDir[Length - 1] == L'\\')
+        return FALSE;
 
-    Status = NtOpenFile(&FileHandle,
-                        GENERIC_READ | SYNCHRONIZE,
-                        &ObjectAttributes,
-                        &IoStatusBlock,
-                        0,
-                        FILE_SYNCHRONOUS_IO_NONALERT);
-    if (!NT_SUCCESS(Status))
+    /* Path must not contain whitespace characters */
+    for (i = 0; i < Length; i++)
     {
-      return FALSE;
+        if (iswspace(InstallDir[i]))
+            return FALSE;
     }
 
-    NtClose(FileHandle);
+    /* Path component must not end with a dot */
+    for (i = 0; i < Length; i++)
+    {
+        if (InstallDir[i] == L'\\' && i > 0)
+        {
+            if (InstallDir[i - 1] == L'.')
+                return FALSE;
+        }
+    }
+
+    if (InstallDir[Length - 1] == L'.')
+        return FALSE;
 
     return TRUE;
 }