ntdll: Avoid inter-process APCs when called for the process itself.
[wine/multimedia.git] / dlls / msi / files.c
bloba4819fc1d12c9ee10f164b5f0d19a498df7e6a65
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 WCHAR source[MAX_PATH];
70 static UINT msi_change_media( MSIPACKAGE *package, struct media_info *mi )
72 LPSTR msg;
73 LPWSTR error, error_dialog;
74 UINT r = ERROR_SUCCESS;
76 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
77 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
79 if ( msi_get_property_int(package, szUILevel, 0) == INSTALLUILEVEL_NONE && !gUIHandlerA )
80 return ERROR_SUCCESS;
82 error = generate_error_string( package, 1302, 1, mi->disk_prompt );
83 error_dialog = msi_dup_property( package, error_prop );
85 while ( r == ERROR_SUCCESS && GetFileAttributesW( mi->source ) == INVALID_FILE_ATTRIBUTES )
87 r = msi_spawn_error_dialog( package, error_dialog, error );
89 if (gUIHandlerA)
91 msg = strdupWtoA( error );
92 gUIHandlerA( gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg );
93 msi_free(msg);
97 msi_free( error );
98 msi_free( error_dialog );
100 return r;
104 * This is a helper function for handling embedded cabinet media
106 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
107 WCHAR* source)
109 UINT rc;
110 USHORT* data;
111 UINT size;
112 DWORD write;
113 HANDLE the_file;
114 WCHAR tmp[MAX_PATH];
116 rc = read_raw_stream_data(package->db,stream_name,&data,&size);
117 if (rc != ERROR_SUCCESS)
118 return rc;
120 write = MAX_PATH;
121 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
122 GetTempPathW(MAX_PATH,tmp);
124 GetTempFileNameW(tmp,stream_name,0,source);
126 track_tempfile(package, source);
127 the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
128 FILE_ATTRIBUTE_NORMAL, NULL);
130 if (the_file == INVALID_HANDLE_VALUE)
132 ERR("Unable to create file %s\n",debugstr_w(source));
133 rc = ERROR_FUNCTION_FAILED;
134 goto end;
137 WriteFile(the_file,data,size,&write,NULL);
138 CloseHandle(the_file);
139 TRACE("wrote %i bytes to %s\n",write,debugstr_w(source));
140 end:
141 msi_free(data);
142 return rc;
146 /* Support functions for FDI functions */
147 typedef struct
149 MSIPACKAGE* package;
150 struct media_info *mi;
151 } CabData;
153 static void * cabinet_alloc(ULONG cb)
155 return msi_alloc(cb);
158 static void cabinet_free(void *pv)
160 msi_free(pv);
163 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
165 HANDLE handle;
166 DWORD dwAccess = 0;
167 DWORD dwShareMode = 0;
168 DWORD dwCreateDisposition = OPEN_EXISTING;
169 switch (oflag & _O_ACCMODE)
171 case _O_RDONLY:
172 dwAccess = GENERIC_READ;
173 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
174 break;
175 case _O_WRONLY:
176 dwAccess = GENERIC_WRITE;
177 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
178 break;
179 case _O_RDWR:
180 dwAccess = GENERIC_READ | GENERIC_WRITE;
181 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
182 break;
184 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
185 dwCreateDisposition = CREATE_NEW;
186 else if (oflag & _O_CREAT)
187 dwCreateDisposition = CREATE_ALWAYS;
188 handle = CreateFileA( pszFile, dwAccess, dwShareMode, NULL,
189 dwCreateDisposition, 0, NULL );
190 if (handle == INVALID_HANDLE_VALUE)
191 return 0;
192 return (INT_PTR) handle;
195 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
197 HANDLE handle = (HANDLE) hf;
198 DWORD dwRead;
199 if (ReadFile(handle, pv, cb, &dwRead, NULL))
200 return dwRead;
201 return 0;
204 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
206 HANDLE handle = (HANDLE) hf;
207 DWORD dwWritten;
208 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
209 return dwWritten;
210 return 0;
213 static int cabinet_close(INT_PTR hf)
215 HANDLE handle = (HANDLE) hf;
216 return CloseHandle(handle) ? 0 : -1;
219 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
221 HANDLE handle = (HANDLE) hf;
222 /* flags are compatible and so are passed straight through */
223 return SetFilePointer(handle, dist, NULL, seektype);
226 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
228 MSIRECORD *uirow;
229 LPWSTR uipath, p;
231 /* the UI chunk */
232 uirow = MSI_CreateRecord( 9 );
233 MSI_RecordSetStringW( uirow, 1, f->FileName );
234 uipath = strdupW( f->TargetPath );
235 p = strrchrW(uipath,'\\');
236 if (p)
237 p[1]=0;
238 MSI_RecordSetStringW( uirow, 9, uipath);
239 MSI_RecordSetInteger( uirow, 6, f->FileSize );
240 ui_actiondata( package, action, uirow);
241 msiobj_release( &uirow->hdr );
242 msi_free( uipath );
243 ui_progress( package, 2, f->FileSize, 0, 0);
246 static UINT msi_media_get_disk_info( MSIPACKAGE *package, struct media_info *mi )
248 MSIRECORD *row;
249 LPWSTR ptr;
251 static const WCHAR query[] =
252 {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
253 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
254 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
256 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
257 if (!row)
259 TRACE("Unable to query row\n");
260 return ERROR_FUNCTION_FAILED;
263 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
264 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
266 ptr = strrchrW(mi->source, '\\') + 1;
267 lstrcpyW(ptr, mi->cabinet);
268 msiobj_release(&row->hdr);
270 return ERROR_SUCCESS;
273 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
275 TRACE("(%d)\n", fdint);
277 switch (fdint)
279 case fdintPARTIAL_FILE:
281 CabData *data = (CabData *)pfdin->pv;
282 data->mi->is_continuous = FALSE;
283 return 0;
285 case fdintNEXT_CABINET:
287 CabData *data = (CabData *)pfdin->pv;
288 struct media_info *mi = data->mi;
289 LPWSTR cab = strdupAtoW(pfdin->psz1);
290 UINT rc;
292 msi_free(mi->disk_prompt);
294 mi->disk_id++;
295 mi->is_continuous = TRUE;
297 rc = msi_media_get_disk_info(data->package, mi);
298 if (rc != ERROR_SUCCESS)
300 ERR("Failed to get next cabinet information: %d\n", rc);
301 return -1;
304 if (lstrcmpiW(mi->cabinet, cab))
306 msi_free(cab);
307 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
308 return -1;
311 msi_free(cab);
313 TRACE("Searching for %s\n", debugstr_w(mi->source));
315 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
316 rc = msi_change_media(data->package, mi);
318 if (rc != ERROR_SUCCESS)
319 return -1;
321 return 0;
323 case fdintCOPY_FILE:
325 CabData *data = (CabData*) pfdin->pv;
326 HANDLE handle;
327 LPWSTR file;
328 MSIFILE *f;
329 DWORD attrs;
331 file = strdupAtoW(pfdin->psz1);
332 f = get_loaded_file(data->package, file);
333 msi_free(file);
335 if (!f)
337 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
338 return 0;
341 if (f->state != msifs_missing && f->state != msifs_overwrite)
343 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
344 return 0;
347 msi_file_update_ui( data->package, f, szInstallFiles );
349 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
351 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
352 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
354 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
355 NULL, CREATE_ALWAYS, attrs, NULL );
356 if ( handle == INVALID_HANDLE_VALUE )
358 if ( GetFileAttributesW( f->TargetPath ) != INVALID_FILE_ATTRIBUTES )
359 f->state = msifs_installed;
360 else
361 ERR("failed to create %s (error %d)\n",
362 debugstr_w( f->TargetPath ), GetLastError() );
364 return 0;
367 f->state = msifs_installed;
368 return (INT_PTR) handle;
370 case fdintCLOSE_FILE_INFO:
372 FILETIME ft;
373 FILETIME ftLocal;
374 HANDLE handle = (HANDLE) pfdin->hf;
376 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
377 return -1;
378 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
379 return -1;
380 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
381 return -1;
382 CloseHandle(handle);
383 return 1;
385 default:
386 return 0;
390 /***********************************************************************
391 * extract_cabinet_file
393 * Extract files from a cab file.
395 static BOOL extract_cabinet_file(MSIPACKAGE* package, struct media_info *mi)
397 LPSTR cabinet, cab_path = NULL;
398 LPWSTR ptr;
399 HFDI hfdi;
400 ERF erf;
401 BOOL ret = FALSE;
402 CabData data;
404 TRACE("Extracting %s\n", debugstr_w(mi->source));
406 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
407 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
408 if (!hfdi)
410 ERR("FDICreate failed\n");
411 return FALSE;
414 ptr = strrchrW(mi->source, '\\') + 1;
415 cabinet = strdupWtoA(ptr);
416 if (!cabinet)
417 goto done;
419 cab_path = strdupWtoA(mi->source);
420 if (!cab_path)
421 goto done;
423 cab_path[ptr - mi->source] = '\0';
425 data.package = package;
426 data.mi = mi;
428 ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, &data);
429 if (!ret)
430 ERR("FDICopy failed\n");
432 done:
433 FDIDestroy(hfdi);
434 msi_free(cabinet);
435 msi_free(cab_path);
437 return ret;
440 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, LPCWSTR path)
442 if (!file->IsCompressed)
444 LPWSTR p, path;
445 p = resolve_folder(package, file->Component->Directory, TRUE, FALSE, NULL);
446 path = build_directory_name(2, p, file->ShortName);
447 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
449 msi_free(path);
450 path = build_directory_name(2, p, file->LongName);
452 file->SourcePath = path;
453 msi_free(p);
455 else
456 file->SourcePath = build_directory_name(2, path, file->File);
459 static void free_media_info( struct media_info *mi )
461 msi_free( mi->disk_prompt );
462 msi_free( mi->cabinet );
463 msi_free( mi->volume_label );
464 msi_free( mi );
467 static UINT download_remote_cabinet(MSIPACKAGE *package, struct media_info *mi)
469 WCHAR temppath[MAX_PATH];
470 LPWSTR src, ptr;
471 LPCWSTR cab;
473 src = strdupW(package->BaseURL);
474 if (!src)
475 return ERROR_OUTOFMEMORY;
477 ptr = strrchrW(src, '/');
478 if (!ptr)
480 msi_free(src);
481 return ERROR_FUNCTION_FAILED;
484 *(ptr + 1) = '\0';
485 ptr = strrchrW(mi->source, '\\');
486 if (!ptr)
487 ptr = mi->source;
489 src = msi_realloc(src, (lstrlenW(src) + lstrlenW(ptr)) * sizeof(WCHAR));
490 if (!src)
491 return ERROR_OUTOFMEMORY;
493 lstrcatW(src, ptr + 1);
495 temppath[0] = '\0';
496 cab = msi_download_file(src, temppath);
497 lstrcpyW(mi->source, cab);
499 msi_free(src);
500 return ERROR_SUCCESS;
503 static UINT load_media_info(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
505 MSIRECORD *row;
506 LPWSTR source_dir;
507 UINT r;
509 static const WCHAR query[] = {
510 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
511 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
512 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
513 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
514 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
515 '`','D','i','s','k','I','d','`',0
518 row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
519 if (!row)
521 TRACE("Unable to query row\n");
522 return ERROR_FUNCTION_FAILED;
525 mi->disk_id = MSI_RecordGetInteger(row, 1);
526 mi->last_sequence = MSI_RecordGetInteger(row, 2);
527 msi_free(mi->disk_prompt);
528 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
529 msi_free(mi->cabinet);
530 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
531 msi_free(mi->volume_label);
532 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
533 msiobj_release(&row->hdr);
535 source_dir = msi_dup_property(package, cszSourceDir);
537 if (mi->cabinet && mi->cabinet[0] == '#')
539 r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
540 if (r != ERROR_SUCCESS)
542 ERR("Failed to extract cabinet stream\n");
543 return ERROR_FUNCTION_FAILED;
546 else
548 lstrcpyW(mi->source, source_dir);
551 if (mi->cabinet)
552 lstrcatW(mi->source, mi->cabinet);
555 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
556 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT,
557 mi->disk_id, mi->volume_label, mi->disk_prompt);
559 MsiSourceListSetInfoW(package->ProductCode, NULL,
560 MSIINSTALLCONTEXT_USERMANAGED,
561 MSICODE_PRODUCT | MSISOURCETYPE_MEDIA,
562 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->source);
564 msi_free(source_dir);
565 return ERROR_SUCCESS;
568 static UINT ready_media(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
570 UINT rc = ERROR_SUCCESS;
571 BOOL found = FALSE;
573 /* media info for continuous cabinet is already loaded */
574 if (mi->is_continuous)
575 return ERROR_SUCCESS;
577 rc = load_media_info(package, file, mi);
578 if (rc != ERROR_SUCCESS)
580 ERR("Unable to load media info\n");
581 return ERROR_FUNCTION_FAILED;
584 if (file->IsCompressed &&
585 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
587 if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
589 rc = download_remote_cabinet(package, mi);
590 if (rc == ERROR_SUCCESS &&
591 GetFileAttributesW(mi->source) != INVALID_FILE_ATTRIBUTES)
593 found = TRUE;
597 if (!found)
598 rc = msi_change_media(package, mi);
601 return rc;
604 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
605 MSIFILE** file)
607 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
609 if (lstrcmpW( file_key, (*file)->File )==0)
611 if ((*file)->state >= msifs_overwrite)
612 return ERROR_SUCCESS;
613 else
614 return ERROR_FILE_NOT_FOUND;
618 return ERROR_FUNCTION_FAILED;
621 static void schedule_install_files(MSIPACKAGE *package)
623 MSIFILE *file;
625 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
627 if (!ACTION_VerifyComponentForAction(file->Component, INSTALLSTATE_LOCAL))
629 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
631 ui_progress(package,2,file->FileSize,0,0);
632 file->state = msifs_skipped;
637 static UINT copy_install_file(MSIFILE *file)
639 BOOL ret;
640 UINT gle;
642 TRACE("Copying %s to %s\n", debugstr_w(file->SourcePath),
643 debugstr_w(file->TargetPath));
645 ret = CopyFileW(file->SourcePath, file->TargetPath, FALSE);
646 if (ret)
648 file->state = msifs_installed;
649 return ERROR_SUCCESS;
652 gle = GetLastError();
653 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
655 TRACE("overwriting existing file\n");
656 gle = ERROR_SUCCESS;
658 else if (gle == ERROR_FILE_NOT_FOUND)
660 /* FIXME: this needs to be tested, I'm pretty sure it fails */
661 TRACE("Source file not found\n");
662 gle = ERROR_SUCCESS;
664 else if (!(file->Attributes & msidbFileAttributesVital))
666 TRACE("Ignoring error for nonvital\n");
667 gle = ERROR_SUCCESS;
670 return gle;
674 * ACTION_InstallFiles()
676 * For efficiency, this is done in two passes:
677 * 1) Correct all the TargetPaths and determine what files are to be installed.
678 * 2) Extract Cabinets and copy files.
680 UINT ACTION_InstallFiles(MSIPACKAGE *package)
682 struct media_info *mi;
683 UINT rc = ERROR_SUCCESS;
684 LPWSTR ptr;
685 MSIFILE *file;
687 /* increment progress bar each time action data is sent */
688 ui_progress(package,1,1,0,0);
690 /* handle the keys for the SourceList */
691 ptr = strrchrW(package->PackagePath,'\\');
692 if (ptr)
694 ptr++;
695 MsiSourceListSetInfoW(package->ProductCode, NULL,
696 MSIINSTALLCONTEXT_USERMANAGED,
697 MSICODE_PRODUCT,
698 INSTALLPROPERTY_PACKAGENAMEW, ptr);
701 schedule_install_files(package);
704 * Despite MSDN specifying that the CreateFolders action
705 * should be called before InstallFiles, some installers don't
706 * do that, and they seem to work correctly. We need to create
707 * directories here to make sure that the files can be copied.
709 msi_create_component_directories( package );
711 mi = msi_alloc_zero( sizeof(struct media_info) );
713 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
715 if (file->state != msifs_missing && file->state != msifs_overwrite)
716 continue;
718 if (file->Sequence > mi->last_sequence || mi->is_continuous)
720 rc = ready_media(package, file, mi);
721 if (rc != ERROR_SUCCESS)
723 ERR("Failed to ready media\n");
724 rc = ERROR_FUNCTION_FAILED;
725 break;
728 if (file->IsCompressed && !extract_cabinet_file(package, mi))
730 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
731 rc = ERROR_FUNCTION_FAILED;
732 break;
736 set_file_source(package, file, mi->source);
738 TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
739 debugstr_w(file->TargetPath));
741 if (!file->IsCompressed)
743 rc = copy_install_file(file);
744 if (rc != ERROR_SUCCESS)
746 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(file->SourcePath),
747 debugstr_w(file->TargetPath), rc);
748 rc = ERROR_INSTALL_FAILURE;
749 break;
752 else if (file->state != msifs_installed)
754 ERR("compressed file wasn't extracted (%s)\n", debugstr_w(file->TargetPath));
755 rc = ERROR_INSTALL_FAILURE;
756 break;
760 free_media_info( mi );
761 return rc;
764 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
766 MSIPACKAGE *package = (MSIPACKAGE*)param;
767 WCHAR dest_name[0x100];
768 LPWSTR dest_path, dest;
769 LPCWSTR file_key, component;
770 DWORD sz;
771 DWORD rc;
772 MSICOMPONENT *comp;
773 MSIFILE *file;
775 component = MSI_RecordGetString(row,2);
776 comp = get_loaded_component(package,component);
778 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
780 TRACE("Skipping copy due to disabled component %s\n",
781 debugstr_w(component));
783 /* the action taken was the same as the current install state */
784 comp->Action = comp->Installed;
786 return ERROR_SUCCESS;
789 comp->Action = INSTALLSTATE_LOCAL;
791 file_key = MSI_RecordGetString(row,3);
792 if (!file_key)
794 ERR("Unable to get file key\n");
795 return ERROR_FUNCTION_FAILED;
798 rc = get_file_target(package,file_key,&file);
800 if (rc != ERROR_SUCCESS)
802 ERR("Original file unknown %s\n",debugstr_w(file_key));
803 return ERROR_SUCCESS;
806 if (MSI_RecordIsNull(row,4))
807 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
808 else
810 sz=0x100;
811 MSI_RecordGetStringW(row,4,dest_name,&sz);
812 reduce_to_longfilename(dest_name);
815 if (MSI_RecordIsNull(row,5))
817 LPWSTR p;
818 dest_path = strdupW(file->TargetPath);
819 p = strrchrW(dest_path,'\\');
820 if (p)
821 *p=0;
823 else
825 LPCWSTR destkey;
826 destkey = MSI_RecordGetString(row,5);
827 dest_path = resolve_folder(package, destkey, FALSE,FALSE,NULL);
828 if (!dest_path)
830 /* try a Property */
831 dest_path = msi_dup_property( package, destkey );
832 if (!dest_path)
834 FIXME("Unable to get destination folder, try AppSearch properties\n");
835 return ERROR_SUCCESS;
840 dest = build_directory_name(2, dest_path, dest_name);
842 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
843 debugstr_w(dest));
845 if (strcmpW(file->TargetPath,dest))
846 rc = !CopyFileW(file->TargetPath,dest,TRUE);
847 else
848 rc = ERROR_SUCCESS;
850 if (rc != ERROR_SUCCESS)
851 ERR("Failed to copy file %s -> %s, last error %d\n",
852 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
854 FIXME("We should track these duplicate files as well\n");
856 msi_free(dest_path);
857 msi_free(dest);
859 msi_file_update_ui(package, file, szDuplicateFiles);
861 return ERROR_SUCCESS;
864 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
866 UINT rc;
867 MSIQUERY * view;
868 static const WCHAR ExecSeqQuery[] =
869 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
870 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
872 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
873 if (rc != ERROR_SUCCESS)
874 return ERROR_SUCCESS;
876 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
877 msiobj_release(&view->hdr);
879 return rc;
882 /* compares the version of a file read from the filesystem and
883 * the version specified in the File table
885 static int msi_compare_file_version( MSIFILE *file )
887 WCHAR version[MAX_PATH];
888 DWORD size;
889 UINT r;
891 size = MAX_PATH;
892 version[0] = '\0';
893 r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
894 if ( r != ERROR_SUCCESS )
895 return 0;
897 return lstrcmpW( version, file->Version );
900 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
902 MSIFILE *file;
904 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
906 MSIRECORD *uirow;
907 LPWSTR uipath, p;
909 if ( !file->Component )
910 continue;
911 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
912 continue;
914 if ( file->state == msifs_installed )
915 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
917 if ( file->state != msifs_present )
918 continue;
920 /* only remove a file if the version to be installed
921 * is strictly newer than the old file
923 if ( msi_compare_file_version( file ) >= 0 )
924 continue;
926 TRACE("removing %s\n", debugstr_w(file->File) );
927 if ( !DeleteFileW( file->TargetPath ) )
928 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
929 file->state = msifs_missing;
931 /* the UI chunk */
932 uirow = MSI_CreateRecord( 9 );
933 MSI_RecordSetStringW( uirow, 1, file->FileName );
934 uipath = strdupW( file->TargetPath );
935 p = strrchrW(uipath,'\\');
936 if (p)
937 p[1]=0;
938 MSI_RecordSetStringW( uirow, 9, uipath);
939 ui_actiondata( package, szRemoveFiles, uirow);
940 msiobj_release( &uirow->hdr );
941 msi_free( uipath );
942 /* FIXME: call ui_progress here? */
945 return ERROR_SUCCESS;