msi: Properly update the UI in the DuplicateFiles and RemoveDuplicateFiles actions.
[wine/multimedia.git] / dlls / msi / files.c
blobc55bbfa75f94b0ec2190c0d3cce7e72ce7f25235
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Aric Stewart for CodeWeavers
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.
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.
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
23 * Actions dealing with files These are
25 * InstallFiles
26 * DuplicateFiles
27 * MoveFiles
28 * PatchFiles (TODO)
29 * RemoveDuplicateFiles
30 * RemoveFiles
33 #include <stdarg.h>
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"
48 WINE_DEFAULT_DEBUG_CHANNEL(msi);
50 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
52 MSIRECORD *uirow;
53 LPWSTR uipath, p;
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);
70 /* compares the version of a file read from the filesystem and
71 * the version specified in the File table
73 static int msi_compare_file_version(MSIFILE *file)
75 WCHAR version[MAX_PATH];
76 DWORD size;
77 UINT r;
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;
85 return lstrcmpW(version, file->Version);
88 static void schedule_install_files(MSIPACKAGE *package)
90 MSIFILE *file;
92 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
94 if (file->Component->ActionRequest != INSTALLSTATE_LOCAL)
96 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
98 ui_progress(package,2,file->FileSize,0,0);
99 file->state = msifs_skipped;
104 static UINT copy_file(MSIFILE *file, LPWSTR source)
106 BOOL ret;
108 ret = CopyFileW(source, file->TargetPath, FALSE);
109 if (!ret)
110 return GetLastError();
112 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
114 file->state = msifs_installed;
115 return ERROR_SUCCESS;
118 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
120 UINT gle;
122 TRACE("Copying %s to %s\n", debugstr_w(source),
123 debugstr_w(file->TargetPath));
125 gle = copy_file(file, source);
126 if (gle == ERROR_SUCCESS)
127 return gle;
129 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
131 TRACE("overwriting existing file\n");
132 return ERROR_SUCCESS;
134 else if (gle == ERROR_ACCESS_DENIED)
136 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
138 gle = copy_file(file, source);
139 TRACE("Overwriting existing file: %d\n", gle);
141 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
143 WCHAR tmpfileW[MAX_PATH], *pathW, *p;
144 DWORD len;
146 TRACE("file in use, scheduling rename operation\n");
148 GetTempFileNameW(szBackSlash, szMsi, 0, tmpfileW);
149 len = strlenW(file->TargetPath) + strlenW(tmpfileW) + 1;
150 if (!(pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
151 return ERROR_OUTOFMEMORY;
153 strcpyW(pathW, file->TargetPath);
154 if ((p = strrchrW(pathW, '\\'))) *p = 0;
155 strcatW(pathW, tmpfileW);
157 if (CopyFileW(source, pathW, FALSE) &&
158 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
159 MoveFileExW(pathW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
161 file->state = msifs_installed;
162 package->need_reboot = 1;
163 gle = ERROR_SUCCESS;
165 else
167 gle = GetLastError();
168 WARN("failed to schedule rename operation: %d)\n", gle);
170 HeapFree(GetProcessHeap(), 0, pathW);
173 return gle;
176 static BOOL check_dest_hash_matches(MSIFILE *file)
178 MSIFILEHASHINFO hash;
179 UINT r;
181 if (!file->hash.dwFileHashInfoSize)
182 return FALSE;
184 hash.dwFileHashInfoSize = sizeof(MSIFILEHASHINFO);
185 r = MsiGetFileHashW(file->TargetPath, 0, &hash);
186 if (r != ERROR_SUCCESS)
187 return FALSE;
189 return !memcmp(&hash, &file->hash, sizeof(MSIFILEHASHINFO));
192 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
193 LPWSTR *path, DWORD *attrs, PVOID user)
195 static MSIFILE *f = NULL;
197 if (action == MSICABEXTRACT_BEGINEXTRACT)
199 f = get_loaded_file(package, file);
200 if (!f)
202 WARN("unknown file in cabinet (%s)\n", debugstr_w(file));
203 return FALSE;
206 if (f->state != msifs_missing && f->state != msifs_overwrite)
208 TRACE("Skipping extraction of %s\n", debugstr_w(file));
209 return FALSE;
212 msi_file_update_ui(package, f, szInstallFiles);
214 *path = strdupW(f->TargetPath);
215 *attrs = f->Attributes;
217 else if (action == MSICABEXTRACT_FILEEXTRACTED)
219 f->state = msifs_installed;
220 f = NULL;
223 return TRUE;
227 * ACTION_InstallFiles()
229 * For efficiency, this is done in two passes:
230 * 1) Correct all the TargetPaths and determine what files are to be installed.
231 * 2) Extract Cabinets and copy files.
233 UINT ACTION_InstallFiles(MSIPACKAGE *package)
235 MSIMEDIAINFO *mi;
236 UINT rc = ERROR_SUCCESS;
237 MSIFILE *file;
239 /* increment progress bar each time action data is sent */
240 ui_progress(package,1,1,0,0);
242 schedule_install_files(package);
245 * Despite MSDN specifying that the CreateFolders action
246 * should be called before InstallFiles, some installers don't
247 * do that, and they seem to work correctly. We need to create
248 * directories here to make sure that the files can be copied.
250 msi_create_component_directories( package );
252 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
254 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
256 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
257 continue;
259 if (check_dest_hash_matches(file))
261 TRACE("File hashes match, not overwriting\n");
262 continue;
265 if (MsiGetFileVersionW(file->TargetPath, NULL, NULL, NULL, NULL) == ERROR_SUCCESS &&
266 msi_compare_file_version(file) >= 0)
268 TRACE("Destination file version greater, not overwriting\n");
269 continue;
272 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
273 (file->IsCompressed && !mi->is_extracted))
275 MSICABDATA data;
277 rc = ready_media(package, file, mi);
278 if (rc != ERROR_SUCCESS)
280 ERR("Failed to ready media\n");
281 break;
284 data.mi = mi;
285 data.package = package;
286 data.cb = installfiles_cb;
287 data.user = NULL;
289 if (file->IsCompressed &&
290 !msi_cabextract(package, mi, &data))
292 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
293 rc = ERROR_FUNCTION_FAILED;
294 break;
298 if (!file->IsCompressed)
300 LPWSTR source = resolve_file_source(package, file);
302 TRACE("file paths %s to %s\n", debugstr_w(source),
303 debugstr_w(file->TargetPath));
305 msi_file_update_ui(package, file, szInstallFiles);
306 rc = copy_install_file(package, file, source);
307 if (rc != ERROR_SUCCESS)
309 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
310 debugstr_w(file->TargetPath), rc);
311 rc = ERROR_INSTALL_FAILURE;
312 msi_free(source);
313 break;
316 msi_free(source);
318 else if (file->state != msifs_installed)
320 ERR("compressed file wasn't extracted (%s)\n",
321 debugstr_w(file->TargetPath));
322 rc = ERROR_INSTALL_FAILURE;
323 break;
327 msi_free_media_info(mi);
328 return rc;
331 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
333 DWORD len;
334 WCHAR *dst_name, *dst_path, *dst;
336 if (MSI_RecordIsNull( row, 4 ))
338 len = strlenW( src ) + 1;
339 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
340 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
342 else
344 MSI_RecordGetStringW( row, 4, NULL, &len );
345 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
346 MSI_RecordGetStringW( row, 4, dst_name, &len );
347 reduce_to_longfilename( dst_name );
350 if (MSI_RecordIsNull( row, 5 ))
352 WCHAR *p;
353 dst_path = strdupW( src );
354 p = strrchrW( dst_path, '\\' );
355 if (p) *p = 0;
357 else
359 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
361 dst_path = resolve_folder( package, dst_key, FALSE, FALSE, TRUE, NULL );
362 if (!dst_path)
364 /* try a property */
365 dst_path = msi_dup_property( package, dst_key );
366 if (!dst_path)
368 FIXME("Unable to get destination folder, try AppSearch properties\n");
369 msi_free( dst_name );
370 return NULL;
375 dst = build_directory_name( 2, dst_path, dst_name );
376 create_full_pathW( dst_path );
378 msi_free( dst_name );
379 msi_free( dst_path );
380 return dst;
383 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
385 MSIPACKAGE *package = param;
386 LPWSTR dest;
387 LPCWSTR file_key, component;
388 MSICOMPONENT *comp;
389 MSIRECORD *uirow;
390 MSIFILE *file;
392 component = MSI_RecordGetString(row,2);
393 comp = get_loaded_component(package,component);
394 if (!comp)
395 return ERROR_SUCCESS;
397 if (comp->ActionRequest != INSTALLSTATE_LOCAL)
399 TRACE("Component not scheduled for installation %s\n", debugstr_w(component));
400 comp->Action = comp->Installed;
401 return ERROR_SUCCESS;
403 comp->Action = INSTALLSTATE_LOCAL;
405 file_key = MSI_RecordGetString(row,3);
406 if (!file_key)
408 ERR("Unable to get file key\n");
409 return ERROR_FUNCTION_FAILED;
412 file = get_loaded_file( package, file_key );
413 if (!file)
415 ERR("Original file unknown %s\n", debugstr_w(file_key));
416 return ERROR_SUCCESS;
419 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
420 if (!dest)
422 WARN("Unable to get duplicate filename\n");
423 return ERROR_SUCCESS;
426 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
428 if (!CopyFileW( file->TargetPath, dest, TRUE ))
430 WARN("Failed to copy file %s -> %s (%u)\n",
431 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
434 FIXME("We should track these duplicate files as well\n");
436 uirow = MSI_CreateRecord( 9 );
437 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
438 MSI_RecordSetInteger( uirow, 6, file->FileSize );
439 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
440 ui_actiondata( package, szDuplicateFiles, uirow );
441 msiobj_release( &uirow->hdr );
443 msi_free(dest);
444 return ERROR_SUCCESS;
447 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
449 UINT rc;
450 MSIQUERY * view;
451 static const WCHAR ExecSeqQuery[] =
452 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
453 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
455 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
456 if (rc != ERROR_SUCCESS)
457 return ERROR_SUCCESS;
459 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
460 msiobj_release(&view->hdr);
462 return rc;
465 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
467 MSIPACKAGE *package = param;
468 LPWSTR dest;
469 LPCWSTR file_key, component;
470 MSICOMPONENT *comp;
471 MSIRECORD *uirow;
472 MSIFILE *file;
474 component = MSI_RecordGetString( row, 2 );
475 comp = get_loaded_component( package, component );
476 if (!comp)
477 return ERROR_SUCCESS;
479 if (comp->ActionRequest != INSTALLSTATE_ABSENT)
481 TRACE("Component not scheduled for removal %s\n", debugstr_w(component));
482 comp->Action = comp->Installed;
483 return ERROR_SUCCESS;
485 comp->Action = INSTALLSTATE_ABSENT;
487 file_key = MSI_RecordGetString( row, 3 );
488 if (!file_key)
490 ERR("Unable to get file key\n");
491 return ERROR_FUNCTION_FAILED;
494 file = get_loaded_file( package, file_key );
495 if (!file)
497 ERR("Original file unknown %s\n", debugstr_w(file_key));
498 return ERROR_SUCCESS;
501 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
502 if (!dest)
504 WARN("Unable to get duplicate filename\n");
505 return ERROR_SUCCESS;
508 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
510 if (!DeleteFileW( dest ))
512 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
515 uirow = MSI_CreateRecord( 9 );
516 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
517 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
518 ui_actiondata( package, szRemoveDuplicateFiles, uirow );
519 msiobj_release( &uirow->hdr );
521 msi_free(dest);
522 return ERROR_SUCCESS;
525 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
527 UINT rc;
528 MSIQUERY *view;
529 static const WCHAR query[] =
530 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
531 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
533 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
534 if (rc != ERROR_SUCCESS)
535 return ERROR_SUCCESS;
537 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
538 msiobj_release( &view->hdr );
540 return rc;
543 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
545 INSTALLSTATE request = comp->ActionRequest;
547 if (request == INSTALLSTATE_UNKNOWN)
548 return FALSE;
550 if (install_mode == msidbRemoveFileInstallModeOnInstall &&
551 (request == INSTALLSTATE_LOCAL || request == INSTALLSTATE_SOURCE))
552 return TRUE;
554 if (request == INSTALLSTATE_ABSENT)
556 if (!comp->ComponentId)
557 return FALSE;
559 if (install_mode == msidbRemoveFileInstallModeOnRemove)
560 return TRUE;
563 if (install_mode == msidbRemoveFileInstallModeOnBoth)
564 return TRUE;
566 return FALSE;
569 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
571 MSIPACKAGE *package = param;
572 MSICOMPONENT *comp;
573 LPCWSTR component, filename, dirprop;
574 UINT install_mode;
575 LPWSTR dir = NULL, path = NULL;
576 DWORD size;
577 UINT r;
579 component = MSI_RecordGetString(row, 2);
580 filename = MSI_RecordGetString(row, 3);
581 dirprop = MSI_RecordGetString(row, 4);
582 install_mode = MSI_RecordGetInteger(row, 5);
584 comp = get_loaded_component(package, component);
585 if (!comp)
587 ERR("Invalid component: %s\n", debugstr_w(component));
588 return ERROR_FUNCTION_FAILED;
591 if (!verify_comp_for_removal(comp, install_mode))
593 TRACE("Skipping removal due to missing conditions\n");
594 comp->Action = comp->Installed;
595 return ERROR_SUCCESS;
598 dir = msi_dup_property(package, dirprop);
599 if (!dir)
600 return ERROR_OUTOFMEMORY;
602 size = (filename != NULL) ? lstrlenW(filename) : 0;
603 size += lstrlenW(dir) + 2;
604 path = msi_alloc(size * sizeof(WCHAR));
605 if (!path)
607 r = ERROR_OUTOFMEMORY;
608 goto done;
611 if (filename)
613 lstrcpyW(path, dir);
614 PathAddBackslashW(path);
615 lstrcatW(path, filename);
617 TRACE("Deleting misc file: %s\n", debugstr_w(path));
618 DeleteFileW(path);
620 else
622 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
623 RemoveDirectoryW(dir);
626 done:
627 msi_free(path);
628 msi_free(dir);
629 return ERROR_SUCCESS;
632 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
634 MSIQUERY *view;
635 MSIFILE *file;
636 UINT r;
638 static const WCHAR query[] = {
639 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
640 '`','R','e','m','o','v','e','F','i','l','e','`',0};
641 static const WCHAR folder_query[] = {
642 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
643 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0};
645 r = MSI_DatabaseOpenViewW(package->db, query, &view);
646 if (r == ERROR_SUCCESS)
648 MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
649 msiobj_release(&view->hdr);
652 r = MSI_DatabaseOpenViewW(package->db, folder_query, &view);
653 if (r == ERROR_SUCCESS)
654 msiobj_release(&view->hdr);
656 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
658 MSIRECORD *uirow;
659 LPWSTR dir, uipath, p;
661 if ( file->state == msifs_installed )
662 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
664 if ( file->Component->ActionRequest != INSTALLSTATE_ABSENT ||
665 file->Component->Installed == INSTALLSTATE_SOURCE )
666 continue;
668 /* don't remove a file if the old file
669 * is strictly newer than the version to be installed
671 if ( msi_compare_file_version( file ) < 0 )
672 continue;
674 TRACE("removing %s\n", debugstr_w(file->File) );
675 if (!DeleteFileW( file->TargetPath ))
677 WARN("failed to delete %s\n", debugstr_w(file->TargetPath));
679 /* FIXME: check persistence for each directory */
680 else if (r && (dir = strdupW( file->TargetPath )))
682 if ((p = strrchrW( dir, '\\' ))) *p = 0;
683 RemoveDirectoryW( dir );
684 msi_free( dir );
686 file->state = msifs_missing;
688 /* the UI chunk */
689 uirow = MSI_CreateRecord( 9 );
690 MSI_RecordSetStringW( uirow, 1, file->FileName );
691 uipath = strdupW( file->TargetPath );
692 p = strrchrW(uipath,'\\');
693 if (p)
694 p[1]=0;
695 MSI_RecordSetStringW( uirow, 9, uipath);
696 ui_actiondata( package, szRemoveFiles, uirow);
697 msiobj_release( &uirow->hdr );
698 msi_free( uipath );
699 /* FIXME: call ui_progress here? */
702 return ERROR_SUCCESS;