shlfileop.c 41.7 KB
Newer Older
1 2
/*
 * SHFileOperation
3 4
 *
 * Copyright 2000 Juergen Schmied
5
 * Copyright 2002 Andriy Palamarchuk
6 7
 * Copyright 2002 Dietrich Teickner (from Odin)
 * Copyright 2002 Rolf Kalbermatter
8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 */
23 24 25 26

#include "config.h"
#include "wine/port.h"

27
#include <stdarg.h>
28
#include <string.h>
29
#include <ctype.h>
30

31 32
#include "windef.h"
#include "winbase.h"
33 34
#include "winreg.h"
#include "shellapi.h"
35 36
#include "wingdi.h"
#include "winuser.h"
37
#include "shlobj.h"
38
#include "shresdef.h"
39 40
#define NO_SHLWAPI_STREAM
#include "shlwapi.h"
41
#include "shell32_main.h"
42
#include "undocshell.h"
43
#include "wine/unicode.h"
44
#include "wine/debug.h"
45

46
WINE_DEFAULT_DEBUG_CHANNEL(shell);
47

48 49 50 51 52
#define IsAttribFile(x) (!(x == -1) && !(x & FILE_ATTRIBUTE_DIRECTORY))
#define IsAttribDir(x)  (!(x == -1) && (x & FILE_ATTRIBUTE_DIRECTORY))

#define IsDotDir(x)     ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))

53 54
#define FO_MASK         0xF

55 56 57 58 59
CHAR aWildcardFile[] = {'*','.','*',0};
WCHAR wWildcardFile[] = {'*','.','*',0};
WCHAR wWildcardChars[] = {'*','?',0};
WCHAR wBackslash[] = {'\\',0};

60 61 62 63 64 65 66 67
static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec);
static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec);
static DWORD SHNotifyRemoveDirectoryA(LPCSTR path);
static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path);
static DWORD SHNotifyDeleteFileA(LPCSTR path);
static DWORD SHNotifyDeleteFileW(LPCWSTR path);
static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest, BOOL bRenameIfExists);
static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bRenameIfExists);
68 69

typedef struct
70
{
71
	UINT caption_resource_id, text_resource_id;
72
} SHELL_ConfirmIDstruc;
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
{
	switch (nKindOfDialog) {
	  case ASK_DELETE_FILE:
	    ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
	    ids->text_resource_id  = IDS_DELETEITEM_TEXT;
	    return TRUE;
	  case ASK_DELETE_FOLDER:
	    ids->caption_resource_id  = IDS_DELETEFOLDER_CAPTION;
	    ids->text_resource_id  = IDS_DELETEITEM_TEXT;
	    return TRUE;
	  case ASK_DELETE_MULTIPLE_ITEM:
	    ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
	    ids->text_resource_id  = IDS_DELETEMULTIPLE_TEXT;
	    return TRUE;
	  case ASK_OVERWRITE_FILE:
	    ids->caption_resource_id  = IDS_OVERWRITEFILE_CAPTION;
	    ids->text_resource_id  = IDS_OVERWRITEFILE_TEXT;
	    return TRUE;
	  default:
	    FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
95
	}
96 97 98 99 100 101 102 103 104 105
	return FALSE;
}

BOOL SHELL_ConfirmDialog(int nKindOfDialog, LPCSTR szDir)
{
	CHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
	SHELL_ConfirmIDstruc ids;

	if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
	  return FALSE;
106

107 108
	LoadStringA(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption));
	LoadStringA(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText));
109

110
	FormatMessageA(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
111
	               szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
112

113
	return (IDOK == MessageBoxA(GetActiveWindow(), szBuffer, szCaption, MB_OKCANCEL | MB_ICONEXCLAMATION));
114 115
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
BOOL SHELL_ConfirmDialogW(int nKindOfDialog, LPCWSTR szDir)
{
	WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
	SHELL_ConfirmIDstruc ids;

	if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
	  return FALSE;

	LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption));
	LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText));

	FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
	               szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);

	return (IDOK == MessageBoxW(GetActiveWindow(), szBuffer, szCaption, MB_OKCANCEL | MB_ICONEXCLAMATION));
}

133
/**************************************************************************
134
 * SHELL_DeleteDirectoryA()  [internal]
135 136 137
 *
 * like rm -r
 */
138
BOOL SHELL_DeleteDirectoryA(LPCSTR pszDir, BOOL bShowUI)
139
{
140 141
	BOOL    ret = TRUE;
	HANDLE  hFind;
142
	WIN32_FIND_DATAA wfd;
143
	char    szTemp[MAX_PATH];
144

145 146 147 148
	/* Make sure the directory exists before eventually prompting the user */
	PathCombineA(szTemp, pszDir, aWildcardFile);
	hFind = FindFirstFileA(szTemp, &wfd);
	if (hFind == INVALID_HANDLE_VALUE)
149
	  return FALSE;
150

151
	if (!bShowUI || SHELL_ConfirmDialog(ASK_DELETE_FOLDER, pszDir))
152 153 154
	{
	  do
	  {
155 156 157 158 159 160 161 162 163
	    LPSTR lp = wfd.cAlternateFileName;
	    if (!lp[0])
	      lp = wfd.cFileName;
	    if (IsDotDir(lp))
	      continue;
	    PathCombineA(szTemp, pszDir, lp);
	    if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
	      ret = SHELL_DeleteDirectoryA(szTemp, FALSE);
	    else
164
	      ret = (SHNotifyDeleteFileA(szTemp) == ERROR_SUCCESS);
165
	  } while (ret && FindNextFileA(hFind, &wfd));
166
	}
167 168
	FindClose(hFind);
	if (ret)
169
	  ret = (SHNotifyRemoveDirectoryA(pszDir) == ERROR_SUCCESS);
170 171
	return ret;
}
172

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
BOOL SHELL_DeleteDirectoryW(LPCWSTR pszDir, BOOL bShowUI)
{
	BOOL    ret = TRUE;
	HANDLE  hFind;
	WIN32_FIND_DATAW wfd;
	WCHAR   szTemp[MAX_PATH];

	/* Make sure the directory exists before eventually prompting the user */
	PathCombineW(szTemp, pszDir, wWildcardFile);
	hFind = FindFirstFileW(szTemp, &wfd);
	if (hFind == INVALID_HANDLE_VALUE)
	  return FALSE;

	if (!bShowUI || SHELL_ConfirmDialogW(ASK_DELETE_FOLDER, pszDir))
	{
	  do
	  {
	    LPWSTR lp = wfd.cAlternateFileName;
	    if (!lp[0])
	      lp = wfd.cFileName;
	    if (IsDotDir(lp))
	      continue;
	    PathCombineW(szTemp, pszDir, lp);
	    if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
	      ret = SHELL_DeleteDirectoryW(szTemp, FALSE);
	    else
199
	      ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
200 201 202 203
	  } while (ret && FindNextFileW(hFind, &wfd));
	}
	FindClose(hFind);
	if (ret)
204
	  ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
205 206 207
	return ret;
}

