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