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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Actions dealing with files These are
29 * RemoveDuplicateFiles(TODO)
38 #include "wine/debug.h"
42 #include "msvcrt/fcntl.h"
45 #include "wine/unicode.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msi);
50 extern const WCHAR szInstallFiles[];
51 extern const WCHAR szDuplicateFiles[];
52 extern const WCHAR szMoveFiles[];
53 extern const WCHAR szPatchFiles[];
54 extern const WCHAR szRemoveDuplicateFiles[];
55 extern const WCHAR szRemoveFiles[];
57 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 %li 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)
123 DWORD dwShareMode = 0;
124 DWORD dwCreateDisposition = OPEN_EXISTING;
125 switch (oflag & _O_ACCMODE)
128 dwAccess = GENERIC_READ;
129 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
132 dwAccess = GENERIC_WRITE;
133 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
136 dwAccess = GENERIC_READ | GENERIC_WRITE;
137 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
140 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
141 dwCreateDisposition = CREATE_NEW;
142 else if (oflag & _O_CREAT)
143 dwCreateDisposition = CREATE_ALWAYS;
144 return (INT_PTR)CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
145 dwCreateDisposition, 0, NULL);
148 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
151 if (ReadFile((HANDLE)hf, pv, cb, &dwRead, NULL))
156 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
159 if (WriteFile((HANDLE)hf, pv, cb, &dwWritten, NULL))
164 static int cabinet_close(INT_PTR hf)
166 return CloseHandle((HANDLE)hf) ? 0 : -1;
169 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
171 /* flags are compatible and so are passed straight through */
172 return SetFilePointer((HANDLE)hf, dist, NULL, seektype);
175 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
181 CabData *data = (CabData*) pfdin->pv;
182 ULONG len = strlen(data->cab_path) + strlen(pfdin->psz1);
189 static const WCHAR tmpprefix[] = {'C','A','B','T','M','P','_',0};
196 given_file = strdupAtoW(pfdin->psz1);
197 f = get_loaded_file(data->package, given_file);
198 msi_free(given_file);
202 ERR("Unknown File in Cabinet (%s)\n",debugstr_a(pfdin->psz1));
206 if (!((f->State == 1 || f->State == 2)))
208 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
212 file = msi_alloc((len+1)*sizeof(char));
213 strcpy(file, data->cab_path);
214 strcat(file, pfdin->psz1);
216 TRACE("file: %s\n", debugstr_a(file));
218 /* track this file so it can be deleted if not installed */
219 trackpath=strdupAtoW(file);
220 tracknametmp=strdupAtoW(strrchr(file,'\\')+1);
221 trackname = msi_alloc((strlenW(tracknametmp) +
222 strlenW(tmpprefix)+1) * sizeof(WCHAR));
224 strcpyW(trackname,tmpprefix);
225 strcatW(trackname,tracknametmp);
227 track_tempfile(data->package, trackname, trackpath);
231 msi_free(tracknametmp);
234 uirow=MSI_CreateRecord(9);
235 MSI_RecordSetStringW( uirow, 1, f->FileName );
236 uipath = strdupW( f->TargetPath );
237 *(strrchrW(uipath,'\\')+1)=0;
238 MSI_RecordSetStringW(uirow,9,uipath);
239 MSI_RecordSetInteger( uirow, 6, f->FileSize );
240 ui_actiondata(data->package,szInstallFiles,uirow);
241 msiobj_release( &uirow->hdr );
244 ui_progress( data->package, 2, f->FileSize, 0, 0);
246 handle = cabinet_open(file, _O_WRONLY | _O_CREAT, 0);
250 case fdintCLOSE_FILE_INFO:
254 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
256 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
258 if (!SetFileTime((HANDLE)pfdin->hf, &ftLocal, 0, &ftLocal))
261 cabinet_close(pfdin->hf);
269 /***********************************************************************
270 * extract_cabinet_file
272 * Extract files from a cab file.
274 static BOOL extract_cabinet_file(MSIPACKAGE* package, LPCWSTR source,
284 TRACE("Extracting %s to %s\n",debugstr_w(source), debugstr_w(path));
286 hfdi = FDICreate(cabinet_alloc,
297 ERR("FDICreate failed\n");
301 if (!(cabinet = strdupWtoA( source )))
306 if (!(cab_path = strdupWtoA( path )))
313 data.package = package;
314 data.cab_path = cab_path;
316 ret = FDICopy(hfdi, cabinet, "", 0, cabinet_notify, NULL, &data);
319 ERR("FDICopy failed\n");
329 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, MSICOMPONENT*
332 if (file->Attributes & msidbFileAttributesNoncompressed)
335 p = resolve_folder(package, comp->Directory, TRUE, FALSE, NULL);
336 file->SourcePath = build_directory_name(2, p, file->ShortName);
340 file->SourcePath = build_directory_name(2, path, file->File);
343 static BOOL check_volume(LPCWSTR path, LPCWSTR want_volume, LPWSTR volume,
347 WCHAR name[MAX_PATH];
350 if (!(path[0] && path[1] == ':'))
357 TRACE("Checking volume %s .. (%s)\n",debugstr_w(drive), debugstr_w(want_volume));
358 type = GetDriveTypeW(drive);
359 TRACE("drive is of type %x\n",type);
361 if (type == DRIVE_UNKNOWN || type == DRIVE_NO_ROOT_DIR ||
362 type == DRIVE_FIXED || type == DRIVE_RAMDISK)
365 GetVolumeInformationW(drive, name, MAX_PATH, NULL, NULL, NULL, NULL, 0);
366 TRACE("Drive contains %s\n", debugstr_w(name));
367 volume = strdupW(name);
370 return (strcmpiW(want_volume,name)==0);
373 static BOOL check_for_sourcefile(LPCWSTR source)
375 DWORD attrib = GetFileAttributesW(source);
376 return (!(attrib == INVALID_FILE_ATTRIBUTES));
379 static UINT ready_volume(MSIPACKAGE* package, LPCWSTR path, LPWSTR last_volume,
380 MSIRECORD *row,UINT *type )
382 LPWSTR volume = NULL;
383 LPCWSTR want_volume = MSI_RecordGetString(row, 5);
384 BOOL ok = check_volume(path, want_volume, volume, type);
386 TRACE("Readying Volume for %s (%s, %s)\n",debugstr_w(path), debugstr_w(want_volume), debugstr_w(last_volume));
388 if (check_for_sourcefile(path) && !ok)
390 FIXME("Found the Sourcefile but not on the correct volume.(%s,%s,%s)\n",
391 debugstr_w(path),debugstr_w(want_volume), debugstr_w(volume));
392 return ERROR_SUCCESS;
401 prompt = MSI_RecordGetString(row,3);
402 msg = generate_error_string(package, 1302, 1, prompt);
403 rc = MessageBoxW(NULL,msg,NULL,MB_OKCANCEL);
407 ok = check_for_sourcefile(path);
409 return ERROR_INSTALL_USEREXIT;
412 msi_free(last_volume);
413 last_volume = strdupW(volume);
414 return ERROR_SUCCESS;
422 WCHAR source[MAX_PATH];
425 static struct media_info *create_media_info( void )
427 struct media_info *mi;
429 mi = msi_alloc( sizeof *mi );
432 mi->last_sequence = 0;
433 mi->last_volume = NULL;
434 mi->last_path = NULL;
442 static void free_media_info( struct media_info *mi )
444 msi_free( mi->last_path );
448 static UINT ready_media_for_file( MSIPACKAGE *package, struct media_info *mi,
451 UINT rc = ERROR_SUCCESS;
453 static const WCHAR ExecSeqQuery[] =
454 {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
455 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
456 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
457 ' ','%', 'i',' ','O','R','D','E','R',' ','B','Y',' ',
458 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',0};
464 MSICOMPONENT *comp = file->Component;
466 if (file->Sequence <= mi->last_sequence)
468 set_file_source(package,file,comp,mi->last_path);
469 TRACE("Media already ready (%u, %u)\n",file->Sequence,mi->last_sequence);
470 return ERROR_SUCCESS;
474 row = MSI_QueryGetRecord(package->db, ExecSeqQuery, file->Sequence);
477 TRACE("Unable to query row\n");
478 return ERROR_FUNCTION_FAILED;
481 seq = MSI_RecordGetInteger(row,2);
482 mi->last_sequence = seq;
484 volume = MSI_RecordGetString(row, 5);
485 prompt = MSI_RecordGetString(row, 3);
487 msi_free(mi->last_path);
488 mi->last_path = NULL;
490 if (file->Attributes & msidbFileAttributesNoncompressed)
492 mi->last_path = resolve_folder(package, comp->Directory, TRUE, FALSE, NULL);
493 set_file_source(package,file,comp,mi->last_path);
494 rc = ready_volume(package, file->SourcePath, mi->last_volume, row,&type);
496 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
497 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count, volume,
500 if (type == DRIVE_REMOVABLE || type == DRIVE_CDROM ||
501 type == DRIVE_RAMDISK)
502 MsiSourceListSetInfoW(package->ProductCode, NULL,
503 MSIINSTALLCONTEXT_USERMANAGED,
504 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
505 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
507 MsiSourceListSetInfoW(package->ProductCode, NULL,
508 MSIINSTALLCONTEXT_USERMANAGED,
509 MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
510 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
511 msiobj_release(&row->hdr);
515 cab = MSI_RecordGetString(row,4);
518 TRACE("Source is CAB %s\n",debugstr_w(cab));
519 /* the stream does not contain the # character */
524 writeout_cabinet_stream(package,&cab[1],mi->source);
525 mi->last_path = strdupW(mi->source);
526 *(strrchrW(mi->last_path,'\\')+1)=0;
528 path = msi_dup_property( package, cszSourceDir );
530 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
531 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count,
534 MsiSourceListSetInfoW(package->ProductCode, NULL,
535 MSIINSTALLCONTEXT_USERMANAGED,
536 MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
537 INSTALLPROPERTY_LASTUSEDSOURCEW, path);
544 mi->last_path = msi_alloc(MAX_PATH*sizeof(WCHAR));
545 if (MSI_GetPropertyW(package, cszSourceDir, mi->source, &sz))
547 ERR("No Source dir defined \n");
548 rc = ERROR_FUNCTION_FAILED;
552 strcpyW(mi->last_path,mi->source);
553 strcatW(mi->source,cab);
555 rc = ready_volume(package, mi->source, mi->last_volume, row, &type);
556 if (type == DRIVE_REMOVABLE || type == DRIVE_CDROM ||
557 type == DRIVE_RAMDISK)
558 MsiSourceListSetInfoW(package->ProductCode, NULL,
559 MSIINSTALLCONTEXT_USERMANAGED,
560 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
561 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
563 MsiSourceListSetInfoW(package->ProductCode, NULL,
564 MSIINSTALLCONTEXT_USERMANAGED,
565 MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
566 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
568 /* extract the cab file into a folder in the temp folder */
570 if (MSI_GetPropertyW(package, cszTempFolder,mi->last_path, &sz)
572 GetTempPathW(MAX_PATH,mi->last_path);
575 rc = !extract_cabinet_file(package, mi->source, mi->last_path);
580 mi->last_path = msi_alloc(MAX_PATH*sizeof(WCHAR));
581 MSI_GetPropertyW(package,cszSourceDir,mi->source,&sz);
582 strcpyW(mi->last_path,mi->source);
583 rc = ready_volume(package, mi->last_path, mi->last_volume, row, &type);
585 if (type == DRIVE_REMOVABLE || type == DRIVE_CDROM ||
586 type == DRIVE_RAMDISK)
587 MsiSourceListSetInfoW(package->ProductCode, NULL,
588 MSIINSTALLCONTEXT_USERMANAGED,
589 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
590 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
592 MsiSourceListSetInfoW(package->ProductCode, NULL,
593 MSIINSTALLCONTEXT_USERMANAGED,
594 MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
595 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
597 set_file_source(package, file, comp, mi->last_path);
599 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
600 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count, volume,
603 msiobj_release(&row->hdr);
608 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
614 return ERROR_INVALID_HANDLE;
616 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
618 if (lstrcmpW( file_key, file->File )==0)
620 if (file->State >= 2)
622 *file_source = strdupW( file->TargetPath );
623 return ERROR_SUCCESS;
626 return ERROR_FILE_NOT_FOUND;
630 return ERROR_FUNCTION_FAILED;
634 * In order to make this work more effeciencly I am going to do this in 2
636 * Pass 1) Correct all the TargetPaths and determin what files are to be
638 * Pass 2) Extract Cabinents and copy files.
640 UINT ACTION_InstallFiles(MSIPACKAGE *package)
642 struct media_info *mi;
643 UINT rc = ERROR_SUCCESS;
648 return ERROR_INVALID_HANDLE;
650 /* increment progress bar each time action data is sent */
651 ui_progress(package,1,1,0,0);
653 /* handle the keys for the SouceList */
654 ptr = strrchrW(package->PackagePath,'\\');
658 MsiSourceListSetInfoW(package->ProductCode, NULL,
659 MSIINSTALLCONTEXT_USERMANAGED,
661 INSTALLPROPERTY_PACKAGENAMEW, ptr);
663 FIXME("Write DiskPrompt\n");
666 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
668 if (!ACTION_VerifyComponentForAction(package, file->Component,
671 ui_progress(package,2,file->FileSize,0,0);
672 TRACE("File %s is not scheduled for install\n",
673 debugstr_w(file->File));
679 mi = create_media_info();
682 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
684 if ((file->State == 1) || (file->State == 2))
686 TRACE("Pass 2: %s\n",debugstr_w(file->File));
688 rc = ready_media_for_file( package, mi, file );
689 if (rc != ERROR_SUCCESS)
691 ERR("Unable to ready media\n");
692 rc = ERROR_FUNCTION_FAILED;
696 TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
697 debugstr_w(file->TargetPath));
699 if (file->Attributes & msidbFileAttributesNoncompressed)
700 rc = CopyFileW(file->SourcePath,file->TargetPath,FALSE);
702 rc = MoveFileW(file->SourcePath, file->TargetPath);
707 ERR("Unable to move/copy file (%s -> %s) (error %d)\n",
708 debugstr_w(file->SourcePath), debugstr_w(file->TargetPath),
710 if (rc == ERROR_ALREADY_EXISTS && file->State == 2)
712 if (!CopyFileW(file->SourcePath,file->TargetPath,FALSE))
713 ERR("Unable to copy file (%s -> %s) (error %ld)\n",
714 debugstr_w(file->SourcePath),
715 debugstr_w(file->TargetPath), GetLastError());
716 if (!(file->Attributes & msidbFileAttributesNoncompressed))
717 DeleteFileW(file->SourcePath);
720 else if (rc == ERROR_FILE_NOT_FOUND)
722 ERR("Source File Not Found! Continuing\n");
725 else if (file->Attributes & msidbFileAttributesVital)
727 ERR("Ignoring Error and continuing (nonvital file)...\n");
740 free_media_info( mi );
744 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
746 MSIPACKAGE *package = (MSIPACKAGE*)param;
747 WCHAR *file_source = NULL;
748 WCHAR dest_name[0x100];
749 LPWSTR dest_path, dest;
750 LPCWSTR file_key, component;
755 component = MSI_RecordGetString(row,2);
756 comp = get_loaded_component(package,component);
758 if (!ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_LOCAL))
760 TRACE("Skipping copy due to disabled component %s\n",
761 debugstr_w(component));
763 /* the action taken was the same as the current install state */
764 comp->Action = comp->Installed;
766 return ERROR_SUCCESS;
769 comp->Action = INSTALLSTATE_LOCAL;
771 file_key = MSI_RecordGetString(row,3);
774 ERR("Unable to get file key\n");
775 return ERROR_FUNCTION_FAILED;
778 rc = get_file_target(package,file_key,&file_source);
780 if (rc != ERROR_SUCCESS)
782 ERR("Original file unknown %s\n",debugstr_w(file_key));
783 msi_free(file_source);
784 return ERROR_SUCCESS;
787 if (MSI_RecordIsNull(row,4))
788 strcpyW(dest_name,strrchrW(file_source,'\\')+1);
792 MSI_RecordGetStringW(row,4,dest_name,&sz);
793 reduce_to_longfilename(dest_name);
796 if (MSI_RecordIsNull(row,5))
799 dest_path = strdupW(file_source);
800 p = strrchrW(dest_path,'\\');
807 destkey = MSI_RecordGetString(row,5);
808 dest_path = resolve_folder(package, destkey, FALSE,FALSE,NULL);
812 dest_path = msi_dup_property( package, destkey );
815 FIXME("Unable to get destination folder, try AppSearch properties\n");
816 msi_free(file_source);
817 return ERROR_SUCCESS;
822 dest = build_directory_name(2, dest_path, dest_name);
824 TRACE("Duplicating file %s to %s\n",debugstr_w(file_source),
827 if (strcmpW(file_source,dest))
828 rc = !CopyFileW(file_source,dest,TRUE);
832 if (rc != ERROR_SUCCESS)
833 ERR("Failed to copy file %s -> %s, last error %ld\n", debugstr_w(file_source), debugstr_w(dest_path), GetLastError());
835 FIXME("We should track these duplicate files as well\n");
839 msi_free(file_source);
841 return ERROR_SUCCESS;
844 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
848 static const WCHAR ExecSeqQuery[] =
849 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
850 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
853 return ERROR_INVALID_HANDLE;
855 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
856 if (rc != ERROR_SUCCESS)
857 return ERROR_SUCCESS;
859 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
860 msiobj_release(&view->hdr);