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};
62 * This is a helper function for handling embedded cabinet media
64 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
74 rc = read_raw_stream_data(package->db,stream_name,&data,&size);
75 if (rc != ERROR_SUCCESS)
79 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
80 GetTempPathW(MAX_PATH,tmp);
82 GetTempFileNameW(tmp,stream_name,0,source);
84 track_tempfile(package,strrchrW(source,'\\'), source);
85 the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
86 FILE_ATTRIBUTE_NORMAL, NULL);
88 if (the_file == INVALID_HANDLE_VALUE)
90 ERR("Unable to create file %s\n",debugstr_w(source));
91 rc = ERROR_FUNCTION_FAILED;
95 WriteFile(the_file,data,size,&write,NULL);
96 CloseHandle(the_file);
97 TRACE("wrote %i bytes to %s\n",write,debugstr_w(source));
104 /* Support functions for FDI functions */
111 static void * cabinet_alloc(ULONG cb)
113 return msi_alloc(cb);
116 static void cabinet_free(void *pv)
121 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
125 DWORD dwShareMode = 0;
126 DWORD dwCreateDisposition = OPEN_EXISTING;
127 switch (oflag & _O_ACCMODE)
130 dwAccess = GENERIC_READ;
131 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
134 dwAccess = GENERIC_WRITE;
135 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
138 dwAccess = GENERIC_READ | GENERIC_WRITE;
139 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
142 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
143 dwCreateDisposition = CREATE_NEW;
144 else if (oflag & _O_CREAT)
145 dwCreateDisposition = CREATE_ALWAYS;
146 handle = CreateFileA( pszFile, dwAccess, dwShareMode, NULL,
147 dwCreateDisposition, 0, NULL );
148 if (handle == INVALID_HANDLE_VALUE)
150 return (INT_PTR) handle;
153 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
155 HANDLE handle = (HANDLE) hf;
157 if (ReadFile(handle, pv, cb, &dwRead, NULL))
162 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
164 HANDLE handle = (HANDLE) hf;
166 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
171 static int cabinet_close(INT_PTR hf)
173 HANDLE handle = (HANDLE) hf;
174 return CloseHandle(handle) ? 0 : -1;
177 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
179 HANDLE handle = (HANDLE) hf;
180 /* flags are compatible and so are passed straight through */
181 return SetFilePointer(handle, dist, NULL, seektype);
184 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
190 uirow = MSI_CreateRecord( 9 );
191 MSI_RecordSetStringW( uirow, 1, f->FileName );
192 uipath = strdupW( f->TargetPath );
193 p = strrchrW(uipath,'\\');
196 MSI_RecordSetStringW( uirow, 9, uipath);
197 MSI_RecordSetInteger( uirow, 6, f->FileSize );
198 ui_actiondata( package, action, uirow);
199 msiobj_release( &uirow->hdr );
201 ui_progress( package, 2, f->FileSize, 0, 0);
204 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
210 CabData *data = (CabData*) pfdin->pv;
216 file = strdupAtoW(pfdin->psz1);
217 f = get_loaded_file(data->package, file);
222 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
226 if (f->state != msifs_missing && f->state != msifs_overwrite)
228 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
232 msi_file_update_ui( data->package, f, szInstallFiles );
234 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
236 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
237 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
239 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
240 NULL, CREATE_ALWAYS, attrs, NULL );
241 if ( handle == INVALID_HANDLE_VALUE )
243 ERR("failed to create %s (error %d)\n",
244 debugstr_w( f->TargetPath ), GetLastError() );
248 f->state = msifs_installed;
249 return (INT_PTR) handle;
251 case fdintCLOSE_FILE_INFO:
255 HANDLE handle = (HANDLE) pfdin->hf;
257 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
259 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
261 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
271 /***********************************************************************
272 * extract_cabinet_file
274 * Extract files from a cab file.
276 static BOOL extract_cabinet_file(MSIPACKAGE* package, LPCWSTR source,
284 static CHAR empty[] = "";
287 TRACE("Extracting %s to %s\n",debugstr_w(source), debugstr_w(path));
289 hfdi = FDICreate(cabinet_alloc,
300 ERR("FDICreate failed\n");
304 if (!(cabinet = strdupWtoA( source )))
309 if (!(cab_path = strdupWtoA( path )))
316 data.package = package;
317 data.cab_path = cab_path;
319 ret = FDICopy(hfdi, cabinet, empty, 0, cabinet_notify, NULL, &data);
322 ERR("FDICopy failed\n");
332 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, MSICOMPONENT*
335 if (!file->IsCompressed)
338 p = resolve_folder(package, comp->Directory, TRUE, FALSE, NULL);
339 path = build_directory_name(2, p, file->ShortName);
340 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
343 path = build_directory_name(2, p, file->LongName);
345 file->SourcePath = path;
349 file->SourcePath = build_directory_name(2, path, file->File);
357 WCHAR source[MAX_PATH];
360 static struct media_info *create_media_info( void )
362 struct media_info *mi;
364 mi = msi_alloc( sizeof *mi );
367 mi->last_sequence = 0;
368 mi->last_volume = NULL;
369 mi->last_path = NULL;
377 static void free_media_info( struct media_info *mi )
379 msi_free( mi->last_path );
383 /* downloads a remote cabinet and extracts it if it exists */
384 static UINT msi_extract_remote_cabinet( MSIPACKAGE *package, struct media_info *mi )
386 FDICABINETINFO cabinfo;
387 WCHAR temppath[MAX_PATH];
396 /* the URL is the path prefix of the package URL and the filename
397 * of the file to download
399 ptr = strrchrW(package->PackagePath, '/');
400 lstrcpynW(src, package->PackagePath, ptr - package->PackagePath + 2);
401 ptr = strrchrW(mi->source, '\\');
402 lstrcatW(src, ptr + 1);
404 file = msi_download_file( src, temppath );
405 lstrcpyW(mi->source, file);
407 /* check if the remote cabinet still exists, ignore if it doesn't */
408 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
409 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
412 ERR("FDICreate failed\n");
413 return ERROR_FUNCTION_FAILED;
416 cabpath = strdupWtoA(mi->source);
417 hf = cabinet_open(cabpath, _O_RDONLY, 0);
418 if (!FDIIsCabinet(hfdi, hf, &cabinfo))
420 WARN("Remote cabinet %s does not exist.\n", debugstr_w(mi->source));
422 return ERROR_SUCCESS;
426 return !extract_cabinet_file(package, mi->source, mi->last_path);
429 static UINT ready_media_for_file( MSIPACKAGE *package, struct media_info *mi,
432 UINT rc = ERROR_SUCCESS;
434 static const WCHAR ExecSeqQuery[] =
435 {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
436 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
437 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
438 ' ','%', 'i',' ','O','R','D','E','R',' ','B','Y',' ',
439 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',0};
444 MSICOMPONENT *comp = file->Component;
446 if (file->Sequence <= mi->last_sequence)
448 set_file_source(package,file,comp,mi->last_path);
449 TRACE("Media already ready (%u, %u)\n",file->Sequence,mi->last_sequence);
450 return ERROR_SUCCESS;
454 row = MSI_QueryGetRecord(package->db, ExecSeqQuery, file->Sequence);
457 TRACE("Unable to query row\n");
458 return ERROR_FUNCTION_FAILED;
461 volume = MSI_RecordGetString(row, 5);
462 prompt = MSI_RecordGetString(row, 3);
464 msi_free(mi->last_path);
465 mi->last_path = NULL;
467 if (!file->IsCompressed)
469 mi->last_path = resolve_folder(package, comp->Directory, TRUE, FALSE, NULL);
470 set_file_source(package,file,comp,mi->last_path);
472 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
473 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count, volume,
476 MsiSourceListSetInfoW(package->ProductCode, NULL,
477 MSIINSTALLCONTEXT_USERMANAGED,
478 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
479 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
480 msiobj_release(&row->hdr);
484 seq = MSI_RecordGetInteger(row,2);
485 mi->last_sequence = seq;
487 cab = MSI_RecordGetString(row,4);
490 TRACE("Source is CAB %s\n",debugstr_w(cab));
491 /* the stream does not contain the # character */
496 writeout_cabinet_stream(package,&cab[1],mi->source);
497 mi->last_path = strdupW(mi->source);
498 *(strrchrW(mi->last_path,'\\')+1)=0;
500 path = msi_dup_property( package, cszSourceDir );
502 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
503 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count,
506 MsiSourceListSetInfoW(package->ProductCode, NULL,
507 MSIINSTALLCONTEXT_USERMANAGED,
508 MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
509 INSTALLPROPERTY_LASTUSEDSOURCEW, path);
516 mi->last_path = msi_alloc(MAX_PATH*sizeof(WCHAR));
517 if (MSI_GetPropertyW(package, cszSourceDir, mi->source, &sz))
519 ERR("No Source dir defined\n");
520 rc = ERROR_FUNCTION_FAILED;
524 strcpyW(mi->last_path,mi->source);
525 strcatW(mi->source,cab);
527 MsiSourceListSetInfoW(package->ProductCode, NULL,
528 MSIINSTALLCONTEXT_USERMANAGED,
529 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
530 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
532 /* extract the cab file into a folder in the temp folder */
534 if (MSI_GetPropertyW(package, cszTempFolder,mi->last_path, &sz)
536 GetTempPathW(MAX_PATH,mi->last_path);
540 /* only download the remote cabinet file if a local copy does not exist */
541 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
542 UrlIsW(package->PackagePath, URLIS_URL))
544 rc = msi_extract_remote_cabinet(package, mi);
548 rc = !extract_cabinet_file(package, mi->source, mi->last_path);
554 mi->last_path = msi_alloc(MAX_PATH*sizeof(WCHAR));
555 MSI_GetPropertyW(package,cszSourceDir,mi->source,&sz);
556 strcpyW(mi->last_path,mi->source);
558 MsiSourceListSetInfoW(package->ProductCode, NULL,
559 MSIINSTALLCONTEXT_USERMANAGED,
560 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
561 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
563 set_file_source(package, file, comp, mi->last_path);
565 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
566 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count, volume,
569 msiobj_release(&row->hdr);
574 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
577 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
579 if (lstrcmpW( file_key, (*file)->File )==0)
581 if ((*file)->state >= msifs_overwrite)
582 return ERROR_SUCCESS;
584 return ERROR_FILE_NOT_FOUND;
588 return ERROR_FUNCTION_FAILED;
592 * ACTION_InstallFiles()
594 * For efficiency, this is done in two passes:
595 * 1) Correct all the TargetPaths and determine what files are to be installed.
596 * 2) Extract Cabinets and copy files.
598 UINT ACTION_InstallFiles(MSIPACKAGE *package)
600 struct media_info *mi;
601 UINT rc = ERROR_SUCCESS;
605 /* increment progress bar each time action data is sent */
606 ui_progress(package,1,1,0,0);
608 /* handle the keys for the SourceList */
609 ptr = strrchrW(package->PackagePath,'\\');
613 MsiSourceListSetInfoW(package->ProductCode, NULL,
614 MSIINSTALLCONTEXT_USERMANAGED,
616 INSTALLPROPERTY_PACKAGENAMEW, ptr);
618 /* FIXME("Write DiskPrompt\n"); */
621 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
623 if (!ACTION_VerifyComponentForAction( file->Component, INSTALLSTATE_LOCAL ))
625 ui_progress(package,2,file->FileSize,0,0);
626 TRACE("File %s is not scheduled for install\n",
627 debugstr_w(file->File));
629 file->state = msifs_skipped;
634 * Despite MSDN specifying that the CreateFolders action
635 * should be called before InstallFiles, some installers don't
636 * do that, and they seem to work correctly. We need to create
637 * directories here to make sure that the files can be copied.
639 msi_create_component_directories( package );
641 mi = create_media_info();
644 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
646 if (file->state != msifs_missing && file->state != msifs_overwrite)
649 TRACE("Pass 2: %s\n",debugstr_w(file->File));
651 rc = ready_media_for_file( package, mi, file );
652 if (rc != ERROR_SUCCESS)
654 ERR("Unable to ready media\n");
655 rc = ERROR_FUNCTION_FAILED;
659 TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
660 debugstr_w(file->TargetPath));
662 if (file->state != msifs_missing && file->state != msifs_overwrite)
665 /* compressed files are extracted in ready_media_for_file */
666 if (file->IsCompressed)
668 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(file->TargetPath))
669 ERR("compressed file wasn't extracted (%s)\n",
670 debugstr_w(file->TargetPath));
674 rc = CopyFileW(file->SourcePath,file->TargetPath,FALSE);
678 ERR("Unable to copy file (%s -> %s) (error %d)\n",
679 debugstr_w(file->SourcePath), debugstr_w(file->TargetPath), rc);
680 if (rc == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
684 else if (rc == ERROR_FILE_NOT_FOUND)
686 ERR("Source File Not Found! Continuing\n");
689 else if (file->Attributes & msidbFileAttributesVital)
691 ERR("Ignoring Error and continuing (nonvital file)...\n");
697 file->state = msifs_installed;
703 free_media_info( mi );
707 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
709 MSIPACKAGE *package = (MSIPACKAGE*)param;
710 WCHAR dest_name[0x100];
711 LPWSTR dest_path, dest;
712 LPCWSTR file_key, component;
718 component = MSI_RecordGetString(row,2);
719 comp = get_loaded_component(package,component);
721 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
723 TRACE("Skipping copy due to disabled component %s\n",
724 debugstr_w(component));
726 /* the action taken was the same as the current install state */
727 comp->Action = comp->Installed;
729 return ERROR_SUCCESS;
732 comp->Action = INSTALLSTATE_LOCAL;
734 file_key = MSI_RecordGetString(row,3);
737 ERR("Unable to get file key\n");
738 return ERROR_FUNCTION_FAILED;
741 rc = get_file_target(package,file_key,&file);
743 if (rc != ERROR_SUCCESS)
745 ERR("Original file unknown %s\n",debugstr_w(file_key));
746 return ERROR_SUCCESS;
749 if (MSI_RecordIsNull(row,4))
750 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
754 MSI_RecordGetStringW(row,4,dest_name,&sz);
755 reduce_to_longfilename(dest_name);
758 if (MSI_RecordIsNull(row,5))
761 dest_path = strdupW(file->TargetPath);
762 p = strrchrW(dest_path,'\\');
769 destkey = MSI_RecordGetString(row,5);
770 dest_path = resolve_folder(package, destkey, FALSE,FALSE,NULL);
774 dest_path = msi_dup_property( package, destkey );
777 FIXME("Unable to get destination folder, try AppSearch properties\n");
778 return ERROR_SUCCESS;
783 dest = build_directory_name(2, dest_path, dest_name);
785 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
788 if (strcmpW(file->TargetPath,dest))
789 rc = !CopyFileW(file->TargetPath,dest,TRUE);
793 if (rc != ERROR_SUCCESS)
794 ERR("Failed to copy file %s -> %s, last error %d\n",
795 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
797 FIXME("We should track these duplicate files as well\n");
802 msi_file_update_ui(package, file, szDuplicateFiles);
804 return ERROR_SUCCESS;
807 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
811 static const WCHAR ExecSeqQuery[] =
812 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
813 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
815 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
816 if (rc != ERROR_SUCCESS)
817 return ERROR_SUCCESS;
819 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
820 msiobj_release(&view->hdr);
825 /* compares the version of a file read from the filesystem and
826 * the version specified in the File table
828 static int msi_compare_file_version( MSIFILE *file )
830 WCHAR version[MAX_PATH];
836 r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
837 if ( r != ERROR_SUCCESS )
840 return lstrcmpW( version, file->Version );
843 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
847 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
852 if ( !file->Component )
854 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
857 if ( file->state == msifs_installed )
858 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
860 if ( file->state != msifs_present )
863 /* only remove a file if the version to be installed
864 * is strictly newer than the old file
866 if ( msi_compare_file_version( file ) >= 0 )
869 TRACE("removing %s\n", debugstr_w(file->File) );
870 if ( !DeleteFileW( file->TargetPath ) )
871 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
872 file->state = msifs_missing;
875 uirow = MSI_CreateRecord( 9 );
876 MSI_RecordSetStringW( uirow, 1, file->FileName );
877 uipath = strdupW( file->TargetPath );
878 p = strrchrW(uipath,'\\');
881 MSI_RecordSetStringW( uirow, 9, uipath);
882 ui_actiondata( package, szRemoveFiles, uirow);
883 msiobj_release( &uirow->hdr );
885 /* FIXME: call ui_progress here? */
888 return ERROR_SUCCESS;