Release 0.9.39.
[wine/gsoc-2012-control.git] / dlls / msi / files.c
blob42f954cc9341ca2ae27672b362f975a052c86847
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));
286 ptr = strrchrW(mi->source, '\\') + 1;
287 lstrcpyW(ptr, mi->cabinet);
288 msiobj_release(&row->hdr);
290 return ERROR_SUCCESS;
293 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
295 TRACE("(%d)\n", fdint);
297 switch (fdint)
299 case fdintPARTIAL_FILE:
301 CabData *data = (CabData *)pfdin->pv;
302 data->mi->is_continuous = FALSE;
303 return 0;
305 case fdintNEXT_CABINET:
307 CabData *data = (CabData *)pfdin->pv;
308 struct media_info *mi = data->mi;
309 LPWSTR cab = strdupAtoW(pfdin->psz1);
310 UINT rc;
312 msi_free(mi->disk_prompt);
314 mi->disk_id++;
315 mi->is_continuous = TRUE;
317 rc = msi_media_get_disk_info(data->package, mi);
318 if (rc != ERROR_SUCCESS)
320 ERR("Failed to get next cabinet information: %d\n", rc);
321 return -1;
324 if (lstrcmpiW(mi->cabinet, cab))
326 msi_free(cab);
327 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
328 return -1;
331 msi_free(cab);
333 TRACE("Searching for %s\n", debugstr_w(mi->source));
335 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
336 rc = msi_change_media(data->package, mi);
338 if (rc != ERROR_SUCCESS)
339 return -1;
341 return 0;
343 case fdintCOPY_FILE:
345 CabData *data = (CabData*) pfdin->pv;
346 HANDLE handle;
347 LPWSTR file;
348 MSIFILE *f;
349 DWORD attrs;
351 file = strdupAtoW(pfdin->psz1);
352 f = get_loaded_file(data->package, file);
353 msi_free(file);
355 if (!f)
357 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
358 return 0;
361 if (f->state != msifs_missing && f->state != msifs_overwrite)
363 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
364 return 0;
367 msi_file_update_ui( data->package, f, szInstallFiles );
369 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
371 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
372 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
374 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
375 NULL, CREATE_ALWAYS, attrs, NULL );
376 if ( handle == INVALID_HANDLE_VALUE )
378 if ( GetFileAttributesW( f->TargetPath ) != INVALID_FILE_ATTRIBUTES )
379 f->state = msifs_installed;
380 else
381 ERR("failed to create %s (error %d)\n",
382 debugstr_w( f->TargetPath ), GetLastError() );
384 return 0;
387 f->state = msifs_installed;
388 return (INT_PTR) handle;
390 case fdintCLOSE_FILE_INFO:
392 FILETIME ft;
393 FILETIME ftLocal;
394 HANDLE handle = (HANDLE) pfdin->hf;
396 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
397 return -1;
398 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
399 return -1;
400 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
401 return -1;
402 CloseHandle(handle);
403 return 1;
405 default:
406 return 0;
410 /***********************************************************************
411 * extract_cabinet_file
413 * Extract files from a cab file.
415 static BOOL extract_cabinet_file(MSIPACKAGE* package, struct media_info *mi)
417 LPSTR cabinet, cab_path = NULL;
418 LPWSTR ptr;
419 HFDI hfdi;
420 ERF erf;
421 BOOL ret = FALSE;
422 CabData data;
424 TRACE("Extracting %s\n", debugstr_w(mi->source));
426 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
427 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
428 if (!hfdi)
430 ERR("FDICreate failed\n");
431 return FALSE;
434 ptr = strrchrW(mi->source, '\\') + 1;
435 cabinet = strdupWtoA(ptr);
436 if (!cabinet)
437 goto done;
439 cab_path = strdupWtoA(mi->source);
440 if (!cab_path)
441 goto done;
443 cab_path[ptr - mi->source] = '\0';
445 data.package = package;
446 data.mi = mi;
448 ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, &data);
449 if (!ret)
450 ERR("FDICopy failed\n");
452 done:
453 FDIDestroy(hfdi);
454 msi_free(cabinet);
455 msi_free(cab_path);
457 if (ret)
458 mi->is_extracted = TRUE;
460 return ret;
463 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, LPCWSTR path)
465 if (!file->IsCompressed)
467 LPWSTR p, path;
468 p = resolve_folder(package, file->Component->Directory, TRUE, FALSE, TRUE, NULL);
469 path = build_directory_name(2, p, file->ShortName);
470 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
472 msi_free(path);
473 path = build_directory_name(2, p, file->LongName);
475 file->SourcePath = path;
476 msi_free(p);
478 else
479 file->SourcePath = build_directory_name(2, path, file->File);
482 static void free_media_info( struct media_info *mi )
484 msi_free( mi->disk_prompt );
485 msi_free( mi->cabinet );
486 msi_free( mi->volume_label );
487 msi_free( mi );
490 static UINT download_remote_cabinet(MSIPACKAGE *package, struct media_info *mi)
492 WCHAR temppath[MAX_PATH];
493 LPWSTR src, ptr;
494 LPCWSTR cab;
496 src = strdupW(package->BaseURL);
497 if (!src)
498 return ERROR_OUTOFMEMORY;
500 ptr = strrchrW(src, '/');
501 if (!ptr)
503 msi_free(src);
504 return ERROR_FUNCTION_FAILED;
507 *(ptr + 1) = '\0';
508 ptr = strrchrW(mi->source, '\\');
509 if (!ptr)
510 ptr = mi->source;
512 src = msi_realloc(src, (lstrlenW(src) + lstrlenW(ptr)) * sizeof(WCHAR));
513 if (!src)
514 return ERROR_OUTOFMEMORY;
516 lstrcatW(src, ptr + 1);
518 temppath[0] = '\0';
519 cab = msi_download_file(src, temppath);
520 lstrcpyW(mi->source, cab);
522 msi_free(src);
523 return ERROR_SUCCESS;
526 static UINT load_media_info(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
528 MSIRECORD *row;
529 LPWSTR source_dir;
530 UINT r;
532 static const WCHAR query[] = {
533 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
534 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
535 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
536 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
537 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
538 '`','D','i','s','k','I','d','`',0
541 row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
542 if (!row)
544 TRACE("Unable to query row\n");
545 return ERROR_FUNCTION_FAILED;
548 mi->is_extracted = FALSE;
549 mi->disk_id = MSI_RecordGetInteger(row, 1);
550 mi->last_sequence = MSI_RecordGetInteger(row, 2);
551 msi_free(mi->disk_prompt);
552 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
553 msi_free(mi->cabinet);
554 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
555 msi_free(mi->volume_label);
556 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
557 msiobj_release(&row->hdr);
559 source_dir = msi_dup_property(package, cszSourceDir);
561 if (mi->cabinet && mi->cabinet[0] == '#')
563 r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
564 if (r != ERROR_SUCCESS)
566 ERR("Failed to extract cabinet stream\n");
567 return ERROR_FUNCTION_FAILED;
570 else
572 lstrcpyW(mi->source, source_dir);
575 if (mi->cabinet)
576 lstrcatW(mi->source, mi->cabinet);
579 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
580 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT,
581 mi->disk_id, mi->volume_label, mi->disk_prompt);
583 MsiSourceListSetInfoW(package->ProductCode, NULL,
584 MSIINSTALLCONTEXT_USERMANAGED,
585 MSICODE_PRODUCT | MSISOURCETYPE_MEDIA,
586 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->source);
588 msi_free(source_dir);
589 return ERROR_SUCCESS;
592 static UINT ready_media(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
594 UINT rc = ERROR_SUCCESS, type;
595 BOOL found = TRUE;
596 LPWSTR source_dir;
598 /* media info for continuous cabinet is already loaded */
599 if (mi->is_continuous)
600 return ERROR_SUCCESS;
602 rc = load_media_info(package, file, mi);
603 if (rc != ERROR_SUCCESS)
605 ERR("Unable to load media info\n");
606 return ERROR_FUNCTION_FAILED;
609 if (mi->volume_label && mi->disk_id > 1)
611 source_dir = msi_dup_property(package, cszSourceDir);
612 PathStripToRootW(source_dir);
613 type = GetDriveTypeW(source_dir);
615 if (type == DRIVE_CDROM || type == DRIVE_REMOVABLE)
616 found = source_matches_volume(mi, source_dir);
618 if (!found)
619 found = GetFileAttributesW(mi->cabinet) != INVALID_FILE_ATTRIBUTES;
621 msi_free(source_dir);
624 if (file->IsCompressed &&
625 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
627 found = FALSE;
629 if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
631 rc = download_remote_cabinet(package, mi);
632 if (rc == ERROR_SUCCESS &&
633 GetFileAttributesW(mi->source) != INVALID_FILE_ATTRIBUTES)
635 found = TRUE;
640 if (!found)
641 rc = msi_change_media(package, mi);
643 return rc;
646 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
647 MSIFILE** file)
649 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
651 if (lstrcmpW( file_key, (*file)->File )==0)
653 if ((*file)->state >= msifs_overwrite)
654 return ERROR_SUCCESS;
655 else
656 return ERROR_FILE_NOT_FOUND;
660 return ERROR_FUNCTION_FAILED;
663 static void schedule_install_files(MSIPACKAGE *package)
665 MSIFILE *file;
667 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
669 if (!ACTION_VerifyComponentForAction(file->Component, INSTALLSTATE_LOCAL))
671 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
673 ui_progress(package,2,file->FileSize,0,0);
674 file->state = msifs_skipped;
679 static UINT copy_file(MSIFILE *file)
681 BOOL ret;
683 ret = CopyFileW(file->SourcePath, file->TargetPath, FALSE);
684 if (ret)
686 file->state = msifs_installed;
687 return ERROR_SUCCESS;
690 return GetLastError();
693 static UINT copy_install_file(MSIFILE *file)
695 UINT gle;
697 TRACE("Copying %s to %s\n", debugstr_w(file->SourcePath),
698 debugstr_w(file->TargetPath));
700 gle = copy_file(file);
701 if (gle == ERROR_SUCCESS)
702 return gle;
704 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
706 TRACE("overwriting existing file\n");
707 gle = ERROR_SUCCESS;
709 else if (gle == ERROR_FILE_NOT_FOUND)
711 /* FIXME: this needs to be tested, I'm pretty sure it fails */
712 TRACE("Source file not found\n");
713 gle = ERROR_SUCCESS;
715 else if (gle == ERROR_ACCESS_DENIED)
717 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
719 gle = copy_file(file);
720 TRACE("Overwriting existing file: %d\n", gle);
722 else if (!(file->Attributes & msidbFileAttributesVital))
724 TRACE("Ignoring error for nonvital\n");
725 gle = ERROR_SUCCESS;
728 return gle;
732 * ACTION_InstallFiles()
734 * For efficiency, this is done in two passes:
735 * 1) Correct all the TargetPaths and determine what files are to be installed.
736 * 2) Extract Cabinets and copy files.
738 UINT ACTION_InstallFiles(MSIPACKAGE *package)
740 struct media_info *mi;
741 UINT rc = ERROR_SUCCESS;
742 LPWSTR ptr;
743 MSIFILE *file;
745 /* increment progress bar each time action data is sent */
746 ui_progress(package,1,1,0,0);
748 /* handle the keys for the SourceList */
749 ptr = strrchrW(package->PackagePath,'\\');
750 if (ptr)
752 ptr++;
753 MsiSourceListSetInfoW(package->ProductCode, NULL,
754 MSIINSTALLCONTEXT_USERMANAGED,
755 MSICODE_PRODUCT,
756 INSTALLPROPERTY_PACKAGENAMEW, ptr);
759 schedule_install_files(package);
762 * Despite MSDN specifying that the CreateFolders action
763 * should be called before InstallFiles, some installers don't
764 * do that, and they seem to work correctly. We need to create
765 * directories here to make sure that the files can be copied.
767 msi_create_component_directories( package );
769 mi = msi_alloc_zero( sizeof(struct media_info) );
771 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
773 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
774 continue;
776 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
777 (file->IsCompressed && !mi->is_extracted))
779 rc = ready_media(package, file, mi);
780 if (rc != ERROR_SUCCESS)
782 ERR("Failed to ready media\n");
783 rc = ERROR_FUNCTION_FAILED;
784 break;
787 if (file->IsCompressed && !extract_cabinet_file(package, mi))
789 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
790 rc = ERROR_FUNCTION_FAILED;
791 break;
795 set_file_source(package, file, mi->source);
797 TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
798 debugstr_w(file->TargetPath));
800 if (!file->IsCompressed)
802 msi_file_update_ui(package, file, szInstallFiles);
803 rc = copy_install_file(file);
804 if (rc != ERROR_SUCCESS)
806 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(file->SourcePath),
807 debugstr_w(file->TargetPath), rc);
808 rc = ERROR_INSTALL_FAILURE;
809 break;
812 else if (file->state != msifs_installed)
814 ERR("compressed file wasn't extracted (%s)\n", debugstr_w(file->TargetPath));
815 rc = ERROR_INSTALL_FAILURE;
816 break;
820 free_media_info( mi );
821 return rc;
824 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
826 MSIPACKAGE *package = (MSIPACKAGE*)param;
827 WCHAR dest_name[0x100];
828 LPWSTR dest_path, dest;
829 LPCWSTR file_key, component;
830 DWORD sz;
831 DWORD rc;
832 MSICOMPONENT *comp;
833 MSIFILE *file;
835 component = MSI_RecordGetString(row,2);
836 comp = get_loaded_component(package,component);
838 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
840 TRACE("Skipping copy due to disabled component %s\n",
841 debugstr_w(component));
843 /* the action taken was the same as the current install state */
844 comp->Action = comp->Installed;
846 return ERROR_SUCCESS;
849 comp->Action = INSTALLSTATE_LOCAL;
851 file_key = MSI_RecordGetString(row,3);
852 if (!file_key)
854 ERR("Unable to get file key\n");
855 return ERROR_FUNCTION_FAILED;
858 rc = get_file_target(package,file_key,&file);
860 if (rc != ERROR_SUCCESS)
862 ERR("Original file unknown %s\n",debugstr_w(file_key));
863 return ERROR_SUCCESS;
866 if (MSI_RecordIsNull(row,4))
867 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
868 else
870 sz=0x100;
871 MSI_RecordGetStringW(row,4,dest_name,&sz);
872 reduce_to_longfilename(dest_name);
875 if (MSI_RecordIsNull(row,5))
877 LPWSTR p;
878 dest_path = strdupW(file->TargetPath);
879 p = strrchrW(dest_path,'\\');
880 if (p)
881 *p=0;
883 else
885 LPCWSTR destkey;
886 destkey = MSI_RecordGetString(row,5);
887 dest_path = resolve_folder(package, destkey, FALSE, FALSE, TRUE, NULL);
888 if (!dest_path)
890 /* try a Property */
891 dest_path = msi_dup_property( package, destkey );
892 if (!dest_path)
894 FIXME("Unable to get destination folder, try AppSearch properties\n");
895 return ERROR_SUCCESS;
900 dest = build_directory_name(2, dest_path, dest_name);
902 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
903 debugstr_w(dest));
905 CreateDirectoryW(dest_path, NULL);
907 if (strcmpW(file->TargetPath,dest))
908 rc = !CopyFileW(file->TargetPath,dest,TRUE);
909 else
910 rc = ERROR_SUCCESS;
912 if (rc != ERROR_SUCCESS)
913 ERR("Failed to copy file %s -> %s, last error %d\n",
914 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
916 FIXME("We should track these duplicate files as well\n");
918 msi_free(dest_path);
919 msi_free(dest);
921 msi_file_update_ui(package, file, szDuplicateFiles);
923 return ERROR_SUCCESS;
926 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
928 UINT rc;
929 MSIQUERY * view;
930 static const WCHAR ExecSeqQuery[] =
931 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
932 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
934 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
935 if (rc != ERROR_SUCCESS)
936 return ERROR_SUCCESS;
938 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
939 msiobj_release(&view->hdr);
941 return rc;
944 /* compares the version of a file read from the filesystem and
945 * the version specified in the File table
947 static int msi_compare_file_version( MSIFILE *file )
949 WCHAR version[MAX_PATH];
950 DWORD size;
951 UINT r;
953 size = MAX_PATH;
954 version[0] = '\0';
955 r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
956 if ( r != ERROR_SUCCESS )
957 return 0;
959 return lstrcmpW( version, file->Version );
962 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
964 MSIFILE *file;
966 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
968 MSIRECORD *uirow;
969 LPWSTR uipath, p;
971 if ( !file->Component )
972 continue;
973 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
974 continue;
976 if ( file->state == msifs_installed )
977 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
979 if ( file->state != msifs_present )
980 continue;
982 /* only remove a file if the version to be installed
983 * is strictly newer than the old file
985 if ( msi_compare_file_version( file ) >= 0 )
986 continue;
988 TRACE("removing %s\n", debugstr_w(file->File) );
989 if ( !DeleteFileW( file->TargetPath ) )
990 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
991 file->state = msifs_missing;
993 /* the UI chunk */
994 uirow = MSI_CreateRecord( 9 );
995 MSI_RecordSetStringW( uirow, 1, file->FileName );
996 uipath = strdupW( file->TargetPath );
997 p = strrchrW(uipath,'\\');
998 if (p)
999 p[1]=0;
1000 MSI_RecordSetStringW( uirow, 9, uipath);
1001 ui_actiondata( package, szRemoveFiles, uirow);
1002 msiobj_release( &uirow->hdr );
1003 msi_free( uipath );
1004 /* FIXME: call ui_progress here? */
1007 return ERROR_SUCCESS;