ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

filesup.c
Go to the documentation of this file.
00001 /*
00002  *  ReactOS kernel
00003  *  Copyright (C) 2002 ReactOS Team
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License along
00016  *  with this program; if not, write to the Free Software Foundation, Inc.,
00017  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00018  */
00019 /* COPYRIGHT:       See COPYING in the top level directory
00020  * PROJECT:         ReactOS text-mode setup
00021  * FILE:            subsys/system/usetup/filesup.c
00022  * PURPOSE:         File support functions
00023  * PROGRAMMER:      Eric Kohl
00024  *                  Casper S. Hornstrup (chorns@users.sourceforge.net)
00025  */
00026 
00027 /* INCLUDES *****************************************************************/
00028 
00029 #include "usetup.h"
00030 
00031 #define NDEBUG
00032 #include <debug.h>
00033 
00034 /* FUNCTIONS ****************************************************************/
00035 
00036 static BOOLEAN HasCurrentCabinet = FALSE;
00037 static WCHAR CurrentCabinetName[MAX_PATH];
00038 static CAB_SEARCH Search;
00039 
00040 NTSTATUS
00041 SetupCreateDirectory(PWCHAR DirectoryName)
00042 {
00043   OBJECT_ATTRIBUTES ObjectAttributes;
00044   IO_STATUS_BLOCK IoStatusBlock;
00045   UNICODE_STRING PathName;
00046   HANDLE DirectoryHandle;
00047   NTSTATUS Status;
00048 
00049   RtlCreateUnicodeString(&PathName,
00050              DirectoryName);
00051   if (PathName.Length > sizeof(WCHAR) &&
00052       PathName.Buffer[PathName.Length / sizeof(WCHAR) - 2] == L'\\' &&
00053       PathName.Buffer[PathName.Length / sizeof(WCHAR) - 1] == L'.')
00054     {
00055        PathName.Length -= sizeof(WCHAR);
00056        PathName.Buffer[PathName.Length / sizeof(WCHAR)] = 0;
00057     }
00058 
00059   if (PathName.Length > sizeof(WCHAR) &&
00060       PathName.Buffer[PathName.Length / sizeof(WCHAR) - 1] == L'\\')
00061     {
00062       PathName.Length -= sizeof(WCHAR);
00063       PathName.Buffer[PathName.Length / sizeof(WCHAR)] = 0;
00064    }
00065 
00066   InitializeObjectAttributes(&ObjectAttributes,
00067                  &PathName,
00068                  OBJ_CASE_INSENSITIVE | OBJ_INHERIT,
00069                  NULL,
00070                  NULL);
00071 
00072   Status = NtCreateFile(&DirectoryHandle,
00073             DIRECTORY_ALL_ACCESS,
00074             &ObjectAttributes,
00075             &IoStatusBlock,
00076             NULL,
00077             FILE_ATTRIBUTE_DIRECTORY,
00078             FILE_SHARE_READ | FILE_SHARE_WRITE,
00079             FILE_OPEN_IF,
00080             FILE_DIRECTORY_FILE,
00081             NULL,
00082             0);
00083   if (NT_SUCCESS(Status))
00084     {
00085       NtClose(DirectoryHandle);
00086     }
00087 
00088   RtlFreeUnicodeString(&PathName);
00089 
00090   return(Status);
00091 }
00092 
00093 
00094 NTSTATUS
00095 SetupCopyFile(PWCHAR SourceFileName,
00096           PWCHAR DestinationFileName)
00097 {
00098   OBJECT_ATTRIBUTES ObjectAttributes;
00099   HANDLE FileHandleSource;
00100   HANDLE FileHandleDest;
00101   static IO_STATUS_BLOCK IoStatusBlock;
00102   FILE_STANDARD_INFORMATION FileStandard;
00103   FILE_BASIC_INFORMATION FileBasic;
00104   ULONG RegionSize;
00105   UNICODE_STRING FileName;
00106   NTSTATUS Status;
00107   PVOID SourceFileMap = 0;
00108   HANDLE SourceFileSection;
00109   SIZE_T SourceSectionSize = 0;
00110   LARGE_INTEGER ByteOffset;
00111 
00112 #ifdef __REACTOS__
00113   RtlInitUnicodeString(&FileName,
00114                SourceFileName);
00115 
00116   InitializeObjectAttributes(&ObjectAttributes,
00117                  &FileName,
00118                  OBJ_CASE_INSENSITIVE,
00119                  NULL,
00120                  NULL);
00121 
00122   Status = NtOpenFile(&FileHandleSource,
00123               GENERIC_READ,
00124               &ObjectAttributes,
00125               &IoStatusBlock,
00126               FILE_SHARE_READ,
00127               FILE_SEQUENTIAL_ONLY);
00128   if(!NT_SUCCESS(Status))
00129     {
00130       DPRINT1("NtOpenFile failed: %x, %wZ\n", Status, &FileName);
00131       goto done;
00132     }
00133 #else
00134   FileHandleSource = CreateFileW(SourceFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
00135   if (FileHandleSource == INVALID_HANDLE_VALUE)
00136   {
00137     Status = STATUS_UNSUCCESSFUL;
00138     goto done;
00139   }
00140 #endif
00141 
00142   Status = NtQueryInformationFile(FileHandleSource,
00143                   &IoStatusBlock,
00144                   &FileStandard,
00145                   sizeof(FILE_STANDARD_INFORMATION),
00146                   FileStandardInformation);
00147   if(!NT_SUCCESS(Status))
00148     {
00149       DPRINT1("NtQueryInformationFile failed: %x\n", Status);
00150       goto closesrc;
00151     }
00152   Status = NtQueryInformationFile(FileHandleSource,
00153                   &IoStatusBlock,&FileBasic,
00154                   sizeof(FILE_BASIC_INFORMATION),
00155                   FileBasicInformation);
00156   if(!NT_SUCCESS(Status))
00157     {
00158       DPRINT1("NtQueryInformationFile failed: %x\n", Status);
00159       goto closesrc;
00160     }
00161 
00162   Status = NtCreateSection( &SourceFileSection,
00163                 SECTION_MAP_READ,
00164                 NULL,
00165                 NULL,
00166                 PAGE_READONLY,
00167                 SEC_COMMIT,
00168                 FileHandleSource);
00169   if(!NT_SUCCESS(Status))
00170     {
00171       DPRINT1("NtCreateSection failed: %x, %wZ\n", Status, SourceFileName);
00172       goto closesrc;
00173     }
00174 
00175   Status = NtMapViewOfSection( SourceFileSection,
00176                    NtCurrentProcess(),
00177                    &SourceFileMap,
00178                    0,
00179                    0,
00180                    NULL,
00181                    &SourceSectionSize,
00182                    ViewUnmap,
00183                    0,
00184                    PAGE_READONLY );
00185   if(!NT_SUCCESS(Status))
00186     {
00187       DPRINT1("NtMapViewOfSection failed: %x, %wZ\n", Status, SourceFileName);
00188       goto closesrcsec;
00189     }
00190 
00191   RtlInitUnicodeString(&FileName,
00192                DestinationFileName);
00193 
00194   InitializeObjectAttributes(&ObjectAttributes,
00195                  &FileName,
00196                  OBJ_CASE_INSENSITIVE,
00197                  NULL,
00198                  NULL);
00199 
00200   Status = NtCreateFile(&FileHandleDest,
00201             GENERIC_WRITE,
00202             &ObjectAttributes,
00203             &IoStatusBlock,
00204             NULL,
00205             FILE_ATTRIBUTE_NORMAL,
00206             0,
00207             FILE_OVERWRITE_IF,
00208             FILE_NO_INTERMEDIATE_BUFFERING |
00209             FILE_SEQUENTIAL_ONLY |
00210             FILE_SYNCHRONOUS_IO_NONALERT,
00211             NULL,
00212             0);
00213   if(!NT_SUCCESS(Status))
00214     {
00215       DPRINT1("NtCreateFile failed: %x\n", Status);
00216       goto unmapsrcsec;
00217     }
00218 
00219   RegionSize = (ULONG)PAGE_ROUND_UP(FileStandard.EndOfFile.u.LowPart);
00220   IoStatusBlock.Status = 0;
00221   ByteOffset.QuadPart = 0;
00222   Status = NtWriteFile(FileHandleDest,
00223                NULL,
00224                NULL,
00225                NULL,
00226                &IoStatusBlock,
00227                SourceFileMap,
00228                RegionSize,
00229                &ByteOffset,
00230                NULL);
00231   if(!NT_SUCCESS(Status))
00232     {
00233       DPRINT1("NtWriteFile failed: %x:%x, iosb: %p src: %p, size: %x\n", Status, IoStatusBlock.Status, &IoStatusBlock, SourceFileMap, RegionSize);
00234       goto closedest;
00235     }
00236   /* Copy file date/time from source file */
00237   Status = NtSetInformationFile(FileHandleDest,
00238                 &IoStatusBlock,
00239                 &FileBasic,
00240                 sizeof(FILE_BASIC_INFORMATION),
00241                 FileBasicInformation);
00242   if(!NT_SUCCESS(Status))
00243     {
00244       DPRINT1("NtSetInformationFile failed: %x\n", Status);
00245       goto closedest;
00246     }
00247 
00248   /* shorten the file back to it's real size after completing the write */
00249   Status = NtSetInformationFile(FileHandleDest,
00250                &IoStatusBlock,
00251                &FileStandard.EndOfFile,
00252                sizeof(FILE_END_OF_FILE_INFORMATION),
00253                FileEndOfFileInformation);
00254 
00255   if(!NT_SUCCESS(Status))
00256     {
00257       DPRINT1("NtSetInformationFile failed: %x\n", Status);
00258     }
00259 
00260  closedest:
00261   NtClose(FileHandleDest);
00262  unmapsrcsec:
00263   NtUnmapViewOfSection( NtCurrentProcess(), SourceFileMap );
00264  closesrcsec:
00265   NtClose(SourceFileSection);
00266  closesrc:
00267   NtClose(FileHandleSource);
00268  done:
00269   return(Status);
00270 }
00271 
00272 #ifdef __REACTOS__
00273 NTSTATUS
00274 SetupExtractFile(PWCHAR CabinetFileName,
00275         PWCHAR SourceFileName,
00276           PWCHAR DestinationPathName)
00277 {
00278   ULONG CabStatus;
00279 
00280   DPRINT("SetupExtractFile(CabinetFileName %S, SourceFileName %S, DestinationPathName %S)\n",
00281     CabinetFileName, SourceFileName, DestinationPathName);
00282 
00283   if (HasCurrentCabinet)
00284     {
00285       DPRINT("CurrentCabinetName: %S\n", CurrentCabinetName);
00286     }
00287 
00288   if ((HasCurrentCabinet) && (wcscmp(CabinetFileName, CurrentCabinetName) == 0))
00289     {
00290       DPRINT("Using same cabinet as last time\n");
00291 
00292       /* Use our last location because the files should be sequential */
00293       CabStatus = CabinetFindNextFileSequential(SourceFileName, &Search);
00294       if (CabStatus != CAB_STATUS_SUCCESS)
00295       {
00296           DPRINT("Sequential miss on file: %S\n", SourceFileName);
00297           
00298           /* Looks like we got unlucky */
00299           CabStatus = CabinetFindFirst(SourceFileName, &Search);
00300       }
00301     }
00302   else
00303     {
00304       DPRINT("Using new cabinet\n");
00305 
00306       if (HasCurrentCabinet)
00307         {
00308           CabinetCleanup();
00309         }
00310 
00311       wcscpy(CurrentCabinetName, CabinetFileName);
00312 
00313       CabinetInitialize();
00314       CabinetSetEventHandlers(NULL, NULL, NULL);
00315       CabinetSetCabinetName(CabinetFileName);
00316 
00317       CabStatus = CabinetOpen();
00318       if (CabStatus == CAB_STATUS_SUCCESS)
00319         {
00320           DPRINT("Opened cabinet %S\n", CabinetGetCabinetName());
00321           HasCurrentCabinet = TRUE;
00322         }
00323       else
00324         {
00325           DPRINT("Cannot open cabinet (%d)\n", CabStatus);
00326           return STATUS_UNSUCCESSFUL;
00327         }
00328         
00329       /* We have to start at the beginning here */
00330       CabStatus = CabinetFindFirst(SourceFileName, &Search);
00331     }
00332     
00333   if (CabStatus != CAB_STATUS_SUCCESS)
00334   {
00335       DPRINT1("Unable to find '%S' in cabinet '%S'\n", SourceFileName, CabinetGetCabinetName());
00336       return STATUS_UNSUCCESSFUL;
00337   }
00338 
00339   CabinetSetDestinationPath(DestinationPathName);
00340   CabStatus = CabinetExtractFile(&Search);
00341   if (CabStatus != CAB_STATUS_SUCCESS)
00342     {
00343       DPRINT("Cannot extract file %S (%d)\n", SourceFileName, CabStatus);
00344       return STATUS_UNSUCCESSFUL;
00345     }
00346 
00347   return STATUS_SUCCESS;
00348 }
00349 #endif
00350 
00351 BOOLEAN
00352 DoesFileExist(PWSTR PathName,
00353           PWSTR FileName)
00354 {
00355   OBJECT_ATTRIBUTES ObjectAttributes;
00356   IO_STATUS_BLOCK IoStatusBlock;
00357   UNICODE_STRING Name;
00358   WCHAR FullName[MAX_PATH];
00359   HANDLE FileHandle;
00360   NTSTATUS Status;
00361 
00362   wcscpy(FullName, PathName);
00363   if (FileName != NULL)
00364     {
00365       if (FileName[0] != L'\\')
00366     wcscat(FullName, L"\\");
00367       wcscat(FullName, FileName);
00368     }
00369 
00370   RtlInitUnicodeString(&Name,
00371                FullName);
00372 
00373   InitializeObjectAttributes(&ObjectAttributes,
00374                  &Name,
00375                  OBJ_CASE_INSENSITIVE,
00376                  NULL,
00377                  NULL);
00378 
00379   Status = NtOpenFile(&FileHandle,
00380               GENERIC_READ,
00381               &ObjectAttributes,
00382               &IoStatusBlock,
00383               0,
00384               FILE_SYNCHRONOUS_IO_NONALERT);
00385   if (!NT_SUCCESS(Status))
00386     {
00387       return(FALSE);
00388     }
00389 
00390   NtClose(FileHandle);
00391 
00392   return(TRUE);
00393 }
00394 
00395 /* EOF */

Generated on Fri May 25 2012 04:16:08 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.