208
/**************************************************************************
209
 *  SHELL_DeleteFileA()      [internal]
210 211 212
 */
BOOL SHELL_DeleteFileA(LPCSTR pszFile, BOOL bShowUI)
{
213
	if (bShowUI && !SHELL_ConfirmDialog(ASK_DELETE_FILE, pszFile))
214
	  return FALSE;
215

216
	return (SHNotifyDeleteFileA(pszFile) == ERROR_SUCCESS);
217 218
}

219 220 221 222 223
BOOL SHELL_DeleteFileW(LPCWSTR pszFile, BOOL bShowUI)
{
	if (bShowUI && !SHELL_ConfirmDialogW(ASK_DELETE_FILE, pszFile))
	  return FALSE;

224
	return (SHNotifyDeleteFileW(pszFile) == ERROR_SUCCESS);
225 226 227 228
}

/**************************************************************************
 * Win32CreateDirectory      [SHELL32.93]
229
 *
230 231 232 233 234 235 236 237 238 239 240
 * Creates a directory. Also triggers a change notify if one exists.
 *
 * PARAMS
 *  path       [I]   path to directory to create
 *
 * RETURNS
 *  TRUE if successful, FALSE otherwise
 *
 * NOTES:
 *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
 *  This is Unicode on NT/2000
241
 */
242
static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
243
{
244
	WCHAR wPath[MAX_PATH];
245 246
	TRACE("(%s, %p)\n", debugstr_a(path), sec);

247 248
	MultiByteToWideChar(CP_ACP, 0, path, -1, wPath, MAX_PATH);
	return SHNotifyCreateDirectoryW(wPath, sec);
249 250
}

251 252
/**********************************************************************/

253
static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
254 255 256
{
	TRACE("(%s, %p)\n", debugstr_w(path), sec);

257 258 259 260 261 262
	if (StrPBrkW(path, wWildcardChars))
	{
	  /* FIXME: This test is currently necessary since our CreateDirectory
	     implementation does create directories with wildcard characters
	     without objection!! Once this is fixed, this here can go away. */
	  SetLastError(ERROR_INVALID_NAME);
263 264 265
#ifdef W98_FO_FUNCTION /* W98 */
	  return ERROR_FILE_NOT_FOUND;
#else
266
	  return ERROR_INVALID_NAME;
267
#endif
268 269 270
	}

	if (CreateDirectoryW(path, sec))
271 272
	{
	  SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
273
	  return ERROR_SUCCESS;
274
	}
275
	return GetLastError();
276 277
}

278 279
/**********************************************************************/

280
BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
281 282
{
	if (SHELL_OsIsUnicode())
283 284
	  return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
	return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
285 286
}

287 288 289 290 291 292 293 294 295 296 297 298 299 300
/************************************************************************
 * Win32RemoveDirectory      [SHELL32.94]
 *
 * Deletes a directory. Also triggers a change notify if one exists.
 *
 * PARAMS
 *  path       [I]   path to directory to delete
 *
 * RETURNS
 *  TRUE if successful, FALSE otherwise
 *
 * NOTES:
 *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
 *  This is Unicode on NT/2000
301
 */
302

303
static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
304
{
305
	WCHAR wPath[MAX_PATH];
306 307
	TRACE("(%s)\n", debugstr_a(path));

308 309
	MultiByteToWideChar(CP_ACP, 0, path, -1, wPath, MAX_PATH);
	return SHNotifyRemoveDirectoryW(wPath);
310 311
}

312 313
/***********************************************************************/

314
static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
315
{
316 317 318 319 320
	BOOL ret;
	TRACE("(%s)\n", debugstr_w(path));

	ret = RemoveDirectoryW(path);
	if (!ret)
321
	{
322 323 324 325 326
	  /* Directory may be write protected */
	  DWORD dwAttr = GetFileAttributesW(path);
	  if (dwAttr != -1 && dwAttr & FILE_ATTRIBUTE_READONLY)
	    if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
	      ret = RemoveDirectoryW(path);
327
	}
328
	if (ret)
329
	{
330
	  SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
331 332 333
	  return ERROR_SUCCESS;
	}
	return GetLastError();
334 335
}

336 337
/***********************************************************************/

338 339 340
BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
{
	if (SHELL_OsIsUnicode())
341 342
	  return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
	return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
343 344
}

345
/************************************************************************
346
 * Win32DeleteFile           [SHELL32.164]
347
 *
348
 * Deletes a file. Also triggers a change notify if one exists.
349
 *
350 351 352 353 354 355 356
 * PARAMS
 *  path       [I]   path to file to delete
 *
 * RETURNS
 *  TRUE if successful, FALSE otherwise
 *
 * NOTES:
357 358
 *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
 *  This is Unicode on NT/2000
359
 */
360

361
static DWORD SHNotifyDeleteFileA(LPCSTR path)
362
{
363
	WCHAR wPath[MAX_PATH];
364 365
	TRACE("(%s)\n", debugstr_a(path));

366 367
	MultiByteToWideChar(CP_ACP, 0, path, -1, wPath, MAX_PATH);
	return SHNotifyDeleteFileW(wPath);
368 369
}

370 371
/***********************************************************************/

