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(MSIPACKAGE
*package
, 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");
156 return ERROR_SUCCESS
;
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
);
165 if (gle
== ERROR_SHARING_VIOLATION
)
167 static const WCHAR msiW
[] = {'m','s','i',0};
168 static const WCHAR slashW
[] = {'\\',0};
169 WCHAR tmpfileW
[MAX_PATH
], *pathW
, *p
;
172 TRACE("file in use, scheduling rename operation\n");
174 GetTempFileNameW(slashW
, msiW
, 0, tmpfileW
);
175 len
= strlenW(file
->TargetPath
) + strlenW(tmpfileW
) + 1;
176 if (!(pathW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
))))
177 return ERROR_OUTOFMEMORY
;
179 strcpyW(pathW
, file
->TargetPath
);
180 if ((p
= strrchrW(pathW
, '\\'))) *p
= 0;
181 strcatW(pathW
, tmpfileW
);
183 if (CopyFileW(source
, pathW
, FALSE
) &&
184 MoveFileExW(file
->TargetPath
, NULL
, MOVEFILE_DELAY_UNTIL_REBOOT
) &&
185 MoveFileExW(pathW
, file
->TargetPath
, MOVEFILE_DELAY_UNTIL_REBOOT
))
187 file
->state
= msifs_installed
;
188 package
->need_reboot
= 1;
193 gle
= GetLastError();
194 WARN("failed to schedule rename operation: %d)\n", gle
);
196 HeapFree(GetProcessHeap(), 0, pathW
);
202 static BOOL
check_dest_hash_matches(MSIFILE
*file
)
204 MSIFILEHASHINFO hash
;
207 if (!file
->hash
.dwFileHashInfoSize
)
210 hash
.dwFileHashInfoSize
= sizeof(MSIFILEHASHINFO
);
211 r
= MsiGetFileHashW(file
->TargetPath
, 0, &hash
);
212 if (r
!= ERROR_SUCCESS
)
215 return !memcmp(&hash
, &file
->hash
, sizeof(MSIFILEHASHINFO
));
218 static BOOL
installfiles_cb(MSIPACKAGE
*package
, LPCWSTR file
, DWORD action
,
219 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
221 static MSIFILE
*f
= NULL
;
223 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
225 f
= get_loaded_file(package
, file
);
228 WARN("unknown file in cabinet (%s)\n", debugstr_w(file
));
232 if (f
->state
!= msifs_missing
&& f
->state
!= msifs_overwrite
)
234 TRACE("Skipping extraction of %s\n", debugstr_w(file
));
238 msi_file_update_ui(package
, f
, szInstallFiles
);
240 *path
= strdupW(f
->TargetPath
);
241 *attrs
= f
->Attributes
;
243 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
245 f
->state
= msifs_installed
;
253 * ACTION_InstallFiles()
255 * For efficiency, this is done in two passes:
256 * 1) Correct all the TargetPaths and determine what files are to be installed.
257 * 2) Extract Cabinets and copy files.
259 UINT
ACTION_InstallFiles(MSIPACKAGE
*package
)
262 UINT rc
= ERROR_SUCCESS
;
265 /* increment progress bar each time action data is sent */
266 ui_progress(package
,1,1,0,0);
268 schedule_install_files(package
);
271 * Despite MSDN specifying that the CreateFolders action
272 * should be called before InstallFiles, some installers don't
273 * do that, and they seem to work correctly. We need to create
274 * directories here to make sure that the files can be copied.
276 msi_create_component_directories( package
);
278 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
280 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
282 if (file
->state
!= msifs_missing
&& !mi
->is_continuous
&& file
->state
!= msifs_overwrite
)
285 if (check_dest_hash_matches(file
))
287 TRACE("File hashes match, not overwriting\n");
291 if (MsiGetFileVersionW(file
->TargetPath
, NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
&&
292 msi_compare_file_version(file
) >= 0)
294 TRACE("Destination file version greater, not overwriting\n");
298 if (file
->Sequence
> mi
->last_sequence
|| mi
->is_continuous
||
299 (file
->IsCompressed
&& !mi
->is_extracted
))
303 rc
= ready_media(package
, file
, mi
);
304 if (rc
!= ERROR_SUCCESS
)
306 ERR("Failed to ready media\n");
311 data
.package
= package
;
312 data
.cb
= installfiles_cb
;
315 if (file
->IsCompressed
&&
316 !msi_cabextract(package
, mi
, &data
))
318 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
319 rc
= ERROR_FUNCTION_FAILED
;
324 if (!file
->IsCompressed
)
326 LPWSTR source
= resolve_file_source(package
, file
);
328 TRACE("file paths %s to %s\n", debugstr_w(source
),
329 debugstr_w(file
->TargetPath
));
331 msi_file_update_ui(package
, file
, szInstallFiles
);
332 rc
= copy_install_file(package
, file
, source
);
333 if (rc
!= ERROR_SUCCESS
)
335 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source
),
336 debugstr_w(file
->TargetPath
), rc
);
337 rc
= ERROR_INSTALL_FAILURE
;
344 else if (file
->state
!= msifs_installed
)
346 ERR("compressed file wasn't extracted (%s)\n",
347 debugstr_w(file
->TargetPath
));
348 rc
= ERROR_INSTALL_FAILURE
;
353 msi_free_media_info(mi
);
357 static UINT
ITERATE_DuplicateFiles(MSIRECORD
*row
, LPVOID param
)
359 MSIPACKAGE
*package
= param
;
360 WCHAR dest_name
[0x100];
361 LPWSTR dest_path
, dest
;
362 LPCWSTR file_key
, component
;
368 component
= MSI_RecordGetString(row
,2);
369 comp
= get_loaded_component(package
,component
);
371 if (!ACTION_VerifyComponentForAction( comp
, INSTALLSTATE_LOCAL
))
373 TRACE("Skipping copy due to disabled component %s\n",
374 debugstr_w(component
));
376 /* the action taken was the same as the current install state */
377 comp
->Action
= comp
->Installed
;
379 return ERROR_SUCCESS
;
382 comp
->Action
= INSTALLSTATE_LOCAL
;
384 file_key
= MSI_RecordGetString(row
,3);
387 ERR("Unable to get file key\n");
388 return ERROR_FUNCTION_FAILED
;
391 rc
= get_file_target(package
,file_key
,&file
);
393 if (rc
!= ERROR_SUCCESS
)
395 ERR("Original file unknown %s\n",debugstr_w(file_key
));
396 return ERROR_SUCCESS
;
399 if (MSI_RecordIsNull(row
,4))
400 strcpyW(dest_name
,strrchrW(file
->TargetPath
,'\\')+1);
404 MSI_RecordGetStringW(row
,4,dest_name
,&sz
);
405 reduce_to_longfilename(dest_name
);
408 if (MSI_RecordIsNull(row
,5))
411 dest_path
= strdupW(file
->TargetPath
);
412 p
= strrchrW(dest_path
,'\\');
419 destkey
= MSI_RecordGetString(row
,5);
420 dest_path
= resolve_folder(package
, destkey
, FALSE
, FALSE
, TRUE
, NULL
);
424 dest_path
= msi_dup_property( package
, destkey
);
427 FIXME("Unable to get destination folder, try AppSearch properties\n");
428 return ERROR_SUCCESS
;
433 dest
= build_directory_name(2, dest_path
, dest_name
);
434 create_full_pathW(dest_path
);
436 TRACE("Duplicating file %s to %s\n",debugstr_w(file
->TargetPath
),
439 if (strcmpW(file
->TargetPath
,dest
))
440 rc
= !CopyFileW(file
->TargetPath
,dest
,TRUE
);
444 if (rc
!= ERROR_SUCCESS
)
445 ERR("Failed to copy file %s -> %s, last error %d\n",
446 debugstr_w(file
->TargetPath
), debugstr_w(dest_path
), GetLastError());
448 FIXME("We should track these duplicate files as well\n");
453 msi_file_update_ui(package
, file
, szDuplicateFiles
);
455 return ERROR_SUCCESS
;
458 UINT
ACTION_DuplicateFiles(MSIPACKAGE
*package
)
462 static const WCHAR ExecSeqQuery
[] =
463 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
464 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
466 rc
= MSI_DatabaseOpenViewW(package
->db
, ExecSeqQuery
, &view
);
467 if (rc
!= ERROR_SUCCESS
)
468 return ERROR_SUCCESS
;
470 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_DuplicateFiles
, package
);
471 msiobj_release(&view
->hdr
);
476 static BOOL
verify_comp_for_removal(MSICOMPONENT
*comp
, UINT install_mode
)
478 INSTALLSTATE request
= comp
->ActionRequest
;
480 if (request
== INSTALLSTATE_UNKNOWN
)
483 if (install_mode
== msidbRemoveFileInstallModeOnInstall
&&
484 (request
== INSTALLSTATE_LOCAL
|| request
== INSTALLSTATE_SOURCE
))
487 if (request
== INSTALLSTATE_ABSENT
)
489 if (!comp
->ComponentId
)
492 if (install_mode
== msidbRemoveFileInstallModeOnRemove
)
496 if (install_mode
== msidbRemoveFileInstallModeOnBoth
)
502 static UINT
ITERATE_RemoveFiles(MSIRECORD
*row
, LPVOID param
)
504 MSIPACKAGE
*package
= param
;
506 LPCWSTR component
, filename
, dirprop
;
508 LPWSTR dir
= NULL
, path
= NULL
;
512 component
= MSI_RecordGetString(row
, 2);
513 filename
= MSI_RecordGetString(row
, 3);
514 dirprop
= MSI_RecordGetString(row
, 4);
515 install_mode
= MSI_RecordGetInteger(row
, 5);
517 comp
= get_loaded_component(package
, component
);
520 ERR("Invalid component: %s\n", debugstr_w(component
));
521 return ERROR_FUNCTION_FAILED
;
524 if (!verify_comp_for_removal(comp
, install_mode
))
526 TRACE("Skipping removal due to missing conditions\n");
527 comp
->Action
= comp
->Installed
;
528 return ERROR_SUCCESS
;
531 dir
= msi_dup_property(package
, dirprop
);
533 return ERROR_OUTOFMEMORY
;
535 size
= (filename
!= NULL
) ? lstrlenW(filename
) : 0;
536 size
+= lstrlenW(dir
) + 2;
537 path
= msi_alloc(size
* sizeof(WCHAR
));
540 r
= ERROR_OUTOFMEMORY
;
545 PathAddBackslashW(path
);
549 lstrcatW(path
, filename
);
551 TRACE("Deleting misc file: %s\n", debugstr_w(path
));
556 TRACE("Removing misc directory: %s\n", debugstr_w(path
));
557 RemoveDirectoryW(path
);
563 return ERROR_SUCCESS
;
566 UINT
ACTION_RemoveFiles( MSIPACKAGE
*package
)
572 static const WCHAR query
[] = {
573 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
574 '`','R','e','m','o','v','e','F','i','l','e','`',0};
576 r
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
577 if (r
== ERROR_SUCCESS
)
579 MSI_IterateRecords(view
, NULL
, ITERATE_RemoveFiles
, package
);
580 msiobj_release(&view
->hdr
);
583 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
588 if ( file
->state
== msifs_installed
)
589 ERR("removing installed file %s\n", debugstr_w(file
->TargetPath
));
591 if ( file
->Component
->ActionRequest
!= INSTALLSTATE_ABSENT
||
592 file
->Component
->Installed
== INSTALLSTATE_SOURCE
)
595 /* don't remove a file if the old file
596 * is strictly newer than the version to be installed
598 if ( msi_compare_file_version( file
) < 0 )
601 TRACE("removing %s\n", debugstr_w(file
->File
) );
602 if ( !DeleteFileW( file
->TargetPath
) )
603 TRACE("failed to delete %s\n", debugstr_w(file
->TargetPath
));
604 file
->state
= msifs_missing
;
607 uirow
= MSI_CreateRecord( 9 );
608 MSI_RecordSetStringW( uirow
, 1, file
->FileName
);
609 uipath
= strdupW( file
->TargetPath
);
610 p
= strrchrW(uipath
,'\\');
613 MSI_RecordSetStringW( uirow
, 9, uipath
);
614 ui_actiondata( package
, szRemoveFiles
, uirow
);
615 msiobj_release( &uirow
->hdr
);
617 /* FIXME: call ui_progress here? */
620 return ERROR_SUCCESS
;