fdopen: don't rewind the file after creating the FILE* handle. Added
[wine] / dlls / shell32 / shlfileop.c
1 /*
2  * SHFileOperation
3  *
4  * Copyright 2000 Juergen Schmied
5  * Copyright 2002 Andriy Palamarchuk
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <string.h>
26
27 #include "winreg.h"
28 #include "shellapi.h"
29 #include "shlobj.h"
30 #include "shresdef.h"
31 #include "shell32_main.h"
32 #include "undocshell.h"
33 #include "shlwapi.h"
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(shell);
37
38 BOOL SHELL_WarnItemDelete (int nKindOfDialog, LPCSTR szDir)
39 {
40         char szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
41
42         if(nKindOfDialog == ASK_DELETE_FILE)
43         {
44           LoadStringA(shell32_hInstance, IDS_DELETEITEM_TEXT, szText,
45                 sizeof(szText));
46           LoadStringA(shell32_hInstance, IDS_DELETEITEM_CAPTION,
47                 szCaption, sizeof(szCaption));
48         }
49         else if(nKindOfDialog == ASK_DELETE_FOLDER)
50         {
51           LoadStringA(shell32_hInstance, IDS_DELETEITEM_TEXT, szText,
52                 sizeof(szText));
53           LoadStringA(shell32_hInstance, IDS_DELETEFOLDER_CAPTION,
54                 szCaption, sizeof(szCaption));
55         }
56         else if(nKindOfDialog == ASK_DELETE_MULTIPLE_ITEM)
57         {
58           LoadStringA(shell32_hInstance, IDS_DELETEMULTIPLE_TEXT, szText,
59                 sizeof(szText));
60           LoadStringA(shell32_hInstance, IDS_DELETEITEM_CAPTION,
61                 szCaption, sizeof(szCaption));
62         }
63         else {
64           FIXME("Called without a valid nKindOfDialog specified!\n");
65           LoadStringA(shell32_hInstance, IDS_DELETEITEM_TEXT, szText,
66                 sizeof(szText));
67           LoadStringA(shell32_hInstance, IDS_DELETEITEM_CAPTION,
68                 szCaption, sizeof(szCaption));
69         }
70
71         FormatMessageA(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
72             szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
73
74         return (IDOK == MessageBoxA(GetActiveWindow(), szBuffer, szCaption, MB_OKCANCEL | MB_ICONEXCLAMATION));
75 }
76
77 /**************************************************************************
78  *      SHELL_DeleteDirectoryA()
79  *
80  * like rm -r
81  */
82
83 BOOL SHELL_DeleteDirectoryA(LPCSTR pszDir, BOOL bShowUI)
84 {
85         BOOL            ret = FALSE;
86         HANDLE          hFind;
87         WIN32_FIND_DATAA wfd;
88         char            szTemp[MAX_PATH];
89
90         strcpy(szTemp, pszDir);
91         PathAddBackslashA(szTemp);
92         strcat(szTemp, "*.*");
93
94         if (bShowUI && !SHELL_WarnItemDelete(ASK_DELETE_FOLDER, pszDir))
95           return FALSE;
96
97         if(INVALID_HANDLE_VALUE != (hFind = FindFirstFileA(szTemp, &wfd)))
98         {
99           do
100           {
101             if(strcasecmp(wfd.cFileName, ".") && strcasecmp(wfd.cFileName, ".."))
102             {
103               strcpy(szTemp, pszDir);
104               PathAddBackslashA(szTemp);
105               strcat(szTemp, wfd.cFileName);
106
107               if(FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
108                 SHELL_DeleteDirectoryA(szTemp, FALSE);
109               else
110                 DeleteFileA(szTemp);
111             }
112           } while(FindNextFileA(hFind, &wfd));
113
114           FindClose(hFind);
115           ret = RemoveDirectoryA(pszDir);
116         }
117
118         return ret;
119 }
120
121 /**************************************************************************
122  *      SHELL_DeleteFileA()
123  */
124
125 BOOL SHELL_DeleteFileA(LPCSTR pszFile, BOOL bShowUI)
126 {
127         if (bShowUI && !SHELL_WarnItemDelete(ASK_DELETE_FILE, pszFile))
128                 return FALSE;
129
130         return DeleteFileA(pszFile);
131 }
132
133 /*************************************************************************
134  * SHCreateDirectory                            [SHELL32.165]
135  *
136  * NOTES
137  *  exported by ordinal
138  *  not sure about LPSECURITY_ATTRIBUTES
139  */
140 DWORD WINAPI SHCreateDirectory(LPSECURITY_ATTRIBUTES sec,LPCSTR path)
141 {
142         DWORD ret;
143         TRACE("(%p,%s)\n",sec,path);
144         if ((ret = CreateDirectoryA(path,sec)))
145         {
146           SHChangeNotifyA(SHCNE_MKDIR, SHCNF_PATHA, path, NULL);
147         }
148         return ret;
149 }
150
151 /************************************************************************
152  *      Win32DeleteFile                         [SHELL32.164]
153  *
154  * Deletes a file.  Also triggers a change notify if one exists.
155  *
156  * FIXME:
157  * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be
158  * ANSI.  Is this Unicode on NT?
159  *
160  */
161
162 BOOL WINAPI Win32DeleteFile(LPSTR fName)
163 {
164         TRACE("%p(%s)\n", fName, fName);
165
166         DeleteFileA(fName);
167         SHChangeNotifyA(SHCNE_DELETE, SHCNF_PATHA, fName, NULL);
168         return TRUE;
169 }
170
171 /**************************************************************************
172  *      SHELL_FileNamesMatch()
173  *
174  * Accepts two \0 delimited lists of the file names. Checks whether number of
175  * files in the both lists is the same.
176  */
177 BOOL SHELL_FileNamesMatch(LPCSTR pszFiles1, LPCSTR pszFiles2)
178 {
179     while ((pszFiles1[strlen(pszFiles1) + 1] != '\0') &&
180            (pszFiles2[strlen(pszFiles2) + 1] != '\0'))
181     {
182         pszFiles1 += strlen(pszFiles1) + 1;
183         pszFiles2 += strlen(pszFiles2) + 1;
184     }
185
186     return
187         ((pszFiles1[strlen(pszFiles1) + 1] == '\0') &&
188          (pszFiles2[strlen(pszFiles2) + 1] == '\0')) ||
189         ((pszFiles1[strlen(pszFiles1) + 1] != '\0') &&
190          (pszFiles2[strlen(pszFiles2) + 1] != '\0'));
191 }
192
193 /*************************************************************************
194  * SHFileOperationA                             [SHELL32.@]
195  *
196  * NOTES
197  *     exported by name
198  */
199 DWORD WINAPI SHFileOperationA (LPSHFILEOPSTRUCTA lpFileOp)
200 {
201         LPSTR pFrom = (LPSTR)lpFileOp->pFrom;
202         LPSTR pTo = (LPSTR)lpFileOp->pTo;
203         LPSTR pTempTo;
204         TRACE("flags (0x%04x) : %s%s%s%s%s%s%s%s%s%s%s%s \n", lpFileOp->fFlags,
205                 lpFileOp->fFlags & FOF_MULTIDESTFILES ? "FOF_MULTIDESTFILES " : "",
206                 lpFileOp->fFlags & FOF_CONFIRMMOUSE ? "FOF_CONFIRMMOUSE " : "",
207                 lpFileOp->fFlags & FOF_SILENT ? "FOF_SILENT " : "",
208                 lpFileOp->fFlags & FOF_RENAMEONCOLLISION ? "FOF_RENAMEONCOLLISION " : "",
209                 lpFileOp->fFlags & FOF_NOCONFIRMATION ? "FOF_NOCONFIRMATION " : "",
210                 lpFileOp->fFlags & FOF_WANTMAPPINGHANDLE ? "FOF_WANTMAPPINGHANDLE " : "",
211                 lpFileOp->fFlags & FOF_ALLOWUNDO ? "FOF_ALLOWUNDO " : "",
212                 lpFileOp->fFlags & FOF_FILESONLY ? "FOF_FILESONLY " : "",
213                 lpFileOp->fFlags & FOF_SIMPLEPROGRESS ? "FOF_SIMPLEPROGRESS " : "",
214                 lpFileOp->fFlags & FOF_NOCONFIRMMKDIR ? "FOF_NOCONFIRMMKDIR " : "",
215                 lpFileOp->fFlags & FOF_NOERRORUI ? "FOF_NOERRORUI " : "",
216                 lpFileOp->fFlags & 0xf800 ? "MORE-UNKNOWN-Flags" : "");
217         switch(lpFileOp->wFunc) {
218         case FO_COPY:
219         case FO_MOVE:
220         {
221                 /* establish when pTo is interpreted as the name of the destination file
222                  * or the directory where the Fromfile should be copied to.
223                  * This depends on:
224                  * (1) pTo points to the name of an existing directory;
225                  * (2) the flag FOF_MULTIDESTFILES is present;
226                  * (3) whether pFrom point to multiple filenames.
227                  *
228                  * Some experiments:
229                  *
230                  * destisdir               1 1 1 1 0 0 0 0
231                  * FOF_MULTIDESTFILES      1 1 0 0 1 1 0 0
232                  * multiple from filenames 1 0 1 0 1 0 1 0
233                  *                         ---------------
234                  * copy files to dir       1 0 1 1 0 0 1 0
235                  * create dir              0 0 0 0 0 0 1 0
236                  */
237                 int multifrom = pFrom[strlen(pFrom) + 1] != '\0';
238                 int destisdir = PathIsDirectoryA( pTo );
239                 int todir = 0;
240
241                 if (lpFileOp->wFunc == FO_COPY)
242                     TRACE("File Copy:\n");
243                 else
244                     TRACE("File Move:\n");
245
246                 if( destisdir ) {
247                     if ( !((lpFileOp->fFlags & FOF_MULTIDESTFILES) && !multifrom))
248                         todir = 1;
249                 } else {
250                     if ( !(lpFileOp->fFlags & FOF_MULTIDESTFILES) && multifrom)
251                         todir = 1;
252                 }
253
254                 if ((pTo[strlen(pTo) + 1] != '\0') &&
255                     !(lpFileOp->fFlags & FOF_MULTIDESTFILES))
256                 {
257                     WARN("Attempt to use multiple file names as a destination "
258                          "without specifying FOF_MULTIDESTFILES\n");
259                     return 1;
260                 }
261
262                 if ((lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
263                     !SHELL_FileNamesMatch(pTo, pFrom))
264                 {
265                     WARN("Attempt to use multiple file names as a destination "
266                          "with mismatching number of files in the source and "
267                          "destination lists\n");
268                     return 1;
269                 }
270
271                 if ( todir ) {
272                     char szTempFrom[MAX_PATH];
273                     char *fromfile;
274                     int lenPTo;
275                     if ( ! destisdir) {
276                         TRACE("   creating directory %s\n",pTo);
277                         SHCreateDirectory(NULL,pTo);
278                     }
279                     lenPTo = strlen(pTo);
280                     while(1) {
281                         HANDLE hFind;
282                         WIN32_FIND_DATAA wfd;
283
284                         if(!pFrom[0]) break;
285                         TRACE("   From Pattern='%s'\n", pFrom);
286                         if(INVALID_HANDLE_VALUE != (hFind = FindFirstFileA(pFrom, &wfd)))
287                         {
288                           do
289                           {
290                             if(strcasecmp(wfd.cFileName, ".") && strcasecmp(wfd.cFileName, ".."))
291                             {
292                               strcpy(szTempFrom, pFrom);
293
294                               pTempTo = HeapAlloc(GetProcessHeap(), 0,
295                                                   lenPTo + strlen(wfd.cFileName) + 5);
296                               if (pTempTo) {
297                                   strcpy(pTempTo,pTo);
298                                   PathAddBackslashA(pTempTo);
299                                   strcat(pTempTo,wfd.cFileName);
300
301                                   fromfile = PathFindFileNameA(szTempFrom);
302                                   fromfile[0] = '\0';
303                                   PathAddBackslashA(szTempFrom);
304                                   strcat(szTempFrom, wfd.cFileName);
305                                   TRACE("   From='%s' To='%s'\n", szTempFrom, pTempTo);
306                                   if(lpFileOp->wFunc == FO_COPY)
307                                   {
308                                       if(FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
309                                       {
310                                           /* copy recursively */
311                                           if(!(lpFileOp->fFlags & FOF_FILESONLY))
312                                           {
313                                               SHFILEOPSTRUCTA shfo;
314
315                                               SHCreateDirectory(NULL,pTempTo);
316                                               PathAddBackslashA(szTempFrom);
317                                               strcat(szTempFrom, "*.*");
318                                               szTempFrom[strlen(szTempFrom) + 1] = '\0';
319                                               pTempTo[strlen(pTempTo) + 1] = '\0';
320                                               memcpy(&shfo, lpFileOp, sizeof(shfo));
321                                               shfo.pFrom = szTempFrom;
322                                               shfo.pTo = pTempTo;
323                                               SHFileOperationA(&shfo);
324
325                                               szTempFrom[strlen(szTempFrom) - 4] = '\0';
326                                           }
327                                       }
328                                       else
329                                           CopyFileA(szTempFrom, pTempTo, FALSE);
330                                   }
331                                   else
332                                   {
333                                       /* move file/directory */
334                                       MoveFileA(szTempFrom, pTempTo);
335                                   }
336                                   HeapFree(GetProcessHeap(), 0, pTempTo);
337                               }
338                             }
339                           } while(FindNextFileA(hFind, &wfd));
340                           FindClose(hFind);
341                         }
342                         else
343                         {
344                             /* can't find file with specified name */
345                             break;
346                         }
347                         pFrom += strlen(pFrom) + 1;
348                     }
349                 } else {
350                     while(1) {
351                             if(!pFrom[0]) break;
352                             if(!pTo[0]) break;
353                             TRACE("   From='%s' To='%s'\n", pFrom, pTo);
354
355                             pTempTo = HeapAlloc(GetProcessHeap(), 0, strlen(pTo)+1);
356                             if (pTempTo)
357                             {
358                                 strcpy( pTempTo, pTo );
359                                 PathRemoveFileSpecA(pTempTo);
360                                 TRACE("   Creating Directory '%s'\n", pTempTo);
361                                 SHCreateDirectory(NULL,pTempTo);
362                                 HeapFree(GetProcessHeap(), 0, pTempTo);
363                             }
364                             if (lpFileOp->wFunc == FO_COPY)
365                                 CopyFileA(pFrom, pTo, FALSE);
366                             else
367                                 MoveFileA(pFrom, pTo);
368
369                             pFrom += strlen(pFrom) + 1;
370                             pTo += strlen(pTo) + 1;
371                     }
372                 }
373                 TRACE("Setting AnyOpsAborted=FALSE\n");
374                 lpFileOp->fAnyOperationsAborted=FALSE;
375                 return 0;
376         }
377
378         case FO_DELETE:
379         {
380                 HANDLE          hFind;
381                 WIN32_FIND_DATAA wfd;
382                 char            szTemp[MAX_PATH];
383                 char            *file_name;
384
385                 TRACE("File Delete:\n");
386                 while(1) {
387                         if(!pFrom[0]) break;
388                         TRACE("   Pattern='%s'\n", pFrom);
389                         if(INVALID_HANDLE_VALUE != (hFind = FindFirstFileA(pFrom, &wfd)))
390                         {
391                           do
392                           {
393                             if(strcasecmp(wfd.cFileName, ".") && strcasecmp(wfd.cFileName, ".."))
394                             {
395                               strcpy(szTemp, pFrom);
396                               file_name = PathFindFileNameA(szTemp);
397                               file_name[0] = '\0';
398                               PathAddBackslashA(szTemp);
399                               strcat(szTemp, wfd.cFileName);
400
401                               TRACE("   File='%s'\n", szTemp);
402                               if(FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
403                               {
404                                 if(!(lpFileOp->fFlags & FOF_FILESONLY))
405                                     SHELL_DeleteDirectoryA(szTemp, FALSE);
406                               }
407                               else
408                                 DeleteFileA(szTemp);
409                             }
410                           } while(FindNextFileA(hFind, &wfd));
411
412                           FindClose(hFind);
413                         }
414                         pFrom += strlen(pFrom) + 1;
415                 }
416                 TRACE("Setting AnyOpsAborted=FALSE\n");
417                 lpFileOp->fAnyOperationsAborted=FALSE;
418                 return 0;
419         }
420
421         case FO_RENAME:
422             TRACE("File Rename:\n");
423             if (pFrom[strlen(pFrom) + 1] != '\0')
424             {
425                 WARN("Attempt to rename more than one file\n");
426                 return 1;
427             }
428             lpFileOp->fAnyOperationsAborted = FALSE;
429             TRACE("From %s, To %s\n", pFrom, pTo);
430             return !MoveFileA(pFrom, pTo);
431
432         default:
433                 FIXME("Unhandled shell file operation %d\n", lpFileOp->wFunc);
434         }
435
436         return 1;
437 }
438
439 /*************************************************************************
440  * SHFileOperationW                             [SHELL32.@]
441  *
442  * NOTES
443  *     exported by name
444  */
445 DWORD WINAPI SHFileOperationW (LPSHFILEOPSTRUCTW lpFileOp)
446 {
447         FIXME("(%p):stub.\n", lpFileOp);
448         return 1;
449 }
450
451 /*************************************************************************
452  * SHFileOperation                              [SHELL32.@]
453  *
454  */
455 DWORD WINAPI SHFileOperationAW(LPVOID lpFileOp)
456 {
457         if (SHELL_OsIsUnicode())
458           return SHFileOperationW(lpFileOp);
459         return SHFileOperationA(lpFileOp);
460 }
461
462 /*************************************************************************
463  * SheGetDirW [SHELL32.281]
464  *
465  */
466 HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
467 {       FIXME("%p %p stub\n",u,v);
468         return 0;
469 }
470
471 /*************************************************************************
472  * SheChangeDirW [SHELL32.274]
473  *
474  */
475 HRESULT WINAPI SheChangeDirW(LPWSTR u)
476 {       FIXME("(%s),stub\n",debugstr_w(u));
477         return 0;
478 }
479
480 /*************************************************************************
481  * IsNetDrive                   [SHELL32.66]
482  */
483 BOOL WINAPI IsNetDrive(DWORD drive)
484 {
485         char root[4];
486         strcpy(root, "A:\\");
487         root[0] += drive;
488         return (GetDriveTypeA(root) == DRIVE_REMOTE);
489 }