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