Extract file directly to their target location, bypassing the need to
[wine/multimedia.git] / dlls / msi / files.c
blob471294da5c57f0b60a5064dc5c9ddf23c086939b
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "wine/unicode.h"
46 #include "action.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msi);
50 extern const WCHAR szInstallFiles[];
51 extern const WCHAR szDuplicateFiles[];
52 extern const WCHAR szMoveFiles[];
53 extern const WCHAR szPatchFiles[];
54 extern const WCHAR szRemoveDuplicateFiles[];
55 extern const WCHAR szRemoveFiles[];
57 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
61 * This is a helper function for handling embedded cabinet media
63 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
64 WCHAR* source)
66 UINT rc;
67 USHORT* data;
68 UINT size;
69 DWORD write;
70 HANDLE the_file;
71 WCHAR tmp[MAX_PATH];
73 rc = read_raw_stream_data(package->db,stream_name,&data,&size);
74 if (rc != ERROR_SUCCESS)
75 return rc;
77 write = MAX_PATH;
78 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
79 GetTempPathW(MAX_PATH,tmp);
81 GetTempFileNameW(tmp,stream_name,0,source);
83 track_tempfile(package,strrchrW(source,'\\'), source);
84 the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
85 FILE_ATTRIBUTE_NORMAL, NULL);
87 if (the_file == INVALID_HANDLE_VALUE)
89 ERR("Unable to create file %s\n",debugstr_w(source));
90 rc = ERROR_FUNCTION_FAILED;
91 goto end;
94 WriteFile(the_file,data,size,&write,NULL);
95 CloseHandle(the_file);
96 TRACE("wrote %li bytes to %s\n",write,debugstr_w(source));
97 end:
98 msi_free(data);
99 return rc;
103 /* Support functions for FDI functions */
104 typedef struct
106 MSIPACKAGE* package;
107 LPCSTR cab_path;
108 } CabData;
110 static void * cabinet_alloc(ULONG cb)
112 return msi_alloc(cb);
115 static void cabinet_free(void *pv)
117 msi_free(pv);
120 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
122 HANDLE handle;
123 DWORD dwAccess = 0;
124 DWORD dwShareMode = 0;
125 DWORD dwCreateDisposition = OPEN_EXISTING;
126 switch (oflag & _O_ACCMODE)
128 case _O_RDONLY:
129 dwAccess = GENERIC_READ;
130 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
131 break;
132 case _O_WRONLY:
133 dwAccess = GENERIC_WRITE;
134 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
135 break;
136 case _O_RDWR:
137 dwAccess = GENERIC_READ | GENERIC_WRITE;
138 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
139 break;
141 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
142 dwCreateDisposition = CREATE_NEW;
143 else if (oflag & _O_CREAT)
144 dwCreateDisposition = CREATE_ALWAYS;
145 handle = CreateFileA( pszFile, dwAccess, dwShareMode, NULL,
146 dwCreateDisposition, 0, NULL );
147 if (handle == INVALID_HANDLE_VALUE)
148 return 0;
149 return (INT_PTR) handle;
152 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
154 HANDLE handle = (HANDLE) hf;
155 DWORD dwRead;
156 if (ReadFile(handle, pv, cb, &dwRead, NULL))
157 return dwRead;
158 return 0;
161 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
163 HANDLE handle = (HANDLE) hf;
164 DWORD dwWritten;
165 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
166 return dwWritten;
167 return 0;
170 static int cabinet_close(INT_PTR hf)
172 HANDLE handle = (HANDLE) hf;
173 return CloseHandle(handle) ? 0 : -1;
176 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
178 HANDLE handle = (HANDLE) hf;
179 /* flags are compatible and so are passed straight through */
180 return SetFilePointer(handle, dist, NULL, seektype);
183 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f )
185 MSIRECORD *uirow;
186 LPWSTR uipath, p;
188 /* the UI chunk */
189 uirow = MSI_CreateRecord( 9 );
190 MSI_RecordSetStringW( uirow, 1, f->FileName );
191 uipath = strdupW( f->TargetPath );
192 p = strrchrW(uipath,'\\');
193 if (p)
194 p[1]=0;
195 MSI_RecordSetStringW( uirow, 9, uipath);
196 MSI_RecordSetInteger( uirow, 6, f->FileSize );
197 ui_actiondata( package, szInstallFiles, uirow);
198 msiobj_release( &uirow->hdr );
199 msi_free( uipath );
200 ui_progress( package, 2, f->FileSize, 0, 0);
203 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
205 switch (fdint)
207 case fdintCOPY_FILE:
209 CabData *data = (CabData*) pfdin->pv;
210 HANDLE handle;
211 LPWSTR file;
212 MSIFILE *f;
214 file = strdupAtoW(pfdin->psz1);
215 f = get_loaded_file(data->package, file);
216 msi_free(file);
218 if (!f)
220 ERR("Unknown File in Cabinet (%s)\n",debugstr_a(pfdin->psz1));
221 return 0;
224 if (f->State != 1 && f->State != 2)
226 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
227 return 0;
230 msi_file_update_ui( data->package, f );
232 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
234 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
235 NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL );
236 if ( handle == INVALID_HANDLE_VALUE )
238 ERR("failed to create %s (error %ld)\n",
239 debugstr_w( f->TargetPath ), GetLastError() );
240 return 0;
244 * FIXME: 0 is a valid return from CreateFile
245 * but an invalid handle for the cabinet API
247 if ( !handle )
248 ERR("CreateFile returned 0 - not handled\n");
250 f->State = 4;
251 return (INT_PTR) handle;
253 case fdintCLOSE_FILE_INFO:
255 FILETIME ft;
256 FILETIME ftLocal;
257 HANDLE handle = (HANDLE) pfdin->hf;
259 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
260 return -1;
261 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
262 return -1;
263 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
264 return -1;
265 CloseHandle(handle);
266 return 1;
268 default:
269 return 0;
273 /***********************************************************************
274 * extract_cabinet_file
276 * Extract files from a cab file.
278 static BOOL extract_cabinet_file(MSIPACKAGE* package, LPCWSTR source,
279 LPCWSTR path)
281 HFDI hfdi;
282 ERF erf;
283 BOOL ret;
284 char *cabinet;
285 char *cab_path;
286 CabData data;
288 TRACE("Extracting %s to %s\n",debugstr_w(source), debugstr_w(path));
290 hfdi = FDICreate(cabinet_alloc,
291 cabinet_free,
292 cabinet_open,
293 cabinet_read,
294 cabinet_write,
295 cabinet_close,
296 cabinet_seek,
298 &erf);
299 if (!hfdi)
301 ERR("FDICreate failed\n");
302 return FALSE;
305 if (!(cabinet = strdupWtoA( source )))
307 FDIDestroy(hfdi);
308 return FALSE;
310 if (!(cab_path = strdupWtoA( path )))
312 FDIDestroy(hfdi);
313 msi_free(cabinet);
314 return FALSE;
317 data.package = package;
318 data.cab_path = cab_path;
320 ret = FDICopy(hfdi, cabinet, "", 0, cabinet_notify, NULL, &data);
322 if (!ret)
323 ERR("FDICopy failed\n");
325 FDIDestroy(hfdi);
327 msi_free(cabinet);
328 msi_free(cab_path);
330 return ret;
333 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, MSICOMPONENT*
334 comp, LPCWSTR path)
336 if (file->Attributes & msidbFileAttributesNoncompressed)
338 LPWSTR p;
339 p = resolve_folder(package, comp->Directory, TRUE, FALSE, NULL);
340 file->SourcePath = build_directory_name(2, p, file->ShortName);
341 msi_free(p);
343 else
344 file->SourcePath = build_directory_name(2, path, file->File);
347 static BOOL check_volume(LPCWSTR path, LPCWSTR want_volume, LPWSTR volume,
348 UINT *intype)
350 WCHAR drive[4];
351 WCHAR name[MAX_PATH];
352 UINT type;
354 if (!(path[0] && path[1] == ':'))
355 return TRUE;
357 drive[0] = path[0];
358 drive[1] = path[1];
359 drive[2] = '\\';
360 drive[3] = 0;
361 TRACE("Checking volume %s .. (%s)\n",debugstr_w(drive), debugstr_w(want_volume));
362 type = GetDriveTypeW(drive);
363 TRACE("drive is of type %x\n",type);
365 if (type == DRIVE_UNKNOWN || type == DRIVE_NO_ROOT_DIR ||
366 type == DRIVE_FIXED || type == DRIVE_RAMDISK)
367 return TRUE;
369 GetVolumeInformationW(drive, name, MAX_PATH, NULL, NULL, NULL, NULL, 0);
370 TRACE("Drive contains %s\n", debugstr_w(name));
371 volume = strdupW(name);
372 if (*intype)
373 *intype=type;
374 return (strcmpiW(want_volume,name)==0);
377 static BOOL check_for_sourcefile(LPCWSTR source)
379 DWORD attrib = GetFileAttributesW(source);
380 return (!(attrib == INVALID_FILE_ATTRIBUTES));
383 static UINT ready_volume(MSIPACKAGE* package, LPCWSTR path, LPWSTR last_volume,
384 MSIRECORD *row,UINT *type )
386 LPWSTR volume = NULL;
387 LPCWSTR want_volume = MSI_RecordGetString(row, 5);
388 BOOL ok = check_volume(path, want_volume, volume, type);
390 TRACE("Readying Volume for %s (%s, %s)\n", debugstr_w(path),
391 debugstr_w(want_volume), debugstr_w(last_volume));
393 if (check_for_sourcefile(path) && !ok)
395 FIXME("Found the Sourcefile but not on the correct volume.(%s,%s,%s)\n",
396 debugstr_w(path),debugstr_w(want_volume), debugstr_w(volume));
397 return ERROR_SUCCESS;
400 while (!ok)
402 INT rc;
403 LPCWSTR prompt;
404 LPWSTR msg;
406 prompt = MSI_RecordGetString(row,3);
407 msg = generate_error_string(package, 1302, 1, prompt);
408 rc = MessageBoxW(NULL,msg,NULL,MB_OKCANCEL);
409 msi_free(volume);
410 msi_free(msg);
411 if (rc == IDOK)
412 ok = check_for_sourcefile(path);
413 else
414 return ERROR_INSTALL_USEREXIT;
417 msi_free(last_volume);
418 last_volume = strdupW(volume);
419 return ERROR_SUCCESS;
422 struct media_info {
423 UINT last_sequence;
424 LPWSTR last_volume;
425 LPWSTR last_path;
426 DWORD count;
427 WCHAR source[MAX_PATH];
430 static struct media_info *create_media_info( void )
432 struct media_info *mi;
434 mi = msi_alloc( sizeof *mi );
435 if (mi)
437 mi->last_sequence = 0;
438 mi->last_volume = NULL;
439 mi->last_path = NULL;
440 mi->count = 0;
441 mi->source[0] = 0;
444 return mi;
447 static void free_media_info( struct media_info *mi )
449 msi_free( mi->last_path );
450 msi_free( mi );
453 static UINT ready_media_for_file( MSIPACKAGE *package, struct media_info *mi,
454 MSIFILE *file )
456 UINT rc = ERROR_SUCCESS;
457 MSIRECORD * row = 0;
458 static const WCHAR ExecSeqQuery[] =
459 {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
460 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
461 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
462 ' ','%', 'i',' ','O','R','D','E','R',' ','B','Y',' ',
463 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',0};
464 LPCWSTR cab, volume;
465 DWORD sz;
466 INT seq;
467 UINT type;
468 LPCWSTR prompt;
469 MSICOMPONENT *comp = file->Component;
471 if (file->Sequence <= mi->last_sequence)
473 set_file_source(package,file,comp,mi->last_path);
474 TRACE("Media already ready (%u, %u)\n",file->Sequence,mi->last_sequence);
475 return ERROR_SUCCESS;
478 mi->count ++;
479 row = MSI_QueryGetRecord(package->db, ExecSeqQuery, file->Sequence);
480 if (!row)
482 TRACE("Unable to query row\n");
483 return ERROR_FUNCTION_FAILED;
486 seq = MSI_RecordGetInteger(row,2);
487 mi->last_sequence = seq;
489 volume = MSI_RecordGetString(row, 5);
490 prompt = MSI_RecordGetString(row, 3);
492 msi_free(mi->last_path);
493 mi->last_path = NULL;
495 if (file->Attributes & msidbFileAttributesNoncompressed)
497 mi->last_path = resolve_folder(package, comp->Directory, TRUE, FALSE, NULL);
498 set_file_source(package,file,comp,mi->last_path);
499 rc = ready_volume(package, file->SourcePath, mi->last_volume, row,&type);
501 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
502 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count, volume,
503 prompt);
505 if (type == DRIVE_REMOVABLE || type == DRIVE_CDROM ||
506 type == DRIVE_RAMDISK)
507 MsiSourceListSetInfoW(package->ProductCode, NULL,
508 MSIINSTALLCONTEXT_USERMANAGED,
509 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
510 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
511 else
512 MsiSourceListSetInfoW(package->ProductCode, NULL,
513 MSIINSTALLCONTEXT_USERMANAGED,
514 MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
515 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
516 msiobj_release(&row->hdr);
517 return rc;
520 cab = MSI_RecordGetString(row,4);
521 if (cab)
523 TRACE("Source is CAB %s\n",debugstr_w(cab));
524 /* the stream does not contain the # character */
525 if (cab[0]=='#')
527 LPWSTR path;
529 writeout_cabinet_stream(package,&cab[1],mi->source);
530 mi->last_path = strdupW(mi->source);
531 *(strrchrW(mi->last_path,'\\')+1)=0;
533 path = msi_dup_property( package, cszSourceDir );
535 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
536 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count,
537 volume, prompt);
539 MsiSourceListSetInfoW(package->ProductCode, NULL,
540 MSIINSTALLCONTEXT_USERMANAGED,
541 MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
542 INSTALLPROPERTY_LASTUSEDSOURCEW, path);
544 msi_free(path);
546 else
548 sz = MAX_PATH;
549 mi->last_path = msi_alloc(MAX_PATH*sizeof(WCHAR));
550 if (MSI_GetPropertyW(package, cszSourceDir, mi->source, &sz))
552 ERR("No Source dir defined \n");
553 rc = ERROR_FUNCTION_FAILED;
555 else
557 strcpyW(mi->last_path,mi->source);
558 strcatW(mi->source,cab);
560 rc = ready_volume(package, mi->source, mi->last_volume, row, &type);
561 if (type == DRIVE_REMOVABLE || type == DRIVE_CDROM ||
562 type == DRIVE_RAMDISK)
563 MsiSourceListSetInfoW(package->ProductCode, NULL,
564 MSIINSTALLCONTEXT_USERMANAGED,
565 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
566 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
567 else
568 MsiSourceListSetInfoW(package->ProductCode, NULL,
569 MSIINSTALLCONTEXT_USERMANAGED,
570 MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
571 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
573 /* extract the cab file into a folder in the temp folder */
574 sz = MAX_PATH;
575 if (MSI_GetPropertyW(package, cszTempFolder,mi->last_path, &sz)
576 != ERROR_SUCCESS)
577 GetTempPathW(MAX_PATH,mi->last_path);
580 rc = !extract_cabinet_file(package, mi->source, mi->last_path);
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);
588 rc = ready_volume(package, mi->last_path, mi->last_volume, row, &type);
590 if (type == DRIVE_REMOVABLE || type == DRIVE_CDROM ||
591 type == DRIVE_RAMDISK)
592 MsiSourceListSetInfoW(package->ProductCode, NULL,
593 MSIINSTALLCONTEXT_USERMANAGED,
594 MSICODE_PRODUCT|MSISOURCETYPE_MEDIA,
595 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
596 else
597 MsiSourceListSetInfoW(package->ProductCode, NULL,
598 MSIINSTALLCONTEXT_USERMANAGED,
599 MSICODE_PRODUCT|MSISOURCETYPE_NETWORK,
600 INSTALLPROPERTY_LASTUSEDSOURCEW, mi->last_path);
602 set_file_source(package, file, comp, mi->last_path);
604 MsiSourceListAddMediaDiskW(package->ProductCode, NULL,
605 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT, mi->count, volume,
606 prompt);
608 msiobj_release(&row->hdr);
610 return rc;
613 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
614 LPWSTR* file_source)
616 MSIFILE *file;
618 if (!package)
619 return ERROR_INVALID_HANDLE;
621 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
623 if (lstrcmpW( file_key, file->File )==0)
625 if (file->State >= 2)
627 *file_source = strdupW( file->TargetPath );
628 return ERROR_SUCCESS;
630 else
631 return ERROR_FILE_NOT_FOUND;
635 return ERROR_FUNCTION_FAILED;
639 * In order to make this work more effeciencly I am going to do this in 2
640 * passes.
641 * Pass 1) Correct all the TargetPaths and determin what files are to be
642 * installed.
643 * Pass 2) Extract Cabinents and copy files.
645 UINT ACTION_InstallFiles(MSIPACKAGE *package)
647 struct media_info *mi;
648 UINT rc = ERROR_SUCCESS;
649 LPWSTR ptr;
650 MSIFILE *file;
652 if (!package)
653 return ERROR_INVALID_HANDLE;
655 /* increment progress bar each time action data is sent */
656 ui_progress(package,1,1,0,0);
658 /* handle the keys for the SouceList */
659 ptr = strrchrW(package->PackagePath,'\\');
660 if (ptr)
662 ptr ++;
663 MsiSourceListSetInfoW(package->ProductCode, NULL,
664 MSIINSTALLCONTEXT_USERMANAGED,
665 MSICODE_PRODUCT,
666 INSTALLPROPERTY_PACKAGENAMEW, ptr);
668 /* FIXME("Write DiskPrompt\n"); */
670 /* Pass 1 */
671 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
673 if (!ACTION_VerifyComponentForAction(package, file->Component,
674 INSTALLSTATE_LOCAL))
676 ui_progress(package,2,file->FileSize,0,0);
677 TRACE("File %s is not scheduled for install\n",
678 debugstr_w(file->File));
680 file->State = 5;
684 mi = create_media_info();
686 /* Pass 2 */
687 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
689 if (file->State != 1 && file->State != 2)
690 continue;
692 TRACE("Pass 2: %s\n",debugstr_w(file->File));
694 rc = ready_media_for_file( package, mi, file );
695 if (rc != ERROR_SUCCESS)
697 ERR("Unable to ready media\n");
698 rc = ERROR_FUNCTION_FAILED;
699 break;
702 TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
703 debugstr_w(file->TargetPath));
705 if (file->State != 1 && file->State != 2)
706 continue;
708 /* compressed files are extracted in ready_media_for_file */
709 if (~file->Attributes & msidbFileAttributesNoncompressed)
711 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(file->TargetPath))
712 ERR("compressed file wasn't extracted (%s)\n",
713 debugstr_w(file->TargetPath));
714 continue;
717 rc = CopyFileW(file->SourcePath,file->TargetPath,FALSE);
718 if (!rc)
720 rc = GetLastError();
721 ERR("Unable to copy file (%s -> %s) (error %d)\n",
722 debugstr_w(file->SourcePath), debugstr_w(file->TargetPath), rc);
723 if (rc == ERROR_ALREADY_EXISTS && file->State == 2)
725 rc = 0;
727 else if (rc == ERROR_FILE_NOT_FOUND)
729 ERR("Source File Not Found! Continuing\n");
730 rc = 0;
732 else if (file->Attributes & msidbFileAttributesVital)
734 ERR("Ignoring Error and continuing (nonvital file)...\n");
735 rc = 0;
738 else
740 file->State = 4;
741 rc = ERROR_SUCCESS;
745 /* cleanup */
746 free_media_info( mi );
747 return rc;
750 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
752 MSIPACKAGE *package = (MSIPACKAGE*)param;
753 WCHAR *file_source = NULL;
754 WCHAR dest_name[0x100];
755 LPWSTR dest_path, dest;
756 LPCWSTR file_key, component;
757 DWORD sz;
758 DWORD rc;
759 MSICOMPONENT *comp;
761 component = MSI_RecordGetString(row,2);
762 comp = get_loaded_component(package,component);
764 if (!ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_LOCAL))
766 TRACE("Skipping copy due to disabled component %s\n",
767 debugstr_w(component));
769 /* the action taken was the same as the current install state */
770 comp->Action = comp->Installed;
772 return ERROR_SUCCESS;
775 comp->Action = INSTALLSTATE_LOCAL;
777 file_key = MSI_RecordGetString(row,3);
778 if (!file_key)
780 ERR("Unable to get file key\n");
781 return ERROR_FUNCTION_FAILED;
784 rc = get_file_target(package,file_key,&file_source);
786 if (rc != ERROR_SUCCESS)
788 ERR("Original file unknown %s\n",debugstr_w(file_key));
789 msi_free(file_source);
790 return ERROR_SUCCESS;
793 if (MSI_RecordIsNull(row,4))
794 strcpyW(dest_name,strrchrW(file_source,'\\')+1);
795 else
797 sz=0x100;
798 MSI_RecordGetStringW(row,4,dest_name,&sz);
799 reduce_to_longfilename(dest_name);
802 if (MSI_RecordIsNull(row,5))
804 LPWSTR p;
805 dest_path = strdupW(file_source);
806 p = strrchrW(dest_path,'\\');
807 if (p)
808 *p=0;
810 else
812 LPCWSTR destkey;
813 destkey = MSI_RecordGetString(row,5);
814 dest_path = resolve_folder(package, destkey, FALSE,FALSE,NULL);
815 if (!dest_path)
817 /* try a Property */
818 dest_path = msi_dup_property( package, destkey );
819 if (!dest_path)
821 FIXME("Unable to get destination folder, try AppSearch properties\n");
822 msi_free(file_source);
823 return ERROR_SUCCESS;
828 dest = build_directory_name(2, dest_path, dest_name);
830 TRACE("Duplicating file %s to %s\n",debugstr_w(file_source),
831 debugstr_w(dest));
833 if (strcmpW(file_source,dest))
834 rc = !CopyFileW(file_source,dest,TRUE);
835 else
836 rc = ERROR_SUCCESS;
838 if (rc != ERROR_SUCCESS)
839 ERR("Failed to copy file %s -> %s, last error %ld\n", debugstr_w(file_source), debugstr_w(dest_path), GetLastError());
841 FIXME("We should track these duplicate files as well\n");
843 msi_free(dest_path);
844 msi_free(dest);
845 msi_free(file_source);
847 return ERROR_SUCCESS;
850 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
852 UINT rc;
853 MSIQUERY * view;
854 static const WCHAR ExecSeqQuery[] =
855 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
856 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
858 if (!package)
859 return ERROR_INVALID_HANDLE;
861 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
862 if (rc != ERROR_SUCCESS)
863 return ERROR_SUCCESS;
865 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
866 msiobj_release(&view->hdr);
868 return rc;