[SHELL32]
[reactos.git] / dll / win32 / shell32 / shlfileop.cpp
1 /*
2 * SHFileOperation
3 *
4 * Copyright 2000 Juergen Schmied
5 * Copyright 2002 Andriy Palamarchuk
6 * Copyright 2004 Dietrich Teickner (from Odin)
7 * Copyright 2004 Rolf Kalbermatter
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24 #include "precomp.h"
25
26 #include "xdg.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(shell);
29
30 #define IsAttrib(x, y) ((INVALID_FILE_ATTRIBUTES != (x)) && ((x) & (y)))
31 #define IsAttribFile(x) (!((x) & FILE_ATTRIBUTE_DIRECTORY))
32 #define IsAttribDir(x) IsAttrib(x, FILE_ATTRIBUTE_DIRECTORY)
33 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
34
35 #define FO_MASK 0xF
36 #define WM_FILE (WM_USER + 1)
37 #define TIMER_ID (100)
38
39 static const WCHAR wWildcardFile[] = {'*',0};
40 static const WCHAR wWildcardChars[] = {'*','?',0};
41
42 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec);
43 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path);
44 static DWORD SHNotifyDeleteFileW(LPCWSTR path);
45 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest, BOOL isdir);
46 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists);
47 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly);
48
49 typedef struct
50 {
51 SHFILEOPSTRUCTW *req;
52 DWORD dwYesToAllMask;
53 BOOL bManyItems;
54 BOOL bCancelled;
55 } FILE_OPERATION;
56
57 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
58
59 typedef struct
60 {
61 DWORD attributes;
62 LPWSTR szDirectory;
63 LPWSTR szFilename;
64 LPWSTR szFullPath;
65 BOOL bFromWildcard;
66 BOOL bFromRelative;
67 BOOL bExists;
68 } FILE_ENTRY;
69
70 typedef struct
71 {
72 FILE_ENTRY *feFiles;
73 DWORD num_alloc;
74 DWORD dwNumFiles;
75 BOOL bAnyFromWildcard;
76 BOOL bAnyDirectories;
77 BOOL bAnyDontExist;
78 } FILE_LIST;
79
80 typedef struct
81 {
82 FILE_LIST * from;
83 FILE_LIST * to;
84 FILE_OPERATION * op;
85 DWORD Index;
86 HWND hDlgCtrl;
87 HWND hwndDlg;
88 }FILE_OPERATION_CONTEXT;
89
90
91 /* Confirm dialogs with an optional "Yes To All" as used in file operations confirmations
92 */
93 static const WCHAR CONFIRM_MSG_PROP[] = {'W','I','N','E','_','C','O','N','F','I','R','M',0};
94
95 struct confirm_msg_info
96 {
97 LPWSTR lpszText;
98 LPWSTR lpszCaption;
99 HICON hIcon;
100 BOOL bYesToAll;
101 };
102
103 /* as some buttons may be hidden and the dialog height may change we may need
104 * to move the controls */
105 static void confirm_msg_move_button(HWND hDlg, INT iId, INT *xPos, INT yOffset, BOOL bShow)
106 {
107 HWND hButton = GetDlgItem(hDlg, iId);
108 RECT r;
109
110 if (bShow)
111 {
112 POINT pt;
113 int width;
114
115 GetWindowRect(hButton, &r);
116 width = r.right - r.left;
117 pt.x = r.left;
118 pt.y = r.top;
119 ScreenToClient(hDlg, &pt);
120 MoveWindow(hButton, *xPos - width, pt.y - yOffset, width, r.bottom - r.top, FALSE);
121 *xPos -= width + 5;
122 }
123 else
124 ShowWindow(hButton, SW_HIDE);
125 }
126
127 /* Note: we paint the text manually and don't use the static control to make
128 * sure the text has the same height as the one computed in WM_INITDIALOG
129 */
130 static INT_PTR ConfirmMsgBox_Paint(HWND hDlg)
131 {
132 PAINTSTRUCT ps;
133 HFONT hOldFont;
134 RECT r;
135 HDC hdc;
136
137 BeginPaint(hDlg, &ps);
138 hdc = ps.hdc;
139
140 GetClientRect(GetDlgItem(hDlg, IDC_YESTOALL_MESSAGE), &r);
141 /* this will remap the rect to dialog coords */
142 MapWindowPoints(GetDlgItem(hDlg, IDC_YESTOALL_MESSAGE), hDlg, (LPPOINT)&r, 2);
143 hOldFont = (HFONT)SelectObject(hdc, (HFONT)SendDlgItemMessageW(hDlg, IDC_YESTOALL_MESSAGE, WM_GETFONT, 0, 0));
144 DrawTextW(hdc, (LPWSTR)GetPropW(hDlg, CONFIRM_MSG_PROP), -1, &r, DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_WORDBREAK);
145 SelectObject(hdc, hOldFont);
146 EndPaint(hDlg, &ps);
147
148 return TRUE;
149 }
150
151 static INT_PTR ConfirmMsgBox_Init(HWND hDlg, LPARAM lParam)
152 {
153 struct confirm_msg_info *info = (struct confirm_msg_info *)lParam;
154 INT xPos, yOffset;
155 int width, height;
156 HFONT hOldFont;
157 HDC hdc;
158 RECT r;
159
160 SetWindowTextW(hDlg, info->lpszCaption);
161 ShowWindow(GetDlgItem(hDlg, IDC_YESTOALL_MESSAGE), SW_HIDE);
162 SetPropW(hDlg, CONFIRM_MSG_PROP, info->lpszText);
163 SendDlgItemMessageW(hDlg, IDC_YESTOALL_ICON, STM_SETICON, (WPARAM)info->hIcon, 0);
164
165 /* compute the text height and resize the dialog */
166 GetClientRect(GetDlgItem(hDlg, IDC_YESTOALL_MESSAGE), &r);
167 hdc = GetDC(hDlg);
168 yOffset = r.bottom;
169 hOldFont = (HFONT)SelectObject(hdc, (HFONT)SendDlgItemMessageW(hDlg, IDC_YESTOALL_MESSAGE, WM_GETFONT, 0, 0));
170 DrawTextW(hdc, info->lpszText, -1, &r, DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_WORDBREAK | DT_CALCRECT);
171 SelectObject(hdc, hOldFont);
172 yOffset -= r.bottom;
173 yOffset = min(yOffset, 35); /* don't make the dialog too small */
174 ReleaseDC(hDlg, hdc);
175
176 GetClientRect(hDlg, &r);
177 xPos = r.right - 7;
178 GetWindowRect(hDlg, &r);
179 width = r.right - r.left;
180 height = r.bottom - r.top - yOffset;
181 MoveWindow(hDlg, (GetSystemMetrics(SM_CXSCREEN) - width)/2,
182 (GetSystemMetrics(SM_CYSCREEN) - height)/2, width, height, FALSE);
183
184 confirm_msg_move_button(hDlg, IDCANCEL, &xPos, yOffset, info->bYesToAll);
185 confirm_msg_move_button(hDlg, IDNO, &xPos, yOffset, TRUE);
186 confirm_msg_move_button(hDlg, IDC_YESTOALL, &xPos, yOffset, info->bYesToAll);
187 confirm_msg_move_button(hDlg, IDYES, &xPos, yOffset, TRUE);
188
189 return TRUE;
190 }
191
192 static INT_PTR CALLBACK ConfirmMsgBoxProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
193 {
194 switch (uMsg)
195 {
196 case WM_INITDIALOG:
197 return ConfirmMsgBox_Init(hDlg, lParam);
198 case WM_PAINT:
199 return ConfirmMsgBox_Paint(hDlg);
200 case WM_COMMAND:
201 EndDialog(hDlg, wParam);
202 break;
203 case WM_CLOSE:
204 EndDialog(hDlg, IDCANCEL);
205 break;
206 }
207 return FALSE;
208 }
209
210 int SHELL_ConfirmMsgBox(HWND hWnd, LPWSTR lpszText, LPWSTR lpszCaption, HICON hIcon, BOOL bYesToAll)
211 {
212 struct confirm_msg_info info;
213
214 info.lpszText = lpszText;
215 info.lpszCaption = lpszCaption;
216 info.hIcon = hIcon;
217 info.bYesToAll = bYesToAll;
218 return DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_YESTOALL_MSGBOX), hWnd, ConfirmMsgBoxProc, (LPARAM)&info);
219 }
220
221 /* confirmation dialogs content */
222 typedef struct
223 {
224 HINSTANCE hIconInstance;
225 UINT icon_resource_id;
226 UINT caption_resource_id, text_resource_id;
227 } SHELL_ConfirmIDstruc;
228
229 static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
230 {
231 ids->hIconInstance = shell32_hInstance;
232 switch (nKindOfDialog)
233 {
234 case ASK_DELETE_FILE:
235 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
236 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
237 ids->text_resource_id = IDS_DELETEITEM_TEXT;
238 return TRUE;
239
240 case ASK_DELETE_FOLDER:
241 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
242 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
243 ids->text_resource_id = IDS_DELETEITEM_TEXT;
244 return TRUE;
245
246 case ASK_DELETE_MULTIPLE_ITEM:
247 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
248 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
249 ids->text_resource_id = IDS_DELETEMULTIPLE_TEXT;
250 return TRUE;
251
252 case ASK_TRASH_FILE:
253 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
254 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
255 ids->text_resource_id = IDS_TRASHITEM_TEXT;
256 return TRUE;
257
258 case ASK_TRASH_FOLDER:
259 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
260 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
261 ids->text_resource_id = IDS_TRASHFOLDER_TEXT;
262 return TRUE;
263
264 case ASK_TRASH_MULTIPLE_ITEM:
265 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
266 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
267 ids->text_resource_id = IDS_TRASHMULTIPLE_TEXT;
268 return TRUE;
269
270 case ASK_CANT_TRASH_ITEM:
271 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
272 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
273 ids->text_resource_id = IDS_CANTTRASH_TEXT;
274 return TRUE;
275
276 case ASK_DELETE_SELECTED:
277 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
278 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
279 ids->text_resource_id = IDS_DELETESELECTED_TEXT;
280 return TRUE;
281
282 case ASK_OVERWRITE_FILE:
283 ids->hIconInstance = NULL;
284 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
285 ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
286 ids->text_resource_id = IDS_OVERWRITEFILE_TEXT;
287 return TRUE;
288
289 case ASK_OVERWRITE_FOLDER:
290 ids->hIconInstance = NULL;
291 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
292 ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
293 ids->text_resource_id = IDS_OVERWRITEFOLDER_TEXT;
294 return TRUE;
295
296 default:
297 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
298 }
299 return FALSE;
300 }
301
302 static BOOL SHELL_ConfirmDialogW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir, FILE_OPERATION *op)
303 {
304 WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
305 SHELL_ConfirmIDstruc ids;
306 DWORD_PTR args[1];
307 HICON hIcon;
308 int ret;
309
310 assert(nKindOfDialog >= 0 && nKindOfDialog < 32);
311 if (op && (op->dwYesToAllMask & (1 << nKindOfDialog)))
312 return TRUE;
313
314 if (!SHELL_ConfirmIDs(nKindOfDialog, &ids)) return FALSE;
315
316 LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption)/sizeof(WCHAR));
317 LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText)/sizeof(WCHAR));
318
319 args[0] = (DWORD_PTR)szDir;
320 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
321 szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)args);
322 hIcon = LoadIconW(ids.hIconInstance, (LPWSTR)MAKEINTRESOURCE(ids.icon_resource_id));
323
324 ret = SHELL_ConfirmMsgBox(hWnd, szBuffer, szCaption, hIcon, op && op->bManyItems);
325 if (op)
326 {
327 if (ret == IDC_YESTOALL)
328 {
329 op->dwYesToAllMask |= (1 << nKindOfDialog);
330 ret = IDYES;
331 }
332 if (ret == IDCANCEL)
333 op->bCancelled = TRUE;
334 if (ret != IDYES)
335 op->req->fAnyOperationsAborted = TRUE;
336 }
337 return ret == IDYES;
338 }
339
340 BOOL SHELL_ConfirmYesNoW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir)
341 {
342 return SHELL_ConfirmDialogW(hWnd, nKindOfDialog, szDir, NULL);
343 }
344
345 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
346 {
347 DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
348
349 if (len < minChars)
350 len = minChars;
351
352 *wPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
353 if (*wPath)
354 {
355 MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
356 return NO_ERROR;
357 }
358 return E_OUTOFMEMORY;
359 }
360
361 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
362 {
363 HeapFree(GetProcessHeap(), 0, wPath);
364 }
365
366 EXTERN_C HRESULT WINAPI SHIsFileAvailableOffline(LPCWSTR path, LPDWORD status)
367 {
368 FIXME("(%s, %p) stub\n", debugstr_w(path), status);
369 return E_FAIL;
370 }
371
372 /**************************************************************************
373 * SHELL_DeleteDirectory() [internal]
374 *
375 * Asks for confirmation when bShowUI is true and deletes the directory and
376 * all its subdirectories and files if necessary.
377 */
378 BOOL SHELL_DeleteDirectoryW(HWND hwnd, LPCWSTR pszDir, BOOL bShowUI)
379 {
380 BOOL ret = TRUE;
381 HANDLE hFind;
382 WIN32_FIND_DATAW wfd;
383 WCHAR szTemp[MAX_PATH];
384
385 /* Make sure the directory exists before eventually prompting the user */
386 PathCombineW(szTemp, pszDir, wWildcardFile);
387 hFind = FindFirstFileW(szTemp, &wfd);
388 if (hFind == INVALID_HANDLE_VALUE)
389 return FALSE;
390
391 if (!bShowUI || (ret = SHELL_ConfirmDialogW(hwnd, ASK_DELETE_FOLDER, pszDir, NULL)))
392 {
393 do
394 {
395 if (IsDotDir(wfd.cFileName))
396 continue;
397 PathCombineW(szTemp, pszDir, wfd.cFileName);
398 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
399 ret = SHELL_DeleteDirectoryW(hwnd, szTemp, FALSE);
400 else
401 ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
402 } while (ret && FindNextFileW(hFind, &wfd));
403 }
404 FindClose(hFind);
405 if (ret)
406 ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
407 return ret;
408 }
409
410 /**************************************************************************
411 * Win32CreateDirectory [SHELL32.93]
412 *
413 * Creates a directory. Also triggers a change notify if one exists.
414 *
415 * PARAMS
416 * path [I] path to directory to create
417 *
418 * RETURNS
419 * TRUE if successful, FALSE otherwise
420 */
421
422 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
423 {
424 TRACE("(%s, %p)\n", debugstr_w(path), sec);
425
426 if (CreateDirectoryW(path, sec))
427 {
428 SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
429 return ERROR_SUCCESS;
430 }
431 return GetLastError();
432 }
433
434 /**********************************************************************/
435
436 EXTERN_C BOOL WINAPI Win32CreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
437 {
438 return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
439 }
440
441 /************************************************************************
442 * Win32RemoveDirectory [SHELL32.94]
443 *
444 * Deletes a directory. Also triggers a change notify if one exists.
445 *
446 * PARAMS
447 * path [I] path to directory to delete
448 *
449 * RETURNS
450 * TRUE if successful, FALSE otherwise
451 */
452 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
453 {
454 BOOL ret;
455 TRACE("(%s)\n", debugstr_w(path));
456
457 ret = RemoveDirectoryW(path);
458 if (!ret)
459 {
460 /* Directory may be write protected */
461 DWORD dwAttr = GetFileAttributesW(path);
462 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
463 if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
464 ret = RemoveDirectoryW(path);
465 }
466 if (ret)
467 {
468 SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
469 return ERROR_SUCCESS;
470 }
471 return GetLastError();
472 }
473
474 /***********************************************************************/
475
476 EXTERN_C BOOL WINAPI Win32RemoveDirectoryW(LPCWSTR path)
477 {
478 return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
479 }
480
481 /************************************************************************
482 * Win32DeleteFile [SHELL32.164]
483 *
484 * Deletes a file. Also triggers a change notify if one exists.
485 *
486 * PARAMS
487 * path [I] path to file to delete
488 *
489 * RETURNS
490 * TRUE if successful, FALSE otherwise
491 */
492 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
493 {
494 BOOL ret;
495
496 TRACE("(%s)\n", debugstr_w(path));
497
498 ret = DeleteFileW(path);
499 if (!ret)
500 {
501 /* File may be write protected or a system file */
502 DWORD dwAttr = GetFileAttributesW(path);
503 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
504 if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
505 ret = DeleteFileW(path);
506 }
507 if (ret)
508 {
509 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
510 return ERROR_SUCCESS;
511 }
512 return GetLastError();
513 }
514
515 /***********************************************************************/
516
517 EXTERN_C DWORD WINAPI Win32DeleteFileW(LPCWSTR path)
518 {
519 return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
520 }
521
522 /************************************************************************
523 * SHNotifyMoveFile [internal]
524 *
525 * Moves a file. Also triggers a change notify if one exists.
526 *
527 * PARAMS
528 * src [I] path to source file to move
529 * dest [I] path to target file to move to
530 *
531 * RETURNS
532 * ERORR_SUCCESS if successful
533 */
534 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest, BOOL isdir)
535 {
536 BOOL ret;
537
538 TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
539
540 ret = MoveFileExW(src, dest, MOVEFILE_REPLACE_EXISTING);
541
542 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
543 if (!ret)
544 ret = MoveFileW(src, dest);
545
546 if (!ret)
547 {
548 DWORD dwAttr;
549
550 dwAttr = SHFindAttrW(dest, FALSE);
551 if (INVALID_FILE_ATTRIBUTES == dwAttr)
552 {
553 /* Source file may be write protected or a system file */
554 dwAttr = GetFileAttributesW(src);
555 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
556 if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
557 ret = MoveFileW(src, dest);
558 }
559 }
560 if (ret)
561 {
562 SHChangeNotify(isdir ? SHCNE_MKDIR : SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
563 SHChangeNotify(isdir ? SHCNE_RMDIR : SHCNE_DELETE, SHCNF_PATHW, src, NULL);
564 return ERROR_SUCCESS;
565 }
566 return GetLastError();
567 }
568
569 static DWORD WINAPI SHOperationProgressRoutine(LARGE_INTEGER TotalFileSize, LARGE_INTEGER TotalBytesTransferred, LARGE_INTEGER StreamSize, LARGE_INTEGER StreamBytesTransferred, DWORD dwStreamNumber, DWORD dwCallbackReason, HANDLE hSourceFile, HANDLE hDestinationFile, LPVOID lpData)
570 {
571 FILE_OPERATION_CONTEXT * Context;
572 LARGE_INTEGER Progress;
573
574 /* get context */
575 Context = (FILE_OPERATION_CONTEXT*)lpData;
576
577 if (TotalBytesTransferred.QuadPart)
578 {
579 Progress.QuadPart = (TotalBytesTransferred.QuadPart * 100) / TotalFileSize.QuadPart;
580 }
581 else
582 {
583 Progress.QuadPart = 1;
584 }
585
586 /* update progress bar */
587 SendMessageW(Context->hDlgCtrl, PBM_SETPOS, (WPARAM)Progress.u.LowPart, 0);
588
589 if (TotalBytesTransferred.QuadPart == TotalFileSize.QuadPart)
590 {
591 /* file was copied */
592 Context->Index++;
593 PostMessageW(Context->hwndDlg, WM_FILE, 0, 0);
594 }
595
596 return PROGRESS_CONTINUE;
597 }
598
599 BOOL
600 QueueFile(
601 FILE_OPERATION_CONTEXT * Context)
602 {
603 FILE_ENTRY * from, *to = NULL;
604 BOOL bRet = FALSE;
605
606 if (Context->Index >= Context->from->dwNumFiles)
607 return FALSE;
608
609 /* get current file */
610 from = &Context->from->feFiles[Context->Index];
611
612 if (Context->op->req->wFunc != FO_DELETE)
613 to = &Context->to->feFiles[Context->Index];
614
615 /* update status */
616 SetDlgItemTextW(Context->hwndDlg, 14001, from->szFullPath);
617
618 if (Context->op->req->wFunc == FO_COPY)
619 {
620 bRet = CopyFileExW(from->szFullPath, to->szFullPath, SHOperationProgressRoutine, (LPVOID)Context, &Context->op->bCancelled, 0);
621 }
622 else if (Context->op->req->wFunc == FO_MOVE)
623 {
624 //bRet = MoveFileWithProgressW(from->szFullPath, to->szFullPath, SHOperationProgressRoutine, (LPVOID)Context, MOVEFILE_COPY_ALLOWED);
625 }
626 else if (Context->op->req->wFunc == FO_DELETE)
627 {
628 bRet = DeleteFile(from->szFullPath);
629 }
630
631 return bRet;
632 }
633
634 static INT_PTR CALLBACK SHOperationDialog(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
635 {
636 FILE_OPERATION_CONTEXT * Context;
637
638 Context = (FILE_OPERATION_CONTEXT*) GetWindowLongPtr(hwndDlg, DWLP_USER);
639
640 switch(uMsg)
641 {
642 case WM_INITDIALOG:
643 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG)lParam);
644
645 /* get context */
646 Context = (FILE_OPERATION_CONTEXT*)lParam;
647
648 /* store progress bar handle */
649 Context->hDlgCtrl = GetDlgItem(hwndDlg, 14000);
650
651 /* store window handle */
652 Context->hwndDlg = hwndDlg;
653
654 /* set progress bar range */
655 (void)SendMessageW(Context->hDlgCtrl, (UINT) PBM_SETRANGE, 0, MAKELPARAM(0, 100));
656
657 /* start file queueing */
658 SetTimer(hwndDlg, TIMER_ID, 1000, NULL);
659 //QueueFile(Context);
660
661 return TRUE;
662
663 case WM_CLOSE:
664 Context->op->bCancelled = TRUE;
665 EndDialog(hwndDlg, Context->op->bCancelled);
666 return TRUE;
667
668 case WM_COMMAND:
669 if (LOWORD(wParam) == 14002)
670 {
671 Context->op->bCancelled = TRUE;
672 EndDialog(hwndDlg, Context->op->bCancelled);
673 return TRUE;
674 }; break;
675
676 case WM_TIMER:
677 if (wParam == TIMER_ID)
678 {
679 QueueFile(Context);
680 KillTimer(hwndDlg, TIMER_ID);
681 }; break;
682
683 case WM_FILE:
684 if (!QueueFile(Context))
685 EndDialog(hwndDlg, Context->op->bCancelled);
686 default:
687 break;
688 }
689 return FALSE;
690 }
691
692 HRESULT
693 SHShowFileOperationDialog(FILE_OPERATION *op, FILE_LIST *flFrom, FILE_LIST *flTo)
694 {
695 HWND hwnd;
696 BOOL bRet;
697 MSG msg;
698 FILE_OPERATION_CONTEXT Context;
699
700 Context.from = flFrom;
701 Context.to = flTo;
702 Context.op = op;
703 Context.Index = 0;
704 Context.op->bCancelled = FALSE;
705
706 hwnd = CreateDialogParam(shell32_hInstance, MAKEINTRESOURCE(IDD_FILE_COPY), NULL, SHOperationDialog, (LPARAM)&Context);
707 if (hwnd == NULL)
708 {
709 ERR("Failed to create dialog\n");
710 return E_FAIL;
711 }
712 ShowWindow(hwnd, SW_SHOWNORMAL);
713
714 while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
715 {
716 if (!IsWindow(hwnd) || !IsDialogMessage(hwnd, &msg))
717 {
718 TranslateMessage(&msg);
719 DispatchMessage(&msg);
720 }
721 }
722
723 return NOERROR;
724 }
725
726
727 /************************************************************************
728 * SHNotifyCopyFile [internal]
729 *
730 * Copies a file. Also triggers a change notify if one exists.
731 *
732 * PARAMS
733 * src [I] path to source file to move
734 * dest [I] path to target file to move to
735 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
736 * a file with this name already exists
737 *
738 * RETURNS
739 * ERROR_SUCCESS if successful
740 */
741 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
742 {
743 BOOL ret;
744 DWORD attribs;
745
746 TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bFailIfExists ? "failIfExists" : "");
747
748 /* Destination file may already exist with read only attribute */
749 attribs = GetFileAttributesW(dest);
750 if (IsAttrib(attribs, FILE_ATTRIBUTE_READONLY))
751 SetFileAttributesW(dest, attribs & ~FILE_ATTRIBUTE_READONLY);
752
753 if (GetFileAttributesW(dest) & FILE_ATTRIBUTE_READONLY)
754 {
755 SetFileAttributesW(dest, attribs & ~FILE_ATTRIBUTE_READONLY);
756 if (GetFileAttributesW(dest) & FILE_ATTRIBUTE_READONLY)
757 {
758 TRACE("[shell32, SHNotifyCopyFileW] STILL SHIT\n");
759 }
760 }
761
762 ret = CopyFileW(src, dest, bFailIfExists);
763 if (ret)
764 {
765 SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
766 return ERROR_SUCCESS;
767 }
768
769 return GetLastError();
770 }
771
772 /*************************************************************************
773 * SHCreateDirectory [SHELL32.165]
774 *
775 * This function creates a file system folder whose fully qualified path is
776 * given by path. If one or more of the intermediate folders do not exist,
777 * they will be created as well.
778 *
779 * PARAMS
780 * hWnd [I]
781 * path [I] path of directory to create
782 *
783 * RETURNS
784 * ERROR_SUCCESS or one of the following values:
785 * ERROR_BAD_PATHNAME if the path is relative
786 * ERROR_FILE_EXISTS when a file with that name exists
787 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
788 * ERROR_INVALID_NAME if the path contains invalid chars
789 * ERROR_ALREADY_EXISTS when the directory already exists
790 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
791 *
792 * NOTES
793 * exported by ordinal
794 * Win9x exports ANSI
795 * WinNT/2000 exports Unicode
796 */
797 int WINAPI SHCreateDirectory(HWND hWnd, LPCWSTR path)
798 {
799 return SHCreateDirectoryExW(hWnd, path, NULL);
800 }
801
802 /*************************************************************************
803 * SHCreateDirectoryExA [SHELL32.@]
804 *
805 * This function creates a file system folder whose fully qualified path is
806 * given by path. If one or more of the intermediate folders do not exist,
807 * they will be created as well.
808 *
809 * PARAMS
810 * hWnd [I]
811 * path [I] path of directory to create
812 * sec [I] security attributes to use or NULL
813 *
814 * RETURNS
815 * ERROR_SUCCESS or one of the following values:
816 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
817 * ERROR_INVALID_NAME if the path contains invalid chars
818 * ERROR_FILE_EXISTS when a file with that name exists
819 * ERROR_ALREADY_EXISTS when the directory already exists
820 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
821 *
822 * FIXME: Not implemented yet;
823 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
824 * if the path is a network path to deal with network drivers which might have a limited
825 * but unknown maximum path length. If not:
826 *
827 * If hWnd is set to a valid window handle, a message box is displayed warning
828 * the user that the files may not be accessible. If the user chooses not to
829 * proceed, the function returns ERROR_CANCELLED.
830 *
831 * If hWnd is set to NULL, no user interface is displayed and the function
832 * returns ERROR_CANCELLED.
833 */
834 int WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
835 {
836 LPWSTR wPath;
837 DWORD retCode;
838
839 TRACE("(%s, %p)\n", debugstr_a(path), sec);
840
841 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
842 if (!retCode)
843 {
844 retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
845 SHELL32_FreeUnicodeBuf(wPath);
846 }
847 return retCode;
848 }
849
850 /*************************************************************************
851 * SHCreateDirectoryExW [SHELL32.@]
852 *
853 * See SHCreateDirectoryExA.
854 */
855 int WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
856 {
857 int ret = ERROR_BAD_PATHNAME;
858 TRACE("(%p, %s, %p)\n", hWnd, debugstr_w(path), sec);
859
860 if (PathIsRelativeW(path))
861 {
862 SetLastError(ret);
863 }
864 else
865 {
866 ret = SHNotifyCreateDirectoryW(path, sec);
867 /* Refuse to work on certain error codes before trying to create directories recursively */
868 if (ret != ERROR_SUCCESS &&
869 ret != ERROR_FILE_EXISTS &&
870 ret != ERROR_ALREADY_EXISTS &&
871 ret != ERROR_FILENAME_EXCED_RANGE)
872 {
873 WCHAR *pEnd, *pSlash, szTemp[MAX_PATH + 1]; /* extra for PathAddBackslash() */
874
875 lstrcpynW(szTemp, path, MAX_PATH);
876 pEnd = PathAddBackslashW(szTemp);
877 pSlash = szTemp + 3;
878
879 while (*pSlash)
880 {
881 while (*pSlash && *pSlash != '\\') pSlash++;
882 if (*pSlash)
883 {
884 *pSlash = 0; /* terminate path at separator */
885
886 ret = SHNotifyCreateDirectoryW(szTemp, pSlash + 1 == pEnd ? sec : NULL);
887 }
888 *pSlash++ = '\\'; /* put the separator back */
889 }
890 }
891
892 if (ret && hWnd && (ERROR_CANCELLED != ret))
893 {
894 /* We failed and should show a dialog box */
895 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
896 ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
897 }
898 }
899
900 return ret;
901 }
902
903 /*************************************************************************
904 * SHFindAttrW [internal]
905 *
906 * Get the Attributes for a file or directory. The difference to GetAttributes()
907 * is that this function will also work for paths containing wildcard characters
908 * in its filename.
909
910 * PARAMS
911 * path [I] path of directory or file to check
912 * fileOnly [I] TRUE if only files should be found
913 *
914 * RETURNS
915 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
916 * the first file or directory found otherwise
917 */
918 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
919 {
920 WIN32_FIND_DATAW wfd;
921 BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
922 DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
923 HANDLE hFind = FindFirstFileW(pName, &wfd);
924
925 TRACE("%s %d\n", debugstr_w(pName), fileOnly);
926 if (INVALID_HANDLE_VALUE != hFind)
927 {
928 do
929 {
930 if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
931 continue;
932 dwAttr = wfd.dwFileAttributes;
933 break;
934 } while (FindNextFileW(hFind, &wfd));
935
936 FindClose(hFind);
937 }
938 return dwAttr;
939 }
940
941 /*************************************************************************
942 *
943 * _ConvertAtoW helper function for SHFileOperationA
944 *
945 * Converts a string or string-list to unicode.
946 */
947 static DWORD _ConvertAtoW(PCSTR strSrc, PCWSTR* pStrDest, BOOL isList)
948 {
949 *pStrDest = NULL;
950
951 // If the input is null, nothing to convert.
952 if (!strSrc)
953 return 0;
954
955 // Measure the total size, depending on if it's a zero-terminated list.
956 int sizeA = 0;
957 if (isList)
958 {
959 PCSTR tmpSrc = strSrc;
960 int size;
961 do
962 {
963 size = lstrlenA(tmpSrc) + 1;
964 sizeA += size;
965 tmpSrc += size;
966 } while (size != 1);
967 }
968 else
969 {
970 sizeA = lstrlenA(strSrc) + 1;
971 }
972
973 // Measure the needed allocation size.
974 int sizeW = MultiByteToWideChar(CP_ACP, 0, strSrc, sizeA, NULL, 0);
975 if (!sizeW)
976 return GetLastError();
977
978 PWSTR strDest = (PWSTR) HeapAlloc(GetProcessHeap(), 0, sizeW * sizeof(WCHAR));
979 if (!strDest)
980 return ERROR_OUTOFMEMORY;
981
982 int err = MultiByteToWideChar(CP_ACP, 0, strSrc, sizeA, strDest, sizeW);
983 if (!err)
984 {
985 HeapFree(GetProcessHeap(), 0, strDest);
986 return GetLastError();
987 }
988
989 *pStrDest = strDest;
990 return 0;
991 }
992
993 /*************************************************************************
994 * SHFileOperationA [SHELL32.@]
995 *
996 * Function to copy, move, delete and create one or more files with optional
997 * user prompts.
998 *
999 * PARAMS
1000 * lpFileOp [I/O] pointer to a structure containing all the necessary information
1001 *
1002 * RETURNS
1003 * Success: ERROR_SUCCESS.
1004 * Failure: ERROR_CANCELLED.
1005 *
1006 * NOTES
1007 * exported by name
1008 */
1009 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
1010 {
1011 int errCode, retCode;
1012 SHFILEOPSTRUCTW nFileOp = { 0 };
1013
1014 // Convert A information to W
1015 nFileOp.hwnd = lpFileOp->hwnd;
1016 nFileOp.wFunc = lpFileOp->wFunc;
1017 nFileOp.fFlags = lpFileOp->fFlags;
1018
1019 errCode = _ConvertAtoW(lpFileOp->pFrom, &nFileOp.pFrom, TRUE);
1020 if (errCode != 0)
1021 goto cleanup;
1022
1023 if (FO_DELETE != (nFileOp.wFunc & FO_MASK))
1024 {
1025 errCode = _ConvertAtoW(lpFileOp->pTo, &nFileOp.pTo, TRUE);
1026 if (errCode != 0)
1027 goto cleanup;
1028 }
1029
1030 if (nFileOp.fFlags & FOF_SIMPLEPROGRESS)
1031 {
1032 errCode = _ConvertAtoW(lpFileOp->lpszProgressTitle, &nFileOp.lpszProgressTitle, FALSE);
1033 if (errCode != 0)
1034 goto cleanup;
1035 }
1036
1037 // Call the actual function
1038 retCode = SHFileOperationW(&nFileOp);
1039
1040 // Cleanup
1041 cleanup:
1042 if (nFileOp.pFrom)
1043 HeapFree(GetProcessHeap(), 0, (PVOID) nFileOp.pFrom);
1044 if (nFileOp.pTo)
1045 HeapFree(GetProcessHeap(), 0, (PVOID) nFileOp.pTo);
1046 if (nFileOp.lpszProgressTitle)
1047 HeapFree(GetProcessHeap(), 0, (PVOID) nFileOp.lpszProgressTitle);
1048
1049 if (errCode != 0)
1050 {
1051 lpFileOp->fAnyOperationsAborted = TRUE;
1052 SetLastError(errCode);
1053
1054 return errCode;
1055 }
1056
1057 // Thankfully, starting with NT4 the name mappings are always unicode, so no need to convert.
1058 lpFileOp->hNameMappings = nFileOp.hNameMappings;
1059 lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
1060 return retCode;
1061 }
1062
1063 static void __inline grow_list(FILE_LIST *list)
1064 {
1065 FILE_ENTRY *newx = (FILE_ENTRY *)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->feFiles,
1066 list->num_alloc * 2 * sizeof(*newx) );
1067 list->feFiles = newx;
1068 list->num_alloc *= 2;
1069 }
1070
1071 /* adds a file to the FILE_ENTRY struct
1072 */
1073 static void add_file_to_entry(FILE_ENTRY *feFile, LPCWSTR szFile)
1074 {
1075 DWORD dwLen = lstrlenW(szFile) + 1;
1076 LPCWSTR ptr;
1077
1078 feFile->szFullPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
1079 lstrcpyW(feFile->szFullPath, szFile);
1080
1081 ptr = StrRChrW(szFile, NULL, '\\');
1082 if (ptr)
1083 {
1084 dwLen = ptr - szFile + 1;
1085 feFile->szDirectory = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
1086 lstrcpynW(feFile->szDirectory, szFile, dwLen);
1087
1088 dwLen = lstrlenW(feFile->szFullPath) - dwLen + 1;
1089 feFile->szFilename = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
1090 lstrcpyW(feFile->szFilename, ptr + 1); /* skip over backslash */
1091 }
1092 feFile->bFromWildcard = FALSE;
1093 }
1094
1095 static LPWSTR wildcard_to_file(LPCWSTR szWildCard, LPCWSTR szFileName)
1096 {
1097 LPCWSTR ptr;
1098 LPWSTR szFullPath;
1099 DWORD dwDirLen, dwFullLen;
1100
1101 ptr = StrRChrW(szWildCard, NULL, '\\');
1102 dwDirLen = ptr - szWildCard + 1;
1103
1104 dwFullLen = dwDirLen + lstrlenW(szFileName) + 1;
1105 szFullPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwFullLen * sizeof(WCHAR));
1106
1107 lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
1108 lstrcatW(szFullPath, szFileName);
1109
1110 return szFullPath;
1111 }
1112
1113 static void parse_wildcard_files(FILE_LIST *flList, LPCWSTR szFile, LPDWORD pdwListIndex)
1114 {
1115 WIN32_FIND_DATAW wfd;
1116 HANDLE hFile = FindFirstFileW(szFile, &wfd);
1117 FILE_ENTRY *file;
1118 LPWSTR szFullPath;
1119 BOOL res;
1120
1121 if (hFile == INVALID_HANDLE_VALUE) return;
1122
1123 for (res = TRUE; res; res = FindNextFileW(hFile, &wfd))
1124 {
1125 if (IsDotDir(wfd.cFileName))
1126 continue;
1127
1128 if (*pdwListIndex >= flList->num_alloc)
1129 grow_list( flList );
1130
1131 szFullPath = wildcard_to_file(szFile, wfd.cFileName);
1132 file = &flList->feFiles[(*pdwListIndex)++];
1133 add_file_to_entry(file, szFullPath);
1134 file->bFromWildcard = TRUE;
1135 file->attributes = wfd.dwFileAttributes;
1136
1137 if (IsAttribDir(file->attributes))
1138 flList->bAnyDirectories = TRUE;
1139
1140 HeapFree(GetProcessHeap(), 0, szFullPath);
1141 }
1142
1143 FindClose(hFile);
1144 }
1145
1146 /* takes the null-separated file list and fills out the FILE_LIST */
1147 static HRESULT parse_file_list(FILE_LIST *flList, LPCWSTR szFiles)
1148 {
1149 LPCWSTR ptr = szFiles;
1150 WCHAR szCurFile[MAX_PATH];
1151 DWORD i = 0;
1152
1153 if (!szFiles)
1154 return ERROR_INVALID_PARAMETER;
1155
1156 flList->bAnyFromWildcard = FALSE;
1157 flList->bAnyDirectories = FALSE;
1158 flList->bAnyDontExist = FALSE;
1159 flList->num_alloc = 32;
1160 flList->dwNumFiles = 0;
1161
1162 /* empty list */
1163 if (!szFiles[0])
1164 return ERROR_ACCESS_DENIED;
1165
1166 flList->feFiles = (FILE_ENTRY *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1167 flList->num_alloc * sizeof(FILE_ENTRY));
1168
1169 while (*ptr)
1170 {
1171 if (i >= flList->num_alloc) grow_list( flList );
1172
1173 /* change relative to absolute path */
1174 if (PathIsRelativeW(ptr))
1175 {
1176 GetCurrentDirectoryW(MAX_PATH, szCurFile);
1177 PathCombineW(szCurFile, szCurFile, ptr);
1178 flList->feFiles[i].bFromRelative = TRUE;
1179 }
1180 else
1181 {
1182 lstrcpyW(szCurFile, ptr);
1183 flList->feFiles[i].bFromRelative = FALSE;
1184 }
1185
1186 /* parse wildcard files if they are in the filename */
1187 if (StrPBrkW(szCurFile, wWildcardChars))
1188 {
1189 parse_wildcard_files(flList, szCurFile, &i);
1190 flList->bAnyFromWildcard = TRUE;
1191 i--;
1192 }
1193 else
1194 {
1195 FILE_ENTRY *file = &flList->feFiles[i];
1196 add_file_to_entry(file, szCurFile);
1197 file->attributes = GetFileAttributesW( file->szFullPath );
1198 file->bExists = (file->attributes != INVALID_FILE_ATTRIBUTES);
1199
1200 if (!file->bExists)
1201 flList->bAnyDontExist = TRUE;
1202
1203 if (IsAttribDir(file->attributes))
1204 flList->bAnyDirectories = TRUE;
1205 }
1206
1207 /* advance to the next string */
1208 ptr += lstrlenW(ptr) + 1;
1209 i++;
1210 }
1211 flList->dwNumFiles = i;
1212
1213 return S_OK;
1214 }
1215
1216 /* free the FILE_LIST */
1217 static void destroy_file_list(FILE_LIST *flList)
1218 {
1219 DWORD i;
1220
1221 if (!flList || !flList->feFiles)
1222 return;
1223
1224 for (i = 0; i < flList->dwNumFiles; i++)
1225 {
1226 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szDirectory);
1227 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFilename);
1228 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFullPath);
1229 }
1230
1231 HeapFree(GetProcessHeap(), 0, flList->feFiles);
1232 }
1233
1234 static void copy_dir_to_dir(FILE_OPERATION *op, const FILE_ENTRY *feFrom, LPCWSTR szDestPath)
1235 {
1236 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1237 SHFILEOPSTRUCTW fileOp;
1238
1239 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1240
1241 if (IsDotDir(feFrom->szFilename))
1242 return;
1243
1244 if (PathFileExistsW(szDestPath))
1245 PathCombineW(szTo, szDestPath, feFrom->szFilename);
1246 else
1247 lstrcpyW(szTo, szDestPath);
1248
1249 if (!(op->req->fFlags & FOF_NOCONFIRMATION) && PathFileExistsW(szTo))
1250 {
1251 if (!SHELL_ConfirmDialogW(op->req->hwnd, ASK_OVERWRITE_FOLDER, feFrom->szFilename, op))
1252 {
1253 /* Vista returns an ERROR_CANCELLED even if user pressed "No" */
1254 if (!op->bManyItems)
1255 op->bCancelled = TRUE;
1256 return;
1257 }
1258 }
1259
1260 szTo[lstrlenW(szTo) + 1] = '\0';
1261 SHNotifyCreateDirectoryW(szTo, NULL);
1262
1263 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1264 szFrom[lstrlenW(szFrom) + 1] = '\0';
1265
1266 fileOp = *op->req;
1267 fileOp.pFrom = szFrom;
1268 fileOp.pTo = szTo;
1269 fileOp.fFlags &= ~FOF_MULTIDESTFILES; /* we know we're copying to one dir */
1270
1271 /* Don't ask the user about overwriting files when he accepted to overwrite the
1272 folder. FIXME: this is not exactly what Windows does - e.g. there would be
1273 an additional confirmation for a nested folder */
1274 fileOp.fFlags |= FOF_NOCONFIRMATION;
1275
1276 SHFileOperationW(&fileOp);
1277 }
1278
1279 static BOOL copy_file_to_file(FILE_OPERATION *op, const WCHAR *szFrom, const WCHAR *szTo)
1280 {
1281 if (!(op->req->fFlags & FOF_NOCONFIRMATION) && PathFileExistsW(szTo))
1282 {
1283 if (!SHELL_ConfirmDialogW(op->req->hwnd, ASK_OVERWRITE_FILE, PathFindFileNameW(szTo), op))
1284 return 0;
1285 }
1286
1287 return SHNotifyCopyFileW(szFrom, szTo, FALSE) == 0;
1288 }
1289
1290 /* copy a file or directory to another directory */
1291 static void copy_to_dir(FILE_OPERATION *op, const FILE_ENTRY *feFrom, const FILE_ENTRY *feTo)
1292 {
1293 if (!PathFileExistsW(feTo->szFullPath))
1294 SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
1295
1296 if (IsAttribFile(feFrom->attributes))
1297 {
1298 WCHAR szDestPath[MAX_PATH];
1299
1300 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1301 copy_file_to_file(op, feFrom->szFullPath, szDestPath);
1302 }
1303 else if (!(op->req->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1304 copy_dir_to_dir(op, feFrom, feTo->szFullPath);
1305 }
1306
1307 static void create_dest_dirs(LPCWSTR szDestDir)
1308 {
1309 WCHAR dir[MAX_PATH];
1310 LPCWSTR ptr = StrChrW(szDestDir, '\\');
1311
1312 /* make sure all directories up to last one are created */
1313 while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
1314 {
1315 lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
1316
1317 if (!PathFileExistsW(dir))
1318 SHNotifyCreateDirectoryW(dir, NULL);
1319 }
1320
1321 /* create last directory */
1322 if (!PathFileExistsW(szDestDir))
1323 SHNotifyCreateDirectoryW(szDestDir, NULL);
1324 }
1325
1326 /* the FO_COPY operation */
1327 static HRESULT copy_files(FILE_OPERATION *op, const FILE_LIST *flFrom, FILE_LIST *flTo)
1328 {
1329 DWORD i;
1330 const FILE_ENTRY *entryToCopy;
1331 const FILE_ENTRY *fileDest = &flTo->feFiles[0];
1332
1333 if (flFrom->bAnyDontExist)
1334 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1335
1336 if (flTo->dwNumFiles == 0)
1337 {
1338 /* If the destination is empty, SHFileOperation should use the current directory */
1339 WCHAR curdir[MAX_PATH+1];
1340
1341 GetCurrentDirectoryW(MAX_PATH, curdir);
1342 curdir[lstrlenW(curdir)+1] = 0;
1343
1344 destroy_file_list(flTo);
1345 ZeroMemory(flTo, sizeof(FILE_LIST));
1346 parse_file_list(flTo, curdir);
1347 fileDest = &flTo->feFiles[0];
1348 }
1349
1350 if (op->req->fFlags & FOF_MULTIDESTFILES)
1351 {
1352 if (flFrom->bAnyFromWildcard)
1353 return ERROR_CANCELLED;
1354
1355 if (flFrom->dwNumFiles != flTo->dwNumFiles)
1356 {
1357 if (flFrom->dwNumFiles != 1 && !IsAttribDir(fileDest->attributes))
1358 return ERROR_CANCELLED;
1359
1360 /* Free all but the first entry. */
1361 for (i = 1; i < flTo->dwNumFiles; i++)
1362 {
1363 HeapFree(GetProcessHeap(), 0, flTo->feFiles[i].szDirectory);
1364 HeapFree(GetProcessHeap(), 0, flTo->feFiles[i].szFilename);
1365 HeapFree(GetProcessHeap(), 0, flTo->feFiles[i].szFullPath);
1366 }
1367
1368 flTo->dwNumFiles = 1;
1369 }
1370 else if (IsAttribDir(fileDest->attributes))
1371 {
1372 for (i = 1; i < flTo->dwNumFiles; i++)
1373 if (!IsAttribDir(flTo->feFiles[i].attributes) ||
1374 !IsAttribDir(flFrom->feFiles[i].attributes))
1375 {
1376 return ERROR_CANCELLED;
1377 }
1378 }
1379 }
1380 else if (flFrom->dwNumFiles != 1)
1381 {
1382 if (flTo->dwNumFiles != 1 && !IsAttribDir(fileDest->attributes))
1383 return ERROR_CANCELLED;
1384
1385 if (PathFileExistsW(fileDest->szFullPath) &&
1386 IsAttribFile(fileDest->attributes))
1387 {
1388 return ERROR_CANCELLED;
1389 }
1390
1391 if (flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
1392 !PathFileExistsW(fileDest->szFullPath))
1393 {
1394 return ERROR_CANCELLED;
1395 }
1396 }
1397
1398 for (i = 0; i < flFrom->dwNumFiles; i++)
1399 {
1400 entryToCopy = &flFrom->feFiles[i];
1401
1402 if ((op->req->fFlags & FOF_MULTIDESTFILES) &&
1403 flTo->dwNumFiles > 1)
1404 {
1405 fileDest = &flTo->feFiles[i];
1406 }
1407
1408 if (IsAttribDir(entryToCopy->attributes) &&
1409 !lstrcmpiW(entryToCopy->szFullPath, fileDest->szDirectory))
1410 {
1411 return ERROR_SUCCESS;
1412 }
1413
1414 create_dest_dirs(fileDest->szDirectory);
1415
1416 if (!lstrcmpiW(entryToCopy->szFullPath, fileDest->szFullPath))
1417 {
1418 if (IsAttribFile(entryToCopy->attributes))
1419 return ERROR_NO_MORE_SEARCH_HANDLES;
1420 else
1421 return ERROR_SUCCESS;
1422 }
1423
1424 if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
1425 IsAttribDir(fileDest->attributes))
1426 {
1427 copy_to_dir(op, entryToCopy, fileDest);
1428 }
1429 else if (IsAttribDir(entryToCopy->attributes))
1430 {
1431 copy_dir_to_dir(op, entryToCopy, fileDest->szFullPath);
1432 }
1433 else
1434 {
1435 if (!copy_file_to_file(op, entryToCopy->szFullPath, fileDest->szFullPath))
1436 {
1437 op->req->fAnyOperationsAborted = TRUE;
1438 return ERROR_CANCELLED;
1439 }
1440 }
1441
1442 /* Vista return code. XP would return e.g. ERROR_FILE_NOT_FOUND, ERROR_ALREADY_EXISTS */
1443 if (op->bCancelled)
1444 return ERROR_CANCELLED;
1445 }
1446
1447 /* Vista return code. On XP if the used pressed "No" for the last item,
1448 * ERROR_ARENA_TRASHED would be returned */
1449 return ERROR_SUCCESS;
1450 }
1451
1452 static BOOL confirm_delete_list(HWND hWnd, DWORD fFlags, BOOL fTrash, const FILE_LIST *flFrom)
1453 {
1454 if (flFrom->dwNumFiles > 1)
1455 {
1456 WCHAR tmp[8];
1457 const WCHAR format[] = {'%','d',0};
1458
1459 wnsprintfW(tmp, sizeof(tmp)/sizeof(tmp[0]), format, flFrom->dwNumFiles);
1460 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_MULTIPLE_ITEM:ASK_DELETE_MULTIPLE_ITEM), tmp, NULL);
1461 }
1462 else
1463 {
1464 const FILE_ENTRY *fileEntry = &flFrom->feFiles[0];
1465
1466 if (IsAttribFile(fileEntry->attributes))
1467 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FILE:ASK_DELETE_FILE), fileEntry->szFullPath, NULL);
1468 else if (!(fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1469 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FOLDER:ASK_DELETE_FOLDER), fileEntry->szFullPath, NULL);
1470 }
1471 return TRUE;
1472 }
1473
1474 /* the FO_DELETE operation */
1475 static HRESULT delete_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom)
1476 {
1477 const FILE_ENTRY *fileEntry;
1478 DWORD i;
1479 BOOL bPathExists;
1480 BOOL bTrash;
1481
1482 if (!flFrom->dwNumFiles)
1483 return ERROR_SUCCESS;
1484
1485 /* Windows also checks only the first item */
1486 bTrash = (lpFileOp->fFlags & FOF_ALLOWUNDO)
1487 && TRASH_CanTrashFile(flFrom->feFiles[0].szFullPath);
1488
1489 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (!bTrash && lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1490 if (!confirm_delete_list(lpFileOp->hwnd, lpFileOp->fFlags, bTrash, flFrom))
1491 {
1492 lpFileOp->fAnyOperationsAborted = TRUE;
1493 return 0;
1494 }
1495
1496 for (i = 0; i < flFrom->dwNumFiles; i++)
1497 {
1498 bPathExists = TRUE;
1499 fileEntry = &flFrom->feFiles[i];
1500
1501 if (!IsAttribFile(fileEntry->attributes) &&
1502 (lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1503 continue;
1504
1505 if (bTrash)
1506 {
1507 BOOL bDelete;
1508 if (TRASH_TrashFile(fileEntry->szFullPath))
1509 {
1510 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, fileEntry->szFullPath, NULL);
1511 continue;
1512 }
1513
1514 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1515 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1516 bDelete = SHELL_ConfirmDialogW(lpFileOp->hwnd, ASK_CANT_TRASH_ITEM, fileEntry->szFullPath, NULL);
1517 else
1518 bDelete = TRUE;
1519
1520 if (!bDelete)
1521 {
1522 lpFileOp->fAnyOperationsAborted = TRUE;
1523 break;
1524 }
1525 }
1526
1527 /* delete the file or directory */
1528 if (IsAttribFile(fileEntry->attributes))
1529 {
1530 bPathExists = DeleteFileW(fileEntry->szFullPath);
1531 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, fileEntry->szFullPath, NULL);
1532 }
1533 else
1534 bPathExists = SHELL_DeleteDirectoryW(lpFileOp->hwnd, fileEntry->szFullPath, FALSE);
1535
1536 if (!bPathExists)
1537 {
1538 DWORD err = GetLastError();
1539
1540 if (ERROR_FILE_NOT_FOUND == err)
1541 {
1542 // This is a windows 2003 server specific value which ahs been removed.
1543 // Later versions of windows return ERROR_FILE_NOT_FOUND.
1544 return 1026;
1545 }
1546 else
1547 {
1548 return err;
1549 }
1550 }
1551 }
1552
1553 return ERROR_SUCCESS;
1554 }
1555
1556 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, const FILE_ENTRY *feFrom, LPCWSTR szDestPath)
1557 {
1558 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1559 SHFILEOPSTRUCTW fileOp;
1560
1561 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1562
1563 if (IsDotDir(feFrom->szFilename))
1564 return;
1565
1566 SHNotifyCreateDirectoryW(szDestPath, NULL);
1567
1568 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1569 szFrom[lstrlenW(szFrom) + 1] = '\0';
1570
1571 lstrcpyW(szTo, szDestPath);
1572 szTo[lstrlenW(szDestPath) + 1] = '\0';
1573
1574 fileOp = *lpFileOp;
1575 fileOp.pFrom = szFrom;
1576 fileOp.pTo = szTo;
1577
1578 SHFileOperationW(&fileOp);
1579 }
1580
1581 /* moves a file or directory to another directory */
1582 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, const FILE_ENTRY *feFrom, const FILE_ENTRY *feTo)
1583 {
1584 WCHAR szDestPath[MAX_PATH];
1585
1586 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1587
1588 if (IsAttribFile(feFrom->attributes))
1589 SHNotifyMoveFileW(feFrom->szFullPath, szDestPath, FALSE);
1590 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1591 move_dir_to_dir(lpFileOp, feFrom, szDestPath);
1592 }
1593
1594 /* the FO_MOVE operation */
1595 static HRESULT move_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom, const FILE_LIST *flTo)
1596 {
1597 DWORD i;
1598 const FILE_ENTRY *entryToMove;
1599 const FILE_ENTRY *fileDest;
1600
1601 if (!flFrom->dwNumFiles || !flTo->dwNumFiles)
1602 return ERROR_CANCELLED;
1603
1604 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1605 flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1606 {
1607 return ERROR_CANCELLED;
1608 }
1609
1610 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1611 !flFrom->bAnyDirectories &&
1612 flFrom->dwNumFiles > flTo->dwNumFiles)
1613 {
1614 return ERROR_CANCELLED;
1615 }
1616
1617 if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1618 return ERROR_CANCELLED;
1619
1620 if ((lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1621 flFrom->dwNumFiles != flTo->dwNumFiles)
1622 {
1623 return ERROR_CANCELLED;
1624 }
1625
1626 fileDest = &flTo->feFiles[0];
1627 for (i = 0; i < flFrom->dwNumFiles; i++)
1628 {
1629 entryToMove = &flFrom->feFiles[i];
1630
1631 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1632 fileDest = &flTo->feFiles[i];
1633
1634 if (!PathFileExistsW(fileDest->szDirectory))
1635 return ERROR_CANCELLED;
1636
1637 if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1638 move_to_dir(lpFileOp, entryToMove, fileDest);
1639 else
1640 SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath, IsAttribDir(entryToMove->attributes));
1641 }
1642
1643 return ERROR_SUCCESS;
1644 }
1645
1646 /* the FO_RENAME files */
1647 static HRESULT rename_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom, const FILE_LIST *flTo)
1648 {
1649 const FILE_ENTRY *feFrom;
1650 const FILE_ENTRY *feTo;
1651
1652 if (flFrom->dwNumFiles != 1)
1653 return ERROR_GEN_FAILURE;
1654
1655 if (flTo->dwNumFiles != 1)
1656 return ERROR_CANCELLED;
1657
1658 feFrom = &flFrom->feFiles[0];
1659 feTo= &flTo->feFiles[0];
1660
1661 /* fail if destination doesn't exist */
1662 if (!feFrom->bExists)
1663 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1664
1665 /* fail if destination already exists */
1666 if (feTo->bExists)
1667 return ERROR_ALREADY_EXISTS;
1668
1669 return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath, IsAttribDir(feFrom->attributes));
1670 }
1671
1672 /* alert the user if an unsupported flag is used */
1673 static void check_flags(FILEOP_FLAGS fFlags)
1674 {
1675 WORD wUnsupportedFlags = FOF_NO_CONNECTED_ELEMENTS |
1676 FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE |
1677 FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1678
1679 if (fFlags & wUnsupportedFlags)
1680 FIXME("Unsupported flags: %04x\n", fFlags);
1681 }
1682
1683 /*************************************************************************
1684 * SHFileOperationW [SHELL32.@]
1685 *
1686 * See SHFileOperationA
1687 */
1688 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1689 {
1690 FILE_OPERATION op;
1691 FILE_LIST flFrom, flTo;
1692 int ret = 0;
1693
1694 if (!lpFileOp)
1695 return ERROR_INVALID_PARAMETER;
1696
1697 check_flags(lpFileOp->fFlags);
1698
1699 ZeroMemory(&flFrom, sizeof(FILE_LIST));
1700 ZeroMemory(&flTo, sizeof(FILE_LIST));
1701
1702 if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1703 return ret;
1704
1705 if (lpFileOp->wFunc != FO_DELETE)
1706 parse_file_list(&flTo, lpFileOp->pTo);
1707
1708 ZeroMemory(&op, sizeof(op));
1709 op.req = lpFileOp;
1710 op.bManyItems = (flFrom.dwNumFiles > 1);
1711
1712 switch (lpFileOp->wFunc)
1713 {
1714 case FO_COPY:
1715 ret = copy_files(&op, &flFrom, &flTo);
1716 break;
1717 case FO_DELETE:
1718 ret = delete_files(lpFileOp, &flFrom);
1719 break;
1720 case FO_MOVE:
1721 ret = move_files(lpFileOp, &flFrom, &flTo);
1722 break;
1723 case FO_RENAME:
1724 ret = rename_files(lpFileOp, &flFrom, &flTo);
1725 break;
1726 default:
1727 ret = ERROR_INVALID_PARAMETER;
1728 break;
1729 }
1730
1731 destroy_file_list(&flFrom);
1732
1733 if (lpFileOp->wFunc != FO_DELETE)
1734 destroy_file_list(&flTo);
1735
1736 if (ret == ERROR_CANCELLED)
1737 lpFileOp->fAnyOperationsAborted = TRUE;
1738
1739 return ret;
1740 }
1741
1742 // Used by SHFreeNameMappings
1743 static int CALLBACK _DestroyCallback(void *p, void *pData)
1744 {
1745 LPSHNAMEMAPPINGW lp = (SHNAMEMAPPINGW *)p;
1746
1747 SHFree(lp->pszOldPath);
1748 SHFree(lp->pszNewPath);
1749
1750 return TRUE;
1751 }
1752
1753 /*************************************************************************
1754 * SHFreeNameMappings [shell32.246]
1755 *
1756 * Free the mapping handle returned by SHFileOperation if FOF_WANTSMAPPINGHANDLE
1757 * was specified.
1758 *
1759 * PARAMS
1760 * hNameMapping [I] handle to the name mappings used during renaming of files
1761 *
1762 * RETURNS
1763 * Nothing
1764 */
1765 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1766 {
1767 if (hNameMapping)
1768 {
1769 DSA_DestroyCallback((HDSA) hNameMapping, _DestroyCallback, NULL);
1770 }
1771 }
1772
1773 /*************************************************************************
1774 * SheGetDirA [SHELL32.@]
1775 *
1776 * drive = 0: returns the current directory path
1777 * drive > 0: returns the current directory path of the specified drive
1778 * drive=1 -> A: drive=2 -> B: ...
1779 * returns 0 if successful
1780 */
1781 EXTERN_C DWORD WINAPI SheGetDirA(DWORD drive, LPSTR buffer)
1782 {
1783 WCHAR org_path[MAX_PATH];
1784 DWORD ret;
1785 char drv_path[3];
1786
1787 /* change current directory to the specified drive */
1788 if (drive) {
1789 strcpy(drv_path, "A:");
1790 drv_path[0] += (char)drive-1;
1791
1792 GetCurrentDirectoryW(MAX_PATH, org_path);
1793
1794 SetCurrentDirectoryA(drv_path);
1795 }
1796
1797 /* query current directory path of the specified drive */
1798 ret = GetCurrentDirectoryA(MAX_PATH, buffer);
1799
1800 /* back to the original drive */
1801 if (drive)
1802 SetCurrentDirectoryW(org_path);
1803
1804 if (!ret)
1805 return GetLastError();
1806
1807 return 0;
1808 }
1809
1810 /*************************************************************************
1811 * SheGetDirW [SHELL32.@]
1812 *
1813 * drive = 0: returns the current directory path
1814 * drive > 0: returns the current directory path of the specified drive
1815 * drive=1 -> A: drive=2 -> B: ...
1816 * returns 0 if successful
1817 */
1818 EXTERN_C DWORD WINAPI SheGetDirW(DWORD drive, LPWSTR buffer)
1819 {
1820 WCHAR org_path[MAX_PATH];
1821 DWORD ret;
1822 char drv_path[3];
1823
1824 /* change current directory to the specified drive */
1825 if (drive)
1826 {
1827 strcpy(drv_path, "A:");
1828 drv_path[0] += (char)drive-1;
1829
1830 GetCurrentDirectoryW(MAX_PATH, org_path);
1831
1832 SetCurrentDirectoryA(drv_path);
1833 }
1834
1835 /* query current directory path of the specified drive */
1836 ret = GetCurrentDirectoryW(MAX_PATH, buffer);
1837
1838 /* back to the original drive */
1839 if (drive)
1840 SetCurrentDirectoryW(org_path);
1841
1842 if (!ret)
1843 return GetLastError();
1844
1845 return 0;
1846 }
1847
1848 /*************************************************************************
1849 * SheChangeDirA [SHELL32.@]
1850 *
1851 * changes the current directory to the specified path
1852 * and returns 0 if successful
1853 */
1854 EXTERN_C DWORD WINAPI SheChangeDirA(LPSTR path)
1855 {
1856 if (SetCurrentDirectoryA(path))
1857 return 0;
1858 else
1859 return GetLastError();
1860 }
1861
1862 /*************************************************************************
1863 * SheChangeDirW [SHELL32.@]
1864 *
1865 * changes the current directory to the specified path
1866 * and returns 0 if successful
1867 */
1868 EXTERN_C DWORD WINAPI SheChangeDirW(LPWSTR path)
1869 {
1870 if (SetCurrentDirectoryW(path))
1871 return 0;
1872 else
1873 return GetLastError();
1874 }
1875
1876 /*************************************************************************
1877 * IsNetDrive [SHELL32.66]
1878 */
1879 EXTERN_C int WINAPI IsNetDrive(int drive)
1880 {
1881 char root[4];
1882 strcpy(root, "A:\\");
1883 root[0] += (char)drive;
1884 return (GetDriveTypeA(root) == DRIVE_REMOTE);
1885 }
1886
1887
1888 /*************************************************************************
1889 * RealDriveType [SHELL32.524]
1890 */
1891 EXTERN_C INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1892 {
1893 char root[] = "A:\\";
1894 root[0] += (char)drive;
1895 return GetDriveTypeA(root);
1896 }
1897
1898 /***********************************************************************
1899 * SHPathPrepareForWriteW (SHELL32.@)
1900 */
1901 EXTERN_C HRESULT WINAPI SHPathPrepareForWriteW(HWND hwnd, IUnknown *modless, LPCWSTR path, DWORD flags)
1902 {
1903 DWORD res;
1904 DWORD err;
1905 LPCWSTR realpath;
1906 int len;
1907 WCHAR* last_slash;
1908 WCHAR* temppath=NULL;
1909
1910 TRACE("%p %p %s 0x%80x\n", hwnd, modless, debugstr_w(path), flags);
1911
1912 if (flags & ~(SHPPFW_DIRCREATE|SHPPFW_ASKDIRCREATE|SHPPFW_IGNOREFILENAME))
1913 FIXME("unimplemented flags 0x%08x\n", flags);
1914
1915 /* cut off filename if necessary */
1916 if (flags & SHPPFW_IGNOREFILENAME)
1917 {
1918 last_slash = StrRChrW(path, NULL, '\\');
1919 if (last_slash == NULL)
1920 len = 1;
1921 else
1922 len = last_slash - path + 1;
1923 temppath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1924 if (!temppath)
1925 return E_OUTOFMEMORY;
1926 StrCpyNW(temppath, path, len);
1927 realpath = temppath;
1928 }
1929 else
1930 {
1931 realpath = path;
1932 }
1933
1934 /* try to create the directory if asked to */
1935 if (flags & (SHPPFW_DIRCREATE|SHPPFW_ASKDIRCREATE))
1936 {
1937 if (flags & SHPPFW_ASKDIRCREATE)
1938 FIXME("treating SHPPFW_ASKDIRCREATE as SHPPFW_DIRCREATE\n");
1939
1940 SHCreateDirectoryExW(0, realpath, NULL);
1941 }
1942
1943 /* check if we can access the directory */
1944 res = GetFileAttributesW(realpath);
1945
1946 HeapFree(GetProcessHeap(), 0, temppath);
1947
1948 if (res == INVALID_FILE_ATTRIBUTES)
1949 {
1950 err = GetLastError();
1951 if (err == ERROR_FILE_NOT_FOUND)
1952 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
1953 return HRESULT_FROM_WIN32(err);
1954 }
1955 else if (res & FILE_ATTRIBUTE_DIRECTORY)
1956 return S_OK;
1957 else
1958 return HRESULT_FROM_WIN32(ERROR_DIRECTORY);
1959 }
1960
1961 /***********************************************************************
1962 * SHPathPrepareForWriteA (SHELL32.@)
1963 */
1964 EXTERN_C HRESULT WINAPI SHPathPrepareForWriteA(HWND hwnd, IUnknown *modless, LPCSTR path, DWORD flags)
1965 {
1966 WCHAR wpath[MAX_PATH];
1967 MultiByteToWideChar( CP_ACP, 0, path, -1, wpath, MAX_PATH);
1968 return SHPathPrepareForWriteW(hwnd, modless, wpath, flags);
1969 }