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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* $Id: directory.c,v 1.6 2004/09/30 20:23:00 ekohl Exp $
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
31 /* FUNCTIONS ***************************************************************/
34 CopyDirectory (LPCWSTR lpDestinationPath
,
37 WCHAR szFileName
[MAX_PATH
];
38 WCHAR szFullSrcName
[MAX_PATH
];
39 WCHAR szFullDstName
[MAX_PATH
];
40 WIN32_FIND_DATAW FindFileData
;
45 DPRINT ("CopyDirectory (%S, %S) called\n",
46 lpDestinationPath
, lpSourcePath
);
48 wcscpy (szFileName
, lpSourcePath
);
49 wcscat (szFileName
, L
"\\*.*");
51 hFind
= FindFirstFileW (szFileName
,
53 if (hFind
== INVALID_HANDLE_VALUE
)
55 DPRINT1 ("Error: %lu\n", GetLastError());
59 wcscpy (szFullSrcName
, lpSourcePath
);
60 lpSrcPtr
= AppendBackslash (szFullSrcName
);
62 wcscpy (szFullDstName
, lpDestinationPath
);
63 lpDstPtr
= AppendBackslash (szFullDstName
);
67 if (wcscmp (FindFileData
.cFileName
, L
".") &&
68 wcscmp (FindFileData
.cFileName
, L
".."))
70 wcscpy (lpSrcPtr
, FindFileData
.cFileName
);
71 wcscpy (lpDstPtr
, FindFileData
.cFileName
);
73 if (FindFileData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
75 DPRINT ("Create directory: %S\n", szFullDstName
);
76 if (!CreateDirectoryW (szFullDstName
, NULL
))
78 if (GetLastError () != ERROR_ALREADY_EXISTS
)
80 DPRINT1 ("Error: %lu\n", GetLastError());
87 if (!CopyDirectory (szFullDstName
, szFullSrcName
))
89 DPRINT1 ("Error: %lu\n", GetLastError());
97 DPRINT1 ("Copy file: %S -> %S\n", szFullSrcName
, szFullDstName
);
98 if (!CopyFileW (szFullSrcName
, szFullDstName
, FALSE
))
100 DPRINT1 ("Error: %lu\n", GetLastError());
107 /* Copy file attributes */
108 if (FindFileData
.dwFileAttributes
& ~FILE_ATTRIBUTE_DIRECTORY
)
110 SetFileAttributesW (szFullDstName
,
111 FindFileData
.dwFileAttributes
);
115 if (!FindNextFileW (hFind
, &FindFileData
))
117 if (GetLastError () != ERROR_NO_MORE_FILES
)
119 DPRINT1 ("Error: %lu\n", GetLastError());
128 DPRINT ("Copy Directory() done\n");
135 CreateDirectoryPath (LPCWSTR lpPathName
,
136 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
138 WCHAR szPath
[MAX_PATH
];
142 DPRINT ("CreateDirectoryPath() called\n");
144 if (lpPathName
== NULL
|| *lpPathName
== 0)
147 if (CreateDirectoryW (lpPathName
,
148 lpSecurityAttributes
))
151 dwError
= GetLastError ();
152 if (dwError
== ERROR_ALREADY_EXISTS
)
155 wcscpy (szPath
, lpPathName
);
157 if (wcslen(szPath
) > 3 && szPath
[1] == ':' && szPath
[2] == '\\')
168 Ptr
= wcschr (Ptr
, L
'\\');
172 DPRINT ("CreateDirectory(%S)\n", szPath
);
173 if (!CreateDirectoryW (szPath
,
174 lpSecurityAttributes
))
176 dwError
= GetLastError ();
177 if (dwError
!= ERROR_ALREADY_EXISTS
)
188 DPRINT ("CreateDirectoryPath() done\n");
195 RecursiveRemoveDir (LPCWSTR lpPath
)
197 WCHAR szPath
[MAX_PATH
];
198 WIN32_FIND_DATAW FindData
;
202 wcscpy (szPath
, lpPath
);
203 wcscat (szPath
, L
"\\*.*");
204 DPRINT ("Search path: '%S'\n", szPath
);
206 hFind
= FindFirstFileW (szPath
,
208 if (hFind
== INVALID_HANDLE_VALUE
)
214 if (wcscmp (FindData
.cFileName
, L
".") &&
215 wcscmp (FindData
.cFileName
, L
".."))
217 wcscpy (szPath
, lpPath
);
218 wcscat (szPath
, L
"\\");
219 wcscat (szPath
, FindData
.cFileName
);
220 DPRINT ("File name: '%S'\n", szPath
);
222 if (FindData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
224 DPRINT ("Delete directory: '%S'\n", szPath
);
226 if (!RecursiveRemoveDir (szPath
))
232 if (FindData
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
)
234 SetFileAttributesW (szPath
,
235 FindData
.dwFileAttributes
& ~FILE_ATTRIBUTE_READONLY
);
238 if (!RemoveDirectoryW (szPath
))
246 DPRINT ("Delete file: '%S'\n", szPath
);
248 if (FindData
.dwFileAttributes
& (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
250 SetFileAttributesW (szPath
,
251 FILE_ATTRIBUTE_NORMAL
);
254 if (!DeleteFileW (szPath
))
262 if (!FindNextFileW (hFind
, &FindData
))
264 if (GetLastError () != ERROR_NO_MORE_FILES
)
266 DPRINT1 ("Error: %lu\n", GetLastError());
282 RemoveDirectoryPath (LPCWSTR lpPathName
)
284 if (!RecursiveRemoveDir (lpPathName
))
287 DPRINT1 ("Delete directory: '%S'\n", lpPathName
);
288 return RemoveDirectoryW (lpPathName
);