372
static DWORD SHNotifyDeleteFileW(LPCWSTR path)
373
{
374
	BOOL ret;
375

376 377 378 379 380 381 382 383 384 385 386 387
	TRACE("(%s)\n", debugstr_w(path));

	ret = DeleteFileW(path);
	if (!ret)
	{
	  /* File may be write protected or a system file */
	  DWORD dwAttr = GetFileAttributesW(path);
	  if ((dwAttr != -1) && (dwAttr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
	    if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
	      ret = DeleteFileW(path);
	}
	if (ret)
388
	{
389
	  SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
390 391 392
	  return ERROR_SUCCESS;
	}
	return GetLastError();
393 394
}

395 396
/***********************************************************************/

397 398 399
DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
{
	if (SHELL_OsIsUnicode())
400 401
	  return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
	return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
402 403 404 405 406 407 408 409 410 411
}

/************************************************************************
 * SHNotifyMoveFile          [internal]
 *
 * Moves a file. Also triggers a change notify if one exists.
 *
 * PARAMS
 *  src        [I]   path to source file to move
 *  dest       [I]   path to target file to move to
412 413
 *  bRename    [I]   if TRUE, the target file will be renamed if a
 *                   file with this name already exists
414 415
 *
 * RETURNS
416
 *  ERORR_SUCCESS if successful
417
 */
418
static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest, BOOL bRename)
419 420 421
{
	BOOL ret;

422
	TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bRename ? "renameIfExists" : "");
423

424 425 426 427 428 429 430 431 432 433 434 435 436
	if (StrPBrkW(dest, wWildcardChars))
	{
	  /* FIXME: This test is currently necessary since our MoveFile
	     implementation does create files with wildcard characters
	     without objection!! Once this is fixed, this here can go away. */
	  SetLastError(ERROR_INVALID_NAME);
#ifdef W98_FO_FUNCTION /* W98 */
	  return ERROR_FILE_NOT_FOUND;
#else
	  return ERROR_INVALID_NAME;
#endif
	}

437 438 439 440 441 442 443 444
	ret = MoveFileW(src, dest);
	if (!ret)
	{
	  /* Source file may be write protected or a system file */
	  DWORD dwAttr = GetFileAttributesW(src);
	  if ((dwAttr != -1) && (dwAttr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
	    if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
	      ret = MoveFileW(src, dest);
445 446 447 448 449 450 451

	  if (!ret && bRename)
	  {
	    /* Destination file probably exists */
	    dwAttr = GetFileAttributesW(dest);
	    if (dwAttr != -1)
	    {
452
	      FIXME("Rename on move to existing file not implemented!\n");
453 454
	    }
	  }
455 456
	}
	if (ret)
457
	{
458
	  SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
459 460 461
	  return ERROR_SUCCESS;
	}
	return GetLastError();
462 463 464 465 466 467 468 469 470 471 472 473 474 475
}

/************************************************************************
 * SHNotifyCopyFile          [internal]
 *
 * Copies a file. Also triggers a change notify if one exists.
 *
 * PARAMS
 *  src        [I]   path to source file to move
 *  dest       [I]   path to target file to move to
 *  bRename    [I]   if TRUE, the target file will be renamed if a
 *                   file with this name already exists
 *
 * RETURNS
476
 *  ERROR_SUCCESS if successful
477
 */
478
static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bRename)
479
{
480
	BOOL ret;
481

482 483 484 485 486 487 488 489 490
	TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bRename ? "renameIfExists" : "");

	ret = CopyFileW(src, dest, TRUE);
	if (!ret && bRename)
	{
	  /* Destination file probably exists */
	  DWORD dwAttr = GetFileAttributesW(dest);
	  if (dwAttr != -1)
	  {
491
	    FIXME("Rename on copy to existing file not implemented!\n");
492 493 494
	  }
	}
	if (ret)
495
	{
496
	  SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
497 498 499
	  return ERROR_SUCCESS;
	}
	return GetLastError();
500 501
}

502 503 504 505 506 507
/*************************************************************************
 * SHCreateDirectory         [SHELL32.165]
 *
 * Create a directory at the specified location
 *
 * PARAMS
508 509
 *  hWnd       [I]
 *  path       [I]   path of directory to create
510 511 512 513 514 515 516 517 518 519 520 521 522 523
 *
 * RETURNS
 *  ERRROR_SUCCESS or one of the following values:
 *  ERROR_BAD_PATHNAME if the path is relative
 *  ERROR_FILE_EXISTS when a file with that name exists
 *  ERROR_ALREADY_EXISTS when the directory already exists
 *  ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
 *
 * NOTES
 *  exported by ordinal
 *  Win9x exports ANSI
 *  WinNT/2000 exports Unicode
 */
DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
524 525
{
	if (SHELL_OsIsUnicode())
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
	  return SHCreateDirectoryExW(hWnd, path, NULL);
	return SHCreateDirectoryExA(hWnd, path, NULL);
}

/*************************************************************************
 * SHCreateDirectoryExA      [SHELL32.@]
 *
 * Create a directory at the specified location
 *
 * PARAMS
 *  hWnd       [I]   
 *  path       [I]   path of directory to create 
 *  sec        [I]   security attributes to use or NULL
 *
 * RETURNS
 *  ERRROR_SUCCESS or one of the following values:
 *  ERROR_BAD_PATHNAME if the path is relative
543
 *  ERORO_INVALID_NAME if the path contains invalid chars
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
 *  ERROR_FILE_EXISTS when a file with that name exists
 *  ERROR_ALREADY_EXISTS when the directory already exists
 *  ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
 */
DWORD WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
{
	WCHAR wPath[MAX_PATH];
	TRACE("(%p, %s, %p)\n",hWnd, debugstr_a(path), sec);

	MultiByteToWideChar(CP_ACP, 0, path, -1, wPath, MAX_PATH);
	return SHCreateDirectoryExW(hWnd, wPath, sec);
}

/*************************************************************************
 * SHCreateDirectoryExW      [SHELL32.@]
 */
