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};
67 WCHAR source[MAX_PATH];
70 static UINT msi_change_media( MSIPACKAGE *package, struct media_info *mi )
72 LPWSTR error, error_dialog;
73 UINT r = ERROR_SUCCESS;
75 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
76 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
78 if ( msi_get_property_int(package, szUILevel, 0) == INSTALLUILEVEL_NONE )
81 error = generate_error_string( package, 1302, 1, mi->disk_prompt );
82 error_dialog = msi_dup_property( package, error_prop );
84 while ( r == ERROR_SUCCESS && GetFileAttributesW( mi->source ) == INVALID_FILE_ATTRIBUTES )
85 r = msi_spawn_error_dialog( package, error_dialog, error );
88 msi_free( error_dialog );
94 * This is a helper function for handling embedded cabinet media
96 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
106 rc = read_raw_stream_data(package->db,stream_name,&data,&size);
107 if (rc != ERROR_SUCCESS)
111 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
112 GetTempPathW(MAX_PATH,tmp);
114 GetTempFileNameW(tmp,stream_name,0,source);
116 track_tempfile(package,strrchrW(source,'\\'), source);
117 the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
118 FILE_ATTRIBUTE_NORMAL, NULL);
120 if (the_file == INVALID_HANDLE_VALUE)
122 ERR("Unable to create file %s\n",debugstr_w(source));
123 rc = ERROR_FUNCTION_FAILED;
127 WriteFile(the_file,data,size,&write,NULL);
128 CloseHandle(the_file);
129 TRACE("wrote %i bytes to %s\n",write,debugstr_w(source));
136 /* Support functions for FDI functions */
140 struct media_info *mi;
143 static void * cabinet_alloc(ULONG cb)
145 return msi_alloc(cb);
148 static void cabinet_free(void *pv)
153 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
157 DWORD dwShareMode = 0;
158 DWORD dwCreateDisposition = OPEN_EXISTING;
159 switch (oflag & _O_ACCMODE)
162 dwAccess = GENERIC_READ;
163 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
166 dwAccess = GENERIC_WRITE;
167 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
170 dwAccess = GENERIC_READ | GENERIC_WRITE;
171 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
174 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
175 dwCreateDisposition = CREATE_NEW;
176 else if (oflag & _O_CREAT)
177 dwCreateDisposition = CREATE_ALWAYS;
178 handle = CreateFileA( pszFile, dwAccess, dwShareMode, NULL,
179 dwCreateDisposition, 0, NULL );
180 if (handle == INVALID_HANDLE_VALUE)
182 return (INT_PTR) handle;
185 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
187 HANDLE handle = (HANDLE) hf;
189 if (ReadFile(handle, pv, cb, &dwRead, NULL))
194 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
196 HANDLE handle = (HANDLE) hf;
198 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
203 static int cabinet_close(INT_PTR hf)
205 HANDLE handle = (HANDLE) hf;
206 return CloseHandle(handle) ? 0 : -1;
209 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
211 HANDLE handle = (HANDLE) hf;
212 /* flags are compatible and so are passed straight through */
213 return SetFilePointer(handle, dist, NULL, seektype);
216 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
222 uirow = MSI_CreateRecord( 9 );
223 MSI_RecordSetStringW( uirow, 1, f->FileName );
224 uipath = strdupW( f->TargetPath );
225 p = strrchrW(uipath,'\\');
228 MSI_RecordSetStringW( uirow, 9, uipath);
229 MSI_RecordSetInteger( uirow, 6, f->FileSize );
230 ui_actiondata( package, action, uirow);
231 msiobj_release( &uirow->hdr );
233 ui_progress( package, 2, f->FileSize, 0, 0);
236 static UINT msi_media_get_disk_info( CabData *data )
238 MSIPACKAGE *package = data->package;
242 static const WCHAR query[] =
243 {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
244 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
245 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
247 row = MSI_QueryGetRecord(package->db, query, data->mi->disk_id);
250 TRACE("Unable to query row\n");
251 return ERROR_FUNCTION_FAILED;
254 data->mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
255 data->mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
257 ptr = strrchrW(data->mi->source, '\\') + 1;
258 lstrcpyW(ptr, data->mi->cabinet);
260 return ERROR_SUCCESS;
263 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
265 TRACE("(%d)\n", fdint);
269 case fdintPARTIAL_FILE:
271 CabData *data = (CabData *)pfdin->pv;
272 data->mi->is_continuous = FALSE;
275 case fdintNEXT_CABINET:
277 CabData *data = (CabData *)pfdin->pv;
278 struct media_info *mi = data->mi;
279 LPWSTR cab = strdupAtoW(pfdin->psz1);
282 msi_free(mi->disk_prompt);
285 mi->is_continuous = TRUE;
287 rc = msi_media_get_disk_info(data);
288 if (rc != ERROR_SUCCESS)
290 ERR("Failed to get next cabinet information: %d\n", rc);
294 if (lstrcmpiW(mi->cabinet, cab))
297 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
303 TRACE("Searching for %s\n", debugstr_w(mi->source));
305 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
306 rc = msi_change_media(data->package, mi);
308 if (rc != ERROR_SUCCESS)
315 CabData *data = (CabData*) pfdin->pv;
321 file = strdupAtoW(pfdin->psz1);
322 f = get_loaded_file(data->package, file);
327 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
331 if (f->state != msifs_missing && f->state != msifs_overwrite)
333 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
337 msi_file_update_ui( data->package, f, szInstallFiles );
339 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
341 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
342 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
344 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
345 NULL, CREATE_ALWAYS, attrs, NULL );
346 if ( handle == INVALID_HANDLE_VALUE )
348 ERR("failed to create %s (error %d)\n",
349 debugstr_w( f->TargetPath ), GetLastError() );
353 f->state = msifs_installed;
354 return (INT_PTR) handle;
356 case fdintCLOSE_FILE_INFO:
360 HANDLE handle = (HANDLE) pfdin->hf;
362 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
364 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
366 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
376 /***********************************************************************
377 * extract_cabinet_file
379 * Extract files from a cab file.
381 static BOOL extract_cabinet_file(MSIPACKAGE* package, struct media_info *mi)
383 LPSTR cabinet, cab_path = NULL;
390 TRACE("Extracting %s\n", debugstr_w(mi->source));
392 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
393 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
396 ERR("FDICreate failed\n");
400 ptr = strrchrW(mi->source, '\\') + 1;
401 cabinet = strdupWtoA(ptr);
405 cab_path = strdupWtoA(mi->source);
409 cab_path[ptr - mi->source] = '\0';
411 data.package = package;
414 ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, &data);
416 ERR("FDICopy failed\n");
426 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, LPCWSTR path)
428 if (!file->IsCompressed)
431 p = resolve_folder(package, file->Component->Directory, TRUE, FALSE, NULL);
432 path = build_directory_name(2, p, file->ShortName);
433 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
436 path = build_directory_name(2, p, file->LongName);
438 file->SourcePath = path;
442 file->SourcePath = build_directory_name(2, path, file->File);
445 static void free_media_info( struct media_info *mi )
447 msi_free( mi->disk_prompt );
448 msi_free( mi->cabinet );
449 msi_free( mi->volume_label );
453 static UINT download_remote_cabinet(MSIPACKAGE *package, struct media_info *mi)
455 WCHAR temppath[MAX_PATH];
459 src = strdupW(package->BaseURL);
461 return ERROR_OUTOFMEMORY;
463 ptr = strrchrW(src, '/');
467 return ERROR_FUNCTION_FAILED;
471 ptr = strrchrW(mi->source, '\\');
472 lstrcatW(src, ptr + 1);
474 cab = msi_download_file(src, temppath);
475 lstrcpyW(mi->source, cab);
478 return ERROR_SUCCESS;
481 static UINT load_media_info(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
487 static const WCHAR query[] = {
488 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
489 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
490 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
491 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
492 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
493 '`','D','i','s','k','I','d','`',0
496 row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
499 TRACE("Unable to query row\n");
500 return ERROR_FUNCTION_FAILED;
503 mi->disk_id = MSI_RecordGetInteger(row, 1);
504 mi->last_sequence = MSI_RecordGetInteger(row, 2);
505 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
506 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
507 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
508 msiobj_release(&row->hdr);
510 source_dir = msi_dup_property(package, cszSourceDir);
512 if (mi->cabinet && mi->cabinet[0] == '#')
514 r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
515 if (r != ERROR_SUCCESS)
517 ERR("Failed to extract cabinet stream\n");
518 return ERROR_FUNCTION_FAILED;
523 lstrcpyW(mi->source, source_dir);
527 lstrcatW(mi->source, mi->cabinet);
530 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
531 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT,
532 mi->disk_id, mi->volume_label, mi->disk_prompt);
534 MsiSourceListSetInfoW(package->ProductCode, NULL,
535 MSIINSTALLCONTEXT_USERMANAGED,
536 MSICODE_PRODUCT | MSISOURCETYPE_MEDIA,
537 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->source);
539 msi_free(source_dir);
540 return ERROR_SUCCESS;
543 static UINT ready_media(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
545 UINT rc = ERROR_SUCCESS;
548 /* media info for continuous cabinet is already loaded */
549 if (mi->is_continuous)
550 return ERROR_SUCCESS;
552 rc = load_media_info(package, file, mi);
553 if (rc != ERROR_SUCCESS)
555 ERR("Unable to load media info\n");
556 return ERROR_FUNCTION_FAILED;
559 if (file->IsCompressed &&
560 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
562 if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
564 rc = download_remote_cabinet(package, mi);
565 if (rc == ERROR_SUCCESS &&
566 GetFileAttributesW(mi->source) != INVALID_FILE_ATTRIBUTES)
573 rc = msi_change_media(package, mi);
579 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
582 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
584 if (lstrcmpW( file_key, (*file)->File )==0)
586 if ((*file)->state >= msifs_overwrite)
587 return ERROR_SUCCESS;
589 return ERROR_FILE_NOT_FOUND;
593 return ERROR_FUNCTION_FAILED;
596 static void schedule_install_files(MSIPACKAGE *package)
600 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
602 if (!ACTION_VerifyComponentForAction(file->Component, INSTALLSTATE_LOCAL))
604 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
606 ui_progress(package,2,file->FileSize,0,0);
607 file->state = msifs_skipped;
612 static UINT copy_install_file(MSIFILE *file)
617 TRACE("Copying %s to %s\n", debugstr_w(file->SourcePath),
618 debugstr_w(file->TargetPath));
620 ret = CopyFileW(file->SourcePath, file->TargetPath, FALSE);
623 file->state = msifs_installed;
624 return ERROR_SUCCESS;
627 gle = GetLastError();
628 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
630 TRACE("overwriting existing file\n");
633 else if (gle == ERROR_FILE_NOT_FOUND)
635 /* FIXME: this needs to be tested, I'm pretty sure it fails */
636 TRACE("Source file not found\n");
639 else if (!(file->Attributes & msidbFileAttributesVital))
641 TRACE("Ignoring error for nonvital\n");
649 * ACTION_InstallFiles()
651 * For efficiency, this is done in two passes:
652 * 1) Correct all the TargetPaths and determine what files are to be installed.
653 * 2) Extract Cabinets and copy files.
655 UINT ACTION_InstallFiles(MSIPACKAGE *package)
657 struct media_info *mi;
658 UINT rc = ERROR_SUCCESS;
662 /* increment progress bar each time action data is sent */
663 ui_progress(package,1,1,0,0);
665 /* handle the keys for the SourceList */
666 ptr = strrchrW(package->PackagePath,'\\');
670 MsiSourceListSetInfoW(package->ProductCode, NULL,
671 MSIINSTALLCONTEXT_USERMANAGED,
673 INSTALLPROPERTY_PACKAGENAMEW, ptr);
676 schedule_install_files(package);
679 * Despite MSDN specifying that the CreateFolders action
680 * should be called before InstallFiles, some installers don't
681 * do that, and they seem to work correctly. We need to create
682 * directories here to make sure that the files can be copied.
684 msi_create_component_directories( package );
686 mi = msi_alloc_zero( sizeof(struct media_info) );
688 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
690 if (file->state != msifs_missing && file->state != msifs_overwrite)
693 if (file->Sequence > mi->last_sequence || mi->is_continuous)
695 rc = ready_media(package, file, mi);
696 if (rc != ERROR_SUCCESS)
698 ERR("Failed to ready media\n");
699 rc = ERROR_FUNCTION_FAILED;
703 if (file->IsCompressed && !extract_cabinet_file(package, mi))
705 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
706 rc = ERROR_FUNCTION_FAILED;
711 set_file_source(package, file, mi->source);
713 TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
714 debugstr_w(file->TargetPath));
716 if (!file->IsCompressed)
718 rc = copy_install_file(file);
719 if (rc != ERROR_SUCCESS)
721 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(file->SourcePath),
722 debugstr_w(file->TargetPath), rc);
723 rc = ERROR_INSTALL_FAILURE;
727 else if (file->state != msifs_installed)
729 ERR("compressed file wasn't extracted (%s)\n", debugstr_w(file->TargetPath));
730 rc = ERROR_INSTALL_FAILURE;
735 free_media_info( mi );
739 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
741 MSIPACKAGE *package = (MSIPACKAGE*)param;
742 WCHAR dest_name[0x100];
743 LPWSTR dest_path, dest;
744 LPCWSTR file_key, component;
750 component = MSI_RecordGetString(row,2);
751 comp = get_loaded_component(package,component);
753 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
755 TRACE("Skipping copy due to disabled component %s\n",
756 debugstr_w(component));
758 /* the action taken was the same as the current install state */
759 comp->Action = comp->Installed;
761 return ERROR_SUCCESS;
764 comp->Action = INSTALLSTATE_LOCAL;
766 file_key = MSI_RecordGetString(row,3);
769 ERR("Unable to get file key\n");
770 return ERROR_FUNCTION_FAILED;
773 rc = get_file_target(package,file_key,&file);
775 if (rc != ERROR_SUCCESS)
777 ERR("Original file unknown %s\n",debugstr_w(file_key));
778 return ERROR_SUCCESS;
781 if (MSI_RecordIsNull(row,4))
782 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
786 MSI_RecordGetStringW(row,4,dest_name,&sz);
787 reduce_to_longfilename(dest_name);
790 if (MSI_RecordIsNull(row,5))
793 dest_path = strdupW(file->TargetPath);
794 p = strrchrW(dest_path,'\\');
801 destkey = MSI_RecordGetString(row,5);
802 dest_path = resolve_folder(package, destkey, FALSE,FALSE,NULL);
806 dest_path = msi_dup_property( package, destkey );
809 FIXME("Unable to get destination folder, try AppSearch properties\n");
810 return ERROR_SUCCESS;
815 dest = build_directory_name(2, dest_path, dest_name);
817 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
820 if (strcmpW(file->TargetPath,dest))
821 rc = !CopyFileW(file->TargetPath,dest,TRUE);
825 if (rc != ERROR_SUCCESS)
826 ERR("Failed to copy file %s -> %s, last error %d\n",
827 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
829 FIXME("We should track these duplicate files as well\n");
834 msi_file_update_ui(package, file, szDuplicateFiles);
836 return ERROR_SUCCESS;
839 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
843 static const WCHAR ExecSeqQuery[] =
844 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
845 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
847 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
848 if (rc != ERROR_SUCCESS)
849 return ERROR_SUCCESS;
851 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
852 msiobj_release(&view->hdr);
857 /* compares the version of a file read from the filesystem and
858 * the version specified in the File table
860 static int msi_compare_file_version( MSIFILE *file )
862 WCHAR version[MAX_PATH];
868 r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
869 if ( r != ERROR_SUCCESS )
872 return lstrcmpW( version, file->Version );
875 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
879 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
884 if ( !file->Component )
886 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
889 if ( file->state == msifs_installed )
890 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
892 if ( file->state != msifs_present )
895 /* only remove a file if the version to be installed
896 * is strictly newer than the old file
898 if ( msi_compare_file_version( file ) >= 0 )
901 TRACE("removing %s\n", debugstr_w(file->File) );
902 if ( !DeleteFileW( file->TargetPath ) )
903 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
904 file->state = msifs_missing;
907 uirow = MSI_CreateRecord( 9 );
908 MSI_RecordSetStringW( uirow, 1, file->FileName );
909 uipath = strdupW( file->TargetPath );
910 p = strrchrW(uipath,'\\');
913 MSI_RecordSetStringW( uirow, 9, uipath);
914 ui_actiondata( package, szRemoveFiles, uirow);
915 msiobj_release( &uirow->hdr );
917 /* FIXME: call ui_progress here? */
920 return ERROR_SUCCESS;