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 const WCHAR cszTempFolder
[]= {'T','e','m','p','F','o','l','d','e','r',0};
70 WCHAR source
[MAX_PATH
];
73 static BOOL
source_matches_volume(struct media_info
*mi
, LPWSTR source_root
)
75 WCHAR volume_name
[MAX_PATH
+ 1];
77 if (!GetVolumeInformationW(source_root
, volume_name
, MAX_PATH
+ 1,
78 NULL
, NULL
, NULL
, NULL
, 0))
80 ERR("Failed to get volume information\n");
84 return !lstrcmpW(mi
->volume_label
, volume_name
);
87 static UINT
msi_change_media( MSIPACKAGE
*package
, struct media_info
*mi
)
90 LPWSTR error
, error_dialog
;
92 UINT r
= ERROR_SUCCESS
;
94 static const WCHAR szUILevel
[] = {'U','I','L','e','v','e','l',0};
95 static const WCHAR error_prop
[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
97 if ( (msi_get_property_int(package
, szUILevel
, 0) & INSTALLUILEVEL_MASK
) == INSTALLUILEVEL_NONE
&& !gUIHandlerA
)
100 error
= generate_error_string( package
, 1302, 1, mi
->disk_prompt
);
101 error_dialog
= msi_dup_property( package
, error_prop
);
102 source_dir
= msi_dup_property( package
, cszSourceDir
);
103 PathStripToRootW(source_dir
);
105 while ( r
== ERROR_SUCCESS
&&
106 !source_matches_volume(mi
, source_dir
) )
108 r
= msi_spawn_error_dialog( package
, error_dialog
, error
);
112 msg
= strdupWtoA( error
);
113 gUIHandlerA( gUIContext
, MB_RETRYCANCEL
| INSTALLMESSAGE_ERROR
, msg
);
119 msi_free( error_dialog
);
120 msi_free( source_dir
);
126 * This is a helper function for handling embedded cabinet media
128 static UINT
writeout_cabinet_stream(MSIPACKAGE
*package
, LPCWSTR stream_name
,
138 rc
= read_raw_stream_data(package
->db
,stream_name
,&data
,&size
);
139 if (rc
!= ERROR_SUCCESS
)
143 if (MSI_GetPropertyW(package
, cszTempFolder
, tmp
, &write
))
144 GetTempPathW(MAX_PATH
,tmp
);
146 GetTempFileNameW(tmp
,stream_name
,0,source
);
148 track_tempfile(package
, source
);
149 the_file
= CreateFileW(source
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
150 FILE_ATTRIBUTE_NORMAL
, NULL
);
152 if (the_file
== INVALID_HANDLE_VALUE
)
154 ERR("Unable to create file %s\n",debugstr_w(source
));
155 rc
= ERROR_FUNCTION_FAILED
;
159 WriteFile(the_file
,data
,size
,&write
,NULL
);
160 CloseHandle(the_file
);
161 TRACE("wrote %i bytes to %s\n",write
,debugstr_w(source
));
168 /* Support functions for FDI functions */
172 struct media_info
*mi
;
175 static void * cabinet_alloc(ULONG cb
)
177 return msi_alloc(cb
);
180 static void cabinet_free(void *pv
)
185 static INT_PTR
cabinet_open(char *pszFile
, int oflag
, int pmode
)
189 DWORD dwShareMode
= 0;
190 DWORD dwCreateDisposition
= OPEN_EXISTING
;
191 switch (oflag
& _O_ACCMODE
)
194 dwAccess
= GENERIC_READ
;
195 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_DELETE
;
198 dwAccess
= GENERIC_WRITE
;
199 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
202 dwAccess
= GENERIC_READ
| GENERIC_WRITE
;
203 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
206 if ((oflag
& (_O_CREAT
| _O_EXCL
)) == (_O_CREAT
| _O_EXCL
))
207 dwCreateDisposition
= CREATE_NEW
;
208 else if (oflag
& _O_CREAT
)
209 dwCreateDisposition
= CREATE_ALWAYS
;
210 handle
= CreateFileA( pszFile
, dwAccess
, dwShareMode
, NULL
,
211 dwCreateDisposition
, 0, NULL
);
212 if (handle
== INVALID_HANDLE_VALUE
)
214 return (INT_PTR
) handle
;
217 static UINT
cabinet_read(INT_PTR hf
, void *pv
, UINT cb
)
219 HANDLE handle
= (HANDLE
) hf
;
221 if (ReadFile(handle
, pv
, cb
, &dwRead
, NULL
))
226 static UINT
cabinet_write(INT_PTR hf
, void *pv
, UINT cb
)
228 HANDLE handle
= (HANDLE
) hf
;
230 if (WriteFile(handle
, pv
, cb
, &dwWritten
, NULL
))
235 static int cabinet_close(INT_PTR hf
)
237 HANDLE handle
= (HANDLE
) hf
;
238 return CloseHandle(handle
) ? 0 : -1;
241 static long cabinet_seek(INT_PTR hf
, long dist
, int seektype
)
243 HANDLE handle
= (HANDLE
) hf
;
244 /* flags are compatible and so are passed straight through */
245 return SetFilePointer(handle
, dist
, NULL
, seektype
);
248 static void msi_file_update_ui( MSIPACKAGE
*package
, MSIFILE
*f
, const WCHAR
*action
)
254 uirow
= MSI_CreateRecord( 9 );
255 MSI_RecordSetStringW( uirow
, 1, f
->FileName
);
256 uipath
= strdupW( f
->TargetPath
);
257 p
= strrchrW(uipath
,'\\');
260 MSI_RecordSetStringW( uirow
, 9, uipath
);
261 MSI_RecordSetInteger( uirow
, 6, f
->FileSize
);
262 ui_actiondata( package
, action
, uirow
);
263 msiobj_release( &uirow
->hdr
);
265 ui_progress( package
, 2, f
->FileSize
, 0, 0);
268 static UINT
msi_media_get_disk_info( MSIPACKAGE
*package
, struct media_info
*mi
)
273 static const WCHAR query
[] =
274 {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
275 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
276 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
278 row
= MSI_QueryGetRecord(package
->db
, query
, mi
->disk_id
);
281 TRACE("Unable to query row\n");
282 return ERROR_FUNCTION_FAILED
;
285 mi
->disk_prompt
= strdupW(MSI_RecordGetString(row
, 3));
286 mi
->cabinet
= strdupW(MSI_RecordGetString(row
, 4));
287 mi
->volume_label
= strdupW(MSI_RecordGetString(row
, 5));
289 if (!mi
->first_volume
)
290 mi
->first_volume
= strdupW(mi
->volume_label
);
292 ptr
= strrchrW(mi
->source
, '\\') + 1;
293 lstrcpyW(ptr
, mi
->cabinet
);
294 msiobj_release(&row
->hdr
);
296 return ERROR_SUCCESS
;
299 static INT_PTR
cabinet_notify(FDINOTIFICATIONTYPE fdint
, PFDINOTIFICATION pfdin
)
301 TRACE("(%d)\n", fdint
);
305 case fdintPARTIAL_FILE
:
307 CabData
*data
= (CabData
*)pfdin
->pv
;
308 data
->mi
->is_continuous
= FALSE
;
311 case fdintNEXT_CABINET
:
313 CabData
*data
= (CabData
*)pfdin
->pv
;
314 struct media_info
*mi
= data
->mi
;
315 LPWSTR cab
= strdupAtoW(pfdin
->psz1
);
318 msi_free(mi
->disk_prompt
);
319 msi_free(mi
->cabinet
);
320 msi_free(mi
->volume_label
);
321 mi
->disk_prompt
= NULL
;
323 mi
->volume_label
= NULL
;
326 mi
->is_continuous
= TRUE
;
328 rc
= msi_media_get_disk_info(data
->package
, mi
);
329 if (rc
!= ERROR_SUCCESS
)
332 ERR("Failed to get next cabinet information: %d\n", rc
);
336 if (lstrcmpiW(mi
->cabinet
, cab
))
339 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
345 TRACE("Searching for %s\n", debugstr_w(mi
->source
));
347 if (GetFileAttributesW(mi
->source
) == INVALID_FILE_ATTRIBUTES
)
348 rc
= msi_change_media(data
->package
, mi
);
350 if (rc
!= ERROR_SUCCESS
)
357 CabData
*data
= (CabData
*) pfdin
->pv
;
363 file
= strdupAtoW(pfdin
->psz1
);
364 f
= get_loaded_file(data
->package
, file
);
369 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin
->psz1
));
373 if (f
->state
!= msifs_missing
&& f
->state
!= msifs_overwrite
)
375 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin
->psz1
));
379 msi_file_update_ui( data
->package
, f
, szInstallFiles
);
381 TRACE("extracting %s\n", debugstr_w(f
->TargetPath
) );
383 attrs
= f
->Attributes
& (FILE_ATTRIBUTE_READONLY
|FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
);
384 if (!attrs
) attrs
= FILE_ATTRIBUTE_NORMAL
;
386 handle
= CreateFileW( f
->TargetPath
, GENERIC_READ
| GENERIC_WRITE
, 0,
387 NULL
, CREATE_ALWAYS
, attrs
, NULL
);
388 if ( handle
== INVALID_HANDLE_VALUE
)
390 if ( GetFileAttributesW( f
->TargetPath
) != INVALID_FILE_ATTRIBUTES
)
391 f
->state
= msifs_installed
;
393 ERR("failed to create %s (error %d)\n",
394 debugstr_w( f
->TargetPath
), GetLastError() );
399 f
->state
= msifs_installed
;
400 return (INT_PTR
) handle
;
402 case fdintCLOSE_FILE_INFO
:
404 CabData
*data
= (CabData
*) pfdin
->pv
;
407 HANDLE handle
= (HANDLE
) pfdin
->hf
;
409 data
->mi
->is_continuous
= FALSE
;
411 if (!DosDateTimeToFileTime(pfdin
->date
, pfdin
->time
, &ft
))
413 if (!LocalFileTimeToFileTime(&ft
, &ftLocal
))
415 if (!SetFileTime(handle
, &ftLocal
, 0, &ftLocal
))
425 /***********************************************************************
426 * extract_cabinet_file
428 * Extract files from a cab file.
430 static BOOL
extract_cabinet_file(MSIPACKAGE
* package
, struct media_info
*mi
)
432 LPSTR cabinet
, cab_path
= NULL
;
439 TRACE("Extracting %s\n", debugstr_w(mi
->source
));
441 hfdi
= FDICreate(cabinet_alloc
, cabinet_free
, cabinet_open
, cabinet_read
,
442 cabinet_write
, cabinet_close
, cabinet_seek
, 0, &erf
);
445 ERR("FDICreate failed\n");
449 ptr
= strrchrW(mi
->source
, '\\') + 1;
450 cabinet
= strdupWtoA(ptr
);
454 cab_path
= strdupWtoA(mi
->source
);
458 cab_path
[ptr
- mi
->source
] = '\0';
460 data
.package
= package
;
463 ret
= FDICopy(hfdi
, cabinet
, cab_path
, 0, cabinet_notify
, NULL
, &data
);
465 ERR("FDICopy failed\n");
473 mi
->is_extracted
= TRUE
;
478 static VOID
set_file_source(MSIPACKAGE
* package
, MSIFILE
* file
, LPCWSTR path
)
480 if (!file
->IsCompressed
)
483 p
= resolve_folder(package
, file
->Component
->Directory
, TRUE
, FALSE
, TRUE
, NULL
);
484 path
= build_directory_name(2, p
, file
->ShortName
);
485 if (file
->LongName
&&
486 INVALID_FILE_ATTRIBUTES
== GetFileAttributesW( path
))
489 path
= build_directory_name(2, p
, file
->LongName
);
491 file
->SourcePath
= path
;
495 file
->SourcePath
= build_directory_name(2, path
, file
->File
);
498 static void free_media_info( struct media_info
*mi
)
500 msi_free( mi
->disk_prompt
);
501 msi_free( mi
->cabinet
);
502 msi_free( mi
->volume_label
);
503 msi_free( mi
->first_volume
);
507 static UINT
load_media_info(MSIPACKAGE
*package
, MSIFILE
*file
, struct media_info
*mi
)
515 static const WCHAR query
[] = {
516 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
517 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
518 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
519 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
520 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
521 '`','D','i','s','k','I','d','`',0
524 row
= MSI_QueryGetRecord(package
->db
, query
, file
->Sequence
, mi
->disk_id
);
527 TRACE("Unable to query row\n");
528 return ERROR_FUNCTION_FAILED
;
531 mi
->is_extracted
= FALSE
;
532 mi
->disk_id
= MSI_RecordGetInteger(row
, 1);
533 mi
->last_sequence
= MSI_RecordGetInteger(row
, 2);
534 msi_free(mi
->disk_prompt
);
535 mi
->disk_prompt
= strdupW(MSI_RecordGetString(row
, 3));
536 msi_free(mi
->cabinet
);
537 mi
->cabinet
= strdupW(MSI_RecordGetString(row
, 4));
538 msi_free(mi
->volume_label
);
539 mi
->volume_label
= strdupW(MSI_RecordGetString(row
, 5));
540 msiobj_release(&row
->hdr
);
542 if (!mi
->first_volume
)
543 mi
->first_volume
= strdupW(mi
->volume_label
);
545 source_dir
= msi_dup_property(package
, cszSourceDir
);
546 lstrcpyW(mi
->source
, source_dir
);
548 PathStripToRootW(source_dir
);
549 mi
->type
= GetDriveTypeW(source_dir
);
551 if (file
->IsCompressed
&& mi
->cabinet
)
553 if (mi
->cabinet
[0] == '#')
555 r
= writeout_cabinet_stream(package
, &mi
->cabinet
[1], mi
->source
);
556 if (r
!= ERROR_SUCCESS
)
558 ERR("Failed to extract cabinet stream\n");
559 return ERROR_FUNCTION_FAILED
;
563 lstrcatW(mi
->source
, mi
->cabinet
);
566 options
= MSICODE_PRODUCT
;
567 if (mi
->type
== DRIVE_CDROM
|| mi
->type
== DRIVE_REMOVABLE
)
570 options
|= MSISOURCETYPE_MEDIA
;
572 else if (package
->BaseURL
&& UrlIsW(package
->BaseURL
, URLIS_URL
))
574 source
= package
->BaseURL
;
575 options
|= MSISOURCETYPE_URL
;
580 options
|= MSISOURCETYPE_NETWORK
;
583 if (mi
->type
== DRIVE_CDROM
|| mi
->type
== DRIVE_REMOVABLE
)
584 msi_package_add_media_disk(package
, MSIINSTALLCONTEXT_USERUNMANAGED
,
585 MSICODE_PRODUCT
, mi
->disk_id
,
586 mi
->volume_label
, mi
->disk_prompt
);
588 msi_package_add_info(package
, MSIINSTALLCONTEXT_USERUNMANAGED
,
589 options
, INSTALLPROPERTY_LASTUSEDSOURCEW
, source
);
591 msi_free(source_dir
);
592 return ERROR_SUCCESS
;
595 /* FIXME: search NETWORK and URL sources as well */
596 static UINT
find_published_source(MSIPACKAGE
*package
, struct media_info
*mi
)
598 WCHAR source
[MAX_PATH
];
599 WCHAR volume
[MAX_PATH
];
600 WCHAR prompt
[MAX_PATH
];
601 DWORD volumesz
, promptsz
;
606 r
= MsiSourceListGetInfoW(package
->ProductCode
, NULL
,
607 MSIINSTALLCONTEXT_USERUNMANAGED
, MSICODE_PRODUCT
,
608 INSTALLPROPERTY_LASTUSEDSOURCEW
, source
, &size
);
609 if (r
!= ERROR_SUCCESS
)
615 while (MsiSourceListEnumMediaDisksW(package
->ProductCode
, NULL
,
616 MSIINSTALLCONTEXT_USERUNMANAGED
,
617 MSICODE_PRODUCT
, index
++, &id
,
618 volume
, &volumesz
, prompt
, &promptsz
) == ERROR_SUCCESS
)
621 mi
->volume_label
= msi_realloc(mi
->volume_label
, ++volumesz
* sizeof(WCHAR
));
622 lstrcpyW(mi
->volume_label
, volume
);
623 mi
->disk_prompt
= msi_realloc(mi
->disk_prompt
, ++promptsz
* sizeof(WCHAR
));
624 lstrcpyW(mi
->disk_prompt
, prompt
);
626 if (source_matches_volume(mi
, source
))
628 /* FIXME: what about SourceDir */
629 lstrcpyW(mi
->source
, source
);
630 lstrcatW(mi
->source
, mi
->cabinet
);
631 return ERROR_SUCCESS
;
635 return ERROR_FUNCTION_FAILED
;
638 static UINT
ready_media(MSIPACKAGE
*package
, MSIFILE
*file
, struct media_info
*mi
)
640 UINT rc
= ERROR_SUCCESS
;
642 /* media info for continuous cabinet is already loaded */
643 if (mi
->is_continuous
)
644 return ERROR_SUCCESS
;
646 rc
= load_media_info(package
, file
, mi
);
647 if (rc
!= ERROR_SUCCESS
)
649 ERR("Unable to load media info\n");
650 return ERROR_FUNCTION_FAILED
;
653 /* cabinet is internal, no checks needed */
654 if (!mi
->cabinet
|| mi
->cabinet
[0] == '#')
655 return ERROR_SUCCESS
;
657 /* package should be downloaded */
658 if (file
->IsCompressed
&&
659 GetFileAttributesW(mi
->source
) == INVALID_FILE_ATTRIBUTES
&&
660 package
->BaseURL
&& UrlIsW(package
->BaseURL
, URLIS_URL
))
662 WCHAR temppath
[MAX_PATH
];
664 msi_download_file(mi
->source
, temppath
);
665 lstrcpyW(mi
->source
, temppath
);
666 return ERROR_SUCCESS
;
669 /* check volume matches, change media if not */
670 if (mi
->volume_label
&& mi
->disk_id
> 1 &&
671 lstrcmpW(mi
->first_volume
, mi
->volume_label
))
673 LPWSTR source
= msi_dup_property(package
, cszSourceDir
);
676 matches
= source_matches_volume(mi
, source
);
679 if ((mi
->type
== DRIVE_CDROM
|| mi
->type
== DRIVE_REMOVABLE
) && !matches
)
681 rc
= msi_change_media(package
, mi
);
682 if (rc
!= ERROR_SUCCESS
)
687 if (file
->IsCompressed
&&
688 GetFileAttributesW(mi
->source
) == INVALID_FILE_ATTRIBUTES
)
690 /* FIXME: this might be done earlier in the install process */
691 rc
= find_published_source(package
, mi
);
692 if (rc
!= ERROR_SUCCESS
)
694 ERR("Cabinet not found: %s\n", debugstr_w(mi
->source
));
695 return ERROR_INSTALL_FAILURE
;
699 return ERROR_SUCCESS
;
702 static UINT
get_file_target(MSIPACKAGE
*package
, LPCWSTR file_key
,
705 LIST_FOR_EACH_ENTRY( *file
, &package
->files
, MSIFILE
, entry
)
707 if (lstrcmpW( file_key
, (*file
)->File
)==0)
709 if ((*file
)->state
>= msifs_overwrite
)
710 return ERROR_SUCCESS
;
712 return ERROR_FILE_NOT_FOUND
;
716 return ERROR_FUNCTION_FAILED
;
719 static void schedule_install_files(MSIPACKAGE
*package
)
723 LIST_FOR_EACH_ENTRY(file
, &package
->files
, MSIFILE
, entry
)
725 if (!ACTION_VerifyComponentForAction(file
->Component
, INSTALLSTATE_LOCAL
))
727 TRACE("File %s is not scheduled for install\n", debugstr_w(file
->File
));
729 ui_progress(package
,2,file
->FileSize
,0,0);
730 file
->state
= msifs_skipped
;
735 static UINT
copy_file(MSIFILE
*file
)
739 ret
= CopyFileW(file
->SourcePath
, file
->TargetPath
, FALSE
);
742 file
->state
= msifs_installed
;
743 return ERROR_SUCCESS
;
746 return GetLastError();
749 static UINT
copy_install_file(MSIFILE
*file
)
753 TRACE("Copying %s to %s\n", debugstr_w(file
->SourcePath
),
754 debugstr_w(file
->TargetPath
));
756 gle
= copy_file(file
);
757 if (gle
== ERROR_SUCCESS
)
760 if (gle
== ERROR_ALREADY_EXISTS
&& file
->state
== msifs_overwrite
)
762 TRACE("overwriting existing file\n");
765 else if (gle
== ERROR_FILE_NOT_FOUND
)
767 /* FIXME: this needs to be tested, I'm pretty sure it fails */
768 TRACE("Source file not found\n");
771 else if (gle
== ERROR_ACCESS_DENIED
)
773 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
775 gle
= copy_file(file
);
776 TRACE("Overwriting existing file: %d\n", gle
);
778 else if (!(file
->Attributes
& msidbFileAttributesVital
))
780 TRACE("Ignoring error for nonvital\n");
787 static BOOL
check_dest_hash_matches(MSIFILE
*file
)
789 MSIFILEHASHINFO hash
;
792 if (!file
->hash
.dwFileHashInfoSize
)
795 hash
.dwFileHashInfoSize
= sizeof(MSIFILEHASHINFO
);
796 r
= MsiGetFileHashW(file
->TargetPath
, 0, &hash
);
797 if (r
!= ERROR_SUCCESS
)
800 return !memcmp(&hash
, &file
->hash
, sizeof(MSIFILEHASHINFO
));
804 * ACTION_InstallFiles()
806 * For efficiency, this is done in two passes:
807 * 1) Correct all the TargetPaths and determine what files are to be installed.
808 * 2) Extract Cabinets and copy files.
810 UINT
ACTION_InstallFiles(MSIPACKAGE
*package
)
812 struct media_info
*mi
;
813 UINT rc
= ERROR_SUCCESS
;
816 /* increment progress bar each time action data is sent */
817 ui_progress(package
,1,1,0,0);
819 schedule_install_files(package
);
822 * Despite MSDN specifying that the CreateFolders action
823 * should be called before InstallFiles, some installers don't
824 * do that, and they seem to work correctly. We need to create
825 * directories here to make sure that the files can be copied.
827 msi_create_component_directories( package
);
829 mi
= msi_alloc_zero( sizeof(struct media_info
) );
831 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
833 if (file
->state
!= msifs_missing
&& !mi
->is_continuous
&& file
->state
!= msifs_overwrite
)
836 if (check_dest_hash_matches(file
))
838 TRACE("File hashes match, not overwriting\n");
842 if (file
->Sequence
> mi
->last_sequence
|| mi
->is_continuous
||
843 (file
->IsCompressed
&& !mi
->is_extracted
))
845 rc
= ready_media(package
, file
, mi
);
846 if (rc
!= ERROR_SUCCESS
)
848 ERR("Failed to ready media\n");
852 if (file
->IsCompressed
&& !extract_cabinet_file(package
, mi
))
854 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
855 rc
= ERROR_FUNCTION_FAILED
;
860 set_file_source(package
, file
, mi
->source
);
862 TRACE("file paths %s to %s\n",debugstr_w(file
->SourcePath
),
863 debugstr_w(file
->TargetPath
));
865 if (!file
->IsCompressed
)
867 msi_file_update_ui(package
, file
, szInstallFiles
);
868 rc
= copy_install_file(file
);
869 if (rc
!= ERROR_SUCCESS
)
871 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(file
->SourcePath
),
872 debugstr_w(file
->TargetPath
), rc
);
873 rc
= ERROR_INSTALL_FAILURE
;
877 else if (file
->state
!= msifs_installed
)
879 ERR("compressed file wasn't extracted (%s)\n", debugstr_w(file
->TargetPath
));
880 rc
= ERROR_INSTALL_FAILURE
;
885 free_media_info( mi
);
889 static UINT
ITERATE_DuplicateFiles(MSIRECORD
*row
, LPVOID param
)
891 MSIPACKAGE
*package
= (MSIPACKAGE
*)param
;
892 WCHAR dest_name
[0x100];
893 LPWSTR dest_path
, dest
;
894 LPCWSTR file_key
, component
;
900 component
= MSI_RecordGetString(row
,2);
901 comp
= get_loaded_component(package
,component
);
903 if (!ACTION_VerifyComponentForAction( comp
, INSTALLSTATE_LOCAL
))
905 TRACE("Skipping copy due to disabled component %s\n",
906 debugstr_w(component
));
908 /* the action taken was the same as the current install state */
909 comp
->Action
= comp
->Installed
;
911 return ERROR_SUCCESS
;
914 comp
->Action
= INSTALLSTATE_LOCAL
;
916 file_key
= MSI_RecordGetString(row
,3);
919 ERR("Unable to get file key\n");
920 return ERROR_FUNCTION_FAILED
;
923 rc
= get_file_target(package
,file_key
,&file
);
925 if (rc
!= ERROR_SUCCESS
)
927 ERR("Original file unknown %s\n",debugstr_w(file_key
));
928 return ERROR_SUCCESS
;
931 if (MSI_RecordIsNull(row
,4))
932 strcpyW(dest_name
,strrchrW(file
->TargetPath
,'\\')+1);
936 MSI_RecordGetStringW(row
,4,dest_name
,&sz
);
937 reduce_to_longfilename(dest_name
);
940 if (MSI_RecordIsNull(row
,5))
943 dest_path
= strdupW(file
->TargetPath
);
944 p
= strrchrW(dest_path
,'\\');
951 destkey
= MSI_RecordGetString(row
,5);
952 dest_path
= resolve_folder(package
, destkey
, FALSE
, FALSE
, TRUE
, NULL
);
956 dest_path
= msi_dup_property( package
, destkey
);
959 FIXME("Unable to get destination folder, try AppSearch properties\n");
960 return ERROR_SUCCESS
;
965 dest
= build_directory_name(2, dest_path
, dest_name
);
966 create_full_pathW(dest_path
);
968 TRACE("Duplicating file %s to %s\n",debugstr_w(file
->TargetPath
),
971 if (strcmpW(file
->TargetPath
,dest
))
972 rc
= !CopyFileW(file
->TargetPath
,dest
,TRUE
);
976 if (rc
!= ERROR_SUCCESS
)
977 ERR("Failed to copy file %s -> %s, last error %d\n",
978 debugstr_w(file
->TargetPath
), debugstr_w(dest_path
), GetLastError());
980 FIXME("We should track these duplicate files as well\n");
985 msi_file_update_ui(package
, file
, szDuplicateFiles
);
987 return ERROR_SUCCESS
;
990 UINT
ACTION_DuplicateFiles(MSIPACKAGE
*package
)
994 static const WCHAR ExecSeqQuery
[] =
995 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
996 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
998 rc
= MSI_DatabaseOpenViewW(package
->db
, ExecSeqQuery
, &view
);
999 if (rc
!= ERROR_SUCCESS
)
1000 return ERROR_SUCCESS
;
1002 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_DuplicateFiles
, package
);
1003 msiobj_release(&view
->hdr
);
1008 /* compares the version of a file read from the filesystem and
1009 * the version specified in the File table
1011 static int msi_compare_file_version( MSIFILE
*file
)
1013 WCHAR version
[MAX_PATH
];
1019 r
= MsiGetFileVersionW( file
->TargetPath
, version
, &size
, NULL
, NULL
);
1020 if ( r
!= ERROR_SUCCESS
)
1023 return lstrcmpW( version
, file
->Version
);
1026 UINT
ACTION_RemoveFiles( MSIPACKAGE
*package
)
1030 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
1035 if ( !file
->Component
)
1037 if ( file
->Component
->Installed
== INSTALLSTATE_LOCAL
)
1040 if ( file
->state
== msifs_installed
)
1041 ERR("removing installed file %s\n", debugstr_w(file
->TargetPath
));
1043 if ( file
->state
!= msifs_present
)
1046 /* only remove a file if the version to be installed
1047 * is strictly newer than the old file
1049 if ( msi_compare_file_version( file
) >= 0 )
1052 TRACE("removing %s\n", debugstr_w(file
->File
) );
1053 if ( !DeleteFileW( file
->TargetPath
) )
1054 ERR("failed to delete %s\n", debugstr_w(file
->TargetPath
) );
1055 file
->state
= msifs_missing
;
1058 uirow
= MSI_CreateRecord( 9 );
1059 MSI_RecordSetStringW( uirow
, 1, file
->FileName
);
1060 uipath
= strdupW( file
->TargetPath
);
1061 p
= strrchrW(uipath
,'\\');
1064 MSI_RecordSetStringW( uirow
, 9, uipath
);
1065 ui_actiondata( package
, szRemoveFiles
, uirow
);
1066 msiobj_release( &uirow
->hdr
);
1068 /* FIXME: call ui_progress here? */
1071 return ERROR_SUCCESS
;