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