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

directory.c
Go to the documentation of this file.
00001 /*
00002  *  ReactOS kernel
00003  *  Copyright (C) 2004 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 /* $Id: directory.c 43790 2009-10-27 10:34:16Z dgorbachev $
00020  *
00021  * COPYRIGHT:       See COPYING in the top level directory
00022  * PROJECT:         ReactOS system libraries
00023  * FILE:            lib/userenv/directory.c
00024  * PURPOSE:         User profile code
00025  * PROGRAMMER:      Eric Kohl
00026  */
00027 
00028 #include <precomp.h>
00029 
00030 #define NDEBUG
00031 #include <debug.h>
00032 
00033 
00034 /* FUNCTIONS ***************************************************************/
00035 
00036 BOOL WINAPI
00037 CopyProfileDirectoryA(LPCSTR lpSourcePath,
00038               LPCSTR lpDestinationPath,
00039               DWORD dwFlags)
00040 {
00041   UNICODE_STRING SrcPath;
00042   UNICODE_STRING DstPath;
00043   NTSTATUS Status;
00044   BOOL bResult;
00045 
00046   Status = RtlCreateUnicodeStringFromAsciiz(&SrcPath,
00047                                             (LPSTR)lpSourcePath);
00048   if (!NT_SUCCESS(Status))
00049     {
00050       SetLastError (RtlNtStatusToDosError (Status));
00051       return FALSE;
00052     }
00053 
00054   Status = RtlCreateUnicodeStringFromAsciiz(&DstPath,
00055                                             (LPSTR)lpDestinationPath);
00056   if (!NT_SUCCESS(Status))
00057     {
00058       RtlFreeUnicodeString(&SrcPath);
00059       SetLastError (RtlNtStatusToDosError (Status));
00060       return FALSE;
00061     }
00062 
00063   bResult = CopyProfileDirectoryW(SrcPath.Buffer,
00064                                   DstPath.Buffer,
00065                                   dwFlags);
00066 
00067   RtlFreeUnicodeString(&DstPath);
00068   RtlFreeUnicodeString(&SrcPath);
00069 
00070   return bResult;
00071 }
00072 
00073 
00074 BOOL WINAPI
00075 CopyProfileDirectoryW(LPCWSTR lpSourcePath,
00076               LPCWSTR lpDestinationPath,
00077               DWORD dwFlags)
00078 {
00079   /* FIXME: dwFlags are ignored! */
00080   return CopyDirectory(lpDestinationPath, lpSourcePath);
00081 }
00082 
00083 
00084 BOOL
00085 CopyDirectory (LPCWSTR lpDestinationPath,
00086            LPCWSTR lpSourcePath)
00087 {
00088   WCHAR szFileName[MAX_PATH];
00089   WCHAR szFullSrcName[MAX_PATH];
00090   WCHAR szFullDstName[MAX_PATH];
00091   WIN32_FIND_DATAW FindFileData;
00092   LPWSTR lpSrcPtr;
00093   LPWSTR lpDstPtr;
00094   HANDLE hFind;
00095 
00096   DPRINT ("CopyDirectory (%S, %S) called\n",
00097       lpDestinationPath, lpSourcePath);
00098 
00099   wcscpy (szFileName, lpSourcePath);
00100   wcscat (szFileName, L"\\*.*");
00101 
00102   hFind = FindFirstFileW (szFileName,
00103               &FindFileData);
00104   if (hFind == INVALID_HANDLE_VALUE)
00105     {
00106       DPRINT1 ("Error: %lu\n", GetLastError());
00107       return FALSE;
00108     }
00109 
00110   wcscpy (szFullSrcName, lpSourcePath);
00111   lpSrcPtr = AppendBackslash (szFullSrcName);
00112 
00113   wcscpy (szFullDstName, lpDestinationPath);
00114   lpDstPtr = AppendBackslash (szFullDstName);
00115 
00116   for (;;)
00117     {
00118       if (wcscmp (FindFileData.cFileName, L".") &&
00119       wcscmp (FindFileData.cFileName, L".."))
00120     {
00121       wcscpy (lpSrcPtr, FindFileData.cFileName);
00122       wcscpy (lpDstPtr, FindFileData.cFileName);
00123 
00124       if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
00125         {
00126           DPRINT ("Create directory: %S\n", szFullDstName);
00127           if (!CreateDirectoryExW (szFullSrcName, szFullDstName, NULL))
00128         {
00129           if (GetLastError () != ERROR_ALREADY_EXISTS)
00130             {
00131               DPRINT1 ("Error: %lu\n", GetLastError());
00132 
00133               FindClose (hFind);
00134               return FALSE;
00135             }
00136         }
00137 
00138           if (!CopyDirectory (szFullDstName, szFullSrcName))
00139         {
00140           DPRINT1 ("Error: %lu\n", GetLastError());
00141 
00142           FindClose (hFind);
00143           return FALSE;
00144         }
00145         }
00146       else
00147         {
00148           DPRINT ("Copy file: %S -> %S\n", szFullSrcName, szFullDstName);
00149           if (!CopyFileW (szFullSrcName, szFullDstName, FALSE))
00150         {
00151           DPRINT1 ("Error: %lu\n", GetLastError());
00152 
00153           FindClose (hFind);
00154           return FALSE;
00155         }
00156         }
00157     }
00158 
00159       if (!FindNextFileW (hFind, &FindFileData))
00160     {
00161       if (GetLastError () != ERROR_NO_MORE_FILES)
00162         {
00163           DPRINT1 ("Error: %lu\n", GetLastError());
00164         }
00165 
00166       break;
00167     }
00168     }
00169 
00170   FindClose (hFind);
00171 
00172   DPRINT ("CopyDirectory() done\n");
00173 
00174   return TRUE;
00175 }
00176 
00177 
00178 BOOL
00179 CreateDirectoryPath (LPCWSTR lpPathName,
00180              LPSECURITY_ATTRIBUTES lpSecurityAttributes)
00181 {
00182   WCHAR szPath[MAX_PATH];
00183   LPWSTR Ptr;
00184   DWORD dwError;
00185 
00186   DPRINT ("CreateDirectoryPath() called\n");
00187 
00188   if (lpPathName == NULL || *lpPathName == 0)
00189     return TRUE;
00190 
00191   if (CreateDirectoryW (lpPathName,
00192             lpSecurityAttributes))
00193     return TRUE;
00194 
00195   dwError = GetLastError ();
00196   if (dwError == ERROR_ALREADY_EXISTS)
00197     return TRUE;
00198 
00199   wcscpy (szPath, lpPathName);
00200 
00201   if (wcslen(szPath) > 3 && szPath[1] == ':' && szPath[2] == '\\')
00202     {
00203       Ptr = &szPath[3];
00204     }
00205   else
00206     {
00207       Ptr = szPath;
00208     }
00209 
00210   while (Ptr != NULL)
00211     {
00212       Ptr = wcschr (Ptr, L'\\');
00213       if (Ptr != NULL)
00214         *Ptr = 0;
00215 
00216       DPRINT ("CreateDirectory(%S)\n", szPath);
00217       if (!CreateDirectoryW (szPath,
00218                  lpSecurityAttributes))
00219     {
00220       dwError = GetLastError ();
00221       if (dwError != ERROR_ALREADY_EXISTS)
00222         return FALSE;
00223     }
00224 
00225       if (Ptr != NULL)
00226     {
00227       *Ptr = L'\\';
00228       Ptr++;
00229     }
00230     }
00231 
00232   DPRINT ("CreateDirectoryPath() done\n");
00233 
00234   return TRUE;
00235 }
00236 
00237 
00238 static BOOL
00239 RecursiveRemoveDir (LPCWSTR lpPath)
00240 {
00241   WCHAR szPath[MAX_PATH];
00242   WIN32_FIND_DATAW FindData;
00243   HANDLE hFind;
00244   BOOL bResult;
00245 
00246   wcscpy (szPath, lpPath);
00247   wcscat (szPath, L"\\*.*");
00248   DPRINT ("Search path: '%S'\n", szPath);
00249 
00250   hFind = FindFirstFileW (szPath,
00251               &FindData);
00252   if (hFind == INVALID_HANDLE_VALUE)
00253     return FALSE;
00254 
00255   bResult = TRUE;
00256   while (TRUE)
00257     {
00258       if (wcscmp (FindData.cFileName, L".") &&
00259       wcscmp (FindData.cFileName, L".."))
00260     {
00261       wcscpy (szPath, lpPath);
00262       wcscat (szPath, L"\\");
00263       wcscat (szPath, FindData.cFileName);
00264       DPRINT ("File name: '%S'\n", szPath);
00265 
00266       if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
00267         {
00268           DPRINT ("Delete directory: '%S'\n", szPath);
00269 
00270           if (!RecursiveRemoveDir (szPath))
00271         {
00272           bResult = FALSE;
00273           break;
00274         }
00275 
00276           if (FindData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
00277         {
00278           SetFileAttributesW (szPath,
00279                       FindData.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY);
00280         }
00281 
00282           if (!RemoveDirectoryW (szPath))
00283         {
00284           bResult = FALSE;
00285           break;
00286         }
00287         }
00288       else
00289         {
00290           DPRINT ("Delete file: '%S'\n", szPath);
00291 
00292           if (FindData.dwFileAttributes & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
00293         {
00294           SetFileAttributesW (szPath,
00295                       FILE_ATTRIBUTE_NORMAL);
00296         }
00297 
00298           if (!DeleteFileW (szPath))
00299         {
00300           bResult = FALSE;
00301           break;
00302         }
00303         }
00304     }
00305 
00306       if (!FindNextFileW (hFind, &FindData))
00307     {
00308       if (GetLastError () != ERROR_NO_MORE_FILES)
00309         {
00310           DPRINT1 ("Error: %lu\n", GetLastError());
00311           bResult = FALSE;
00312           break;
00313         }
00314 
00315       break;
00316     }
00317     }
00318 
00319   FindClose (hFind);
00320 
00321   return bResult;
00322 }
00323 
00324 
00325 BOOL
00326 RemoveDirectoryPath (LPCWSTR lpPathName)
00327 {
00328   if (!RecursiveRemoveDir (lpPathName))
00329     return FALSE;
00330 
00331   DPRINT ("Delete directory: '%S'\n", lpPathName);
00332   return RemoveDirectoryW (lpPathName);
00333 }
00334 
00335 /* EOF */

Generated on Sun May 27 2012 04:26:42 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.