1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 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 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 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 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 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 845 846 847 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 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 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 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 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 1069 1070 1071 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 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 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 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
|
/*
* SHFileOperation
*
* Copyright 2000 Juergen Schmied
* Copyright 2002 Andriy Palamarchuk
* Copyright 2004 Dietrich Teickner (from Odin)
* Copyright 2004 Rolf Kalbermatter
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "shellapi.h"
#include "wingdi.h"
#include "winuser.h"
#include "shlobj.h"
#include "shresdef.h"
#define NO_SHLWAPI_STREAM
#include "shlwapi.h"
#include "shell32_main.h"
#include "undocshell.h"
#include "wine/debug.h"
#include "xdg.h"
WINE_DEFAULT_DEBUG_CHANNEL(shell);
#define IsAttrib(x, y) ((INVALID_FILE_ATTRIBUTES != (x)) && ((x) & (y)))
#define IsAttribFile(x) (!((x) & FILE_ATTRIBUTE_DIRECTORY))
#define IsAttribDir(x) IsAttrib(x, FILE_ATTRIBUTE_DIRECTORY)
#define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
#define FO_MASK 0xF
#define DE_SAMEFILE 0x71
#define DE_DESTSAMETREE 0x7D
static const WCHAR wWildcardFile[] = {'*',0};
static const WCHAR wWildcardChars[] = {'*','?',0};
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);
static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists);
static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly);
typedef struct
{
SHFILEOPSTRUCTW *req;
DWORD dwYesToAllMask;
BOOL bManyItems;
BOOL bCancelled;
} FILE_OPERATION;
/* Confirm dialogs with an optional "Yes To All" as used in file operations confirmations
*/
static const WCHAR CONFIRM_MSG_PROP[] = {'W','I','N','E','_','C','O','N','F','I','R','M',0};
struct confirm_msg_info
{
LPWSTR lpszText;
LPWSTR lpszCaption;
HICON hIcon;
BOOL bYesToAll;
};
/* as some buttons may be hidden and the dialog height may change we may need
* to move the controls */
static void confirm_msg_move_button(HWND hDlg, INT iId, INT *xPos, INT yOffset, BOOL bShow)
{
HWND hButton = GetDlgItem(hDlg, iId);
RECT r;
if (bShow) {
int width;
GetWindowRect(hButton, &r);
MapWindowPoints( 0, hDlg, (POINT *)&r, 2 );
width = r.right - r.left;
SetWindowPos(hButton, 0, *xPos - width, r.top - yOffset, 0, 0,
SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW );
*xPos -= width + 5;
}
else
ShowWindow(hButton, SW_HIDE);
}
/* Note: we paint the text manually and don't use the static control to make
* sure the text has the same height as the one computed in WM_INITDIALOG
*/
static INT_PTR ConfirmMsgBox_Paint(HWND hDlg)
{
PAINTSTRUCT ps;
HFONT hOldFont;
RECT r;
HDC hdc;
BeginPaint(hDlg, &ps);
hdc = ps.hdc;
GetClientRect(GetDlgItem(hDlg, IDD_MESSAGE), &r);
/* this will remap the rect to dialog coords */
MapWindowPoints(GetDlgItem(hDlg, IDD_MESSAGE), hDlg, (LPPOINT)&r, 2);
hOldFont = SelectObject(hdc, (HFONT)SendDlgItemMessageW(hDlg, IDD_MESSAGE, WM_GETFONT, 0, 0));
DrawTextW(hdc, GetPropW(hDlg, CONFIRM_MSG_PROP), -1, &r, DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_WORDBREAK);
SelectObject(hdc, hOldFont);
EndPaint(hDlg, &ps);
return TRUE;
}
static INT_PTR ConfirmMsgBox_Init(HWND hDlg, LPARAM lParam)
{
struct confirm_msg_info *info = (struct confirm_msg_info *)lParam;
INT xPos, yOffset;
int width, height;
HFONT hOldFont;
HDC hdc;
RECT r;
SetWindowTextW(hDlg, info->lpszCaption);
ShowWindow(GetDlgItem(hDlg, IDD_MESSAGE), SW_HIDE);
SetPropW(hDlg, CONFIRM_MSG_PROP, info->lpszText);
SendDlgItemMessageW(hDlg, IDD_ICON, STM_SETICON, (WPARAM)info->hIcon, 0);
/* compute the text height and resize the dialog */
GetClientRect(GetDlgItem(hDlg, IDD_MESSAGE), &r);
hdc = GetDC(hDlg);
yOffset = r.bottom;
hOldFont = SelectObject(hdc, (HFONT)SendDlgItemMessageW(hDlg, IDD_MESSAGE, WM_GETFONT, 0, 0));
DrawTextW(hdc, info->lpszText, -1, &r, DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_WORDBREAK | DT_CALCRECT);
SelectObject(hdc, hOldFont);
yOffset -= r.bottom;
yOffset = min(yOffset, 35); /* don't make the dialog too small */
ReleaseDC(hDlg, hdc);
GetClientRect(hDlg, &r);
xPos = r.right - 7;
GetWindowRect(hDlg, &r);
width = r.right - r.left;
height = r.bottom - r.top - yOffset;
MoveWindow(hDlg, (GetSystemMetrics(SM_CXSCREEN) - width)/2,
(GetSystemMetrics(SM_CYSCREEN) - height)/2, width, height, FALSE);
confirm_msg_move_button(hDlg, IDCANCEL, &xPos, yOffset, info->bYesToAll);
confirm_msg_move_button(hDlg, IDNO, &xPos, yOffset, TRUE);
confirm_msg_move_button(hDlg, IDD_YESTOALL, &xPos, yOffset, info->bYesToAll);
confirm_msg_move_button(hDlg, IDYES, &xPos, yOffset, TRUE);
return TRUE;
}
static INT_PTR CALLBACK ConfirmMsgBoxProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
return ConfirmMsgBox_Init(hDlg, lParam);
case WM_PAINT:
return ConfirmMsgBox_Paint(hDlg);
case WM_COMMAND:
EndDialog(hDlg, wParam);
break;
case WM_CLOSE:
EndDialog(hDlg, IDCANCEL);
break;
}
return FALSE;
}
static int SHELL_ConfirmMsgBox(HWND hWnd, LPWSTR lpszText, LPWSTR lpszCaption, HICON hIcon, BOOL bYesToAll)
{
static const WCHAR wszTemplate[] = {'S','H','E','L','L','_','Y','E','S','T','O','A','L','L','_','M','S','G','B','O','X',0};
struct confirm_msg_info info;
info.lpszText = lpszText;
info.lpszCaption = lpszCaption;
info.hIcon = hIcon;
info.bYesToAll = bYesToAll;
return DialogBoxParamW(shell32_hInstance, wszTemplate, hWnd, ConfirmMsgBoxProc, (LPARAM)&info);
}
/* confirmation dialogs content */
typedef struct
{
HINSTANCE hIconInstance;
UINT icon_resource_id;
UINT caption_resource_id, text_resource_id;
} SHELL_ConfirmIDstruc;
static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
{
ids->hIconInstance = shell32_hInstance;
switch (nKindOfDialog) {
case ASK_DELETE_FILE:
ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
ids->text_resource_id = IDS_DELETEITEM_TEXT;
return TRUE;
case ASK_DELETE_FOLDER:
ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
ids->text_resource_id = IDS_DELETEITEM_TEXT;
return TRUE;
case ASK_DELETE_MULTIPLE_ITEM:
ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
ids->text_resource_id = IDS_DELETEMULTIPLE_TEXT;
return TRUE;
case ASK_TRASH_FILE:
ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
ids->text_resource_id = IDS_TRASHITEM_TEXT;
return TRUE;
case ASK_TRASH_FOLDER:
ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
ids->text_resource_id = IDS_TRASHFOLDER_TEXT;
return TRUE;
case ASK_TRASH_MULTIPLE_ITEM:
ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
ids->text_resource_id = IDS_TRASHMULTIPLE_TEXT;
return TRUE;
case ASK_CANT_TRASH_ITEM:
ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
ids->text_resource_id = IDS_CANTTRASH_TEXT;
return TRUE;
case ASK_DELETE_SELECTED:
ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
ids->text_resource_id = IDS_DELETESELECTED_TEXT;
return TRUE;
case ASK_OVERWRITE_FILE:
ids->hIconInstance = NULL;
ids->icon_resource_id = IDI_WARNING;
ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
ids->text_resource_id = IDS_OVERWRITEFILE_TEXT;
return TRUE;
case ASK_OVERWRITE_FOLDER:
ids->hIconInstance = NULL;
ids->icon_resource_id = IDI_WARNING;
ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
ids->text_resource_id = IDS_OVERWRITEFOLDER_TEXT;
return TRUE;
default:
FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
}
return FALSE;
}
static BOOL SHELL_ConfirmDialogW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir, FILE_OPERATION *op)
{
WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
SHELL_ConfirmIDstruc ids;
DWORD_PTR args[1];
HICON hIcon;
int ret;
assert(nKindOfDialog >= 0 && nKindOfDialog < 32);
if (op && (op->dwYesToAllMask & (1 << nKindOfDialog)))
return TRUE;
if (!SHELL_ConfirmIDs(nKindOfDialog, &ids)) return FALSE;
LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption)/sizeof(WCHAR));
LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText)/sizeof(WCHAR));
args[0] = (DWORD_PTR)szDir;
FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
szText, 0, 0, szBuffer, sizeof(szBuffer)/sizeof(szBuffer[0]), (__ms_va_list*)args);
hIcon = LoadIconW(ids.hIconInstance, (LPWSTR)MAKEINTRESOURCE(ids.icon_resource_id));
ret = SHELL_ConfirmMsgBox(hWnd, szBuffer, szCaption, hIcon, op && op->bManyItems);
if (op) {
if (ret == IDD_YESTOALL) {
op->dwYesToAllMask |= (1 << nKindOfDialog);
ret = IDYES;
}
if (ret == IDCANCEL)
op->bCancelled = TRUE;
if (ret != IDYES)
op->req->fAnyOperationsAborted = TRUE;
}
return ret == IDYES;
}
BOOL SHELL_ConfirmYesNoW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir)
{
return SHELL_ConfirmDialogW(hWnd, nKindOfDialog, szDir, NULL);
}
static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
{
DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
if (len < minChars)
len = minChars;
*wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
if (*wPath)
{
MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
return NO_ERROR;
}
return E_OUTOFMEMORY;
}
static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
{
HeapFree(GetProcessHeap(), 0, wPath);
}
HRESULT WINAPI SHIsFileAvailableOffline(LPCWSTR path, LPDWORD status)
{
FIXME("(%s, %p) stub\n", debugstr_w(path), status);
return E_FAIL;
}
/**************************************************************************
* SHELL_DeleteDirectory() [internal]
*
* Asks for confirmation when bShowUI is true and deletes the directory and
* all its subdirectories and files if necessary.
*/
static DWORD SHELL_DeleteDirectoryW(HWND hwnd, LPCWSTR pszDir, BOOL bShowUI)
{
DWORD ret = 0;
HANDLE hFind;
WIN32_FIND_DATAW wfd;
WCHAR szTemp[MAX_PATH];
PathCombineW(szTemp, pszDir, wWildcardFile);
hFind = FindFirstFileW(szTemp, &wfd);
if (hFind != INVALID_HANDLE_VALUE) {
if (!bShowUI || SHELL_ConfirmDialogW(hwnd, ASK_DELETE_FOLDER, pszDir, NULL)) {
do {
if (IsDotDir(wfd.cFileName))
continue;
PathCombineW(szTemp, pszDir, wfd.cFileName);
if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
ret = SHELL_DeleteDirectoryW(hwnd, szTemp, FALSE);
else
ret = SHNotifyDeleteFileW(szTemp);
} while (!ret && FindNextFileW(hFind, &wfd));
}
FindClose(hFind);
}
if (ret == ERROR_SUCCESS)
ret = SHNotifyRemoveDirectoryW(pszDir);
return ret == ERROR_PATH_NOT_FOUND ?
0x7C: /* DE_INVALIDFILES (legacy Windows error) */
ret;
}
/**************************************************************************
* Win32CreateDirectory [SHELL32.93]
*
* 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
*/
static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
{
LPWSTR wPath;
DWORD retCode;
TRACE("(%s, %p)\n", debugstr_a(path), sec);
retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
if (!retCode)
{
retCode = SHNotifyCreateDirectoryW(wPath, sec);
SHELL32_FreeUnicodeBuf(wPath);
}
return retCode;
}
/**********************************************************************/
static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
{
TRACE("(%s, %p)\n", debugstr_w(path), sec);
if (CreateDirectoryW(path, sec))
{
SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
return ERROR_SUCCESS;
}
return GetLastError();
}
/**********************************************************************/
BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
{
if (SHELL_OsIsUnicode())
return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
}
/************************************************************************
* 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
*/
static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
{
LPWSTR wPath;
DWORD retCode;
TRACE("(%s)\n", debugstr_a(path));
retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
if (!retCode)
{
retCode = SHNotifyRemoveDirectoryW(wPath);
SHELL32_FreeUnicodeBuf(wPath);
}
return retCode;
}
/***********************************************************************/
static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
{
BOOL ret;
TRACE("(%s)\n", debugstr_w(path));
ret = RemoveDirectoryW(path);
if (!ret)
{
/* Directory may be write protected */
DWORD dwAttr = GetFileAttributesW(path);
if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
ret = RemoveDirectoryW(path);
}
if (ret)
{
SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
return ERROR_SUCCESS;
}
return GetLastError();
}
/***********************************************************************/
BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
{
if (SHELL_OsIsUnicode())
return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
}
/************************************************************************
* Win32DeleteFile [SHELL32.164]
*
* Deletes a file. Also triggers a change notify if one exists.
*
* PARAMS
* path [I] path to file 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
*/
static DWORD SHNotifyDeleteFileA(LPCSTR path)
{
LPWSTR wPath;
DWORD retCode;
TRACE("(%s)\n", debugstr_a(path));
retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
if (!retCode)
{
retCode = SHNotifyDeleteFileW(wPath);
SHELL32_FreeUnicodeBuf(wPath);
}
return retCode;
}
/***********************************************************************/
static DWORD SHNotifyDeleteFileW(LPCWSTR path)
{
BOOL ret;
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 (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
ret = DeleteFileW(path);
}
if (ret)
{
SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
return ERROR_SUCCESS;
}
return GetLastError();
}
/***********************************************************************/
DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
{
if (SHELL_OsIsUnicode())
return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
}
/************************************************************************
* 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
*
* RETURNS
* ERROR_SUCCESS if successful
*/
static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest)
{
BOOL ret;
TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
ret = MoveFileExW(src, dest, MOVEFILE_REPLACE_EXISTING);
/* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
if (!ret)
ret = MoveFileW(src, dest);
if (!ret)
{
DWORD dwAttr;
dwAttr = SHFindAttrW(dest, FALSE);
if (INVALID_FILE_ATTRIBUTES == dwAttr)
{
/* Source file may be write protected or a system file */
dwAttr = GetFileAttributesW(src);
if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
ret = MoveFileW(src, dest);
}
}
if (ret)
{
SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
return ERROR_SUCCESS;
}
return GetLastError();
}
/************************************************************************
* 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
* bFailIfExists [I] if TRUE, the target file will not be overwritten if
* a file with this name already exists
*
* RETURNS
* ERROR_SUCCESS if successful
*/
static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
{
BOOL ret;
DWORD attribs;
TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bFailIfExists ? "failIfExists" : "");
/* Destination file may already exist with read only attribute */
attribs = GetFileAttributesW(dest);
if (IsAttrib(attribs, FILE_ATTRIBUTE_READONLY))
SetFileAttributesW(dest, attribs & ~FILE_ATTRIBUTE_READONLY);
ret = CopyFileW(src, dest, bFailIfExists);
if (ret)
{
SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
return ERROR_SUCCESS;
}
return GetLastError();
}
/*************************************************************************
* SHCreateDirectory [SHELL32.165]
*
* This function creates a file system folder whose fully qualified path is
* given by path. If one or more of the intermediate folders do not exist,
* they will be created as well.
*
* PARAMS
* hWnd [I]
* path [I] path of directory to create
*
* RETURNS
* ERROR_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_PATH_NOT_FOUND can't find the path, probably invalid
* ERROR_INVALID_NAME if the path contains invalid chars
* ERROR_ALREADY_EXISTS when the directory already exists
* ERROR_FILENAME_EXCED_RANGE if the filename was too long to process
*
* NOTES
* exported by ordinal
* Win9x exports ANSI
* WinNT/2000 exports Unicode
*/
DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
{
if (SHELL_OsIsUnicode())
return SHCreateDirectoryExW(hWnd, path, NULL);
return SHCreateDirectoryExA(hWnd, path, NULL);
}
/*************************************************************************
* SHCreateDirectoryExA [SHELL32.@]
*
* This function creates a file system folder whose fully qualified path is
* given by path. If one or more of the intermediate folders do not exist,
* they will be created as well.
*
* PARAMS
* hWnd [I]
* path [I] path of directory to create
* sec [I] security attributes to use or NULL
*
* RETURNS
* ERROR_SUCCESS or one of the following values:
* ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
* ERROR_INVALID_NAME if the path contains invalid chars
* 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 too long to process
*
* FIXME: Not implemented yet;
* SHCreateDirectoryEx also verifies that the files in the directory will be visible
* if the path is a network path to deal with network drivers which might have a limited
* but unknown maximum path length. If not:
*
* If hWnd is set to a valid window handle, a message box is displayed warning
* the user that the files may not be accessible. If the user chooses not to
* proceed, the function returns ERROR_CANCELLED.
*
* If hWnd is set to NULL, no user interface is displayed and the function
* returns ERROR_CANCELLED.
*/
int WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
{
LPWSTR wPath;
DWORD retCode;
TRACE("(%s, %p)\n", debugstr_a(path), sec);
retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
if (!retCode)
{
retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
SHELL32_FreeUnicodeBuf(wPath);
}
return retCode;
}
/*************************************************************************
* SHCreateDirectoryExW [SHELL32.@]
*
* See SHCreateDirectoryExA.
*/
int WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
{
int ret = ERROR_BAD_PATHNAME;
TRACE("(%p, %s, %p)\n", hWnd, debugstr_w(path), sec);
if (PathIsRelativeW(path))
{
SetLastError(ret);
}
else
{
ret = SHNotifyCreateDirectoryW(path, sec);
/* Refuse to work on certain error codes before trying to create directories recursively */
if (ret != ERROR_SUCCESS &&
ret != ERROR_FILE_EXISTS &&
ret != ERROR_ALREADY_EXISTS &&
ret != ERROR_FILENAME_EXCED_RANGE)
{
WCHAR *pEnd, *pSlash, szTemp[MAX_PATH + 1]; /* extra for PathAddBackslash() */
lstrcpynW(szTemp, path, MAX_PATH);
pEnd = PathAddBackslashW(szTemp);
pSlash = szTemp + 3;
while (*pSlash)
{
while (*pSlash && *pSlash != '\\') pSlash++;
if (*pSlash)
{
*pSlash = 0; /* terminate path at separator */
ret = SHNotifyCreateDirectoryW(szTemp, pSlash + 1 == pEnd ? sec : NULL);
}
*pSlash++ = '\\'; /* put the separator back */
}
}
if (ret && hWnd && (ERROR_CANCELLED != ret))
{
/* We failed and should show a dialog box */
FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
}
}
return ret;
}
/*************************************************************************
* SHFindAttrW [internal]
*
* Get the Attributes for a file or directory. The difference to GetAttributes()
* is that this function will also work for paths containing wildcard characters
* in its filename.
* PARAMS
* path [I] path of directory or file to check
* fileOnly [I] TRUE if only files should be found
*
* RETURNS
* INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
* the first file or directory found otherwise
*/
static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
{
WIN32_FIND_DATAW wfd;
BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
HANDLE hFind = FindFirstFileW(pName, &wfd);
TRACE("%s %d\n", debugstr_w(pName), fileOnly);
if (INVALID_HANDLE_VALUE != hFind)
{
do
{
if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
continue;
dwAttr = wfd.dwFileAttributes;
break;
}
while (FindNextFileW(hFind, &wfd));
FindClose(hFind);
}
return dwAttr;
}
/*************************************************************************
*
* SHNameTranslate HelperFunction for SHFileOperationA
*
* Translates a list of 0 terminated ASCII strings into Unicode. If *wString
* 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.
*/
static 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, (LPCSTR)*pWToFrom, 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
*
* RETURNS
* Success: ERROR_SUCCESS.
* Failure: ERROR_CANCELLED.
*
* NOTES
* exported by name
*/
int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
{
SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
int retCode = 0;
DWORD size;
LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
wString = NULL; /* we change this in SHNameTranslate */
TRACE("\n");
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 */
while (1) /* every loop calculate size, second translate also, if we have storage for this */
{
size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
if (ForFree)
{
retCode = SHFileOperationW(&nFileOp);
HeapFree(GetProcessHeap(), 0, ForFree); /* we cannot use wString, it was changed */
break;
}
else
{
wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
if (ForFree) continue;
retCode = ERROR_OUTOFMEMORY;
nFileOp.fAnyOperationsAborted = TRUE;
return retCode;
}
}
lpFileOp->hNameMappings = nFileOp.hNameMappings;
lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
return retCode;
}
#define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
typedef struct
{
DWORD attributes;
LPWSTR szDirectory;
LPWSTR szFilename;
LPWSTR szFullPath;
BOOL bFromWildcard;
BOOL bFromRelative;
BOOL bExists;
} FILE_ENTRY;
typedef struct
{
FILE_ENTRY *feFiles;
DWORD num_alloc;
DWORD dwNumFiles;
BOOL bAnyFromWildcard;
BOOL bAnyDirectories;
BOOL bAnyDontExist;
} FILE_LIST;
static inline void grow_list(FILE_LIST *list)
{
FILE_ENTRY *new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->feFiles,
list->num_alloc * 2 * sizeof(*new) );
list->feFiles = new;
list->num_alloc *= 2;
}
/* adds a file to the FILE_ENTRY struct
*/
static void add_file_to_entry(FILE_ENTRY *feFile, LPCWSTR szFile)
{
DWORD dwLen = lstrlenW(szFile) + 1;
LPCWSTR ptr;
feFile->szFullPath = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
lstrcpyW(feFile->szFullPath, szFile);
ptr = StrRChrW(szFile, NULL, '\\');
if (ptr)
{
dwLen = ptr - szFile + 1;
feFile->szDirectory = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
lstrcpynW(feFile->szDirectory, szFile, dwLen);
dwLen = lstrlenW(feFile->szFullPath) - dwLen + 1;
feFile->szFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
lstrcpyW(feFile->szFilename, ptr + 1); /* skip over backslash */
}
feFile->bFromWildcard = FALSE;
}
static LPWSTR wildcard_to_file(LPCWSTR szWildCard, LPCWSTR szFileName)
{
LPCWSTR ptr;
LPWSTR szFullPath;
DWORD dwDirLen, dwFullLen;
ptr = StrRChrW(szWildCard, NULL, '\\');
dwDirLen = ptr - szWildCard + 1;
dwFullLen = dwDirLen + lstrlenW(szFileName) + 1;
szFullPath = HeapAlloc(GetProcessHeap(), 0, dwFullLen * sizeof(WCHAR));
lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
lstrcatW(szFullPath, szFileName);
return szFullPath;
}
static void parse_wildcard_files(FILE_LIST *flList, LPCWSTR szFile, LPDWORD pdwListIndex)
{
WIN32_FIND_DATAW wfd;
HANDLE hFile = FindFirstFileW(szFile, &wfd);
FILE_ENTRY *file;
LPWSTR szFullPath;
BOOL res;
if (hFile == INVALID_HANDLE_VALUE) return;
for (res = TRUE; res; res = FindNextFileW(hFile, &wfd))
{
if (IsDotDir(wfd.cFileName)) continue;
if (*pdwListIndex >= flList->num_alloc) grow_list( flList );
szFullPath = wildcard_to_file(szFile, wfd.cFileName);
file = &flList->feFiles[(*pdwListIndex)++];
add_file_to_entry(file, szFullPath);
file->bFromWildcard = TRUE;
file->attributes = wfd.dwFileAttributes;
if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
HeapFree(GetProcessHeap(), 0, szFullPath);
}
FindClose(hFile);
}
/* takes the null-separated file list and fills out the FILE_LIST */
static HRESULT parse_file_list(FILE_LIST *flList, LPCWSTR szFiles)
{
LPCWSTR ptr = szFiles;
WCHAR szCurFile[MAX_PATH];
DWORD i = 0;
if (!szFiles)
return ERROR_INVALID_PARAMETER;
flList->bAnyFromWildcard = FALSE;
flList->bAnyDirectories = FALSE;
flList->bAnyDontExist = FALSE;
flList->num_alloc = 32;
flList->dwNumFiles = 0;
/* empty list */
if (!szFiles[0])
return ERROR_ACCESS_DENIED;
flList->feFiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
flList->num_alloc * sizeof(FILE_ENTRY));
while (*ptr)
{
if (i >= flList->num_alloc) grow_list( flList );
/* change relative to absolute path */
if (PathIsRelativeW(ptr))
{
GetCurrentDirectoryW(MAX_PATH, szCurFile);
PathCombineW(szCurFile, szCurFile, ptr);
flList->feFiles[i].bFromRelative = TRUE;
}
else
{
lstrcpyW(szCurFile, ptr);
flList->feFiles[i].bFromRelative = FALSE;
}
/* parse wildcard files if they are in the filename */
if (StrPBrkW(szCurFile, wWildcardChars))
{
parse_wildcard_files(flList, szCurFile, &i);
flList->bAnyFromWildcard = TRUE;
i--;
}
else
{
FILE_ENTRY *file = &flList->feFiles[i];
add_file_to_entry(file, szCurFile);
file->attributes = GetFileAttributesW( file->szFullPath );
file->bExists = (file->attributes != INVALID_FILE_ATTRIBUTES);
if (!file->bExists) flList->bAnyDontExist = TRUE;
if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
}
/* advance to the next string */
ptr += lstrlenW(ptr) + 1;
i++;
}
flList->dwNumFiles = i;
return S_OK;
}
/* free the FILE_LIST */
static void destroy_file_list(FILE_LIST *flList)
{
DWORD i;
if (!flList || !flList->feFiles)
return;
for (i = 0; i < flList->dwNumFiles; i++)
{
HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szDirectory);
HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFilename);
HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFullPath);
}
HeapFree(GetProcessHeap(), 0, flList->feFiles);
}
static void copy_dir_to_dir(FILE_OPERATION *op, const FILE_ENTRY *feFrom, LPCWSTR szDestPath)
{
WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
SHFILEOPSTRUCTW fileOp;
static const WCHAR wildCardFiles[] = {'*','.','*',0};
if (IsDotDir(feFrom->szFilename))
return;
if (PathFileExistsW(szDestPath))
PathCombineW(szTo, szDestPath, feFrom->szFilename);
else
lstrcpyW(szTo, szDestPath);
if (!(op->req->fFlags & FOF_NOCONFIRMATION) && PathFileExistsW(szTo)) {
if (!SHELL_ConfirmDialogW(op->req->hwnd, ASK_OVERWRITE_FOLDER, feFrom->szFilename, op))
{
/* Vista returns an ERROR_CANCELLED even if user pressed "No" */
if (!op->bManyItems)
op->bCancelled = TRUE;
return;
}
}
szTo[lstrlenW(szTo) + 1] = '\0';
SHNotifyCreateDirectoryW(szTo, NULL);
PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
szFrom[lstrlenW(szFrom) + 1] = '\0';
fileOp = *op->req;
fileOp.pFrom = szFrom;
fileOp.pTo = szTo;
fileOp.fFlags &= ~FOF_MULTIDESTFILES; /* we know we're copying to one dir */
/* Don't ask the user about overwriting files when he accepted to overwrite the
folder. FIXME: this is not exactly what Windows does - e.g. there would be
an additional confirmation for a nested folder */
fileOp.fFlags |= FOF_NOCONFIRMATION;
SHFileOperationW(&fileOp);
}
static BOOL copy_file_to_file(FILE_OPERATION *op, const WCHAR *szFrom, const WCHAR *szTo)
{
if (!(op->req->fFlags & FOF_NOCONFIRMATION) && PathFileExistsW(szTo))
{
if (!SHELL_ConfirmDialogW(op->req->hwnd, ASK_OVERWRITE_FILE, PathFindFileNameW(szTo), op))
return FALSE;
}
return SHNotifyCopyFileW(szFrom, szTo, FALSE) == 0;
}
/* copy a file or directory to another directory */
static void copy_to_dir(FILE_OPERATION *op, const FILE_ENTRY *feFrom, const FILE_ENTRY *feTo)
{
if (!PathFileExistsW(feTo->szFullPath))
SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
if (IsAttribFile(feFrom->attributes))
{
WCHAR szDestPath[MAX_PATH];
PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
copy_file_to_file(op, feFrom->szFullPath, szDestPath);
}
else if (!(op->req->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
copy_dir_to_dir(op, feFrom, feTo->szFullPath);
}
static void create_dest_dirs(LPCWSTR szDestDir)
{
WCHAR dir[MAX_PATH];
LPCWSTR ptr = StrChrW(szDestDir, '\\');
/* make sure all directories up to last one are created */
while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
{
lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
if (!PathFileExistsW(dir))
SHNotifyCreateDirectoryW(dir, NULL);
}
/* create last directory */
if (!PathFileExistsW(szDestDir))
SHNotifyCreateDirectoryW(szDestDir, NULL);
}
/* the FO_COPY operation */
static int copy_files(FILE_OPERATION *op, const FILE_LIST *flFrom, FILE_LIST *flTo)
{
DWORD i;
const FILE_ENTRY *entryToCopy;
const FILE_ENTRY *fileDest = &flTo->feFiles[0];
if (flFrom->bAnyDontExist)
return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
if (flTo->dwNumFiles == 0)
{
/* If the destination is empty, SHFileOperation should use the current directory */
WCHAR curdir[MAX_PATH+1];
GetCurrentDirectoryW(MAX_PATH, curdir);
curdir[lstrlenW(curdir)+1] = 0;
destroy_file_list(flTo);
ZeroMemory(flTo, sizeof(FILE_LIST));
parse_file_list(flTo, curdir);
fileDest = &flTo->feFiles[0];
}
if (op->req->fFlags & FOF_MULTIDESTFILES)
{
if (flFrom->bAnyFromWildcard)
return ERROR_CANCELLED;
if (flFrom->dwNumFiles != flTo->dwNumFiles)
{
if (flFrom->dwNumFiles != 1 && !IsAttribDir(fileDest->attributes))
return ERROR_CANCELLED;
/* Free all but the first entry. */
for (i = 1; i < flTo->dwNumFiles; i++)
{
HeapFree(GetProcessHeap(), 0, flTo->feFiles[i].szDirectory);
HeapFree(GetProcessHeap(), 0, flTo->feFiles[i].szFilename);
HeapFree(GetProcessHeap(), 0, flTo->feFiles[i].szFullPath);
}
flTo->dwNumFiles = 1;
}
else if (IsAttribDir(fileDest->attributes))
{
for (i = 1; i < flTo->dwNumFiles; i++)
if (!IsAttribDir(flTo->feFiles[i].attributes) ||
!IsAttribDir(flFrom->feFiles[i].attributes))
{
return ERROR_CANCELLED;
}
}
}
else if (flFrom->dwNumFiles != 1)
{
if (flTo->dwNumFiles != 1 && !IsAttribDir(fileDest->attributes))
return ERROR_CANCELLED;
if (PathFileExistsW(fileDest->szFullPath) &&
IsAttribFile(fileDest->attributes))
{
return ERROR_CANCELLED;
}
if (flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
!PathFileExistsW(fileDest->szFullPath))
{
return ERROR_CANCELLED;
}
}
for (i = 0; i < flFrom->dwNumFiles; i++)
{
entryToCopy = &flFrom->feFiles[i];
if ((op->req->fFlags & FOF_MULTIDESTFILES) &&
flTo->dwNumFiles > 1)
{
fileDest = &flTo->feFiles[i];
}
if (IsAttribDir(entryToCopy->attributes) &&
!lstrcmpiW(entryToCopy->szFullPath, fileDest->szDirectory))
{
return ERROR_SUCCESS;
}
create_dest_dirs(fileDest->szDirectory);
if (!lstrcmpiW(entryToCopy->szFullPath, fileDest->szFullPath))
{
if (IsAttribFile(entryToCopy->attributes))
return ERROR_NO_MORE_SEARCH_HANDLES;
else
return ERROR_SUCCESS;
}
if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
IsAttribDir(fileDest->attributes))
{
copy_to_dir(op, entryToCopy, fileDest);
}
else if (IsAttribDir(entryToCopy->attributes))
{
copy_dir_to_dir(op, entryToCopy, fileDest->szFullPath);
}
else
{
if (!copy_file_to_file(op, entryToCopy->szFullPath, fileDest->szFullPath))
{
op->req->fAnyOperationsAborted = TRUE;
return ERROR_CANCELLED;
}
}
/* Vista return code. XP would return e.g. ERROR_FILE_NOT_FOUND, ERROR_ALREADY_EXISTS */
if (op->bCancelled)
return ERROR_CANCELLED;
}
/* Vista return code. On XP if the used pressed "No" for the last item,
* ERROR_ARENA_TRASHED would be returned */
return ERROR_SUCCESS;
}
static BOOL confirm_delete_list(HWND hWnd, DWORD fFlags, BOOL fTrash, const FILE_LIST *flFrom)
{
if (flFrom->dwNumFiles > 1)
{
WCHAR tmp[8];
const WCHAR format[] = {'%','d',0};
wnsprintfW(tmp, sizeof(tmp)/sizeof(tmp[0]), format, flFrom->dwNumFiles);
return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_MULTIPLE_ITEM:ASK_DELETE_MULTIPLE_ITEM), tmp, NULL);
}
else
{
const FILE_ENTRY *fileEntry = &flFrom->feFiles[0];
if (IsAttribFile(fileEntry->attributes))
return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FILE:ASK_DELETE_FILE), fileEntry->szFullPath, NULL);
else if (!(fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FOLDER:ASK_DELETE_FOLDER), fileEntry->szFullPath, NULL);
}
return TRUE;
}
/* the FO_DELETE operation */
static int delete_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom)
{
const FILE_ENTRY *fileEntry;
DWORD i;
int ret;
BOOL bTrash;
if (!flFrom->dwNumFiles)
return ERROR_SUCCESS;
/* Windows also checks only the first item */
bTrash = (lpFileOp->fFlags & FOF_ALLOWUNDO)
&& TRASH_CanTrashFile(flFrom->feFiles[0].szFullPath);
if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (!bTrash && lpFileOp->fFlags & FOF_WANTNUKEWARNING))
if (!confirm_delete_list(lpFileOp->hwnd, lpFileOp->fFlags, bTrash, flFrom))
{
lpFileOp->fAnyOperationsAborted = TRUE;
return 0;
}
for (i = 0; i < flFrom->dwNumFiles; i++)
{
fileEntry = &flFrom->feFiles[i];
if (!IsAttribFile(fileEntry->attributes) &&
(lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
continue;
if (bTrash)
{
BOOL bDelete;
if (TRASH_TrashFile(fileEntry->szFullPath))
continue;
/* Note: Windows silently deletes the file in such a situation, we show a dialog */
if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (lpFileOp->fFlags & FOF_WANTNUKEWARNING))
bDelete = SHELL_ConfirmDialogW(lpFileOp->hwnd, ASK_CANT_TRASH_ITEM, fileEntry->szFullPath, NULL);
else
bDelete = TRUE;
if (!bDelete)
{
lpFileOp->fAnyOperationsAborted = TRUE;
break;
}
}
/* delete the file or directory */
if (IsAttribFile(fileEntry->attributes))
ret = DeleteFileW(fileEntry->szFullPath) ?
ERROR_SUCCESS : GetLastError();
else
ret = SHELL_DeleteDirectoryW(lpFileOp->hwnd, fileEntry->szFullPath, FALSE);
if (ret)
return ret;
}
return ERROR_SUCCESS;
}
/* moves a file or directory to another directory */
static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, const FILE_ENTRY *feFrom, const FILE_ENTRY *feTo)
{
WCHAR szDestPath[MAX_PATH];
PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
SHNotifyMoveFileW(feFrom->szFullPath, szDestPath);
}
/* the FO_MOVE operation */
static int move_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom, const FILE_LIST *flTo)
{
DWORD i;
INT mismatched = 0;
const FILE_ENTRY *entryToMove;
const FILE_ENTRY *fileDest;
if (!flFrom->dwNumFiles)
return ERROR_SUCCESS;
if (!flTo->dwNumFiles)
return ERROR_FILE_NOT_FOUND;
if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
{
return ERROR_CANCELLED;
}
if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
!flFrom->bAnyDirectories &&
flFrom->dwNumFiles > flTo->dwNumFiles)
{
return ERROR_CANCELLED;
}
if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
return ERROR_CANCELLED;
if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
mismatched = flFrom->dwNumFiles - flTo->dwNumFiles;
fileDest = &flTo->feFiles[0];
for (i = 0; i < flFrom->dwNumFiles; i++)
{
entryToMove = &flFrom->feFiles[i];
if (!PathFileExistsW(fileDest->szDirectory))
return ERROR_CANCELLED;
if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
{
if (i >= flTo->dwNumFiles)
break;
fileDest = &flTo->feFiles[i];
if (mismatched && !fileDest->bExists)
{
create_dest_dirs(flTo->feFiles[i].szFullPath);
flTo->feFiles[i].bExists = TRUE;
flTo->feFiles[i].attributes = FILE_ATTRIBUTE_DIRECTORY;
}
}
if (fileDest->bExists && IsAttribDir(fileDest->attributes))
move_to_dir(lpFileOp, entryToMove, fileDest);
else
SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath);
}
if (mismatched > 0)
{
if (flFrom->bAnyDirectories)
return DE_DESTSAMETREE;
else
return DE_SAMEFILE;
}
return ERROR_SUCCESS;
}
/* the FO_RENAME files */
static int rename_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom, const FILE_LIST *flTo)
{
const FILE_ENTRY *feFrom;
const FILE_ENTRY *feTo;
if (flFrom->dwNumFiles != 1)
return ERROR_GEN_FAILURE;
if (flTo->dwNumFiles != 1)
return ERROR_CANCELLED;
feFrom = &flFrom->feFiles[0];
feTo= &flTo->feFiles[0];
/* fail if destination doesn't exist */
if (!feFrom->bExists)
return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
/* fail if destination already exists */
if (feTo->bExists)
return ERROR_ALREADY_EXISTS;
return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath);
}
/* alert the user if an unsupported flag is used */
static void check_flags(FILEOP_FLAGS fFlags)
{
WORD wUnsupportedFlags = FOF_NO_CONNECTED_ELEMENTS |
FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE |
FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
if (fFlags & wUnsupportedFlags)
FIXME("Unsupported flags: %04x\n", fFlags);
}
/*************************************************************************
* SHFileOperationW [SHELL32.@]
*
* See SHFileOperationA
*/
int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
{
FILE_OPERATION op;
FILE_LIST flFrom, flTo;
int ret = 0;
if (!lpFileOp)
return ERROR_INVALID_PARAMETER;
check_flags(lpFileOp->fFlags);
ZeroMemory(&flFrom, sizeof(FILE_LIST));
ZeroMemory(&flTo, sizeof(FILE_LIST));
if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
return ret;
if (lpFileOp->wFunc != FO_DELETE)
parse_file_list(&flTo, lpFileOp->pTo);
ZeroMemory(&op, sizeof(op));
op.req = lpFileOp;
op.bManyItems = (flFrom.dwNumFiles > 1);
lpFileOp->fAnyOperationsAborted = FALSE;
switch (lpFileOp->wFunc)
{
case FO_COPY:
ret = copy_files(&op, &flFrom, &flTo);
break;
case FO_DELETE:
ret = delete_files(lpFileOp, &flFrom);
break;
case FO_MOVE:
ret = move_files(lpFileOp, &flFrom, &flTo);
break;
case FO_RENAME:
ret = rename_files(lpFileOp, &flFrom, &flTo);
break;
default:
ret = ERROR_INVALID_PARAMETER;
break;
}
destroy_file_list(&flFrom);
if (lpFileOp->wFunc != FO_DELETE)
destroy_file_list(&flTo);
if (ret == ERROR_CANCELLED)
lpFileOp->fAnyOperationsAborted = TRUE;
SetLastError(ERROR_SUCCESS);
return ret;
}
#define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
/*************************************************************************
* SHFreeNameMappings [shell32.246]
*
* Free the mapping handle returned by SHFileOperation if FOF_WANTSMAPPINGHANDLE
* was specified.
*
* PARAMS
* hNameMapping [I] handle to the name mappings used during renaming of files
*
* RETURNS
* Nothing
*/
void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
{
if (hNameMapping)
{
int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
for (; i>= 0; i--)
{
LPSHNAMEMAPPINGW lp = DSA_GetItemPtr(hNameMapping, i);
SHFree(lp->pszOldPath);
SHFree(lp->pszNewPath);
}
DSA_Destroy(hNameMapping);
}
}
/*************************************************************************
* SheGetDirA [SHELL32.@]
*
* drive = 0: returns the current directory path
* drive > 0: returns the current directory path of the specified drive
* drive=1 -> A: drive=2 -> B: ...
* returns 0 if successful
*/
DWORD WINAPI SheGetDirA(DWORD drive, LPSTR buffer)
{
WCHAR org_path[MAX_PATH];
DWORD ret;
char drv_path[3];
/* change current directory to the specified drive */
if (drive) {
strcpy(drv_path, "A:");
drv_path[0] += (char)drive-1;
GetCurrentDirectoryW(MAX_PATH, org_path);
SetCurrentDirectoryA(drv_path);
}
/* query current directory path of the specified drive */
ret = GetCurrentDirectoryA(MAX_PATH, buffer);
/* back to the original drive */
if (drive)
SetCurrentDirectoryW(org_path);
if (!ret)
return GetLastError();
return 0;
}
/*************************************************************************
* SheGetDirW [SHELL32.@]
*
* drive = 0: returns the current directory path
* drive > 0: returns the current directory path of the specified drive
* drive=1 -> A: drive=2 -> B: ...
* returns 0 if successful
*/
DWORD WINAPI SheGetDirW(DWORD drive, LPWSTR buffer)
{
WCHAR org_path[MAX_PATH];
DWORD ret;
char drv_path[3];
/* change current directory to the specified drive */
if (drive) {
strcpy(drv_path, "A:");
drv_path[0] += (char)drive-1;
GetCurrentDirectoryW(MAX_PATH, org_path);
SetCurrentDirectoryA(drv_path);
}
/* query current directory path of the specified drive */
ret = GetCurrentDirectoryW(MAX_PATH, buffer);
/* back to the original drive */
if (drive)
SetCurrentDirectoryW(org_path);
if (!ret)
return GetLastError();
return 0;
}
/*************************************************************************
* SheChangeDirA [SHELL32.@]
*
* changes the current directory to the specified path
* and returns 0 if successful
*/
DWORD WINAPI SheChangeDirA(LPSTR path)
{
if (SetCurrentDirectoryA(path))
return 0;
else
return GetLastError();
}
/*************************************************************************
* SheChangeDirW [SHELL32.@]
*
* changes the current directory to the specified path
* and returns 0 if successful
*/
DWORD WINAPI SheChangeDirW(LPWSTR path)
{
if (SetCurrentDirectoryW(path))
return 0;
else
return GetLastError();
}
/*************************************************************************
* IsNetDrive [SHELL32.66]
*/
int WINAPI IsNetDrive(int drive)
{
char root[4];
strcpy(root, "A:\\");
root[0] += (char)drive;
return (GetDriveTypeA(root) == DRIVE_REMOTE);
}
/*************************************************************************
* RealDriveType [SHELL32.524]
*/
int WINAPI RealDriveType(int drive, BOOL bQueryNet)
{
char root[] = "A:\\";
root[0] += (char)drive;
return GetDriveTypeA(root);
}
/***********************************************************************
* SHPathPrepareForWriteA (SHELL32.@)
*/
HRESULT WINAPI SHPathPrepareForWriteA(HWND hwnd, IUnknown *modless, LPCSTR path, DWORD flags)
{
WCHAR wpath[MAX_PATH];
MultiByteToWideChar( CP_ACP, 0, path, -1, wpath, MAX_PATH);
return SHPathPrepareForWriteW(hwnd, modless, wpath, flags);
}
/***********************************************************************
* SHPathPrepareForWriteW (SHELL32.@)
*/
HRESULT WINAPI SHPathPrepareForWriteW(HWND hwnd, IUnknown *modless, LPCWSTR path, DWORD flags)
{
DWORD res;
DWORD err;
LPCWSTR realpath;
int len;
WCHAR* last_slash;
WCHAR* temppath=NULL;
TRACE("%p %p %s 0x%08x\n", hwnd, modless, debugstr_w(path), flags);
if (flags & ~(SHPPFW_DIRCREATE|SHPPFW_ASKDIRCREATE|SHPPFW_IGNOREFILENAME))
FIXME("unimplemented flags 0x%08x\n", flags);
/* cut off filename if necessary */
if (flags & SHPPFW_IGNOREFILENAME)
{
last_slash = StrRChrW(path, NULL, '\\');
if (last_slash == NULL)
len = 1;
else
len = last_slash - path + 1;
temppath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
if (!temppath)
return E_OUTOFMEMORY;
StrCpyNW(temppath, path, len);
realpath = temppath;
}
else
{
realpath = path;
}
/* try to create the directory if asked to */
if (flags & (SHPPFW_DIRCREATE|SHPPFW_ASKDIRCREATE))
{
if (flags & SHPPFW_ASKDIRCREATE)
FIXME("treating SHPPFW_ASKDIRCREATE as SHPPFW_DIRCREATE\n");
SHCreateDirectoryExW(0, realpath, NULL);
}
/* check if we can access the directory */
res = GetFileAttributesW(realpath);
HeapFree(GetProcessHeap(), 0, temppath);
if (res == INVALID_FILE_ATTRIBUTES)
{
err = GetLastError();
if (err == ERROR_FILE_NOT_FOUND)
return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
return HRESULT_FROM_WIN32(err);
}
else if (res & FILE_ATTRIBUTE_DIRECTORY)
return S_OK;
else
return HRESULT_FROM_WIN32(ERROR_DIRECTORY);
}
|