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