winhelp: Display keywords index dialog box.
[wine.git] / dlls / msi / files.c
blob442c73aa48b35fbd0f6a679dadb096a2990901d4
1 /*
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
25 * InstallFiles
26 * DuplicateFiles
27 * MoveFiles (TODO)
28 * PatchFiles (TODO)
29 * RemoveDuplicateFiles(TODO)
30 * RemoveFiles(TODO)
33 #include <stdarg.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winerror.h"
38 #include "wine/debug.h"
39 #include "fdi.h"
40 #include "msi.h"
41 #include "msidefs.h"
42 #include "msvcrt/fcntl.h"
43 #include "msipriv.h"
44 #include "winuser.h"
45 #include "winreg.h"
46 #include "shlwapi.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};
60 struct media_info {
61 UINT disk_id;
62 UINT last_sequence;
63 LPWSTR disk_prompt;
64 LPWSTR cabinet;
65 LPWSTR first_volume;
66 LPWSTR volume_label;
67 BOOL is_continuous;
68 BOOL is_extracted;
69 WCHAR source[MAX_PATH];
72 static BOOL source_matches_volume(struct media_info *mi, LPWSTR source_root)
74 WCHAR volume_name[MAX_PATH + 1];
76 if (!GetVolumeInformationW(source_root, volume_name, MAX_PATH + 1,
77 NULL, NULL, NULL, NULL, 0))
79 ERR("Failed to get volume information\n");
80 return FALSE;
83 return !lstrcmpW(mi->volume_label, volume_name);
86 static UINT msi_change_media( MSIPACKAGE *package, struct media_info *mi )
88 LPSTR msg;
89 LPWSTR error, error_dialog;
90 LPWSTR source_dir;
91 UINT r = ERROR_SUCCESS;
93 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
94 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
96 if ( (msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE && !gUIHandlerA )
97 return ERROR_SUCCESS;
99 error = generate_error_string( package, 1302, 1, mi->disk_prompt );
100 error_dialog = msi_dup_property( package, error_prop );
101 source_dir = msi_dup_property( package, cszSourceDir );
102 PathStripToRootW(source_dir);
104 while ( r == ERROR_SUCCESS &&
105 !source_matches_volume(mi, source_dir) )
107 r = msi_spawn_error_dialog( package, error_dialog, error );
109 if (gUIHandlerA)
111 msg = strdupWtoA( error );
112 gUIHandlerA( gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg );
113 msi_free(msg);
117 msi_free( error );
118 msi_free( error_dialog );
119 msi_free( source_dir );
121 return r;
125 * This is a helper function for handling embedded cabinet media
127 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
128 WCHAR* source)
130 UINT rc;
131 USHORT* data;
132 UINT size;
133 DWORD write;
134 HANDLE the_file;
135 WCHAR tmp[MAX_PATH];
137 rc = read_raw_stream_data(package->db,stream_name,&data,&size);
138 if (rc != ERROR_SUCCESS)
139 return rc;
141 write = MAX_PATH;
142 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
143 GetTempPathW(MAX_PATH,tmp);
145 GetTempFileNameW(tmp,stream_name,0,source);
147 track_tempfile(package, source);
148 the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
149 FILE_ATTRIBUTE_NORMAL, NULL);
151 if (the_file == INVALID_HANDLE_VALUE)
153 ERR("Unable to create file %s\n",debugstr_w(source));
154 rc = ERROR_FUNCTION_FAILED;
155 goto end;
158 WriteFile(the_file,data,size,&write,NULL);
159 CloseHandle(the_file);
160 TRACE("wrote %i bytes to %s\n",write,debugstr_w(source));
161 end:
162 msi_free(data);
163 return rc;
167 /* Support functions for FDI functions */
168 typedef struct
170 MSIPACKAGE* package;
171 struct media_info *mi;
172 } CabData;
174 static void * cabinet_alloc(ULONG cb)
176 return msi_alloc(cb);
179 static void cabinet_free(void *pv)
181 msi_free(pv);
184 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
186 HANDLE handle;
187 DWORD dwAccess = 0;
188 DWORD dwShareMode = 0;
189 DWORD dwCreateDisposition = OPEN_EXISTING;
190 switch (oflag & _O_ACCMODE)
192 case _O_RDONLY:
193 dwAccess = GENERIC_READ;
194 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
195 break;
196 case _O_WRONLY:
197 dwAccess = GENERIC_WRITE;
198 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
199 break;
200 case _O_RDWR:
201 dwAccess = GENERIC_READ | GENERIC_WRITE;
202 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
203 break;
205 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
206 dwCreateDisposition = CREATE_NEW;
207 else if (oflag & _O_CREAT)
208 dwCreateDisposition = CREATE_ALWAYS;
209 handle = CreateFileA( pszFile, dwAccess, dwShareMode, NULL,
210 dwCreateDisposition, 0, NULL );
211 if (handle == INVALID_HANDLE_VALUE)
212 return 0;
213 return (INT_PTR) handle;
216 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
218 HANDLE handle = (HANDLE) hf;
219 DWORD dwRead;
220 if (ReadFile(handle, pv, cb, &dwRead, NULL))
221 return dwRead;
222 return 0;
225 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
227 HANDLE handle = (HANDLE) hf;
228 DWORD dwWritten;
229 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
230 return dwWritten;
231 return 0;
234 static int cabinet_close(INT_PTR hf)
236 HANDLE handle = (HANDLE) hf;
237 return CloseHandle(handle) ? 0 : -1;
240 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
242 HANDLE handle = (HANDLE) hf;
243 /* flags are compatible and so are passed straight through */
244 return SetFilePointer(handle, dist, NULL, seektype);
247 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
249 MSIRECORD *uirow;
250 LPWSTR uipath, p;
252 /* the UI chunk */
253 uirow = MSI_CreateRecord( 9 );
254 MSI_RecordSetStringW( uirow, 1, f->FileName );
255 uipath = strdupW( f->TargetPath );
256 p = strrchrW(uipath,'\\');
257 if (p)
258 p[1]=0;
259 MSI_RecordSetStringW( uirow, 9, uipath);
260 MSI_RecordSetInteger( uirow, 6, f->FileSize );
261 ui_actiondata( package, action, uirow);
262 msiobj_release( &uirow->hdr );
263 msi_free( uipath );
264 ui_progress( package, 2, f->FileSize, 0, 0);
267 static UINT msi_media_get_disk_info( MSIPACKAGE *package, struct media_info *mi )
269 MSIRECORD *row;
270 LPWSTR ptr;
272 static const WCHAR query[] =
273 {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
274 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
275 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
277 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
278 if (!row)
280 TRACE("Unable to query row\n");
281 return ERROR_FUNCTION_FAILED;
284 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
285 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
286 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
288 if (!mi->first_volume)
289 mi->first_volume = strdupW(mi->volume_label);
291 ptr = strrchrW(mi->source, '\\') + 1;
292 lstrcpyW(ptr, mi->cabinet);
293 msiobj_release(&row->hdr);
295 return ERROR_SUCCESS;
298 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
300 TRACE("(%d)\n", fdint);
302 switch (fdint)
304 case fdintPARTIAL_FILE:
306 CabData *data = (CabData *)pfdin->pv;
307 data->mi->is_continuous = FALSE;
308 return 0;
310 case fdintNEXT_CABINET:
312 CabData *data = (CabData *)pfdin->pv;
313 struct media_info *mi = data->mi;
314 LPWSTR cab = strdupAtoW(pfdin->psz1);
315 UINT rc;
317 msi_free(mi->disk_prompt);
318 msi_free(mi->cabinet);
319 msi_free(mi->volume_label);
320 mi->disk_prompt = NULL;
321 mi->cabinet = NULL;
322 mi->volume_label = NULL;
324 mi->disk_id++;
325 mi->is_continuous = TRUE;
327 rc = msi_media_get_disk_info(data->package, mi);
328 if (rc != ERROR_SUCCESS)
330 msi_free(cab);
331 ERR("Failed to get next cabinet information: %d\n", rc);
332 return -1;
335 if (lstrcmpiW(mi->cabinet, cab))
337 msi_free(cab);
338 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
339 return -1;
342 msi_free(cab);
344 TRACE("Searching for %s\n", debugstr_w(mi->source));
346 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
347 rc = msi_change_media(data->package, mi);
349 if (rc != ERROR_SUCCESS)
350 return -1;
352 return 0;
354 case fdintCOPY_FILE:
356 CabData *data = (CabData*) pfdin->pv;
357 HANDLE handle;
358 LPWSTR file;
359 MSIFILE *f;
360 DWORD attrs;
362 file = strdupAtoW(pfdin->psz1);
363 f = get_loaded_file(data->package, file);
364 msi_free(file);
366 if (!f)
368 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
369 return 0;
372 if (f->state != msifs_missing && f->state != msifs_overwrite)
374 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
375 return 0;
378 msi_file_update_ui( data->package, f, szInstallFiles );
380 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
382 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
383 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
385 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
386 NULL, CREATE_ALWAYS, attrs, NULL );
387 if ( handle == INVALID_HANDLE_VALUE )
389 if ( GetFileAttributesW( f->TargetPath ) != INVALID_FILE_ATTRIBUTES )
390 f->state = msifs_installed;
391 else
392 ERR("failed to create %s (error %d)\n",
393 debugstr_w( f->TargetPath ), GetLastError() );
395 return 0;
398 f->state = msifs_installed;
399 return (INT_PTR) handle;
401 case fdintCLOSE_FILE_INFO:
403 FILETIME ft;
404 FILETIME ftLocal;
405 HANDLE handle = (HANDLE) pfdin->hf;
407 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
408 return -1;
409 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
410 return -1;
411 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
412 return -1;
413 CloseHandle(handle);
414 return 1;
416 default:
417 return 0;
421 /***********************************************************************
422 * extract_cabinet_file
424 * Extract files from a cab file.
426 static BOOL extract_cabinet_file(MSIPACKAGE* package, struct media_info *mi)
428 LPSTR cabinet, cab_path = NULL;
429 LPWSTR ptr;
430 HFDI hfdi;
431 ERF erf;
432 BOOL ret = FALSE;
433 CabData data;
435 TRACE("Extracting %s\n", debugstr_w(mi->source));
437 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
438 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
439 if (!hfdi)
441 ERR("FDICreate failed\n");
442 return FALSE;
445 ptr = strrchrW(mi->source, '\\') + 1;
446 cabinet = strdupWtoA(ptr);
447 if (!cabinet)
448 goto done;
450 cab_path = strdupWtoA(mi->source);
451 if (!cab_path)
452 goto done;
454 cab_path[ptr - mi->source] = '\0';
456 data.package = package;
457 data.mi = mi;
459 ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, &data);
460 if (!ret)
461 ERR("FDICopy failed\n");
463 done:
464 FDIDestroy(hfdi);
465 msi_free(cabinet);
466 msi_free(cab_path);
468 if (ret)
469 mi->is_extracted = TRUE;
471 return ret;
474 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, LPCWSTR path)
476 if (!file->IsCompressed)
478 LPWSTR p, path;
479 p = resolve_folder(package, file->Component->Directory, TRUE, FALSE, TRUE, NULL);
480 path = build_directory_name(2, p, file->ShortName);
481 if (file->LongName &&
482 INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
484 msi_free(path);
485 path = build_directory_name(2, p, file->LongName);
487 file->SourcePath = path;
488 msi_free(p);
490 else
491 file->SourcePath = build_directory_name(2, path, file->File);
494 static void free_media_info( struct media_info *mi )
496 msi_free( mi->disk_prompt );
497 msi_free( mi->cabinet );
498 msi_free( mi->volume_label );
499 msi_free( mi->first_volume );
500 msi_free( mi );
503 static UINT download_remote_cabinet(MSIPACKAGE *package, struct media_info *mi)
505 WCHAR temppath[MAX_PATH];
506 LPWSTR src, ptr;
507 LPCWSTR cab;
509 src = strdupW(package->BaseURL);
510 if (!src)
511 return ERROR_OUTOFMEMORY;
513 ptr = strrchrW(src, '/');
514 if (!ptr)
516 msi_free(src);
517 return ERROR_FUNCTION_FAILED;
520 *(ptr + 1) = '\0';
521 ptr = strrchrW(mi->source, '\\');
522 if (!ptr)
523 ptr = mi->source;
525 src = msi_realloc(src, (lstrlenW(src) + lstrlenW(ptr)) * sizeof(WCHAR));
526 if (!src)
527 return ERROR_OUTOFMEMORY;
529 lstrcatW(src, ptr + 1);
531 temppath[0] = '\0';
532 cab = msi_download_file(src, temppath);
533 lstrcpyW(mi->source, cab);
535 msi_free(src);
536 return ERROR_SUCCESS;
539 static UINT load_media_info(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
541 MSIRECORD *row;
542 LPWSTR source_dir;
543 UINT r;
545 static const WCHAR query[] = {
546 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
547 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
548 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
549 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
550 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
551 '`','D','i','s','k','I','d','`',0
554 row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
555 if (!row)
557 TRACE("Unable to query row\n");
558 return ERROR_FUNCTION_FAILED;
561 mi->is_extracted = FALSE;
562 mi->disk_id = MSI_RecordGetInteger(row, 1);
563 mi->last_sequence = MSI_RecordGetInteger(row, 2);
564 msi_free(mi->disk_prompt);
565 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
566 msi_free(mi->cabinet);
567 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
568 msi_free(mi->volume_label);
569 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
570 msiobj_release(&row->hdr);
572 if (!mi->first_volume)
573 mi->first_volume = strdupW(mi->volume_label);
575 source_dir = msi_dup_property(package, cszSourceDir);
577 if (mi->cabinet && mi->cabinet[0] == '#')
579 r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
580 if (r != ERROR_SUCCESS)
582 ERR("Failed to extract cabinet stream\n");
583 return ERROR_FUNCTION_FAILED;
586 else
588 lstrcpyW(mi->source, source_dir);
591 if (mi->cabinet)
592 lstrcatW(mi->source, mi->cabinet);
595 msi_package_add_media_disk(package, MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT,
596 mi->disk_id, mi->volume_label, mi->disk_prompt);
598 msi_package_add_info(package, MSIINSTALLCONTEXT_USERMANAGED,
599 MSICODE_PRODUCT | MSISOURCETYPE_MEDIA,
600 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->source);
602 msi_free(source_dir);
603 return ERROR_SUCCESS;
606 static UINT ready_media(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
608 UINT rc = ERROR_SUCCESS;
610 /* media info for continuous cabinet is already loaded */
611 if (mi->is_continuous)
612 return ERROR_SUCCESS;
614 rc = load_media_info(package, file, mi);
615 if (rc != ERROR_SUCCESS)
617 ERR("Unable to load media info\n");
618 return ERROR_FUNCTION_FAILED;
621 /* cabinet is internal, no checks needed */
622 if (!mi->cabinet || mi->cabinet[0] == '#')
623 return ERROR_SUCCESS;
625 /* package should be downloaded */
626 if (file->IsCompressed &&
627 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
628 package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
630 return download_remote_cabinet(package, mi);
633 /* check volume matches, change media if not */
634 if (mi->volume_label && mi->disk_id > 1 &&
635 lstrcmpW(mi->first_volume, mi->volume_label))
637 LPWSTR source = msi_dup_property(package, cszSourceDir);
638 BOOL matches;
639 UINT type;
641 PathStripToRootW(source);
642 type = GetDriveTypeW(source);
643 matches = source_matches_volume(mi, source);
644 msi_free(source);
646 if ((type == DRIVE_CDROM || type == DRIVE_REMOVABLE) && !matches)
648 rc = msi_change_media(package, mi);
649 if (rc != ERROR_SUCCESS)
650 return rc;
654 if (file->IsCompressed &&
655 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
657 ERR("Cabinet not found: %s\n", debugstr_w(mi->source));
658 return ERROR_INSTALL_FAILURE;
661 return ERROR_SUCCESS;
664 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
665 MSIFILE** file)
667 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
669 if (lstrcmpW( file_key, (*file)->File )==0)
671 if ((*file)->state >= msifs_overwrite)
672 return ERROR_SUCCESS;
673 else
674 return ERROR_FILE_NOT_FOUND;
678 return ERROR_FUNCTION_FAILED;
681 static void schedule_install_files(MSIPACKAGE *package)
683 MSIFILE *file;
685 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
687 if (!ACTION_VerifyComponentForAction(file->Component, INSTALLSTATE_LOCAL))
689 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
691 ui_progress(package,2,file->FileSize,0,0);
692 file->state = msifs_skipped;
697 static UINT copy_file(MSIFILE *file)
699 BOOL ret;
701 ret = CopyFileW(file->SourcePath, file->TargetPath, FALSE);
702 if (ret)
704 file->state = msifs_installed;
705 return ERROR_SUCCESS;
708 return GetLastError();
711 static UINT copy_install_file(MSIFILE *file)
713 UINT gle;
715 TRACE("Copying %s to %s\n", debugstr_w(file->SourcePath),
716 debugstr_w(file->TargetPath));
718 gle = copy_file(file);
719 if (gle == ERROR_SUCCESS)
720 return gle;
722 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
724 TRACE("overwriting existing file\n");
725 gle = ERROR_SUCCESS;
727 else if (gle == ERROR_FILE_NOT_FOUND)
729 /* FIXME: this needs to be tested, I'm pretty sure it fails */
730 TRACE("Source file not found\n");
731 gle = ERROR_SUCCESS;
733 else if (gle == ERROR_ACCESS_DENIED)
735 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
737 gle = copy_file(file);
738 TRACE("Overwriting existing file: %d\n", gle);
740 else if (!(file->Attributes & msidbFileAttributesVital))
742 TRACE("Ignoring error for nonvital\n");
743 gle = ERROR_SUCCESS;
746 return gle;
749 static BOOL check_dest_hash_matches(MSIFILE *file)
751 MSIFILEHASHINFO hash;
752 UINT r;
754 if (!file->hash.dwFileHashInfoSize)
755 return FALSE;
757 hash.dwFileHashInfoSize = sizeof(MSIFILEHASHINFO);
758 r = MsiGetFileHashW(file->TargetPath, 0, &hash);
759 if (r != ERROR_SUCCESS)
760 return FALSE;
762 return !memcmp(&hash, &file->hash, sizeof(MSIFILEHASHINFO));
766 * ACTION_InstallFiles()
768 * For efficiency, this is done in two passes:
769 * 1) Correct all the TargetPaths and determine what files are to be installed.
770 * 2) Extract Cabinets and copy files.
772 UINT ACTION_InstallFiles(MSIPACKAGE *package)
774 struct media_info *mi;
775 UINT rc = ERROR_SUCCESS;
776 LPWSTR ptr;
777 MSIFILE *file;
779 /* increment progress bar each time action data is sent */
780 ui_progress(package,1,1,0,0);
782 /* handle the keys for the SourceList */
783 ptr = strrchrW(package->PackagePath,'\\');
784 if (ptr)
786 ptr++;
787 msi_package_add_info(package, MSIINSTALLCONTEXT_USERMANAGED,
788 MSICODE_PRODUCT, INSTALLPROPERTY_PACKAGENAMEW, ptr);
791 schedule_install_files(package);
794 * Despite MSDN specifying that the CreateFolders action
795 * should be called before InstallFiles, some installers don't
796 * do that, and they seem to work correctly. We need to create
797 * directories here to make sure that the files can be copied.
799 msi_create_component_directories( package );
801 mi = msi_alloc_zero( sizeof(struct media_info) );
803 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
805 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
806 continue;
808 if (check_dest_hash_matches(file))
810 TRACE("File hashes match, not overwriting\n");
811 continue;
814 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
815 (file->IsCompressed && !mi->is_extracted))
817 rc = ready_media(package, file, mi);
818 if (rc != ERROR_SUCCESS)
820 ERR("Failed to ready media\n");
821 break;
824 if (file->IsCompressed && !extract_cabinet_file(package, mi))
826 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
827 rc = ERROR_FUNCTION_FAILED;
828 break;
832 set_file_source(package, file, mi->source);
834 TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
835 debugstr_w(file->TargetPath));
837 if (!file->IsCompressed)
839 msi_file_update_ui(package, file, szInstallFiles);
840 rc = copy_install_file(file);
841 if (rc != ERROR_SUCCESS)
843 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(file->SourcePath),
844 debugstr_w(file->TargetPath), rc);
845 rc = ERROR_INSTALL_FAILURE;
846 break;
849 else if (file->state != msifs_installed)
851 ERR("compressed file wasn't extracted (%s)\n", debugstr_w(file->TargetPath));
852 rc = ERROR_INSTALL_FAILURE;
853 break;
857 free_media_info( mi );
858 return rc;
861 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
863 MSIPACKAGE *package = (MSIPACKAGE*)param;
864 WCHAR dest_name[0x100];
865 LPWSTR dest_path, dest;
866 LPCWSTR file_key, component;
867 DWORD sz;
868 DWORD rc;
869 MSICOMPONENT *comp;
870 MSIFILE *file;
872 component = MSI_RecordGetString(row,2);
873 comp = get_loaded_component(package,component);
875 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
877 TRACE("Skipping copy due to disabled component %s\n",
878 debugstr_w(component));
880 /* the action taken was the same as the current install state */
881 comp->Action = comp->Installed;
883 return ERROR_SUCCESS;
886 comp->Action = INSTALLSTATE_LOCAL;
888 file_key = MSI_RecordGetString(row,3);
889 if (!file_key)
891 ERR("Unable to get file key\n");
892 return ERROR_FUNCTION_FAILED;
895 rc = get_file_target(package,file_key,&file);
897 if (rc != ERROR_SUCCESS)
899 ERR("Original file unknown %s\n",debugstr_w(file_key));
900 return ERROR_SUCCESS;
903 if (MSI_RecordIsNull(row,4))
904 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
905 else
907 sz=0x100;
908 MSI_RecordGetStringW(row,4,dest_name,&sz);
909 reduce_to_longfilename(dest_name);
912 if (MSI_RecordIsNull(row,5))
914 LPWSTR p;
915 dest_path = strdupW(file->TargetPath);
916 p = strrchrW(dest_path,'\\');
917 if (p)
918 *p=0;
920 else
922 LPCWSTR destkey;
923 destkey = MSI_RecordGetString(row,5);
924 dest_path = resolve_folder(package, destkey, FALSE, FALSE, TRUE, NULL);
925 if (!dest_path)
927 /* try a Property */
928 dest_path = msi_dup_property( package, destkey );
929 if (!dest_path)
931 FIXME("Unable to get destination folder, try AppSearch properties\n");
932 return ERROR_SUCCESS;
937 dest = build_directory_name(2, dest_path, dest_name);
939 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
940 debugstr_w(dest));
942 CreateDirectoryW(dest_path, NULL);
944 if (strcmpW(file->TargetPath,dest))
945 rc = !CopyFileW(file->TargetPath,dest,TRUE);
946 else
947 rc = ERROR_SUCCESS;
949 if (rc != ERROR_SUCCESS)
950 ERR("Failed to copy file %s -> %s, last error %d\n",
951 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
953 FIXME("We should track these duplicate files as well\n");
955 msi_free(dest_path);
956 msi_free(dest);
958 msi_file_update_ui(package, file, szDuplicateFiles);
960 return ERROR_SUCCESS;
963 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
965 UINT rc;
966 MSIQUERY * view;
967 static const WCHAR ExecSeqQuery[] =
968 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
969 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
971 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
972 if (rc != ERROR_SUCCESS)
973 return ERROR_SUCCESS;
975 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
976 msiobj_release(&view->hdr);
978 return rc;
981 /* compares the version of a file read from the filesystem and
982 * the version specified in the File table
984 static int msi_compare_file_version( MSIFILE *file )
986 WCHAR version[MAX_PATH];
987 DWORD size;
988 UINT r;
990 size = MAX_PATH;
991 version[0] = '\0';
992 r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
993 if ( r != ERROR_SUCCESS )
994 return 0;
996 return lstrcmpW( version, file->Version );
999 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1001 MSIFILE *file;
1003 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1005 MSIRECORD *uirow;
1006 LPWSTR uipath, p;
1008 if ( !file->Component )
1009 continue;
1010 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
1011 continue;
1013 if ( file->state == msifs_installed )
1014 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1016 if ( file->state != msifs_present )
1017 continue;
1019 /* only remove a file if the version to be installed
1020 * is strictly newer than the old file
1022 if ( msi_compare_file_version( file ) >= 0 )
1023 continue;
1025 TRACE("removing %s\n", debugstr_w(file->File) );
1026 if ( !DeleteFileW( file->TargetPath ) )
1027 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
1028 file->state = msifs_missing;
1030 /* the UI chunk */
1031 uirow = MSI_CreateRecord( 9 );
1032 MSI_RecordSetStringW( uirow, 1, file->FileName );
1033 uipath = strdupW( file->TargetPath );
1034 p = strrchrW(uipath,'\\');
1035 if (p)
1036 p[1]=0;
1037 MSI_RecordSetStringW( uirow, 9, uipath);
1038 ui_actiondata( package, szRemoveFiles, uirow);
1039 msiobj_release( &uirow->hdr );
1040 msi_free( uipath );
1041 /* FIXME: call ui_progress here? */
1044 return ERROR_SUCCESS;