msi: Add support for installing side-by-side assemblies.
[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
28  * PatchFiles (TODO)
29  * RemoveDuplicateFiles
30  * RemoveFiles
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 "msipriv.h"
43 #include "winuser.h"
44 #include "winreg.h"
45 #include "shlwapi.h"
46 #include "wine/unicode.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(msi);
49
50 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
51 {
52     MSIRECORD *uirow;
53
54     uirow = MSI_CreateRecord( 9 );
55     MSI_RecordSetStringW( uirow, 1, f->FileName );
56     MSI_RecordSetStringW( uirow, 9, f->Component->Directory );
57     MSI_RecordSetInteger( uirow, 6, f->FileSize );
58     ui_actiondata( package, action, uirow );
59     msiobj_release( &uirow->hdr );
60     ui_progress( package, 2, f->FileSize, 0, 0 );
61 }
62
63 static void schedule_install_files(MSIPACKAGE *package)
64 {
65     MSIFILE *file;
66
67     LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
68     {
69         if (file->Component->ActionRequest != INSTALLSTATE_LOCAL || !file->Component->Enabled)
70         {
71             TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
72
73             ui_progress(package,2,file->FileSize,0,0);
74             file->state = msifs_skipped;
75         }
76         else
77             file->Component->Action = INSTALLSTATE_LOCAL;
78     }
79 }
80
81 static UINT copy_file(MSIFILE *file, LPWSTR source)
82 {
83     BOOL ret;
84
85     ret = CopyFileW(source, file->TargetPath, FALSE);
86     if (!ret)
87         return GetLastError();
88
89     SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
90
91     file->state = msifs_installed;
92     return ERROR_SUCCESS;
93 }
94
95 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
96 {
97     UINT gle;
98
99     TRACE("Copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
100
101     gle = copy_file(file, source);
102     if (gle == ERROR_SUCCESS)
103         return gle;
104
105     if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
106     {
107         TRACE("overwriting existing file\n");
108         return ERROR_SUCCESS;
109     }
110     else if (gle == ERROR_ACCESS_DENIED)
111     {
112         SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
113
114         gle = copy_file(file, source);
115         TRACE("Overwriting existing file: %d\n", gle);
116     }
117     if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
118     {
119         WCHAR tmpfileW[MAX_PATH], *pathW, *p;
120         DWORD len;
121
122         TRACE("file in use, scheduling rename operation\n");
123
124         GetTempFileNameW(szBackSlash, szMsi, 0, tmpfileW);
125         len = strlenW(file->TargetPath) + strlenW(tmpfileW) + 1;
126         if (!(pathW = msi_alloc(len * sizeof(WCHAR))))
127             return ERROR_OUTOFMEMORY;
128
129         strcpyW(pathW, file->TargetPath);
130         if ((p = strrchrW(pathW, '\\'))) *p = 0;
131         strcatW(pathW, tmpfileW);
132
133         if (CopyFileW(source, pathW, FALSE) &&
134             MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
135             MoveFileExW(pathW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
136         {
137             file->state = msifs_installed;
138             package->need_reboot = 1;
139             gle = ERROR_SUCCESS;
140         }
141         else
142         {
143             gle = GetLastError();
144             WARN("failed to schedule rename operation: %d)\n", gle);
145         }
146         msi_free(pathW);
147     }
148
149     return gle;
150 }
151
152 static UINT msi_create_directory( MSIPACKAGE *package, const WCHAR *dir )
153 {
154     MSIFOLDER *folder;
155     WCHAR *install_path;
156
157     install_path = resolve_folder( package, dir, FALSE, FALSE, TRUE, &folder );
158     if (!install_path)
159         return ERROR_FUNCTION_FAILED;
160
161     if (folder->State == 0)
162     {
163         create_full_pathW( install_path );
164         folder->State = 2;
165     }
166     msi_free( install_path );
167     return ERROR_SUCCESS;
168 }
169
170 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
171                             LPWSTR *path, DWORD *attrs, PVOID user)
172 {
173     static MSIFILE *f = NULL;
174     UINT_PTR disk_id = (UINT_PTR)user;
175
176     if (action == MSICABEXTRACT_BEGINEXTRACT)
177     {
178         f = get_loaded_file(package, file);
179         if (!f)
180         {
181             WARN("unknown file in cabinet (%s)\n", debugstr_w(file));
182             return FALSE;
183         }
184
185         if (f->disk_id != disk_id || (f->state != msifs_missing && f->state != msifs_overwrite))
186             return FALSE;
187
188         msi_file_update_ui(package, f, szInstallFiles);
189         if (!f->Component->assembly)
190             msi_create_directory(package, f->Component->Directory);
191
192         *path = strdupW(f->TargetPath);
193         *attrs = f->Attributes;
194     }
195     else if (action == MSICABEXTRACT_FILEEXTRACTED)
196     {
197         f->state = msifs_installed;
198         f = NULL;
199     }
200
201     return TRUE;
202 }
203
204 /*
205  * ACTION_InstallFiles()
206  * 
207  * For efficiency, this is done in two passes:
208  * 1) Correct all the TargetPaths and determine what files are to be installed.
209  * 2) Extract Cabinets and copy files.
210  */
211 UINT ACTION_InstallFiles(MSIPACKAGE *package)
212 {
213     MSIMEDIAINFO *mi;
214     MSICOMPONENT *comp;
215     UINT rc = ERROR_SUCCESS;
216     MSIFILE *file;
217
218     /* increment progress bar each time action data is sent */
219     ui_progress(package,1,1,0,0);
220
221     schedule_install_files(package);
222
223     mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
224
225     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
226     {
227         if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
228             continue;
229
230         if (file->Sequence > mi->last_sequence || mi->is_continuous ||
231             (file->IsCompressed && !mi->is_extracted))
232         {
233             MSICABDATA data;
234
235             rc = ready_media(package, file, mi);
236             if (rc != ERROR_SUCCESS)
237             {
238                 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
239                 goto done;
240             }
241
242             data.mi = mi;
243             data.package = package;
244             data.cb = installfiles_cb;
245             data.user = (PVOID)(UINT_PTR)mi->disk_id;
246
247             if (file->IsCompressed &&
248                 !msi_cabextract(package, mi, &data))
249             {
250                 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
251                 rc = ERROR_INSTALL_FAILURE;
252                 goto done;
253             }
254         }
255
256         if (!file->IsCompressed)
257         {
258             LPWSTR source = resolve_file_source(package, file);
259
260             TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
261
262             msi_file_update_ui(package, file, szInstallFiles);
263             if (!file->Component->assembly)
264                 msi_create_directory(package, file->Component->Directory);
265
266             rc = copy_install_file(package, file, source);
267             if (rc != ERROR_SUCCESS)
268             {
269                 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
270                     debugstr_w(file->TargetPath), rc);
271                 rc = ERROR_INSTALL_FAILURE;
272                 msi_free(source);
273                 goto done;
274             }
275             msi_free(source);
276         }
277         else if (file->state != msifs_installed)
278         {
279             ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->TargetPath));
280             rc = ERROR_INSTALL_FAILURE;
281             goto done;
282         }
283     }
284     LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
285     {
286         if (comp->Enabled && comp->assembly && !comp->assembly->installed)
287         {
288             rc = install_assembly( package, comp );
289             if (rc != ERROR_SUCCESS)
290             {
291                 ERR("Failed to install assembly\n");
292                 rc = ERROR_INSTALL_FAILURE;
293                 break;
294             }
295         }
296     }
297
298 done:
299     msi_free_media_info(mi);
300     return rc;
301 }
302
303 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
304
305 typedef struct
306 {
307     struct list entry;
308     LPWSTR sourcename;
309     LPWSTR destname;
310     LPWSTR source;
311     LPWSTR dest;
312 } FILE_LIST;
313
314 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
315 {
316     BOOL ret;
317
318     if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
319         GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
320     {
321         WARN("Source or dest is directory, not moving\n");
322         return FALSE;
323     }
324
325     if (options == msidbMoveFileOptionsMove)
326     {
327         TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
328         ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
329         if (!ret)
330         {
331             WARN("MoveFile failed: %d\n", GetLastError());
332             return FALSE;
333         }
334     }
335     else
336     {
337         TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
338         ret = CopyFileW(source, dest, FALSE);
339         if (!ret)
340         {
341             WARN("CopyFile failed: %d\n", GetLastError());
342             return FALSE;
343         }
344     }
345
346     return TRUE;
347 }
348
349 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
350 {
351     LPWSTR path, ptr;
352     DWORD dirlen, pathlen;
353
354     ptr = strrchrW(wildcard, '\\');
355     dirlen = ptr - wildcard + 1;
356
357     pathlen = dirlen + lstrlenW(filename) + 1;
358     path = msi_alloc(pathlen * sizeof(WCHAR));
359
360     lstrcpynW(path, wildcard, dirlen + 1);
361     lstrcatW(path, filename);
362
363     return path;
364 }
365
366 static void free_file_entry(FILE_LIST *file)
367 {
368     msi_free(file->source);
369     msi_free(file->dest);
370     msi_free(file);
371 }
372
373 static void free_list(FILE_LIST *list)
374 {
375     while (!list_empty(&list->entry))
376     {
377         FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
378
379         list_remove(&file->entry);
380         free_file_entry(file);
381     }
382 }
383
384 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
385 {
386     FILE_LIST *new, *file;
387     LPWSTR ptr, filename;
388     DWORD size;
389
390     new = msi_alloc_zero(sizeof(FILE_LIST));
391     if (!new)
392         return FALSE;
393
394     new->source = strdupW(source);
395     ptr = strrchrW(dest, '\\') + 1;
396     filename = strrchrW(new->source, '\\') + 1;
397
398     new->sourcename = filename;
399
400     if (*ptr)
401         new->destname = ptr;
402     else
403         new->destname = new->sourcename;
404
405     size = (ptr - dest) + lstrlenW(filename) + 1;
406     new->dest = msi_alloc(size * sizeof(WCHAR));
407     if (!new->dest)
408     {
409         free_file_entry(new);
410         return FALSE;
411     }
412
413     lstrcpynW(new->dest, dest, ptr - dest + 1);
414     lstrcatW(new->dest, filename);
415
416     if (list_empty(&files->entry))
417     {
418         list_add_head(&files->entry, &new->entry);
419         return TRUE;
420     }
421
422     LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
423     {
424         if (strcmpW( source, file->source ) < 0)
425         {
426             list_add_before(&file->entry, &new->entry);
427             return TRUE;
428         }
429     }
430
431     list_add_after(&file->entry, &new->entry);
432     return TRUE;
433 }
434
435 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
436 {
437     WIN32_FIND_DATAW wfd;
438     HANDLE hfile;
439     LPWSTR path;
440     BOOL res;
441     FILE_LIST files, *file;
442     DWORD size;
443
444     hfile = FindFirstFileW(source, &wfd);
445     if (hfile == INVALID_HANDLE_VALUE) return FALSE;
446
447     list_init(&files.entry);
448
449     for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
450     {
451         if (is_dot_dir(wfd.cFileName)) continue;
452
453         path = wildcard_to_file(source, wfd.cFileName);
454         if (!path)
455         {
456             res = FALSE;
457             goto done;
458         }
459
460         add_wildcard(&files, path, dest);
461         msi_free(path);
462     }
463
464     /* no files match the wildcard */
465     if (list_empty(&files.entry))
466         goto done;
467
468     /* only the first wildcard match gets renamed to dest */
469     file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
470     size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
471     file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
472     if (!file->dest)
473     {
474         res = FALSE;
475         goto done;
476     }
477
478     /* file->dest may be shorter after the reallocation, so add a NULL
479      * terminator.  This is needed for the call to strrchrW, as there will no
480      * longer be a NULL terminator within the bounds of the allocation in this case.
481      */
482     file->dest[size - 1] = '\0';
483     lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
484
485     while (!list_empty(&files.entry))
486     {
487         file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
488
489         msi_move_file(file->source, file->dest, options);
490
491         list_remove(&file->entry);
492         free_file_entry(file);
493     }
494
495     res = TRUE;
496
497 done:
498     free_list(&files);
499     FindClose(hfile);
500     return res;
501 }
502
503 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
504 {
505     MSIPACKAGE *package = param;
506     MSIRECORD *uirow;
507     MSICOMPONENT *comp;
508     LPCWSTR sourcename, component;
509     LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
510     int options;
511     DWORD size;
512     BOOL ret, wildcards;
513
514     component = MSI_RecordGetString(rec, 2);
515     comp = get_loaded_component(package, component);
516     if (!comp)
517         return ERROR_SUCCESS;
518
519     if (!comp->Enabled)
520     {
521         TRACE("component is disabled\n");
522         return ERROR_SUCCESS;
523     }
524
525     if (comp->ActionRequest != INSTALLSTATE_LOCAL && comp->ActionRequest != INSTALLSTATE_SOURCE)
526     {
527         TRACE("Component not scheduled for installation: %s\n", debugstr_w(component));
528         comp->Action = comp->Installed;
529         return ERROR_SUCCESS;
530     }
531     comp->Action = comp->ActionRequest;
532
533     sourcename = MSI_RecordGetString(rec, 3);
534     options = MSI_RecordGetInteger(rec, 7);
535
536     sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
537     if (!sourcedir)
538         goto done;
539
540     destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
541     if (!destdir)
542         goto done;
543
544     if (!sourcename)
545     {
546         if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
547             goto done;
548
549         source = strdupW(sourcedir);
550         if (!source)
551             goto done;
552     }
553     else
554     {
555         size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
556         source = msi_alloc(size * sizeof(WCHAR));
557         if (!source)
558             goto done;
559
560         lstrcpyW(source, sourcedir);
561         if (source[lstrlenW(source) - 1] != '\\')
562             lstrcatW(source, szBackSlash);
563         lstrcatW(source, sourcename);
564     }
565
566     wildcards = strchrW(source, '*') || strchrW(source, '?');
567
568     if (MSI_RecordIsNull(rec, 4))
569     {
570         if (!wildcards)
571         {
572             destname = strdupW(sourcename);
573             if (!destname)
574                 goto done;
575         }
576     }
577     else
578     {
579         destname = strdupW(MSI_RecordGetString(rec, 4));
580         if (destname)
581             reduce_to_longfilename(destname);
582     }
583
584     size = 0;
585     if (destname)
586         size = lstrlenW(destname);
587
588     size += lstrlenW(destdir) + 2;
589     dest = msi_alloc(size * sizeof(WCHAR));
590     if (!dest)
591         goto done;
592
593     lstrcpyW(dest, destdir);
594     if (dest[lstrlenW(dest) - 1] != '\\')
595         lstrcatW(dest, szBackSlash);
596
597     if (destname)
598         lstrcatW(dest, destname);
599
600     if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
601     {
602         ret = CreateDirectoryW(destdir, NULL);
603         if (!ret)
604         {
605             WARN("CreateDirectory failed: %d\n", GetLastError());
606             goto done;
607         }
608     }
609
610     if (!wildcards)
611         msi_move_file(source, dest, options);
612     else
613         move_files_wildcard(source, dest, options);
614
615 done:
616     uirow = MSI_CreateRecord( 9 );
617     MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
618     MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
619     MSI_RecordSetStringW( uirow, 9, destdir );
620     ui_actiondata( package, szMoveFiles, uirow );
621     msiobj_release( &uirow->hdr );
622
623     msi_free(sourcedir);
624     msi_free(destdir);
625     msi_free(destname);
626     msi_free(source);
627     msi_free(dest);
628
629     return ERROR_SUCCESS;
630 }
631
632 UINT ACTION_MoveFiles( MSIPACKAGE *package )
633 {
634     UINT rc;
635     MSIQUERY *view;
636
637     static const WCHAR ExecSeqQuery[] =
638         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
639          '`','M','o','v','e','F','i','l','e','`',0};
640
641     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
642     if (rc != ERROR_SUCCESS)
643         return ERROR_SUCCESS;
644
645     rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
646     msiobj_release(&view->hdr);
647
648     return rc;
649 }
650
651 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
652 {
653     DWORD len;
654     WCHAR *dst_name, *dst_path, *dst;
655
656     if (MSI_RecordIsNull( row, 4 ))
657     {
658         len = strlenW( src ) + 1;
659         if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
660         strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
661     }
662     else
663     {
664         MSI_RecordGetStringW( row, 4, NULL, &len );
665         if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
666         MSI_RecordGetStringW( row, 4, dst_name, &len );
667         reduce_to_longfilename( dst_name );
668     }
669
670     if (MSI_RecordIsNull( row, 5 ))
671     {
672         WCHAR *p;
673         dst_path = strdupW( src );
674         p = strrchrW( dst_path, '\\' );
675         if (p) *p = 0;
676     }
677     else
678     {
679         const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
680
681         dst_path = resolve_folder( package, dst_key, FALSE, FALSE, TRUE, NULL );
682         if (!dst_path)
683         {
684             /* try a property */
685             dst_path = msi_dup_property( package->db, dst_key );
686             if (!dst_path)
687             {
688                 FIXME("Unable to get destination folder, try AppSearch properties\n");
689                 msi_free( dst_name );
690                 return NULL;
691             }
692         }
693     }
694
695     dst = build_directory_name( 2, dst_path, dst_name );
696     create_full_pathW( dst_path );
697
698     msi_free( dst_name );
699     msi_free( dst_path );
700     return dst;
701 }
702
703 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
704 {
705     MSIPACKAGE *package = param;
706     LPWSTR dest;
707     LPCWSTR file_key, component;
708     MSICOMPONENT *comp;
709     MSIRECORD *uirow;
710     MSIFILE *file;
711
712     component = MSI_RecordGetString(row,2);
713     comp = get_loaded_component(package,component);
714     if (!comp)
715         return ERROR_SUCCESS;
716
717     if (!comp->Enabled)
718     {
719         TRACE("component is disabled\n");
720         return ERROR_SUCCESS;
721     }
722
723     if (comp->ActionRequest != INSTALLSTATE_LOCAL)
724     {
725         TRACE("Component not scheduled for installation %s\n", debugstr_w(component));
726         comp->Action = comp->Installed;
727         return ERROR_SUCCESS;
728     }
729     comp->Action = INSTALLSTATE_LOCAL;
730
731     file_key = MSI_RecordGetString(row,3);
732     if (!file_key)
733     {
734         ERR("Unable to get file key\n");
735         return ERROR_FUNCTION_FAILED;
736     }
737
738     file = get_loaded_file( package, file_key );
739     if (!file)
740     {
741         ERR("Original file unknown %s\n", debugstr_w(file_key));
742         return ERROR_SUCCESS;
743     }
744
745     dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
746     if (!dest)
747     {
748         WARN("Unable to get duplicate filename\n");
749         return ERROR_SUCCESS;
750     }
751
752     TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
753
754     if (!CopyFileW( file->TargetPath, dest, TRUE ))
755     {
756         WARN("Failed to copy file %s -> %s (%u)\n",
757              debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
758     }
759
760     FIXME("We should track these duplicate files as well\n");   
761
762     uirow = MSI_CreateRecord( 9 );
763     MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
764     MSI_RecordSetInteger( uirow, 6, file->FileSize );
765     MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
766     ui_actiondata( package, szDuplicateFiles, uirow );
767     msiobj_release( &uirow->hdr );
768
769     msi_free(dest);
770     return ERROR_SUCCESS;
771 }
772
773 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
774 {
775     UINT rc;
776     MSIQUERY * view;
777     static const WCHAR ExecSeqQuery[] =
778         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
779          '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
780
781     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
782     if (rc != ERROR_SUCCESS)
783         return ERROR_SUCCESS;
784
785     rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
786     msiobj_release(&view->hdr);
787
788     return rc;
789 }
790
791 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
792 {
793     MSIPACKAGE *package = param;
794     LPWSTR dest;
795     LPCWSTR file_key, component;
796     MSICOMPONENT *comp;
797     MSIRECORD *uirow;
798     MSIFILE *file;
799
800     component = MSI_RecordGetString( row, 2 );
801     comp = get_loaded_component( package, component );
802     if (!comp)
803         return ERROR_SUCCESS;
804
805     if (!comp->Enabled)
806     {
807         TRACE("component is disabled\n");
808         return ERROR_SUCCESS;
809     }
810
811     if (comp->ActionRequest != INSTALLSTATE_ABSENT)
812     {
813         TRACE("Component not scheduled for removal %s\n", debugstr_w(component));
814         comp->Action = comp->Installed;
815         return ERROR_SUCCESS;
816     }
817     comp->Action = INSTALLSTATE_ABSENT;
818
819     file_key = MSI_RecordGetString( row, 3 );
820     if (!file_key)
821     {
822         ERR("Unable to get file key\n");
823         return ERROR_FUNCTION_FAILED;
824     }
825
826     file = get_loaded_file( package, file_key );
827     if (!file)
828     {
829         ERR("Original file unknown %s\n", debugstr_w(file_key));
830         return ERROR_SUCCESS;
831     }
832
833     dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
834     if (!dest)
835     {
836         WARN("Unable to get duplicate filename\n");
837         return ERROR_SUCCESS;
838     }
839
840     TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
841
842     if (!DeleteFileW( dest ))
843     {
844         WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
845     }
846
847     uirow = MSI_CreateRecord( 9 );
848     MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
849     MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
850     ui_actiondata( package, szRemoveDuplicateFiles, uirow );
851     msiobj_release( &uirow->hdr );
852
853     msi_free(dest);
854     return ERROR_SUCCESS;
855 }
856
857 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
858 {
859     UINT rc;
860     MSIQUERY *view;
861     static const WCHAR query[] =
862         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
863          '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
864
865     rc = MSI_DatabaseOpenViewW( package->db, query, &view );
866     if (rc != ERROR_SUCCESS)
867         return ERROR_SUCCESS;
868
869     rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
870     msiobj_release( &view->hdr );
871
872     return rc;
873 }
874
875 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
876 {
877     INSTALLSTATE request = comp->ActionRequest;
878
879     if (request == INSTALLSTATE_UNKNOWN)
880         return FALSE;
881
882     if (install_mode == msidbRemoveFileInstallModeOnInstall &&
883         (request == INSTALLSTATE_LOCAL || request == INSTALLSTATE_SOURCE))
884         return TRUE;
885
886     if (request == INSTALLSTATE_ABSENT)
887     {
888         if (!comp->ComponentId)
889             return FALSE;
890
891         if (install_mode == msidbRemoveFileInstallModeOnRemove)
892             return TRUE;
893     }
894
895     if (install_mode == msidbRemoveFileInstallModeOnBoth)
896         return TRUE;
897
898     return FALSE;
899 }
900
901 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
902 {
903     MSIPACKAGE *package = param;
904     MSICOMPONENT *comp;
905     MSIRECORD *uirow;
906     LPCWSTR component, filename, dirprop;
907     UINT install_mode;
908     LPWSTR dir = NULL, path = NULL;
909     DWORD size;
910     UINT ret = ERROR_SUCCESS;
911
912     component = MSI_RecordGetString(row, 2);
913     filename = MSI_RecordGetString(row, 3);
914     dirprop = MSI_RecordGetString(row, 4);
915     install_mode = MSI_RecordGetInteger(row, 5);
916
917     comp = get_loaded_component(package, component);
918     if (!comp)
919     {
920         ERR("Invalid component: %s\n", debugstr_w(component));
921         return ERROR_FUNCTION_FAILED;
922     }
923
924     if (!comp->Enabled)
925     {
926         TRACE("component is disabled\n");
927         return ERROR_SUCCESS;
928     }
929
930     if (!verify_comp_for_removal(comp, install_mode))
931     {
932         TRACE("Skipping removal due to missing conditions\n");
933         comp->Action = comp->Installed;
934         return ERROR_SUCCESS;
935     }
936
937     dir = msi_dup_property(package->db, dirprop);
938     if (!dir)
939         return ERROR_OUTOFMEMORY;
940
941     size = (filename != NULL) ? lstrlenW(filename) : 0;
942     size += lstrlenW(dir) + 2;
943     path = msi_alloc(size * sizeof(WCHAR));
944     if (!path)
945     {
946         ret = ERROR_OUTOFMEMORY;
947         goto done;
948     }
949
950     if (filename)
951     {
952         lstrcpyW(path, dir);
953         PathAddBackslashW(path);
954         lstrcatW(path, filename);
955
956         TRACE("Deleting misc file: %s\n", debugstr_w(path));
957         DeleteFileW(path);
958     }
959     else
960     {
961         TRACE("Removing misc directory: %s\n", debugstr_w(dir));
962         RemoveDirectoryW(dir);
963     }
964
965 done:
966     uirow = MSI_CreateRecord( 9 );
967     MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
968     MSI_RecordSetStringW( uirow, 9, dir );
969     ui_actiondata( package, szRemoveFiles, uirow );
970     msiobj_release( &uirow->hdr );
971
972     msi_free(path);
973     msi_free(dir);
974     return ret;
975 }
976
977 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
978 {
979     MSIQUERY *view;
980     MSIFILE *file;
981     UINT r;
982
983     static const WCHAR query[] = {
984         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
985         '`','R','e','m','o','v','e','F','i','l','e','`',0};
986     static const WCHAR folder_query[] = {
987         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
988         '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0};
989
990     r = MSI_DatabaseOpenViewW(package->db, query, &view);
991     if (r == ERROR_SUCCESS)
992     {
993         MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
994         msiobj_release(&view->hdr);
995     }
996
997     r = MSI_DatabaseOpenViewW(package->db, folder_query, &view);
998     if (r == ERROR_SUCCESS)
999         msiobj_release(&view->hdr);
1000
1001     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1002     {
1003         MSIRECORD *uirow;
1004         LPWSTR dir, p;
1005         VS_FIXEDFILEINFO *ver;
1006
1007         if ( file->state == msifs_installed )
1008             ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1009
1010         if ( file->Component->ActionRequest != INSTALLSTATE_ABSENT ||
1011              file->Component->Installed == INSTALLSTATE_SOURCE )
1012             continue;
1013
1014         if (!file->Component->Enabled)
1015         {
1016             TRACE("component is disabled\n");
1017             continue;
1018         }
1019
1020         if (file->Version)
1021         {
1022             ver = msi_get_disk_file_version( file->TargetPath );
1023             if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1024             {
1025                 TRACE("newer version detected, not removing file\n");
1026                 msi_free( ver );
1027                 continue;
1028             }
1029             msi_free( ver );
1030         }
1031
1032         TRACE("removing %s\n", debugstr_w(file->File) );
1033         if (!DeleteFileW( file->TargetPath ))
1034         {
1035             WARN("failed to delete %s\n",  debugstr_w(file->TargetPath));
1036         }
1037         /* FIXME: check persistence for each directory */
1038         else if (r && (dir = strdupW( file->TargetPath )))
1039         {
1040             if ((p = strrchrW( dir, '\\' ))) *p = 0;
1041             RemoveDirectoryW( dir );
1042             msi_free( dir );
1043         }
1044         file->state = msifs_missing;
1045
1046         uirow = MSI_CreateRecord( 9 );
1047         MSI_RecordSetStringW( uirow, 1, file->FileName );
1048         MSI_RecordSetStringW( uirow, 9, file->Component->Directory );
1049         ui_actiondata( package, szRemoveFiles, uirow );
1050         msiobj_release( &uirow->hdr );
1051         /* FIXME: call ui_progress here? */
1052     }
1053
1054     return ERROR_SUCCESS;
1055 }