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