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"
50 WINE_DEFAULT_DEBUG_CHANNEL(msi);
52 extern const WCHAR szInstallFiles[];
53 extern const WCHAR szDuplicateFiles[];
54 extern const WCHAR szMoveFiles[];
55 extern const WCHAR szPatchFiles[];
56 extern const WCHAR szRemoveDuplicateFiles[];
57 extern const WCHAR szRemoveFiles[];
59 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
61 extern LPCWSTR msi_download_file( LPCWSTR szUrl, LPWSTR filename );
64 * This is a helper function for handling embedded cabinet media
66 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
76 rc = read_raw_stream_data(package->db,stream_name,&data,&size);
77 if (rc != ERROR_SUCCESS)
81 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
82 GetTempPathW(MAX_PATH,tmp);
84 GetTempFileNameW(tmp,stream_name,0,source);
86 track_tempfile(package,strrchrW(source,'\\'), source);
87 the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
88 FILE_ATTRIBUTE_NORMAL, NULL);
90 if (the_file == INVALID_HANDLE_VALUE)
92 ERR("Unable to create file %s\n",debugstr_w(source));
93 rc = ERROR_FUNCTION_FAILED;
97 WriteFile(the_file,data,size,&write,NULL);
98 CloseHandle(the_file);
99 TRACE("wrote %li bytes to %s\n",write,debugstr_w(source));
106 /* Support functions for FDI functions */
113 static void * cabinet_alloc(ULONG cb)
115 return msi_alloc(cb);
118 static void cabinet_free(void *pv)
123 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
127 DWORD dwShareMode = 0;
128 DWORD dwCreateDisposition = OPEN_EXISTING;
129 switch (oflag & _O_ACCMODE)
132 dwAccess = GENERIC_READ;
133 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
136 dwAccess = GENERIC_WRITE;
137 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
140 dwAccess = GENERIC_READ | GENERIC_WRITE;
141 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
144 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
145 dwCreateDisposition = CREATE_NEW;
146 else if (oflag & _O_CREAT)
147 dwCreateDisposition = CREATE_ALWAYS;
148 handle = CreateFileA( pszFile, dwAccess, dwShareMode, NULL,
149 dwCreateDisposition, 0, NULL );
150 if (handle == INVALID_HANDLE_VALUE)
152 return (INT_PTR) handle;
155 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
157 HANDLE handle = (HANDLE) hf;
159 if (ReadFile(handle, pv, cb, &dwRead, NULL))
164 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
166 HANDLE handle = (HANDLE) hf;
168 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
173 static int cabinet_close(INT_PTR hf)
175 HANDLE handle = (HANDLE) hf;
176 return CloseHandle(handle) ? 0 : -1;
179 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
181 HANDLE handle = (HANDLE) hf;
182 /* flags are compatible and so are passed straight through */
183 return SetFilePointer(handle, dist, NULL, seektype);
186 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
192 uirow = MSI_CreateRecord( 9 );
193 MSI_RecordSetStringW( uirow, 1, f->FileName );
194 uipath = strdupW( f->TargetPath );
195 p = strrchrW(uipath,'\\');
198 MSI_RecordSetStringW( uirow, 9, uipath);
199 MSI_RecordSetInteger( uirow, 6, f->FileSize );
200 ui_actiondata( package, action, uirow);
201 msiobj_release( &uirow->hdr );
203 ui_progress( package, 2, f->FileSize, 0, 0);
206 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
212 CabData *data = (CabData*) pfdin->pv;
218 file = strdupAtoW(pfdin->psz1);
219 f = get_loaded_file(data->package, file);
224 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
228 if (f->state != msifs_missing && f->state != msifs_overwrite)
230 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
234 msi_file_update_ui( data->package, f, szInstallFiles );
236 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
238 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
239 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
241 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
242 NULL, CREATE_ALWAYS, attrs, NULL );
243 if ( handle == INVALID_HANDLE_VALUE )
245 ERR("failed to create %s (error %ld)\n",
246 debugstr_w( f->TargetPath ), GetLastError() );
250 f->state = msifs_installed;
251 return (INT_PTR) handle;
253 case fdintCLOSE_FILE_INFO:
257 HANDLE handle = (HANDLE) pfdin->hf;
259 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
261 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
263 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
273 /***********************************************************************
274 * extract_cabinet_file
276 * Extract files from a cab file.
278 static BOOL extract_cabinet_file(MSIPACKAGE* package, LPCWSTR source,
286 static CHAR empty[] = "";
289 TRACE("Extracting %s to %s\n",debugstr_w(source), debugstr_w(path));
291 hfdi = FDICreate(cabinet_alloc,
302 ERR("FDICreate failed\n");
306 if (!(cabinet = strdupWtoA( source )))
311 if (!(cab_path = strdupWtoA( path )))
318 data.package = package;
319 data.cab_path = cab_path;
321 ret = FDICopy(hfdi, cabinet, empty, 0, cabinet_notify, NULL, &data);
324 ERR("FDICopy failed\n");
334 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, MSICOMPONENT*
337 if (!file->IsCompressed)
340 p = resolve_folder(package, comp->Directory, TRUE, FALSE, NULL);
341 path = build_directory_name(2, p, file->ShortName);
342 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
345 path = build_directory_name(2, p, file->LongName);
347 file->SourcePath = path;
351 file->SourcePath = build_directory_name(2, path, file->File);
359 WCHAR source[MAX_PATH];
362 static struct media_info *create_media_info( void )
364 struct media_info *mi;
366 mi = msi_alloc( sizeof *mi );
369 mi->last_sequence = 0;
370 mi->last_volume = NULL;
371 mi->last_path = NULL;
379 static void free_media_info( struct media_info *mi )
381 msi_free( mi->last_path );
385 /* downloads a remote cabinet and extracts it if it exists */
386 static UINT msi_extract_remote_cabinet( MSIPACKAGE *package, struct media_info *mi )
388 FDICABINETINFO cabinfo;
389 WCHAR temppath[MAX_PATH];
398 /* the URL is the path prefix of the package URL and the filename
399 * of the file to download
401 ptr = strrchrW(package->PackagePath, '/');
402 lstrcpynW(src, package->PackagePath, ptr - package->PackagePath + 2);
403 ptr = strrchrW(mi->source, '\\');
404 lstrcatW(src, ptr + 1);
406 file = msi_download_file( src, temppath );
407 lstrcpyW(mi->source, file);
409 /* check if the remote cabinet still exists, ignore if it doesn't */
410 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
411 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
414 ERR("FDICreate failed\n");
415 return ERROR_FUNCTION_FAILED;
418 cabpath = strdupWtoA(mi->source);
419 hf = cabinet_open(cabpath, _O_RDONLY, 0);
420 if (!FDIIsCabinet(hfdi, hf, &cabinfo))
422 WARN("Remote cabinet %s does not exist.\n", debugstr_w(mi->source));
424 return ERROR_SUCCESS;
428 return !extract_cabinet_file(package, mi->source, mi->last_path);
431 static UINT ready_media_for_file( MSIPACKAGE *package, struct media_info *mi,
434 UINT rc = ERROR_SUCCESS;
436 static const WCHAR ExecSeqQuery[] =
437 {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
438 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
439 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
440 ' ','%', 'i',' ','O','R','D','E','R',' ','B','Y',' ',
441 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',0};
446 MSICOMPONENT *comp = file->Component;
448 if (file->Sequence <= mi->last_sequence)
450 set_file_source(package,file,comp,mi->last_path);
451 TRACE("Media already ready (%u, %u)\n",file->Sequence,mi->last_sequence);
452 return ERROR_SUCCESS;
456 row = MSI_QueryGetRecord(package->db, ExecSeqQuery, file->Sequence);
459 TRACE("Unable to query row\n");
460 return ERROR_FUNCTION_FAILED;
463 seq = MSI_RecordGetInteger(row,2);
464 mi->last_sequence = seq;
466 volume = MSI_RecordGetString(row, 5);
467 prompt = MSI_RecordGetString(row, 3);
469 msi_free(mi->last_path);
470 mi->last_path = NULL;
472 if (!file->IsCompressed)
474 mi->last_path = resolve_folder(package, comp->Directory, TRUE, FALSE, NULL);
475 set_file_source(package,file,comp,mi->last_path);
477 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
478 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count, volume,
481 MsiSourceListSetInfoW(package->ProductCode, NULL,
482 MSIINSTALLCONTEXT_USERMANAGED,
483 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
484 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
485 msiobj_release(&row->hdr);
489 cab = MSI_RecordGetString(row,4);
492 TRACE("Source is CAB %s\n",debugstr_w(cab));
493 /* the stream does not contain the # character */
498 writeout_cabinet_stream(package,&cab[1],mi->source);
499 mi->last_path = strdupW(mi->source);
500 *(strrchrW(mi->last_path,'\\')+1)=0;
502 path = msi_dup_property( package, cszSourceDir );
504 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
505 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count,
508 MsiSourceListSetInfoW(package->ProductCode, NULL,
509 MSIINSTALLCONTEXT_USERMANAGED,
510 MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
511 INSTALLPROPERTY_LASTUSEDSOURCEW, path);
518 mi->last_path = msi_alloc(MAX_PATH*sizeof(WCHAR));
519 if (MSI_GetPropertyW(package, cszSourceDir, mi->source, &sz))
521 ERR("No Source dir defined\n");
522 rc = ERROR_FUNCTION_FAILED;
526 strcpyW(mi->last_path,mi->source);
527 strcatW(mi->source,cab);
529 MsiSourceListSetInfoW(package->ProductCode, NULL,
530 MSIINSTALLCONTEXT_USERMANAGED,
531 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
532 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
534 /* extract the cab file into a folder in the temp folder */
536 if (MSI_GetPropertyW(package, cszTempFolder,mi->last_path, &sz)
538 GetTempPathW(MAX_PATH,mi->last_path);
542 /* only download the remote cabinet file if a local copy does not exist */
543 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
544 UrlIsW(package->PackagePath, URLIS_URL))
546 rc = msi_extract_remote_cabinet(package, mi);
550 rc = !extract_cabinet_file(package, mi->source, mi->last_path);
556 mi->last_path = msi_alloc(MAX_PATH*sizeof(WCHAR));
557 MSI_GetPropertyW(package,cszSourceDir,mi->source,&sz);
558 strcpyW(mi->last_path,mi->source);
560 MsiSourceListSetInfoW(package->ProductCode, NULL,
561 MSIINSTALLCONTEXT_USERMANAGED,
562 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
563 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
565 set_file_source(package, file, comp, mi->last_path);
567 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
568 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count, volume,
571 msiobj_release(&row->hdr);
576 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
579 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
581 if (lstrcmpW( file_key, (*file)->File )==0)
583 if ((*file)->state >= msifs_overwrite)
584 return ERROR_SUCCESS;
586 return ERROR_FILE_NOT_FOUND;
590 return ERROR_FUNCTION_FAILED;
594 * ACTION_InstallFiles()
596 * For efficiency, this is done in two passes:
597 * 1) Correct all the TargetPaths and determine what files are to be installed.
598 * 2) Extract Cabinets and copy files.
600 UINT ACTION_InstallFiles(MSIPACKAGE *package)
602 struct media_info *mi;
603 UINT rc = ERROR_SUCCESS;
607 /* increment progress bar each time action data is sent */
608 ui_progress(package,1,1,0,0);
610 /* handle the keys for the SourceList */
611 ptr = strrchrW(package->PackagePath,'\\');
615 MsiSourceListSetInfoW(package->ProductCode, NULL,
616 MSIINSTALLCONTEXT_USERMANAGED,
618 INSTALLPROPERTY_PACKAGENAMEW, ptr);
620 /* FIXME("Write DiskPrompt\n"); */
623 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
625 if (!ACTION_VerifyComponentForAction( file->Component, INSTALLSTATE_LOCAL ))
627 ui_progress(package,2,file->FileSize,0,0);
628 TRACE("File %s is not scheduled for install\n",
629 debugstr_w(file->File));
631 file->state = msifs_skipped;
636 * Despite MSDN specifying that the CreateFolders action
637 * should be called before InstallFiles, some installers don't
638 * do that, and they seem to work correctly. We need to create
639 * directories here to make sure that the files can be copied.
641 msi_create_component_directories( package );
643 mi = create_media_info();
646 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
648 if (file->state != msifs_missing && file->state != msifs_overwrite)
651 TRACE("Pass 2: %s\n",debugstr_w(file->File));
653 rc = ready_media_for_file( package, mi, file );
654 if (rc != ERROR_SUCCESS)
656 ERR("Unable to ready media\n");
657 rc = ERROR_FUNCTION_FAILED;
661 TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
662 debugstr_w(file->TargetPath));
664 if (file->state != msifs_missing && file->state != msifs_overwrite)
667 /* compressed files are extracted in ready_media_for_file */
668 if (file->IsCompressed)
670 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(file->TargetPath))
671 ERR("compressed file wasn't extracted (%s)\n",
672 debugstr_w(file->TargetPath));
676 rc = CopyFileW(file->SourcePath,file->TargetPath,FALSE);
680 ERR("Unable to copy file (%s -> %s) (error %d)\n",
681 debugstr_w(file->SourcePath), debugstr_w(file->TargetPath), rc);
682 if (rc == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
686 else if (rc == ERROR_FILE_NOT_FOUND)
688 ERR("Source File Not Found! Continuing\n");
691 else if (file->Attributes & msidbFileAttributesVital)
693 ERR("Ignoring Error and continuing (nonvital file)...\n");
699 file->state = msifs_installed;
705 free_media_info( mi );
709 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
711 MSIPACKAGE *package = (MSIPACKAGE*)param;
712 WCHAR dest_name[0x100];
713 LPWSTR dest_path, dest;
714 LPCWSTR file_key, component;
720 component = MSI_RecordGetString(row,2);
721 comp = get_loaded_component(package,component);
723 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
725 TRACE("Skipping copy due to disabled component %s\n",
726 debugstr_w(component));
728 /* the action taken was the same as the current install state */
729 comp->Action = comp->Installed;
731 return ERROR_SUCCESS;
734 comp->Action = INSTALLSTATE_LOCAL;
736 file_key = MSI_RecordGetString(row,3);
739 ERR("Unable to get file key\n");
740 return ERROR_FUNCTION_FAILED;
743 rc = get_file_target(package,file_key,&file);
745 if (rc != ERROR_SUCCESS)
747 ERR("Original file unknown %s\n",debugstr_w(file_key));
748 return ERROR_SUCCESS;
751 if (MSI_RecordIsNull(row,4))
752 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
756 MSI_RecordGetStringW(row,4,dest_name,&sz);
757 reduce_to_longfilename(dest_name);
760 if (MSI_RecordIsNull(row,5))
763 dest_path = strdupW(file->TargetPath);
764 p = strrchrW(dest_path,'\\');
771 destkey = MSI_RecordGetString(row,5);
772 dest_path = resolve_folder(package, destkey, FALSE,FALSE,NULL);
776 dest_path = msi_dup_property( package, destkey );
779 FIXME("Unable to get destination folder, try AppSearch properties\n");
780 return ERROR_SUCCESS;
785 dest = build_directory_name(2, dest_path, dest_name);
787 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
790 if (strcmpW(file->TargetPath,dest))
791 rc = !CopyFileW(file->TargetPath,dest,TRUE);
795 if (rc != ERROR_SUCCESS)
796 ERR("Failed to copy file %s -> %s, last error %ld\n",
797 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
799 FIXME("We should track these duplicate files as well\n");
804 msi_file_update_ui(package, file, szDuplicateFiles);
806 return ERROR_SUCCESS;
809 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
813 static const WCHAR ExecSeqQuery[] =
814 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
815 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
817 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
818 if (rc != ERROR_SUCCESS)
819 return ERROR_SUCCESS;
821 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
822 msiobj_release(&view->hdr);
827 /* compares the version of a file read from the filesystem and
828 * the version specified in the File table
830 static int msi_compare_file_version( MSIFILE *file )
832 WCHAR version[MAX_PATH];
838 r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
839 if ( r != ERROR_SUCCESS )
842 return lstrcmpW( version, file->Version );
845 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
849 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
854 if ( !file->Component )
856 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
859 if ( file->state == msifs_installed )
860 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
862 if ( file->state != msifs_present )
865 /* only remove a file if the version to be installed
866 * is strictly newer than the old file
868 if ( msi_compare_file_version( file ) >= 0 )
871 TRACE("removing %s\n", debugstr_w(file->File) );
872 if ( !DeleteFileW( file->TargetPath ) )
873 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
874 file->state = msifs_missing;
877 uirow = MSI_CreateRecord( 9 );
878 MSI_RecordSetStringW( uirow, 1, file->FileName );
879 uipath = strdupW( file->TargetPath );
880 p = strrchrW(uipath,'\\');
883 MSI_RecordSetStringW( uirow, 9, uipath);
884 ui_actiondata( package, szRemoveFiles, uirow);
885 msiobj_release( &uirow->hdr );
887 /* FIXME: call ui_progress here? */
890 return ERROR_SUCCESS;