shell32: Fix the Chinese translations.
[wine.git] / dlls / msi / files.c
blobae7f3f04ac5d7692430b4816c830032bb9939e67
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 static BOOL source_matches_volume(MSIMEDIAINFO *mi, LPWSTR source_root)
62 WCHAR volume_name[MAX_PATH + 1];
64 if (!GetVolumeInformationW(source_root, volume_name, MAX_PATH + 1,
65 NULL, NULL, NULL, NULL, 0))
67 ERR("Failed to get volume information\n");
68 return FALSE;
71 return !lstrcmpW(mi->volume_label, volume_name);
74 static UINT msi_change_media( MSIPACKAGE *package, MSIMEDIAINFO *mi )
76 LPSTR msg;
77 LPWSTR error, error_dialog;
78 LPWSTR source_dir;
79 UINT r = ERROR_SUCCESS;
81 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
82 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
84 if ( (msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE && !gUIHandlerA )
85 return ERROR_SUCCESS;
87 error = generate_error_string( package, 1302, 1, mi->disk_prompt );
88 error_dialog = msi_dup_property( package, error_prop );
89 source_dir = msi_dup_property( package, cszSourceDir );
90 PathStripToRootW(source_dir);
92 while ( r == ERROR_SUCCESS &&
93 !source_matches_volume(mi, source_dir) )
95 r = msi_spawn_error_dialog( package, error_dialog, error );
97 if (gUIHandlerA)
99 msg = strdupWtoA( error );
100 gUIHandlerA( gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg );
101 msi_free(msg);
105 msi_free( error );
106 msi_free( error_dialog );
107 msi_free( source_dir );
109 return r;
113 * This is a helper function for handling embedded cabinet media
115 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
116 WCHAR* source)
118 UINT rc;
119 USHORT* data;
120 UINT size;
121 DWORD write;
122 HANDLE the_file;
123 WCHAR tmp[MAX_PATH];
125 rc = read_raw_stream_data(package->db,stream_name,&data,&size);
126 if (rc != ERROR_SUCCESS)
127 return rc;
129 write = MAX_PATH;
130 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
131 GetTempPathW(MAX_PATH,tmp);
133 GetTempFileNameW(tmp,stream_name,0,source);
135 track_tempfile(package, source);
136 the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
137 FILE_ATTRIBUTE_NORMAL, NULL);
139 if (the_file == INVALID_HANDLE_VALUE)
141 ERR("Unable to create file %s\n",debugstr_w(source));
142 rc = ERROR_FUNCTION_FAILED;
143 goto end;
146 WriteFile(the_file,data,size,&write,NULL);
147 CloseHandle(the_file);
148 TRACE("wrote %i bytes to %s\n",write,debugstr_w(source));
149 end:
150 msi_free(data);
151 return rc;
155 /* Support functions for FDI functions */
156 typedef struct
158 MSIPACKAGE* package;
159 MSIMEDIAINFO *mi;
160 } CabData;
162 static void * cabinet_alloc(ULONG cb)
164 return msi_alloc(cb);
167 static void cabinet_free(void *pv)
169 msi_free(pv);
172 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
174 HANDLE handle;
175 DWORD dwAccess = 0;
176 DWORD dwShareMode = 0;
177 DWORD dwCreateDisposition = OPEN_EXISTING;
178 switch (oflag & _O_ACCMODE)
180 case _O_RDONLY:
181 dwAccess = GENERIC_READ;
182 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
183 break;
184 case _O_WRONLY:
185 dwAccess = GENERIC_WRITE;
186 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
187 break;
188 case _O_RDWR:
189 dwAccess = GENERIC_READ | GENERIC_WRITE;
190 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
191 break;
193 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
194 dwCreateDisposition = CREATE_NEW;
195 else if (oflag & _O_CREAT)
196 dwCreateDisposition = CREATE_ALWAYS;
197 handle = CreateFileA( pszFile, dwAccess, dwShareMode, NULL,
198 dwCreateDisposition, 0, NULL );
199 if (handle == INVALID_HANDLE_VALUE)
200 return 0;
201 return (INT_PTR) handle;
204 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
206 HANDLE handle = (HANDLE) hf;
207 DWORD dwRead;
208 if (ReadFile(handle, pv, cb, &dwRead, NULL))
209 return dwRead;
210 return 0;
213 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
215 HANDLE handle = (HANDLE) hf;
216 DWORD dwWritten;
217 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
218 return dwWritten;
219 return 0;
222 static int cabinet_close(INT_PTR hf)
224 HANDLE handle = (HANDLE) hf;
225 return CloseHandle(handle) ? 0 : -1;
228 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
230 HANDLE handle = (HANDLE) hf;
231 /* flags are compatible and so are passed straight through */
232 return SetFilePointer(handle, dist, NULL, seektype);
235 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
237 MSIRECORD *uirow;
238 LPWSTR uipath, p;
240 /* the UI chunk */
241 uirow = MSI_CreateRecord( 9 );
242 MSI_RecordSetStringW( uirow, 1, f->FileName );
243 uipath = strdupW( f->TargetPath );
244 p = strrchrW(uipath,'\\');
245 if (p)
246 p[1]=0;
247 MSI_RecordSetStringW( uirow, 9, uipath);
248 MSI_RecordSetInteger( uirow, 6, f->FileSize );
249 ui_actiondata( package, action, uirow);
250 msiobj_release( &uirow->hdr );
251 msi_free( uipath );
252 ui_progress( package, 2, f->FileSize, 0, 0);
255 static UINT msi_media_get_disk_info( MSIPACKAGE *package, MSIMEDIAINFO *mi )
257 MSIRECORD *row;
258 LPWSTR ptr;
260 static const WCHAR query[] =
261 {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
262 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
263 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
265 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
266 if (!row)
268 TRACE("Unable to query row\n");
269 return ERROR_FUNCTION_FAILED;
272 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
273 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
274 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
276 if (!mi->first_volume)
277 mi->first_volume = strdupW(mi->volume_label);
279 ptr = strrchrW(mi->source, '\\') + 1;
280 lstrcpyW(ptr, mi->cabinet);
281 msiobj_release(&row->hdr);
283 return ERROR_SUCCESS;
286 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
288 TRACE("(%d)\n", fdint);
290 switch (fdint)
292 case fdintPARTIAL_FILE:
294 CabData *data = (CabData *)pfdin->pv;
295 data->mi->is_continuous = FALSE;
296 return 0;
298 case fdintNEXT_CABINET:
300 CabData *data = (CabData *)pfdin->pv;
301 MSIMEDIAINFO *mi = data->mi;
302 LPWSTR cab = strdupAtoW(pfdin->psz1);
303 UINT rc;
305 msi_free(mi->disk_prompt);
306 msi_free(mi->cabinet);
307 msi_free(mi->volume_label);
308 mi->disk_prompt = NULL;
309 mi->cabinet = NULL;
310 mi->volume_label = NULL;
312 mi->disk_id++;
313 mi->is_continuous = TRUE;
315 rc = msi_media_get_disk_info(data->package, mi);
316 if (rc != ERROR_SUCCESS)
318 msi_free(cab);
319 ERR("Failed to get next cabinet information: %d\n", rc);
320 return -1;
323 if (lstrcmpiW(mi->cabinet, cab))
325 msi_free(cab);
326 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
327 return -1;
330 msi_free(cab);
332 TRACE("Searching for %s\n", debugstr_w(mi->source));
334 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
335 rc = msi_change_media(data->package, mi);
337 if (rc != ERROR_SUCCESS)
338 return -1;
340 return 0;
342 case fdintCOPY_FILE:
344 CabData *data = (CabData*) pfdin->pv;
345 HANDLE handle;
346 LPWSTR file;
347 MSIFILE *f;
348 DWORD attrs;
350 file = strdupAtoW(pfdin->psz1);
351 f = get_loaded_file(data->package, file);
352 msi_free(file);
354 if (!f)
356 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
357 return 0;
360 if (f->state != msifs_missing && f->state != msifs_overwrite)
362 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
363 return 0;
366 msi_file_update_ui( data->package, f, szInstallFiles );
368 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
370 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
371 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
373 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
374 NULL, CREATE_ALWAYS, attrs, NULL );
375 if ( handle == INVALID_HANDLE_VALUE )
377 if ( GetFileAttributesW( f->TargetPath ) != INVALID_FILE_ATTRIBUTES )
378 f->state = msifs_installed;
379 else
380 ERR("failed to create %s (error %d)\n",
381 debugstr_w( f->TargetPath ), GetLastError() );
383 return 0;
386 f->state = msifs_installed;
387 return (INT_PTR) handle;
389 case fdintCLOSE_FILE_INFO:
391 CabData *data = (CabData*) pfdin->pv;
392 FILETIME ft;
393 FILETIME ftLocal;
394 HANDLE handle = (HANDLE) pfdin->hf;
396 data->mi->is_continuous = FALSE;
398 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
399 return -1;
400 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
401 return -1;
402 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
403 return -1;
404 CloseHandle(handle);
405 return 1;
407 default:
408 return 0;
412 /***********************************************************************
413 * msi_cabextract
415 * Extract files from a cab file.
417 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi,
418 PFNFDINOTIFY notify, LPVOID data)
420 LPSTR cabinet, cab_path = NULL;
421 LPWSTR ptr;
422 HFDI hfdi;
423 ERF erf;
424 BOOL ret = FALSE;
426 TRACE("Extracting %s\n", debugstr_w(mi->source));
428 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
429 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
430 if (!hfdi)
432 ERR("FDICreate failed\n");
433 return FALSE;
436 ptr = strrchrW(mi->source, '\\') + 1;
437 cabinet = strdupWtoA(ptr);
438 if (!cabinet)
439 goto done;
441 cab_path = strdupWtoA(mi->source);
442 if (!cab_path)
443 goto done;
445 cab_path[ptr - mi->source] = '\0';
447 ret = FDICopy(hfdi, cabinet, cab_path, 0, notify, NULL, data);
448 if (!ret)
449 ERR("FDICopy failed\n");
451 done:
452 FDIDestroy(hfdi);
453 msi_free(cabinet);
454 msi_free(cab_path);
456 if (ret)
457 mi->is_extracted = TRUE;
459 return ret;
462 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, LPCWSTR path)
464 if (!file->IsCompressed)
466 LPWSTR p, path;
467 p = resolve_folder(package, file->Component->Directory, TRUE, FALSE, TRUE, NULL);
468 path = build_directory_name(2, p, file->ShortName);
469 if (file->LongName &&
470 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 void msi_free_media_info( MSIMEDIAINFO *mi )
484 msi_free( mi->disk_prompt );
485 msi_free( mi->cabinet );
486 msi_free( mi->volume_label );
487 msi_free( mi->first_volume );
488 msi_free( mi );
491 UINT msi_load_media_info(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
493 MSIRECORD *row;
494 LPWSTR source_dir;
495 LPWSTR source;
496 DWORD options;
497 UINT r;
499 static const WCHAR query[] = {
500 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
501 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
502 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
503 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
504 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
505 '`','D','i','s','k','I','d','`',0
508 row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
509 if (!row)
511 TRACE("Unable to query row\n");
512 return ERROR_FUNCTION_FAILED;
515 mi->is_extracted = FALSE;
516 mi->disk_id = MSI_RecordGetInteger(row, 1);
517 mi->last_sequence = MSI_RecordGetInteger(row, 2);
518 msi_free(mi->disk_prompt);
519 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
520 msi_free(mi->cabinet);
521 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
522 msi_free(mi->volume_label);
523 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
524 msiobj_release(&row->hdr);
526 if (!mi->first_volume)
527 mi->first_volume = strdupW(mi->volume_label);
529 source_dir = msi_dup_property(package, cszSourceDir);
530 lstrcpyW(mi->source, source_dir);
532 PathStripToRootW(source_dir);
533 mi->type = GetDriveTypeW(source_dir);
535 if (file->IsCompressed && mi->cabinet)
537 if (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
547 lstrcatW(mi->source, mi->cabinet);
550 options = MSICODE_PRODUCT;
551 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
553 source = source_dir;
554 options |= MSISOURCETYPE_MEDIA;
556 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
558 source = package->BaseURL;
559 options |= MSISOURCETYPE_URL;
561 else
563 source = mi->source;
564 options |= MSISOURCETYPE_NETWORK;
567 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
568 msi_package_add_media_disk(package, package->Context,
569 MSICODE_PRODUCT, mi->disk_id,
570 mi->volume_label, mi->disk_prompt);
572 msi_package_add_info(package, package->Context,
573 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
575 msi_free(source_dir);
576 return ERROR_SUCCESS;
579 /* FIXME: search NETWORK and URL sources as well */
580 UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
582 WCHAR source[MAX_PATH];
583 WCHAR volume[MAX_PATH];
584 WCHAR prompt[MAX_PATH];
585 DWORD volumesz, promptsz;
586 DWORD index, size, id;
587 UINT r;
589 size = MAX_PATH;
590 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
591 package->Context, MSICODE_PRODUCT,
592 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
593 if (r != ERROR_SUCCESS)
594 return r;
596 index = 0;
597 volumesz = MAX_PATH;
598 promptsz = MAX_PATH;
599 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
600 package->Context,
601 MSICODE_PRODUCT, index++, &id,
602 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
604 mi->disk_id = id;
605 mi->volume_label = msi_realloc(mi->volume_label, ++volumesz * sizeof(WCHAR));
606 lstrcpyW(mi->volume_label, volume);
607 mi->disk_prompt = msi_realloc(mi->disk_prompt, ++promptsz * sizeof(WCHAR));
608 lstrcpyW(mi->disk_prompt, prompt);
610 if (source_matches_volume(mi, source))
612 /* FIXME: what about SourceDir */
613 lstrcpyW(mi->source, source);
614 lstrcatW(mi->source, mi->cabinet);
615 return ERROR_SUCCESS;
619 return ERROR_FUNCTION_FAILED;
622 static UINT ready_media(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
624 UINT rc = ERROR_SUCCESS;
626 /* media info for continuous cabinet is already loaded */
627 if (mi->is_continuous)
628 return ERROR_SUCCESS;
630 rc = msi_load_media_info(package, file, mi);
631 if (rc != ERROR_SUCCESS)
633 ERR("Unable to load media info\n");
634 return ERROR_FUNCTION_FAILED;
637 /* cabinet is internal, no checks needed */
638 if (!mi->cabinet || mi->cabinet[0] == '#')
639 return ERROR_SUCCESS;
641 /* package should be downloaded */
642 if (file->IsCompressed &&
643 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
644 package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
646 WCHAR temppath[MAX_PATH];
648 msi_download_file(mi->source, temppath);
649 lstrcpyW(mi->source, temppath);
650 return ERROR_SUCCESS;
653 /* check volume matches, change media if not */
654 if (mi->volume_label && mi->disk_id > 1 &&
655 lstrcmpW(mi->first_volume, mi->volume_label))
657 LPWSTR source = msi_dup_property(package, cszSourceDir);
658 BOOL matches;
660 matches = source_matches_volume(mi, source);
661 msi_free(source);
663 if ((mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE) && !matches)
665 rc = msi_change_media(package, mi);
666 if (rc != ERROR_SUCCESS)
667 return rc;
671 if (file->IsCompressed &&
672 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
674 /* FIXME: this might be done earlier in the install process */
675 rc = find_published_source(package, mi);
676 if (rc != ERROR_SUCCESS)
678 ERR("Cabinet not found: %s\n", debugstr_w(mi->source));
679 return ERROR_INSTALL_FAILURE;
683 return ERROR_SUCCESS;
686 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
687 MSIFILE** file)
689 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
691 if (lstrcmpW( file_key, (*file)->File )==0)
693 if ((*file)->state >= msifs_overwrite)
694 return ERROR_SUCCESS;
695 else
696 return ERROR_FILE_NOT_FOUND;
700 return ERROR_FUNCTION_FAILED;
703 static void schedule_install_files(MSIPACKAGE *package)
705 MSIFILE *file;
707 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
709 if (!ACTION_VerifyComponentForAction(file->Component, INSTALLSTATE_LOCAL))
711 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
713 ui_progress(package,2,file->FileSize,0,0);
714 file->state = msifs_skipped;
719 static UINT copy_file(MSIFILE *file)
721 BOOL ret;
723 ret = CopyFileW(file->SourcePath, file->TargetPath, FALSE);
724 if (ret)
726 file->state = msifs_installed;
727 return ERROR_SUCCESS;
730 return GetLastError();
733 static UINT copy_install_file(MSIFILE *file)
735 UINT gle;
737 TRACE("Copying %s to %s\n", debugstr_w(file->SourcePath),
738 debugstr_w(file->TargetPath));
740 gle = copy_file(file);
741 if (gle == ERROR_SUCCESS)
742 return gle;
744 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
746 TRACE("overwriting existing file\n");
747 gle = ERROR_SUCCESS;
749 else if (gle == ERROR_FILE_NOT_FOUND)
751 /* FIXME: this needs to be tested, I'm pretty sure it fails */
752 TRACE("Source file not found\n");
753 gle = ERROR_SUCCESS;
755 else if (gle == ERROR_ACCESS_DENIED)
757 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
759 gle = copy_file(file);
760 TRACE("Overwriting existing file: %d\n", gle);
762 else if (!(file->Attributes & msidbFileAttributesVital))
764 TRACE("Ignoring error for nonvital\n");
765 gle = ERROR_SUCCESS;
768 return gle;
771 static BOOL check_dest_hash_matches(MSIFILE *file)
773 MSIFILEHASHINFO hash;
774 UINT r;
776 if (!file->hash.dwFileHashInfoSize)
777 return FALSE;
779 hash.dwFileHashInfoSize = sizeof(MSIFILEHASHINFO);
780 r = MsiGetFileHashW(file->TargetPath, 0, &hash);
781 if (r != ERROR_SUCCESS)
782 return FALSE;
784 return !memcmp(&hash, &file->hash, sizeof(MSIFILEHASHINFO));
788 * ACTION_InstallFiles()
790 * For efficiency, this is done in two passes:
791 * 1) Correct all the TargetPaths and determine what files are to be installed.
792 * 2) Extract Cabinets and copy files.
794 UINT ACTION_InstallFiles(MSIPACKAGE *package)
796 MSIMEDIAINFO *mi;
797 UINT rc = ERROR_SUCCESS;
798 MSIFILE *file;
800 /* increment progress bar each time action data is sent */
801 ui_progress(package,1,1,0,0);
803 schedule_install_files(package);
806 * Despite MSDN specifying that the CreateFolders action
807 * should be called before InstallFiles, some installers don't
808 * do that, and they seem to work correctly. We need to create
809 * directories here to make sure that the files can be copied.
811 msi_create_component_directories( package );
813 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
815 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
817 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
818 continue;
820 if (check_dest_hash_matches(file))
822 TRACE("File hashes match, not overwriting\n");
823 continue;
826 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
827 (file->IsCompressed && !mi->is_extracted))
829 CabData data;
831 rc = ready_media(package, file, mi);
832 if (rc != ERROR_SUCCESS)
834 ERR("Failed to ready media\n");
835 break;
838 data.mi = mi;
839 data.package = package;
841 if (file->IsCompressed &&
842 !msi_cabextract(package, mi, cabinet_notify, &data))
844 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
845 rc = ERROR_FUNCTION_FAILED;
846 break;
850 set_file_source(package, file, mi->source);
852 TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
853 debugstr_w(file->TargetPath));
855 if (!file->IsCompressed)
857 msi_file_update_ui(package, file, szInstallFiles);
858 rc = copy_install_file(file);
859 if (rc != ERROR_SUCCESS)
861 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(file->SourcePath),
862 debugstr_w(file->TargetPath), rc);
863 rc = ERROR_INSTALL_FAILURE;
864 break;
867 else if (file->state != msifs_installed)
869 ERR("compressed file wasn't extracted (%s)\n", debugstr_w(file->TargetPath));
870 rc = ERROR_INSTALL_FAILURE;
871 break;
875 msi_free_media_info( mi );
876 return rc;
879 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
881 MSIPACKAGE *package = (MSIPACKAGE*)param;
882 WCHAR dest_name[0x100];
883 LPWSTR dest_path, dest;
884 LPCWSTR file_key, component;
885 DWORD sz;
886 DWORD rc;
887 MSICOMPONENT *comp;
888 MSIFILE *file;
890 component = MSI_RecordGetString(row,2);
891 comp = get_loaded_component(package,component);
893 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
895 TRACE("Skipping copy due to disabled component %s\n",
896 debugstr_w(component));
898 /* the action taken was the same as the current install state */
899 comp->Action = comp->Installed;
901 return ERROR_SUCCESS;
904 comp->Action = INSTALLSTATE_LOCAL;
906 file_key = MSI_RecordGetString(row,3);
907 if (!file_key)
909 ERR("Unable to get file key\n");
910 return ERROR_FUNCTION_FAILED;
913 rc = get_file_target(package,file_key,&file);
915 if (rc != ERROR_SUCCESS)
917 ERR("Original file unknown %s\n",debugstr_w(file_key));
918 return ERROR_SUCCESS;
921 if (MSI_RecordIsNull(row,4))
922 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
923 else
925 sz=0x100;
926 MSI_RecordGetStringW(row,4,dest_name,&sz);
927 reduce_to_longfilename(dest_name);
930 if (MSI_RecordIsNull(row,5))
932 LPWSTR p;
933 dest_path = strdupW(file->TargetPath);
934 p = strrchrW(dest_path,'\\');
935 if (p)
936 *p=0;
938 else
940 LPCWSTR destkey;
941 destkey = MSI_RecordGetString(row,5);
942 dest_path = resolve_folder(package, destkey, FALSE, FALSE, TRUE, NULL);
943 if (!dest_path)
945 /* try a Property */
946 dest_path = msi_dup_property( package, destkey );
947 if (!dest_path)
949 FIXME("Unable to get destination folder, try AppSearch properties\n");
950 return ERROR_SUCCESS;
955 dest = build_directory_name(2, dest_path, dest_name);
956 create_full_pathW(dest_path);
958 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
959 debugstr_w(dest));
961 if (strcmpW(file->TargetPath,dest))
962 rc = !CopyFileW(file->TargetPath,dest,TRUE);
963 else
964 rc = ERROR_SUCCESS;
966 if (rc != ERROR_SUCCESS)
967 ERR("Failed to copy file %s -> %s, last error %d\n",
968 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
970 FIXME("We should track these duplicate files as well\n");
972 msi_free(dest_path);
973 msi_free(dest);
975 msi_file_update_ui(package, file, szDuplicateFiles);
977 return ERROR_SUCCESS;
980 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
982 UINT rc;
983 MSIQUERY * view;
984 static const WCHAR ExecSeqQuery[] =
985 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
986 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
988 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
989 if (rc != ERROR_SUCCESS)
990 return ERROR_SUCCESS;
992 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
993 msiobj_release(&view->hdr);
995 return rc;
998 /* compares the version of a file read from the filesystem and
999 * the version specified in the File table
1001 static int msi_compare_file_version( MSIFILE *file )
1003 WCHAR version[MAX_PATH];
1004 DWORD size;
1005 UINT r;
1007 size = MAX_PATH;
1008 version[0] = '\0';
1009 r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
1010 if ( r != ERROR_SUCCESS )
1011 return 0;
1013 return lstrcmpW( version, file->Version );
1016 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1018 MSIFILE *file;
1020 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1022 MSIRECORD *uirow;
1023 LPWSTR uipath, p;
1025 if ( !file->Component )
1026 continue;
1027 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
1028 continue;
1030 if ( file->state == msifs_installed )
1031 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1033 if ( file->state != msifs_present )
1034 continue;
1036 /* only remove a file if the version to be installed
1037 * is strictly newer than the old file
1039 if ( msi_compare_file_version( file ) >= 0 )
1040 continue;
1042 TRACE("removing %s\n", debugstr_w(file->File) );
1043 if ( !DeleteFileW( file->TargetPath ) )
1044 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
1045 file->state = msifs_missing;
1047 /* the UI chunk */
1048 uirow = MSI_CreateRecord( 9 );
1049 MSI_RecordSetStringW( uirow, 1, file->FileName );
1050 uipath = strdupW( file->TargetPath );
1051 p = strrchrW(uipath,'\\');
1052 if (p)
1053 p[1]=0;
1054 MSI_RecordSetStringW( uirow, 9, uipath);
1055 ui_actiondata( package, szRemoveFiles, uirow);
1056 msiobj_release( &uirow->hdr );
1057 msi_free( uipath );
1058 /* FIXME: call ui_progress here? */
1061 return ERROR_SUCCESS;