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];
71 * This is a helper function for handling embedded cabinet media
73 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
83 rc = read_raw_stream_data(package->db,stream_name,&data,&size);
84 if (rc != ERROR_SUCCESS)
88 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
89 GetTempPathW(MAX_PATH,tmp);
91 GetTempFileNameW(tmp,stream_name,0,source);
93 track_tempfile(package,strrchrW(source,'\\'), source);
94 the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
95 FILE_ATTRIBUTE_NORMAL, NULL);
97 if (the_file == INVALID_HANDLE_VALUE)
99 ERR("Unable to create file %s\n",debugstr_w(source));
100 rc = ERROR_FUNCTION_FAILED;
104 WriteFile(the_file,data,size,&write,NULL);
105 CloseHandle(the_file);
106 TRACE("wrote %i bytes to %s\n",write,debugstr_w(source));
113 /* Support functions for FDI functions */
117 struct media_info *mi;
120 static void * cabinet_alloc(ULONG cb)
122 return msi_alloc(cb);
125 static void cabinet_free(void *pv)
130 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
134 DWORD dwShareMode = 0;
135 DWORD dwCreateDisposition = OPEN_EXISTING;
136 switch (oflag & _O_ACCMODE)
139 dwAccess = GENERIC_READ;
140 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
143 dwAccess = GENERIC_WRITE;
144 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
147 dwAccess = GENERIC_READ | GENERIC_WRITE;
148 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
151 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
152 dwCreateDisposition = CREATE_NEW;
153 else if (oflag & _O_CREAT)
154 dwCreateDisposition = CREATE_ALWAYS;
155 handle = CreateFileA( pszFile, dwAccess, dwShareMode, NULL,
156 dwCreateDisposition, 0, NULL );
157 if (handle == INVALID_HANDLE_VALUE)
159 return (INT_PTR) handle;
162 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
164 HANDLE handle = (HANDLE) hf;
166 if (ReadFile(handle, pv, cb, &dwRead, NULL))
171 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
173 HANDLE handle = (HANDLE) hf;
175 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
180 static int cabinet_close(INT_PTR hf)
182 HANDLE handle = (HANDLE) hf;
183 return CloseHandle(handle) ? 0 : -1;
186 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
188 HANDLE handle = (HANDLE) hf;
189 /* flags are compatible and so are passed straight through */
190 return SetFilePointer(handle, dist, NULL, seektype);
193 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
199 uirow = MSI_CreateRecord( 9 );
200 MSI_RecordSetStringW( uirow, 1, f->FileName );
201 uipath = strdupW( f->TargetPath );
202 p = strrchrW(uipath,'\\');
205 MSI_RecordSetStringW( uirow, 9, uipath);
206 MSI_RecordSetInteger( uirow, 6, f->FileSize );
207 ui_actiondata( package, action, uirow);
208 msiobj_release( &uirow->hdr );
210 ui_progress( package, 2, f->FileSize, 0, 0);
213 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
219 CabData *data = (CabData*) pfdin->pv;
225 file = strdupAtoW(pfdin->psz1);
226 f = get_loaded_file(data->package, file);
231 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
235 if (f->state != msifs_missing && f->state != msifs_overwrite)
237 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
241 msi_file_update_ui( data->package, f, szInstallFiles );
243 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
245 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
246 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
248 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
249 NULL, CREATE_ALWAYS, attrs, NULL );
250 if ( handle == INVALID_HANDLE_VALUE )
252 ERR("failed to create %s (error %d)\n",
253 debugstr_w( f->TargetPath ), GetLastError() );
257 f->state = msifs_installed;
258 return (INT_PTR) handle;
260 case fdintCLOSE_FILE_INFO:
264 HANDLE handle = (HANDLE) pfdin->hf;
266 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
268 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
270 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
280 /***********************************************************************
281 * extract_cabinet_file
283 * Extract files from a cab file.
285 static BOOL extract_cabinet_file(MSIPACKAGE* package, struct media_info *mi)
287 LPSTR cabinet, cab_path = NULL;
294 TRACE("Extracting %s\n", debugstr_w(mi->source));
296 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
297 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
300 ERR("FDICreate failed\n");
304 ptr = strrchrW(mi->source, '\\') + 1;
305 cabinet = strdupWtoA(ptr);
309 cab_path = strdupWtoA(mi->source);
313 cab_path[ptr - mi->source] = '\0';
315 data.package = package;
318 ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, &data);
320 ERR("FDICopy failed\n");
330 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, LPCWSTR path)
332 if (!file->IsCompressed)
335 p = resolve_folder(package, file->Component->Directory, TRUE, FALSE, NULL);
336 path = build_directory_name(2, p, file->ShortName);
337 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
340 path = build_directory_name(2, p, file->LongName);
342 file->SourcePath = path;
346 file->SourcePath = build_directory_name(2, path, file->File);
349 static void free_media_info( struct media_info *mi )
351 msi_free( mi->disk_prompt );
352 msi_free( mi->cabinet );
353 msi_free( mi->volume_label );
357 static UINT msi_change_media( MSIPACKAGE *package, struct media_info *mi )
359 LPWSTR error, error_dialog;
360 UINT r = ERROR_SUCCESS;
362 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
363 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
365 if ( msi_get_property_int(package, szUILevel, 0) == INSTALLUILEVEL_NONE )
366 return ERROR_SUCCESS;
368 error = generate_error_string( package, 1302, 1, mi->disk_prompt );
369 error_dialog = msi_dup_property( package, error_prop );
371 while ( r == ERROR_SUCCESS && GetFileAttributesW( mi->source ) == INVALID_FILE_ATTRIBUTES )
372 r = msi_spawn_error_dialog( package, error_dialog, error );
375 msi_free( error_dialog );
380 static UINT download_remote_cabinet(MSIPACKAGE *package, struct media_info *mi)
382 WCHAR temppath[MAX_PATH];
386 src = strdupW(package->BaseURL);
388 return ERROR_OUTOFMEMORY;
390 ptr = strrchrW(src, '/');
394 return ERROR_FUNCTION_FAILED;
398 ptr = strrchrW(mi->source, '\\');
399 lstrcatW(src, ptr + 1);
401 cab = msi_download_file(src, temppath);
402 lstrcpyW(mi->source, cab);
405 return ERROR_SUCCESS;
408 static UINT ready_media_for_file( MSIPACKAGE *package, struct media_info *mi,
411 UINT rc = ERROR_SUCCESS;
413 static const WCHAR ExecSeqQuery[] =
414 {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
415 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
416 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
417 ' ','%', 'i',' ','O','R','D','E','R',' ','B','Y',' ',
418 '`','D','i','s','k','I','d','`',0};
424 if (file->Sequence <= mi->last_sequence)
426 set_file_source(package, file, mi->source);
427 TRACE("Media already ready (%u, %u)\n",file->Sequence,mi->last_sequence);
428 return ERROR_SUCCESS;
431 row = MSI_QueryGetRecord(package->db, ExecSeqQuery, file->Sequence);
434 TRACE("Unable to query row\n");
435 return ERROR_FUNCTION_FAILED;
438 volume = MSI_RecordGetString(row, 5);
439 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
441 source_dir = msi_dup_property(package, cszSourceDir);
443 if (!file->IsCompressed)
445 lstrcpyW(mi->source, source_dir);
446 set_file_source(package, file, mi->source);
448 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
449 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->disk_id, volume,
452 MsiSourceListSetInfoW(package->ProductCode, NULL,
453 MSIINSTALLCONTEXT_USERMANAGED,
454 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
455 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->source);
456 msiobj_release(&row->hdr);
460 seq = MSI_RecordGetInteger(row,2);
461 mi->last_sequence = seq;
463 cab = MSI_RecordGetString(row,4);
466 TRACE("Source is CAB %s\n",debugstr_w(cab));
467 /* the stream does not contain the # character */
472 rc = writeout_cabinet_stream(package,&cab[1],mi->source);
473 if (rc != ERROR_SUCCESS)
476 path = msi_dup_property( package, cszSourceDir );
478 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
479 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->disk_id,
480 volume, mi->disk_prompt);
482 MsiSourceListSetInfoW(package->ProductCode, NULL,
483 MSIINSTALLCONTEXT_USERMANAGED,
484 MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
485 INSTALLPROPERTY_LASTUSEDSOURCEW, path);
492 if (MSI_GetPropertyW(package, cszSourceDir, mi->source, &sz))
494 ERR("No Source dir defined\n");
495 rc = ERROR_FUNCTION_FAILED;
499 strcatW(mi->source,cab);
501 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
502 rc = msi_change_media(package, mi);
504 if ( rc != ERROR_SUCCESS )
507 MsiSourceListSetInfoW(package->ProductCode, NULL,
508 MSIINSTALLCONTEXT_USERMANAGED,
509 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
510 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->source);
514 /* only download the remote cabinet file if a local copy does not exist */
515 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
516 UrlIsW(package->BaseURL, URLIS_URL))
518 rc = download_remote_cabinet(package, mi);
519 if (rc != ERROR_SUCCESS ||
520 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
526 rc = !extract_cabinet_file(package, mi);
531 MSI_GetPropertyW(package,cszSourceDir,mi->source,&sz);
533 MsiSourceListSetInfoW(package->ProductCode, NULL,
534 MSIINSTALLCONTEXT_USERMANAGED,
535 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
536 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->source);
538 set_file_source(package, file, mi->source);
540 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
541 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->disk_id, volume,
545 msi_free(source_dir);
546 msiobj_release(&row->hdr);
551 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
554 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
556 if (lstrcmpW( file_key, (*file)->File )==0)
558 if ((*file)->state >= msifs_overwrite)
559 return ERROR_SUCCESS;
561 return ERROR_FILE_NOT_FOUND;
565 return ERROR_FUNCTION_FAILED;
568 static void schedule_install_files(MSIPACKAGE *package)
572 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
574 if (!ACTION_VerifyComponentForAction(file->Component, INSTALLSTATE_LOCAL))
576 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
578 ui_progress(package,2,file->FileSize,0,0);
579 file->state = msifs_skipped;
584 static UINT copy_install_file(MSIFILE *file)
589 TRACE("Copying %s to %s\n", debugstr_w(file->SourcePath),
590 debugstr_w(file->TargetPath));
592 ret = CopyFileW(file->SourcePath, file->TargetPath, FALSE);
595 file->state = msifs_installed;
596 return ERROR_SUCCESS;
599 gle = GetLastError();
600 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
602 TRACE("overwriting existing file\n");
605 else if (gle == ERROR_FILE_NOT_FOUND)
607 /* FIXME: this needs to be tested, I'm pretty sure it fails */
608 TRACE("Source file not found\n");
611 else if (!(file->Attributes & msidbFileAttributesVital))
613 TRACE("Ignoring error for nonvital\n");
621 * ACTION_InstallFiles()
623 * For efficiency, this is done in two passes:
624 * 1) Correct all the TargetPaths and determine what files are to be installed.
625 * 2) Extract Cabinets and copy files.
627 UINT ACTION_InstallFiles(MSIPACKAGE *package)
629 struct media_info *mi;
630 UINT rc = ERROR_SUCCESS;
634 /* increment progress bar each time action data is sent */
635 ui_progress(package,1,1,0,0);
637 /* handle the keys for the SourceList */
638 ptr = strrchrW(package->PackagePath,'\\');
642 MsiSourceListSetInfoW(package->ProductCode, NULL,
643 MSIINSTALLCONTEXT_USERMANAGED,
645 INSTALLPROPERTY_PACKAGENAMEW, ptr);
648 schedule_install_files(package);
651 * Despite MSDN specifying that the CreateFolders action
652 * should be called before InstallFiles, some installers don't
653 * do that, and they seem to work correctly. We need to create
654 * directories here to make sure that the files can be copied.
656 msi_create_component_directories( package );
658 mi = msi_alloc_zero( sizeof(struct media_info) );
661 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
663 if (file->state != msifs_missing && file->state != msifs_overwrite)
666 TRACE("Pass 2: %s\n",debugstr_w(file->File));
668 rc = ready_media_for_file( package, mi, file );
669 if (rc != ERROR_SUCCESS)
671 ERR("Unable to ready media\n");
672 rc = ERROR_FUNCTION_FAILED;
676 TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
677 debugstr_w(file->TargetPath));
679 if (file->state != msifs_missing && file->state != msifs_overwrite)
682 /* compressed files are extracted in ready_media_for_file */
683 if (file->IsCompressed)
685 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(file->TargetPath))
687 ERR("compressed file wasn't extracted (%s)\n",
688 debugstr_w(file->TargetPath));
689 rc = ERROR_INSTALL_FAILURE;
696 rc = copy_install_file(file);
697 if (rc != ERROR_SUCCESS)
699 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(file->SourcePath),
700 debugstr_w(file->TargetPath), rc);
701 rc = ERROR_INSTALL_FAILURE;
707 free_media_info( mi );
711 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
713 MSIPACKAGE *package = (MSIPACKAGE*)param;
714 WCHAR dest_name[0x100];
715 LPWSTR dest_path, dest;
716 LPCWSTR file_key, component;
722 component = MSI_RecordGetString(row,2);
723 comp = get_loaded_component(package,component);
725 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
727 TRACE("Skipping copy due to disabled component %s\n",
728 debugstr_w(component));
730 /* the action taken was the same as the current install state */
731 comp->Action = comp->Installed;
733 return ERROR_SUCCESS;
736 comp->Action = INSTALLSTATE_LOCAL;
738 file_key = MSI_RecordGetString(row,3);
741 ERR("Unable to get file key\n");
742 return ERROR_FUNCTION_FAILED;
745 rc = get_file_target(package,file_key,&file);
747 if (rc != ERROR_SUCCESS)
749 ERR("Original file unknown %s\n",debugstr_w(file_key));
750 return ERROR_SUCCESS;
753 if (MSI_RecordIsNull(row,4))
754 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
758 MSI_RecordGetStringW(row,4,dest_name,&sz);
759 reduce_to_longfilename(dest_name);
762 if (MSI_RecordIsNull(row,5))
765 dest_path = strdupW(file->TargetPath);
766 p = strrchrW(dest_path,'\\');
773 destkey = MSI_RecordGetString(row,5);
774 dest_path = resolve_folder(package, destkey, FALSE,FALSE,NULL);
778 dest_path = msi_dup_property( package, destkey );
781 FIXME("Unable to get destination folder, try AppSearch properties\n");
782 return ERROR_SUCCESS;
787 dest = build_directory_name(2, dest_path, dest_name);
789 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
792 if (strcmpW(file->TargetPath,dest))
793 rc = !CopyFileW(file->TargetPath,dest,TRUE);
797 if (rc != ERROR_SUCCESS)
798 ERR("Failed to copy file %s -> %s, last error %d\n",
799 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
801 FIXME("We should track these duplicate files as well\n");
806 msi_file_update_ui(package, file, szDuplicateFiles);
808 return ERROR_SUCCESS;
811 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
815 static const WCHAR ExecSeqQuery[] =
816 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
817 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
819 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
820 if (rc != ERROR_SUCCESS)
821 return ERROR_SUCCESS;
823 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
824 msiobj_release(&view->hdr);
829 /* compares the version of a file read from the filesystem and
830 * the version specified in the File table
832 static int msi_compare_file_version( MSIFILE *file )
834 WCHAR version[MAX_PATH];
840 r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
841 if ( r != ERROR_SUCCESS )
844 return lstrcmpW( version, file->Version );
847 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
851 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
856 if ( !file->Component )
858 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
861 if ( file->state == msifs_installed )
862 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
864 if ( file->state != msifs_present )
867 /* only remove a file if the version to be installed
868 * is strictly newer than the old file
870 if ( msi_compare_file_version( file ) >= 0 )
873 TRACE("removing %s\n", debugstr_w(file->File) );
874 if ( !DeleteFileW( file->TargetPath ) )
875 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
876 file->state = msifs_missing;
879 uirow = MSI_CreateRecord( 9 );
880 MSI_RecordSetStringW( uirow, 1, file->FileName );
881 uipath = strdupW( file->TargetPath );
882 p = strrchrW(uipath,'\\');
885 MSI_RecordSetStringW( uirow, 9, uipath);
886 ui_actiondata( package, szRemoveFiles, uirow);
887 msiobj_release( &uirow->hdr );
889 /* FIXME: call ui_progress here? */
892 return ERROR_SUCCESS;