[SHELL32]
[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 WINE_DEFAULT_DEBUG_CHANNEL(shell);
27
28 #define IsAttrib(x, y) ((INVALID_FILE_ATTRIBUTES != (x)) && ((x) & (y)))
29 #define IsAttribFile(x) (!((x) & FILE_ATTRIBUTE_DIRECTORY))
30 #define IsAttribDir(x) IsAttrib(x, FILE_ATTRIBUTE_DIRECTORY)
31 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
32
33 #define FO_MASK 0xF
34 #define WM_FILE (WM_USER + 1)
35 #define TIMER_ID (100)
36
37 static const WCHAR wWildcardFile[] = {'*',0};
38 static const WCHAR wWildcardChars[] = {'*','?',0};
39
40 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec);
41 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path);
42 static DWORD SHNotifyDeleteFileW(LPCWSTR path);
43 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest, BOOL isdir);
44 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists);
45 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly);
46
47 typedef struct
48 {
49 SHFILEOPSTRUCTW *req;
50 DWORD dwYesToAllMask;
51 BOOL bManyItems;
52 BOOL bCancelled;
53 } FILE_OPERATION;
54
55 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
56
57 typedef struct
58 {
59 DWORD attributes;
60 LPWSTR szDirectory;
61 LPWSTR szFilename;
62 LPWSTR szFullPath;
63 BOOL bFromWildcard;
64 BOOL bFromRelative;
65 BOOL bExists;
66 } FILE_ENTRY;
67
68 typedef struct
69 {
70 FILE_ENTRY *feFiles;
71 DWORD num_alloc;
72 DWORD dwNumFiles;
73 BOOL bAnyFromWildcard;
74 BOOL bAnyDirectories;
75 BOOL bAnyDontExist;
76 } FILE_LIST;
77
78 typedef struct
79 {
80 FILE_LIST * from;
81 FILE_LIST * to;
82 FILE_OPERATION * op;
83 DWORD Index;
84 HWND hDlgCtrl;
85 HWND hwndDlg;
86 }FILE_OPERATION_CONTEXT;
87
88
89 /* Confirm dialogs with an optional "Yes To All" as used in file operations confirmations
90 */
91 static const WCHAR CONFIRM_MSG_PROP[] = {'W','I','N','E','_','C','O','N','F','I','R','M',0};
92
93 struct confirm_msg_info
94 {
95 LPWSTR lpszText;
96 LPWSTR lpszCaption;
97 HICON hIcon;
98 BOOL bYesToAll;
99 };
100
101 /* as some buttons may be hidden and the dialog height may change we may need
102 * to move the controls */
103 static void confirm_msg_move_button(HWND hDlg, INT iId, INT *xPos, INT yOffset, BOOL bShow)
104 {
105 HWND hButton = GetDlgItem(hDlg, iId);
106 RECT r;
107
108 if (bShow)
109 {
110 POINT pt;
111 int width;
112
113 GetWindowRect(hButton, &r);
114 width = r.right - r.left;
115 pt.x = r.left;
116 pt.y = r.top;
117 ScreenToClient(hDlg, &pt);
118 MoveWindow(hButton, *xPos - width, pt.y - yOffset, width, r.bottom - r.top, FALSE);
119 *xPos -= width + 5;
120 }
121 else
122 ShowWindow(hButton, SW_HIDE);
123 }
124
125 /* Note: we paint the text manually and don't use the static control to make
126 * sure the text has the same height as the one computed in WM_INITDIALOG
127 */
128 static INT_PTR ConfirmMsgBox_Paint(HWND hDlg)
129 {
130 PAINTSTRUCT ps;
131 HFONT hOldFont;
132 RECT r;
133 HDC hdc;
134
135 BeginPaint(hDlg, &ps);
136 hdc = ps.hdc;
137
138 GetClientRect(GetDlgItem(hDlg, IDC_YESTOALL_MESSAGE), &r);
139 /* this will remap the rect to dialog coords */
140 MapWindowPoints(GetDlgItem(hDlg, IDC_YESTOALL_MESSAGE), hDlg, (LPPOINT)&r, 2);
141 hOldFont = (HFONT)SelectObject(hdc, (HFONT)SendDlgItemMessageW(hDlg, IDC_YESTOALL_MESSAGE, WM_GETFONT, 0, 0));
142 DrawTextW(hdc, (LPWSTR)GetPropW(hDlg, CONFIRM_MSG_PROP), -1, &r, DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_WORDBREAK);
143 SelectObject(hdc, hOldFont);
144 EndPaint(hDlg, &ps);
145
146 return TRUE;
147 }
148
149 static INT_PTR ConfirmMsgBox_Init(HWND hDlg, LPARAM lParam)
150 {
151 struct confirm_msg_info *info = (struct confirm_msg_info *)lParam;
152 INT xPos, yOffset;
153 int width, height;
154 HFONT hOldFont;
155 HDC hdc;
156 RECT r;
157
158 SetWindowTextW(hDlg, info->lpszCaption);
159 ShowWindow(GetDlgItem(hDlg, IDC_YESTOALL_MESSAGE), SW_HIDE);
160 SetPropW(hDlg, CONFIRM_MSG_PROP, info->lpszText);
161 SendDlgItemMessageW(hDlg, IDC_YESTOALL_ICON, STM_SETICON, (WPARAM)info->hIcon, 0);
162
163 /* compute the text height and resize the dialog */
164 GetClientRect(GetDlgItem(hDlg, IDC_YESTOALL_MESSAGE), &r);
165 hdc = GetDC(hDlg);
166 yOffset = r.bottom;
167 hOldFont = (HFONT)SelectObject(hdc, (HFONT)SendDlgItemMessageW(hDlg, IDC_YESTOALL_MESSAGE, WM_GETFONT, 0, 0));
168 DrawTextW(hdc, info->lpszText, -1, &r, DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_WORDBREAK | DT_CALCRECT);
169 SelectObject(hdc, hOldFont);
170 yOffset -= r.bottom;
171 yOffset = min(yOffset, 35); /* don't make the dialog too small */
172 ReleaseDC(hDlg, hdc);
173
174 GetClientRect(hDlg, &r);
175 xPos = r.right - 7;
176 GetWindowRect(hDlg, &r);
177 width = r.right - r.left;
178 height = r.bottom - r.top - yOffset;
179 MoveWindow(hDlg, (GetSystemMetrics(SM_CXSCREEN) - width)/2,
180 (GetSystemMetrics(SM_CYSCREEN) - height)/2, width, height, FALSE);
181
182 confirm_msg_move_button(hDlg, IDCANCEL, &xPos, yOffset, info->bYesToAll);
183 confirm_msg_move_button(hDlg, IDNO, &xPos, yOffset, TRUE);
184 confirm_msg_move_button(hDlg, IDC_YESTOALL, &xPos, yOffset, info->bYesToAll);
185 confirm_msg_move_button(hDlg, IDYES, &xPos, yOffset, TRUE);
186
187 return TRUE;
188 }
189
190 static INT_PTR CALLBACK ConfirmMsgBoxProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
191 {
192 switch (uMsg)
193 {
194 case WM_INITDIALOG:
195 return ConfirmMsgBox_Init(hDlg, lParam);
196 case WM_PAINT:
197 return ConfirmMsgBox_Paint(hDlg);
198 case WM_COMMAND:
199 EndDialog(hDlg, wParam);
200 break;
201 case WM_CLOSE:
202 EndDialog(hDlg, IDCANCEL);
203 break;
204 }
205 return FALSE;
206 }
207
208 int SHELL_ConfirmMsgBox(HWND hWnd, LPWSTR lpszText, LPWSTR lpszCaption, HICON hIcon, BOOL bYesToAll)
209 {
210 struct confirm_msg_info info;
211
212 info.lpszText = lpszText;
213 info.lpszCaption = lpszCaption;
214 info.hIcon = hIcon;
215 info.bYesToAll = bYesToAll;
216 return DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_YESTOALL_MSGBOX), hWnd, ConfirmMsgBoxProc, (LPARAM)&info);
217 }
218
219 /* confirmation dialogs content */
220 typedef struct
221 {
222 HINSTANCE hIconInstance;
223 UINT icon_resource_id;
224 UINT caption_resource_id, text_resource_id;
225 } SHELL_ConfirmIDstruc;
226
227 static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
228 {
229 ids->hIconInstance = shell32_hInstance;
230 switch (nKindOfDialog)
231 {
232 case ASK_DELETE_FILE:
233 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
234 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
235 ids->text_resource_id = IDS_DELETEITEM_TEXT;
236 return TRUE;
237
238 case ASK_DELETE_FOLDER:
239 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
240 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
241 ids->text_resource_id = IDS_DELETEITEM_TEXT;
242 return TRUE;
243
244 case ASK_DELETE_MULTIPLE_ITEM:
245 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
246 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
247 ids->text_resource_id = IDS_DELETEMULTIPLE_TEXT;
248 return TRUE;
249
250 case ASK_TRASH_FILE:
251 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
252 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
253 ids->text_resource_id = IDS_TRASHITEM_TEXT;
254 return TRUE;
255
256 case ASK_TRASH_FOLDER:
257 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
258 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
259 ids->text_resource_id = IDS_TRASHFOLDER_TEXT;
260 return TRUE;
261
262 case ASK_TRASH_MULTIPLE_ITEM:
263 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
264 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
265 ids->text_resource_id = IDS_TRASHMULTIPLE_TEXT;
266 return TRUE;
267
268 case ASK_CANT_TRASH_ITEM:
269 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
270 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
271 ids->text_resource_id = IDS_CANTTRASH_TEXT;
272 return TRUE;
273
274 case ASK_DELETE_SELECTED:
275 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
276 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
277 ids->text_resource_id = IDS_DELETESELECTED_TEXT;
278 return TRUE;
279
280 case ASK_OVERWRITE_FILE:
281 ids->hIconInstance = NULL;
282 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
283 ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
284 ids->text_resource_id = IDS_OVERWRITEFILE_TEXT;
285 return TRUE;
286
287 case ASK_OVERWRITE_FOLDER:
288 ids->hIconInstance = NULL;
289 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
290 ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
291 ids->text_resource_id = IDS_OVERWRITEFOLDER_TEXT;
292 return TRUE;
293
294 default:
295 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
296 }
297 return FALSE;
298 }
299
300 static BOOL SHELL_ConfirmDialogW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir, FILE_OPERATION *op)
301 {
302 WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
303 SHELL_ConfirmIDstruc ids;
304 DWORD_PTR args[1];
305 HICON hIcon;
306 int ret;
307
308 assert(nKindOfDialog >= 0 && nKindOfDialog < 32);
309 if (op && (op->dwYesToAllMask & (1 << nKindOfDialog)))
310 return TRUE;
311
312 if (!SHELL_ConfirmIDs(nKindOfDialog, &ids)) return FALSE;
313
314 LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption)/sizeof(WCHAR));
315 LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText)/sizeof(WCHAR));
316
317 args[0] = (DWORD_PTR)szDir;
318 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
319 szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)args);
320 hIcon = LoadIconW(ids.hIconInstance, (LPWSTR)MAKEINTRESOURCE(ids.icon_resource_id));
321
322 ret = SHELL_ConfirmMsgBox(hWnd, szBuffer, szCaption, hIcon, op && op->bManyItems);
323 if (op)
324 {
325 if (ret == IDC_YESTOALL)
326 {
327 op->dwYesToAllMask |= (1 << nKindOfDialog);
328 ret = IDYES;
329 }
330 if (ret == IDCANCEL)
331 op->bCancelled = TRUE;
332 if (ret != IDYES)
333 op->req->fAnyOperationsAborted = TRUE;
334 }
335 return ret == IDYES;
336 }
337
338 BOOL SHELL_ConfirmYesNoW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir)
339 {
340 return SHELL_ConfirmDialogW(hWnd, nKindOfDialog, szDir, NULL);
341 }
342
343 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
344 {
345 DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
346
347 if (len < minChars)
348 len = minChars;
349
350 *wPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
351 if (*wPath)
352 {
353 MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
354 return NO_ERROR;
355 }
356 return E_OUTOFMEMORY;
357 }
358
359 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
360 {
361 HeapFree(GetProcessHeap(), 0, wPath);
362 }
363
364 EXTERN_C HRESULT WINAPI SHIsFileAvailableOffline(LPCWSTR path, LPDWORD status)
365 {
366 FIXME("(%s, %p) stub\n", debugstr_w(path), status);
367 return E_FAIL;
368 }
369
370 /**************************************************************************
371 * SHELL_DeleteDirectory() [internal]
372 *
373 * Asks for confirmation when bShowUI is true and deletes the directory and
374 * all its subdirectories and files if necessary.
375 */
376 BOOL SHELL_DeleteDirectoryW(HWND hwnd, LPCWSTR pszDir, BOOL bShowUI)
377 {
378 BOOL ret = TRUE;
379 HANDLE hFind;
380 WIN32_FIND_DATAW wfd;
381 WCHAR szTemp[MAX_PATH];
382
383 /* Make sure the directory exists before eventually prompting the user */
384 PathCombineW(szTemp, pszDir, wWildcardFile);
385 hFind = FindFirstFileW(szTemp, &wfd);
386 if (hFind == INVALID_HANDLE_VALUE)
387 return FALSE;
388
389 if (!bShowUI || (ret = SHELL_ConfirmDialogW(hwnd, ASK_DELETE_FOLDER, pszDir, NULL)))
390 {
391 do
392 {
393 if (IsDotDir(wfd.cFileName))
394 continue;
395 PathCombineW(szTemp, pszDir, wfd.cFileName);
396 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
397 ret = SHELL_DeleteDirectoryW(hwnd, szTemp, FALSE);
398 else
399 ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
400 } while (ret && FindNextFileW(hFind, &wfd));
401 }
402 FindClose(hFind);
403 if (ret)
404 ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
405 return ret;
406 }
407
408 /**************************************************************************
409 * Win32CreateDirectory [SHELL32.93]
410 *
411 * Creates a directory. Also triggers a change notify if one exists.
412 *
413 * PARAMS
414 * path [I] path to directory to create
415 *
416 * RETURNS
417 * TRUE if successful, FALSE otherwise
418 */
419
420 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
421 {
422 TRACE("(%s, %p)\n", debugstr_w(path), sec);
423
424 if (CreateDirectoryW(path, sec))
425 {
426 SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
427 return ERROR_SUCCESS;
428 }
429 return GetLastError();
430 }
431
432 /**********************************************************************/
433
434 EXTERN_C BOOL WINAPI Win32CreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
435 {
436 return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
437 }
438
439 /************************************************************************
440 * Win32RemoveDirectory [SHELL32.94]
441 *
442 * Deletes a directory. Also triggers a change notify if one exists.
443 *
444 * PARAMS
445 * path [I] path to directory to delete
446 *
447 * RETURNS
448 * TRUE if successful, FALSE otherwise
449 */
450 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
451 {
452 BOOL ret;
453 TRACE("(%s)\n", debugstr_w(path));
454
455 ret = RemoveDirectoryW(path);
456 if (!ret)
457 {
458 /* Directory may be write protected */
459 DWORD dwAttr = GetFileAttributesW(path);
460 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
461 if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
462 ret = RemoveDirectoryW(path);
463 }
464 if (ret)
465 {
466 SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
467 return ERROR_SUCCESS;
468 }
469 return GetLastError();
470 }
471
472 /***********************************************************************/
473
474 EXTERN_C BOOL WINAPI Win32RemoveDirectoryW(LPCWSTR path)
475 {
476 return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
477 }
478
479 /************************************************************************
480 * Win32DeleteFile [SHELL32.164]
481 *
482 * Deletes a file. Also triggers a change notify if one exists.
483 *
484 * PARAMS
485 * path [I] path to file to delete
486 *
487 * RETURNS
488 * TRUE if successful, FALSE otherwise
489 */
490 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
491 {
492 BOOL ret;
493
494 TRACE("(%s)\n", debugstr_w(path));
495
496 ret = DeleteFileW(path);
497 if (!ret)
498 {
499 /* File may be write protected or a system file */
500 DWORD dwAttr = GetFileAttributesW(path);
501 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
502 if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
503 ret = DeleteFileW(path);
504 }
505 if (ret)
506 {
507 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
508 return ERROR_SUCCESS;
509 }
510 return GetLastError();
511 }
512
513 /***********************************************************************/
514
515 EXTERN_C DWORD WINAPI Win32DeleteFileW(LPCWSTR path)
516 {
517 return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
518 }
519
520 /************************************************************************
521 * SHNotifyMoveFile [internal]
522 *
523 * Moves a file. Also triggers a change notify if one exists.
524 *
525 * PARAMS
526 * src [I] path to source file to move
527 * dest [I] path to target file to move to
528 *
529 * RETURNS
530 * ERORR_SUCCESS if successful
531 */
532 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest, BOOL isdir)
533 {
534 BOOL ret;
535
536 TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
537
538 ret = MoveFileExW(src, dest, MOVEFILE_REPLACE_EXISTING);
539
540 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
541 if (!ret)
542 ret = MoveFileW(src, dest);
543
544 if (!ret)
545 {
546 DWORD dwAttr;
547
548 dwAttr = SHFindAttrW(dest, FALSE);
549 if (INVALID_FILE_ATTRIBUTES == dwAttr)
550 {
551 /* Source file may be write protected or a system file */
552 dwAttr = GetFileAttributesW(src);
553 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
554 if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
555 ret = MoveFileW(src, dest);
556 }
557 }
558 if (ret)
559 {
560 SHChangeNotify(isdir ? SHCNE_MKDIR : SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
561 SHChangeNotify(isdir ? SHCNE_RMDIR : SHCNE_DELETE, SHCNF_PATHW, src, NULL);
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 {
1477 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, fileEntry->szFullPath, NULL);
1478 continue;
1479 }
1480
1481 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1482 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1483 bDelete = SHELL_ConfirmDialogW(lpFileOp->hwnd, ASK_CANT_TRASH_ITEM, fileEntry->szFullPath, NULL);
1484 else
1485 bDelete = TRUE;
1486
1487 if (!bDelete)
1488 {
1489 lpFileOp->fAnyOperationsAborted = TRUE;
1490 break;
1491 }
1492 }
1493
1494 /* delete the file or directory */
1495 if (IsAttribFile(fileEntry->attributes))
1496 {
1497 bPathExists = DeleteFileW(fileEntry->szFullPath);
1498 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, fileEntry->szFullPath, NULL);
1499 }
1500 else
1501 bPathExists = SHELL_DeleteDirectoryW(lpFileOp->hwnd, fileEntry->szFullPath, FALSE);
1502
1503 if (!bPathExists)
1504 {
1505 DWORD err = GetLastError();
1506
1507 if (ERROR_FILE_NOT_FOUND == err)
1508 {
1509 // This is a windows 2003 server specific value which ahs been removed.
1510 // Later versions of windows return ERROR_FILE_NOT_FOUND.
1511 return 1026;
1512 }
1513 else
1514 {
1515 return err;
1516 }
1517 }
1518 }
1519
1520 return ERROR_SUCCESS;
1521 }
1522
1523 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, const FILE_ENTRY *feFrom, LPCWSTR szDestPath)
1524 {
1525 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1526 SHFILEOPSTRUCTW fileOp;
1527
1528 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1529
1530 if (IsDotDir(feFrom->szFilename))
1531 return;
1532
1533 SHNotifyCreateDirectoryW(szDestPath, NULL);
1534
1535 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1536 szFrom[lstrlenW(szFrom) + 1] = '\0';
1537
1538 lstrcpyW(szTo, szDestPath);
1539 szTo[lstrlenW(szDestPath) + 1] = '\0';
1540
1541 fileOp = *lpFileOp;
1542 fileOp.pFrom = szFrom;
1543 fileOp.pTo = szTo;
1544
1545 SHFileOperationW(&fileOp);
1546 }
1547
1548 /* moves a file or directory to another directory */
1549 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, const FILE_ENTRY *feFrom, const FILE_ENTRY *feTo)
1550 {
1551 WCHAR szDestPath[MAX_PATH];
1552
1553 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1554
1555 if (IsAttribFile(feFrom->attributes))
1556 SHNotifyMoveFileW(feFrom->szFullPath, szDestPath, FALSE);
1557 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1558 move_dir_to_dir(lpFileOp, feFrom, szDestPath);
1559 }
1560
1561 /* the FO_MOVE operation */
1562 static DWORD move_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom, const FILE_LIST *flTo)
1563 {
1564 DWORD i;
1565 INT mismatched = 0;
1566 const FILE_ENTRY *entryToMove;
1567 const FILE_ENTRY *fileDest;
1568
1569 if (!flFrom->dwNumFiles)
1570 return ERROR_SUCCESS;
1571
1572 if (!flTo->dwNumFiles)
1573 return ERROR_FILE_NOT_FOUND;
1574
1575 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1576 flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1577 {
1578 return ERROR_CANCELLED;
1579 }
1580
1581 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1582 !flFrom->bAnyDirectories &&
1583 flFrom->dwNumFiles > flTo->dwNumFiles)
1584 {
1585 return ERROR_CANCELLED;
1586 }
1587
1588 if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1589 return ERROR_CANCELLED;
1590
1591 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1592 mismatched = flFrom->dwNumFiles - flTo->dwNumFiles;
1593
1594 fileDest = &flTo->feFiles[0];
1595 for (i = 0; i < flFrom->dwNumFiles; i++)
1596 {
1597 entryToMove = &flFrom->feFiles[i];
1598
1599 if (!PathFileExistsW(fileDest->szDirectory))
1600 return ERROR_CANCELLED;
1601
1602 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1603 {
1604 if (i >= flTo->dwNumFiles)
1605 break;
1606 fileDest = &flTo->feFiles[i];
1607 if (mismatched && !fileDest->bExists)
1608 {
1609 create_dest_dirs(flTo->feFiles[i].szFullPath);
1610 flTo->feFiles[i].bExists = TRUE;
1611 flTo->feFiles[i].attributes = FILE_ATTRIBUTE_DIRECTORY;
1612 }
1613 }
1614
1615 if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1616 move_to_dir(lpFileOp, entryToMove, fileDest);
1617 else
1618 SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath, IsAttribDir(entryToMove->attributes));
1619 }
1620
1621 if (mismatched > 0)
1622 {
1623 if (flFrom->bAnyDirectories)
1624 return DE_DESTSAMETREE;
1625 else
1626 return DE_SAMEFILE;
1627 }
1628
1629 return ERROR_SUCCESS;
1630 }
1631
1632 /* the FO_RENAME files */
1633 static HRESULT rename_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom, const FILE_LIST *flTo)
1634 {
1635 const FILE_ENTRY *feFrom;
1636 const FILE_ENTRY *feTo;
1637
1638 if (flFrom->dwNumFiles != 1)
1639 return ERROR_GEN_FAILURE;
1640
1641 if (flTo->dwNumFiles != 1)
1642 return ERROR_CANCELLED;
1643
1644 feFrom = &flFrom->feFiles[0];
1645 feTo= &flTo->feFiles[0];
1646
1647 /* fail if destination doesn't exist */
1648 if (!feFrom->bExists)
1649 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1650
1651 /* fail if destination already exists */
1652 if (feTo->bExists)
1653 return ERROR_ALREADY_EXISTS;
1654
1655 return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath, IsAttribDir(feFrom->attributes));
1656 }
1657
1658 /* alert the user if an unsupported flag is used */
1659 static void check_flags(FILEOP_FLAGS fFlags)
1660 {
1661 WORD wUnsupportedFlags = FOF_NO_CONNECTED_ELEMENTS |
1662 FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE |
1663 FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1664
1665 if (fFlags & wUnsupportedFlags)
1666 FIXME("Unsupported flags: %04x\n", fFlags);
1667 }
1668
1669 /*************************************************************************
1670 * SHFileOperationW [SHELL32.@]
1671 *
1672 * See SHFileOperationA
1673 */
1674 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1675 {
1676 FILE_OPERATION op;
1677 FILE_LIST flFrom, flTo;
1678 int ret = 0;
1679
1680 if (!lpFileOp)
1681 return ERROR_INVALID_PARAMETER;
1682
1683 check_flags(lpFileOp->fFlags);
1684
1685 ZeroMemory(&flFrom, sizeof(FILE_LIST));
1686 ZeroMemory(&flTo, sizeof(FILE_LIST));
1687
1688 if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1689 return ret;
1690
1691 if (lpFileOp->wFunc != FO_DELETE)
1692 parse_file_list(&flTo, lpFileOp->pTo);
1693
1694 ZeroMemory(&op, sizeof(op));
1695 op.req = lpFileOp;
1696 op.bManyItems = (flFrom.dwNumFiles > 1);
1697
1698 switch (lpFileOp->wFunc)
1699 {
1700 case FO_COPY:
1701 ret = copy_files(&op, &flFrom, &flTo);
1702 break;
1703 case FO_DELETE:
1704 ret = delete_files(lpFileOp, &flFrom);
1705 break;
1706 case FO_MOVE:
1707 ret = move_files(lpFileOp, &flFrom, &flTo);
1708 break;
1709 case FO_RENAME:
1710 ret = rename_files(lpFileOp, &flFrom, &flTo);
1711 break;
1712 default:
1713 ret = ERROR_INVALID_PARAMETER;
1714 break;
1715 }
1716
1717 destroy_file_list(&flFrom);
1718
1719 if (lpFileOp->wFunc != FO_DELETE)
1720 destroy_file_list(&flTo);
1721
1722 if (ret == ERROR_CANCELLED)
1723 lpFileOp->fAnyOperationsAborted = TRUE;
1724
1725 return ret;
1726 }
1727
1728 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1729
1730 /*************************************************************************
1731 * SHFreeNameMappings [shell32.246]
1732 *
1733 * Free the mapping handle returned by SHFileOperation if FOF_WANTSMAPPINGHANDLE
1734 * was specified.
1735 *
1736 * PARAMS
1737 * hNameMapping [I] handle to the name mappings used during renaming of files
1738 *
1739 * RETURNS
1740 * Nothing
1741 */
1742 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1743 {
1744 if (hNameMapping)
1745 {
1746 int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1747
1748 for (; i>= 0; i--)
1749 {
1750 LPSHNAMEMAPPINGW lp = (SHNAMEMAPPINGW *)DSA_GetItemPtr((HDSA)hNameMapping, i);
1751
1752 SHFree(lp->pszOldPath);
1753 SHFree(lp->pszNewPath);
1754 }
1755 DSA_Destroy((HDSA)hNameMapping);
1756 }
1757 }
1758
1759 /*************************************************************************
1760 * SheGetDirA [SHELL32.@]
1761 *
1762 * drive = 0: returns the current directory path
1763 * drive > 0: returns the current directory path of the specified drive
1764 * drive=1 -> A: drive=2 -> B: ...
1765 * returns 0 if successful
1766 */
1767 EXTERN_C DWORD WINAPI SheGetDirA(DWORD drive, LPSTR buffer)
1768 {
1769 WCHAR org_path[MAX_PATH];
1770 DWORD ret;
1771 char drv_path[3];
1772
1773 /* change current directory to the specified drive */
1774 if (drive) {
1775 strcpy(drv_path, "A:");
1776 drv_path[0] += (char)drive-1;
1777
1778 GetCurrentDirectoryW(MAX_PATH, org_path);
1779
1780 SetCurrentDirectoryA(drv_path);
1781 }
1782
1783 /* query current directory path of the specified drive */
1784 ret = GetCurrentDirectoryA(MAX_PATH, buffer);
1785
1786 /* back to the original drive */
1787 if (drive)
1788 SetCurrentDirectoryW(org_path);
1789
1790 if (!ret)
1791 return GetLastError();
1792
1793 return 0;
1794 }
1795
1796 /*************************************************************************
1797 * SheGetDirW [SHELL32.@]
1798 *
1799 * drive = 0: returns the current directory path
1800 * drive > 0: returns the current directory path of the specified drive
1801 * drive=1 -> A: drive=2 -> B: ...
1802 * returns 0 if successful
1803 */
1804 EXTERN_C DWORD WINAPI SheGetDirW(DWORD drive, LPWSTR buffer)
1805 {
1806 WCHAR org_path[MAX_PATH];
1807 DWORD ret;
1808 char drv_path[3];
1809
1810 /* change current directory to the specified drive */
1811 if (drive)
1812 {
1813 strcpy(drv_path, "A:");
1814 drv_path[0] += (char)drive-1;
1815
1816 GetCurrentDirectoryW(MAX_PATH, org_path);
1817
1818 SetCurrentDirectoryA(drv_path);
1819 }
1820
1821 /* query current directory path of the specified drive */
1822 ret = GetCurrentDirectoryW(MAX_PATH, buffer);
1823
1824 /* back to the original drive */
1825 if (drive)
1826 SetCurrentDirectoryW(org_path);
1827
1828 if (!ret)
1829 return GetLastError();
1830
1831 return 0;
1832 }
1833
1834 /*************************************************************************
1835 * SheChangeDirA [SHELL32.@]
1836 *
1837 * changes the current directory to the specified path
1838 * and returns 0 if successful
1839 */
1840 EXTERN_C DWORD WINAPI SheChangeDirA(LPSTR path)
1841 {
1842 if (SetCurrentDirectoryA(path))
1843 return 0;
1844 else
1845 return GetLastError();
1846 }
1847
1848 /*************************************************************************
1849 * SheChangeDirW [SHELL32.@]
1850 *
1851 * changes the current directory to the specified path
1852 * and returns 0 if successful
1853 */
1854 EXTERN_C DWORD WINAPI SheChangeDirW(LPWSTR path)
1855 {
1856 if (SetCurrentDirectoryW(path))
1857 return 0;
1858 else
1859 return GetLastError();
1860 }
1861
1862 /*************************************************************************
1863 * IsNetDrive [SHELL32.66]
1864 */
1865 EXTERN_C int WINAPI IsNetDrive(int drive)
1866 {
1867 char root[4];
1868 strcpy(root, "A:\\");
1869 root[0] += (char)drive;
1870 return (GetDriveTypeA(root) == DRIVE_REMOTE);
1871 }
1872
1873
1874 /*************************************************************************
1875 * RealDriveType [SHELL32.524]
1876 */
1877 EXTERN_C INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1878 {
1879 char root[] = "A:\\";
1880 root[0] += (char)drive;
1881 return GetDriveTypeA(root);
1882 }
1883
1884 /***********************************************************************
1885 * SHPathPrepareForWriteW (SHELL32.@)
1886 */
1887 EXTERN_C HRESULT WINAPI SHPathPrepareForWriteW(HWND hwnd, IUnknown *modless, LPCWSTR path, DWORD flags)
1888 {
1889 DWORD res;
1890 DWORD err;
1891 LPCWSTR realpath;
1892 int len;
1893 WCHAR* last_slash;
1894 WCHAR* temppath=NULL;
1895
1896 TRACE("%p %p %s 0x%80x\n", hwnd, modless, debugstr_w(path), flags);
1897
1898 if (flags & ~(SHPPFW_DIRCREATE|SHPPFW_ASKDIRCREATE|SHPPFW_IGNOREFILENAME))
1899 FIXME("unimplemented flags 0x%08x\n", flags);
1900
1901 /* cut off filename if necessary */
1902 if (flags & SHPPFW_IGNOREFILENAME)
1903 {
1904 last_slash = StrRChrW(path, NULL, '\\');
1905 if (last_slash == NULL)
1906 len = 1;
1907 else
1908 len = last_slash - path + 1;
1909 temppath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1910 if (!temppath)
1911 return E_OUTOFMEMORY;
1912 StrCpyNW(temppath, path, len);
1913 realpath = temppath;
1914 }
1915 else
1916 {
1917 realpath = path;
1918 }
1919
1920 /* try to create the directory if asked to */
1921 if (flags & (SHPPFW_DIRCREATE|SHPPFW_ASKDIRCREATE))
1922 {
1923 if (flags & SHPPFW_ASKDIRCREATE)
1924 FIXME("treating SHPPFW_ASKDIRCREATE as SHPPFW_DIRCREATE\n");
1925
1926 SHCreateDirectoryExW(0, realpath, NULL);
1927 }
1928
1929 /* check if we can access the directory */
1930 res = GetFileAttributesW(realpath);
1931
1932 HeapFree(GetProcessHeap(), 0, temppath);
1933
1934 if (res == INVALID_FILE_ATTRIBUTES)
1935 {
1936 err = GetLastError();
1937 if (err == ERROR_FILE_NOT_FOUND)
1938 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
1939 return HRESULT_FROM_WIN32(err);
1940 }
1941 else if (res & FILE_ATTRIBUTE_DIRECTORY)
1942 return S_OK;
1943 else
1944 return HRESULT_FROM_WIN32(ERROR_DIRECTORY);
1945 }
1946
1947 /***********************************************************************
1948 * SHPathPrepareForWriteA (SHELL32.@)
1949 */
1950 EXTERN_C HRESULT WINAPI SHPathPrepareForWriteA(HWND hwnd, IUnknown *modless, LPCSTR path, DWORD flags)
1951 {
1952 WCHAR wpath[MAX_PATH];
1953 MultiByteToWideChar( CP_ACP, 0, path, -1, wpath, MAX_PATH);
1954 return SHPathPrepareForWriteW(hwnd, modless, wpath, flags);
1955 }