DWORD WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
{
562
	DWORD ret = ERROR_BAD_PATHNAME;
563 564 565 566
	TRACE("(%p, %s, %p)\n",hWnd, debugstr_w(path), sec);

	if (PathIsRelativeW(path))
	{
567
	  SetLastError(ret);
568 569 570
	}
	else
	{
571 572 573 574
	  ret = SHNotifyCreateDirectoryW(path, sec);
	  if (ret != ERROR_FILE_EXISTS &&
	      ret != ERROR_ALREADY_EXISTS &&
	      ret != ERROR_FILENAME_EXCED_RANGE)
575
	  {
576 577 578
	  /* handling network file names?
	    lstrcpynW(pathName, path, MAX_PATH);
	    lpStr = PathAddBackslashW(pathName);*/
579
	    FIXME("Semi-stub, non zero hWnd should be used somehow?\n");
580 581 582
	  }
	}
	return ret;
583 584
}

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
/*************************************************************************
 *
 * SHFileStrICmp HelperFunction for SHFileOperationW
 *
 */
BOOL SHFileStrICmpW(LPWSTR p1, LPWSTR p2, LPWSTR p1End, LPWSTR p2End)
{
	WCHAR C1 = '\0';
	WCHAR C2 = '\0';
	int i_Temp = -1;
	int i_len1 = lstrlenW(p1);
	int i_len2 = lstrlenW(p2);

	if (p1End && (&p1[i_len1] >= p1End) && ('\\' == p1End[0]))
	{
	  C1 = p1End[0];
	  p1End[0] = '\0';
	  i_len1 = lstrlenW(p1);
	}
	if (p2End)
	{
	  if ((&p2[i_len2] >= p2End) && ('\\' == p2End[0]))
	  {
	    C2 = p2End[0];
	    if (C2)
	      p2End[0] = '\0';
	  }
	}
	else
	{
	  if ((i_len1 <= i_len2) && ('\\' == p2[i_len1]))
	  {
	    C2 = p2[i_len1];
	    if (C2)
	      p2[i_len1] = '\0';
	  }
	}
	i_len2 = lstrlenW(p2);
	if (i_len1 == i_len2)
	  i_Temp = lstrcmpiW(p1,p2);
	if (C1)
	  p1[i_len1] = C1;
	if (C2)
	  p2[i_len2] = C2;
	return !(i_Temp);
}

/*************************************************************************
 *
 * SHFileStrCpyCat HelperFunction for SHFileOperationW
 *
 */
LPWSTR SHFileStrCpyCatW(LPWSTR pTo, LPCWSTR pFrom, LPCWSTR pCatStr)
{
	LPWSTR pToFile = NULL;
	int  i_len;
	if (pTo)
	{
	  if (pFrom)
	    lstrcpyW(pTo, pFrom);
	  if (pCatStr)
	  {
	    i_len = lstrlenW(pTo);
	    if ((i_len) && (pTo[--i_len] != '\\'))
	      i_len++;
	    pTo[i_len] = '\\';
	    if (pCatStr[0] == '\\')
	      pCatStr++; \
	    lstrcpyW(&pTo[i_len+1], pCatStr);
	  }
	  pToFile = StrRChrW(pTo,NULL,'\\');
656
	  /* termination of the new string-group */
657 658 659 660 661
	  pTo[(lstrlenW(pTo)) + 1] = '\0';
	}
	return pToFile;
}

662 663 664 665
/**************************************************************************
 *	SHELL_FileNamesMatch()
 *
 * Accepts two \0 delimited lists of the file names. Checks whether number of
666
 * files in both lists is the same, and checks also if source-name exists.
667
 */
668
BOOL SHELL_FileNamesMatch(LPCWSTR pszFiles1, LPCWSTR pszFiles2, BOOL bOnlySrc)
669
{
670 671 672 673 674 675 676 677 678 679 680 681 682
	while ((pszFiles1[0] != '\0') &&
	       (bOnlySrc || (pszFiles2[0] != '\0')))
	{
	  if (NULL == StrPBrkW(pszFiles1, wWildcardChars))
	  {
	    if (-1 == GetFileAttributesW(pszFiles1))
	      return FALSE;
	  }
	  pszFiles1 += lstrlenW(pszFiles1) + 1;
	  if (!bOnlySrc)
	    pszFiles2 += lstrlenW(pszFiles2) + 1;
	}
	return ((pszFiles1[0] == '\0') && (bOnlySrc || (pszFiles2[0] == '\0')));
683 684
}

685
/*************************************************************************
686
 *
687
 * SHNameTranslate HelperFunction for SHFileOperationA
688
 *
689
 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
 * is NULL, only the necessary size of the string is determined and returned,
 * otherwise the ASCII strings are copied into it and the buffer is increased
 * to point to the location after the final 0 termination char.
 */
DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
{
	DWORD size = 0, aSize = 0;
	LPCSTR aString = (LPCSTR)*pWToFrom;

	if (aString)
	{
	  do
	  {
	    size = lstrlenA(aString) + 1;
	    aSize += size;
	    aString += size;
	  } while ((size != 1) && more);
	  /* The two sizes might be different in the case of multibyte chars */
	  size = MultiByteToWideChar(CP_ACP, 0, aString, aSize, *wString, 0);
	  if (*wString) /* only in the second loop */
	  {
	    MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
	    *pWToFrom = *wString;
	    *wString += size;
	  }
	}
	return size;
}
/*************************************************************************
 * SHFileOperationA          [SHELL32.@]
 *
 * Function to copy, move, delete and create one or more files with optional
 * user prompts.
 *
 * PARAMS
 *  lpFileOp   [I/O] pointer to a structure containing all the necessary information
726 727
 *
 * NOTES
728 729
 *  exported by name
 */
