push 97f44e0adb27fff75ba63d8fb97c65db9edfbe82
[wine/hacks.git] / dlls / msi / files.c
blobc50e4200a4b154df483df5a43435b77d7855d77f
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 volume_label;
66 BOOL is_continuous;
67 BOOL is_extracted;
68 WCHAR source[MAX_PATH];
71 static BOOL source_matches_volume(struct media_info *mi, LPWSTR source_root)
73 WCHAR volume_name[MAX_PATH + 1];
75 if (!GetVolumeInformationW(source_root, volume_name, MAX_PATH + 1,
76 NULL, NULL, NULL, NULL, 0))
78 ERR("Failed to get volume information\n");
79 return FALSE;
82 return !lstrcmpW(mi->volume_label, volume_name);
85 static UINT msi_change_media( MSIPACKAGE *package, struct media_info *mi )
87 LPSTR msg;
88 LPWSTR error, error_dialog;
89 LPWSTR source_dir;
90 UINT r = ERROR_SUCCESS;
92 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
93 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
95 if ( (msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE && !gUIHandlerA )
96 return ERROR_SUCCESS;
98 error = generate_error_string( package, 1302, 1, mi->disk_prompt );
99 error_dialog = msi_dup_property( package, error_prop );
100 source_dir = msi_dup_property( package, cszSourceDir );
101 PathStripToRootW(source_dir);
103 while ( r == ERROR_SUCCESS &&
104 !source_matches_volume(mi, source_dir) )
106 r = msi_spawn_error_dialog( package, error_dialog, error );
108 if (gUIHandlerA)
110 msg = strdupWtoA( error );
111 gUIHandlerA( gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg );
112 msi_free(msg);
116 msi_free( error );
117 msi_free( error_dialog );
118 msi_free( source_dir );
120 return r;
124 * This is a helper function for handling embedded cabinet media
126 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
127 WCHAR* source)
129 UINT rc;
130 USHORT* data;
131 UINT size;
132 DWORD write;
133 HANDLE the_file;
134 WCHAR tmp[MAX_PATH];
136 rc = read_raw_stream_data(package->db,stream_name,&data,&size);
137 if (rc != ERROR_SUCCESS)
138 return rc;
140 write = MAX_PATH;
141 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
142 GetTempPathW(MAX_PATH,tmp);
144 GetTempFileNameW(tmp,stream_name,0,source);
146 track_tempfile(package, source);
147 the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
148 FILE_ATTRIBUTE_NORMAL, NULL);
150 if (the_file == INVALID_HANDLE_VALUE)
152 ERR("Unable to create file %s\n",debugstr_w(source));
153 rc = ERROR_FUNCTION_FAILED;
154 goto end;
157 WriteFile(the_file,data,size,&write,NULL);
158 CloseHandle(the_file);
159 TRACE("wrote %i bytes to %s\n",write,debugstr_w(source));
160 end:
161 msi_free(data);
162 return rc;
166 /* Support functions for FDI functions */
167 typedef struct
169 MSIPACKAGE* package;
170 struct media_info *mi;
171 } CabData;
173 static void * cabinet_alloc(ULONG cb)
175 return msi_alloc(cb);
178 static void cabinet_free(void *pv)
180 msi_free(pv);
183 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
185 HANDLE handle;
186 DWORD dwAccess = 0;
187 DWORD dwShareMode = 0;
188 DWORD dwCreateDisposition = OPEN_EXISTING;
189 switch (oflag & _O_ACCMODE)
191 case _O_RDONLY:
192 dwAccess = GENERIC_READ;
193 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
194 break;
195 case _O_WRONLY:
196 dwAccess = GENERIC_WRITE;
197 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
198 break;
199 case _O_RDWR:
200 dwAccess = GENERIC_READ | GENERIC_WRITE;
201 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
202 break;
204 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
205 dwCreateDisposition = CREATE_NEW;
206 else if (oflag & _O_CREAT)
207 dwCreateDisposition = CREATE_ALWAYS;
208 handle = CreateFileA( pszFile, dwAccess, dwShareMode, NULL,
209 dwCreateDisposition, 0, NULL );
210 if (handle == INVALID_HANDLE_VALUE)
211 return 0;
212 return (INT_PTR) handle;
215 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
217 HANDLE handle = (HANDLE) hf;
218 DWORD dwRead;
219 if (ReadFile(handle, pv, cb, &dwRead, NULL))
220 return dwRead;
221 return 0;
224 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
226 HANDLE handle = (HANDLE) hf;
227 DWORD dwWritten;
228 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
229 return dwWritten;
230 return 0;
233 static int cabinet_close(INT_PTR hf)
235 HANDLE handle = (HANDLE) hf;
236 return CloseHandle(handle) ? 0 : -1;
239 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
241 HANDLE handle = (HANDLE) hf;
242 /* flags are compatible and so are passed straight through */
243 return SetFilePointer(handle, dist, NULL, seektype);
246 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
248 MSIRECORD *uirow;
249 LPWSTR uipath, p;
251 /* the UI chunk */
252 uirow = MSI_CreateRecord( 9 );
253 MSI_RecordSetStringW( uirow, 1, f->FileName );
254 uipath = strdupW( f->TargetPath );
255 p = strrchrW(uipath,'\\');
256 if (p)
257 p[1]=0;
258 MSI_RecordSetStringW( uirow, 9, uipath);
259 MSI_RecordSetInteger( uirow, 6, f->FileSize );
260 ui_actiondata( package, action, uirow);
261 msiobj_release( &uirow->hdr );
262 msi_free( uipath );
263 ui_progress( package, 2, f->FileSize, 0, 0);
266 static UINT msi_media_get_disk_info( MSIPACKAGE *package, struct media_info *mi )
268 MSIRECORD *row;
269 LPWSTR ptr;
271 static const WCHAR query[] =
272 {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
273 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
274 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
276 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
277 if (!row)
279 TRACE("Unable to query row\n");
280 return ERROR_FUNCTION_FAILED;
283 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
284 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
285 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
287 ptr = strrchrW(mi->source, '\\') + 1;
288 lstrcpyW(ptr, mi->cabinet);
289 msiobj_release(&row->hdr);
291 return ERROR_SUCCESS;
294 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
296 TRACE("(%d)\n", fdint);
298 switch (fdint)
300 case fdintPARTIAL_FILE:
302 CabData *data = (CabData *)pfdin->pv;
303 data->mi->is_continuous = FALSE;
304 return 0;
306 case fdintNEXT_CABINET:
308 CabData *data = (CabData *)pfdin->pv;
309 struct media_info *mi = data->mi;
310 LPWSTR cab = strdupAtoW(pfdin->psz1);
311 UINT rc;
313 msi_free(mi->disk_prompt);
314 msi_free(mi->cabinet);
315 msi_free(mi->volume_label);
316 mi->disk_prompt = NULL;
317 mi->cabinet = NULL;
318 mi->volume_label = NULL;
320 mi->disk_id++;
321 mi->is_continuous = TRUE;
323 rc = msi_media_get_disk_info(data->package, mi);
324 if (rc != ERROR_SUCCESS)
326 ERR("Failed to get next cabinet information: %d\n", rc);
327 return -1;
330 if (lstrcmpiW(mi->cabinet, cab))
332 msi_free(cab);
333 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
334 return -1;
337 msi_free(cab);
339 TRACE("Searching for %s\n", debugstr_w(mi->source));
341 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
342 rc = msi_change_media(data->package, mi);
344 if (rc != ERROR_SUCCESS)
345 return -1;
347 return 0;
349 case fdintCOPY_FILE:
351 CabData *data = (CabData*) pfdin->pv;
352 HANDLE handle;
353 LPWSTR file;
354 MSIFILE *f;
355 DWORD attrs;
357 file = strdupAtoW(pfdin->psz1);
358 f = get_loaded_file(data->package, file);
359 msi_free(file);
361 if (!f)
363 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
364 return 0;
367 if (f->state != msifs_missing && f->state != msifs_overwrite)
369 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
370 return 0;
373 msi_file_update_ui( data->package, f, szInstallFiles );
375 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
377 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
378 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
380 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
381 NULL, CREATE_ALWAYS, attrs, NULL );
382 if ( handle == INVALID_HANDLE_VALUE )
384 if ( GetFileAttributesW( f->TargetPath ) != INVALID_FILE_ATTRIBUTES )
385 f->state = msifs_installed;
386 else
387 ERR("failed to create %s (error %d)\n",
388 debugstr_w( f->TargetPath ), GetLastError() );
390 return 0;
393 f->state = msifs_installed;
394 return (INT_PTR) handle;
396 case fdintCLOSE_FILE_INFO:
398 FILETIME ft;
399 FILETIME ftLocal;
400 HANDLE handle = (HANDLE) pfdin->hf;
402 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
403 return -1;
404 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
405 return -1;
406 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
407 return -1;
408 CloseHandle(handle);
409 return 1;
411 default:
412 return 0;
416 /***********************************************************************
417 * extract_cabinet_file
419 * Extract files from a cab file.
421 static BOOL extract_cabinet_file(MSIPACKAGE* package, struct media_info *mi)
423 LPSTR cabinet, cab_path = NULL;
424 LPWSTR ptr;
425 HFDI hfdi;
426 ERF erf;
427 BOOL ret = FALSE;
428 CabData data;
430 TRACE("Extracting %s\n", debugstr_w(mi->source));
432 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
433 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
434 if (!hfdi)
436 ERR("FDICreate failed\n");
437 return FALSE;
440 ptr = strrchrW(mi->source, '\\') + 1;
441 cabinet = strdupWtoA(ptr);
442 if (!cabinet)
443 goto done;
445 cab_path = strdupWtoA(mi->source);
446 if (!cab_path)
447 goto done;
449 cab_path[ptr - mi->source] = '\0';
451 data.package = package;
452 data.mi = mi;
454 ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, &data);
455 if (!ret)
456 ERR("FDICopy failed\n");
458 done:
459 FDIDestroy(hfdi);
460 msi_free(cabinet);
461 msi_free(cab_path);
463 if (ret)
464 mi->is_extracted = TRUE;
466 return ret;
469 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, LPCWSTR path)
471 if (!file->IsCompressed)
473 LPWSTR p, path;
474 p = resolve_folder(package, file->Component->Directory, TRUE, FALSE, TRUE, NULL);
475 path = build_directory_name(2, p, file->ShortName);
476 if (file->LongName &&
477 INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
479 msi_free(path);
480 path = build_directory_name(2, p, file->LongName);
482 file->SourcePath = path;
483 msi_free(p);
485 else
486 file->SourcePath = build_directory_name(2, path, file->File);
489 static void free_media_info( struct media_info *mi )
491 msi_free( mi->disk_prompt );
492 msi_free( mi->cabinet );
493 msi_free( mi->volume_label );
494 msi_free( mi );
497 static UINT download_remote_cabinet(MSIPACKAGE *package, struct media_info *mi)
499 WCHAR temppath[MAX_PATH];
500 LPWSTR src, ptr;
501 LPCWSTR cab;
503 src = strdupW(package->BaseURL);
504 if (!src)
505 return ERROR_OUTOFMEMORY;
507 ptr = strrchrW(src, '/');
508 if (!ptr)
510 msi_free(src);
511 return ERROR_FUNCTION_FAILED;
514 *(ptr + 1) = '\0';
515 ptr = strrchrW(mi->source, '\\');
516 if (!ptr)
517 ptr = mi->source;
519 src = msi_realloc(src, (lstrlenW(src) + lstrlenW(ptr)) * sizeof(WCHAR));
520 if (!src)
521 return ERROR_OUTOFMEMORY;
523 lstrcatW(src, ptr + 1);
525 temppath[0] = '\0';
526 cab = msi_download_file(src, temppath);
527 lstrcpyW(mi->source, cab);
529 msi_free(src);
530 return ERROR_SUCCESS;
533 static UINT load_media_info(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
535 MSIRECORD *row;
536 LPWSTR source_dir;
537 UINT r;
539 static const WCHAR query[] = {
540 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
541 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
542 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
543 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
544 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
545 '`','D','i','s','k','I','d','`',0
548 row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
549 if (!row)
551 TRACE("Unable to query row\n");
552 return ERROR_FUNCTION_FAILED;
555 mi->is_extracted = FALSE;
556 mi->disk_id = MSI_RecordGetInteger(row, 1);
557 mi->last_sequence = MSI_RecordGetInteger(row, 2);
558 msi_free(mi->disk_prompt);
559 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
560 msi_free(mi->cabinet);
561 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
562 msi_free(mi->volume_label);
563 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
564 msiobj_release(&row->hdr);
566 source_dir = msi_dup_property(package, cszSourceDir);
568 if (mi->cabinet && mi->cabinet[0] == '#')
570 r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
571 if (r != ERROR_SUCCESS)
573 ERR("Failed to extract cabinet stream\n");
574 return ERROR_FUNCTION_FAILED;
577 else
579 lstrcpyW(mi->source, source_dir);
582 if (mi->cabinet)
583 lstrcatW(mi->source, mi->cabinet);
586 msi_package_add_media_disk(package, MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT,
587 mi->disk_id, mi->volume_label, mi->disk_prompt);
589 msi_package_add_info(package, MSIINSTALLCONTEXT_USERMANAGED,
590 MSICODE_PRODUCT | MSISOURCETYPE_MEDIA,
591 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->source);
593 msi_free(source_dir);
594 return ERROR_SUCCESS;
597 static UINT ready_media(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
599 UINT rc = ERROR_SUCCESS;
601 /* media info for continuous cabinet is already loaded */
602 if (mi->is_continuous)
603 return ERROR_SUCCESS;
605 rc = load_media_info(package, file, mi);
606 if (rc != ERROR_SUCCESS)
608 ERR("Unable to load media info\n");
609 return ERROR_FUNCTION_FAILED;
612 /* package should be downloaded */
613 if (file->IsCompressed &&
614 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
615 package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
617 return download_remote_cabinet(package, mi);
620 /* check volume matches, change media if not */
621 if (mi->volume_label && mi->disk_id > 1)
623 LPWSTR source = msi_dup_property(package, cszSourceDir);
624 BOOL matches;
625 UINT type;
627 PathStripToRootW(source);
628 type = GetDriveTypeW(source);
629 matches = source_matches_volume(mi, source);
630 msi_free(source);
632 if ((type == DRIVE_CDROM || type == DRIVE_REMOVABLE) && !matches)
634 rc = msi_change_media(package, mi);
635 if (rc != ERROR_SUCCESS)
636 return rc;
640 if (file->IsCompressed &&
641 mi->cabinet && mi->cabinet[0] != '#' &&
642 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
644 ERR("Cabinet not found: %s\n", debugstr_w(mi->source));
645 return ERROR_INSTALL_FAILURE;
648 return ERROR_SUCCESS;
651 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
652 MSIFILE** file)
654 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
656 if (lstrcmpW( file_key, (*file)->File )==0)
658 if ((*file)->state >= msifs_overwrite)
659 return ERROR_SUCCESS;
660 else
661 return ERROR_FILE_NOT_FOUND;
665 return ERROR_FUNCTION_FAILED;
668 static void schedule_install_files(MSIPACKAGE *package)
670 MSIFILE *file;
672 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
674 if (!ACTION_VerifyComponentForAction(file->Component, INSTALLSTATE_LOCAL))
676 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
678 ui_progress(package,2,file->FileSize,0,0);
679 file->state = msifs_skipped;
684 static UINT copy_file(MSIFILE *file)
686 BOOL ret;
688 ret = CopyFileW(file->SourcePath, file->TargetPath, FALSE);
689 if (ret)
691 file->state = msifs_installed;
692 return ERROR_SUCCESS;
695 return GetLastError();
698 static UINT copy_install_file(MSIFILE *file)
700 UINT gle;
702 TRACE("Copying %s to %s\n", debugstr_w(file->SourcePath),
703 debugstr_w(file->TargetPath));
705 gle = copy_file(file);
706 if (gle == ERROR_SUCCESS)
707 return gle;
709 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
711 TRACE("overwriting existing file\n");
712 gle = ERROR_SUCCESS;
714 else if (gle == ERROR_FILE_NOT_FOUND)
716 /* FIXME: this needs to be tested, I'm pretty sure it fails */
717 TRACE("Source file not found\n");
718 gle = ERROR_SUCCESS;
720 else if (gle == ERROR_ACCESS_DENIED)
722 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
724 gle = copy_file(file);
725 TRACE("Overwriting existing file: %d\n", gle);
727 else if (!(file->Attributes & msidbFileAttributesVital))
729 TRACE("Ignoring error for nonvital\n");
730 gle = ERROR_SUCCESS;
733 return gle;
737 * ACTION_InstallFiles()
739 * For efficiency, this is done in two passes:
740 * 1) Correct all the TargetPaths and determine what files are to be installed.
741 * 2) Extract Cabinets and copy files.
743 UINT ACTION_InstallFiles(MSIPACKAGE *package)
745 struct media_info *mi;
746 UINT rc = ERROR_SUCCESS;
747 LPWSTR ptr;
748 MSIFILE *file;
750 /* increment progress bar each time action data is sent */
751 ui_progress(package,1,1,0,0);
753 /* handle the keys for the SourceList */
754 ptr = strrchrW(package->PackagePath,'\\');
755 if (ptr)
757 ptr++;
758 msi_package_add_info(package, MSIINSTALLCONTEXT_USERMANAGED,
759 MSICODE_PRODUCT, INSTALLPROPERTY_PACKAGENAMEW, ptr);
762 schedule_install_files(package);
765 * Despite MSDN specifying that the CreateFolders action
766 * should be called before InstallFiles, some installers don't
767 * do that, and they seem to work correctly. We need to create
768 * directories here to make sure that the files can be copied.
770 msi_create_component_directories( package );
772 mi = msi_alloc_zero( sizeof(struct media_info) );
774 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
776 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
777 continue;
779 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
780 (file->IsCompressed && !mi->is_extracted))
782 rc = ready_media(package, file, mi);
783 if (rc != ERROR_SUCCESS)
785 ERR("Failed to ready media\n");
786 break;
789 if (file->IsCompressed && !extract_cabinet_file(package, mi))
791 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
792 rc = ERROR_FUNCTION_FAILED;
793 break;
797 set_file_source(package, file, mi->source);
799 TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
800 debugstr_w(file->TargetPath));
802 if (!file->IsCompressed)
804 msi_file_update_ui(package, file, szInstallFiles);
805 rc = copy_install_file(file);
806 if (rc != ERROR_SUCCESS)
808 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(file->SourcePath),
809 debugstr_w(file->TargetPath), rc);
810 rc = ERROR_INSTALL_FAILURE;
811 break;
814 else if (file->state != msifs_installed)
816 ERR("compressed file wasn't extracted (%s)\n", debugstr_w(file->TargetPath));
817 rc = ERROR_INSTALL_FAILURE;
818 break;
822 free_media_info( mi );
823 return rc;
826 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
828 MSIPACKAGE *package = (MSIPACKAGE*)param;
829 WCHAR dest_name[0x100];
830 LPWSTR dest_path, dest;
831 LPCWSTR file_key, component;
832 DWORD sz;
833 DWORD rc;
834 MSICOMPONENT *comp;
835 MSIFILE *file;
837 component = MSI_RecordGetString(row,2);
838 comp = get_loaded_component(package,component);
840 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
842 TRACE("Skipping copy due to disabled component %s\n",
843 debugstr_w(component));
845 /* the action taken was the same as the current install state */
846 comp->Action = comp->Installed;
848 return ERROR_SUCCESS;
851 comp->Action = INSTALLSTATE_LOCAL;
853 file_key = MSI_RecordGetString(row,3);
854 if (!file_key)
856 ERR("Unable to get file key\n");
857 return ERROR_FUNCTION_FAILED;
860 rc = get_file_target(package,file_key,&file);
862 if (rc != ERROR_SUCCESS)
864 ERR("Original file unknown %s\n",debugstr_w(file_key));
865 return ERROR_SUCCESS;
868 if (MSI_RecordIsNull(row,4))
869 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
870 else
872 sz=0x100;
873 MSI_RecordGetStringW(row,4,dest_name,&sz);
874 reduce_to_longfilename(dest_name);
877 if (MSI_RecordIsNull(row,5))
879 LPWSTR p;
880 dest_path = strdupW(file->TargetPath);
881 p = strrchrW(dest_path,'\\');
882 if (p)
883 *p=0;
885 else
887 LPCWSTR destkey;
888 destkey = MSI_RecordGetString(row,5);
889 dest_path = resolve_folder(package, destkey, FALSE, FALSE, TRUE, NULL);
890 if (!dest_path)
892 /* try a Property */
893 dest_path = msi_dup_property( package, destkey );
894 if (!dest_path)
896 FIXME("Unable to get destination folder, try AppSearch properties\n");
897 return ERROR_SUCCESS;
902 dest = build_directory_name(2, dest_path, dest_name);
904 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
905 debugstr_w(dest));
907 CreateDirectoryW(dest_path, NULL);
909 if (strcmpW(file->TargetPath,dest))
910 rc = !CopyFileW(file->TargetPath,dest,TRUE);
911 else
912 rc = ERROR_SUCCESS;
914 if (rc != ERROR_SUCCESS)
915 ERR("Failed to copy file %s -> %s, last error %d\n",
916 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
918 FIXME("We should track these duplicate files as well\n");
920 msi_free(dest_path);
921 msi_free(dest);
923 msi_file_update_ui(package, file, szDuplicateFiles);
925 return ERROR_SUCCESS;
928 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
930 UINT rc;
931 MSIQUERY * view;
932 static const WCHAR ExecSeqQuery[] =
933 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
934 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
936 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
937 if (rc != ERROR_SUCCESS)
938 return ERROR_SUCCESS;
940 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
941 msiobj_release(&view->hdr);
943 return rc;
946 /* compares the version of a file read from the filesystem and
947 * the version specified in the File table
949 static int msi_compare_file_version( MSIFILE *file )
951 WCHAR version[MAX_PATH];
952 DWORD size;
953 UINT r;
955 size = MAX_PATH;
956 version[0] = '\0';
957 r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
958 if ( r != ERROR_SUCCESS )
959 return 0;
961 return lstrcmpW( version, file->Version );
964 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
966 MSIFILE *file;
968 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
970 MSIRECORD *uirow;
971 LPWSTR uipath, p;
973 if ( !file->Component )
974 continue;
975 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
976 continue;
978 if ( file->state == msifs_installed )
979 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
981 if ( file->state != msifs_present )
982 continue;
984 /* only remove a file if the version to be installed
985 * is strictly newer than the old file
987 if ( msi_compare_file_version( file ) >= 0 )
988 continue;
990 TRACE("removing %s\n", debugstr_w(file->File) );
991 if ( !DeleteFileW( file->TargetPath ) )
992 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
993 file->state = msifs_missing;
995 /* the UI chunk */
996 uirow = MSI_CreateRecord( 9 );
997 MSI_RecordSetStringW( uirow, 1, file->FileName );
998 uipath = strdupW( file->TargetPath );
999 p = strrchrW(uipath,'\\');
1000 if (p)
1001 p[1]=0;
1002 MSI_RecordSetStringW( uirow, 9, uipath);
1003 ui_actiondata( package, szRemoveFiles, uirow);
1004 msiobj_release( &uirow->hdr );
1005 msi_free( uipath );
1006 /* FIXME: call ui_progress here? */
1009 return ERROR_SUCCESS;