8fe73ffb51b3a977afcf7e40e1d2f30379bebbef
[reactos.git] / reactos / 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 * SHNameTranslate HelperFunction for SHFileOperationA
944 *
945 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
946 * is NULL, only the necessary size of the string is determined and returned,
947 * otherwise the ASCII strings are copied into it and the buffer is increased
948 * to point to the location after the final 0 termination char.
949 */
950 static DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
951 {
952 DWORD size = 0, aSize = 0;
953 LPCSTR aString = (LPCSTR)*pWToFrom;
954
955 if (aString)
956 {
957 do
958 {
959 size = lstrlenA(aString) + 1;
960 aSize += size;
961 aString += size;
962 } while ((size != 1) && more);
963
964 /* The two sizes might be different in the case of multibyte chars */
965 size = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, 0);
966 if (*wString) /* only in the second loop */
967 {
968 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
969 *pWToFrom = *wString;
970 *wString += size;
971 }
972 }
973 return size;
974 }
975 /*************************************************************************
976 * SHFileOperationA [SHELL32.@]
977 *
978 * Function to copy, move, delete and create one or more files with optional
979 * user prompts.
980 *
981 * PARAMS
982 * lpFileOp [I/O] pointer to a structure containing all the necessary information
983 *
984 * RETURNS
985 * Success: ERROR_SUCCESS.
986 * Failure: ERROR_CANCELLED.
987 *
988 * NOTES
989 * exported by name
990 */
991 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
992 {
993 SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
994 int retCode = 0;
995 DWORD size;
996 LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
997 wString = NULL; /* we change this in SHNameTranslate */
998
999 TRACE("\n");
1000 if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
1001 nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
1002 if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
1003 nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
1004 while (1) /* every loop calculate size, second translate also, if we have storage for this */
1005 {
1006 size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
1007 size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
1008 size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
1009
1010 if (ForFree)
1011 {
1012 retCode = SHFileOperationW(&nFileOp);
1013 HeapFree(GetProcessHeap(), 0, ForFree); /* we cannot use wString, it was changed */
1014 break;
1015 }
1016 else
1017 {
1018 wString = ForFree = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1019 if (ForFree) continue;
1020 retCode = ERROR_OUTOFMEMORY;
1021 nFileOp.fAnyOperationsAborted = TRUE;
1022 SetLastError(retCode);
1023 return retCode;
1024 }
1025 }
1026
1027 lpFileOp->hNameMappings = nFileOp.hNameMappings;
1028 lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
1029 return retCode;
1030 }
1031
1032 static void __inline grow_list(FILE_LIST *list)
1033 {
1034 FILE_ENTRY *newx = (FILE_ENTRY *)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->feFiles,
1035 list->num_alloc * 2 * sizeof(*newx) );
1036 list->feFiles = newx;
1037 list->num_alloc *= 2;
1038 }
1039
1040 /* adds a file to the FILE_ENTRY struct
1041 */
1042 static void add_file_to_entry(FILE_ENTRY *feFile, LPCWSTR szFile)
1043 {
1044 DWORD dwLen = lstrlenW(szFile) + 1;
1045 LPCWSTR ptr;
1046
1047 feFile->szFullPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
1048 lstrcpyW(feFile->szFullPath, szFile);
1049
1050 ptr = StrRChrW(szFile, NULL, '\\');
1051 if (ptr)
1052 {
1053 dwLen = ptr - szFile + 1;
1054 feFile->szDirectory = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
1055 lstrcpynW(feFile->szDirectory, szFile, dwLen);
1056
1057 dwLen = lstrlenW(feFile->szFullPath) - dwLen + 1;
1058 feFile->szFilename = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
1059 lstrcpyW(feFile->szFilename, ptr + 1); /* skip over backslash */
1060 }
1061 feFile->bFromWildcard = FALSE;
1062 }
1063
1064 static LPWSTR wildcard_to_file(LPCWSTR szWildCard, LPCWSTR szFileName)
1065 {
1066 LPCWSTR ptr;
1067 LPWSTR szFullPath;
1068 DWORD dwDirLen, dwFullLen;
1069
1070 ptr = StrRChrW(szWildCard, NULL, '\\');
1071 dwDirLen = ptr - szWildCard + 1;
1072
1073 dwFullLen = dwDirLen + lstrlenW(szFileName) + 1;
1074 szFullPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwFullLen * sizeof(WCHAR));
1075
1076 lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
1077 lstrcatW(szFullPath, szFileName);
1078
1079 return szFullPath;
1080 }
1081
1082 static void parse_wildcard_files(FILE_LIST *flList, LPCWSTR szFile, LPDWORD pdwListIndex)
1083 {
1084 WIN32_FIND_DATAW wfd;
1085 HANDLE hFile = FindFirstFileW(szFile, &wfd);
1086 FILE_ENTRY *file;
1087 LPWSTR szFullPath;
1088 BOOL res;
1089
1090 if (hFile == INVALID_HANDLE_VALUE) return;
1091
1092 for (res = TRUE; res; res = FindNextFileW(hFile, &wfd))
1093 {
1094 if (IsDotDir(wfd.cFileName))
1095 continue;
1096
1097 if (*pdwListIndex >= flList->num_alloc)
1098 grow_list( flList );
1099
1100 szFullPath = wildcard_to_file(szFile, wfd.cFileName);
1101 file = &flList->feFiles[(*pdwListIndex)++];
1102 add_file_to_entry(file, szFullPath);
1103 file->bFromWildcard = TRUE;
1104 file->attributes = wfd.dwFileAttributes;
1105
1106 if (IsAttribDir(file->attributes))
1107 flList->bAnyDirectories = TRUE;
1108
1109 HeapFree(GetProcessHeap(), 0, szFullPath);
1110 }
1111
1112 FindClose(hFile);
1113 }
1114
1115 /* takes the null-separated file list and fills out the FILE_LIST */
1116 static HRESULT parse_file_list(FILE_LIST *flList, LPCWSTR szFiles)
1117 {
1118 LPCWSTR ptr = szFiles;
1119 WCHAR szCurFile[MAX_PATH];
1120 DWORD i = 0;
1121
1122 if (!szFiles)
1123 return ERROR_INVALID_PARAMETER;
1124
1125 flList->bAnyFromWildcard = FALSE;
1126 flList->bAnyDirectories = FALSE;
1127 flList->bAnyDontExist = FALSE;
1128 flList->num_alloc = 32;
1129 flList->dwNumFiles = 0;
1130
1131 /* empty list */
1132 if (!szFiles[0])
1133 return ERROR_ACCESS_DENIED;
1134
1135 flList->feFiles = (FILE_ENTRY *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1136 flList->num_alloc * sizeof(FILE_ENTRY));
1137
1138 while (*ptr)
1139 {
1140 if (i >= flList->num_alloc) grow_list( flList );
1141
1142 /* change relative to absolute path */
1143 if (PathIsRelativeW(ptr))
1144 {
1145 GetCurrentDirectoryW(MAX_PATH, szCurFile);
1146 PathCombineW(szCurFile, szCurFile, ptr);
1147 flList->feFiles[i].bFromRelative = TRUE;
1148 }
1149 else
1150 {
1151 lstrcpyW(szCurFile, ptr);
1152 flList->feFiles[i].bFromRelative = FALSE;
1153 }
1154
1155 /* parse wildcard files if they are in the filename */
1156 if (StrPBrkW(szCurFile, wWildcardChars))
1157 {
1158 parse_wildcard_files(flList, szCurFile, &i);
1159 flList->bAnyFromWildcard = TRUE;
1160 i--;
1161 }
1162 else
1163 {
1164 FILE_ENTRY *file = &flList->feFiles[i];
1165 add_file_to_entry(file, szCurFile);
1166 file->attributes = GetFileAttributesW( file->szFullPath );
1167 file->bExists = (file->attributes != INVALID_FILE_ATTRIBUTES);
1168
1169 if (!file->bExists)
1170 flList->bAnyDontExist = TRUE;
1171
1172 if (IsAttribDir(file->attributes))
1173 flList->bAnyDirectories = TRUE;
1174 }
1175
1176 /* advance to the next string */
1177 ptr += lstrlenW(ptr) + 1;
1178 i++;
1179 }
1180 flList->dwNumFiles = i;
1181
1182 return S_OK;
1183 }
1184
1185 /* free the FILE_LIST */
1186 static void destroy_file_list(FILE_LIST *flList)
1187 {
1188 DWORD i;
1189
1190 if (!flList || !flList->feFiles)
1191 return;
1192
1193 for (i = 0; i < flList->dwNumFiles; i++)
1194 {
1195 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szDirectory);
1196 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFilename);
1197 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFullPath);
1198 }
1199
1200 HeapFree(GetProcessHeap(), 0, flList->feFiles);
1201 }
1202
1203 static void copy_dir_to_dir(FILE_OPERATION *op, const FILE_ENTRY *feFrom, LPCWSTR szDestPath)
1204 {
1205 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1206 SHFILEOPSTRUCTW fileOp;
1207
1208 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1209
1210 if (IsDotDir(feFrom->szFilename))
1211 return;
1212
1213 if (PathFileExistsW(szDestPath))
1214 PathCombineW(szTo, szDestPath, feFrom->szFilename);
1215 else
1216 lstrcpyW(szTo, szDestPath);
1217
1218 if (!(op->req->fFlags & FOF_NOCONFIRMATION) && PathFileExistsW(szTo))
1219 {
1220 if (!SHELL_ConfirmDialogW(op->req->hwnd, ASK_OVERWRITE_FOLDER, feFrom->szFilename, op))
1221 {
1222 /* Vista returns an ERROR_CANCELLED even if user pressed "No" */
1223 if (!op->bManyItems)
1224 op->bCancelled = TRUE;
1225 return;
1226 }
1227 }
1228
1229 szTo[lstrlenW(szTo) + 1] = '\0';
1230 SHNotifyCreateDirectoryW(szTo, NULL);
1231
1232 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1233 szFrom[lstrlenW(szFrom) + 1] = '\0';
1234
1235 fileOp = *op->req;
1236 fileOp.pFrom = szFrom;
1237 fileOp.pTo = szTo;
1238 fileOp.fFlags &= ~FOF_MULTIDESTFILES; /* we know we're copying to one dir */
1239
1240 /* Don't ask the user about overwriting files when he accepted to overwrite the
1241 folder. FIXME: this is not exactly what Windows does - e.g. there would be
1242 an additional confirmation for a nested folder */
1243 fileOp.fFlags |= FOF_NOCONFIRMATION;
1244
1245 SHFileOperationW(&fileOp);
1246 }
1247
1248 static BOOL copy_file_to_file(FILE_OPERATION *op, const WCHAR *szFrom, const WCHAR *szTo)
1249 {
1250 if (!(op->req->fFlags & FOF_NOCONFIRMATION) && PathFileExistsW(szTo))
1251 {
1252 if (!SHELL_ConfirmDialogW(op->req->hwnd, ASK_OVERWRITE_FILE, PathFindFileNameW(szTo), op))
1253 return 0;
1254 }
1255
1256 return SHNotifyCopyFileW(szFrom, szTo, FALSE) == 0;
1257 }
1258
1259 /* copy a file or directory to another directory */
1260 static void copy_to_dir(FILE_OPERATION *op, const FILE_ENTRY *feFrom, const FILE_ENTRY *feTo)
1261 {
1262 if (!PathFileExistsW(feTo->szFullPath))
1263 SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
1264
1265 if (IsAttribFile(feFrom->attributes))
1266 {
1267 WCHAR szDestPath[MAX_PATH];
1268
1269 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1270 copy_file_to_file(op, feFrom->szFullPath, szDestPath);
1271 }
1272 else if (!(op->req->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1273 copy_dir_to_dir(op, feFrom, feTo->szFullPath);
1274 }
1275
1276 static void create_dest_dirs(LPCWSTR szDestDir)
1277 {
1278 WCHAR dir[MAX_PATH];
1279 LPCWSTR ptr = StrChrW(szDestDir, '\\');
1280
1281 /* make sure all directories up to last one are created */
1282 while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
1283 {
1284 lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
1285
1286 if (!PathFileExistsW(dir))
1287 SHNotifyCreateDirectoryW(dir, NULL);
1288 }
1289
1290 /* create last directory */
1291 if (!PathFileExistsW(szDestDir))
1292 SHNotifyCreateDirectoryW(szDestDir, NULL);
1293 }
1294
1295 /* the FO_COPY operation */
1296 static HRESULT copy_files(FILE_OPERATION *op, const FILE_LIST *flFrom, FILE_LIST *flTo)
1297 {
1298 DWORD i;
1299 const FILE_ENTRY *entryToCopy;
1300 const FILE_ENTRY *fileDest = &flTo->feFiles[0];
1301
1302 if (flFrom->bAnyDontExist)
1303 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1304
1305 if (flTo->dwNumFiles == 0)
1306 {
1307 /* If the destination is empty, SHFileOperation should use the current directory */
1308 WCHAR curdir[MAX_PATH+1];
1309
1310 GetCurrentDirectoryW(MAX_PATH, curdir);
1311 curdir[lstrlenW(curdir)+1] = 0;
1312
1313 destroy_file_list(flTo);
1314 ZeroMemory(flTo, sizeof(FILE_LIST));
1315 parse_file_list(flTo, curdir);
1316 fileDest = &flTo->feFiles[0];
1317 }
1318
1319 if (op->req->fFlags & FOF_MULTIDESTFILES)
1320 {
1321 if (flFrom->bAnyFromWildcard)
1322 return ERROR_CANCELLED;
1323
1324 if (flFrom->dwNumFiles != flTo->dwNumFiles)
1325 {
1326 if (flFrom->dwNumFiles != 1 && !IsAttribDir(fileDest->attributes))
1327 return ERROR_CANCELLED;
1328
1329 /* Free all but the first entry. */
1330 for (i = 1; i < flTo->dwNumFiles; i++)
1331 {
1332 HeapFree(GetProcessHeap(), 0, flTo->feFiles[i].szDirectory);
1333 HeapFree(GetProcessHeap(), 0, flTo->feFiles[i].szFilename);
1334 HeapFree(GetProcessHeap(), 0, flTo->feFiles[i].szFullPath);
1335 }
1336
1337 flTo->dwNumFiles = 1;
1338 }
1339 else if (IsAttribDir(fileDest->attributes))
1340 {
1341 for (i = 1; i < flTo->dwNumFiles; i++)
1342 if (!IsAttribDir(flTo->feFiles[i].attributes) ||
1343 !IsAttribDir(flFrom->feFiles[i].attributes))
1344 {
1345 return ERROR_CANCELLED;
1346 }
1347 }
1348 }
1349 else if (flFrom->dwNumFiles != 1)
1350 {
1351 if (flTo->dwNumFiles != 1 && !IsAttribDir(fileDest->attributes))
1352 return ERROR_CANCELLED;
1353
1354 if (PathFileExistsW(fileDest->szFullPath) &&
1355 IsAttribFile(fileDest->attributes))
1356 {
1357 return ERROR_CANCELLED;
1358 }
1359
1360 if (flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
1361 !PathFileExistsW(fileDest->szFullPath))
1362 {
1363 return ERROR_CANCELLED;
1364 }
1365 }
1366
1367 for (i = 0; i < flFrom->dwNumFiles; i++)
1368 {
1369 entryToCopy = &flFrom->feFiles[i];
1370
1371 if ((op->req->fFlags & FOF_MULTIDESTFILES) &&
1372 flTo->dwNumFiles > 1)
1373 {
1374 fileDest = &flTo->feFiles[i];
1375 }
1376
1377 if (IsAttribDir(entryToCopy->attributes) &&
1378 !lstrcmpiW(entryToCopy->szFullPath, fileDest->szDirectory))
1379 {
1380 return ERROR_SUCCESS;
1381 }
1382
1383 create_dest_dirs(fileDest->szDirectory);
1384
1385 if (!lstrcmpiW(entryToCopy->szFullPath, fileDest->szFullPath))
1386 {
1387 if (IsAttribFile(entryToCopy->attributes))
1388 return ERROR_NO_MORE_SEARCH_HANDLES;
1389 else
1390 return ERROR_SUCCESS;
1391 }
1392
1393 if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
1394 IsAttribDir(fileDest->attributes))
1395 {
1396 copy_to_dir(op, entryToCopy, fileDest);
1397 }
1398 else if (IsAttribDir(entryToCopy->attributes))
1399 {
1400 copy_dir_to_dir(op, entryToCopy, fileDest->szFullPath);
1401 }
1402 else
1403 {
1404 if (!copy_file_to_file(op, entryToCopy->szFullPath, fileDest->szFullPath))
1405 {
1406 op->req->fAnyOperationsAborted = TRUE;
1407 return ERROR_CANCELLED;
1408 }
1409 }
1410
1411 /* Vista return code. XP would return e.g. ERROR_FILE_NOT_FOUND, ERROR_ALREADY_EXISTS */
1412 if (op->bCancelled)
1413 return ERROR_CANCELLED;
1414 }
1415
1416 /* Vista return code. On XP if the used pressed "No" for the last item,
1417 * ERROR_ARENA_TRASHED would be returned */
1418 return ERROR_SUCCESS;
1419 }
1420
1421 static BOOL confirm_delete_list(HWND hWnd, DWORD fFlags, BOOL fTrash, const FILE_LIST *flFrom)
1422 {
1423 if (flFrom->dwNumFiles > 1)
1424 {
1425 WCHAR tmp[8];
1426 const WCHAR format[] = {'%','d',0};
1427
1428 wnsprintfW(tmp, sizeof(tmp)/sizeof(tmp[0]), format, flFrom->dwNumFiles);
1429 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_MULTIPLE_ITEM:ASK_DELETE_MULTIPLE_ITEM), tmp, NULL);
1430 }
1431 else
1432 {
1433 const FILE_ENTRY *fileEntry = &flFrom->feFiles[0];
1434
1435 if (IsAttribFile(fileEntry->attributes))
1436 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FILE:ASK_DELETE_FILE), fileEntry->szFullPath, NULL);
1437 else if (!(fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1438 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FOLDER:ASK_DELETE_FOLDER), fileEntry->szFullPath, NULL);
1439 }
1440 return TRUE;
1441 }
1442
1443 /* the FO_DELETE operation */
1444 static HRESULT delete_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom)
1445 {
1446 const FILE_ENTRY *fileEntry;
1447 DWORD i;
1448 BOOL bPathExists;
1449 BOOL bTrash;
1450
1451 if (!flFrom->dwNumFiles)
1452 return ERROR_SUCCESS;
1453
1454 /* Windows also checks only the first item */
1455 bTrash = (lpFileOp->fFlags & FOF_ALLOWUNDO)
1456 && TRASH_CanTrashFile(flFrom->feFiles[0].szFullPath);
1457
1458 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (!bTrash && lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1459 if (!confirm_delete_list(lpFileOp->hwnd, lpFileOp->fFlags, bTrash, flFrom))
1460 {
1461 lpFileOp->fAnyOperationsAborted = TRUE;
1462 return 0;
1463 }
1464
1465 for (i = 0; i < flFrom->dwNumFiles; i++)
1466 {
1467 bPathExists = TRUE;
1468 fileEntry = &flFrom->feFiles[i];
1469
1470 if (!IsAttribFile(fileEntry->attributes) &&
1471 (lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1472 continue;
1473
1474 if (bTrash)
1475 {
1476 BOOL bDelete;
1477 if (TRASH_TrashFile(fileEntry->szFullPath))
1478 {
1479 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, fileEntry->szFullPath, NULL);
1480 continue;
1481 }
1482
1483 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1484 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1485 bDelete = SHELL_ConfirmDialogW(lpFileOp->hwnd, ASK_CANT_TRASH_ITEM, fileEntry->szFullPath, NULL);
1486 else
1487 bDelete = TRUE;
1488
1489 if (!bDelete)
1490 {
1491 lpFileOp->fAnyOperationsAborted = TRUE;
1492 break;
1493 }
1494 }
1495
1496 /* delete the file or directory */
1497 if (IsAttribFile(fileEntry->attributes))
1498 {
1499 bPathExists = DeleteFileW(fileEntry->szFullPath);
1500 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, fileEntry->szFullPath, NULL);
1501 }
1502 else
1503 bPathExists = SHELL_DeleteDirectoryW(lpFileOp->hwnd, fileEntry->szFullPath, FALSE);
1504
1505 if (!bPathExists)
1506 {
1507 DWORD err = GetLastError();
1508
1509 if (ERROR_FILE_NOT_FOUND == err)
1510 {
1511 // This is a windows 2003 server specific value which ahs been removed.
1512 // Later versions of windows return ERROR_FILE_NOT_FOUND.
1513 return 1026;
1514 }
1515 else
1516 {
1517 return err;
1518 }
1519 }
1520 }
1521
1522 return ERROR_SUCCESS;
1523 }
1524
1525 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, const FILE_ENTRY *feFrom, LPCWSTR szDestPath)
1526 {
1527 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1528 SHFILEOPSTRUCTW fileOp;
1529
1530 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1531
1532 if (IsDotDir(feFrom->szFilename))
1533 return;
1534
1535 SHNotifyCreateDirectoryW(szDestPath, NULL);
1536
1537 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1538 szFrom[lstrlenW(szFrom) + 1] = '\0';
1539
1540 lstrcpyW(szTo, szDestPath);
1541 szTo[lstrlenW(szDestPath) + 1] = '\0';
1542
1543 fileOp = *lpFileOp;
1544 fileOp.pFrom = szFrom;
1545 fileOp.pTo = szTo;
1546
1547 SHFileOperationW(&fileOp);
1548 }
1549
1550 /* moves a file or directory to another directory */
1551 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, const FILE_ENTRY *feFrom, const FILE_ENTRY *feTo)
1552 {
1553 WCHAR szDestPath[MAX_PATH];
1554
1555 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1556
1557 if (IsAttribFile(feFrom->attributes))
1558 SHNotifyMoveFileW(feFrom->szFullPath, szDestPath, FALSE);
1559 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1560 move_dir_to_dir(lpFileOp, feFrom, szDestPath);
1561 }
1562
1563 /* the FO_MOVE operation */
1564 static HRESULT move_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom, const FILE_LIST *flTo)
1565 {
1566 DWORD i;
1567 const FILE_ENTRY *entryToMove;
1568 const FILE_ENTRY *fileDest;
1569
1570 if (!flFrom->dwNumFiles || !flTo->dwNumFiles)
1571 return ERROR_CANCELLED;
1572
1573 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1574 flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1575 {
1576 return ERROR_CANCELLED;
1577 }
1578
1579 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1580 !flFrom->bAnyDirectories &&
1581 flFrom->dwNumFiles > flTo->dwNumFiles)
1582 {
1583 return ERROR_CANCELLED;
1584 }
1585
1586 if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1587 return ERROR_CANCELLED;
1588
1589 if ((lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1590 flFrom->dwNumFiles != flTo->dwNumFiles)
1591 {
1592 return ERROR_CANCELLED;
1593 }
1594
1595 fileDest = &flTo->feFiles[0];
1596 for (i = 0; i < flFrom->dwNumFiles; i++)
1597 {
1598 entryToMove = &flFrom->feFiles[i];
1599
1600 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1601 fileDest = &flTo->feFiles[i];
1602
1603 if (!PathFileExistsW(fileDest->szDirectory))
1604 return ERROR_CANCELLED;
1605
1606 if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1607 move_to_dir(lpFileOp, entryToMove, fileDest);
1608 else
1609 SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath, IsAttribDir(entryToMove->attributes));
1610 }
1611
1612 return ERROR_SUCCESS;
1613 }
1614
1615 /* the FO_RENAME files */
1616 static HRESULT rename_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom, const FILE_LIST *flTo)
1617 {
1618 const FILE_ENTRY *feFrom;
1619 const FILE_ENTRY *feTo;
1620
1621 if (flFrom->dwNumFiles != 1)
1622 return ERROR_GEN_FAILURE;
1623
1624 if (flTo->dwNumFiles != 1)
1625 return ERROR_CANCELLED;
1626
1627 feFrom = &flFrom->feFiles[0];
1628 feTo= &flTo->feFiles[0];
1629
1630 /* fail if destination doesn't exist */
1631 if (!feFrom->bExists)
1632 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1633
1634 /* fail if destination already exists */
1635 if (feTo->bExists)
1636 return ERROR_ALREADY_EXISTS;
1637
1638 return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath, IsAttribDir(feFrom->attributes));
1639 }
1640
1641 /* alert the user if an unsupported flag is used */
1642 static void check_flags(FILEOP_FLAGS fFlags)
1643 {
1644 WORD wUnsupportedFlags = FOF_NO_CONNECTED_ELEMENTS |
1645 FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE |
1646 FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1647
1648 if (fFlags & wUnsupportedFlags)
1649 FIXME("Unsupported flags: %04x\n", fFlags);
1650 }
1651
1652 /*************************************************************************
1653 * SHFileOperationW [SHELL32.@]
1654 *
1655 * See SHFileOperationA
1656 */
1657 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1658 {
1659 FILE_OPERATION op;
1660 FILE_LIST flFrom, flTo;
1661 int ret = 0;
1662
1663 if (!lpFileOp)
1664 return ERROR_INVALID_PARAMETER;
1665
1666 check_flags(lpFileOp->fFlags);
1667
1668 ZeroMemory(&flFrom, sizeof(FILE_LIST));
1669 ZeroMemory(&flTo, sizeof(FILE_LIST));
1670
1671 if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1672 return ret;
1673
1674 if (lpFileOp->wFunc != FO_DELETE)
1675 parse_file_list(&flTo, lpFileOp->pTo);
1676
1677 ZeroMemory(&op, sizeof(op));
1678 op.req = lpFileOp;
1679 op.bManyItems = (flFrom.dwNumFiles > 1);
1680
1681 switch (lpFileOp->wFunc)
1682 {
1683 case FO_COPY:
1684 ret = copy_files(&op, &flFrom, &flTo);
1685 break;
1686 case FO_DELETE:
1687 ret = delete_files(lpFileOp, &flFrom);
1688 break;
1689 case FO_MOVE:
1690 ret = move_files(lpFileOp, &flFrom, &flTo);
1691 break;
1692 case FO_RENAME:
1693 ret = rename_files(lpFileOp, &flFrom, &flTo);
1694 break;
1695 default:
1696 ret = ERROR_INVALID_PARAMETER;
1697 break;
1698 }
1699
1700 destroy_file_list(&flFrom);
1701
1702 if (lpFileOp->wFunc != FO_DELETE)
1703 destroy_file_list(&flTo);
1704
1705 if (ret == ERROR_CANCELLED)
1706 lpFileOp->fAnyOperationsAborted = TRUE;
1707
1708 return ret;
1709 }
1710
1711 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1712
1713 /*************************************************************************
1714 * SHFreeNameMappings [shell32.246]
1715 *
1716 * Free the mapping handle returned by SHFileOperation if FOF_WANTSMAPPINGHANDLE
1717 * was specified.
1718 *
1719 * PARAMS
1720 * hNameMapping [I] handle to the name mappings used during renaming of files
1721 *
1722 * RETURNS
1723 * Nothing
1724 */
1725 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1726 {
1727 if (hNameMapping)
1728 {
1729 int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1730
1731 for (; i>= 0; i--)
1732 {
1733 LPSHNAMEMAPPINGW lp = (SHNAMEMAPPINGW *)DSA_GetItemPtr((HDSA)hNameMapping, i);
1734
1735 SHFree(lp->pszOldPath);
1736 SHFree(lp->pszNewPath);
1737 }
1738 DSA_Destroy((HDSA)hNameMapping);
1739 }
1740 }
1741
1742 /*************************************************************************
1743 * SheGetDirA [SHELL32.@]
1744 *
1745 * drive = 0: returns the current directory path
1746 * drive > 0: returns the current directory path of the specified drive
1747 * drive=1 -> A: drive=2 -> B: ...
1748 * returns 0 if successful
1749 */
1750 EXTERN_C DWORD WINAPI SheGetDirA(DWORD drive, LPSTR buffer)
1751 {
1752 WCHAR org_path[MAX_PATH];
1753 DWORD ret;
1754 char drv_path[3];
1755
1756 /* change current directory to the specified drive */
1757 if (drive) {
1758 strcpy(drv_path, "A:");
1759 drv_path[0] += (char)drive-1;
1760
1761 GetCurrentDirectoryW(MAX_PATH, org_path);
1762
1763 SetCurrentDirectoryA(drv_path);
1764 }
1765
1766 /* query current directory path of the specified drive */
1767 ret = GetCurrentDirectoryA(MAX_PATH, buffer);
1768
1769 /* back to the original drive */
1770 if (drive)
1771 SetCurrentDirectoryW(org_path);
1772
1773 if (!ret)
1774 return GetLastError();
1775
1776 return 0;
1777 }
1778
1779 /*************************************************************************
1780 * SheGetDirW [SHELL32.@]
1781 *
1782 * drive = 0: returns the current directory path
1783 * drive > 0: returns the current directory path of the specified drive
1784 * drive=1 -> A: drive=2 -> B: ...
1785 * returns 0 if successful
1786 */
1787 EXTERN_C DWORD WINAPI SheGetDirW(DWORD drive, LPWSTR buffer)
1788 {
1789 WCHAR org_path[MAX_PATH];
1790 DWORD ret;
1791 char drv_path[3];
1792
1793 /* change current directory to the specified drive */
1794 if (drive)
1795 {
1796 strcpy(drv_path, "A:");
1797 drv_path[0] += (char)drive-1;
1798
1799 GetCurrentDirectoryW(MAX_PATH, org_path);
1800
1801 SetCurrentDirectoryA(drv_path);
1802 }
1803
1804 /* query current directory path of the specified drive */
1805 ret = GetCurrentDirectoryW(MAX_PATH, buffer);
1806
1807 /* back to the original drive */
1808 if (drive)
1809 SetCurrentDirectoryW(org_path);
1810
1811 if (!ret)
1812 return GetLastError();
1813
1814 return 0;
1815 }
1816
1817 /*************************************************************************
1818 * SheChangeDirA [SHELL32.@]
1819 *
1820 * changes the current directory to the specified path
1821 * and returns 0 if successful
1822 */
1823 EXTERN_C DWORD WINAPI SheChangeDirA(LPSTR path)
1824 {
1825 if (SetCurrentDirectoryA(path))
1826 return 0;
1827 else
1828 return GetLastError();
1829 }
1830
1831 /*************************************************************************
1832 * SheChangeDirW [SHELL32.@]
1833 *
1834 * changes the current directory to the specified path
1835 * and returns 0 if successful
1836 */
1837 EXTERN_C DWORD WINAPI SheChangeDirW(LPWSTR path)
1838 {
1839 if (SetCurrentDirectoryW(path))
1840 return 0;
1841 else
1842 return GetLastError();
1843 }
1844
1845 /*************************************************************************
1846 * IsNetDrive [SHELL32.66]
1847 */
1848 EXTERN_C int WINAPI IsNetDrive(int drive)
1849 {
1850 char root[4];
1851 strcpy(root, "A:\\");
1852 root[0] += (char)drive;
1853 return (GetDriveTypeA(root) == DRIVE_REMOTE);
1854 }
1855
1856
1857 /*************************************************************************
1858 * RealDriveType [SHELL32.524]
1859 */
1860 EXTERN_C INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1861 {
1862 char root[] = "A:\\";
1863 root[0] += (char)drive;
1864 return GetDriveTypeA(root);
1865 }
1866
1867 /***********************************************************************
1868 * SHPathPrepareForWriteW (SHELL32.@)
1869 */
1870 EXTERN_C HRESULT WINAPI SHPathPrepareForWriteW(HWND hwnd, IUnknown *modless, LPCWSTR path, DWORD flags)
1871 {
1872 DWORD res;
1873 DWORD err;
1874 LPCWSTR realpath;
1875 int len;
1876 WCHAR* last_slash;
1877 WCHAR* temppath=NULL;
1878
1879 TRACE("%p %p %s 0x%80x\n", hwnd, modless, debugstr_w(path), flags);
1880
1881 if (flags & ~(SHPPFW_DIRCREATE|SHPPFW_ASKDIRCREATE|SHPPFW_IGNOREFILENAME))
1882 FIXME("unimplemented flags 0x%08x\n", flags);
1883
1884 /* cut off filename if necessary */
1885 if (flags & SHPPFW_IGNOREFILENAME)
1886 {
1887 last_slash = StrRChrW(path, NULL, '\\');
1888 if (last_slash == NULL)
1889 len = 1;
1890 else
1891 len = last_slash - path + 1;
1892 temppath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1893 if (!temppath)
1894 return E_OUTOFMEMORY;
1895 StrCpyNW(temppath, path, len);
1896 realpath = temppath;
1897 }
1898 else
1899 {
1900 realpath = path;
1901 }
1902
1903 /* try to create the directory if asked to */
1904 if (flags & (SHPPFW_DIRCREATE|SHPPFW_ASKDIRCREATE))
1905 {
1906 if (flags & SHPPFW_ASKDIRCREATE)
1907 FIXME("treating SHPPFW_ASKDIRCREATE as SHPPFW_DIRCREATE\n");
1908
1909 SHCreateDirectoryExW(0, realpath, NULL);
1910 }
1911
1912 /* check if we can access the directory */
1913 res = GetFileAttributesW(realpath);
1914
1915 HeapFree(GetProcessHeap(), 0, temppath);
1916
1917 if (res == INVALID_FILE_ATTRIBUTES)
1918 {
1919 err = GetLastError();
1920 if (err == ERROR_FILE_NOT_FOUND)
1921 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
1922 return HRESULT_FROM_WIN32(err);
1923 }
1924 else if (res & FILE_ATTRIBUTE_DIRECTORY)
1925 return S_OK;
1926 else
1927 return HRESULT_FROM_WIN32(ERROR_DIRECTORY);
1928 }
1929
1930 /***********************************************************************
1931 * SHPathPrepareForWriteA (SHELL32.@)
1932 */
1933 EXTERN_C HRESULT WINAPI SHPathPrepareForWriteA(HWND hwnd, IUnknown *modless, LPCSTR path, DWORD flags)
1934 {
1935 WCHAR wpath[MAX_PATH];
1936 MultiByteToWideChar( CP_ACP, 0, path, -1, wpath, MAX_PATH);
1937 return SHPathPrepareForWriteW(hwnd, modless, wpath, flags);
1938 }