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