730
int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
731 732
{
	SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
733 734
	int retCode = 0;
	DWORD size;
735 736
	LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
	       wString = NULL; /* we change this in SHNameTranslate */
737

738
	TRACE("\n");
739 740 741 742
	if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
	  nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
	if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
	  nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
743
	while (1) /* every loop calculate size, second translate also, if we have storage for this */
744 745 746 747 748
	{
	  size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
	  size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
	  size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */

749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
	  if (ForFree)
	  {
	    retCode = SHFileOperationW(&nFileOp);
	    HeapFree(GetProcessHeap(), 0, ForFree); /* we can not use wString, it was changed */
	    break;
	  }
	  else
	  {
	    wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
	    if (ForFree) continue;
	    retCode = ERROR_OUTOFMEMORY;
	    nFileOp.fAnyOperationsAborted = TRUE;
	    SetLastError(retCode);
	    return retCode;
	  }
764
	}
765

766 767 768 769 770
	lpFileOp->hNameMappings = nFileOp.hNameMappings;
	lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
	return retCode;
}

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
static const char * debug_shfileops_flags( DWORD fFlags )
{
    return wine_dbg_sprintf( "%s%s%s%s%s%s%s%s%s%s%s%s%s",
	fFlags & FOF_MULTIDESTFILES ? "FOF_MULTIDESTFILES " : "",
	fFlags & FOF_CONFIRMMOUSE ? "FOF_CONFIRMMOUSE " : "",
	fFlags & FOF_SILENT ? "FOF_SILENT " : "",
	fFlags & FOF_RENAMEONCOLLISION ? "FOF_RENAMEONCOLLISION " : "",
	fFlags & FOF_NOCONFIRMATION ? "FOF_NOCONFIRMATION " : "",
	fFlags & FOF_WANTMAPPINGHANDLE ? "FOF_WANTMAPPINGHANDLE " : "",
	fFlags & FOF_ALLOWUNDO ? "FOF_ALLOWUNDO " : "",
	fFlags & FOF_FILESONLY ? "FOF_FILESONLY " : "",
	fFlags & FOF_SIMPLEPROGRESS ? "FOF_SIMPLEPROGRESS " : "",
	fFlags & FOF_NOCONFIRMMKDIR ? "FOF_NOCONFIRMMKDIR " : "",
	fFlags & FOF_NOERRORUI ? "FOF_NOERRORUI " : "",
	fFlags & FOF_NOCOPYSECURITYATTRIBS ? "FOF_NOCOPYSECURITYATTRIBS" : "",
	fFlags & 0xf000 ? "MORE-UNKNOWN-Flags" : "");
}

static const char * debug_shfileops_action( DWORD op )
{
    LPCSTR cFO_Name [] = {"FO_????","FO_MOVE","FO_COPY","FO_DELETE","FO_RENAME"};
    return wine_dbg_sprintf("%s", cFO_Name[ op ]);
}

795 796 797 798
/*************************************************************************
 * SHFileOperationW          [SHELL32.@]
 *
 * See SHFileOperationA
799
 */
