push 0f15bbd80d260bbd8adf052e820484a405c49375
[wine/hacks.git] / dlls / msi / files.c
blob8d5321c9df2282accd791a7b924acf0f87789812
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 ERR("Failed to get next cabinet information: %d\n", rc);
331 return -1;
334 if (lstrcmpiW(mi->cabinet, cab))
336 msi_free(cab);
337 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
338 return -1;
341 msi_free(cab);
343 TRACE("Searching for %s\n", debugstr_w(mi->source));
345 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
346 rc = msi_change_media(data->package, mi);
348 if (rc != ERROR_SUCCESS)
349 return -1;
351 return 0;
353 case fdintCOPY_FILE:
355 CabData *data = (CabData*) pfdin->pv;
356 HANDLE handle;
357 LPWSTR file;
358 MSIFILE *f;
359 DWORD attrs;
361 file = strdupAtoW(pfdin->psz1);
362 f = get_loaded_file(data->package, file);
363 msi_free(file);
365 if (!f)
367 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
368 return 0;
371 if (f->state != msifs_missing && f->state != msifs_overwrite)
373 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
374 return 0;
377 msi_file_update_ui( data->package, f, szInstallFiles );
379 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
381 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
382 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
384 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
385 NULL, CREATE_ALWAYS, attrs, NULL );
386 if ( handle == INVALID_HANDLE_VALUE )
388 if ( GetFileAttributesW( f->TargetPath ) != INVALID_FILE_ATTRIBUTES )
389 f->state = msifs_installed;
390 else
391 ERR("failed to create %s (error %d)\n",
392 debugstr_w( f->TargetPath ), GetLastError() );
394 return 0;
397 f->state = msifs_installed;
398 return (INT_PTR) handle;
400 case fdintCLOSE_FILE_INFO:
402 FILETIME ft;
403 FILETIME ftLocal;
404 HANDLE handle = (HANDLE) pfdin->hf;
406 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
407 return -1;
408 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
409 return -1;
410 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
411 return -1;
412 CloseHandle(handle);
413 return 1;
415 default:
416 return 0;
420 /***********************************************************************
421 * extract_cabinet_file
423 * Extract files from a cab file.
425 static BOOL extract_cabinet_file(MSIPACKAGE* package, struct media_info *mi)
427 LPSTR cabinet, cab_path = NULL;
428 LPWSTR ptr;
429 HFDI hfdi;
430 ERF erf;
431 BOOL ret = FALSE;
432 CabData data;
434 TRACE("Extracting %s\n", debugstr_w(mi->source));
436 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
437 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
438 if (!hfdi)
440 ERR("FDICreate failed\n");
441 return FALSE;
444 ptr = strrchrW(mi->source, '\\') + 1;
445 cabinet = strdupWtoA(ptr);
446 if (!cabinet)
447 goto done;
449 cab_path = strdupWtoA(mi->source);
450 if (!cab_path)
451 goto done;
453 cab_path[ptr - mi->source] = '\0';
455 data.package = package;
456 data.mi = mi;
458 ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, &data);
459 if (!ret)
460 ERR("FDICopy failed\n");
462 done:
463 FDIDestroy(hfdi);
464 msi_free(cabinet);
465 msi_free(cab_path);
467 if (ret)
468 mi->is_extracted = TRUE;
470 return ret;
473 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, LPCWSTR path)
475 if (!file->IsCompressed)
477 LPWSTR p, path;
478 p = resolve_folder(package, file->Component->Directory, TRUE, FALSE, TRUE, NULL);
479 path = build_directory_name(2, p, file->ShortName);
480 if (file->LongName &&
481 INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
483 msi_free(path);
484 path = build_directory_name(2, p, file->LongName);
486 file->SourcePath = path;
487 msi_free(p);
489 else
490 file->SourcePath = build_directory_name(2, path, file->File);
493 static void free_media_info( struct media_info *mi )
495 msi_free( mi->disk_prompt );
496 msi_free( mi->cabinet );
497 msi_free( mi->volume_label );
498 msi_free( mi->first_volume );
499 msi_free( mi );
502 static UINT download_remote_cabinet(MSIPACKAGE *package, struct media_info *mi)
504 WCHAR temppath[MAX_PATH];
505 LPWSTR src, ptr;
506 LPCWSTR cab;
508 src = strdupW(package->BaseURL);
509 if (!src)
510 return ERROR_OUTOFMEMORY;
512 ptr = strrchrW(src, '/');
513 if (!ptr)
515 msi_free(src);
516 return ERROR_FUNCTION_FAILED;
519 *(ptr + 1) = '\0';
520 ptr = strrchrW(mi->source, '\\');
521 if (!ptr)
522 ptr = mi->source;
524 src = msi_realloc(src, (lstrlenW(src) + lstrlenW(ptr)) * sizeof(WCHAR));
525 if (!src)
526 return ERROR_OUTOFMEMORY;
528 lstrcatW(src, ptr + 1);
530 temppath[0] = '\0';
531 cab = msi_download_file(src, temppath);
532 lstrcpyW(mi->source, cab);
534 msi_free(src);
535 return ERROR_SUCCESS;
538 static UINT load_media_info(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
540 MSIRECORD *row;
541 LPWSTR source_dir;
542 UINT r;
544 static const WCHAR query[] = {
545 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
546 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
547 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
548 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
549 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
550 '`','D','i','s','k','I','d','`',0
553 row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
554 if (!row)
556 TRACE("Unable to query row\n");
557 return ERROR_FUNCTION_FAILED;
560 mi->is_extracted = FALSE;
561 mi->disk_id = MSI_RecordGetInteger(row, 1);
562 mi->last_sequence = MSI_RecordGetInteger(row, 2);
563 msi_free(mi->disk_prompt);
564 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
565 msi_free(mi->cabinet);
566 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
567 msi_free(mi->volume_label);
568 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
569 msiobj_release(&row->hdr);
571 if (!mi->first_volume)
572 mi->first_volume = strdupW(mi->volume_label);
574 source_dir = msi_dup_property(package, cszSourceDir);
576 if (mi->cabinet && mi->cabinet[0] == '#')
578 r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
579 if (r != ERROR_SUCCESS)
581 ERR("Failed to extract cabinet stream\n");
582 return ERROR_FUNCTION_FAILED;
585 else
587 lstrcpyW(mi->source, source_dir);
590 if (mi->cabinet)
591 lstrcatW(mi->source, mi->cabinet);
594 msi_package_add_media_disk(package, MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT,
595 mi->disk_id, mi->volume_label, mi->disk_prompt);
597 msi_package_add_info(package, MSIINSTALLCONTEXT_USERMANAGED,
598 MSICODE_PRODUCT | MSISOURCETYPE_MEDIA,
599 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->source);
601 msi_free(source_dir);
602 return ERROR_SUCCESS;
605 static UINT ready_media(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
607 UINT rc = ERROR_SUCCESS;
609 /* media info for continuous cabinet is already loaded */
610 if (mi->is_continuous)
611 return ERROR_SUCCESS;
613 rc = load_media_info(package, file, mi);
614 if (rc != ERROR_SUCCESS)
616 ERR("Unable to load media info\n");
617 return ERROR_FUNCTION_FAILED;
620 /* cabinet is internal, no checks needed */
621 if (!mi->cabinet || mi->cabinet[0] == '#')
622 return ERROR_SUCCESS;
624 /* package should be downloaded */
625 if (file->IsCompressed &&
626 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
627 package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
629 return download_remote_cabinet(package, mi);
632 /* check volume matches, change media if not */
633 if (mi->volume_label && mi->disk_id > 1 &&
634 lstrcmpW(mi->first_volume, mi->volume_label))
636 LPWSTR source = msi_dup_property(package, cszSourceDir);
637 BOOL matches;
638 UINT type;
640 PathStripToRootW(source);
641 type = GetDriveTypeW(source);
642 matches = source_matches_volume(mi, source);
643 msi_free(source);
645 if ((type == DRIVE_CDROM || type == DRIVE_REMOVABLE) && !matches)
647 rc = msi_change_media(package, mi);
648 if (rc != ERROR_SUCCESS)
649 return rc;
653 if (file->IsCompressed &&
654 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
656 ERR("Cabinet not found: %s\n", debugstr_w(mi->source));
657 return ERROR_INSTALL_FAILURE;
660 return ERROR_SUCCESS;
663 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
664 MSIFILE** file)
666 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
668 if (lstrcmpW( file_key, (*file)->File )==0)
670 if ((*file)->state >= msifs_overwrite)
671 return ERROR_SUCCESS;
672 else
673 return ERROR_FILE_NOT_FOUND;
677 return ERROR_FUNCTION_FAILED;
680 static void schedule_install_files(MSIPACKAGE *package)
682 MSIFILE *file;
684 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
686 if (!ACTION_VerifyComponentForAction(file->Component, INSTALLSTATE_LOCAL))
688 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
690 ui_progress(package,2,file->FileSize,0,0);
691 file->state = msifs_skipped;
696 static UINT copy_file(MSIFILE *file)
698 BOOL ret;
700 ret = CopyFileW(file->SourcePath, file->TargetPath, FALSE);
701 if (ret)
703 file->state = msifs_installed;
704 return ERROR_SUCCESS;
707 return GetLastError();
710 static UINT copy_install_file(MSIFILE *file)
712 UINT gle;
714 TRACE("Copying %s to %s\n", debugstr_w(file->SourcePath),
715 debugstr_w(file->TargetPath));
717 gle = copy_file(file);
718 if (gle == ERROR_SUCCESS)
719 return gle;
721 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
723 TRACE("overwriting existing file\n");
724 gle = ERROR_SUCCESS;
726 else if (gle == ERROR_FILE_NOT_FOUND)
728 /* FIXME: this needs to be tested, I'm pretty sure it fails */
729 TRACE("Source file not found\n");
730 gle = ERROR_SUCCESS;
732 else if (gle == ERROR_ACCESS_DENIED)
734 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
736 gle = copy_file(file);
737 TRACE("Overwriting existing file: %d\n", gle);
739 else if (!(file->Attributes & msidbFileAttributesVital))
741 TRACE("Ignoring error for nonvital\n");
742 gle = ERROR_SUCCESS;
745 return gle;
748 static BOOL check_dest_hash_matches(MSIFILE *file)
750 MSIFILEHASHINFO hash;
751 UINT r;
753 if (!file->hash.dwFileHashInfoSize)
754 return FALSE;
756 hash.dwFileHashInfoSize = sizeof(MSIFILEHASHINFO);
757 r = MsiGetFileHashW(file->TargetPath, 0, &hash);
758 if (r != ERROR_SUCCESS)
759 return FALSE;
761 return !memcmp(&hash, &file->hash, sizeof(MSIFILEHASHINFO));
765 * ACTION_InstallFiles()
767 * For efficiency, this is done in two passes:
768 * 1) Correct all the TargetPaths and determine what files are to be installed.
769 * 2) Extract Cabinets and copy files.
771 UINT ACTION_InstallFiles(MSIPACKAGE *package)
773 struct media_info *mi;
774 UINT rc = ERROR_SUCCESS;
775 LPWSTR ptr;
776 MSIFILE *file;
778 /* increment progress bar each time action data is sent */
779 ui_progress(package,1,1,0,0);
781 /* handle the keys for the SourceList */
782 ptr = strrchrW(package->PackagePath,'\\');
783 if (ptr)
785 ptr++;
786 msi_package_add_info(package, MSIINSTALLCONTEXT_USERMANAGED,
787 MSICODE_PRODUCT, INSTALLPROPERTY_PACKAGENAMEW, ptr);
790 schedule_install_files(package);
793 * Despite MSDN specifying that the CreateFolders action
794 * should be called before InstallFiles, some installers don't
795 * do that, and they seem to work correctly. We need to create
796 * directories here to make sure that the files can be copied.
798 msi_create_component_directories( package );
800 mi = msi_alloc_zero( sizeof(struct media_info) );
802 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
804 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
805 continue;
807 if (check_dest_hash_matches(file))
809 TRACE("File hashes match, not overwriting\n");
810 continue;
813 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
814 (file->IsCompressed && !mi->is_extracted))
816 rc = ready_media(package, file, mi);
817 if (rc != ERROR_SUCCESS)
819 ERR("Failed to ready media\n");
820 break;
823 if (file->IsCompressed && !extract_cabinet_file(package, mi))
825 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
826 rc = ERROR_FUNCTION_FAILED;
827 break;
831 set_file_source(package, file, mi->source);
833 TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
834 debugstr_w(file->TargetPath));
836 if (!file->IsCompressed)
838 msi_file_update_ui(package, file, szInstallFiles);
839 rc = copy_install_file(file);
840 if (rc != ERROR_SUCCESS)
842 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(file->SourcePath),
843 debugstr_w(file->TargetPath), rc);
844 rc = ERROR_INSTALL_FAILURE;
845 break;
848 else if (file->state != msifs_installed)
850 ERR("compressed file wasn't extracted (%s)\n", debugstr_w(file->TargetPath));
851 rc = ERROR_INSTALL_FAILURE;
852 break;
856 free_media_info( mi );
857 return rc;
860 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
862 MSIPACKAGE *package = (MSIPACKAGE*)param;
863 WCHAR dest_name[0x100];
864 LPWSTR dest_path, dest;
865 LPCWSTR file_key, component;
866 DWORD sz;
867 DWORD rc;
868 MSICOMPONENT *comp;
869 MSIFILE *file;
871 component = MSI_RecordGetString(row,2);
872 comp = get_loaded_component(package,component);
874 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
876 TRACE("Skipping copy due to disabled component %s\n",
877 debugstr_w(component));
879 /* the action taken was the same as the current install state */
880 comp->Action = comp->Installed;
882 return ERROR_SUCCESS;
885 comp->Action = INSTALLSTATE_LOCAL;
887 file_key = MSI_RecordGetString(row,3);
888 if (!file_key)
890 ERR("Unable to get file key\n");
891 return ERROR_FUNCTION_FAILED;
894 rc = get_file_target(package,file_key,&file);
896 if (rc != ERROR_SUCCESS)
898 ERR("Original file unknown %s\n",debugstr_w(file_key));
899 return ERROR_SUCCESS;
902 if (MSI_RecordIsNull(row,4))
903 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
904 else
906 sz=0x100;
907 MSI_RecordGetStringW(row,4,dest_name,&sz);
908 reduce_to_longfilename(dest_name);
911 if (MSI_RecordIsNull(row,5))
913 LPWSTR p;
914 dest_path = strdupW(file->TargetPath);
915 p = strrchrW(dest_path,'\\');
916 if (p)
917 *p=0;
919 else
921 LPCWSTR destkey;
922 destkey = MSI_RecordGetString(row,5);
923 dest_path = resolve_folder(package, destkey, FALSE, FALSE, TRUE, NULL);
924 if (!dest_path)
926 /* try a Property */
927 dest_path = msi_dup_property( package, destkey );
928 if (!dest_path)
930 FIXME("Unable to get destination folder, try AppSearch properties\n");
931 return ERROR_SUCCESS;
936 dest = build_directory_name(2, dest_path, dest_name);
938 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
939 debugstr_w(dest));
941 CreateDirectoryW(dest_path, NULL);
943 if (strcmpW(file->TargetPath,dest))
944 rc = !CopyFileW(file->TargetPath,dest,TRUE);
945 else
946 rc = ERROR_SUCCESS;
948 if (rc != ERROR_SUCCESS)
949 ERR("Failed to copy file %s -> %s, last error %d\n",
950 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
952 FIXME("We should track these duplicate files as well\n");
954 msi_free(dest_path);
955 msi_free(dest);
957 msi_file_update_ui(package, file, szDuplicateFiles);
959 return ERROR_SUCCESS;
962 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
964 UINT rc;
965 MSIQUERY * view;
966 static const WCHAR ExecSeqQuery[] =
967 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
968 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
970 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
971 if (rc != ERROR_SUCCESS)
972 return ERROR_SUCCESS;
974 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
975 msiobj_release(&view->hdr);
977 return rc;
980 /* compares the version of a file read from the filesystem and
981 * the version specified in the File table
983 static int msi_compare_file_version( MSIFILE *file )
985 WCHAR version[MAX_PATH];
986 DWORD size;
987 UINT r;
989 size = MAX_PATH;
990 version[0] = '\0';
991 r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
992 if ( r != ERROR_SUCCESS )
993 return 0;
995 return lstrcmpW( version, file->Version );
998 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1000 MSIFILE *file;
1002 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1004 MSIRECORD *uirow;
1005 LPWSTR uipath, p;
1007 if ( !file->Component )
1008 continue;
1009 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
1010 continue;
1012 if ( file->state == msifs_installed )
1013 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1015 if ( file->state != msifs_present )
1016 continue;
1018 /* only remove a file if the version to be installed
1019 * is strictly newer than the old file
1021 if ( msi_compare_file_version( file ) >= 0 )
1022 continue;
1024 TRACE("removing %s\n", debugstr_w(file->File) );
1025 if ( !DeleteFileW( file->TargetPath ) )
1026 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
1027 file->state = msifs_missing;
1029 /* the UI chunk */
1030 uirow = MSI_CreateRecord( 9 );
1031 MSI_RecordSetStringW( uirow, 1, file->FileName );
1032 uipath = strdupW( file->TargetPath );
1033 p = strrchrW(uipath,'\\');
1034 if (p)
1035 p[1]=0;
1036 MSI_RecordSetStringW( uirow, 9, uipath);
1037 ui_actiondata( package, szRemoveFiles, uirow);
1038 msiobj_release( &uirow->hdr );
1039 msi_free( uipath );
1040 /* FIXME: call ui_progress here? */
1043 return ERROR_SUCCESS;