ReactOS  0.4.13-dev-249-gcba1a2f
directory.c
Go to the documentation of this file.
1 /*
2  * ReactOS kernel
3  * Copyright (C) 2004 ReactOS Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 /*
20  * COPYRIGHT: See COPYING in the top level directory
21  * PROJECT: ReactOS system libraries
22  * FILE: dll/win32/userenv/directory.c
23  * PURPOSE: User profile code
24  * PROGRAMMER: Eric Kohl
25  */
26 
27 #include "precomp.h"
28 
29 #define NDEBUG
30 #include <debug.h>
31 
32 /* FUNCTIONS ***************************************************************/
33 
34 BOOL
35 WINAPI
37  LPCSTR lpDestinationPath,
38  DWORD dwFlags)
39 {
40  UNICODE_STRING SrcPath;
41  UNICODE_STRING DstPath;
42  BOOL bResult;
43 
45  (LPSTR)lpSourcePath))
46  {
48  return FALSE;
49  }
50 
52  (LPSTR)lpDestinationPath))
53  {
54  RtlFreeUnicodeString(&SrcPath);
56  return FALSE;
57  }
58 
59  bResult = CopyProfileDirectoryW(SrcPath.Buffer,
60  DstPath.Buffer,
61  dwFlags);
62 
63  RtlFreeUnicodeString(&DstPath);
64  RtlFreeUnicodeString(&SrcPath);
65 
66  return bResult;
67 }
68 
69 
70 BOOL
71 WINAPI
73  LPCWSTR lpDestinationPath,
74  DWORD dwFlags)
75 {
76  /* FIXME: dwFlags are ignored! */
77  return CopyDirectory(lpDestinationPath, lpSourcePath);
78 }
79 
80 
81 BOOL
82 CopyDirectory(LPCWSTR lpDestinationPath,
83  LPCWSTR lpSourcePath)
84 {
85  WCHAR szFileName[MAX_PATH];
86  WCHAR szFullSrcName[MAX_PATH];
87  WCHAR szFullDstName[MAX_PATH];
88  WIN32_FIND_DATAW FindFileData;
89  LPWSTR lpSrcPtr;
90  LPWSTR lpDstPtr;
91  HANDLE hFind;
92 
93  DPRINT("CopyDirectory (%S, %S) called\n",
94  lpDestinationPath, lpSourcePath);
95 
96  wcscpy(szFileName, lpSourcePath);
97  wcscat(szFileName, L"\\*.*");
98 
99  hFind = FindFirstFileW(szFileName,
100  &FindFileData);
101  if (hFind == INVALID_HANDLE_VALUE)
102  {
103  DPRINT1("Error: %lu\n", GetLastError());
104  return FALSE;
105  }
106 
107  wcscpy(szFullSrcName, lpSourcePath);
108  lpSrcPtr = AppendBackslash(szFullSrcName);
109 
110  wcscpy(szFullDstName, lpDestinationPath);
111  lpDstPtr = AppendBackslash(szFullDstName);
112 
113  for (;;)
114  {
115  if (wcscmp(FindFileData.cFileName, L".") &&
116  wcscmp(FindFileData.cFileName, L".."))
117  {
118  wcscpy(lpSrcPtr, FindFileData.cFileName);
119  wcscpy(lpDstPtr, FindFileData.cFileName);
120 
121  if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
122  {
123  DPRINT("Create directory: %S\n", szFullDstName);
124  if (!CreateDirectoryExW(szFullSrcName, szFullDstName, NULL))
125  {
127  {
128  DPRINT1("Error: %lu\n", GetLastError());
129 
130  FindClose(hFind);
131  return FALSE;
132  }
133  }
134 
135  if (!CopyDirectory(szFullDstName, szFullSrcName))
136  {
137  DPRINT1("Error: %lu\n", GetLastError());
138 
139  FindClose(hFind);
140  return FALSE;
141  }
142  }
143  else
144  {
145  DPRINT("Copy file: %S -> %S\n", szFullSrcName, szFullDstName);
146  if (!CopyFileW(szFullSrcName, szFullDstName, FALSE))
147  {
148  DPRINT1("Error: %lu\n", GetLastError());
149 
150  FindClose(hFind);
151  return FALSE;
152  }
153  }
154  }
155 
156  if (!FindNextFileW(hFind, &FindFileData))
157  {
159  {
160  DPRINT1("Error: %lu\n", GetLastError());
161  }
162 
163  break;
164  }
165  }
166 
167  FindClose(hFind);
168 
169  DPRINT("CopyDirectory() done\n");
170 
171  return TRUE;
172 }
173 
174 
175 BOOL
177  LPSECURITY_ATTRIBUTES lpSecurityAttributes)
178 {
180  LPWSTR Ptr;
181  DWORD dwError;
182 
183  DPRINT("CreateDirectoryPath() called\n");
184 
185  if (lpPathName == NULL || *lpPathName == 0)
186  return TRUE;
187 
188  if (CreateDirectoryW(lpPathName,
189  lpSecurityAttributes))
190  return TRUE;
191 
192  dwError = GetLastError();
193  if (dwError == ERROR_ALREADY_EXISTS)
194  return TRUE;
195 
196  wcscpy(szPath, lpPathName);
197 
198  if (wcslen(szPath) > 3 && szPath[1] == ':' && szPath[2] == '\\')
199  {
200  Ptr = &szPath[3];
201  }
202  else
203  {
204  Ptr = szPath;
205  }
206 
207  while (Ptr != NULL)
208  {
209  Ptr = wcschr(Ptr, L'\\');
210  if (Ptr != NULL)
211  *Ptr = 0;
212 
213  DPRINT("CreateDirectory(%S)\n", szPath);
215  lpSecurityAttributes))
216  {
217  dwError = GetLastError();
218  if (dwError != ERROR_ALREADY_EXISTS)
219  return FALSE;
220  }
221 
222  if (Ptr != NULL)
223  {
224  *Ptr = L'\\';
225  Ptr++;
226  }
227  }
228 
229  DPRINT("CreateDirectoryPath() done\n");
230 
231  return TRUE;
232 }
233 
234 
235 static
236 BOOL
238 {
240  WIN32_FIND_DATAW FindData;
241  HANDLE hFind;
242  BOOL bResult;
243 
244  wcscpy(szPath, lpPath);
245  wcscat(szPath, L"\\*.*");
246  DPRINT("Search path: '%S'\n", szPath);
247 
248  hFind = FindFirstFileW(szPath,
249  &FindData);
250  if (hFind == INVALID_HANDLE_VALUE)
251  return FALSE;
252 
253  bResult = TRUE;
254  while (TRUE)
255  {
256  if (wcscmp(FindData.cFileName, L".") &&
257  wcscmp(FindData.cFileName, L".."))
258  {
259  wcscpy(szPath, lpPath);
260  wcscat(szPath, L"\\");
261  wcscat(szPath, FindData.cFileName);
262  DPRINT("File name: '%S'\n", szPath);
263 
264  if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
265  {
266  DPRINT("Delete directory: '%S'\n", szPath);
267 
269  {
270  bResult = FALSE;
271  break;
272  }
273 
274  if (FindData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
275  {
277  FindData.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY);
278  }
279 
280  if (!RemoveDirectoryW(szPath))
281  {
282  bResult = FALSE;
283  break;
284  }
285  }
286  else
287  {
288  DPRINT("Delete file: '%S'\n", szPath);
289 
290  if (FindData.dwFileAttributes & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
291  {
294  }
295 
296  if (!DeleteFileW(szPath))
297  {
298  bResult = FALSE;
299  break;
300  }
301  }
302  }
303 
304  if (!FindNextFileW(hFind, &FindData))
305  {
307  {
308  DPRINT1("Error: %lu\n", GetLastError());
309  bResult = FALSE;
310  break;
311  }
312 
313  break;
314  }
315  }
316 
317  FindClose(hFind);
318 
319  return bResult;
320 }
321 
322 
323 BOOL
325 {
326  if (!RecursiveRemoveDir(lpPathName))
327  return FALSE;
328 
329  DPRINT("Delete directory: '%S'\n", lpPathName);
330  return RemoveDirectoryW(lpPathName);
331 }
332 
333 /* EOF */
BOOL WINAPI SetFileAttributesW(LPCWSTR lpFileName, DWORD dwFileAttributes)
Definition: fileinfo.c:944
BOOL WINAPI CreateDirectoryW(IN LPCWSTR lpPathName, IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
Definition: dir.c:90
BOOL WINAPI FindNextFileW(IN HANDLE hFindFile, OUT LPWIN32_FIND_DATAW lpFindFileData)
Definition: find.c:382
#define TRUE
Definition: types.h:120
LPWSTR AppendBackslash(LPWSTR String)
Definition: misc.c:40
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define FILE_ATTRIBUTE_SYSTEM
Definition: nt_native.h:704
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
char * LPSTR
Definition: xmlstorage.h:182
BOOL CopyDirectory(LPCWSTR lpDestinationPath, LPCWSTR lpSourcePath)
Definition: directory.c:82
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL WINAPI DeleteFileW(IN LPCWSTR lpFileName)
Definition: delete.c:39
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
NTSYSAPI BOOLEAN NTAPI RtlCreateUnicodeStringFromAsciiz(_Out_ PUNICODE_STRING Destination, _In_ PCSZ Source)
const char * LPCSTR
Definition: xmlstorage.h:183
BOOL WINAPI RemoveDirectoryW(IN LPCWSTR lpPathName)
Definition: dir.c:732
#define ERROR_NO_MORE_FILES
Definition: winerror.h:121
#define FILE_ATTRIBUTE_DIRECTORY
Definition: nt_native.h:705
BOOL RemoveDirectoryPath(LPCWSTR lpPathName)
Definition: directory.c:324
_CONST_RETURN wchar_t *__cdecl wcschr(_In_z_ const wchar_t *_Str, wchar_t _Ch)
__wchar_t WCHAR
Definition: xmlstorage.h:180
BOOL WINAPI CopyFileW(IN LPCWSTR lpExistingFileName, IN LPCWSTR lpNewFileName, IN BOOL bFailIfExists)
Definition: copy.c:439
#define MAX_PATH
Definition: compat.h:26
#define WINAPI
Definition: msvc.h:8
#define FILE_ATTRIBUTE_READONLY
Definition: nt_native.h:702
unsigned long DWORD
Definition: ntddk_ex.h:95
static BOOL RecursiveRemoveDir(LPCWSTR lpPath)
Definition: directory.c:237
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
#define SetLastError(x)
Definition: compat.h:409
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
static const WCHAR L[]
Definition: oid.c:1250
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1175
BOOL WINAPI CopyProfileDirectoryA(LPCSTR lpSourcePath, LPCSTR lpDestinationPath, DWORD dwFlags)
Definition: directory.c:36
_Check_return_ _CRTIMP int __cdecl wcscmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
BOOL CreateDirectoryPath(LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
Definition: directory.c:176
LPCWSTR szPath
Definition: env.c:35
_CRTIMP wchar_t *__cdecl wcscat(_Inout_updates_z_(_String_length_(_Dest)+_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
BOOL WINAPI CreateDirectoryExW(IN LPCWSTR lpTemplateDirectory, IN LPCWSTR lpNewDirectory, IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
Definition: dir.c:193
#define DPRINT1
Definition: precomp.h:8
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
WCHAR * LPWSTR
Definition: xmlstorage.h:184
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
BOOL WINAPI CopyProfileDirectoryW(LPCWSTR lpSourcePath, LPCWSTR lpDestinationPath, DWORD dwFlags)
Definition: directory.c:72
HANDLE WINAPI FindFirstFileW(IN LPCWSTR lpFileName, OUT LPWIN32_FIND_DATAW lpFindFileData)
Definition: find.c:320
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502