msi: Don't leak row handles.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "winreg.h"
46 #include "shlwapi.h"
47 #include "wine/unicode.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(msi);
50
51 extern const WCHAR szInstallFiles[];
52 extern const WCHAR szDuplicateFiles[];
53 extern const WCHAR szMoveFiles[];
54 extern const WCHAR szPatchFiles[];
55 extern const WCHAR szRemoveDuplicateFiles[];
56 extern const WCHAR szRemoveFiles[];
57
58 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
59
60 struct media_info {
61     UINT disk_id;
62     UINT last_sequence;
63     LPWSTR disk_prompt;
64     LPWSTR cabinet;
65     LPWSTR volume_label;
66     BOOL is_continuous;
67     WCHAR source[MAX_PATH];
68 };
69
70 static UINT msi_change_media( MSIPACKAGE *package, struct media_info *mi )
71 {
72     LPSTR msg;
73     LPWSTR error, error_dialog;
74     UINT r = ERROR_SUCCESS;
75
76     static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
77     static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
78
79     if ( msi_get_property_int(package, szUILevel, 0) == INSTALLUILEVEL_NONE && !gUIHandlerA )
80         return ERROR_SUCCESS;
81
82     error = generate_error_string( package, 1302, 1, mi->disk_prompt );
83     error_dialog = msi_dup_property( package, error_prop );
84
85     while ( r == ERROR_SUCCESS && GetFileAttributesW( mi->source ) == INVALID_FILE_ATTRIBUTES )
86     {
87         r = msi_spawn_error_dialog( package, error_dialog, error );
88
89         if (gUIHandlerA)
90         {
91             msg = strdupWtoA( error );
92             gUIHandlerA( gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg );
93             msi_free(msg);
94         }
95     }
96
97     msi_free( error );
98     msi_free( error_dialog );
99
100     return r;
101 }
102
103 /*
104  * This is a helper function for handling embedded cabinet media
105  */
106 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
107                                     WCHAR* source)
108 {
109     UINT rc;
110     USHORT* data;
111     UINT    size;
112     DWORD   write;
113     HANDLE  the_file;
114     WCHAR tmp[MAX_PATH];
115
116     rc = read_raw_stream_data(package->db,stream_name,&data,&size); 
117     if (rc != ERROR_SUCCESS)
118         return rc;
119
120     write = MAX_PATH;
121     if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
122         GetTempPathW(MAX_PATH,tmp);
123
124     GetTempFileNameW(tmp,stream_name,0,source);
125
126     track_tempfile(package, source);
127     the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
128                            FILE_ATTRIBUTE_NORMAL, NULL);
129
130     if (the_file == INVALID_HANDLE_VALUE)
131     {
132         ERR("Unable to create file %s\n",debugstr_w(source));
133         rc = ERROR_FUNCTION_FAILED;
134         goto end;
135     }
136
137     WriteFile(the_file,data,size,&write,NULL);
138     CloseHandle(the_file);
139     TRACE("wrote %i bytes to %s\n",write,debugstr_w(source));
140 end:
141     msi_free(data);
142     return rc;
143 }
144
145
146 /* Support functions for FDI functions */
147 typedef struct
148 {
149     MSIPACKAGE* package;
150     struct media_info *mi;
151 } CabData;
152
153 static void * cabinet_alloc(ULONG cb)
154 {
155     return msi_alloc(cb);
156 }
157
158 static void cabinet_free(void *pv)
159 {
160     msi_free(pv);
161 }
162
163 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
164 {
165     HANDLE handle;
166     DWORD dwAccess = 0;
167     DWORD dwShareMode = 0;
168     DWORD dwCreateDisposition = OPEN_EXISTING;
169     switch (oflag & _O_ACCMODE)
170     {
171     case _O_RDONLY:
172         dwAccess = GENERIC_READ;
173         dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
174         break;
175     case _O_WRONLY:
176         dwAccess = GENERIC_WRITE;
177         dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
178         break;
179     case _O_RDWR:
180         dwAccess = GENERIC_READ | GENERIC_WRITE;
181         dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
182         break;
183     }
184     if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
185         dwCreateDisposition = CREATE_NEW;
186     else if (oflag & _O_CREAT)
187         dwCreateDisposition = CREATE_ALWAYS;
188     handle = CreateFileA( pszFile, dwAccess, dwShareMode, NULL, 
189                           dwCreateDisposition, 0, NULL );
190     if (handle == INVALID_HANDLE_VALUE)
191         return 0;
192     return (INT_PTR) handle;
193 }
194
195 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
196 {
197     HANDLE handle = (HANDLE) hf;
198     DWORD dwRead;
199     if (ReadFile(handle, pv, cb, &dwRead, NULL))
200         return dwRead;
201     return 0;
202 }
203
204 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
205 {
206     HANDLE handle = (HANDLE) hf;
207     DWORD dwWritten;
208     if (WriteFile(handle, pv, cb, &dwWritten, NULL))
209         return dwWritten;
210     return 0;
211 }
212
213 static int cabinet_close(INT_PTR hf)
214 {
215     HANDLE handle = (HANDLE) hf;
216     return CloseHandle(handle) ? 0 : -1;
217 }
218
219 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
220 {
221     HANDLE handle = (HANDLE) hf;
222     /* flags are compatible and so are passed straight through */
223     return SetFilePointer(handle, dist, NULL, seektype);
224 }
225
226 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
227 {
228     MSIRECORD *uirow;
229     LPWSTR uipath, p;
230
231     /* the UI chunk */
232     uirow = MSI_CreateRecord( 9 );
233     MSI_RecordSetStringW( uirow, 1, f->FileName );
234     uipath = strdupW( f->TargetPath );
235     p = strrchrW(uipath,'\\');
236     if (p)
237         p[1]=0;
238     MSI_RecordSetStringW( uirow, 9, uipath);
239     MSI_RecordSetInteger( uirow, 6, f->FileSize );
240     ui_actiondata( package, action, uirow);
241     msiobj_release( &uirow->hdr );
242     msi_free( uipath );
243     ui_progress( package, 2, f->FileSize, 0, 0);
244 }
245
246 static UINT msi_media_get_disk_info( CabData *data )
247 {
248     MSIPACKAGE *package = data->package;
249     MSIRECORD *row;
250     LPWSTR ptr;
251
252     static const WCHAR query[] =
253         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
254          '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
255          '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
256
257     row = MSI_QueryGetRecord(package->db, query, data->mi->disk_id);
258     if (!row)
259     {
260         TRACE("Unable to query row\n");
261         return ERROR_FUNCTION_FAILED;
262     }
263
264     data->mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
265     data->mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
266
267     ptr = strrchrW(data->mi->source, '\\') + 1;
268     lstrcpyW(ptr, data->mi->cabinet);
269     msiobj_release(&row->hdr);
270
271     return ERROR_SUCCESS;
272 }
273
274 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
275 {
276     TRACE("(%d)\n", fdint);
277
278     switch (fdint)
279     {
280     case fdintPARTIAL_FILE:
281     {
282         CabData *data = (CabData *)pfdin->pv;
283         data->mi->is_continuous = FALSE;
284         return 0;
285     }
286     case fdintNEXT_CABINET:
287     {
288         CabData *data = (CabData *)pfdin->pv;
289         struct media_info *mi = data->mi;
290         LPWSTR cab = strdupAtoW(pfdin->psz1);
291         UINT rc;
292
293         msi_free(mi->disk_prompt);
294
295         mi->disk_id++;
296         mi->is_continuous = TRUE;
297
298         rc = msi_media_get_disk_info(data);
299         if (rc != ERROR_SUCCESS)
300         {
301             ERR("Failed to get next cabinet information: %d\n", rc);
302             return -1;
303         }
304
305         if (lstrcmpiW(mi->cabinet, cab))
306         {
307             msi_free(cab);
308             ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
309             return -1;
310         }
311
312         msi_free(cab);
313
314         TRACE("Searching for %s\n", debugstr_w(mi->source));
315
316         if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
317             rc = msi_change_media(data->package, mi);
318
319         if (rc != ERROR_SUCCESS)
320             return -1;
321
322         return 0;
323     }
324     case fdintCOPY_FILE:
325     {
326         CabData *data = (CabData*) pfdin->pv;
327         HANDLE handle;
328         LPWSTR file;
329         MSIFILE *f;
330         DWORD attrs;
331
332         file = strdupAtoW(pfdin->psz1);
333         f = get_loaded_file(data->package, file);
334         msi_free(file);
335
336         if (!f)
337         {
338             WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
339             return 0;
340         }
341
342         if (f->state != msifs_missing && f->state != msifs_overwrite)
343         {
344             TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
345             return 0;
346         }
347
348         msi_file_update_ui( data->package, f, szInstallFiles );
349
350         TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
351
352         attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
353         if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
354
355         handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
356                               NULL, CREATE_ALWAYS, attrs, NULL );
357         if ( handle == INVALID_HANDLE_VALUE )
358         {
359             ERR("failed to create %s (error %d)\n",
360                 debugstr_w( f->TargetPath ), GetLastError() );
361             return 0;
362         }
363
364         f->state = msifs_installed;
365         return (INT_PTR) handle;
366     }
367     case fdintCLOSE_FILE_INFO:
368     {
369         FILETIME ft;
370         FILETIME ftLocal;
371         HANDLE handle = (HANDLE) pfdin->hf;
372
373         if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
374             return -1;
375         if (!LocalFileTimeToFileTime(&ft, &ftLocal))
376             return -1;
377         if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
378             return -1;
379         CloseHandle(handle);
380         return 1;
381     }
382     default:
383         return 0;
384     }
385 }
386
387 /***********************************************************************
388  *            extract_cabinet_file
389  *
390  * Extract files from a cab file.
391  */
392 static BOOL extract_cabinet_file(MSIPACKAGE* package, struct media_info *mi)
393 {
394     LPSTR cabinet, cab_path = NULL;
395     LPWSTR ptr;
396     HFDI hfdi;
397     ERF erf;
398     BOOL ret = FALSE;
399     CabData data;
400
401     TRACE("Extracting %s\n", debugstr_w(mi->source));
402
403     hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
404                      cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
405     if (!hfdi)
406     {
407         ERR("FDICreate failed\n");
408         return FALSE;
409     }
410
411     ptr = strrchrW(mi->source, '\\') + 1;
412     cabinet = strdupWtoA(ptr);
413     if (!cabinet)
414         goto done;
415
416     cab_path = strdupWtoA(mi->source);
417     if (!cab_path)
418         goto done;
419
420     cab_path[ptr - mi->source] = '\0';
421
422     data.package = package;
423     data.mi = mi;
424
425     ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, &data);
426     if (!ret)
427         ERR("FDICopy failed\n");
428
429 done:
430     FDIDestroy(hfdi);
431     msi_free(cabinet);
432     msi_free(cab_path);
433
434     return ret;
435 }
436
437 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, LPCWSTR path)
438 {
439     if (!file->IsCompressed)
440     {
441         LPWSTR p, path;
442         p = resolve_folder(package, file->Component->Directory, TRUE, FALSE, NULL);
443         path = build_directory_name(2, p, file->ShortName);
444         if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
445         {
446             msi_free(path);
447             path = build_directory_name(2, p, file->LongName);
448         }
449         file->SourcePath = path;
450         msi_free(p);
451     }
452     else
453         file->SourcePath = build_directory_name(2, path, file->File);
454 }
455
456 static void free_media_info( struct media_info *mi )
457 {
458     msi_free( mi->disk_prompt );
459     msi_free( mi->cabinet );
460     msi_free( mi->volume_label );
461     msi_free( mi );
462 }
463
464 static UINT download_remote_cabinet(MSIPACKAGE *package, struct media_info *mi)
465 {
466     WCHAR temppath[MAX_PATH];
467     LPWSTR src, ptr;
468     LPCWSTR cab;
469
470     src = strdupW(package->BaseURL);
471     if (!src)
472         return ERROR_OUTOFMEMORY;
473
474     ptr = strrchrW(src, '/');
475     if (!ptr)
476     {
477         msi_free(src);
478         return ERROR_FUNCTION_FAILED;
479     }
480
481     *(ptr + 1) = '\0';
482     ptr = strrchrW(mi->source, '\\');
483     src = msi_realloc(src, (lstrlenW(src) + lstrlenW(ptr)) * sizeof(WCHAR));
484     if (!src)
485         return ERROR_OUTOFMEMORY;
486
487     lstrcatW(src, ptr + 1);
488
489     temppath[0] = '\0';
490     cab = msi_download_file(src, temppath);
491     lstrcpyW(mi->source, cab);
492
493     msi_free(src);
494     return ERROR_SUCCESS;
495 }
496
497 static UINT load_media_info(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
498 {
499     MSIRECORD *row;
500     LPWSTR source_dir;
501     UINT r;
502
503     static const WCHAR query[] = {
504         'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
505         '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
506         '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
507         ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
508         ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
509         '`','D','i','s','k','I','d','`',0
510     };
511
512     row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
513     if (!row)
514     {
515         TRACE("Unable to query row\n");
516         return ERROR_FUNCTION_FAILED;
517     }
518
519     mi->disk_id = MSI_RecordGetInteger(row, 1);
520     mi->last_sequence = MSI_RecordGetInteger(row, 2);
521     mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
522     mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
523     mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
524     msiobj_release(&row->hdr);
525
526     source_dir = msi_dup_property(package, cszSourceDir);
527
528     if (mi->cabinet && mi->cabinet[0] == '#')
529     {
530         r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
531         if (r != ERROR_SUCCESS)
532         {
533             ERR("Failed to extract cabinet stream\n");
534             return ERROR_FUNCTION_FAILED;
535         }
536     }
537     else
538     {
539         lstrcpyW(mi->source, source_dir);
540
541
542         if (mi->cabinet)
543             lstrcatW(mi->source, mi->cabinet);
544     }
545
546     MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
547         MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT,
548         mi->disk_id, mi->volume_label, mi->disk_prompt);
549
550     MsiSourceListSetInfoW(package->ProductCode, NULL,
551         MSIINSTALLCONTEXT_USERMANAGED,
552         MSICODE_PRODUCT | MSISOURCETYPE_MEDIA,
553         INSTALLPROPERTY_LASTUSEDSOURCEW, mi->source);
554
555     msi_free(source_dir);
556     return ERROR_SUCCESS;
557 }
558
559 static UINT ready_media(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
560 {
561     UINT rc = ERROR_SUCCESS;
562     BOOL found = FALSE;
563
564     /* media info for continuous cabinet is already loaded */
565     if (mi->is_continuous)
566         return ERROR_SUCCESS;
567
568     rc = load_media_info(package, file, mi);
569     if (rc != ERROR_SUCCESS)
570     {
571         ERR("Unable to load media info\n");
572         return ERROR_FUNCTION_FAILED;
573     }
574
575     if (file->IsCompressed &&
576         GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
577     {
578         if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
579         {
580             rc = download_remote_cabinet(package, mi);
581             if (rc == ERROR_SUCCESS &&
582                 GetFileAttributesW(mi->source) != INVALID_FILE_ATTRIBUTES)
583             {
584                 found = TRUE;
585             }
586         }
587
588         if (!found)
589             rc = msi_change_media(package, mi);
590     }
591
592     return rc;
593 }
594
595 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key, 
596                             MSIFILE** file)
597 {
598     LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
599     {
600         if (lstrcmpW( file_key, (*file)->File )==0)
601         {
602             if ((*file)->state >= msifs_overwrite)
603                 return ERROR_SUCCESS;
604             else
605                 return ERROR_FILE_NOT_FOUND;
606         }
607     }
608
609     return ERROR_FUNCTION_FAILED;
610 }
611
612 static void schedule_install_files(MSIPACKAGE *package)
613 {
614     MSIFILE *file;
615
616     LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
617     {
618         if (!ACTION_VerifyComponentForAction(file->Component, INSTALLSTATE_LOCAL))
619         {
620             TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
621
622             ui_progress(package,2,file->FileSize,0,0);
623             file->state = msifs_skipped;
624         }
625     }
626 }
627
628 static UINT copy_install_file(MSIFILE *file)
629 {
630     BOOL ret;
631     UINT gle;
632
633     TRACE("Copying %s to %s\n", debugstr_w(file->SourcePath),
634           debugstr_w(file->TargetPath));
635
636     ret = CopyFileW(file->SourcePath, file->TargetPath, FALSE);
637     if (ret)
638     {
639         file->state = msifs_installed;
640         return ERROR_SUCCESS;
641     }
642
643     gle = GetLastError();
644     if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
645     {
646         TRACE("overwriting existing file\n");
647         gle = ERROR_SUCCESS;
648     }
649     else if (gle == ERROR_FILE_NOT_FOUND)
650     {
651         /* FIXME: this needs to be tested, I'm pretty sure it fails */
652         TRACE("Source file not found\n");
653         gle = ERROR_SUCCESS;
654     }
655     else if (!(file->Attributes & msidbFileAttributesVital))
656     {
657         TRACE("Ignoring error for nonvital\n");
658         gle = ERROR_SUCCESS;
659     }
660
661     return gle;
662 }
663
664 /*
665  * ACTION_InstallFiles()
666  * 
667  * For efficiency, this is done in two passes:
668  * 1) Correct all the TargetPaths and determine what files are to be installed.
669  * 2) Extract Cabinets and copy files.
670  */
671 UINT ACTION_InstallFiles(MSIPACKAGE *package)
672 {
673     struct media_info *mi;
674     UINT rc = ERROR_SUCCESS;
675     LPWSTR ptr;
676     MSIFILE *file;
677
678     /* increment progress bar each time action data is sent */
679     ui_progress(package,1,1,0,0);
680
681     /* handle the keys for the SourceList */
682     ptr = strrchrW(package->PackagePath,'\\');
683     if (ptr)
684     {
685         ptr++;
686         MsiSourceListSetInfoW(package->ProductCode, NULL,
687                 MSIINSTALLCONTEXT_USERMANAGED,
688                 MSICODE_PRODUCT,
689                 INSTALLPROPERTY_PACKAGENAMEW, ptr);
690     }
691
692     schedule_install_files(package);
693
694     /*
695      * Despite MSDN specifying that the CreateFolders action
696      * should be called before InstallFiles, some installers don't
697      * do that, and they seem to work correctly.  We need to create
698      * directories here to make sure that the files can be copied.
699      */
700     msi_create_component_directories( package );
701
702     mi = msi_alloc_zero( sizeof(struct media_info) );
703
704     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
705     {
706         if (file->state != msifs_missing && file->state != msifs_overwrite)
707             continue;
708
709         if (file->Sequence > mi->last_sequence || mi->is_continuous)
710         {
711             rc = ready_media(package, file, mi);
712             if (rc != ERROR_SUCCESS)
713             {
714                 ERR("Failed to ready media\n");
715                 rc = ERROR_FUNCTION_FAILED;
716                 break;
717             }
718
719             if (file->IsCompressed && !extract_cabinet_file(package, mi))
720             {
721                 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
722                 rc = ERROR_FUNCTION_FAILED;
723                 break;
724             }
725         }
726
727         set_file_source(package, file, mi->source);
728
729         TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
730               debugstr_w(file->TargetPath));
731
732         if (!file->IsCompressed)
733         {
734             rc = copy_install_file(file);
735             if (rc != ERROR_SUCCESS)
736             {
737                 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(file->SourcePath),
738                     debugstr_w(file->TargetPath), rc);
739                 rc = ERROR_INSTALL_FAILURE;
740                 break;
741             }
742         }
743         else if (file->state != msifs_installed)
744         {
745             ERR("compressed file wasn't extracted (%s)\n", debugstr_w(file->TargetPath));
746             rc = ERROR_INSTALL_FAILURE;
747             break;
748         }
749     }
750
751     free_media_info( mi );
752     return rc;
753 }
754
755 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
756 {
757     MSIPACKAGE *package = (MSIPACKAGE*)param;
758     WCHAR dest_name[0x100];
759     LPWSTR dest_path, dest;
760     LPCWSTR file_key, component;
761     DWORD sz;
762     DWORD rc;
763     MSICOMPONENT *comp;
764     MSIFILE *file;
765
766     component = MSI_RecordGetString(row,2);
767     comp = get_loaded_component(package,component);
768
769     if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
770     {
771         TRACE("Skipping copy due to disabled component %s\n",
772                         debugstr_w(component));
773
774         /* the action taken was the same as the current install state */        
775         comp->Action = comp->Installed;
776
777         return ERROR_SUCCESS;
778     }
779
780     comp->Action = INSTALLSTATE_LOCAL;
781
782     file_key = MSI_RecordGetString(row,3);
783     if (!file_key)
784     {
785         ERR("Unable to get file key\n");
786         return ERROR_FUNCTION_FAILED;
787     }
788
789     rc = get_file_target(package,file_key,&file);
790
791     if (rc != ERROR_SUCCESS)
792     {
793         ERR("Original file unknown %s\n",debugstr_w(file_key));
794         return ERROR_SUCCESS;
795     }
796
797     if (MSI_RecordIsNull(row,4))
798         strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
799     else
800     {
801         sz=0x100;
802         MSI_RecordGetStringW(row,4,dest_name,&sz);
803         reduce_to_longfilename(dest_name);
804     }
805
806     if (MSI_RecordIsNull(row,5))
807     {
808         LPWSTR p;
809         dest_path = strdupW(file->TargetPath);
810         p = strrchrW(dest_path,'\\');
811         if (p)
812             *p=0;
813     }
814     else
815     {
816         LPCWSTR destkey;
817         destkey = MSI_RecordGetString(row,5);
818         dest_path = resolve_folder(package, destkey, FALSE,FALSE,NULL);
819         if (!dest_path)
820         {
821             /* try a Property */
822             dest_path = msi_dup_property( package, destkey );
823             if (!dest_path)
824             {
825                 FIXME("Unable to get destination folder, try AppSearch properties\n");
826                 return ERROR_SUCCESS;
827             }
828         }
829     }
830
831     dest = build_directory_name(2, dest_path, dest_name);
832
833     TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
834                     debugstr_w(dest)); 
835
836     if (strcmpW(file->TargetPath,dest))
837         rc = !CopyFileW(file->TargetPath,dest,TRUE);
838     else
839         rc = ERROR_SUCCESS;
840
841     if (rc != ERROR_SUCCESS)
842         ERR("Failed to copy file %s -> %s, last error %d\n",
843             debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
844
845     FIXME("We should track these duplicate files as well\n");   
846
847     msi_free(dest_path);
848     msi_free(dest);
849
850     msi_file_update_ui(package, file, szDuplicateFiles);
851
852     return ERROR_SUCCESS;
853 }
854
855 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
856 {
857     UINT rc;
858     MSIQUERY * view;
859     static const WCHAR ExecSeqQuery[] =
860         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
861          '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
862
863     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
864     if (rc != ERROR_SUCCESS)
865         return ERROR_SUCCESS;
866
867     rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
868     msiobj_release(&view->hdr);
869
870     return rc;
871 }
872
873 /* compares the version of a file read from the filesystem and
874  * the version specified in the File table
875  */
876 static int msi_compare_file_version( MSIFILE *file )
877 {
878     WCHAR version[MAX_PATH];
879     DWORD size;
880     UINT r;
881
882     size = MAX_PATH;
883     version[0] = '\0';
884     r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
885     if ( r != ERROR_SUCCESS )
886         return 0;
887
888     return lstrcmpW( version, file->Version );
889 }
890
891 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
892 {
893     MSIFILE *file;
894
895     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
896     {
897         MSIRECORD *uirow;
898         LPWSTR uipath, p;
899
900         if ( !file->Component )
901             continue;
902         if ( file->Component->Installed == INSTALLSTATE_LOCAL )
903             continue;
904
905         if ( file->state == msifs_installed )
906             ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
907
908         if ( file->state != msifs_present )
909             continue;
910
911         /* only remove a file if the version to be installed
912          * is strictly newer than the old file
913          */
914         if ( msi_compare_file_version( file ) >= 0 )
915             continue;
916
917         TRACE("removing %s\n", debugstr_w(file->File) );
918         if ( !DeleteFileW( file->TargetPath ) )
919             ERR("failed to delete %s\n",  debugstr_w(file->TargetPath) );
920         file->state = msifs_missing;
921
922         /* the UI chunk */
923         uirow = MSI_CreateRecord( 9 );
924         MSI_RecordSetStringW( uirow, 1, file->FileName );
925         uipath = strdupW( file->TargetPath );
926         p = strrchrW(uipath,'\\');
927         if (p)
928             p[1]=0;
929         MSI_RecordSetStringW( uirow, 9, uipath);
930         ui_actiondata( package, szRemoveFiles, uirow);
931         msiobj_release( &uirow->hdr );
932         msi_free( uipath );
933         /* FIXME: call ui_progress here? */
934     }
935
936     return ERROR_SUCCESS;
937 }