ole32: Fill in the xExt and yExt fields in OleMetafilePictFromIconAndLabel.
[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( MSIPACKAGE *package, struct media_info *mi )
247 {
248     MSIRECORD *row;
249     LPWSTR ptr;
250
251     static const WCHAR query[] =
252         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
253          '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
254          '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
255
256     row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
257     if (!row)
258     {
259         TRACE("Unable to query row\n");
260         return ERROR_FUNCTION_FAILED;
261     }
262
263     mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
264     mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
265
266     ptr = strrchrW(mi->source, '\\') + 1;
267     lstrcpyW(ptr, mi->cabinet);
268     msiobj_release(&row->hdr);
269
270     return ERROR_SUCCESS;
271 }
272
273 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
274 {
275     TRACE("(%d)\n", fdint);
276
277     switch (fdint)
278     {
279     case fdintPARTIAL_FILE:
280     {
281         CabData *data = (CabData *)pfdin->pv;
282         data->mi->is_continuous = FALSE;
283         return 0;
284     }
285     case fdintNEXT_CABINET:
286     {
287         CabData *data = (CabData *)pfdin->pv;
288         struct media_info *mi = data->mi;
289         LPWSTR cab = strdupAtoW(pfdin->psz1);
290         UINT rc;
291
292         msi_free(mi->disk_prompt);
293
294         mi->disk_id++;
295         mi->is_continuous = TRUE;
296
297         rc = msi_media_get_disk_info(data->package, mi);
298         if (rc != ERROR_SUCCESS)
299         {
300             ERR("Failed to get next cabinet information: %d\n", rc);
301             return -1;
302         }
303
304         if (lstrcmpiW(mi->cabinet, cab))
305         {
306             msi_free(cab);
307             ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
308             return -1;
309         }
310
311         msi_free(cab);
312
313         TRACE("Searching for %s\n", debugstr_w(mi->source));
314
315         if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
316             rc = msi_change_media(data->package, mi);
317
318         if (rc != ERROR_SUCCESS)
319             return -1;
320
321         return 0;
322     }
323     case fdintCOPY_FILE:
324     {
325         CabData *data = (CabData*) pfdin->pv;
326         HANDLE handle;
327         LPWSTR file;
328         MSIFILE *f;
329         DWORD attrs;
330
331         file = strdupAtoW(pfdin->psz1);
332         f = get_loaded_file(data->package, file);
333         msi_free(file);
334
335         if (!f)
336         {
337             WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
338             return 0;
339         }
340
341         if (f->state != msifs_missing && f->state != msifs_overwrite)
342         {
343             TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
344             return 0;
345         }
346
347         msi_file_update_ui( data->package, f, szInstallFiles );
348
349         TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
350
351         attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
352         if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
353
354         handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
355                               NULL, CREATE_ALWAYS, attrs, NULL );
356         if ( handle == INVALID_HANDLE_VALUE )
357         {
358             if ( GetFileAttributesW( f->TargetPath ) != INVALID_FILE_ATTRIBUTES )
359                 f->state = msifs_installed;
360             else
361                 ERR("failed to create %s (error %d)\n",
362                     debugstr_w( f->TargetPath ), GetLastError() );
363
364             return 0;
365         }
366
367         f->state = msifs_installed;
368         return (INT_PTR) handle;
369     }
370     case fdintCLOSE_FILE_INFO:
371     {
372         FILETIME ft;
373         FILETIME ftLocal;
374         HANDLE handle = (HANDLE) pfdin->hf;
375
376         if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
377             return -1;
378         if (!LocalFileTimeToFileTime(&ft, &ftLocal))
379             return -1;
380         if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
381             return -1;
382         CloseHandle(handle);
383         return 1;
384     }
385     default:
386         return 0;
387     }
388 }
389
390 /***********************************************************************
391  *            extract_cabinet_file
392  *
393  * Extract files from a cab file.
394  */
395 static BOOL extract_cabinet_file(MSIPACKAGE* package, struct media_info *mi)
396 {
397     LPSTR cabinet, cab_path = NULL;
398     LPWSTR ptr;
399     HFDI hfdi;
400     ERF erf;
401     BOOL ret = FALSE;
402     CabData data;
403
404     TRACE("Extracting %s\n", debugstr_w(mi->source));
405
406     hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
407                      cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
408     if (!hfdi)
409     {
410         ERR("FDICreate failed\n");
411         return FALSE;
412     }
413
414     ptr = strrchrW(mi->source, '\\') + 1;
415     cabinet = strdupWtoA(ptr);
416     if (!cabinet)
417         goto done;
418
419     cab_path = strdupWtoA(mi->source);
420     if (!cab_path)
421         goto done;
422
423     cab_path[ptr - mi->source] = '\0';
424
425     data.package = package;
426     data.mi = mi;
427
428     ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, &data);
429     if (!ret)
430         ERR("FDICopy failed\n");
431
432 done:
433     FDIDestroy(hfdi);
434     msi_free(cabinet);
435     msi_free(cab_path);
436
437     return ret;
438 }
439
440 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, LPCWSTR path)
441 {
442     if (!file->IsCompressed)
443     {
444         LPWSTR p, path;
445         p = resolve_folder(package, file->Component->Directory, TRUE, FALSE, NULL);
446         path = build_directory_name(2, p, file->ShortName);
447         if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
448         {
449             msi_free(path);
450             path = build_directory_name(2, p, file->LongName);
451         }
452         file->SourcePath = path;
453         msi_free(p);
454     }
455     else
456         file->SourcePath = build_directory_name(2, path, file->File);
457 }
458
459 static void free_media_info( struct media_info *mi )
460 {
461     msi_free( mi->disk_prompt );
462     msi_free( mi->cabinet );
463     msi_free( mi->volume_label );
464     msi_free( mi );
465 }
466
467 static UINT download_remote_cabinet(MSIPACKAGE *package, struct media_info *mi)
468 {
469     WCHAR temppath[MAX_PATH];
470     LPWSTR src, ptr;
471     LPCWSTR cab;
472
473     src = strdupW(package->BaseURL);
474     if (!src)
475         return ERROR_OUTOFMEMORY;
476
477     ptr = strrchrW(src, '/');
478     if (!ptr)
479     {
480         msi_free(src);
481         return ERROR_FUNCTION_FAILED;
482     }
483
484     *(ptr + 1) = '\0';
485     ptr = strrchrW(mi->source, '\\');
486     if (!ptr)
487         ptr = mi->source;
488
489     src = msi_realloc(src, (lstrlenW(src) + lstrlenW(ptr)) * sizeof(WCHAR));
490     if (!src)
491         return ERROR_OUTOFMEMORY;
492
493     lstrcatW(src, ptr + 1);
494
495     temppath[0] = '\0';
496     cab = msi_download_file(src, temppath);
497     lstrcpyW(mi->source, cab);
498
499     msi_free(src);
500     return ERROR_SUCCESS;
501 }
502
503 static UINT load_media_info(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
504 {
505     MSIRECORD *row;
506     LPWSTR source_dir;
507     UINT r;
508
509     static const WCHAR query[] = {
510         'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
511         '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
512         '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
513         ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
514         ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
515         '`','D','i','s','k','I','d','`',0
516     };
517
518     row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
519     if (!row)
520     {
521         TRACE("Unable to query row\n");
522         return ERROR_FUNCTION_FAILED;
523     }
524
525     mi->disk_id = MSI_RecordGetInteger(row, 1);
526     mi->last_sequence = MSI_RecordGetInteger(row, 2);
527     msi_free(mi->disk_prompt);
528     mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
529     msi_free(mi->cabinet);
530     mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
531     msi_free(mi->volume_label);
532     mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
533     msiobj_release(&row->hdr);
534
535     source_dir = msi_dup_property(package, cszSourceDir);
536
537     if (mi->cabinet && mi->cabinet[0] == '#')
538     {
539         r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
540         if (r != ERROR_SUCCESS)
541         {
542             ERR("Failed to extract cabinet stream\n");
543             return ERROR_FUNCTION_FAILED;
544         }
545     }
546     else
547     {
548         lstrcpyW(mi->source, source_dir);
549
550
551         if (mi->cabinet)
552             lstrcatW(mi->source, mi->cabinet);
553     }
554
555     MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
556         MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT,
557         mi->disk_id, mi->volume_label, mi->disk_prompt);
558
559     MsiSourceListSetInfoW(package->ProductCode, NULL,
560         MSIINSTALLCONTEXT_USERMANAGED,
561         MSICODE_PRODUCT | MSISOURCETYPE_MEDIA,
562         INSTALLPROPERTY_LASTUSEDSOURCEW, mi->source);
563
564     msi_free(source_dir);
565     return ERROR_SUCCESS;
566 }
567
568 static UINT ready_media(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
569 {
570     UINT rc = ERROR_SUCCESS;
571     BOOL found = FALSE;
572
573     /* media info for continuous cabinet is already loaded */
574     if (mi->is_continuous)
575         return ERROR_SUCCESS;
576
577     rc = load_media_info(package, file, mi);
578     if (rc != ERROR_SUCCESS)
579     {
580         ERR("Unable to load media info\n");
581         return ERROR_FUNCTION_FAILED;
582     }
583
584     if (file->IsCompressed &&
585         GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
586     {
587         if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
588         {
589             rc = download_remote_cabinet(package, mi);
590             if (rc == ERROR_SUCCESS &&
591                 GetFileAttributesW(mi->source) != INVALID_FILE_ATTRIBUTES)
592             {
593                 found = TRUE;
594             }
595         }
596
597         if (!found)
598             rc = msi_change_media(package, mi);
599     }
600
601     return rc;
602 }
603
604 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key, 
605                             MSIFILE** file)
606 {
607     LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
608     {
609         if (lstrcmpW( file_key, (*file)->File )==0)
610         {
611             if ((*file)->state >= msifs_overwrite)
612                 return ERROR_SUCCESS;
613             else
614                 return ERROR_FILE_NOT_FOUND;
615         }
616     }
617
618     return ERROR_FUNCTION_FAILED;
619 }
620
621 static void schedule_install_files(MSIPACKAGE *package)
622 {
623     MSIFILE *file;
624
625     LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
626     {
627         if (!ACTION_VerifyComponentForAction(file->Component, INSTALLSTATE_LOCAL))
628         {
629             TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
630
631             ui_progress(package,2,file->FileSize,0,0);
632             file->state = msifs_skipped;
633         }
634     }
635 }
636
637 static UINT copy_install_file(MSIFILE *file)
638 {
639     BOOL ret;
640     UINT gle;
641
642     TRACE("Copying %s to %s\n", debugstr_w(file->SourcePath),
643           debugstr_w(file->TargetPath));
644
645     ret = CopyFileW(file->SourcePath, file->TargetPath, FALSE);
646     if (ret)
647     {
648         file->state = msifs_installed;
649         return ERROR_SUCCESS;
650     }
651
652     gle = GetLastError();
653     if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
654     {
655         TRACE("overwriting existing file\n");
656         gle = ERROR_SUCCESS;
657     }
658     else if (gle == ERROR_FILE_NOT_FOUND)
659     {
660         /* FIXME: this needs to be tested, I'm pretty sure it fails */
661         TRACE("Source file not found\n");
662         gle = ERROR_SUCCESS;
663     }
664     else if (!(file->Attributes & msidbFileAttributesVital))
665     {
666         TRACE("Ignoring error for nonvital\n");
667         gle = ERROR_SUCCESS;
668     }
669
670     return gle;
671 }
672
673 /*
674  * ACTION_InstallFiles()
675  * 
676  * For efficiency, this is done in two passes:
677  * 1) Correct all the TargetPaths and determine what files are to be installed.
678  * 2) Extract Cabinets and copy files.
679  */
680 UINT ACTION_InstallFiles(MSIPACKAGE *package)
681 {
682     struct media_info *mi;
683     UINT rc = ERROR_SUCCESS;
684     LPWSTR ptr;
685     MSIFILE *file;
686
687     /* increment progress bar each time action data is sent */
688     ui_progress(package,1,1,0,0);
689
690     /* handle the keys for the SourceList */
691     ptr = strrchrW(package->PackagePath,'\\');
692     if (ptr)
693     {
694         ptr++;
695         MsiSourceListSetInfoW(package->ProductCode, NULL,
696                 MSIINSTALLCONTEXT_USERMANAGED,
697                 MSICODE_PRODUCT,
698                 INSTALLPROPERTY_PACKAGENAMEW, ptr);
699     }
700
701     schedule_install_files(package);
702
703     /*
704      * Despite MSDN specifying that the CreateFolders action
705      * should be called before InstallFiles, some installers don't
706      * do that, and they seem to work correctly.  We need to create
707      * directories here to make sure that the files can be copied.
708      */
709     msi_create_component_directories( package );
710
711     mi = msi_alloc_zero( sizeof(struct media_info) );
712
713     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
714     {
715         if (file->state != msifs_missing && file->state != msifs_overwrite)
716             continue;
717
718         if (file->Sequence > mi->last_sequence || mi->is_continuous)
719         {
720             rc = ready_media(package, file, mi);
721             if (rc != ERROR_SUCCESS)
722             {
723                 ERR("Failed to ready media\n");
724                 rc = ERROR_FUNCTION_FAILED;
725                 break;
726             }
727
728             if (file->IsCompressed && !extract_cabinet_file(package, mi))
729             {
730                 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
731                 rc = ERROR_FUNCTION_FAILED;
732                 break;
733             }
734         }
735
736         set_file_source(package, file, mi->source);
737
738         TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
739               debugstr_w(file->TargetPath));
740
741         if (!file->IsCompressed)
742         {
743             rc = copy_install_file(file);
744             if (rc != ERROR_SUCCESS)
745             {
746                 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(file->SourcePath),
747                     debugstr_w(file->TargetPath), rc);
748                 rc = ERROR_INSTALL_FAILURE;
749                 break;
750             }
751         }
752         else if (file->state != msifs_installed)
753         {
754             ERR("compressed file wasn't extracted (%s)\n", debugstr_w(file->TargetPath));
755             rc = ERROR_INSTALL_FAILURE;
756             break;
757         }
758     }
759
760     free_media_info( mi );
761     return rc;
762 }
763
764 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
765 {
766     MSIPACKAGE *package = (MSIPACKAGE*)param;
767     WCHAR dest_name[0x100];
768     LPWSTR dest_path, dest;
769     LPCWSTR file_key, component;
770     DWORD sz;
771     DWORD rc;
772     MSICOMPONENT *comp;
773     MSIFILE *file;
774
775     component = MSI_RecordGetString(row,2);
776     comp = get_loaded_component(package,component);
777
778     if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
779     {
780         TRACE("Skipping copy due to disabled component %s\n",
781                         debugstr_w(component));
782
783         /* the action taken was the same as the current install state */        
784         comp->Action = comp->Installed;
785
786         return ERROR_SUCCESS;
787     }
788
789     comp->Action = INSTALLSTATE_LOCAL;
790
791     file_key = MSI_RecordGetString(row,3);
792     if (!file_key)
793     {
794         ERR("Unable to get file key\n");
795         return ERROR_FUNCTION_FAILED;
796     }
797
798     rc = get_file_target(package,file_key,&file);
799
800     if (rc != ERROR_SUCCESS)
801     {
802         ERR("Original file unknown %s\n",debugstr_w(file_key));
803         return ERROR_SUCCESS;
804     }
805
806     if (MSI_RecordIsNull(row,4))
807         strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
808     else
809     {
810         sz=0x100;
811         MSI_RecordGetStringW(row,4,dest_name,&sz);
812         reduce_to_longfilename(dest_name);
813     }
814
815     if (MSI_RecordIsNull(row,5))
816     {
817         LPWSTR p;
818         dest_path = strdupW(file->TargetPath);
819         p = strrchrW(dest_path,'\\');
820         if (p)
821             *p=0;
822     }
823     else
824     {
825         LPCWSTR destkey;
826         destkey = MSI_RecordGetString(row,5);
827         dest_path = resolve_folder(package, destkey, FALSE,FALSE,NULL);
828         if (!dest_path)
829         {
830             /* try a Property */
831             dest_path = msi_dup_property( package, destkey );
832             if (!dest_path)
833             {
834                 FIXME("Unable to get destination folder, try AppSearch properties\n");
835                 return ERROR_SUCCESS;
836             }
837         }
838     }
839
840     dest = build_directory_name(2, dest_path, dest_name);
841
842     TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
843                     debugstr_w(dest)); 
844
845     if (strcmpW(file->TargetPath,dest))
846         rc = !CopyFileW(file->TargetPath,dest,TRUE);
847     else
848         rc = ERROR_SUCCESS;
849
850     if (rc != ERROR_SUCCESS)
851         ERR("Failed to copy file %s -> %s, last error %d\n",
852             debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
853
854     FIXME("We should track these duplicate files as well\n");   
855
856     msi_free(dest_path);
857     msi_free(dest);
858
859     msi_file_update_ui(package, file, szDuplicateFiles);
860
861     return ERROR_SUCCESS;
862 }
863
864 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
865 {
866     UINT rc;
867     MSIQUERY * view;
868     static const WCHAR ExecSeqQuery[] =
869         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
870          '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
871
872     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
873     if (rc != ERROR_SUCCESS)
874         return ERROR_SUCCESS;
875
876     rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
877     msiobj_release(&view->hdr);
878
879     return rc;
880 }
881
882 /* compares the version of a file read from the filesystem and
883  * the version specified in the File table
884  */
885 static int msi_compare_file_version( MSIFILE *file )
886 {
887     WCHAR version[MAX_PATH];
888     DWORD size;
889     UINT r;
890
891     size = MAX_PATH;
892     version[0] = '\0';
893     r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
894     if ( r != ERROR_SUCCESS )
895         return 0;
896
897     return lstrcmpW( version, file->Version );
898 }
899
900 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
901 {
902     MSIFILE *file;
903
904     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
905     {
906         MSIRECORD *uirow;
907         LPWSTR uipath, p;
908
909         if ( !file->Component )
910             continue;
911         if ( file->Component->Installed == INSTALLSTATE_LOCAL )
912             continue;
913
914         if ( file->state == msifs_installed )
915             ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
916
917         if ( file->state != msifs_present )
918             continue;
919
920         /* only remove a file if the version to be installed
921          * is strictly newer than the old file
922          */
923         if ( msi_compare_file_version( file ) >= 0 )
924             continue;
925
926         TRACE("removing %s\n", debugstr_w(file->File) );
927         if ( !DeleteFileW( file->TargetPath ) )
928             ERR("failed to delete %s\n",  debugstr_w(file->TargetPath) );
929         file->state = msifs_missing;
930
931         /* the UI chunk */
932         uirow = MSI_CreateRecord( 9 );
933         MSI_RecordSetStringW( uirow, 1, file->FileName );
934         uipath = strdupW( file->TargetPath );
935         p = strrchrW(uipath,'\\');
936         if (p)
937             p[1]=0;
938         MSI_RecordSetStringW( uirow, 9, uipath);
939         ui_actiondata( package, szRemoveFiles, uirow);
940         msiobj_release( &uirow->hdr );
941         msi_free( uipath );
942         /* FIXME: call ui_progress here? */
943     }
944
945     return ERROR_SUCCESS;
946 }