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