Put the UI update code for cabinet file into a separate function.
[wine] / dlls / msi / files.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2005 Aric Stewart for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21
22 /*
23  * Actions dealing with files These are
24  *
25  * InstallFiles
26  * DuplicateFiles
27  * MoveFiles (TODO)
28  * PatchFiles (TODO)
29  * RemoveDuplicateFiles(TODO)
30  * RemoveFiles(TODO)
31  */
32
33 #include <stdarg.h>
34
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winerror.h"
38 #include "wine/debug.h"
39 #include "fdi.h"
40 #include "msi.h"
41 #include "msidefs.h"
42 #include "msvcrt/fcntl.h"
43 #include "msipriv.h"
44 #include "winuser.h"
45 #include "wine/unicode.h"
46 #include "action.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(msi);
49
50 extern const WCHAR szInstallFiles[];
51 extern const WCHAR szDuplicateFiles[];
52 extern const WCHAR szMoveFiles[];
53 extern const WCHAR szPatchFiles[];
54 extern const WCHAR szRemoveDuplicateFiles[];
55 extern const WCHAR szRemoveFiles[];
56
57 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
58
59
60 /*
61  * This is a helper function for handling embedded cabinet media
62  */
63 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
64                                     WCHAR* source)
65 {
66     UINT rc;
67     USHORT* data;
68     UINT    size;
69     DWORD   write;
70     HANDLE  the_file;
71     WCHAR tmp[MAX_PATH];
72
73     rc = read_raw_stream_data(package->db,stream_name,&data,&size); 
74     if (rc != ERROR_SUCCESS)
75         return rc;
76
77     write = MAX_PATH;
78     if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
79         GetTempPathW(MAX_PATH,tmp);
80
81     GetTempFileNameW(tmp,stream_name,0,source);
82
83     track_tempfile(package,strrchrW(source,'\\'), source);
84     the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
85                            FILE_ATTRIBUTE_NORMAL, NULL);
86
87     if (the_file == INVALID_HANDLE_VALUE)
88     {
89         ERR("Unable to create file %s\n",debugstr_w(source));
90         rc = ERROR_FUNCTION_FAILED;
91         goto end;
92     }
93
94     WriteFile(the_file,data,size,&write,NULL);
95     CloseHandle(the_file);
96     TRACE("wrote %li bytes to %s\n",write,debugstr_w(source));
97 end:
98     msi_free(data);
99     return rc;
100 }
101
102
103 /* Support functions for FDI functions */
104 typedef struct
105 {
106     MSIPACKAGE* package;
107     LPCSTR cab_path;
108 } CabData;
109
110 static void * cabinet_alloc(ULONG cb)
111 {
112     return msi_alloc(cb);
113 }
114
115 static void cabinet_free(void *pv)
116 {
117     msi_free(pv);
118 }
119
120 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
121 {
122     HANDLE handle;
123     DWORD dwAccess = 0;
124     DWORD dwShareMode = 0;
125     DWORD dwCreateDisposition = OPEN_EXISTING;
126     switch (oflag & _O_ACCMODE)
127     {
128     case _O_RDONLY:
129         dwAccess = GENERIC_READ;
130         dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
131         break;
132     case _O_WRONLY:
133         dwAccess = GENERIC_WRITE;
134         dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
135         break;
136     case _O_RDWR:
137         dwAccess = GENERIC_READ | GENERIC_WRITE;
138         dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
139         break;
140     }
141     if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
142         dwCreateDisposition = CREATE_NEW;
143     else if (oflag & _O_CREAT)
144         dwCreateDisposition = CREATE_ALWAYS;
145     handle = CreateFileA( pszFile, dwAccess, dwShareMode, NULL, 
146                           dwCreateDisposition, 0, NULL );
147     if (handle == INVALID_HANDLE_VALUE)
148         return 0;
149     return (INT_PTR) handle;
150 }
151
152 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
153 {
154     HANDLE handle = (HANDLE) hf;
155     DWORD dwRead;
156     if (ReadFile(handle, pv, cb, &dwRead, NULL))
157         return dwRead;
158     return 0;
159 }
160
161 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
162 {
163     HANDLE handle = (HANDLE) hf;
164     DWORD dwWritten;
165     if (WriteFile(handle, pv, cb, &dwWritten, NULL))
166         return dwWritten;
167     return 0;
168 }
169
170 static int cabinet_close(INT_PTR hf)
171 {
172     HANDLE handle = (HANDLE) hf;
173     return CloseHandle(handle) ? 0 : -1;
174 }
175
176 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
177 {
178     HANDLE handle = (HANDLE) hf;
179     /* flags are compatible and so are passed straight through */
180     return SetFilePointer(handle, dist, NULL, seektype);
181 }
182
183 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f )
184 {
185     MSIRECORD *uirow;
186     LPWSTR uipath, p;
187
188     /* the UI chunk */
189     uirow = MSI_CreateRecord( 9 );
190     MSI_RecordSetStringW( uirow, 1, f->FileName );
191     uipath = strdupW( f->TargetPath );
192     p = strrchrW(uipath,'\\');
193     if (p)
194         p[1]=0;
195     MSI_RecordSetStringW( uirow, 9, uipath);
196     MSI_RecordSetInteger( uirow, 6, f->FileSize );
197     ui_actiondata( package, szInstallFiles, uirow);
198     msiobj_release( &uirow->hdr );
199     msi_free( uipath );
200     ui_progress( package, 2, f->FileSize, 0, 0);
201 }
202
203 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
204 {
205     switch (fdint)
206     {
207     case fdintCOPY_FILE:
208     {
209         CabData *data = (CabData*) pfdin->pv;
210         ULONG len = strlen(data->cab_path) + strlen(pfdin->psz1);
211         char *file;
212         INT_PTR handle;
213
214         LPWSTR trackname;
215         LPWSTR trackpath;
216         LPWSTR tracknametmp;
217         static const WCHAR tmpprefix[] = {'C','A','B','T','M','P','_',0};
218         LPWSTR given_file;
219
220         MSIFILE *f;
221
222         given_file = strdupAtoW(pfdin->psz1);
223         f = get_loaded_file(data->package, given_file);
224         msi_free(given_file);
225
226         if (!f)
227         {
228             ERR("Unknown File in Cabinet (%s)\n",debugstr_a(pfdin->psz1));
229             return 0;
230         }
231
232         if (!((f->State == 1 || f->State == 2)))
233         {
234             TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
235             return 0;
236         }
237
238         file = msi_alloc((len+1)*sizeof(char));
239         strcpy(file, data->cab_path);
240         strcat(file, pfdin->psz1);
241
242         TRACE("file: %s\n", debugstr_a(file));
243
244         /* track this file so it can be deleted if not installed */
245         trackpath=strdupAtoW(file);
246         tracknametmp=strdupAtoW(strrchr(file,'\\')+1);
247         trackname = msi_alloc((strlenW(tracknametmp) + 
248                                   strlenW(tmpprefix)+1) * sizeof(WCHAR));
249
250         strcpyW(trackname,tmpprefix);
251         strcatW(trackname,tracknametmp);
252
253         track_tempfile(data->package, trackname, trackpath);
254
255         msi_free(trackpath);
256         msi_free(trackname);
257         msi_free(tracknametmp);
258
259         msi_file_update_ui( data->package, f );
260
261         handle = cabinet_open(file, _O_WRONLY | _O_CREAT, 0);
262         msi_free( file );
263         return handle;
264     }
265     case fdintCLOSE_FILE_INFO:
266     {
267         FILETIME ft;
268         FILETIME ftLocal;
269         HANDLE handle = (HANDLE) pfdin->hf;
270
271         if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
272             return -1;
273         if (!LocalFileTimeToFileTime(&ft, &ftLocal))
274             return -1;
275         if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
276             return -1;
277         CloseHandle(handle);
278         return 1;
279     }
280     default:
281         return 0;
282     }
283 }
284
285 /***********************************************************************
286  *            extract_cabinet_file
287  *
288  * Extract files from a cab file.
289  */
290 static BOOL extract_cabinet_file(MSIPACKAGE* package, LPCWSTR source, 
291                                  LPCWSTR path)
292 {
293     HFDI hfdi;
294     ERF erf;
295     BOOL ret;
296     char *cabinet;
297     char *cab_path;
298     CabData data;
299
300     TRACE("Extracting %s to %s\n",debugstr_w(source), debugstr_w(path));
301
302     hfdi = FDICreate(cabinet_alloc,
303                      cabinet_free,
304                      cabinet_open,
305                      cabinet_read,
306                      cabinet_write,
307                      cabinet_close,
308                      cabinet_seek,
309                      0,
310                      &erf);
311     if (!hfdi)
312     {
313         ERR("FDICreate failed\n");
314         return FALSE;
315     }
316
317     if (!(cabinet = strdupWtoA( source )))
318     {
319         FDIDestroy(hfdi);
320         return FALSE;
321     }
322     if (!(cab_path = strdupWtoA( path )))
323     {
324         FDIDestroy(hfdi);
325         msi_free(cabinet);
326         return FALSE;
327     }
328
329     data.package = package;
330     data.cab_path = cab_path;
331
332     ret = FDICopy(hfdi, cabinet, "", 0, cabinet_notify, NULL, &data);
333
334     if (!ret)
335         ERR("FDICopy failed\n");
336
337     FDIDestroy(hfdi);
338
339     msi_free(cabinet);
340     msi_free(cab_path);
341
342     return ret;
343 }
344
345 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, MSICOMPONENT*
346         comp, LPCWSTR path)
347 {
348     if (file->Attributes & msidbFileAttributesNoncompressed)
349     {
350         LPWSTR p;
351         p = resolve_folder(package, comp->Directory, TRUE, FALSE, NULL);
352         file->SourcePath = build_directory_name(2, p, file->ShortName);
353         msi_free(p);
354     }
355     else
356         file->SourcePath = build_directory_name(2, path, file->File);
357 }
358
359 static BOOL check_volume(LPCWSTR path, LPCWSTR want_volume, LPWSTR volume, 
360         UINT *intype)
361 {
362     WCHAR drive[4];
363     WCHAR name[MAX_PATH];
364     UINT type;
365
366     if (!(path[0] && path[1] == ':'))
367         return TRUE;
368
369     drive[0] = path[0];
370     drive[1] = path[1];
371     drive[2] = '\\';
372     drive[3] = 0;
373     TRACE("Checking volume %s .. (%s)\n",debugstr_w(drive), debugstr_w(want_volume));
374     type = GetDriveTypeW(drive);
375     TRACE("drive is of type %x\n",type);
376
377     if (type == DRIVE_UNKNOWN || type == DRIVE_NO_ROOT_DIR || 
378             type == DRIVE_FIXED || type == DRIVE_RAMDISK)
379         return TRUE;
380
381     GetVolumeInformationW(drive, name, MAX_PATH, NULL, NULL, NULL, NULL, 0);
382     TRACE("Drive contains %s\n", debugstr_w(name));
383     volume = strdupW(name);
384     if (*intype)
385         *intype=type;
386     return (strcmpiW(want_volume,name)==0);
387 }
388
389 static BOOL check_for_sourcefile(LPCWSTR source)
390 {
391     DWORD attrib = GetFileAttributesW(source);
392     return (!(attrib == INVALID_FILE_ATTRIBUTES));
393 }
394
395 static UINT ready_volume(MSIPACKAGE* package, LPCWSTR path, LPWSTR last_volume, 
396                          MSIRECORD *row,UINT *type )
397 {
398     LPWSTR volume = NULL; 
399     LPCWSTR want_volume = MSI_RecordGetString(row, 5);
400     BOOL ok = check_volume(path, want_volume, volume, type);
401
402     TRACE("Readying Volume for %s (%s, %s)\n",debugstr_w(path), debugstr_w(want_volume), debugstr_w(last_volume));
403
404     if (check_for_sourcefile(path) && !ok)
405     {
406         FIXME("Found the Sourcefile but not on the correct volume.(%s,%s,%s)\n",
407                 debugstr_w(path),debugstr_w(want_volume), debugstr_w(volume));
408         return ERROR_SUCCESS;
409     }
410
411     while (!ok)
412     {
413         INT rc;
414         LPCWSTR prompt;
415         LPWSTR msg;
416       
417         prompt = MSI_RecordGetString(row,3);
418         msg = generate_error_string(package, 1302, 1, prompt);
419         rc = MessageBoxW(NULL,msg,NULL,MB_OKCANCEL);
420         msi_free(volume);
421         msi_free(msg);
422         if (rc == IDOK)
423             ok = check_for_sourcefile(path);
424         else
425             return ERROR_INSTALL_USEREXIT;
426     }
427
428     msi_free(last_volume);
429     last_volume = strdupW(volume);
430     return ERROR_SUCCESS;
431 }
432
433 struct media_info {
434     UINT last_sequence; 
435     LPWSTR last_volume;
436     LPWSTR last_path;
437     DWORD count;
438     WCHAR source[MAX_PATH];
439 };
440
441 static struct media_info *create_media_info( void )
442 {
443     struct media_info *mi;
444
445     mi = msi_alloc( sizeof *mi  );
446     if (mi)
447     {
448         mi->last_sequence = 0; 
449         mi->last_volume = NULL;
450         mi->last_path = NULL;
451         mi->count = 0;
452         mi->source[0] = 0;
453     }
454
455     return mi;
456 }
457
458 static void free_media_info( struct media_info *mi )
459 {
460     msi_free( mi->last_path );
461     msi_free( mi );
462 }
463
464 static UINT ready_media_for_file( MSIPACKAGE *package, struct media_info *mi,
465                                   MSIFILE *file )
466 {
467     UINT rc = ERROR_SUCCESS;
468     MSIRECORD * row = 0;
469     static const WCHAR ExecSeqQuery[] =
470         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
471          '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
472          '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
473          ' ','%', 'i',' ','O','R','D','E','R',' ','B','Y',' ',
474          '`','L','a','s','t','S','e','q','u','e','n','c','e','`',0};
475     LPCWSTR cab, volume;
476     DWORD sz;
477     INT seq;
478     UINT type;
479     LPCWSTR prompt;
480     MSICOMPONENT *comp = file->Component;
481
482     if (file->Sequence <= mi->last_sequence)
483     {
484         set_file_source(package,file,comp,mi->last_path);
485         TRACE("Media already ready (%u, %u)\n",file->Sequence,mi->last_sequence);
486         return ERROR_SUCCESS;
487     }
488
489     mi->count ++;
490     row = MSI_QueryGetRecord(package->db, ExecSeqQuery, file->Sequence);
491     if (!row)
492     {
493         TRACE("Unable to query row\n");
494         return ERROR_FUNCTION_FAILED;
495     }
496
497     seq = MSI_RecordGetInteger(row,2);
498     mi->last_sequence = seq;
499
500     volume = MSI_RecordGetString(row, 5);
501     prompt = MSI_RecordGetString(row, 3);
502
503     msi_free(mi->last_path);
504     mi->last_path = NULL;
505
506     if (file->Attributes & msidbFileAttributesNoncompressed)
507     {
508         mi->last_path = resolve_folder(package, comp->Directory, TRUE, FALSE, NULL);
509         set_file_source(package,file,comp,mi->last_path);
510         rc = ready_volume(package, file->SourcePath, mi->last_volume, row,&type);
511
512         MsiSourceListAddMediaDiskW(package->ProductCode, NULL, 
513             MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count, volume,
514             prompt);
515
516         if (type == DRIVE_REMOVABLE || type == DRIVE_CDROM || 
517                 type == DRIVE_RAMDISK)
518             MsiSourceListSetInfoW(package->ProductCode, NULL, 
519                 MSIINSTALLCONTEXT_USERMANAGED, 
520                 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
521                 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
522         else
523             MsiSourceListSetInfoW(package->ProductCode, NULL, 
524                 MSIINSTALLCONTEXT_USERMANAGED, 
525                 MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
526                 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
527         msiobj_release(&row->hdr);
528         return rc;
529     }
530
531     cab = MSI_RecordGetString(row,4);
532     if (cab)
533     {
534         TRACE("Source is CAB %s\n",debugstr_w(cab));
535         /* the stream does not contain the # character */
536         if (cab[0]=='#')
537         {
538             LPWSTR path;
539
540             writeout_cabinet_stream(package,&cab[1],mi->source);
541             mi->last_path = strdupW(mi->source);
542             *(strrchrW(mi->last_path,'\\')+1)=0;
543
544             path = msi_dup_property( package, cszSourceDir );
545
546             MsiSourceListAddMediaDiskW(package->ProductCode, NULL, 
547                 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count,
548                 volume, prompt);
549
550             MsiSourceListSetInfoW(package->ProductCode, NULL,
551                 MSIINSTALLCONTEXT_USERMANAGED,
552                 MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
553                 INSTALLPROPERTY_LASTUSEDSOURCEW, path);
554
555             msi_free(path);
556         }
557         else
558         {
559             sz = MAX_PATH;
560             mi->last_path = msi_alloc(MAX_PATH*sizeof(WCHAR));
561             if (MSI_GetPropertyW(package, cszSourceDir, mi->source, &sz))
562             {
563                 ERR("No Source dir defined \n");
564                 rc = ERROR_FUNCTION_FAILED;
565             }
566             else
567             {
568                 strcpyW(mi->last_path,mi->source);
569                 strcatW(mi->source,cab);
570
571                 rc = ready_volume(package, mi->source, mi->last_volume, row, &type);
572                 if (type == DRIVE_REMOVABLE || type == DRIVE_CDROM || 
573                         type == DRIVE_RAMDISK)
574                     MsiSourceListSetInfoW(package->ProductCode, NULL,
575                             MSIINSTALLCONTEXT_USERMANAGED,
576                             MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
577                             INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
578                 else
579                     MsiSourceListSetInfoW(package->ProductCode, NULL,
580                             MSIINSTALLCONTEXT_USERMANAGED,
581                             MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
582                             INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
583
584                 /* extract the cab file into a folder in the temp folder */
585                 sz = MAX_PATH;
586                 if (MSI_GetPropertyW(package, cszTempFolder,mi->last_path, &sz) 
587                                     != ERROR_SUCCESS)
588                     GetTempPathW(MAX_PATH,mi->last_path);
589             }
590         }
591         rc = !extract_cabinet_file(package, mi->source, mi->last_path);
592     }
593     else
594     {
595         sz = MAX_PATH;
596         mi->last_path = msi_alloc(MAX_PATH*sizeof(WCHAR));
597         MSI_GetPropertyW(package,cszSourceDir,mi->source,&sz);
598         strcpyW(mi->last_path,mi->source);
599         rc = ready_volume(package, mi->last_path, mi->last_volume, row, &type);
600
601         if (type == DRIVE_REMOVABLE || type == DRIVE_CDROM || 
602                 type == DRIVE_RAMDISK)
603             MsiSourceListSetInfoW(package->ProductCode, NULL,
604                     MSIINSTALLCONTEXT_USERMANAGED,
605                     MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
606                     INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
607         else
608             MsiSourceListSetInfoW(package->ProductCode, NULL,
609                     MSIINSTALLCONTEXT_USERMANAGED,
610                     MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
611                     INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
612     }
613     set_file_source(package, file, comp, mi->last_path);
614
615     MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
616             MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count, volume,
617             prompt);
618
619     msiobj_release(&row->hdr);
620
621     return rc;
622 }
623
624 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key, 
625                                    LPWSTR* file_source)
626 {
627     MSIFILE *file;
628
629     if (!package)
630         return ERROR_INVALID_HANDLE;
631
632     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
633     {
634         if (lstrcmpW( file_key, file->File )==0)
635         {
636             if (file->State >= 2)
637             {
638                 *file_source = strdupW( file->TargetPath );
639                 return ERROR_SUCCESS;
640             }
641             else
642                 return ERROR_FILE_NOT_FOUND;
643         }
644     }
645
646     return ERROR_FUNCTION_FAILED;
647 }
648
649 /*
650  * In order to make this work more effeciencly I am going to do this in 2
651  * passes.
652  * Pass 1) Correct all the TargetPaths and determin what files are to be
653  * installed.
654  * Pass 2) Extract Cabinents and copy files.
655  */
656 UINT ACTION_InstallFiles(MSIPACKAGE *package)
657 {
658     struct media_info *mi;
659     UINT rc = ERROR_SUCCESS;
660     LPWSTR ptr;
661     MSIFILE *file;
662
663     if (!package)
664         return ERROR_INVALID_HANDLE;
665
666     /* increment progress bar each time action data is sent */
667     ui_progress(package,1,1,0,0);
668
669     /* handle the keys for the SouceList */
670     ptr = strrchrW(package->PackagePath,'\\');
671     if (ptr)
672     {
673         ptr ++;
674         MsiSourceListSetInfoW(package->ProductCode, NULL,
675                 MSIINSTALLCONTEXT_USERMANAGED,
676                 MSICODE_PRODUCT,
677                 INSTALLPROPERTY_PACKAGENAMEW, ptr);
678     }
679     FIXME("Write DiskPrompt\n");
680     
681     /* Pass 1 */
682     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
683     {
684         if (!ACTION_VerifyComponentForAction(package, file->Component, 
685                                        INSTALLSTATE_LOCAL))
686         {
687             ui_progress(package,2,file->FileSize,0,0);
688             TRACE("File %s is not scheduled for install\n",
689                    debugstr_w(file->File));
690
691             file->State = 5;
692         }
693     }
694
695     mi = create_media_info();
696
697     /* Pass 2 */
698     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
699     {
700         if ((file->State == 1) || (file->State == 2))
701         {
702             TRACE("Pass 2: %s\n",debugstr_w(file->File));
703
704             rc = ready_media_for_file( package, mi, file );
705             if (rc != ERROR_SUCCESS)
706             {
707                 ERR("Unable to ready media\n");
708                 rc = ERROR_FUNCTION_FAILED;
709                 break;
710             }
711
712             TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
713                   debugstr_w(file->TargetPath));
714
715             if (file->Attributes & msidbFileAttributesNoncompressed)
716                 rc = CopyFileW(file->SourcePath,file->TargetPath,FALSE);
717             else
718                 rc = MoveFileW(file->SourcePath, file->TargetPath);
719
720             if (!rc)
721             {
722                 rc = GetLastError();
723                 ERR("Unable to move/copy file (%s -> %s) (error %d)\n",
724                      debugstr_w(file->SourcePath), debugstr_w(file->TargetPath),
725                       rc);
726                 if (rc == ERROR_ALREADY_EXISTS && file->State == 2)
727                 {
728                     if (!CopyFileW(file->SourcePath,file->TargetPath,FALSE))
729                         ERR("Unable to copy file (%s -> %s) (error %ld)\n",
730                             debugstr_w(file->SourcePath), 
731                             debugstr_w(file->TargetPath), GetLastError());
732                     if (!(file->Attributes & msidbFileAttributesNoncompressed))
733                         DeleteFileW(file->SourcePath);
734                     rc = 0;
735                 }
736                 else if (rc == ERROR_FILE_NOT_FOUND)
737                 {
738                     ERR("Source File Not Found!  Continuing\n");
739                     rc = 0;
740                 }
741                 else if (file->Attributes & msidbFileAttributesVital)
742                 {
743                     ERR("Ignoring Error and continuing (nonvital file)...\n");
744                     rc = 0;
745                 }
746             }
747             else
748             {
749                 file->State = 4;
750                 rc = ERROR_SUCCESS;
751             }
752         }
753     }
754
755     /* cleanup */
756     free_media_info( mi );
757     return rc;
758 }
759
760 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
761 {
762     MSIPACKAGE *package = (MSIPACKAGE*)param;
763     WCHAR *file_source = NULL;
764     WCHAR dest_name[0x100];
765     LPWSTR dest_path, dest;
766     LPCWSTR file_key, component;
767     DWORD sz;
768     DWORD rc;
769     MSICOMPONENT *comp;
770
771     component = MSI_RecordGetString(row,2);
772     comp = get_loaded_component(package,component);
773
774     if (!ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_LOCAL))
775     {
776         TRACE("Skipping copy due to disabled component %s\n",
777                         debugstr_w(component));
778
779         /* the action taken was the same as the current install state */        
780         comp->Action = comp->Installed;
781
782         return ERROR_SUCCESS;
783     }
784
785     comp->Action = INSTALLSTATE_LOCAL;
786
787     file_key = MSI_RecordGetString(row,3);
788     if (!file_key)
789     {
790         ERR("Unable to get file key\n");
791         return ERROR_FUNCTION_FAILED;
792     }
793
794     rc = get_file_target(package,file_key,&file_source);
795
796     if (rc != ERROR_SUCCESS)
797     {
798         ERR("Original file unknown %s\n",debugstr_w(file_key));
799         msi_free(file_source);
800         return ERROR_SUCCESS;
801     }
802
803     if (MSI_RecordIsNull(row,4))
804         strcpyW(dest_name,strrchrW(file_source,'\\')+1);
805     else
806     {
807         sz=0x100;
808         MSI_RecordGetStringW(row,4,dest_name,&sz);
809         reduce_to_longfilename(dest_name);
810     }
811
812     if (MSI_RecordIsNull(row,5))
813     {
814         LPWSTR p;
815         dest_path = strdupW(file_source);
816         p = strrchrW(dest_path,'\\');
817         if (p)
818             *p=0;
819     }
820     else
821     {
822         LPCWSTR destkey;
823         destkey = MSI_RecordGetString(row,5);
824         dest_path = resolve_folder(package, destkey, FALSE,FALSE,NULL);
825         if (!dest_path)
826         {
827             /* try a Property */
828             dest_path = msi_dup_property( package, destkey );
829             if (!dest_path)
830             {
831                 FIXME("Unable to get destination folder, try AppSearch properties\n");
832                 msi_free(file_source);
833                 return ERROR_SUCCESS;
834             }
835         }
836     }
837
838     dest = build_directory_name(2, dest_path, dest_name);
839
840     TRACE("Duplicating file %s to %s\n",debugstr_w(file_source),
841                     debugstr_w(dest)); 
842
843     if (strcmpW(file_source,dest))
844         rc = !CopyFileW(file_source,dest,TRUE);
845     else
846         rc = ERROR_SUCCESS;
847
848     if (rc != ERROR_SUCCESS)
849         ERR("Failed to copy file %s -> %s, last error %ld\n", debugstr_w(file_source), debugstr_w(dest_path), GetLastError());
850
851     FIXME("We should track these duplicate files as well\n");   
852
853     msi_free(dest_path);
854     msi_free(dest);
855     msi_free(file_source);
856
857     return ERROR_SUCCESS;
858 }
859
860 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
861 {
862     UINT rc;
863     MSIQUERY * view;
864     static const WCHAR ExecSeqQuery[] =
865         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
866          '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
867
868     if (!package)
869         return ERROR_INVALID_HANDLE;
870
871     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
872     if (rc != ERROR_SUCCESS)
873         return ERROR_SUCCESS;
874
875     rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
876     msiobj_release(&view->hdr);
877
878     return rc;
879 }