3 * Copyright (C) 2004 ReactOS Team
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.
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.
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.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS system libraries
22 * FILE: lib/userenv/directory.c
23 * PURPOSE: User profile code
24 * PROGRAMMER: Eric Kohl
33 /* FUNCTIONS ***************************************************************/
36 CopyProfileDirectoryA(LPCSTR lpSourcePath
,
37 LPCSTR lpDestinationPath
,
40 UNICODE_STRING SrcPath
;
41 UNICODE_STRING DstPath
;
44 if (!RtlCreateUnicodeStringFromAsciiz(&SrcPath
,
47 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
51 if (!RtlCreateUnicodeStringFromAsciiz(&DstPath
,
52 (LPSTR
)lpDestinationPath
))
54 RtlFreeUnicodeString(&SrcPath
);
55 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
59 bResult
= CopyProfileDirectoryW(SrcPath
.Buffer
,
63 RtlFreeUnicodeString(&DstPath
);
64 RtlFreeUnicodeString(&SrcPath
);
71 CopyProfileDirectoryW(LPCWSTR lpSourcePath
,
72 LPCWSTR lpDestinationPath
,
75 /* FIXME: dwFlags are ignored! */
76 return CopyDirectory(lpDestinationPath
, lpSourcePath
);
81 CopyDirectory (LPCWSTR lpDestinationPath
,
84 WCHAR szFileName
[MAX_PATH
];
85 WCHAR szFullSrcName
[MAX_PATH
];
86 WCHAR szFullDstName
[MAX_PATH
];
87 WIN32_FIND_DATAW FindFileData
;
92 DPRINT ("CopyDirectory (%S, %S) called\n",
93 lpDestinationPath
, lpSourcePath
);
95 wcscpy (szFileName
, lpSourcePath
);
96 wcscat (szFileName
, L
"\\*.*");
98 hFind
= FindFirstFileW (szFileName
,
100 if (hFind
== INVALID_HANDLE_VALUE
)
102 DPRINT1 ("Error: %lu\n", GetLastError());
106 wcscpy (szFullSrcName
, lpSourcePath
);
107 lpSrcPtr
= AppendBackslash (szFullSrcName
);
109 wcscpy (szFullDstName
, lpDestinationPath
);
110 lpDstPtr
= AppendBackslash (szFullDstName
);
114 if (wcscmp (FindFileData
.cFileName
, L
".") &&
115 wcscmp (FindFileData
.cFileName
, L
".."))
117 wcscpy (lpSrcPtr
, FindFileData
.cFileName
);
118 wcscpy (lpDstPtr
, FindFileData
.cFileName
);
120 if (FindFileData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
122 DPRINT ("Create directory: %S\n", szFullDstName
);
123 if (!CreateDirectoryExW (szFullSrcName
, szFullDstName
, NULL
))
125 if (GetLastError () != ERROR_ALREADY_EXISTS
)
127 DPRINT1 ("Error: %lu\n", GetLastError());
134 if (!CopyDirectory (szFullDstName
, szFullSrcName
))
136 DPRINT1 ("Error: %lu\n", GetLastError());
144 DPRINT ("Copy file: %S -> %S\n", szFullSrcName
, szFullDstName
);
145 if (!CopyFileW (szFullSrcName
, szFullDstName
, FALSE
))
147 DPRINT1 ("Error: %lu\n", GetLastError());
155 if (!FindNextFileW (hFind
, &FindFileData
))
157 if (GetLastError () != ERROR_NO_MORE_FILES
)
159 DPRINT1 ("Error: %lu\n", GetLastError());
168 DPRINT ("CopyDirectory() done\n");
175 CreateDirectoryPath (LPCWSTR lpPathName
,
176 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
178 WCHAR szPath
[MAX_PATH
];
182 DPRINT ("CreateDirectoryPath() called\n");
184 if (lpPathName
== NULL
|| *lpPathName
== 0)
187 if (CreateDirectoryW (lpPathName
,
188 lpSecurityAttributes
))
191 dwError
= GetLastError ();
192 if (dwError
== ERROR_ALREADY_EXISTS
)
195 wcscpy (szPath
, lpPathName
);
197 if (wcslen(szPath
) > 3 && szPath
[1] == ':' && szPath
[2] == '\\')
208 Ptr
= wcschr (Ptr
, L
'\\');
212 DPRINT ("CreateDirectory(%S)\n", szPath
);
213 if (!CreateDirectoryW (szPath
,
214 lpSecurityAttributes
))
216 dwError
= GetLastError ();
217 if (dwError
!= ERROR_ALREADY_EXISTS
)
228 DPRINT ("CreateDirectoryPath() done\n");
235 RecursiveRemoveDir (LPCWSTR lpPath
)
237 WCHAR szPath
[MAX_PATH
];
238 WIN32_FIND_DATAW FindData
;
242 wcscpy (szPath
, lpPath
);
243 wcscat (szPath
, L
"\\*.*");
244 DPRINT ("Search path: '%S'\n", szPath
);
246 hFind
= FindFirstFileW (szPath
,
248 if (hFind
== INVALID_HANDLE_VALUE
)
254 if (wcscmp (FindData
.cFileName
, L
".") &&
255 wcscmp (FindData
.cFileName
, L
".."))
257 wcscpy (szPath
, lpPath
);
258 wcscat (szPath
, L
"\\");
259 wcscat (szPath
, FindData
.cFileName
);
260 DPRINT ("File name: '%S'\n", szPath
);
262 if (FindData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
264 DPRINT ("Delete directory: '%S'\n", szPath
);
266 if (!RecursiveRemoveDir (szPath
))
272 if (FindData
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
)
274 SetFileAttributesW (szPath
,
275 FindData
.dwFileAttributes
& ~FILE_ATTRIBUTE_READONLY
);
278 if (!RemoveDirectoryW (szPath
))
286 DPRINT ("Delete file: '%S'\n", szPath
);
288 if (FindData
.dwFileAttributes
& (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
290 SetFileAttributesW (szPath
,
291 FILE_ATTRIBUTE_NORMAL
);
294 if (!DeleteFileW (szPath
))
302 if (!FindNextFileW (hFind
, &FindData
))
304 if (GetLastError () != ERROR_NO_MORE_FILES
)
306 DPRINT1 ("Error: %lu\n", GetLastError());
322 RemoveDirectoryPath (LPCWSTR lpPathName
)
324 if (!RecursiveRemoveDir (lpPathName
))
327 DPRINT ("Delete directory: '%S'\n", lpPathName
);
328 return RemoveDirectoryW (lpPathName
);