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(TODO)
38 #include "wine/debug.h"
46 #include "wine/unicode.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
50 extern const WCHAR szInstallFiles
[];
51 extern const WCHAR szDuplicateFiles
[];
52 extern const WCHAR szMoveFiles
[];
53 extern const WCHAR szPatchFiles
[];
54 extern const WCHAR szRemoveDuplicateFiles
[];
55 extern const WCHAR szRemoveFiles
[];
57 static void msi_file_update_ui( MSIPACKAGE
*package
, MSIFILE
*f
, const WCHAR
*action
)
63 uirow
= MSI_CreateRecord( 9 );
64 MSI_RecordSetStringW( uirow
, 1, f
->FileName
);
65 uipath
= strdupW( f
->TargetPath
);
66 p
= strrchrW(uipath
,'\\');
69 MSI_RecordSetStringW( uirow
, 9, uipath
);
70 MSI_RecordSetInteger( uirow
, 6, f
->FileSize
);
71 ui_actiondata( package
, action
, uirow
);
72 msiobj_release( &uirow
->hdr
);
74 ui_progress( package
, 2, f
->FileSize
, 0, 0);
77 /* compares the version of a file read from the filesystem and
78 * the version specified in the File table
80 static int msi_compare_file_version(MSIFILE
*file
)
82 WCHAR version
[MAX_PATH
];
88 r
= MsiGetFileVersionW(file
->TargetPath
, version
, &size
, NULL
, NULL
);
89 if (r
!= ERROR_SUCCESS
)
92 return lstrcmpW(version
, file
->Version
);
95 static UINT
get_file_target(MSIPACKAGE
*package
, LPCWSTR file_key
,
98 LIST_FOR_EACH_ENTRY( *file
, &package
->files
, MSIFILE
, entry
)
100 if (lstrcmpW( file_key
, (*file
)->File
)==0)
102 if ((*file
)->state
>= msifs_overwrite
)
103 return ERROR_SUCCESS
;
105 return ERROR_FILE_NOT_FOUND
;
109 return ERROR_FUNCTION_FAILED
;
112 static void schedule_install_files(MSIPACKAGE
*package
)
116 LIST_FOR_EACH_ENTRY(file
, &package
->files
, MSIFILE
, entry
)
118 if (!ACTION_VerifyComponentForAction(file
->Component
, INSTALLSTATE_LOCAL
))
120 TRACE("File %s is not scheduled for install\n", debugstr_w(file
->File
));
122 ui_progress(package
,2,file
->FileSize
,0,0);
123 file
->state
= msifs_skipped
;
128 static UINT
copy_file(MSIFILE
*file
, LPWSTR source
)
132 ret
= CopyFileW(source
, file
->TargetPath
, FALSE
);
134 return GetLastError();
136 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
138 file
->state
= msifs_installed
;
139 return ERROR_SUCCESS
;
142 static UINT
copy_install_file(MSIFILE
*file
, LPWSTR source
)
146 TRACE("Copying %s to %s\n", debugstr_w(source
),
147 debugstr_w(file
->TargetPath
));
149 gle
= copy_file(file
, source
);
150 if (gle
== ERROR_SUCCESS
)
153 if (gle
== ERROR_ALREADY_EXISTS
&& file
->state
== msifs_overwrite
)
155 TRACE("overwriting existing file\n");
158 else if (gle
== ERROR_ACCESS_DENIED
)
160 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
162 gle
= copy_file(file
, source
);
163 TRACE("Overwriting existing file: %d\n", gle
);
169 static BOOL
check_dest_hash_matches(MSIFILE
*file
)
171 MSIFILEHASHINFO hash
;
174 if (!file
->hash
.dwFileHashInfoSize
)
177 hash
.dwFileHashInfoSize
= sizeof(MSIFILEHASHINFO
);
178 r
= MsiGetFileHashW(file
->TargetPath
, 0, &hash
);
179 if (r
!= ERROR_SUCCESS
)
182 return !memcmp(&hash
, &file
->hash
, sizeof(MSIFILEHASHINFO
));
185 static BOOL
installfiles_cb(MSIPACKAGE
*package
, LPCWSTR file
, DWORD action
,
186 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
188 static MSIFILE
*f
= NULL
;
190 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
192 f
= get_loaded_file(package
, file
);
195 WARN("unknown file in cabinet (%s)\n", debugstr_w(file
));
199 if (f
->state
!= msifs_missing
&& f
->state
!= msifs_overwrite
)
201 TRACE("Skipping extraction of %s\n", debugstr_w(file
));
205 msi_file_update_ui(package
, f
, szInstallFiles
);
207 *path
= strdupW(f
->TargetPath
);
208 *attrs
= f
->Attributes
;
210 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
212 f
->state
= msifs_installed
;
220 * ACTION_InstallFiles()
222 * For efficiency, this is done in two passes:
223 * 1) Correct all the TargetPaths and determine what files are to be installed.
224 * 2) Extract Cabinets and copy files.
226 UINT
ACTION_InstallFiles(MSIPACKAGE
*package
)
229 UINT rc
= ERROR_SUCCESS
;
232 /* increment progress bar each time action data is sent */
233 ui_progress(package
,1,1,0,0);
235 schedule_install_files(package
);
238 * Despite MSDN specifying that the CreateFolders action
239 * should be called before InstallFiles, some installers don't
240 * do that, and they seem to work correctly. We need to create
241 * directories here to make sure that the files can be copied.
243 msi_create_component_directories( package
);
245 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
247 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
249 if (file
->state
!= msifs_missing
&& !mi
->is_continuous
&& file
->state
!= msifs_overwrite
)
252 if (check_dest_hash_matches(file
))
254 TRACE("File hashes match, not overwriting\n");
258 if (MsiGetFileVersionW(file
->TargetPath
, NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
&&
259 msi_compare_file_version(file
) >= 0)
261 TRACE("Destination file version greater, not overwriting\n");
265 if (file
->Sequence
> mi
->last_sequence
|| mi
->is_continuous
||
266 (file
->IsCompressed
&& !mi
->is_extracted
))
270 rc
= ready_media(package
, file
, mi
);
271 if (rc
!= ERROR_SUCCESS
)
273 ERR("Failed to ready media\n");
278 data
.package
= package
;
279 data
.cb
= installfiles_cb
;
282 if (file
->IsCompressed
&&
283 !msi_cabextract(package
, mi
, &data
))
285 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
286 rc
= ERROR_FUNCTION_FAILED
;
291 if (!file
->IsCompressed
)
293 LPWSTR source
= resolve_file_source(package
, file
);
295 TRACE("file paths %s to %s\n", debugstr_w(source
),
296 debugstr_w(file
->TargetPath
));
298 msi_file_update_ui(package
, file
, szInstallFiles
);
299 rc
= copy_install_file(file
, source
);
300 if (rc
!= ERROR_SUCCESS
)
302 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source
),
303 debugstr_w(file
->TargetPath
), rc
);
304 rc
= ERROR_INSTALL_FAILURE
;
311 else if (file
->state
!= msifs_installed
)
313 ERR("compressed file wasn't extracted (%s)\n",
314 debugstr_w(file
->TargetPath
));
315 rc
= ERROR_INSTALL_FAILURE
;
320 msi_free_media_info(mi
);
324 static UINT
ITERATE_DuplicateFiles(MSIRECORD
*row
, LPVOID param
)
326 MSIPACKAGE
*package
= param
;
327 WCHAR dest_name
[0x100];
328 LPWSTR dest_path
, dest
;
329 LPCWSTR file_key
, component
;
335 component
= MSI_RecordGetString(row
,2);
336 comp
= get_loaded_component(package
,component
);
338 if (!ACTION_VerifyComponentForAction( comp
, INSTALLSTATE_LOCAL
))
340 TRACE("Skipping copy due to disabled component %s\n",
341 debugstr_w(component
));
343 /* the action taken was the same as the current install state */
344 comp
->Action
= comp
->Installed
;
346 return ERROR_SUCCESS
;
349 comp
->Action
= INSTALLSTATE_LOCAL
;
351 file_key
= MSI_RecordGetString(row
,3);
354 ERR("Unable to get file key\n");
355 return ERROR_FUNCTION_FAILED
;
358 rc
= get_file_target(package
,file_key
,&file
);
360 if (rc
!= ERROR_SUCCESS
)
362 ERR("Original file unknown %s\n",debugstr_w(file_key
));
363 return ERROR_SUCCESS
;
366 if (MSI_RecordIsNull(row
,4))
367 strcpyW(dest_name
,strrchrW(file
->TargetPath
,'\\')+1);
371 MSI_RecordGetStringW(row
,4,dest_name
,&sz
);
372 reduce_to_longfilename(dest_name
);
375 if (MSI_RecordIsNull(row
,5))
378 dest_path
= strdupW(file
->TargetPath
);
379 p
= strrchrW(dest_path
,'\\');
386 destkey
= MSI_RecordGetString(row
,5);
387 dest_path
= resolve_folder(package
, destkey
, FALSE
, FALSE
, TRUE
, NULL
);
391 dest_path
= msi_dup_property( package
, destkey
);
394 FIXME("Unable to get destination folder, try AppSearch properties\n");
395 return ERROR_SUCCESS
;
400 dest
= build_directory_name(2, dest_path
, dest_name
);
401 create_full_pathW(dest_path
);
403 TRACE("Duplicating file %s to %s\n",debugstr_w(file
->TargetPath
),
406 if (strcmpW(file
->TargetPath
,dest
))
407 rc
= !CopyFileW(file
->TargetPath
,dest
,TRUE
);
411 if (rc
!= ERROR_SUCCESS
)
412 ERR("Failed to copy file %s -> %s, last error %d\n",
413 debugstr_w(file
->TargetPath
), debugstr_w(dest_path
), GetLastError());
415 FIXME("We should track these duplicate files as well\n");
420 msi_file_update_ui(package
, file
, szDuplicateFiles
);
422 return ERROR_SUCCESS
;
425 UINT
ACTION_DuplicateFiles(MSIPACKAGE
*package
)
429 static const WCHAR ExecSeqQuery
[] =
430 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
431 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
433 rc
= MSI_DatabaseOpenViewW(package
->db
, ExecSeqQuery
, &view
);
434 if (rc
!= ERROR_SUCCESS
)
435 return ERROR_SUCCESS
;
437 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_DuplicateFiles
, package
);
438 msiobj_release(&view
->hdr
);
443 static BOOL
verify_comp_for_removal(MSICOMPONENT
*comp
, UINT install_mode
)
445 INSTALLSTATE request
= comp
->ActionRequest
;
447 if (request
== INSTALLSTATE_UNKNOWN
)
450 if (install_mode
== msidbRemoveFileInstallModeOnInstall
&&
451 (request
== INSTALLSTATE_LOCAL
|| request
== INSTALLSTATE_SOURCE
))
454 if (request
== INSTALLSTATE_ABSENT
)
456 if (!comp
->ComponentId
)
459 if (install_mode
== msidbRemoveFileInstallModeOnRemove
)
463 if (install_mode
== msidbRemoveFileInstallModeOnBoth
)
469 static UINT
ITERATE_RemoveFiles(MSIRECORD
*row
, LPVOID param
)
471 MSIPACKAGE
*package
= param
;
473 LPCWSTR component
, filename
, dirprop
;
475 LPWSTR dir
= NULL
, path
= NULL
;
479 component
= MSI_RecordGetString(row
, 2);
480 filename
= MSI_RecordGetString(row
, 3);
481 dirprop
= MSI_RecordGetString(row
, 4);
482 install_mode
= MSI_RecordGetInteger(row
, 5);
484 comp
= get_loaded_component(package
, component
);
487 ERR("Invalid component: %s\n", debugstr_w(component
));
488 return ERROR_FUNCTION_FAILED
;
491 if (!verify_comp_for_removal(comp
, install_mode
))
493 TRACE("Skipping removal due to missing conditions\n");
494 comp
->Action
= comp
->Installed
;
495 return ERROR_SUCCESS
;
498 dir
= msi_dup_property(package
, dirprop
);
500 return ERROR_OUTOFMEMORY
;
502 size
= (filename
!= NULL
) ? lstrlenW(filename
) : 0;
503 size
+= lstrlenW(dir
) + 2;
504 path
= msi_alloc(size
* sizeof(WCHAR
));
507 r
= ERROR_OUTOFMEMORY
;
512 PathAddBackslashW(path
);
516 lstrcatW(path
, filename
);
518 TRACE("Deleting misc file: %s\n", debugstr_w(path
));
523 TRACE("Removing misc directory: %s\n", debugstr_w(path
));
524 RemoveDirectoryW(path
);
530 return ERROR_SUCCESS
;
533 UINT
ACTION_RemoveFiles( MSIPACKAGE
*package
)
539 static const WCHAR query
[] = {
540 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
541 '`','R','e','m','o','v','e','F','i','l','e','`',0};
543 r
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
544 if (r
== ERROR_SUCCESS
)
546 MSI_IterateRecords(view
, NULL
, ITERATE_RemoveFiles
, package
);
547 msiobj_release(&view
->hdr
);
550 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
555 if ( file
->state
== msifs_installed
)
556 ERR("removing installed file %s\n", debugstr_w(file
->TargetPath
));
558 if ( file
->Component
->ActionRequest
!= INSTALLSTATE_ABSENT
||
559 file
->Component
->Installed
== INSTALLSTATE_SOURCE
)
562 /* don't remove a file if the old file
563 * is strictly newer than the version to be installed
565 if ( msi_compare_file_version( file
) < 0 )
568 TRACE("removing %s\n", debugstr_w(file
->File
) );
569 if ( !DeleteFileW( file
->TargetPath
) )
570 TRACE("failed to delete %s\n", debugstr_w(file
->TargetPath
));
571 file
->state
= msifs_missing
;
574 uirow
= MSI_CreateRecord( 9 );
575 MSI_RecordSetStringW( uirow
, 1, file
->FileName
);
576 uipath
= strdupW( file
->TargetPath
);
577 p
= strrchrW(uipath
,'\\');
580 MSI_RecordSetStringW( uirow
, 9, uipath
);
581 ui_actiondata( package
, szRemoveFiles
, uirow
);
582 msiobj_release( &uirow
->hdr
);
584 /* FIXME: call ui_progress here? */
587 return ERROR_SUCCESS
;