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.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS system libraries
23 * FILE: lib/userenv/directory.c
24 * PURPOSE: User profile code
25 * PROGRAMMER: Eric Kohl
34 /* FUNCTIONS ***************************************************************/
37 CopyProfileDirectoryA(LPCSTR lpSourcePath
,
38 LPCSTR lpDestinationPath
,
41 UNICODE_STRING SrcPath
;
42 UNICODE_STRING DstPath
;
46 Status
= RtlCreateUnicodeStringFromAsciiz(&SrcPath
,
48 if (!NT_SUCCESS(Status
))
50 SetLastError (RtlNtStatusToDosError (Status
));
54 Status
= RtlCreateUnicodeStringFromAsciiz(&DstPath
,
55 (LPSTR
)lpDestinationPath
);
56 if (!NT_SUCCESS(Status
))
58 RtlFreeUnicodeString(&SrcPath
);
59 SetLastError (RtlNtStatusToDosError (Status
));
63 bResult
= CopyProfileDirectoryW(SrcPath
.Buffer
,
67 RtlFreeUnicodeString(&DstPath
);
68 RtlFreeUnicodeString(&SrcPath
);
75 CopyProfileDirectoryW(LPCWSTR lpSourcePath
,
76 LPCWSTR lpDestinationPath
,
79 /* FIXME: dwFlags are ignored! */
80 return CopyDirectory(lpDestinationPath
, lpSourcePath
);
85 CopyDirectory (LPCWSTR lpDestinationPath
,
88 WCHAR szFileName
[MAX_PATH
];
89 WCHAR szFullSrcName
[MAX_PATH
];
90 WCHAR szFullDstName
[MAX_PATH
];
91 WIN32_FIND_DATAW FindFileData
;
96 DPRINT ("CopyDirectory (%S, %S) called\n",
97 lpDestinationPath
, lpSourcePath
);
99 wcscpy (szFileName
, lpSourcePath
);
100 wcscat (szFileName
, L
"\\*.*");
102 hFind
= FindFirstFileW (szFileName
,
104 if (hFind
== INVALID_HANDLE_VALUE
)
106 DPRINT1 ("Error: %lu\n", GetLastError());
110 wcscpy (szFullSrcName
, lpSourcePath
);
111 lpSrcPtr
= AppendBackslash (szFullSrcName
);
113 wcscpy (szFullDstName
, lpDestinationPath
);
114 lpDstPtr
= AppendBackslash (szFullDstName
);
118 if (wcscmp (FindFileData
.cFileName
, L
".") &&
119 wcscmp (FindFileData
.cFileName
, L
".."))
121 wcscpy (lpSrcPtr
, FindFileData
.cFileName
);
122 wcscpy (lpDstPtr
, FindFileData
.cFileName
);
124 if (FindFileData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
126 DPRINT ("Create directory: %S\n", szFullDstName
);
127 if (!CreateDirectoryExW (szFullSrcName
, szFullDstName
, NULL
))
129 if (GetLastError () != ERROR_ALREADY_EXISTS
)
131 DPRINT1 ("Error: %lu\n", GetLastError());
138 if (!CopyDirectory (szFullDstName
, szFullSrcName
))
140 DPRINT1 ("Error: %lu\n", GetLastError());
148 DPRINT ("Copy file: %S -> %S\n", szFullSrcName
, szFullDstName
);
149 if (!CopyFileW (szFullSrcName
, szFullDstName
, FALSE
))
151 DPRINT1 ("Error: %lu\n", GetLastError());
159 if (!FindNextFileW (hFind
, &FindFileData
))
161 if (GetLastError () != ERROR_NO_MORE_FILES
)
163 DPRINT1 ("Error: %lu\n", GetLastError());
172 DPRINT ("CopyDirectory() done\n");
179 CreateDirectoryPath (LPCWSTR lpPathName
,
180 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
182 WCHAR szPath
[MAX_PATH
];
186 DPRINT ("CreateDirectoryPath() called\n");
188 if (lpPathName
== NULL
|| *lpPathName
== 0)
191 if (CreateDirectoryW (lpPathName
,
192 lpSecurityAttributes
))
195 dwError
= GetLastError ();
196 if (dwError
== ERROR_ALREADY_EXISTS
)
199 wcscpy (szPath
, lpPathName
);
201 if (wcslen(szPath
) > 3 && szPath
[1] == ':' && szPath
[2] == '\\')
212 Ptr
= wcschr (Ptr
, L
'\\');
216 DPRINT ("CreateDirectory(%S)\n", szPath
);
217 if (!CreateDirectoryW (szPath
,
218 lpSecurityAttributes
))
220 dwError
= GetLastError ();
221 if (dwError
!= ERROR_ALREADY_EXISTS
)
232 DPRINT ("CreateDirectoryPath() done\n");
239 RecursiveRemoveDir (LPCWSTR lpPath
)
241 WCHAR szPath
[MAX_PATH
];
242 WIN32_FIND_DATAW FindData
;
246 wcscpy (szPath
, lpPath
);
247 wcscat (szPath
, L
"\\*.*");
248 DPRINT ("Search path: '%S'\n", szPath
);
250 hFind
= FindFirstFileW (szPath
,
252 if (hFind
== INVALID_HANDLE_VALUE
)
258 if (wcscmp (FindData
.cFileName
, L
".") &&
259 wcscmp (FindData
.cFileName
, L
".."))
261 wcscpy (szPath
, lpPath
);
262 wcscat (szPath
, L
"\\");
263 wcscat (szPath
, FindData
.cFileName
);
264 DPRINT ("File name: '%S'\n", szPath
);
266 if (FindData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
268 DPRINT ("Delete directory: '%S'\n", szPath
);
270 if (!RecursiveRemoveDir (szPath
))
276 if (FindData
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
)
278 SetFileAttributesW (szPath
,
279 FindData
.dwFileAttributes
& ~FILE_ATTRIBUTE_READONLY
);
282 if (!RemoveDirectoryW (szPath
))
290 DPRINT ("Delete file: '%S'\n", szPath
);
292 if (FindData
.dwFileAttributes
& (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
294 SetFileAttributesW (szPath
,
295 FILE_ATTRIBUTE_NORMAL
);
298 if (!DeleteFileW (szPath
))
306 if (!FindNextFileW (hFind
, &FindData
))
308 if (GetLastError () != ERROR_NO_MORE_FILES
)
310 DPRINT1 ("Error: %lu\n", GetLastError());
326 RemoveDirectoryPath (LPCWSTR lpPathName
)
328 if (!RecursiveRemoveDir (lpPathName
))
331 DPRINT ("Delete directory: '%S'\n", lpPathName
);
332 return RemoveDirectoryW (lpPathName
);