msi: Use the file's component instead of passing an extra parameter to set_file_source.
[wine.git] / dlls / msi / files.c
blob3b6b2a978887817b3606b24410308437757bcf04
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 last_sequence;
62 LPWSTR last_volume;
63 LPWSTR last_path;
64 DWORD count;
65 WCHAR source[MAX_PATH];
69 * This is a helper function for handling embedded cabinet media
71 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
72 WCHAR* source)
74 UINT rc;
75 USHORT* data;
76 UINT size;
77 DWORD write;
78 HANDLE the_file;
79 WCHAR tmp[MAX_PATH];
81 rc = read_raw_stream_data(package->db,stream_name,&data,&size);
82 if (rc != ERROR_SUCCESS)
83 return rc;
85 write = MAX_PATH;
86 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
87 GetTempPathW(MAX_PATH,tmp);
89 GetTempFileNameW(tmp,stream_name,0,source);
91 track_tempfile(package,strrchrW(source,'\\'), source);
92 the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
93 FILE_ATTRIBUTE_NORMAL, NULL);
95 if (the_file == INVALID_HANDLE_VALUE)
97 ERR("Unable to create file %s\n",debugstr_w(source));
98 rc = ERROR_FUNCTION_FAILED;
99 goto end;
102 WriteFile(the_file,data,size,&write,NULL);
103 CloseHandle(the_file);
104 TRACE("wrote %i bytes to %s\n",write,debugstr_w(source));
105 end:
106 msi_free(data);
107 return rc;
111 /* Support functions for FDI functions */
112 typedef struct
114 MSIPACKAGE* package;
115 LPCSTR cab_path;
116 } CabData;
118 static void * cabinet_alloc(ULONG cb)
120 return msi_alloc(cb);
123 static void cabinet_free(void *pv)
125 msi_free(pv);
128 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
130 HANDLE handle;
131 DWORD dwAccess = 0;
132 DWORD dwShareMode = 0;
133 DWORD dwCreateDisposition = OPEN_EXISTING;
134 switch (oflag & _O_ACCMODE)
136 case _O_RDONLY:
137 dwAccess = GENERIC_READ;
138 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
139 break;
140 case _O_WRONLY:
141 dwAccess = GENERIC_WRITE;
142 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
143 break;
144 case _O_RDWR:
145 dwAccess = GENERIC_READ | GENERIC_WRITE;
146 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
147 break;
149 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
150 dwCreateDisposition = CREATE_NEW;
151 else if (oflag & _O_CREAT)
152 dwCreateDisposition = CREATE_ALWAYS;
153 handle = CreateFileA( pszFile, dwAccess, dwShareMode, NULL,
154 dwCreateDisposition, 0, NULL );
155 if (handle == INVALID_HANDLE_VALUE)
156 return 0;
157 return (INT_PTR) handle;
160 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
162 HANDLE handle = (HANDLE) hf;
163 DWORD dwRead;
164 if (ReadFile(handle, pv, cb, &dwRead, NULL))
165 return dwRead;
166 return 0;
169 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
171 HANDLE handle = (HANDLE) hf;
172 DWORD dwWritten;
173 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
174 return dwWritten;
175 return 0;
178 static int cabinet_close(INT_PTR hf)
180 HANDLE handle = (HANDLE) hf;
181 return CloseHandle(handle) ? 0 : -1;
184 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
186 HANDLE handle = (HANDLE) hf;
187 /* flags are compatible and so are passed straight through */
188 return SetFilePointer(handle, dist, NULL, seektype);
191 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
193 MSIRECORD *uirow;
194 LPWSTR uipath, p;
196 /* the UI chunk */
197 uirow = MSI_CreateRecord( 9 );
198 MSI_RecordSetStringW( uirow, 1, f->FileName );
199 uipath = strdupW( f->TargetPath );
200 p = strrchrW(uipath,'\\');
201 if (p)
202 p[1]=0;
203 MSI_RecordSetStringW( uirow, 9, uipath);
204 MSI_RecordSetInteger( uirow, 6, f->FileSize );
205 ui_actiondata( package, action, uirow);
206 msiobj_release( &uirow->hdr );
207 msi_free( uipath );
208 ui_progress( package, 2, f->FileSize, 0, 0);
211 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
213 switch (fdint)
215 case fdintCOPY_FILE:
217 CabData *data = (CabData*) pfdin->pv;
218 HANDLE handle;
219 LPWSTR file;
220 MSIFILE *f;
221 DWORD attrs;
223 file = strdupAtoW(pfdin->psz1);
224 f = get_loaded_file(data->package, file);
225 msi_free(file);
227 if (!f)
229 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
230 return 0;
233 if (f->state != msifs_missing && f->state != msifs_overwrite)
235 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
236 return 0;
239 msi_file_update_ui( data->package, f, szInstallFiles );
241 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
243 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
244 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
246 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
247 NULL, CREATE_ALWAYS, attrs, NULL );
248 if ( handle == INVALID_HANDLE_VALUE )
250 ERR("failed to create %s (error %d)\n",
251 debugstr_w( f->TargetPath ), GetLastError() );
252 return 0;
255 f->state = msifs_installed;
256 return (INT_PTR) handle;
258 case fdintCLOSE_FILE_INFO:
260 FILETIME ft;
261 FILETIME ftLocal;
262 HANDLE handle = (HANDLE) pfdin->hf;
264 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
265 return -1;
266 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
267 return -1;
268 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
269 return -1;
270 CloseHandle(handle);
271 return 1;
273 default:
274 return 0;
278 /***********************************************************************
279 * extract_cabinet_file
281 * Extract files from a cab file.
283 static BOOL extract_cabinet_file(MSIPACKAGE* package, struct media_info *mi)
285 HFDI hfdi;
286 ERF erf;
287 BOOL ret;
288 char *cabinet;
289 char *cab_path;
290 static CHAR empty[] = "";
291 CabData data;
293 TRACE("Extracting %s to %s\n",debugstr_w(mi->source), debugstr_w(mi->last_path));
295 hfdi = FDICreate(cabinet_alloc,
296 cabinet_free,
297 cabinet_open,
298 cabinet_read,
299 cabinet_write,
300 cabinet_close,
301 cabinet_seek,
303 &erf);
304 if (!hfdi)
306 ERR("FDICreate failed\n");
307 return FALSE;
310 if (!(cabinet = strdupWtoA( mi->source )))
312 FDIDestroy(hfdi);
313 return FALSE;
315 if (!(cab_path = strdupWtoA( mi->last_path )))
317 FDIDestroy(hfdi);
318 msi_free(cabinet);
319 return FALSE;
322 data.package = package;
323 data.cab_path = cab_path;
325 ret = FDICopy(hfdi, cabinet, empty, 0, cabinet_notify, NULL, &data);
327 if (!ret)
328 ERR("FDICopy failed\n");
330 FDIDestroy(hfdi);
332 msi_free(cabinet);
333 msi_free(cab_path);
335 return ret;
338 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, LPCWSTR path)
340 if (!file->IsCompressed)
342 LPWSTR p, path;
343 p = resolve_folder(package, file->Component->Directory, TRUE, FALSE, NULL);
344 path = build_directory_name(2, p, file->ShortName);
345 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
347 msi_free(path);
348 path = build_directory_name(2, p, file->LongName);
350 file->SourcePath = path;
351 msi_free(p);
353 else
354 file->SourcePath = build_directory_name(2, path, file->File);
357 static struct media_info *create_media_info( void )
359 struct media_info *mi;
361 mi = msi_alloc( sizeof *mi );
362 if (mi)
364 mi->last_sequence = 0;
365 mi->last_volume = NULL;
366 mi->last_path = NULL;
367 mi->count = 0;
368 mi->source[0] = 0;
371 return mi;
374 static void free_media_info( struct media_info *mi )
376 msi_free( mi->last_path );
377 msi_free( mi );
380 /* downloads a remote cabinet and extracts it if it exists */
381 static UINT msi_extract_remote_cabinet( MSIPACKAGE *package, struct media_info *mi )
383 FDICABINETINFO cabinfo;
384 WCHAR temppath[MAX_PATH];
385 WCHAR src[MAX_PATH];
386 LPSTR cabpath;
387 LPCWSTR file;
388 LPWSTR ptr;
389 HFDI hfdi;
390 ERF erf;
391 int hf;
393 /* the URL is the path prefix of the package URL and the filename
394 * of the file to download
396 ptr = strrchrW(package->PackagePath, '/');
397 lstrcpynW(src, package->PackagePath, ptr - package->PackagePath + 2);
398 ptr = strrchrW(mi->source, '\\');
399 lstrcatW(src, ptr + 1);
401 file = msi_download_file( src, temppath );
402 lstrcpyW(mi->source, file);
404 /* check if the remote cabinet still exists, ignore if it doesn't */
405 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
406 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
407 if (!hfdi)
409 ERR("FDICreate failed\n");
410 return ERROR_FUNCTION_FAILED;
413 cabpath = strdupWtoA(mi->source);
414 hf = cabinet_open(cabpath, _O_RDONLY, 0);
415 if (!FDIIsCabinet(hfdi, hf, &cabinfo))
417 WARN("Remote cabinet %s does not exist.\n", debugstr_w(mi->source));
418 msi_free(cabpath);
419 return ERROR_SUCCESS;
422 msi_free(cabpath);
423 return !extract_cabinet_file(package, mi);
426 static UINT msi_change_media( MSIPACKAGE *package, struct media_info *mi, LPCWSTR prompt )
428 LPWSTR error, error_dialog;
429 UINT r = ERROR_SUCCESS;
431 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
432 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
434 if ( msi_get_property_int(package, szUILevel, 0) == INSTALLUILEVEL_NONE )
435 return ERROR_SUCCESS;
437 error = generate_error_string( package, 1302, 1, prompt );
438 error_dialog = msi_dup_property( package, error_prop );
440 while ( r == ERROR_SUCCESS && GetFileAttributesW( mi->source ) == INVALID_FILE_ATTRIBUTES )
441 r = msi_spawn_error_dialog( package, error_dialog, error );
443 msi_free( error );
444 msi_free( error_dialog );
446 return r;
449 static UINT ready_media_for_file( MSIPACKAGE *package, struct media_info *mi,
450 MSIFILE *file )
452 UINT rc = ERROR_SUCCESS;
453 MSIRECORD * row = 0;
454 static const WCHAR ExecSeqQuery[] =
455 {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
456 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
457 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
458 ' ','%', 'i',' ','O','R','D','E','R',' ','B','Y',' ',
459 '`','D','i','s','k','I','d','`',0};
460 LPCWSTR cab, volume;
461 DWORD sz;
462 INT seq;
463 LPCWSTR prompt;
464 MSICOMPONENT *comp = file->Component;
466 if (file->Sequence <= mi->last_sequence)
468 set_file_source(package, file, mi->last_path);
469 TRACE("Media already ready (%u, %u)\n",file->Sequence,mi->last_sequence);
470 return ERROR_SUCCESS;
473 mi->count ++;
474 row = MSI_QueryGetRecord(package->db, ExecSeqQuery, file->Sequence);
475 if (!row)
477 TRACE("Unable to query row\n");
478 return ERROR_FUNCTION_FAILED;
481 volume = MSI_RecordGetString(row, 5);
482 prompt = MSI_RecordGetString(row, 3);
484 msi_free(mi->last_path);
485 mi->last_path = NULL;
487 if (!file->IsCompressed)
489 mi->last_path = resolve_folder(package, comp->Directory, TRUE, FALSE, NULL);
490 set_file_source(package, file, mi->last_path);
492 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
493 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count, volume,
494 prompt);
496 MsiSourceListSetInfoW(package->ProductCode, NULL,
497 MSIINSTALLCONTEXT_USERMANAGED,
498 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
499 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
500 msiobj_release(&row->hdr);
501 return rc;
504 seq = MSI_RecordGetInteger(row,2);
505 mi->last_sequence = seq;
507 cab = MSI_RecordGetString(row,4);
508 if (cab)
510 TRACE("Source is CAB %s\n",debugstr_w(cab));
511 /* the stream does not contain the # character */
512 if (cab[0]=='#')
514 LPWSTR path, p;
516 rc = writeout_cabinet_stream(package,&cab[1],mi->source);
517 if (rc != ERROR_SUCCESS)
518 return rc;
520 mi->last_path = strdupW(mi->source);
521 p = strrchrW(mi->last_path,'\\');
522 if (p)
523 p[1] = 0;
525 path = msi_dup_property( package, cszSourceDir );
527 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
528 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count,
529 volume, prompt);
531 MsiSourceListSetInfoW(package->ProductCode, NULL,
532 MSIINSTALLCONTEXT_USERMANAGED,
533 MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
534 INSTALLPROPERTY_LASTUSEDSOURCEW, path);
536 msi_free(path);
538 else
540 sz = MAX_PATH;
541 mi->last_path = msi_alloc(MAX_PATH*sizeof(WCHAR));
542 if (MSI_GetPropertyW(package, cszSourceDir, mi->source, &sz))
544 ERR("No Source dir defined\n");
545 rc = ERROR_FUNCTION_FAILED;
547 else
549 strcpyW(mi->last_path,mi->source);
550 strcatW(mi->source,cab);
552 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
553 rc = msi_change_media(package, mi, prompt);
555 if ( rc != ERROR_SUCCESS )
556 goto done;
558 MsiSourceListSetInfoW(package->ProductCode, NULL,
559 MSIINSTALLCONTEXT_USERMANAGED,
560 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
561 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
563 /* extract the cab file into a folder in the temp folder */
564 sz = MAX_PATH;
565 if (MSI_GetPropertyW(package, cszTempFolder,mi->last_path, &sz)
566 != ERROR_SUCCESS)
567 GetTempPathW(MAX_PATH,mi->last_path);
571 /* only download the remote cabinet file if a local copy does not exist */
572 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
573 UrlIsW(package->PackagePath, URLIS_URL))
575 rc = msi_extract_remote_cabinet(package, mi);
577 else
579 rc = !extract_cabinet_file(package, mi);
582 else
584 sz = MAX_PATH;
585 mi->last_path = msi_alloc(MAX_PATH*sizeof(WCHAR));
586 MSI_GetPropertyW(package,cszSourceDir,mi->source,&sz);
587 strcpyW(mi->last_path,mi->source);
589 MsiSourceListSetInfoW(package->ProductCode, NULL,
590 MSIINSTALLCONTEXT_USERMANAGED,
591 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
592 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
594 set_file_source(package, file, mi->last_path);
596 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
597 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count, volume,
598 prompt);
600 done:
601 msiobj_release(&row->hdr);
603 return rc;
606 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
607 MSIFILE** file)
609 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
611 if (lstrcmpW( file_key, (*file)->File )==0)
613 if ((*file)->state >= msifs_overwrite)
614 return ERROR_SUCCESS;
615 else
616 return ERROR_FILE_NOT_FOUND;
620 return ERROR_FUNCTION_FAILED;
624 * ACTION_InstallFiles()
626 * For efficiency, this is done in two passes:
627 * 1) Correct all the TargetPaths and determine what files are to be installed.
628 * 2) Extract Cabinets and copy files.
630 UINT ACTION_InstallFiles(MSIPACKAGE *package)
632 struct media_info *mi;
633 UINT rc = ERROR_SUCCESS;
634 LPWSTR ptr;
635 MSIFILE *file;
637 /* increment progress bar each time action data is sent */
638 ui_progress(package,1,1,0,0);
640 /* handle the keys for the SourceList */
641 ptr = strrchrW(package->PackagePath,'\\');
642 if (ptr)
644 ptr ++;
645 MsiSourceListSetInfoW(package->ProductCode, NULL,
646 MSIINSTALLCONTEXT_USERMANAGED,
647 MSICODE_PRODUCT,
648 INSTALLPROPERTY_PACKAGENAMEW, ptr);
650 /* FIXME("Write DiskPrompt\n"); */
652 /* Pass 1 */
653 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
655 if (!ACTION_VerifyComponentForAction( file->Component, INSTALLSTATE_LOCAL ))
657 ui_progress(package,2,file->FileSize,0,0);
658 TRACE("File %s is not scheduled for install\n",
659 debugstr_w(file->File));
661 file->state = msifs_skipped;
666 * Despite MSDN specifying that the CreateFolders action
667 * should be called before InstallFiles, some installers don't
668 * do that, and they seem to work correctly. We need to create
669 * directories here to make sure that the files can be copied.
671 msi_create_component_directories( package );
673 mi = create_media_info();
675 /* Pass 2 */
676 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
678 if (file->state != msifs_missing && file->state != msifs_overwrite)
679 continue;
681 TRACE("Pass 2: %s\n",debugstr_w(file->File));
683 rc = ready_media_for_file( package, mi, file );
684 if (rc != ERROR_SUCCESS)
686 ERR("Unable to ready media\n");
687 rc = ERROR_FUNCTION_FAILED;
688 break;
691 TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
692 debugstr_w(file->TargetPath));
694 if (file->state != msifs_missing && file->state != msifs_overwrite)
695 continue;
697 /* compressed files are extracted in ready_media_for_file */
698 if (file->IsCompressed)
700 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(file->TargetPath))
702 ERR("compressed file wasn't extracted (%s)\n",
703 debugstr_w(file->TargetPath));
704 rc = ERROR_INSTALL_FAILURE;
705 break;
708 continue;
711 rc = CopyFileW(file->SourcePath,file->TargetPath,FALSE);
712 if (!rc)
714 rc = GetLastError();
715 ERR("Unable to copy file (%s -> %s) (error %d)\n",
716 debugstr_w(file->SourcePath), debugstr_w(file->TargetPath), rc);
717 if (rc == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
719 rc = 0;
721 else if (rc == ERROR_FILE_NOT_FOUND)
723 ERR("Source File Not Found! Continuing\n");
724 rc = 0;
726 else if (file->Attributes & msidbFileAttributesVital)
728 ERR("Ignoring Error and continuing (nonvital file)...\n");
729 rc = 0;
732 else
734 file->state = msifs_installed;
735 rc = ERROR_SUCCESS;
739 /* cleanup */
740 free_media_info( mi );
741 return rc;
744 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
746 MSIPACKAGE *package = (MSIPACKAGE*)param;
747 WCHAR dest_name[0x100];
748 LPWSTR dest_path, dest;
749 LPCWSTR file_key, component;
750 DWORD sz;
751 DWORD rc;
752 MSICOMPONENT *comp;
753 MSIFILE *file;
755 component = MSI_RecordGetString(row,2);
756 comp = get_loaded_component(package,component);
758 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
760 TRACE("Skipping copy due to disabled component %s\n",
761 debugstr_w(component));
763 /* the action taken was the same as the current install state */
764 comp->Action = comp->Installed;
766 return ERROR_SUCCESS;
769 comp->Action = INSTALLSTATE_LOCAL;
771 file_key = MSI_RecordGetString(row,3);
772 if (!file_key)
774 ERR("Unable to get file key\n");
775 return ERROR_FUNCTION_FAILED;
778 rc = get_file_target(package,file_key,&file);
780 if (rc != ERROR_SUCCESS)
782 ERR("Original file unknown %s\n",debugstr_w(file_key));
783 return ERROR_SUCCESS;
786 if (MSI_RecordIsNull(row,4))
787 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
788 else
790 sz=0x100;
791 MSI_RecordGetStringW(row,4,dest_name,&sz);
792 reduce_to_longfilename(dest_name);
795 if (MSI_RecordIsNull(row,5))
797 LPWSTR p;
798 dest_path = strdupW(file->TargetPath);
799 p = strrchrW(dest_path,'\\');
800 if (p)
801 *p=0;
803 else
805 LPCWSTR destkey;
806 destkey = MSI_RecordGetString(row,5);
807 dest_path = resolve_folder(package, destkey, FALSE,FALSE,NULL);
808 if (!dest_path)
810 /* try a Property */
811 dest_path = msi_dup_property( package, destkey );
812 if (!dest_path)
814 FIXME("Unable to get destination folder, try AppSearch properties\n");
815 return ERROR_SUCCESS;
820 dest = build_directory_name(2, dest_path, dest_name);
822 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
823 debugstr_w(dest));
825 if (strcmpW(file->TargetPath,dest))
826 rc = !CopyFileW(file->TargetPath,dest,TRUE);
827 else
828 rc = ERROR_SUCCESS;
830 if (rc != ERROR_SUCCESS)
831 ERR("Failed to copy file %s -> %s, last error %d\n",
832 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
834 FIXME("We should track these duplicate files as well\n");
836 msi_free(dest_path);
837 msi_free(dest);
839 msi_file_update_ui(package, file, szDuplicateFiles);
841 return ERROR_SUCCESS;
844 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
846 UINT rc;
847 MSIQUERY * view;
848 static const WCHAR ExecSeqQuery[] =
849 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
850 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
852 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
853 if (rc != ERROR_SUCCESS)
854 return ERROR_SUCCESS;
856 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
857 msiobj_release(&view->hdr);
859 return rc;
862 /* compares the version of a file read from the filesystem and
863 * the version specified in the File table
865 static int msi_compare_file_version( MSIFILE *file )
867 WCHAR version[MAX_PATH];
868 DWORD size;
869 UINT r;
871 size = MAX_PATH;
872 version[0] = '\0';
873 r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
874 if ( r != ERROR_SUCCESS )
875 return 0;
877 return lstrcmpW( version, file->Version );
880 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
882 MSIFILE *file;
884 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
886 MSIRECORD *uirow;
887 LPWSTR uipath, p;
889 if ( !file->Component )
890 continue;
891 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
892 continue;
894 if ( file->state == msifs_installed )
895 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
897 if ( file->state != msifs_present )
898 continue;
900 /* only remove a file if the version to be installed
901 * is strictly newer than the old file
903 if ( msi_compare_file_version( file ) >= 0 )
904 continue;
906 TRACE("removing %s\n", debugstr_w(file->File) );
907 if ( !DeleteFileW( file->TargetPath ) )
908 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
909 file->state = msifs_missing;
911 /* the UI chunk */
912 uirow = MSI_CreateRecord( 9 );
913 MSI_RecordSetStringW( uirow, 1, file->FileName );
914 uipath = strdupW( file->TargetPath );
915 p = strrchrW(uipath,'\\');
916 if (p)
917 p[1]=0;
918 MSI_RecordSetStringW( uirow, 9, uipath);
919 ui_actiondata( package, szRemoveFiles, uirow);
920 msiobj_release( &uirow->hdr );
921 msi_free( uipath );
922 /* FIXME: call ui_progress here? */
925 return ERROR_SUCCESS;