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};
61 * This is a helper function for handling embedded cabinet media
63 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
73 rc = read_raw_stream_data(package->db,stream_name,&data,&size);
74 if (rc != ERROR_SUCCESS)
78 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
79 GetTempPathW(MAX_PATH,tmp);
81 GetTempFileNameW(tmp,stream_name,0,source);
83 track_tempfile(package,strrchrW(source,'\\'), source);
84 the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
85 FILE_ATTRIBUTE_NORMAL, NULL);
87 if (the_file == INVALID_HANDLE_VALUE)
89 ERR("Unable to create file %s\n",debugstr_w(source));
90 rc = ERROR_FUNCTION_FAILED;
94 WriteFile(the_file,data,size,&write,NULL);
95 CloseHandle(the_file);
96 TRACE("wrote %i bytes to %s\n",write,debugstr_w(source));
103 /* Support functions for FDI functions */
110 static void * cabinet_alloc(ULONG cb)
112 return msi_alloc(cb);
115 static void cabinet_free(void *pv)
120 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
124 DWORD dwShareMode = 0;
125 DWORD dwCreateDisposition = OPEN_EXISTING;
126 switch (oflag & _O_ACCMODE)
129 dwAccess = GENERIC_READ;
130 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
133 dwAccess = GENERIC_WRITE;
134 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
137 dwAccess = GENERIC_READ | GENERIC_WRITE;
138 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
141 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
142 dwCreateDisposition = CREATE_NEW;
143 else if (oflag & _O_CREAT)
144 dwCreateDisposition = CREATE_ALWAYS;
145 handle = CreateFileA( pszFile, dwAccess, dwShareMode, NULL,
146 dwCreateDisposition, 0, NULL );
147 if (handle == INVALID_HANDLE_VALUE)
149 return (INT_PTR) handle;
152 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
154 HANDLE handle = (HANDLE) hf;
156 if (ReadFile(handle, pv, cb, &dwRead, NULL))
161 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
163 HANDLE handle = (HANDLE) hf;
165 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
170 static int cabinet_close(INT_PTR hf)
172 HANDLE handle = (HANDLE) hf;
173 return CloseHandle(handle) ? 0 : -1;
176 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
178 HANDLE handle = (HANDLE) hf;
179 /* flags are compatible and so are passed straight through */
180 return SetFilePointer(handle, dist, NULL, seektype);
183 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
189 uirow = MSI_CreateRecord( 9 );
190 MSI_RecordSetStringW( uirow, 1, f->FileName );
191 uipath = strdupW( f->TargetPath );
192 p = strrchrW(uipath,'\\');
195 MSI_RecordSetStringW( uirow, 9, uipath);
196 MSI_RecordSetInteger( uirow, 6, f->FileSize );
197 ui_actiondata( package, action, uirow);
198 msiobj_release( &uirow->hdr );
200 ui_progress( package, 2, f->FileSize, 0, 0);
203 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
209 CabData *data = (CabData*) pfdin->pv;
215 file = strdupAtoW(pfdin->psz1);
216 f = get_loaded_file(data->package, file);
221 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
225 if (f->state != msifs_missing && f->state != msifs_overwrite)
227 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
231 msi_file_update_ui( data->package, f, szInstallFiles );
233 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
235 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
236 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
238 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
239 NULL, CREATE_ALWAYS, attrs, NULL );
240 if ( handle == INVALID_HANDLE_VALUE )
242 ERR("failed to create %s (error %d)\n",
243 debugstr_w( f->TargetPath ), GetLastError() );
247 f->state = msifs_installed;
248 return (INT_PTR) handle;
250 case fdintCLOSE_FILE_INFO:
254 HANDLE handle = (HANDLE) pfdin->hf;
256 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
258 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
260 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
270 /***********************************************************************
271 * extract_cabinet_file
273 * Extract files from a cab file.
275 static BOOL extract_cabinet_file(MSIPACKAGE* package, LPCWSTR source,
283 static CHAR empty[] = "";
286 TRACE("Extracting %s to %s\n",debugstr_w(source), debugstr_w(path));
288 hfdi = FDICreate(cabinet_alloc,
299 ERR("FDICreate failed\n");
303 if (!(cabinet = strdupWtoA( source )))
308 if (!(cab_path = strdupWtoA( path )))
315 data.package = package;
316 data.cab_path = cab_path;
318 ret = FDICopy(hfdi, cabinet, empty, 0, cabinet_notify, NULL, &data);
321 ERR("FDICopy failed\n");
331 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, MSICOMPONENT*
334 if (!file->IsCompressed)
337 p = resolve_folder(package, comp->Directory, TRUE, FALSE, NULL);
338 path = build_directory_name(2, p, file->ShortName);
339 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
342 path = build_directory_name(2, p, file->LongName);
344 file->SourcePath = path;
348 file->SourcePath = build_directory_name(2, path, file->File);
356 WCHAR source[MAX_PATH];
359 static struct media_info *create_media_info( void )
361 struct media_info *mi;
363 mi = msi_alloc( sizeof *mi );
366 mi->last_sequence = 0;
367 mi->last_volume = NULL;
368 mi->last_path = NULL;
376 static void free_media_info( struct media_info *mi )
378 msi_free( mi->last_path );
382 /* downloads a remote cabinet and extracts it if it exists */
383 static UINT msi_extract_remote_cabinet( MSIPACKAGE *package, struct media_info *mi )
385 FDICABINETINFO cabinfo;
386 WCHAR temppath[MAX_PATH];
395 /* the URL is the path prefix of the package URL and the filename
396 * of the file to download
398 ptr = strrchrW(package->PackagePath, '/');
399 lstrcpynW(src, package->PackagePath, ptr - package->PackagePath + 2);
400 ptr = strrchrW(mi->source, '\\');
401 lstrcatW(src, ptr + 1);
403 file = msi_download_file( src, temppath );
404 lstrcpyW(mi->source, file);
406 /* check if the remote cabinet still exists, ignore if it doesn't */
407 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
408 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
411 ERR("FDICreate failed\n");
412 return ERROR_FUNCTION_FAILED;
415 cabpath = strdupWtoA(mi->source);
416 hf = cabinet_open(cabpath, _O_RDONLY, 0);
417 if (!FDIIsCabinet(hfdi, hf, &cabinfo))
419 WARN("Remote cabinet %s does not exist.\n", debugstr_w(mi->source));
421 return ERROR_SUCCESS;
425 return !extract_cabinet_file(package, mi->source, mi->last_path);
428 static UINT msi_change_media( MSIPACKAGE *package, struct media_info *mi, LPCWSTR prompt )
430 LPWSTR error, error_dialog;
431 UINT r = ERROR_SUCCESS;
433 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
434 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
436 if ( msi_get_property_int(package, szUILevel, 0) == INSTALLUILEVEL_NONE )
437 return ERROR_SUCCESS;
439 error = generate_error_string( package, 1302, 1, prompt );
440 error_dialog = msi_dup_property( package, error_prop );
442 while ( r == ERROR_SUCCESS && GetFileAttributesW( mi->source ) == INVALID_FILE_ATTRIBUTES )
443 r = msi_spawn_error_dialog( package, error_dialog, error );
446 msi_free( error_dialog );
451 static UINT ready_media_for_file( MSIPACKAGE *package, struct media_info *mi,
454 UINT rc = ERROR_SUCCESS;
456 static const WCHAR ExecSeqQuery[] =
457 {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
458 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
459 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
460 ' ','%', 'i',' ','O','R','D','E','R',' ','B','Y',' ',
461 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',0};
466 MSICOMPONENT *comp = file->Component;
468 if (file->Sequence <= mi->last_sequence)
470 set_file_source(package,file,comp,mi->last_path);
471 TRACE("Media already ready (%u, %u)\n",file->Sequence,mi->last_sequence);
472 return ERROR_SUCCESS;
476 row = MSI_QueryGetRecord(package->db, ExecSeqQuery, file->Sequence);
479 TRACE("Unable to query row\n");
480 return ERROR_FUNCTION_FAILED;
483 volume = MSI_RecordGetString(row, 5);
484 prompt = MSI_RecordGetString(row, 3);
486 msi_free(mi->last_path);
487 mi->last_path = NULL;
489 if (!file->IsCompressed)
491 mi->last_path = resolve_folder(package, comp->Directory, TRUE, FALSE, NULL);
492 set_file_source(package,file,comp,mi->last_path);
494 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
495 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count, volume,
498 MsiSourceListSetInfoW(package->ProductCode, NULL,
499 MSIINSTALLCONTEXT_USERMANAGED,
500 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
501 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
502 msiobj_release(&row->hdr);
506 seq = MSI_RecordGetInteger(row,2);
507 mi->last_sequence = seq;
509 cab = MSI_RecordGetString(row,4);
512 TRACE("Source is CAB %s\n",debugstr_w(cab));
513 /* the stream does not contain the # character */
518 rc = writeout_cabinet_stream(package,&cab[1],mi->source);
519 if (rc != ERROR_SUCCESS)
522 mi->last_path = strdupW(mi->source);
523 p = strrchrW(mi->last_path,'\\');
527 path = msi_dup_property( package, cszSourceDir );
529 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
530 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count,
533 MsiSourceListSetInfoW(package->ProductCode, NULL,
534 MSIINSTALLCONTEXT_USERMANAGED,
535 MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
536 INSTALLPROPERTY_LASTUSEDSOURCEW, path);
543 mi->last_path = msi_alloc(MAX_PATH*sizeof(WCHAR));
544 if (MSI_GetPropertyW(package, cszSourceDir, mi->source, &sz))
546 ERR("No Source dir defined\n");
547 rc = ERROR_FUNCTION_FAILED;
551 strcpyW(mi->last_path,mi->source);
552 strcatW(mi->source,cab);
554 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
555 rc = msi_change_media(package, mi, prompt);
557 if ( rc != ERROR_SUCCESS )
560 MsiSourceListSetInfoW(package->ProductCode, NULL,
561 MSIINSTALLCONTEXT_USERMANAGED,
562 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
563 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
565 /* extract the cab file into a folder in the temp folder */
567 if (MSI_GetPropertyW(package, cszTempFolder,mi->last_path, &sz)
569 GetTempPathW(MAX_PATH,mi->last_path);
573 /* only download the remote cabinet file if a local copy does not exist */
574 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
575 UrlIsW(package->PackagePath, URLIS_URL))
577 rc = msi_extract_remote_cabinet(package, mi);
581 rc = !extract_cabinet_file(package, mi->source, mi->last_path);
587 mi->last_path = msi_alloc(MAX_PATH*sizeof(WCHAR));
588 MSI_GetPropertyW(package,cszSourceDir,mi->source,&sz);
589 strcpyW(mi->last_path,mi->source);
591 MsiSourceListSetInfoW(package->ProductCode, NULL,
592 MSIINSTALLCONTEXT_USERMANAGED,
593 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
594 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
596 set_file_source(package, file, comp, mi->last_path);
598 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
599 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count, volume,
603 msiobj_release(&row->hdr);
608 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
611 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
613 if (lstrcmpW( file_key, (*file)->File )==0)
615 if ((*file)->state >= msifs_overwrite)
616 return ERROR_SUCCESS;
618 return ERROR_FILE_NOT_FOUND;
622 return ERROR_FUNCTION_FAILED;
626 * ACTION_InstallFiles()
628 * For efficiency, this is done in two passes:
629 * 1) Correct all the TargetPaths and determine what files are to be installed.
630 * 2) Extract Cabinets and copy files.
632 UINT ACTION_InstallFiles(MSIPACKAGE *package)
634 struct media_info *mi;
635 UINT rc = ERROR_SUCCESS;
639 /* increment progress bar each time action data is sent */
640 ui_progress(package,1,1,0,0);
642 /* handle the keys for the SourceList */
643 ptr = strrchrW(package->PackagePath,'\\');
647 MsiSourceListSetInfoW(package->ProductCode, NULL,
648 MSIINSTALLCONTEXT_USERMANAGED,
650 INSTALLPROPERTY_PACKAGENAMEW, ptr);
652 /* FIXME("Write DiskPrompt\n"); */
655 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
657 if (!ACTION_VerifyComponentForAction( file->Component, INSTALLSTATE_LOCAL ))
659 ui_progress(package,2,file->FileSize,0,0);
660 TRACE("File %s is not scheduled for install\n",
661 debugstr_w(file->File));
663 file->state = msifs_skipped;
668 * Despite MSDN specifying that the CreateFolders action
669 * should be called before InstallFiles, some installers don't
670 * do that, and they seem to work correctly. We need to create
671 * directories here to make sure that the files can be copied.
673 msi_create_component_directories( package );
675 mi = create_media_info();
678 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
680 if (file->state != msifs_missing && file->state != msifs_overwrite)
683 TRACE("Pass 2: %s\n",debugstr_w(file->File));
685 rc = ready_media_for_file( package, mi, file );
686 if (rc != ERROR_SUCCESS)
688 ERR("Unable to ready media\n");
689 rc = ERROR_FUNCTION_FAILED;
693 TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
694 debugstr_w(file->TargetPath));
696 if (file->state != msifs_missing && file->state != msifs_overwrite)
699 /* compressed files are extracted in ready_media_for_file */
700 if (file->IsCompressed)
702 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(file->TargetPath))
703 ERR("compressed file wasn't extracted (%s)\n",
704 debugstr_w(file->TargetPath));
708 rc = CopyFileW(file->SourcePath,file->TargetPath,FALSE);
712 ERR("Unable to copy file (%s -> %s) (error %d)\n",
713 debugstr_w(file->SourcePath), debugstr_w(file->TargetPath), rc);
714 if (rc == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
718 else if (rc == ERROR_FILE_NOT_FOUND)
720 ERR("Source File Not Found! Continuing\n");
723 else if (file->Attributes & msidbFileAttributesVital)
725 ERR("Ignoring Error and continuing (nonvital file)...\n");
731 file->state = msifs_installed;
737 free_media_info( mi );
741 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
743 MSIPACKAGE *package = (MSIPACKAGE*)param;
744 WCHAR dest_name[0x100];
745 LPWSTR dest_path, dest;
746 LPCWSTR file_key, component;
752 component = MSI_RecordGetString(row,2);
753 comp = get_loaded_component(package,component);
755 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
757 TRACE("Skipping copy due to disabled component %s\n",
758 debugstr_w(component));
760 /* the action taken was the same as the current install state */
761 comp->Action = comp->Installed;
763 return ERROR_SUCCESS;
766 comp->Action = INSTALLSTATE_LOCAL;
768 file_key = MSI_RecordGetString(row,3);
771 ERR("Unable to get file key\n");
772 return ERROR_FUNCTION_FAILED;
775 rc = get_file_target(package,file_key,&file);
777 if (rc != ERROR_SUCCESS)
779 ERR("Original file unknown %s\n",debugstr_w(file_key));
780 return ERROR_SUCCESS;
783 if (MSI_RecordIsNull(row,4))
784 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
788 MSI_RecordGetStringW(row,4,dest_name,&sz);
789 reduce_to_longfilename(dest_name);
792 if (MSI_RecordIsNull(row,5))
795 dest_path = strdupW(file->TargetPath);
796 p = strrchrW(dest_path,'\\');
803 destkey = MSI_RecordGetString(row,5);
804 dest_path = resolve_folder(package, destkey, FALSE,FALSE,NULL);
808 dest_path = msi_dup_property( package, destkey );
811 FIXME("Unable to get destination folder, try AppSearch properties\n");
812 return ERROR_SUCCESS;
817 dest = build_directory_name(2, dest_path, dest_name);
819 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
822 if (strcmpW(file->TargetPath,dest))
823 rc = !CopyFileW(file->TargetPath,dest,TRUE);
827 if (rc != ERROR_SUCCESS)
828 ERR("Failed to copy file %s -> %s, last error %d\n",
829 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
831 FIXME("We should track these duplicate files as well\n");
836 msi_file_update_ui(package, file, szDuplicateFiles);
838 return ERROR_SUCCESS;
841 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
845 static const WCHAR ExecSeqQuery[] =
846 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
847 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
849 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
850 if (rc != ERROR_SUCCESS)
851 return ERROR_SUCCESS;
853 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
854 msiobj_release(&view->hdr);
859 /* compares the version of a file read from the filesystem and
860 * the version specified in the File table
862 static int msi_compare_file_version( MSIFILE *file )
864 WCHAR version[MAX_PATH];
870 r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
871 if ( r != ERROR_SUCCESS )
874 return lstrcmpW( version, file->Version );
877 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
881 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
886 if ( !file->Component )
888 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
891 if ( file->state == msifs_installed )
892 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
894 if ( file->state != msifs_present )
897 /* only remove a file if the version to be installed
898 * is strictly newer than the old file
900 if ( msi_compare_file_version( file ) >= 0 )
903 TRACE("removing %s\n", debugstr_w(file->File) );
904 if ( !DeleteFileW( file->TargetPath ) )
905 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
906 file->state = msifs_missing;
909 uirow = MSI_CreateRecord( 9 );
910 MSI_RecordSetStringW( uirow, 1, file->FileName );
911 uipath = strdupW( file->TargetPath );
912 p = strrchrW(uipath,'\\');
915 MSI_RecordSetStringW( uirow, 9, uipath);
916 ui_actiondata( package, szRemoveFiles, uirow);
917 msiobj_release( &uirow->hdr );
919 /* FIXME: call ui_progress here? */
922 return ERROR_SUCCESS;