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"
42 #include "msvcrt/fcntl.h"
47 #include "wine/unicode.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
51 extern const WCHAR szInstallFiles
[];
52 extern const WCHAR szDuplicateFiles
[];
53 extern const WCHAR szMoveFiles
[];
54 extern const WCHAR szPatchFiles
[];
55 extern const WCHAR szRemoveDuplicateFiles
[];
56 extern const WCHAR szRemoveFiles
[];
58 static void msi_file_update_ui( MSIPACKAGE
*package
, MSIFILE
*f
, const WCHAR
*action
)
64 uirow
= MSI_CreateRecord( 9 );
65 MSI_RecordSetStringW( uirow
, 1, f
->FileName
);
66 uipath
= strdupW( f
->TargetPath
);
67 p
= strrchrW(uipath
,'\\');
70 MSI_RecordSetStringW( uirow
, 9, uipath
);
71 MSI_RecordSetInteger( uirow
, 6, f
->FileSize
);
72 ui_actiondata( package
, action
, uirow
);
73 msiobj_release( &uirow
->hdr
);
75 ui_progress( package
, 2, f
->FileSize
, 0, 0);
78 /* compares the version of a file read from the filesystem and
79 * the version specified in the File table
81 static int msi_compare_file_version(MSIFILE
*file
)
83 WCHAR version
[MAX_PATH
];
89 r
= MsiGetFileVersionW(file
->TargetPath
, version
, &size
, NULL
, NULL
);
90 if (r
!= ERROR_SUCCESS
)
93 return lstrcmpW(version
, file
->Version
);
96 static UINT
get_file_target(MSIPACKAGE
*package
, LPCWSTR file_key
,
99 LIST_FOR_EACH_ENTRY( *file
, &package
->files
, MSIFILE
, entry
)
101 if (lstrcmpW( file_key
, (*file
)->File
)==0)
103 if ((*file
)->state
>= msifs_overwrite
)
104 return ERROR_SUCCESS
;
106 return ERROR_FILE_NOT_FOUND
;
110 return ERROR_FUNCTION_FAILED
;
113 static void schedule_install_files(MSIPACKAGE
*package
)
117 LIST_FOR_EACH_ENTRY(file
, &package
->files
, MSIFILE
, entry
)
119 if (!ACTION_VerifyComponentForAction(file
->Component
, INSTALLSTATE_LOCAL
))
121 TRACE("File %s is not scheduled for install\n", debugstr_w(file
->File
));
123 ui_progress(package
,2,file
->FileSize
,0,0);
124 file
->state
= msifs_skipped
;
129 static UINT
copy_file(MSIFILE
*file
, LPWSTR source
)
133 ret
= CopyFileW(source
, file
->TargetPath
, FALSE
);
135 return GetLastError();
137 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
139 file
->state
= msifs_installed
;
140 return ERROR_SUCCESS
;
143 static UINT
copy_install_file(MSIFILE
*file
, LPWSTR source
)
147 TRACE("Copying %s to %s\n", debugstr_w(source
),
148 debugstr_w(file
->TargetPath
));
150 gle
= copy_file(file
, source
);
151 if (gle
== ERROR_SUCCESS
)
154 if (gle
== ERROR_ALREADY_EXISTS
&& file
->state
== msifs_overwrite
)
156 TRACE("overwriting existing file\n");
159 else if (gle
== ERROR_ACCESS_DENIED
)
161 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
163 gle
= copy_file(file
, source
);
164 TRACE("Overwriting existing file: %d\n", gle
);
170 static BOOL
check_dest_hash_matches(MSIFILE
*file
)
172 MSIFILEHASHINFO hash
;
175 if (!file
->hash
.dwFileHashInfoSize
)
178 hash
.dwFileHashInfoSize
= sizeof(MSIFILEHASHINFO
);
179 r
= MsiGetFileHashW(file
->TargetPath
, 0, &hash
);
180 if (r
!= ERROR_SUCCESS
)
183 return !memcmp(&hash
, &file
->hash
, sizeof(MSIFILEHASHINFO
));
186 static BOOL
installfiles_cb(MSIPACKAGE
*package
, LPCWSTR file
, DWORD action
,
187 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
189 static MSIFILE
*f
= NULL
;
191 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
193 f
= get_loaded_file(package
, file
);
196 WARN("unknown file in cabinet (%s)\n", debugstr_w(file
));
200 if (f
->state
!= msifs_missing
&& f
->state
!= msifs_overwrite
)
202 TRACE("Skipping extraction of %s\n", debugstr_w(file
));
206 msi_file_update_ui(package
, f
, szInstallFiles
);
208 *path
= strdupW(f
->TargetPath
);
209 *attrs
= f
->Attributes
;
211 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
213 f
->state
= msifs_installed
;
221 * ACTION_InstallFiles()
223 * For efficiency, this is done in two passes:
224 * 1) Correct all the TargetPaths and determine what files are to be installed.
225 * 2) Extract Cabinets and copy files.
227 UINT
ACTION_InstallFiles(MSIPACKAGE
*package
)
230 UINT rc
= ERROR_SUCCESS
;
233 /* increment progress bar each time action data is sent */
234 ui_progress(package
,1,1,0,0);
236 schedule_install_files(package
);
239 * Despite MSDN specifying that the CreateFolders action
240 * should be called before InstallFiles, some installers don't
241 * do that, and they seem to work correctly. We need to create
242 * directories here to make sure that the files can be copied.
244 msi_create_component_directories( package
);
246 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
248 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
250 if (file
->state
!= msifs_missing
&& !mi
->is_continuous
&& file
->state
!= msifs_overwrite
)
253 if (check_dest_hash_matches(file
))
255 TRACE("File hashes match, not overwriting\n");
259 if (MsiGetFileVersionW(file
->TargetPath
, NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
&&
260 msi_compare_file_version(file
) >= 0)
262 TRACE("Destination file version greater, not overwriting\n");
266 if (file
->Sequence
> mi
->last_sequence
|| mi
->is_continuous
||
267 (file
->IsCompressed
&& !mi
->is_extracted
))
271 rc
= ready_media(package
, file
, mi
);
272 if (rc
!= ERROR_SUCCESS
)
274 ERR("Failed to ready media\n");
279 data
.package
= package
;
280 data
.cb
= installfiles_cb
;
283 if (file
->IsCompressed
&&
284 !msi_cabextract(package
, mi
, &data
))
286 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
287 rc
= ERROR_FUNCTION_FAILED
;
292 if (!file
->IsCompressed
)
294 LPWSTR source
= resolve_file_source(package
, file
);
296 TRACE("file paths %s to %s\n", debugstr_w(source
),
297 debugstr_w(file
->TargetPath
));
299 msi_file_update_ui(package
, file
, szInstallFiles
);
300 rc
= copy_install_file(file
, source
);
301 if (rc
!= ERROR_SUCCESS
)
303 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source
),
304 debugstr_w(file
->TargetPath
), rc
);
305 rc
= ERROR_INSTALL_FAILURE
;
312 else if (file
->state
!= msifs_installed
)
314 ERR("compressed file wasn't extracted (%s)\n",
315 debugstr_w(file
->TargetPath
));
316 rc
= ERROR_INSTALL_FAILURE
;
321 msi_free_media_info(mi
);
325 static UINT
ITERATE_DuplicateFiles(MSIRECORD
*row
, LPVOID param
)
327 MSIPACKAGE
*package
= (MSIPACKAGE
*)param
;
328 WCHAR dest_name
[0x100];
329 LPWSTR dest_path
, dest
;
330 LPCWSTR file_key
, component
;
336 component
= MSI_RecordGetString(row
,2);
337 comp
= get_loaded_component(package
,component
);
339 if (!ACTION_VerifyComponentForAction( comp
, INSTALLSTATE_LOCAL
))
341 TRACE("Skipping copy due to disabled component %s\n",
342 debugstr_w(component
));
344 /* the action taken was the same as the current install state */
345 comp
->Action
= comp
->Installed
;
347 return ERROR_SUCCESS
;
350 comp
->Action
= INSTALLSTATE_LOCAL
;
352 file_key
= MSI_RecordGetString(row
,3);
355 ERR("Unable to get file key\n");
356 return ERROR_FUNCTION_FAILED
;
359 rc
= get_file_target(package
,file_key
,&file
);
361 if (rc
!= ERROR_SUCCESS
)
363 ERR("Original file unknown %s\n",debugstr_w(file_key
));
364 return ERROR_SUCCESS
;
367 if (MSI_RecordIsNull(row
,4))
368 strcpyW(dest_name
,strrchrW(file
->TargetPath
,'\\')+1);
372 MSI_RecordGetStringW(row
,4,dest_name
,&sz
);
373 reduce_to_longfilename(dest_name
);
376 if (MSI_RecordIsNull(row
,5))
379 dest_path
= strdupW(file
->TargetPath
);
380 p
= strrchrW(dest_path
,'\\');
387 destkey
= MSI_RecordGetString(row
,5);
388 dest_path
= resolve_folder(package
, destkey
, FALSE
, FALSE
, TRUE
, NULL
);
392 dest_path
= msi_dup_property( package
, destkey
);
395 FIXME("Unable to get destination folder, try AppSearch properties\n");
396 return ERROR_SUCCESS
;
401 dest
= build_directory_name(2, dest_path
, dest_name
);
402 create_full_pathW(dest_path
);
404 TRACE("Duplicating file %s to %s\n",debugstr_w(file
->TargetPath
),
407 if (strcmpW(file
->TargetPath
,dest
))
408 rc
= !CopyFileW(file
->TargetPath
,dest
,TRUE
);
412 if (rc
!= ERROR_SUCCESS
)
413 ERR("Failed to copy file %s -> %s, last error %d\n",
414 debugstr_w(file
->TargetPath
), debugstr_w(dest_path
), GetLastError());
416 FIXME("We should track these duplicate files as well\n");
421 msi_file_update_ui(package
, file
, szDuplicateFiles
);
423 return ERROR_SUCCESS
;
426 UINT
ACTION_DuplicateFiles(MSIPACKAGE
*package
)
430 static const WCHAR ExecSeqQuery
[] =
431 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
432 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
434 rc
= MSI_DatabaseOpenViewW(package
->db
, ExecSeqQuery
, &view
);
435 if (rc
!= ERROR_SUCCESS
)
436 return ERROR_SUCCESS
;
438 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_DuplicateFiles
, package
);
439 msiobj_release(&view
->hdr
);
444 static BOOL
verify_comp_for_removal(MSICOMPONENT
*comp
, UINT install_mode
)
446 INSTALLSTATE request
= comp
->ActionRequest
;
448 if (request
== INSTALLSTATE_UNKNOWN
)
451 if (install_mode
== msidbRemoveFileInstallModeOnInstall
&&
452 (request
== INSTALLSTATE_LOCAL
|| request
== INSTALLSTATE_SOURCE
))
455 if (request
== INSTALLSTATE_ABSENT
)
457 if (!comp
->ComponentId
)
460 if (install_mode
== msidbRemoveFileInstallModeOnRemove
)
464 if (install_mode
== msidbRemoveFileInstallModeOnBoth
)
470 static UINT
ITERATE_RemoveFiles(MSIRECORD
*row
, LPVOID param
)
472 MSIPACKAGE
*package
= (MSIPACKAGE
*)param
;
474 LPCWSTR component
, filename
, dirprop
;
476 LPWSTR dir
= NULL
, path
= NULL
;
480 component
= MSI_RecordGetString(row
, 2);
481 filename
= MSI_RecordGetString(row
, 3);
482 dirprop
= MSI_RecordGetString(row
, 4);
483 install_mode
= MSI_RecordGetInteger(row
, 5);
485 comp
= get_loaded_component(package
, component
);
488 ERR("Invalid component: %s\n", debugstr_w(component
));
489 return ERROR_FUNCTION_FAILED
;
492 if (!verify_comp_for_removal(comp
, install_mode
))
494 TRACE("Skipping removal due to missing conditions\n");
495 comp
->Action
= comp
->Installed
;
496 return ERROR_SUCCESS
;
499 dir
= msi_dup_property(package
, dirprop
);
501 return ERROR_OUTOFMEMORY
;
503 size
= (filename
!= NULL
) ? lstrlenW(filename
) : 0;
504 size
+= lstrlenW(dir
) + 2;
505 path
= msi_alloc(size
* sizeof(WCHAR
));
508 r
= ERROR_OUTOFMEMORY
;
513 PathAddBackslashW(path
);
517 lstrcatW(path
, filename
);
519 TRACE("Deleting misc file: %s\n", debugstr_w(path
));
524 TRACE("Removing misc directory: %s\n", debugstr_w(path
));
525 RemoveDirectoryW(path
);
531 return ERROR_SUCCESS
;
534 UINT
ACTION_RemoveFiles( MSIPACKAGE
*package
)
540 static const WCHAR query
[] = {
541 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
542 '`','R','e','m','o','v','e','F','i','l','e','`',0};
544 r
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
545 if (r
== ERROR_SUCCESS
)
547 MSI_IterateRecords(view
, NULL
, ITERATE_RemoveFiles
, package
);
548 msiobj_release(&view
->hdr
);
551 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
556 if ( file
->state
== msifs_installed
)
557 ERR("removing installed file %s\n", debugstr_w(file
->TargetPath
));
559 if ( file
->Component
->ActionRequest
!= INSTALLSTATE_ABSENT
||
560 file
->Component
->Installed
== INSTALLSTATE_SOURCE
)
563 /* don't remove a file if the old file
564 * is strictly newer than the version to be installed
566 if ( msi_compare_file_version( file
) < 0 )
569 TRACE("removing %s\n", debugstr_w(file
->File
) );
570 if ( !DeleteFileW( file
->TargetPath
) )
571 TRACE("failed to delete %s\n", debugstr_w(file
->TargetPath
));
572 file
->state
= msifs_missing
;
575 uirow
= MSI_CreateRecord( 9 );
576 MSI_RecordSetStringW( uirow
, 1, file
->FileName
);
577 uipath
= strdupW( file
->TargetPath
);
578 p
= strrchrW(uipath
,'\\');
581 MSI_RecordSetStringW( uirow
, 9, uipath
);
582 ui_actiondata( package
, szRemoveFiles
, uirow
);
583 msiobj_release( &uirow
->hdr
);
585 /* FIXME: call ui_progress here? */
588 return ERROR_SUCCESS
;