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