[WINESYNC] msi: Implement deferral for standard and custom actions.
[reactos.git] / dll / win32 / 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:
24 *
25 * InstallFiles
26 * DuplicateFiles
27 * MoveFiles
28 * PatchFiles
29 * RemoveDuplicateFiles
30 * RemoveFiles
31 */
32
33 #include <stdarg.h>
34
35 #define COBJMACROS
36
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "wine/debug.h"
41 #include "fdi.h"
42 #include "msi.h"
43 #include "msidefs.h"
44 #include "msipriv.h"
45 #include "winuser.h"
46 #include "winreg.h"
47 #include "shlwapi.h"
48 #include "patchapi.h"
49 #include "wine/unicode.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(msi);
52
53 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
54 {
55 MSIRECORD *uirow;
56
57 uirow = MSI_CreateRecord( 9 );
58 MSI_RecordSetStringW( uirow, 1, f->FileName );
59 MSI_RecordSetStringW( uirow, 9, f->Component->Directory );
60 MSI_RecordSetInteger( uirow, 6, f->FileSize );
61 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
62 msiobj_release( &uirow->hdr );
63 msi_ui_progress( package, 2, f->FileSize, 0, 0 );
64 }
65
66 static BOOL is_registered_patch_media( MSIPACKAGE *package, UINT disk_id )
67 {
68 MSIPATCHINFO *patch;
69
70 LIST_FOR_EACH_ENTRY( patch, &package->patches, MSIPATCHINFO, entry )
71 {
72 if (patch->disk_id == disk_id && patch->registered) return TRUE;
73 }
74 return FALSE;
75 }
76
77 static BOOL is_obsoleted_by_patch( MSIPACKAGE *package, MSIFILE *file )
78 {
79 if (!list_empty( &package->patches ) && file->disk_id < MSI_INITIAL_MEDIA_TRANSFORM_DISKID)
80 {
81 if (!msi_get_property_int( package->db, szInstalled, 0 )) return FALSE;
82 return TRUE;
83 }
84 if (is_registered_patch_media( package, file->disk_id )) return TRUE;
85 return FALSE;
86 }
87
88 static msi_file_state calculate_install_state( MSIPACKAGE *package, MSIFILE *file )
89 {
90 MSICOMPONENT *comp = file->Component;
91 VS_FIXEDFILEINFO *file_version;
92 WCHAR *font_version;
93 msi_file_state state;
94 DWORD size;
95
96 comp->Action = msi_get_component_action( package, comp );
97 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL || (comp->assembly && comp->assembly->installed))
98 {
99 TRACE("skipping %s (not scheduled for install)\n", debugstr_w(file->File));
100 return msifs_skipped;
101 }
102 if (is_obsoleted_by_patch( package, file ))
103 {
104 TRACE("skipping %s (obsoleted by patch)\n", debugstr_w(file->File));
105 return msifs_skipped;
106 }
107 if ((msi_is_global_assembly( comp ) && !comp->assembly->installed) ||
108 GetFileAttributesW( file->TargetPath ) == INVALID_FILE_ATTRIBUTES)
109 {
110 TRACE("installing %s (missing)\n", debugstr_w(file->File));
111 return msifs_missing;
112 }
113 if (file->Version)
114 {
115 if ((file_version = msi_get_disk_file_version( file->TargetPath )))
116 {
117 if (msi_compare_file_versions( file_version, file->Version ) < 0)
118 {
119 TRACE("overwriting %s (new version %s old version %u.%u.%u.%u)\n",
120 debugstr_w(file->File), debugstr_w(file->Version),
121 HIWORD(file_version->dwFileVersionMS), LOWORD(file_version->dwFileVersionMS),
122 HIWORD(file_version->dwFileVersionLS), LOWORD(file_version->dwFileVersionLS));
123 state = msifs_overwrite;
124 }
125 else
126 {
127 TRACE("keeping %s (new version %s old version %u.%u.%u.%u)\n",
128 debugstr_w(file->File), debugstr_w(file->Version),
129 HIWORD(file_version->dwFileVersionMS), LOWORD(file_version->dwFileVersionMS),
130 HIWORD(file_version->dwFileVersionLS), LOWORD(file_version->dwFileVersionLS));
131 state = msifs_present;
132 }
133 msi_free( file_version );
134 return state;
135 }
136 else if ((font_version = msi_font_version_from_file( file->TargetPath )))
137 {
138 if (msi_compare_font_versions( font_version, file->Version ) < 0)
139 {
140 TRACE("overwriting %s (new version %s old version %s)\n",
141 debugstr_w(file->File), debugstr_w(file->Version), debugstr_w(font_version));
142 state = msifs_overwrite;
143 }
144 else
145 {
146 TRACE("keeping %s (new version %s old version %s)\n",
147 debugstr_w(file->File), debugstr_w(file->Version), debugstr_w(font_version));
148 state = msifs_present;
149 }
150 msi_free( font_version );
151 return state;
152 }
153 }
154 if ((size = msi_get_disk_file_size( file->TargetPath )) != file->FileSize)
155 {
156 TRACE("overwriting %s (old size %u new size %u)\n", debugstr_w(file->File), size, file->FileSize);
157 return msifs_overwrite;
158 }
159 if (file->hash.dwFileHashInfoSize)
160 {
161 if (msi_file_hash_matches( file ))
162 {
163 TRACE("keeping %s (hash match)\n", debugstr_w(file->File));
164 return msifs_hashmatch;
165 }
166 else
167 {
168 TRACE("overwriting %s (hash mismatch)\n", debugstr_w(file->File));
169 return msifs_overwrite;
170 }
171 }
172 /* assume present */
173 TRACE("keeping %s\n", debugstr_w(file->File));
174 return msifs_present;
175 }
176
177 static void schedule_install_files(MSIPACKAGE *package)
178 {
179 MSIFILE *file;
180
181 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
182 {
183 MSICOMPONENT *comp = file->Component;
184
185 file->state = calculate_install_state( package, file );
186 if (file->state == msifs_overwrite && (comp->Attributes & msidbComponentAttributesNeverOverwrite))
187 {
188 TRACE("not overwriting %s\n", debugstr_w(file->TargetPath));
189 file->state = msifs_skipped;
190 }
191 }
192 }
193
194 static UINT copy_file(MSIFILE *file, LPWSTR source)
195 {
196 BOOL ret;
197
198 ret = CopyFileW(source, file->TargetPath, FALSE);
199 if (!ret)
200 return GetLastError();
201
202 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
203 return ERROR_SUCCESS;
204 }
205
206 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
207 {
208 UINT gle;
209
210 TRACE("Copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
211
212 gle = copy_file(file, source);
213 if (gle == ERROR_SUCCESS)
214 return gle;
215
216 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
217 {
218 TRACE("overwriting existing file\n");
219 return ERROR_SUCCESS;
220 }
221 else if (gle == ERROR_ACCESS_DENIED)
222 {
223 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
224
225 gle = copy_file(file, source);
226 TRACE("Overwriting existing file: %d\n", gle);
227 }
228 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
229 {
230 WCHAR *tmpfileW, *pathW, *p;
231 DWORD len;
232
233 TRACE("file in use, scheduling rename operation\n");
234
235 if (!(pathW = strdupW( file->TargetPath ))) return ERROR_OUTOFMEMORY;
236 if ((p = strrchrW(pathW, '\\'))) *p = 0;
237 len = strlenW( pathW ) + 16;
238 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
239 {
240 msi_free( pathW );
241 return ERROR_OUTOFMEMORY;
242 }
243 if (!GetTempFileNameW( pathW, szMsi, 0, tmpfileW )) tmpfileW[0] = 0;
244 msi_free( pathW );
245
246 if (CopyFileW(source, tmpfileW, FALSE) &&
247 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
248 MoveFileExW(tmpfileW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
249 {
250 package->need_reboot_at_end = 1;
251 gle = ERROR_SUCCESS;
252 }
253 else
254 {
255 gle = GetLastError();
256 WARN("failed to schedule rename operation: %d)\n", gle);
257 DeleteFileW( tmpfileW );
258 }
259 msi_free(tmpfileW);
260 }
261
262 return gle;
263 }
264
265 static UINT msi_create_directory( MSIPACKAGE *package, const WCHAR *dir )
266 {
267 MSIFOLDER *folder;
268 const WCHAR *install_path;
269
270 install_path = msi_get_target_folder( package, dir );
271 if (!install_path) return ERROR_FUNCTION_FAILED;
272
273 folder = msi_get_loaded_folder( package, dir );
274 if (folder->State == FOLDER_STATE_UNINITIALIZED)
275 {
276 msi_create_full_path( install_path );
277 folder->State = FOLDER_STATE_CREATED;
278 }
279 return ERROR_SUCCESS;
280 }
281
282 static MSIFILE *find_file( MSIPACKAGE *package, UINT disk_id, const WCHAR *filename )
283 {
284 MSIFILE *file;
285
286 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
287 {
288 if (file->disk_id == disk_id &&
289 file->state != msifs_installed &&
290 !strcmpiW( filename, file->File )) return file;
291 }
292 return NULL;
293 }
294
295 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR filename, DWORD action,
296 LPWSTR *path, DWORD *attrs, PVOID user)
297 {
298 MSIFILE *file = *(MSIFILE **)user;
299
300 if (action == MSICABEXTRACT_BEGINEXTRACT)
301 {
302 if (!(file = find_file( package, file->disk_id, filename )))
303 {
304 TRACE("unknown file in cabinet (%s)\n", debugstr_w(filename));
305 return FALSE;
306 }
307 if (file->state != msifs_missing && file->state != msifs_overwrite)
308 return FALSE;
309
310 if (!msi_is_global_assembly( file->Component ))
311 {
312 msi_create_directory( package, file->Component->Directory );
313 }
314 *path = strdupW( file->TargetPath );
315 *attrs = file->Attributes;
316 *(MSIFILE **)user = file;
317 }
318 else if (action == MSICABEXTRACT_FILEEXTRACTED)
319 {
320 if (!msi_is_global_assembly( file->Component )) file->state = msifs_installed;
321 }
322
323 return TRUE;
324 }
325
326 WCHAR *msi_resolve_file_source( MSIPACKAGE *package, MSIFILE *file )
327 {
328 WCHAR *p, *path;
329
330 TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
331
332 if (file->IsCompressed) return NULL;
333
334 p = msi_resolve_source_folder( package, file->Component->Directory, NULL );
335 path = msi_build_directory_name( 2, p, file->ShortName );
336
337 if (file->LongName && GetFileAttributesW( path ) == INVALID_FILE_ATTRIBUTES)
338 {
339 msi_free( path );
340 path = msi_build_directory_name( 2, p, file->LongName );
341 }
342 msi_free( p );
343 TRACE("file %s source resolves to %s\n", debugstr_w(file->File), debugstr_w(path));
344 return path;
345 }
346
347 /*
348 * ACTION_InstallFiles()
349 *
350 * For efficiency, this is done in two passes:
351 * 1) Correct all the TargetPaths and determine what files are to be installed.
352 * 2) Extract Cabinets and copy files.
353 */
354 UINT ACTION_InstallFiles(MSIPACKAGE *package)
355 {
356 MSIMEDIAINFO *mi;
357 UINT rc = ERROR_SUCCESS;
358 MSIFILE *file;
359
360 msi_set_sourcedir_props(package, FALSE);
361
362 if (package->script == SCRIPT_NONE)
363 return msi_schedule_action(package, SCRIPT_INSTALL, szInstallFiles);
364
365 schedule_install_files(package);
366 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
367
368 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
369 {
370 BOOL is_global_assembly = msi_is_global_assembly( file->Component );
371
372 msi_file_update_ui( package, file, szInstallFiles );
373
374 rc = msi_load_media_info( package, file->Sequence, mi );
375 if (rc != ERROR_SUCCESS)
376 {
377 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
378 rc = ERROR_FUNCTION_FAILED;
379 goto done;
380 }
381
382 if (file->state != msifs_hashmatch &&
383 file->state != msifs_skipped &&
384 (file->state != msifs_present || !msi_get_property_int( package->db, szInstalled, 0 )) &&
385 (rc = ready_media( package, file->IsCompressed, mi )))
386 {
387 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
388 goto done;
389 }
390
391 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
392 continue;
393
394 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
395 (file->IsCompressed && !mi->is_extracted))
396 {
397 MSICABDATA data;
398 MSIFILE *cursor = file;
399
400 data.mi = mi;
401 data.package = package;
402 data.cb = installfiles_cb;
403 data.user = &cursor;
404
405 if (file->IsCompressed && !msi_cabextract(package, mi, &data))
406 {
407 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
408 rc = ERROR_INSTALL_FAILURE;
409 goto done;
410 }
411 }
412
413 if (!file->IsCompressed)
414 {
415 WCHAR *source = msi_resolve_file_source(package, file);
416
417 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
418
419 if (!is_global_assembly)
420 {
421 msi_create_directory(package, file->Component->Directory);
422 }
423 rc = copy_install_file(package, file, source);
424 if (rc != ERROR_SUCCESS)
425 {
426 ERR("Failed to copy %s to %s (%u)\n", debugstr_w(source), debugstr_w(file->TargetPath), rc);
427 rc = ERROR_INSTALL_FAILURE;
428 msi_free(source);
429 goto done;
430 }
431 if (!is_global_assembly) file->state = msifs_installed;
432 msi_free(source);
433 }
434 else if (!is_global_assembly && file->state != msifs_installed &&
435 !(file->Attributes & msidbFileAttributesPatchAdded))
436 {
437 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->File));
438 rc = ERROR_INSTALL_FAILURE;
439 goto done;
440 }
441 }
442 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
443 {
444 MSICOMPONENT *comp = file->Component;
445
446 if (!msi_is_global_assembly( comp ) || comp->assembly->installed ||
447 (file->state != msifs_missing && file->state != msifs_overwrite)) continue;
448
449 rc = msi_install_assembly( package, comp );
450 if (rc != ERROR_SUCCESS)
451 {
452 ERR("Failed to install assembly\n");
453 rc = ERROR_INSTALL_FAILURE;
454 break;
455 }
456 file->state = msifs_installed;
457 }
458
459 done:
460 msi_free_media_info(mi);
461 return rc;
462 }
463
464 static MSIFILEPATCH *find_filepatch( MSIPACKAGE *package, UINT disk_id, const WCHAR *key )
465 {
466 MSIFILEPATCH *patch;
467
468 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
469 {
470 if (!patch->extracted && patch->disk_id == disk_id && !strcmpW( key, patch->File->File ))
471 return patch;
472 }
473 return NULL;
474 }
475
476 static BOOL patchfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
477 LPWSTR *path, DWORD *attrs, PVOID user)
478 {
479 MSIFILEPATCH *patch = *(MSIFILEPATCH **)user;
480
481 if (action == MSICABEXTRACT_BEGINEXTRACT)
482 {
483 MSICOMPONENT *comp;
484
485 if (is_registered_patch_media( package, patch->disk_id ) ||
486 !(patch = find_filepatch( package, patch->disk_id, file ))) return FALSE;
487
488 comp = patch->File->Component;
489 comp->Action = msi_get_component_action( package, comp );
490 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL)
491 {
492 TRACE("file %s component %s not installed or disabled\n",
493 debugstr_w(patch->File->File), debugstr_w(comp->Component));
494 return FALSE;
495 }
496
497 patch->path = msi_create_temp_file( package->db );
498 *path = strdupW( patch->path );
499 *attrs = patch->File->Attributes;
500 *(MSIFILEPATCH **)user = patch;
501 }
502 else if (action == MSICABEXTRACT_FILEEXTRACTED)
503 {
504 patch->extracted = TRUE;
505 }
506
507 return TRUE;
508 }
509
510 static UINT patch_file( MSIPACKAGE *package, MSIFILEPATCH *patch )
511 {
512 UINT r = ERROR_SUCCESS;
513 WCHAR *tmpfile = msi_create_temp_file( package->db );
514
515 if (!tmpfile) return ERROR_INSTALL_FAILURE;
516 if (ApplyPatchToFileW( patch->path, patch->File->TargetPath, tmpfile, 0 ))
517 {
518 DeleteFileW( patch->File->TargetPath );
519 MoveFileW( tmpfile, patch->File->TargetPath );
520 }
521 else
522 {
523 WARN("failed to patch %s: %08x\n", debugstr_w(patch->File->TargetPath), GetLastError());
524 r = ERROR_INSTALL_FAILURE;
525 }
526 DeleteFileW( patch->path );
527 DeleteFileW( tmpfile );
528 msi_free( tmpfile );
529 return r;
530 }
531
532 static UINT patch_assembly( MSIPACKAGE *package, MSIASSEMBLY *assembly, MSIFILEPATCH *patch )
533 {
534 UINT r = ERROR_FUNCTION_FAILED;
535 IAssemblyName *name;
536 IAssemblyEnum *iter;
537
538 if (!(iter = msi_create_assembly_enum( package, assembly->display_name )))
539 return ERROR_FUNCTION_FAILED;
540
541 while ((IAssemblyEnum_GetNextAssembly( iter, NULL, &name, 0 ) == S_OK))
542 {
543 WCHAR *displayname, *path;
544 UINT len = 0;
545 HRESULT hr;
546
547 hr = IAssemblyName_GetDisplayName( name, NULL, &len, 0 );
548 if (hr != E_NOT_SUFFICIENT_BUFFER || !(displayname = msi_alloc( len * sizeof(WCHAR) )))
549 break;
550
551 hr = IAssemblyName_GetDisplayName( name, displayname, &len, 0 );
552 if (FAILED( hr ))
553 {
554 msi_free( displayname );
555 break;
556 }
557
558 if ((path = msi_get_assembly_path( package, displayname )))
559 {
560 if (!CopyFileW( path, patch->File->TargetPath, FALSE ))
561 {
562 ERR("Failed to copy file %s -> %s (%u)\n", debugstr_w(path),
563 debugstr_w(patch->File->TargetPath), GetLastError() );
564 msi_free( path );
565 msi_free( displayname );
566 IAssemblyName_Release( name );
567 break;
568 }
569 r = patch_file( package, patch );
570 msi_free( path );
571 }
572
573 msi_free( displayname );
574 IAssemblyName_Release( name );
575 if (r == ERROR_SUCCESS) break;
576 }
577
578 IAssemblyEnum_Release( iter );
579 return r;
580 }
581
582 UINT ACTION_PatchFiles( MSIPACKAGE *package )
583 {
584 MSIFILEPATCH *patch;
585 MSIMEDIAINFO *mi;
586 UINT rc = ERROR_SUCCESS;
587
588 TRACE("%p\n", package);
589
590 if (package->script == SCRIPT_NONE)
591 return msi_schedule_action(package, SCRIPT_INSTALL, szPatchFiles);
592
593 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
594
595 TRACE("extracting files\n");
596
597 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
598 {
599 MSIFILE *file = patch->File;
600 MSICOMPONENT *comp = file->Component;
601
602 rc = msi_load_media_info( package, patch->Sequence, mi );
603 if (rc != ERROR_SUCCESS)
604 {
605 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
606 rc = ERROR_FUNCTION_FAILED;
607 goto done;
608 }
609 comp->Action = msi_get_component_action( package, comp );
610 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL) continue;
611
612 if (!patch->extracted)
613 {
614 MSICABDATA data;
615 MSIFILEPATCH *cursor = patch;
616
617 rc = ready_media( package, TRUE, mi );
618 if (rc != ERROR_SUCCESS)
619 {
620 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
621 goto done;
622 }
623 data.mi = mi;
624 data.package = package;
625 data.cb = patchfiles_cb;
626 data.user = &cursor;
627
628 if (!msi_cabextract( package, mi, &data ))
629 {
630 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
631 rc = ERROR_INSTALL_FAILURE;
632 goto done;
633 }
634 }
635 }
636
637 TRACE("applying patches\n");
638
639 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
640 {
641 MSICOMPONENT *comp = patch->File->Component;
642
643 if (!patch->path) continue;
644
645 if (msi_is_global_assembly( comp ))
646 rc = patch_assembly( package, comp->assembly, patch );
647 else
648 rc = patch_file( package, patch );
649
650 if (rc && !(patch->Attributes & msidbPatchAttributesNonVital))
651 {
652 ERR("Failed to apply patch to file: %s\n", debugstr_w(patch->File->File));
653 break;
654 }
655
656 if (msi_is_global_assembly( comp ))
657 {
658 if ((rc = msi_install_assembly( package, comp )))
659 {
660 ERR("Failed to install patched assembly\n");
661 break;
662 }
663 }
664 }
665
666 done:
667 msi_free_media_info(mi);
668 return rc;
669 }
670
671 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
672
673 typedef struct
674 {
675 struct list entry;
676 LPWSTR sourcename;
677 LPWSTR destname;
678 LPWSTR source;
679 LPWSTR dest;
680 } FILE_LIST;
681
682 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
683 {
684 BOOL ret;
685
686 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
687 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
688 {
689 WARN("Source or dest is directory, not moving\n");
690 return FALSE;
691 }
692
693 if (options == msidbMoveFileOptionsMove)
694 {
695 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
696 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
697 if (!ret)
698 {
699 WARN("MoveFile failed: %d\n", GetLastError());
700 return FALSE;
701 }
702 }
703 else
704 {
705 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
706 ret = CopyFileW(source, dest, FALSE);
707 if (!ret)
708 {
709 WARN("CopyFile failed: %d\n", GetLastError());
710 return FALSE;
711 }
712 }
713
714 return TRUE;
715 }
716
717 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
718 {
719 LPWSTR path, ptr;
720 DWORD dirlen, pathlen;
721
722 ptr = strrchrW(wildcard, '\\');
723 dirlen = ptr - wildcard + 1;
724
725 pathlen = dirlen + lstrlenW(filename) + 1;
726 path = msi_alloc(pathlen * sizeof(WCHAR));
727
728 lstrcpynW(path, wildcard, dirlen + 1);
729 lstrcatW(path, filename);
730
731 return path;
732 }
733
734 static void free_file_entry(FILE_LIST *file)
735 {
736 msi_free(file->source);
737 msi_free(file->dest);
738 msi_free(file);
739 }
740
741 static void free_list(FILE_LIST *list)
742 {
743 while (!list_empty(&list->entry))
744 {
745 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
746
747 list_remove(&file->entry);
748 free_file_entry(file);
749 }
750 }
751
752 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
753 {
754 FILE_LIST *new, *file;
755 LPWSTR ptr, filename;
756 DWORD size;
757
758 new = msi_alloc_zero(sizeof(FILE_LIST));
759 if (!new)
760 return FALSE;
761
762 new->source = strdupW(source);
763 ptr = strrchrW(dest, '\\') + 1;
764 filename = strrchrW(new->source, '\\') + 1;
765
766 new->sourcename = filename;
767
768 if (*ptr)
769 new->destname = ptr;
770 else
771 new->destname = new->sourcename;
772
773 size = (ptr - dest) + lstrlenW(filename) + 1;
774 new->dest = msi_alloc(size * sizeof(WCHAR));
775 if (!new->dest)
776 {
777 free_file_entry(new);
778 return FALSE;
779 }
780
781 lstrcpynW(new->dest, dest, ptr - dest + 1);
782 lstrcatW(new->dest, filename);
783
784 if (list_empty(&files->entry))
785 {
786 list_add_head(&files->entry, &new->entry);
787 return TRUE;
788 }
789
790 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
791 {
792 if (strcmpW( source, file->source ) < 0)
793 {
794 list_add_before(&file->entry, &new->entry);
795 return TRUE;
796 }
797 }
798
799 list_add_after(&file->entry, &new->entry);
800 return TRUE;
801 }
802
803 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
804 {
805 WIN32_FIND_DATAW wfd;
806 HANDLE hfile;
807 LPWSTR path;
808 BOOL res;
809 FILE_LIST files, *file;
810 DWORD size;
811
812 hfile = FindFirstFileW(source, &wfd);
813 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
814
815 list_init(&files.entry);
816
817 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
818 {
819 if (is_dot_dir(wfd.cFileName)) continue;
820
821 path = wildcard_to_file(source, wfd.cFileName);
822 if (!path)
823 {
824 res = FALSE;
825 goto done;
826 }
827
828 add_wildcard(&files, path, dest);
829 msi_free(path);
830 }
831
832 /* no files match the wildcard */
833 if (list_empty(&files.entry))
834 goto done;
835
836 /* only the first wildcard match gets renamed to dest */
837 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
838 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
839 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
840 if (!file->dest)
841 {
842 res = FALSE;
843 goto done;
844 }
845
846 /* file->dest may be shorter after the reallocation, so add a NULL
847 * terminator. This is needed for the call to strrchrW, as there will no
848 * longer be a NULL terminator within the bounds of the allocation in this case.
849 */
850 file->dest[size - 1] = '\0';
851 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
852
853 while (!list_empty(&files.entry))
854 {
855 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
856
857 msi_move_file(file->source, file->dest, options);
858
859 list_remove(&file->entry);
860 free_file_entry(file);
861 }
862
863 res = TRUE;
864
865 done:
866 free_list(&files);
867 FindClose(hfile);
868 return res;
869 }
870
871 void msi_reduce_to_long_filename( WCHAR *filename )
872 {
873 WCHAR *p = strchrW( filename, '|' );
874 if (p) memmove( filename, p + 1, (strlenW( p + 1 ) + 1) * sizeof(WCHAR) );
875 }
876
877 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
878 {
879 MSIPACKAGE *package = param;
880 MSIRECORD *uirow;
881 MSICOMPONENT *comp;
882 LPCWSTR sourcename, component;
883 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
884 int options;
885 DWORD size;
886 BOOL wildcards;
887
888 component = MSI_RecordGetString(rec, 2);
889 comp = msi_get_loaded_component(package, component);
890 if (!comp)
891 return ERROR_SUCCESS;
892
893 comp->Action = msi_get_component_action( package, comp );
894 if (comp->Action != INSTALLSTATE_LOCAL)
895 {
896 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
897 return ERROR_SUCCESS;
898 }
899
900 sourcename = MSI_RecordGetString(rec, 3);
901 options = MSI_RecordGetInteger(rec, 7);
902
903 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
904 if (!sourcedir)
905 goto done;
906
907 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
908 if (!destdir)
909 goto done;
910
911 if (!sourcename)
912 {
913 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
914 goto done;
915
916 source = strdupW(sourcedir);
917 if (!source)
918 goto done;
919 }
920 else
921 {
922 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
923 source = msi_alloc(size * sizeof(WCHAR));
924 if (!source)
925 goto done;
926
927 lstrcpyW(source, sourcedir);
928 if (source[lstrlenW(source) - 1] != '\\')
929 lstrcatW(source, szBackSlash);
930 lstrcatW(source, sourcename);
931 }
932
933 wildcards = strchrW(source, '*') || strchrW(source, '?');
934
935 if (MSI_RecordIsNull(rec, 4))
936 {
937 if (!wildcards)
938 {
939 WCHAR *p;
940 if (sourcename)
941 destname = strdupW(sourcename);
942 else if ((p = strrchrW(sourcedir, '\\')))
943 destname = strdupW(p + 1);
944 else
945 destname = strdupW(sourcedir);
946 if (!destname)
947 goto done;
948 }
949 }
950 else
951 {
952 destname = strdupW(MSI_RecordGetString(rec, 4));
953 if (destname) msi_reduce_to_long_filename(destname);
954 }
955
956 size = 0;
957 if (destname)
958 size = lstrlenW(destname);
959
960 size += lstrlenW(destdir) + 2;
961 dest = msi_alloc(size * sizeof(WCHAR));
962 if (!dest)
963 goto done;
964
965 lstrcpyW(dest, destdir);
966 if (dest[lstrlenW(dest) - 1] != '\\')
967 lstrcatW(dest, szBackSlash);
968
969 if (destname)
970 lstrcatW(dest, destname);
971
972 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
973 {
974 if (!msi_create_full_path(destdir))
975 {
976 WARN("failed to create directory %u\n", GetLastError());
977 goto done;
978 }
979 }
980
981 if (!wildcards)
982 msi_move_file(source, dest, options);
983 else
984 move_files_wildcard(source, dest, options);
985
986 done:
987 uirow = MSI_CreateRecord( 9 );
988 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
989 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
990 MSI_RecordSetStringW( uirow, 9, destdir );
991 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
992 msiobj_release( &uirow->hdr );
993
994 msi_free(sourcedir);
995 msi_free(destdir);
996 msi_free(destname);
997 msi_free(source);
998 msi_free(dest);
999
1000 return ERROR_SUCCESS;
1001 }
1002
1003 UINT ACTION_MoveFiles( MSIPACKAGE *package )
1004 {
1005 static const WCHAR query[] = {
1006 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1007 '`','M','o','v','e','F','i','l','e','`',0};
1008 MSIQUERY *view;
1009 UINT rc;
1010
1011 if (package->script == SCRIPT_NONE)
1012 return msi_schedule_action(package, SCRIPT_INSTALL, szMoveFiles);
1013
1014 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
1015 if (rc != ERROR_SUCCESS)
1016 return ERROR_SUCCESS;
1017
1018 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
1019 msiobj_release(&view->hdr);
1020 return rc;
1021 }
1022
1023 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
1024 {
1025 DWORD len;
1026 WCHAR *dst_name, *dst_path, *dst;
1027
1028 if (MSI_RecordIsNull( row, 4 ))
1029 {
1030 len = strlenW( src ) + 1;
1031 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
1032 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
1033 }
1034 else
1035 {
1036 MSI_RecordGetStringW( row, 4, NULL, &len );
1037 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
1038 MSI_RecordGetStringW( row, 4, dst_name, &len );
1039 msi_reduce_to_long_filename( dst_name );
1040 }
1041
1042 if (MSI_RecordIsNull( row, 5 ))
1043 {
1044 WCHAR *p;
1045 dst_path = strdupW( src );
1046 p = strrchrW( dst_path, '\\' );
1047 if (p) *p = 0;
1048 }
1049 else
1050 {
1051 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
1052
1053 dst_path = strdupW( msi_get_target_folder( package, dst_key ) );
1054 if (!dst_path)
1055 {
1056 /* try a property */
1057 dst_path = msi_dup_property( package->db, dst_key );
1058 if (!dst_path)
1059 {
1060 FIXME("Unable to get destination folder, try AppSearch properties\n");
1061 msi_free( dst_name );
1062 return NULL;
1063 }
1064 }
1065 }
1066
1067 dst = msi_build_directory_name( 2, dst_path, dst_name );
1068 msi_create_full_path( dst_path );
1069
1070 msi_free( dst_name );
1071 msi_free( dst_path );
1072 return dst;
1073 }
1074
1075 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
1076 {
1077 MSIPACKAGE *package = param;
1078 LPWSTR dest;
1079 LPCWSTR file_key, component;
1080 MSICOMPONENT *comp;
1081 MSIRECORD *uirow;
1082 MSIFILE *file;
1083
1084 component = MSI_RecordGetString(row,2);
1085 comp = msi_get_loaded_component(package, component);
1086 if (!comp)
1087 return ERROR_SUCCESS;
1088
1089 comp->Action = msi_get_component_action( package, comp );
1090 if (comp->Action != INSTALLSTATE_LOCAL)
1091 {
1092 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
1093 return ERROR_SUCCESS;
1094 }
1095
1096 file_key = MSI_RecordGetString(row,3);
1097 if (!file_key)
1098 {
1099 ERR("Unable to get file key\n");
1100 return ERROR_FUNCTION_FAILED;
1101 }
1102
1103 file = msi_get_loaded_file( package, file_key );
1104 if (!file)
1105 {
1106 ERR("Original file unknown %s\n", debugstr_w(file_key));
1107 return ERROR_SUCCESS;
1108 }
1109
1110 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1111 if (!dest)
1112 {
1113 WARN("Unable to get duplicate filename\n");
1114 return ERROR_SUCCESS;
1115 }
1116
1117 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
1118
1119 if (!CopyFileW( file->TargetPath, dest, TRUE ))
1120 {
1121 WARN("Failed to copy file %s -> %s (%u)\n",
1122 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
1123 }
1124
1125 FIXME("We should track these duplicate files as well\n");
1126
1127 uirow = MSI_CreateRecord( 9 );
1128 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1129 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1130 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1131 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1132 msiobj_release( &uirow->hdr );
1133
1134 msi_free(dest);
1135 return ERROR_SUCCESS;
1136 }
1137
1138 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1139 {
1140 static const WCHAR query[] = {
1141 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1142 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1143 MSIQUERY *view;
1144 UINT rc;
1145
1146 if (package->script == SCRIPT_NONE)
1147 return msi_schedule_action(package, SCRIPT_INSTALL, szDuplicateFiles);
1148
1149 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
1150 if (rc != ERROR_SUCCESS)
1151 return ERROR_SUCCESS;
1152
1153 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1154 msiobj_release(&view->hdr);
1155 return rc;
1156 }
1157
1158 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1159 {
1160 MSIPACKAGE *package = param;
1161 LPWSTR dest;
1162 LPCWSTR file_key, component;
1163 MSICOMPONENT *comp;
1164 MSIRECORD *uirow;
1165 MSIFILE *file;
1166
1167 component = MSI_RecordGetString( row, 2 );
1168 comp = msi_get_loaded_component( package, component );
1169 if (!comp)
1170 return ERROR_SUCCESS;
1171
1172 comp->Action = msi_get_component_action( package, comp );
1173 if (comp->Action != INSTALLSTATE_ABSENT)
1174 {
1175 TRACE("component not scheduled for removal %s\n", debugstr_w(component));
1176 return ERROR_SUCCESS;
1177 }
1178
1179 file_key = MSI_RecordGetString( row, 3 );
1180 if (!file_key)
1181 {
1182 ERR("Unable to get file key\n");
1183 return ERROR_FUNCTION_FAILED;
1184 }
1185
1186 file = msi_get_loaded_file( package, file_key );
1187 if (!file)
1188 {
1189 ERR("Original file unknown %s\n", debugstr_w(file_key));
1190 return ERROR_SUCCESS;
1191 }
1192
1193 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1194 if (!dest)
1195 {
1196 WARN("Unable to get duplicate filename\n");
1197 return ERROR_SUCCESS;
1198 }
1199
1200 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1201
1202 if (!DeleteFileW( dest ))
1203 {
1204 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1205 }
1206
1207 uirow = MSI_CreateRecord( 9 );
1208 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1209 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1210 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1211 msiobj_release( &uirow->hdr );
1212
1213 msi_free(dest);
1214 return ERROR_SUCCESS;
1215 }
1216
1217 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1218 {
1219 static const WCHAR query[] = {
1220 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1221 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1222 MSIQUERY *view;
1223 UINT rc;
1224
1225 if (package->script == SCRIPT_NONE)
1226 return msi_schedule_action(package, SCRIPT_INSTALL, szRemoveDuplicateFiles);
1227
1228 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
1229 if (rc != ERROR_SUCCESS)
1230 return ERROR_SUCCESS;
1231
1232 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1233 msiobj_release( &view->hdr );
1234 return rc;
1235 }
1236
1237 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1238 {
1239 /* special case */
1240 if (comp->Action != INSTALLSTATE_SOURCE &&
1241 comp->Attributes & msidbComponentAttributesSourceOnly &&
1242 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1243 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1244
1245 switch (comp->Action)
1246 {
1247 case INSTALLSTATE_LOCAL:
1248 case INSTALLSTATE_SOURCE:
1249 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1250 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1251 break;
1252 case INSTALLSTATE_ABSENT:
1253 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1254 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1255 break;
1256 default: break;
1257 }
1258 return FALSE;
1259 }
1260
1261 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1262 {
1263 MSIPACKAGE *package = param;
1264 MSICOMPONENT *comp;
1265 MSIRECORD *uirow;
1266 LPCWSTR component, dirprop;
1267 UINT install_mode;
1268 LPWSTR dir = NULL, path = NULL, filename = NULL;
1269 DWORD size;
1270 UINT ret = ERROR_SUCCESS;
1271
1272 component = MSI_RecordGetString(row, 2);
1273 dirprop = MSI_RecordGetString(row, 4);
1274 install_mode = MSI_RecordGetInteger(row, 5);
1275
1276 comp = msi_get_loaded_component(package, component);
1277 if (!comp)
1278 return ERROR_SUCCESS;
1279
1280 comp->Action = msi_get_component_action( package, comp );
1281 if (!verify_comp_for_removal(comp, install_mode))
1282 {
1283 TRACE("Skipping removal due to install mode\n");
1284 return ERROR_SUCCESS;
1285 }
1286 if (comp->assembly && !comp->assembly->application)
1287 {
1288 return ERROR_SUCCESS;
1289 }
1290 if (comp->Attributes & msidbComponentAttributesPermanent)
1291 {
1292 TRACE("permanent component, not removing file\n");
1293 return ERROR_SUCCESS;
1294 }
1295
1296 dir = msi_dup_property(package->db, dirprop);
1297 if (!dir)
1298 {
1299 WARN("directory property has no value\n");
1300 return ERROR_SUCCESS;
1301 }
1302 size = 0;
1303 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1304 {
1305 msi_reduce_to_long_filename( filename );
1306 size = lstrlenW( filename );
1307 }
1308 size += lstrlenW(dir) + 2;
1309 path = msi_alloc(size * sizeof(WCHAR));
1310 if (!path)
1311 {
1312 ret = ERROR_OUTOFMEMORY;
1313 goto done;
1314 }
1315
1316 if (filename)
1317 {
1318 lstrcpyW(path, dir);
1319 PathAddBackslashW(path);
1320 lstrcatW(path, filename);
1321
1322 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1323 DeleteFileW(path);
1324 }
1325 else
1326 {
1327 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1328 RemoveDirectoryW(dir);
1329 }
1330
1331 done:
1332 uirow = MSI_CreateRecord( 9 );
1333 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1334 MSI_RecordSetStringW( uirow, 9, dir );
1335 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1336 msiobj_release( &uirow->hdr );
1337
1338 msi_free(filename);
1339 msi_free(path);
1340 msi_free(dir);
1341 return ret;
1342 }
1343
1344 static void remove_folder( MSIFOLDER *folder )
1345 {
1346 FolderList *fl;
1347
1348 LIST_FOR_EACH_ENTRY( fl, &folder->children, FolderList, entry )
1349 {
1350 remove_folder( fl->folder );
1351 }
1352 if (!folder->persistent && folder->State != FOLDER_STATE_REMOVED)
1353 {
1354 if (RemoveDirectoryW( folder->ResolvedTarget )) folder->State = FOLDER_STATE_REMOVED;
1355 }
1356 }
1357
1358 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1359 {
1360 static const WCHAR query[] = {
1361 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1362 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1363 MSIQUERY *view;
1364 MSICOMPONENT *comp;
1365 MSIFILE *file;
1366 UINT r;
1367
1368 if (package->script == SCRIPT_NONE)
1369 return msi_schedule_action(package, SCRIPT_INSTALL, szRemoveFiles);
1370
1371 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1372 if (r == ERROR_SUCCESS)
1373 {
1374 r = MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1375 msiobj_release(&view->hdr);
1376 if (r != ERROR_SUCCESS)
1377 return r;
1378 }
1379
1380 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1381 {
1382 MSIRECORD *uirow;
1383 VS_FIXEDFILEINFO *ver;
1384
1385 comp = file->Component;
1386 msi_file_update_ui( package, file, szRemoveFiles );
1387
1388 comp->Action = msi_get_component_action( package, comp );
1389 if (comp->Action != INSTALLSTATE_ABSENT || comp->Installed == INSTALLSTATE_SOURCE)
1390 continue;
1391
1392 if (comp->assembly && !comp->assembly->application)
1393 continue;
1394
1395 if (comp->Attributes & msidbComponentAttributesPermanent)
1396 {
1397 TRACE("permanent component, not removing file\n");
1398 continue;
1399 }
1400
1401 if (file->Version)
1402 {
1403 ver = msi_get_disk_file_version( file->TargetPath );
1404 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1405 {
1406 TRACE("newer version detected, not removing file\n");
1407 msi_free( ver );
1408 continue;
1409 }
1410 msi_free( ver );
1411 }
1412
1413 if (file->state == msifs_installed)
1414 WARN("removing installed file %s\n", debugstr_w(file->TargetPath));
1415
1416 TRACE("removing %s\n", debugstr_w(file->File) );
1417
1418 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1419 if (!DeleteFileW( file->TargetPath ))
1420 {
1421 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1422 }
1423 file->state = msifs_missing;
1424
1425 uirow = MSI_CreateRecord( 9 );
1426 MSI_RecordSetStringW( uirow, 1, file->FileName );
1427 MSI_RecordSetStringW( uirow, 9, comp->Directory );
1428 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1429 msiobj_release( &uirow->hdr );
1430 }
1431
1432 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
1433 {
1434 comp->Action = msi_get_component_action( package, comp );
1435 if (comp->Action != INSTALLSTATE_ABSENT) continue;
1436
1437 if (comp->Attributes & msidbComponentAttributesPermanent)
1438 {
1439 TRACE("permanent component, not removing directory\n");
1440 continue;
1441 }
1442 if (comp->assembly && !comp->assembly->application)
1443 msi_uninstall_assembly( package, comp );
1444 else
1445 {
1446 MSIFOLDER *folder = msi_get_loaded_folder( package, comp->Directory );
1447 remove_folder( folder );
1448 }
1449 }
1450 return ERROR_SUCCESS;
1451 }