800
int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
801
{
802 803 804 805 806 807 808 809 810 811 812 813 814
	SHFILEOPSTRUCTW nFileOp = *(lpFileOp);

	LPCWSTR pNextFrom = nFileOp.pFrom;
	LPCWSTR pNextTo = nFileOp.pTo;
	LPCWSTR pFrom = pNextFrom;
	LPCWSTR pTo = NULL;
	HANDLE hFind = INVALID_HANDLE_VALUE;
	WIN32_FIND_DATAW wfd;
	LPWSTR pTempFrom = NULL;
	LPWSTR pTempTo = NULL;
	LPWSTR pFromFile;
	LPWSTR pToFile = NULL;
	LPWSTR lpFileName;
815
	int retCode = 0;
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
	DWORD ToAttr;
	DWORD ToPathAttr;
	DWORD FromPathAttr;
	FILEOP_FLAGS OFl = ((FILEOP_FLAGS)lpFileOp->fFlags & 0xfff);

	BOOL b_Multi = (nFileOp.fFlags & FOF_MULTIDESTFILES);

	BOOL b_MultiTo = (FO_DELETE != (lpFileOp->wFunc & FO_MASK));
	BOOL b_MultiPaired = (!b_MultiTo);
	BOOL b_MultiFrom = FALSE;
	BOOL not_overwrite;
	BOOL ask_overwrite;
	BOOL b_SameRoot;
	BOOL b_SameTailName;
	BOOL b_ToInvalidTail = FALSE;
	BOOL b_ToValid; /* for W98-Bug for FO_MOVE with source and target in same rootdrive */
	BOOL b_Mask;
	BOOL b_ToTailSlash = FALSE;

	long FuncSwitch = (nFileOp.wFunc & FO_MASK);
	long level= nFileOp.wFunc>>4;

	/*  default no error */
	nFileOp.fAnyOperationsAborted = FALSE;

	if ((FuncSwitch < FO_MOVE) || (FuncSwitch > FO_RENAME))
	  goto shfileop_normal; /* no valid FunctionCode */

	if (level == 0)
845 846 847
            TRACE("%s: flags (0x%04x) : %s\n",
                debug_shfileops_action(FuncSwitch), nFileOp.fFlags,
                debug_shfileops_flags(nFileOp.fFlags) );
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872

        /* establish when pTo is interpreted as the name of the destination file
         * or the directory where the Fromfile should be copied to.
         * This depends on:
         * (1) pTo points to the name of an existing directory;
         * (2) the flag FOF_MULTIDESTFILES is present;
         * (3) whether pFrom point to multiple filenames.
         *
         * Some experiments:
         *
         * destisdir               1 1 1 1 0 0 0 0
         * FOF_MULTIDESTFILES      1 1 0 0 1 1 0 0
         * multiple from filenames 1 0 1 0 1 0 1 0
         *                         ---------------
         * copy files to dir       1 0 1 1 0 0 1 0
         * create dir              0 0 0 0 0 0 1 0
         */
/*
 * FOF_MULTIDESTFILES, FOF_NOCONFIRMATION, FOF_FILESONLY  are implemented
 * FOF_CONFIRMMOUSE, FOF_SILENT, FOF_NOCONFIRMMKDIR,
 *       FOF_SIMPLEPROGRESS, FOF_NOCOPYSECURITYATTRIBS    are not implemented and ignored
 * FOF_RENAMEONCOLLISION                                  are implemented partially and breaks if file exist
 * FOF_ALLOWUNDO, FOF_WANTMAPPINGHANDLE                   are not implemented and breaks
 * if any other flag set, an error occurs
 */
873 874
        TRACE("%s level=%ld nFileOp.fFlags=0x%x\n", 
                debug_shfileops_action(FuncSwitch), level, lpFileOp->fFlags);
875 876 877 878 879 880 881

/*    OFl &= (-1 - (FOF_MULTIDESTFILES | FOF_FILESONLY)); */
/*    OFl ^= (FOF_SILENT | FOF_NOCONFIRMATION | FOF_SIMPLEPROGRESS | FOF_NOCONFIRMMKDIR); */
        OFl &= (~(FOF_MULTIDESTFILES | FOF_NOCONFIRMATION | FOF_FILESONLY));  /* implemented */
        OFl ^= (FOF_SILENT | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_NOCOPYSECURITYATTRIBS); /* ignored, if one */
        OFl &= (~FOF_SIMPLEPROGRESS);                      /* ignored, only with FOF_SILENT */
        if (OFl)
882
        {
883 884 885
	    if (OFl & (~(FOF_CONFIRMMOUSE | FOF_SILENT | FOF_RENAMEONCOLLISION |
	                 FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_NOCOPYSECURITYATTRIBS)))
	    {
886 887
                TRACE("%s level=%ld lpFileOp->fFlags=0x%x not implemented, Aborted=TRUE, stub\n",
                      debug_shfileops_action(FuncSwitch), level, OFl);
888 889 890 891 892
                retCode = 0x403; /* 1027, we need an extension to shlfileop */
                goto shfileop_error;
	    }
	    else
	    {
893 894
                TRACE("%s level=%ld lpFileOp->fFlags=0x%x not fully implemented, stub\n", 
                      debug_shfileops_action(FuncSwitch), level, OFl);
895 896
	    } 
        } 
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929

        if ((pNextFrom) && (!(b_MultiTo) || (pNextTo)))
        {
	    nFileOp.pFrom = pTempFrom = HeapAlloc(GetProcessHeap(), 0, ((1 + 2 * (b_MultiTo)) * MAX_PATH + 6) * sizeof(WCHAR));
	    if (!pTempFrom)
	    {
                retCode = ERROR_OUTOFMEMORY;
                SetLastError(retCode);
                goto shfileop_error;
	    }
	    if (b_MultiTo)
                pTempTo = &pTempFrom[MAX_PATH + 4];
	    nFileOp.pTo = pTempTo;
	    ask_overwrite = (!(nFileOp.fFlags & FOF_NOCONFIRMATION) && !(nFileOp.fFlags & FOF_RENAMEONCOLLISION));
	    not_overwrite = (!(nFileOp.fFlags & FOF_NOCONFIRMATION) ||  (nFileOp.fFlags & FOF_RENAMEONCOLLISION));
        }
        else
        {
	    retCode = 0x402;      /* 1026 */
	    goto shfileop_error;
        }
        /* need break at error before change sourcepointer */
        while(!nFileOp.fAnyOperationsAborted && (pNextFrom[0]))
        {
	    nFileOp.wFunc =  ((level + 1) << 4) + FuncSwitch;
	    nFileOp.fFlags = lpFileOp->fFlags;

	    if (b_MultiTo)
	    {
                pTo = pNextTo;
                pNextTo = &pNextTo[lstrlenW(pTo)+1];
                b_MultiTo = (b_Multi && pNextTo[0]);
	    }
930

931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
	    pFrom = pNextFrom;
	    pNextFrom = &pNextFrom[lstrlenW(pNextFrom)+1];
	    if (!b_MultiFrom && !b_MultiTo)
                b_MultiFrom = (pNextFrom[0]);

	    pFromFile = SHFileStrCpyCatW(pTempFrom, pFrom, NULL);

	    if (pTo)
	    {
                pToFile = SHFileStrCpyCatW(pTempTo, pTo, NULL);
	    }
	    if (!b_MultiPaired)
	    {
                b_MultiPaired =
                    SHELL_FileNamesMatch(lpFileOp->pFrom, lpFileOp->pTo, (!b_Multi || b_MultiFrom));
946
	    }
947 948 949 950 951 952 953 954 955
	    if (!(b_MultiPaired) || !(pFromFile) || !(pFromFile[1]) || ((pTo) && !(pToFile)))
	    {
                retCode = 0x402;      /* 1026 */
                goto shfileop_error;
	    }
	    if (pTo)
	    {
                b_ToTailSlash = (!pToFile[1]);
                if (b_ToTailSlash)
956
                {
957 958 959 960 961
                    pToFile[0] = '\0';
                    if (StrChrW(pTempTo,'\\'))
                    {
                        pToFile = SHFileStrCpyCatW(pTempTo, NULL, NULL);
                    }
962
                }
963 964
                b_ToInvalidTail = (NULL != StrPBrkW(&pToFile[1], wWildcardChars));
	    }
965

966 967 968 969 970 971
	    /* for all */
	    b_Mask = (NULL != StrPBrkW(&pFromFile[1], wWildcardChars));
	    if (FO_RENAME == FuncSwitch)
	    {
                /* temporary only for FO_RENAME */
                if (b_MultiTo || b_MultiFrom || (b_Mask && !b_ToInvalidTail))
972
                {
973 974
#ifndef W98_FO_FUNCTION
                    retCode = ERROR_GEN_FAILURE;  /* W2K ERROR_GEN_FAILURE, W98 returns no error */
975 976
#endif
                    goto shfileop_error;
977
                }
978
	    }
979

980 981 982 983 984 985 986 987 988 989 990 991
	    hFind = FindFirstFileW(pFrom, &wfd);
	    if (INVALID_HANDLE_VALUE == hFind)
	    {
                if ((FO_DELETE == FuncSwitch) && (b_Mask))
                {
                    pFromFile[0] = '\0';
                    FromPathAttr = GetFileAttributesW(pTempFrom);
                    pFromFile[0] = '\\';
                    if (IsAttribDir(FromPathAttr))
                    {
                        /* FO_DELETE with mask and without found is valid */
                        goto shfileop_normal;
992 993
                    }
                }
994 995 996
                /* root (without mask) is also not allowed as source, tested in W98 */
                retCode = 0x402;   /* 1026 */
                goto shfileop_error;
997
	    }
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016

/* for all */
#define HIGH_ADR (LPWSTR)0xffffffff

/* ???	    b_Mask = (!SHFileStrICmpA(&pFromFile[1], &wfd.cFileName[0], HIGH_ADR, HIGH_ADR)); */
	    if (!pTo) /* FO_DELETE */
	    {
                do
                {
                    lpFileName = wfd.cAlternateFileName;
                    if (!lpFileName[0])
                        lpFileName = wfd.cFileName;
                    if (IsDotDir(lpFileName) ||
                        ((b_Mask) && IsAttribDir(wfd.dwFileAttributes) && (nFileOp.fFlags & FOF_FILESONLY)))
                        continue;
                    SHFileStrCpyCatW(&pFromFile[1], lpFileName, NULL);
                    /* TODO: Check the SHELL_DeleteFileOrDirectoryW() function in shell32.dll */
                    if (IsAttribFile(wfd.dwFileAttributes))
                    {
1017
                        nFileOp.fAnyOperationsAborted = (SHNotifyDeleteFileW(pTempFrom) != ERROR_SUCCESS);
1018
                        retCode = 0x78; /* value unknown */
1019
                    }
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
                    else
                    {
                        nFileOp.fAnyOperationsAborted = (!SHELL_DeleteDirectoryW(pTempFrom, (!(nFileOp.fFlags & FOF_NOCONFIRMATION))));
                        retCode = 0x79; /* value unknown */
                    }
                } while (!nFileOp.fAnyOperationsAborted && FindNextFileW(hFind, &wfd));
                FindClose(hFind);
                hFind = INVALID_HANDLE_VALUE;
                if (nFileOp.fAnyOperationsAborted)
                {
                    goto shfileop_error;
                }
                continue;
	    } /* FO_DELETE ends, pTo must be always valid from here */

	    b_SameRoot = (toupperW(pTempFrom[0]) == toupperW(pTempTo[0]));
	    b_SameTailName = SHFileStrICmpW(pToFile, pFromFile, NULL, NULL);

	    ToPathAttr = ToAttr = GetFileAttributesW(pTempTo);
	    if (!b_Mask && (ToAttr == -1) && (pToFile))
	    {
                pToFile[0] = '\0';
                ToPathAttr = GetFileAttributesW(pTempTo);
                pToFile[0] = '\\';
	    }

	    if (FO_RENAME == FuncSwitch)
	    {
                if (!b_SameRoot || b_Mask /* FO_RENAME works not with Mask */
                    || !SHFileStrICmpW(pTempFrom, pTempTo, pFromFile, NULL)
                    || (SHFileStrICmpW(pTempFrom, pTempTo, pFromFile, HIGH_ADR) && !b_ToTailSlash))
                {
                    retCode = 0x73;
                    goto shfileop_error;
                }
                if (b_ToInvalidTail)
                {
                    retCode=0x2;
                    goto shfileop_error;
                }
                if (-1 == ToPathAttr)
                {
                    retCode = 0x75;
                    goto shfileop_error;
                }
                if (IsAttribDir(wfd.dwFileAttributes) && IsAttribDir(ToAttr))
                {
                    retCode = (b_ToTailSlash) ? 0xb7 : 0x7b;
                    goto shfileop_error;
1069
                }
1070
                /* we use SHNotifyMoveFile() instead MoveFileW */
1071
                if (SHNotifyMoveFileW(pTempFrom, pTempTo, nFileOp.fFlags & FOF_RENAMEONCOLLISION) != ERROR_SUCCESS)
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
                {
                    /* we need still the value for the returncode, we use the mostly assumed */
                    retCode = 0xb7;
                    goto shfileop_error;
                }
                goto shfileop_normal;
	    }

	    /* W98 Bug with FO_MOVE different to FO_COPY, better the same as FO_COPY */
	    b_ToValid = ((b_SameTailName &&  b_SameRoot && (FO_COPY == FuncSwitch)) ||
                         (b_SameTailName && !b_SameRoot) || (b_ToInvalidTail));

	    /* handle mask in source */
	    if (b_Mask)
	    {
                if (!IsAttribDir(ToAttr))
                {
                    retCode = (b_ToInvalidTail &&/* b_SameTailName &&*/ (FO_MOVE == FuncSwitch)) \
                        ? 0x2 : 0x75;
                    goto shfileop_error;
                }
                pToFile = SHFileStrCpyCatW(pTempTo, NULL, wBackslash);
                nFileOp.fFlags = (nFileOp.fFlags | FOF_MULTIDESTFILES);
                do
                {
                    lpFileName = wfd.cAlternateFileName;
                    if (!lpFileName[0])
                        lpFileName = wfd.cFileName;
                    if (IsDotDir(lpFileName) ||
                        (IsAttribDir(wfd.dwFileAttributes) && (nFileOp.fFlags & FOF_FILESONLY)))
                        continue; /* next name in pTempFrom(dir) */
                    SHFileStrCpyCatW(&pToFile[1], lpFileName, NULL);
                    SHFileStrCpyCatW(&pFromFile[1], lpFileName, NULL);
                    retCode = SHFileOperationW (&nFileOp);
                } while(!nFileOp.fAnyOperationsAborted && FindNextFileW(hFind, &wfd));
	    }
	    FindClose(hFind);
	    hFind = INVALID_HANDLE_VALUE;
	    /* FO_COPY/FO_MOVE with mask, FO_DELETE and FO_RENAME are solved */
	    if (b_Mask)
                continue;

	    /* only FO_COPY/FO_MOVE without mask, all others are (must be) solved */
	    if (IsAttribDir(wfd.dwFileAttributes) && (ToAttr == -1))
	    {
                if (pToFile)
                {
                    pToFile[0] = '\0';
                    ToPathAttr = GetFileAttributesW(pTempTo);
                    if ((ToPathAttr == -1) && b_ToValid)
                    {
                        /* create dir must be here, sample target D:\y\ *.* create with RC=10003 */
                        if (SHCreateDirectoryExW(NULL, pTempTo, NULL))
1125
                        {
1126 1127
                            retCode = 0x73;/* value unknown */
                            goto shfileop_error;
1128
                        }
1129
                        ToPathAttr = GetFileAttributesW(pTempTo);
1130
                    }
1131 1132 1133 1134 1135
                    pToFile[0] = '\\';
                    if (b_ToInvalidTail)
                    {
                        retCode = 0x10003;
                        goto shfileop_error;
1136 1137
                    }
                }
1138
	    }
1139

1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
	    /* trailing BackSlash is ever removed and pToFile points to BackSlash before */
	    if (!b_MultiTo && (b_MultiFrom || (!(b_Multi) && IsAttribDir(ToAttr))))
	    {
                if ((FO_MOVE == FuncSwitch) && IsAttribDir(ToAttr) && IsAttribDir(wfd.dwFileAttributes))
                {
                    if (b_Multi)
                    {
                        retCode = 0x73; /* !b_Multi = 0x8 ?? */
                        goto shfileop_error;
                    }
                }
                pToFile = SHFileStrCpyCatW(pTempTo, NULL, wfd.cFileName);
                ToAttr = GetFileAttributesW(pTempTo);
	    }
1154

1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
	    if (IsAttribDir(ToAttr))
	    {
                if (IsAttribFile(wfd.dwFileAttributes))
                {
                    retCode = (FO_COPY == FuncSwitch) ? 0x75 : 0xb7;
                    goto shfileop_error;
                }
	    }
	    else
	    {
                pToFile[0] = '\0';
                ToPathAttr = GetFileAttributesW(pTempTo);
                pToFile[0] = '\\';
                if (IsAttribFile(ToPathAttr))
                {
                    /* error, is this tested ? */
                    retCode = 0x777402;
                    goto shfileop_error;
1173
                }
1174
	    }
1175

1176 1177 1178 1179 1180 1181 1182
	    /* singlesource + no mask */
	    if (-1 == (ToAttr & ToPathAttr))
	    {
                /* Target-dir does not exist, and cannot be created */
                retCode=0x75;
                goto shfileop_error;
	    }
1183

1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
	    switch(FuncSwitch)
	    {
	    case FO_MOVE:
                pToFile = NULL;
                if ((ToAttr == -1) && SHFileStrICmpW(pTempFrom, pTempTo, pFromFile, NULL))
                {
                    nFileOp.wFunc =  ((level+1)<<4) + FO_RENAME;
                }
                else
                {
                    if (b_SameRoot && IsAttribDir(ToAttr) && IsAttribDir(wfd.dwFileAttributes))
                    {
                        /* we need pToFile for FO_DELETE after FO_MOVE contence */
                        pToFile = SHFileStrCpyCatW(pTempFrom, NULL, wWildcardFile);
                    }
                    else
                    {
                        nFileOp.wFunc =  ((level+1)<<4) + FO_COPY;
                    }
                }
                retCode = SHFileOperationW(&nFileOp);
                if (pToFile)
                    ((DWORD*)pToFile)[0] = '\0';
                if (!nFileOp.fAnyOperationsAborted && (FO_RENAME != (nFileOp.wFunc & 0xf)))
                {
                    nFileOp.wFunc =  ((level+1)<<4) + FO_DELETE;
                    retCode = SHFileOperationW(&nFileOp);
                }
                continue;
	    case FO_COPY:
                if (SHFileStrICmpW(pTempFrom, pTempTo, NULL, NULL))
                { /* target is the same as source ? */
                    /* we still need the value for the returncode, we assume 0x71 */
                    retCode = 0x71;
                    goto shfileop_error;
1219
                }
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
                if (IsAttribDir((ToAttr & wfd.dwFileAttributes)))
                {
                    if (IsAttribDir(ToAttr) || !SHCreateDirectoryExW(NULL,pTempTo, NULL))
                    {
/* ???	          nFileOp.fFlags = (nFileOp.fFlags | FOF_MULTIDESTFILES); */
                        SHFileStrCpyCatW(pTempFrom, NULL, wWildcardFile);
                        retCode = SHFileOperationW(&nFileOp);
                    }
                    else
                    {
                        retCode = 0x750;/* value unknown */
                        goto shfileop_error;
                    }
                }
                else
                {
                    if (!(ask_overwrite && SHELL_ConfirmDialogW(ASK_OVERWRITE_FILE, pTempTo))
                        && (not_overwrite))
                    {
                        /* we still need the value for the returncode, we use the mostly assumed */
                        retCode = 0x73;
                        goto shfileop_error;
                    }
1243
                    if (SHNotifyCopyFileW(pTempFrom, pTempTo, nFileOp.fFlags & FOF_RENAMEONCOLLISION) != ERROR_SUCCESS)
1244 1245 1246 1247 1248
                    {
                        retCode = 0x77; /* value unknown */
                        goto shfileop_error;
                    }
                }
1249 1250
	    }
        }
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264

shfileop_normal:
	if (!(nFileOp.fAnyOperationsAborted))
	  retCode = 0;
shfileop_error:
	if (hFind != INVALID_HANDLE_VALUE)
	  FindClose(hFind);
	hFind = INVALID_HANDLE_VALUE;
	if (pTempFrom)
	  HeapFree(GetProcessHeap(), 0, pTempFrom);
	if (retCode)
	{
	  nFileOp.fAnyOperationsAborted = TRUE;
	}
1265
	TRACE("%s level=%ld AnyOpsAborted=%s ret=0x%x, with %s %s%s\n",
1266 1267
              debug_shfileops_action(FuncSwitch), level,
	      nFileOp.fAnyOperationsAborted ? "TRUE":"FALSE",
1268 1269 1270 1271
	      retCode, debugstr_w(pFrom), pTo ? "-> ":"", debugstr_w(pTo));

	lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
	return retCode;
1272 1273 1274
}

/*************************************************************************
1275
 * SHFileOperation        [SHELL32.@]
1276 1277 1278 1279
 *
 */
DWORD WINAPI SHFileOperationAW(LPVOID lpFileOp)
{
1280
	if (SHELL_OsIsUnicode())
1281 1282 1283
	  return SHFileOperationW(lpFileOp);
	return SHFileOperationA(lpFileOp);
}
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302

/*************************************************************************
 * SheGetDirW [SHELL32.281]
 *
 */
HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
{	FIXME("%p %p stub\n",u,v);
	return 0;
}

/*************************************************************************
 * SheChangeDirW [SHELL32.274]
 *
 */
HRESULT WINAPI SheChangeDirW(LPWSTR u)
{	FIXME("(%s),stub\n",debugstr_w(u));
	return 0;
}

1303 1304 1305 1306 1307 1308 1309
/*************************************************************************
 * IsNetDrive			[SHELL32.66]
 */
BOOL WINAPI IsNetDrive(DWORD drive)
{
	char root[4];
	strcpy(root, "A:\\");
1310
	root[0] += (char)drive;
1311 1312
	return (GetDriveTypeA(root) == DRIVE_REMOTE);
}