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
29 * RemoveDuplicateFiles
38 #include "wine/debug.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
)
56 uirow
= MSI_CreateRecord( 9 );
57 MSI_RecordSetStringW( uirow
, 1, f
->FileName
);
58 uipath
= strdupW( f
->TargetPath
);
59 p
= strrchrW(uipath
,'\\');
62 MSI_RecordSetStringW( uirow
, 9, uipath
);
63 MSI_RecordSetInteger( uirow
, 6, f
->FileSize
);
64 ui_actiondata( package
, action
, uirow
);
65 msiobj_release( &uirow
->hdr
);
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
];
81 r
= MsiGetFileVersionW(file
->TargetPath
, version
, &size
, NULL
, NULL
);
82 if (r
!= ERROR_SUCCESS
)
85 return lstrcmpW(version
, file
->Version
);
88 static void schedule_install_files(MSIPACKAGE
*package
)
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
)
108 ret
= CopyFileW(source
, file
->TargetPath
, FALSE
);
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
)
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
)
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
;
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;
167 gle
= GetLastError();
168 WARN("failed to schedule rename operation: %d)\n", gle
);
170 HeapFree(GetProcessHeap(), 0, pathW
);
176 static BOOL
check_dest_hash_matches(MSIFILE
*file
)
178 MSIFILEHASHINFO hash
;
181 if (!file
->hash
.dwFileHashInfoSize
)
184 hash
.dwFileHashInfoSize
= sizeof(MSIFILEHASHINFO
);
185 r
= MsiGetFileHashW(file
->TargetPath
, 0, &hash
);
186 if (r
!= ERROR_SUCCESS
)
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
);
202 WARN("unknown file in cabinet (%s)\n", debugstr_w(file
));
206 if (f
->state
!= msifs_missing
&& f
->state
!= msifs_overwrite
)
208 TRACE("Skipping extraction of %s\n", debugstr_w(file
));
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
;
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
)
236 UINT rc
= ERROR_SUCCESS
;
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
)
259 if (check_dest_hash_matches(file
))
261 TRACE("File hashes match, not overwriting\n");
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");
272 if (file
->Sequence
> mi
->last_sequence
|| mi
->is_continuous
||
273 (file
->IsCompressed
&& !mi
->is_extracted
))
277 rc
= ready_media(package
, file
, mi
);
278 if (rc
!= ERROR_SUCCESS
)
280 ERR("Failed to ready media\n");
285 data
.package
= package
;
286 data
.cb
= installfiles_cb
;
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
;
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
;
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
;
327 msi_free_media_info(mi
);
331 static WCHAR
*get_duplicate_filename( MSIPACKAGE
*package
, MSIRECORD
*row
, const WCHAR
*file_key
, const WCHAR
*src
)
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 );
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 ))
353 dst_path
= strdupW( src
);
354 p
= strrchrW( dst_path
, '\\' );
359 const WCHAR
*dst_key
= MSI_RecordGetString( row
, 5 );
361 dst_path
= resolve_folder( package
, dst_key
, FALSE
, FALSE
, TRUE
, NULL
);
365 dst_path
= msi_dup_property( package
, dst_key
);
368 FIXME("Unable to get destination folder, try AppSearch properties\n");
369 msi_free( dst_name
);
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
);
383 static UINT
ITERATE_DuplicateFiles(MSIRECORD
*row
, LPVOID param
)
385 MSIPACKAGE
*package
= param
;
387 LPCWSTR file_key
, component
;
392 component
= MSI_RecordGetString(row
,2);
393 comp
= get_loaded_component(package
,component
);
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);
408 ERR("Unable to get file key\n");
409 return ERROR_FUNCTION_FAILED
;
412 file
= get_loaded_file( package
, file_key
);
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
);
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
);
444 return ERROR_SUCCESS
;
447 UINT
ACTION_DuplicateFiles(MSIPACKAGE
*package
)
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
);
465 static UINT
ITERATE_RemoveDuplicateFiles( MSIRECORD
*row
, LPVOID param
)
467 MSIPACKAGE
*package
= param
;
469 LPCWSTR file_key
, component
;
474 component
= MSI_RecordGetString( row
, 2 );
475 comp
= get_loaded_component( package
, component
);
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 );
490 ERR("Unable to get file key\n");
491 return ERROR_FUNCTION_FAILED
;
494 file
= get_loaded_file( package
, file_key
);
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
);
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
);
522 return ERROR_SUCCESS
;
525 UINT
ACTION_RemoveDuplicateFiles( MSIPACKAGE
*package
)
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
);
543 static BOOL
verify_comp_for_removal(MSICOMPONENT
*comp
, UINT install_mode
)
545 INSTALLSTATE request
= comp
->ActionRequest
;
547 if (request
== INSTALLSTATE_UNKNOWN
)
550 if (install_mode
== msidbRemoveFileInstallModeOnInstall
&&
551 (request
== INSTALLSTATE_LOCAL
|| request
== INSTALLSTATE_SOURCE
))
554 if (request
== INSTALLSTATE_ABSENT
)
556 if (!comp
->ComponentId
)
559 if (install_mode
== msidbRemoveFileInstallModeOnRemove
)
563 if (install_mode
== msidbRemoveFileInstallModeOnBoth
)
569 static UINT
ITERATE_RemoveFiles(MSIRECORD
*row
, LPVOID param
)
571 MSIPACKAGE
*package
= param
;
573 LPCWSTR component
, filename
, dirprop
;
575 LPWSTR dir
= NULL
, path
= NULL
;
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
);
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
);
600 return ERROR_OUTOFMEMORY
;
602 size
= (filename
!= NULL
) ? lstrlenW(filename
) : 0;
603 size
+= lstrlenW(dir
) + 2;
604 path
= msi_alloc(size
* sizeof(WCHAR
));
607 r
= ERROR_OUTOFMEMORY
;
614 PathAddBackslashW(path
);
615 lstrcatW(path
, filename
);
617 TRACE("Deleting misc file: %s\n", debugstr_w(path
));
622 TRACE("Removing misc directory: %s\n", debugstr_w(dir
));
623 RemoveDirectoryW(dir
);
629 return ERROR_SUCCESS
;
632 UINT
ACTION_RemoveFiles( MSIPACKAGE
*package
)
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
)
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
)
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 )
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
);
686 file
->state
= msifs_missing
;
689 uirow
= MSI_CreateRecord( 9 );
690 MSI_RecordSetStringW( uirow
, 1, file
->FileName
);
691 uipath
= strdupW( file
->TargetPath
);
692 p
= strrchrW(uipath
,'\\');
695 MSI_RecordSetStringW( uirow
, 9, uipath
);
696 ui_actiondata( package
, szRemoveFiles
, uirow
);
697 msiobj_release( &uirow
->hdr
);
699 /* FIXME: call ui_progress here? */
702 return ERROR_SUCCESS
;