msi: Clean up the database queries.
[wine/multimedia.git] / dlls / msi / files.c
blob348ff14d9cddbdf015edcb8f8d8d6e9b403aa7c3
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:
25 * InstallFiles
26 * DuplicateFiles
27 * MoveFiles
28 * PatchFiles
29 * RemoveDuplicateFiles
30 * RemoveFiles
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 "msipriv.h"
43 #include "winuser.h"
44 #include "winreg.h"
45 #include "shlwapi.h"
46 #include "wine/unicode.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msi);
50 static HMODULE hmspatcha;
51 static BOOL (WINAPI *ApplyPatchToFileW)(LPCWSTR, LPCWSTR, LPCWSTR, ULONG);
53 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
55 MSIRECORD *uirow;
57 uirow = MSI_CreateRecord( 9 );
58 MSI_RecordSetStringW( uirow, 1, f->FileName );
59 MSI_RecordSetStringW( uirow, 9, f->Component->Directory );
60 MSI_RecordSetInteger( uirow, 6, f->FileSize );
61 msi_ui_actiondata( package, action, uirow );
62 msiobj_release( &uirow->hdr );
63 msi_ui_progress( package, 2, f->FileSize, 0, 0 );
66 static msi_file_state calculate_install_state( MSIPACKAGE *package, MSIFILE *file )
68 MSICOMPONENT *comp = file->Component;
69 VS_FIXEDFILEINFO *file_version;
70 WCHAR *font_version;
71 msi_file_state state;
72 DWORD file_size;
74 comp->Action = msi_get_component_action( package, comp );
75 if (comp->Action != INSTALLSTATE_LOCAL || (comp->assembly && comp->assembly->installed))
77 TRACE("file %s is not scheduled for install\n", debugstr_w(file->File));
78 return msifs_skipped;
80 if ((comp->assembly && !comp->assembly->application && !comp->assembly->installed) ||
81 GetFileAttributesW( file->TargetPath ) == INVALID_FILE_ATTRIBUTES)
83 TRACE("file %s is missing\n", debugstr_w(file->File));
84 return msifs_missing;
86 if (file->Version)
88 if ((file_version = msi_get_disk_file_version( file->TargetPath )))
90 TRACE("new %s old %u.%u.%u.%u\n", debugstr_w(file->Version),
91 HIWORD(file_version->dwFileVersionMS),
92 LOWORD(file_version->dwFileVersionMS),
93 HIWORD(file_version->dwFileVersionLS),
94 LOWORD(file_version->dwFileVersionLS));
96 if (msi_compare_file_versions( file_version, file->Version ) < 0)
97 state = msifs_overwrite;
98 else
100 TRACE("destination file version equal or greater, not overwriting\n");
101 state = msifs_present;
103 msi_free( file_version );
104 return state;
106 else if ((font_version = font_version_from_file( file->TargetPath )))
108 TRACE("new %s old %s\n", debugstr_w(file->Version), debugstr_w(font_version));
110 if (msi_compare_font_versions( font_version, file->Version ) < 0)
111 state = msifs_overwrite;
112 else
114 TRACE("destination file version equal or greater, not overwriting\n");
115 state = msifs_present;
117 msi_free( font_version );
118 return state;
121 if ((file_size = msi_get_disk_file_size( file->TargetPath )) != file->FileSize)
123 return msifs_overwrite;
125 if (file->hash.dwFileHashInfoSize)
127 if (msi_file_hash_matches( file ))
129 TRACE("file hashes match, not overwriting\n");
130 return msifs_hashmatch;
132 else
134 TRACE("file hashes do not match, overwriting\n");
135 return msifs_overwrite;
138 /* assume present */
139 return msifs_present;
142 static void schedule_install_files(MSIPACKAGE *package)
144 MSIFILE *file;
146 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
148 MSICOMPONENT *comp = file->Component;
150 file->state = calculate_install_state( package, file );
151 if (file->state == msifs_overwrite && (comp->Attributes & msidbComponentAttributesNeverOverwrite))
153 TRACE("not overwriting %s\n", debugstr_w(file->TargetPath));
154 file->state = msifs_skipped;
159 static UINT copy_file(MSIFILE *file, LPWSTR source)
161 BOOL ret;
163 ret = CopyFileW(source, file->TargetPath, FALSE);
164 if (!ret)
165 return GetLastError();
167 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
169 file->state = msifs_installed;
170 return ERROR_SUCCESS;
173 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
175 UINT gle;
177 TRACE("Copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
179 gle = copy_file(file, source);
180 if (gle == ERROR_SUCCESS)
181 return gle;
183 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
185 TRACE("overwriting existing file\n");
186 return ERROR_SUCCESS;
188 else if (gle == ERROR_ACCESS_DENIED)
190 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
192 gle = copy_file(file, source);
193 TRACE("Overwriting existing file: %d\n", gle);
195 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
197 WCHAR *tmpfileW, *pathW, *p;
198 DWORD len;
200 TRACE("file in use, scheduling rename operation\n");
202 if (!(pathW = strdupW( file->TargetPath ))) return ERROR_OUTOFMEMORY;
203 if ((p = strrchrW(pathW, '\\'))) *p = 0;
204 len = strlenW( pathW ) + 16;
205 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
207 msi_free( pathW );
208 return ERROR_OUTOFMEMORY;
210 if (!GetTempFileNameW( pathW, szMsi, 0, tmpfileW )) tmpfileW[0] = 0;
211 msi_free( pathW );
213 if (CopyFileW(source, tmpfileW, FALSE) &&
214 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
215 MoveFileExW(tmpfileW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
217 file->state = msifs_installed;
218 package->need_reboot = 1;
219 gle = ERROR_SUCCESS;
221 else
223 gle = GetLastError();
224 WARN("failed to schedule rename operation: %d)\n", gle);
225 DeleteFileW( tmpfileW );
227 msi_free(tmpfileW);
230 return gle;
233 static UINT msi_create_directory( MSIPACKAGE *package, const WCHAR *dir )
235 MSIFOLDER *folder;
236 const WCHAR *install_path;
238 install_path = msi_get_target_folder( package, dir );
239 if (!install_path) return ERROR_FUNCTION_FAILED;
241 folder = msi_get_loaded_folder( package, dir );
242 if (folder->State == FOLDER_STATE_UNINITIALIZED)
244 msi_create_full_path( install_path );
245 folder->State = FOLDER_STATE_CREATED;
247 return ERROR_SUCCESS;
250 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
251 LPWSTR *path, DWORD *attrs, PVOID user)
253 static MSIFILE *f = NULL;
254 UINT_PTR disk_id = (UINT_PTR)user;
256 if (action == MSICABEXTRACT_BEGINEXTRACT)
258 f = msi_get_loaded_file(package, file);
259 if (!f)
261 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
262 return FALSE;
264 if (f->disk_id != disk_id || (f->state != msifs_missing && f->state != msifs_overwrite))
265 return FALSE;
267 if (!f->Component->assembly || f->Component->assembly->application)
269 msi_create_directory(package, f->Component->Directory);
271 *path = strdupW(f->TargetPath);
272 *attrs = f->Attributes;
274 else if (action == MSICABEXTRACT_FILEEXTRACTED)
276 f->state = msifs_installed;
277 f = NULL;
280 return TRUE;
283 WCHAR *msi_resolve_file_source( MSIPACKAGE *package, MSIFILE *file )
285 WCHAR *p, *path;
287 TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
289 if (file->IsCompressed) return NULL;
291 p = msi_resolve_source_folder( package, file->Component->Directory, NULL );
292 path = msi_build_directory_name( 2, p, file->ShortName );
294 if (file->LongName && GetFileAttributesW( path ) == INVALID_FILE_ATTRIBUTES)
296 msi_free( path );
297 path = msi_build_directory_name( 2, p, file->LongName );
299 msi_free( p );
300 TRACE("file %s source resolves to %s\n", debugstr_w(file->File), debugstr_w(path));
301 return path;
305 * ACTION_InstallFiles()
307 * For efficiency, this is done in two passes:
308 * 1) Correct all the TargetPaths and determine what files are to be installed.
309 * 2) Extract Cabinets and copy files.
311 UINT ACTION_InstallFiles(MSIPACKAGE *package)
313 MSIMEDIAINFO *mi;
314 MSICOMPONENT *comp;
315 UINT rc = ERROR_SUCCESS;
316 MSIFILE *file;
318 schedule_install_files(package);
319 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
321 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
323 msi_file_update_ui( package, file, szInstallFiles );
325 rc = msi_load_media_info( package, file->Sequence, mi );
326 if (rc != ERROR_SUCCESS)
328 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
329 return ERROR_FUNCTION_FAILED;
331 if (!file->Component->Enabled) continue;
333 if (file->state != msifs_hashmatch &&
334 (rc = ready_media( package, file->Sequence, file->IsCompressed, mi )))
336 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
337 goto done;
340 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
341 continue;
343 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
344 (file->IsCompressed && !mi->is_extracted))
346 MSICABDATA data;
348 data.mi = mi;
349 data.package = package;
350 data.cb = installfiles_cb;
351 data.user = (PVOID)(UINT_PTR)mi->disk_id;
353 if (file->IsCompressed &&
354 !msi_cabextract(package, mi, &data))
356 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
357 rc = ERROR_INSTALL_FAILURE;
358 goto done;
362 if (!file->IsCompressed)
364 WCHAR *source = msi_resolve_file_source(package, file);
366 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
368 if (!file->Component->assembly || file->Component->assembly->application)
370 msi_create_directory(package, file->Component->Directory);
372 rc = copy_install_file(package, file, source);
373 if (rc != ERROR_SUCCESS)
375 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
376 debugstr_w(file->TargetPath), rc);
377 rc = ERROR_INSTALL_FAILURE;
378 msi_free(source);
379 goto done;
381 msi_free(source);
383 else if (file->state != msifs_installed && !(file->Attributes & msidbFileAttributesPatchAdded))
385 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->TargetPath));
386 rc = ERROR_INSTALL_FAILURE;
387 goto done;
390 msi_init_assembly_caches( package );
391 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
393 comp->Action = msi_get_component_action( package, comp );
394 if (comp->Action == INSTALLSTATE_LOCAL && comp->assembly && !comp->assembly->installed)
396 rc = msi_install_assembly( package, comp );
397 if (rc != ERROR_SUCCESS)
399 ERR("Failed to install assembly\n");
400 rc = ERROR_INSTALL_FAILURE;
401 break;
405 msi_destroy_assembly_caches( package );
407 done:
408 msi_free_media_info(mi);
409 return rc;
412 static BOOL load_mspatcha(void)
414 hmspatcha = LoadLibraryA("mspatcha.dll");
415 if (!hmspatcha)
417 ERR("Failed to load mspatcha.dll: %d\n", GetLastError());
418 return FALSE;
421 ApplyPatchToFileW = (void*)GetProcAddress(hmspatcha, "ApplyPatchToFileW");
422 if(!ApplyPatchToFileW)
424 ERR("GetProcAddress(ApplyPatchToFileW) failed: %d.\n", GetLastError());
425 return FALSE;
428 return TRUE;
431 static void unload_mspatch(void)
433 FreeLibrary(hmspatcha);
436 static BOOL patchfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
437 LPWSTR *path, DWORD *attrs, PVOID user)
439 static MSIFILEPATCH *p = NULL;
440 static WCHAR patch_path[MAX_PATH] = {0};
441 static WCHAR temp_folder[MAX_PATH] = {0};
443 if (action == MSICABEXTRACT_BEGINEXTRACT)
445 if (temp_folder[0] == '\0')
446 GetTempPathW(MAX_PATH, temp_folder);
448 p = msi_get_loaded_filepatch(package, file);
449 if (!p)
451 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
452 return FALSE;
454 GetTempFileNameW(temp_folder, NULL, 0, patch_path);
456 *path = strdupW(patch_path);
457 *attrs = p->File->Attributes;
459 else if (action == MSICABEXTRACT_FILEEXTRACTED)
461 WCHAR patched_file[MAX_PATH];
462 BOOL br;
464 GetTempFileNameW(temp_folder, NULL, 0, patched_file);
466 br = ApplyPatchToFileW(patch_path, p->File->TargetPath, patched_file, 0);
467 if (br)
469 /* FIXME: baseline cache */
471 DeleteFileW( p->File->TargetPath );
472 MoveFileW( patched_file, p->File->TargetPath );
474 p->IsApplied = TRUE;
476 else
477 ERR("Failed patch %s: %d.\n", debugstr_w(p->File->TargetPath), GetLastError());
479 DeleteFileW(patch_path);
480 p = NULL;
483 return TRUE;
486 UINT ACTION_PatchFiles( MSIPACKAGE *package )
488 MSIFILEPATCH *patch;
489 MSIMEDIAINFO *mi;
490 UINT rc = ERROR_SUCCESS;
491 BOOL mspatcha_loaded = FALSE;
493 TRACE("%p\n", package);
495 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
497 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
499 MSIFILE *file = patch->File;
501 rc = msi_load_media_info( package, patch->Sequence, mi );
502 if (rc != ERROR_SUCCESS)
504 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
505 return ERROR_FUNCTION_FAILED;
507 if (!file->Component->Enabled) continue;
509 if (!patch->IsApplied)
511 MSICABDATA data;
513 rc = ready_media( package, patch->Sequence, TRUE, mi );
514 if (rc != ERROR_SUCCESS)
516 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
517 goto done;
520 if (!mspatcha_loaded && !load_mspatcha())
522 rc = ERROR_FUNCTION_FAILED;
523 goto done;
525 mspatcha_loaded = TRUE;
527 data.mi = mi;
528 data.package = package;
529 data.cb = patchfiles_cb;
530 data.user = (PVOID)(UINT_PTR)mi->disk_id;
532 if (!msi_cabextract(package, mi, &data))
534 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
535 rc = ERROR_INSTALL_FAILURE;
536 goto done;
540 if (!patch->IsApplied && !(patch->Attributes & msidbPatchAttributesNonVital))
542 ERR("Failed to apply patch to file: %s\n", debugstr_w(file->File));
543 rc = ERROR_INSTALL_FAILURE;
544 goto done;
548 done:
549 msi_free_media_info(mi);
550 if (mspatcha_loaded)
551 unload_mspatch();
552 return rc;
555 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
557 typedef struct
559 struct list entry;
560 LPWSTR sourcename;
561 LPWSTR destname;
562 LPWSTR source;
563 LPWSTR dest;
564 } FILE_LIST;
566 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
568 BOOL ret;
570 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
571 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
573 WARN("Source or dest is directory, not moving\n");
574 return FALSE;
577 if (options == msidbMoveFileOptionsMove)
579 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
580 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
581 if (!ret)
583 WARN("MoveFile failed: %d\n", GetLastError());
584 return FALSE;
587 else
589 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
590 ret = CopyFileW(source, dest, FALSE);
591 if (!ret)
593 WARN("CopyFile failed: %d\n", GetLastError());
594 return FALSE;
598 return TRUE;
601 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
603 LPWSTR path, ptr;
604 DWORD dirlen, pathlen;
606 ptr = strrchrW(wildcard, '\\');
607 dirlen = ptr - wildcard + 1;
609 pathlen = dirlen + lstrlenW(filename) + 1;
610 path = msi_alloc(pathlen * sizeof(WCHAR));
612 lstrcpynW(path, wildcard, dirlen + 1);
613 lstrcatW(path, filename);
615 return path;
618 static void free_file_entry(FILE_LIST *file)
620 msi_free(file->source);
621 msi_free(file->dest);
622 msi_free(file);
625 static void free_list(FILE_LIST *list)
627 while (!list_empty(&list->entry))
629 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
631 list_remove(&file->entry);
632 free_file_entry(file);
636 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
638 FILE_LIST *new, *file;
639 LPWSTR ptr, filename;
640 DWORD size;
642 new = msi_alloc_zero(sizeof(FILE_LIST));
643 if (!new)
644 return FALSE;
646 new->source = strdupW(source);
647 ptr = strrchrW(dest, '\\') + 1;
648 filename = strrchrW(new->source, '\\') + 1;
650 new->sourcename = filename;
652 if (*ptr)
653 new->destname = ptr;
654 else
655 new->destname = new->sourcename;
657 size = (ptr - dest) + lstrlenW(filename) + 1;
658 new->dest = msi_alloc(size * sizeof(WCHAR));
659 if (!new->dest)
661 free_file_entry(new);
662 return FALSE;
665 lstrcpynW(new->dest, dest, ptr - dest + 1);
666 lstrcatW(new->dest, filename);
668 if (list_empty(&files->entry))
670 list_add_head(&files->entry, &new->entry);
671 return TRUE;
674 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
676 if (strcmpW( source, file->source ) < 0)
678 list_add_before(&file->entry, &new->entry);
679 return TRUE;
683 list_add_after(&file->entry, &new->entry);
684 return TRUE;
687 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
689 WIN32_FIND_DATAW wfd;
690 HANDLE hfile;
691 LPWSTR path;
692 BOOL res;
693 FILE_LIST files, *file;
694 DWORD size;
696 hfile = FindFirstFileW(source, &wfd);
697 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
699 list_init(&files.entry);
701 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
703 if (is_dot_dir(wfd.cFileName)) continue;
705 path = wildcard_to_file(source, wfd.cFileName);
706 if (!path)
708 res = FALSE;
709 goto done;
712 add_wildcard(&files, path, dest);
713 msi_free(path);
716 /* no files match the wildcard */
717 if (list_empty(&files.entry))
718 goto done;
720 /* only the first wildcard match gets renamed to dest */
721 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
722 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
723 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
724 if (!file->dest)
726 res = FALSE;
727 goto done;
730 /* file->dest may be shorter after the reallocation, so add a NULL
731 * terminator. This is needed for the call to strrchrW, as there will no
732 * longer be a NULL terminator within the bounds of the allocation in this case.
734 file->dest[size - 1] = '\0';
735 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
737 while (!list_empty(&files.entry))
739 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
741 msi_move_file(file->source, file->dest, options);
743 list_remove(&file->entry);
744 free_file_entry(file);
747 res = TRUE;
749 done:
750 free_list(&files);
751 FindClose(hfile);
752 return res;
755 void msi_reduce_to_long_filename( WCHAR *filename )
757 WCHAR *p = strchrW( filename, '|' );
758 if (p) memmove( filename, p + 1, (strlenW( p + 1 ) + 1) * sizeof(WCHAR) );
761 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
763 MSIPACKAGE *package = param;
764 MSIRECORD *uirow;
765 MSICOMPONENT *comp;
766 LPCWSTR sourcename, component;
767 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
768 int options;
769 DWORD size;
770 BOOL ret, wildcards;
772 component = MSI_RecordGetString(rec, 2);
773 comp = msi_get_loaded_component(package, component);
774 if (!comp)
775 return ERROR_SUCCESS;
777 comp->Action = msi_get_component_action( package, comp );
778 if (comp->Action != INSTALLSTATE_LOCAL)
780 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
781 return ERROR_SUCCESS;
784 sourcename = MSI_RecordGetString(rec, 3);
785 options = MSI_RecordGetInteger(rec, 7);
787 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
788 if (!sourcedir)
789 goto done;
791 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
792 if (!destdir)
793 goto done;
795 if (!sourcename)
797 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
798 goto done;
800 source = strdupW(sourcedir);
801 if (!source)
802 goto done;
804 else
806 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
807 source = msi_alloc(size * sizeof(WCHAR));
808 if (!source)
809 goto done;
811 lstrcpyW(source, sourcedir);
812 if (source[lstrlenW(source) - 1] != '\\')
813 lstrcatW(source, szBackSlash);
814 lstrcatW(source, sourcename);
817 wildcards = strchrW(source, '*') || strchrW(source, '?');
819 if (MSI_RecordIsNull(rec, 4))
821 if (!wildcards)
823 destname = strdupW(sourcename);
824 if (!destname)
825 goto done;
828 else
830 destname = strdupW(MSI_RecordGetString(rec, 4));
831 if (destname) msi_reduce_to_long_filename(destname);
834 size = 0;
835 if (destname)
836 size = lstrlenW(destname);
838 size += lstrlenW(destdir) + 2;
839 dest = msi_alloc(size * sizeof(WCHAR));
840 if (!dest)
841 goto done;
843 lstrcpyW(dest, destdir);
844 if (dest[lstrlenW(dest) - 1] != '\\')
845 lstrcatW(dest, szBackSlash);
847 if (destname)
848 lstrcatW(dest, destname);
850 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
852 if (!(ret = msi_create_full_path(destdir)))
854 WARN("failed to create directory %u\n", GetLastError());
855 goto done;
859 if (!wildcards)
860 msi_move_file(source, dest, options);
861 else
862 move_files_wildcard(source, dest, options);
864 done:
865 uirow = MSI_CreateRecord( 9 );
866 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
867 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
868 MSI_RecordSetStringW( uirow, 9, destdir );
869 msi_ui_actiondata( package, szMoveFiles, uirow );
870 msiobj_release( &uirow->hdr );
872 msi_free(sourcedir);
873 msi_free(destdir);
874 msi_free(destname);
875 msi_free(source);
876 msi_free(dest);
878 return ERROR_SUCCESS;
881 UINT ACTION_MoveFiles( MSIPACKAGE *package )
883 static const WCHAR query[] = {
884 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
885 '`','M','o','v','e','F','i','l','e','`',0};
886 MSIQUERY *view;
887 UINT rc;
889 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
890 if (rc != ERROR_SUCCESS)
891 return ERROR_SUCCESS;
893 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
894 msiobj_release(&view->hdr);
895 return rc;
898 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
900 DWORD len;
901 WCHAR *dst_name, *dst_path, *dst;
903 if (MSI_RecordIsNull( row, 4 ))
905 len = strlenW( src ) + 1;
906 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
907 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
909 else
911 MSI_RecordGetStringW( row, 4, NULL, &len );
912 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
913 MSI_RecordGetStringW( row, 4, dst_name, &len );
914 msi_reduce_to_long_filename( dst_name );
917 if (MSI_RecordIsNull( row, 5 ))
919 WCHAR *p;
920 dst_path = strdupW( src );
921 p = strrchrW( dst_path, '\\' );
922 if (p) *p = 0;
924 else
926 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
928 dst_path = strdupW( msi_get_target_folder( package, dst_key ) );
929 if (!dst_path)
931 /* try a property */
932 dst_path = msi_dup_property( package->db, dst_key );
933 if (!dst_path)
935 FIXME("Unable to get destination folder, try AppSearch properties\n");
936 msi_free( dst_name );
937 return NULL;
942 dst = msi_build_directory_name( 2, dst_path, dst_name );
943 msi_create_full_path( dst_path );
945 msi_free( dst_name );
946 msi_free( dst_path );
947 return dst;
950 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
952 MSIPACKAGE *package = param;
953 LPWSTR dest;
954 LPCWSTR file_key, component;
955 MSICOMPONENT *comp;
956 MSIRECORD *uirow;
957 MSIFILE *file;
959 component = MSI_RecordGetString(row,2);
960 comp = msi_get_loaded_component(package, component);
961 if (!comp)
962 return ERROR_SUCCESS;
964 comp->Action = msi_get_component_action( package, comp );
965 if (comp->Action != INSTALLSTATE_LOCAL)
967 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
968 return ERROR_SUCCESS;
971 file_key = MSI_RecordGetString(row,3);
972 if (!file_key)
974 ERR("Unable to get file key\n");
975 return ERROR_FUNCTION_FAILED;
978 file = msi_get_loaded_file( package, file_key );
979 if (!file)
981 ERR("Original file unknown %s\n", debugstr_w(file_key));
982 return ERROR_SUCCESS;
985 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
986 if (!dest)
988 WARN("Unable to get duplicate filename\n");
989 return ERROR_SUCCESS;
992 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
994 if (!CopyFileW( file->TargetPath, dest, TRUE ))
996 WARN("Failed to copy file %s -> %s (%u)\n",
997 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
1000 FIXME("We should track these duplicate files as well\n");
1002 uirow = MSI_CreateRecord( 9 );
1003 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1004 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1005 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1006 msi_ui_actiondata( package, szDuplicateFiles, uirow );
1007 msiobj_release( &uirow->hdr );
1009 msi_free(dest);
1010 return ERROR_SUCCESS;
1013 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1015 static const WCHAR query[] = {
1016 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1017 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1018 MSIQUERY *view;
1019 UINT rc;
1021 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
1022 if (rc != ERROR_SUCCESS)
1023 return ERROR_SUCCESS;
1025 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1026 msiobj_release(&view->hdr);
1027 return rc;
1030 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1032 MSIPACKAGE *package = param;
1033 LPWSTR dest;
1034 LPCWSTR file_key, component;
1035 MSICOMPONENT *comp;
1036 MSIRECORD *uirow;
1037 MSIFILE *file;
1039 component = MSI_RecordGetString( row, 2 );
1040 comp = msi_get_loaded_component( package, component );
1041 if (!comp)
1042 return ERROR_SUCCESS;
1044 comp->Action = msi_get_component_action( package, comp );
1045 if (comp->Action != INSTALLSTATE_ABSENT)
1047 TRACE("component not scheduled for removal %s\n", debugstr_w(component));
1048 return ERROR_SUCCESS;
1051 file_key = MSI_RecordGetString( row, 3 );
1052 if (!file_key)
1054 ERR("Unable to get file key\n");
1055 return ERROR_FUNCTION_FAILED;
1058 file = msi_get_loaded_file( package, file_key );
1059 if (!file)
1061 ERR("Original file unknown %s\n", debugstr_w(file_key));
1062 return ERROR_SUCCESS;
1065 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1066 if (!dest)
1068 WARN("Unable to get duplicate filename\n");
1069 return ERROR_SUCCESS;
1072 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1074 if (!DeleteFileW( dest ))
1076 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1079 uirow = MSI_CreateRecord( 9 );
1080 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1081 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1082 msi_ui_actiondata( package, szRemoveDuplicateFiles, uirow );
1083 msiobj_release( &uirow->hdr );
1085 msi_free(dest);
1086 return ERROR_SUCCESS;
1089 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1091 static const WCHAR query[] = {
1092 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1093 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1094 MSIQUERY *view;
1095 UINT rc;
1097 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
1098 if (rc != ERROR_SUCCESS)
1099 return ERROR_SUCCESS;
1101 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1102 msiobj_release( &view->hdr );
1103 return rc;
1106 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1108 /* special case */
1109 if (comp->Action != INSTALLSTATE_SOURCE &&
1110 comp->Attributes & msidbComponentAttributesSourceOnly &&
1111 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1112 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1114 switch (comp->Action)
1116 case INSTALLSTATE_LOCAL:
1117 case INSTALLSTATE_SOURCE:
1118 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1119 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1120 break;
1121 case INSTALLSTATE_ABSENT:
1122 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1123 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1124 break;
1125 default: break;
1127 return FALSE;
1130 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1132 MSIPACKAGE *package = param;
1133 MSICOMPONENT *comp;
1134 MSIRECORD *uirow;
1135 LPCWSTR component, dirprop;
1136 UINT install_mode;
1137 LPWSTR dir = NULL, path = NULL, filename = NULL;
1138 DWORD size;
1139 UINT ret = ERROR_SUCCESS;
1141 component = MSI_RecordGetString(row, 2);
1142 dirprop = MSI_RecordGetString(row, 4);
1143 install_mode = MSI_RecordGetInteger(row, 5);
1145 comp = msi_get_loaded_component(package, component);
1146 if (!comp)
1147 return ERROR_SUCCESS;
1149 comp->Action = msi_get_component_action( package, comp );
1150 if (!verify_comp_for_removal(comp, install_mode))
1152 TRACE("Skipping removal due to install mode\n");
1153 return ERROR_SUCCESS;
1155 if (comp->assembly && !comp->assembly->application)
1157 return ERROR_SUCCESS;
1159 if (comp->Attributes & msidbComponentAttributesPermanent)
1161 TRACE("permanent component, not removing file\n");
1162 return ERROR_SUCCESS;
1165 dir = msi_dup_property(package->db, dirprop);
1166 if (!dir)
1167 return ERROR_OUTOFMEMORY;
1169 size = 0;
1170 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1172 msi_reduce_to_long_filename( filename );
1173 size = lstrlenW( filename );
1175 size += lstrlenW(dir) + 2;
1176 path = msi_alloc(size * sizeof(WCHAR));
1177 if (!path)
1179 ret = ERROR_OUTOFMEMORY;
1180 goto done;
1183 if (filename)
1185 lstrcpyW(path, dir);
1186 PathAddBackslashW(path);
1187 lstrcatW(path, filename);
1189 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1190 DeleteFileW(path);
1192 else
1194 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1195 RemoveDirectoryW(dir);
1198 done:
1199 uirow = MSI_CreateRecord( 9 );
1200 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1201 MSI_RecordSetStringW( uirow, 9, dir );
1202 msi_ui_actiondata( package, szRemoveFiles, uirow );
1203 msiobj_release( &uirow->hdr );
1205 msi_free(filename);
1206 msi_free(path);
1207 msi_free(dir);
1208 return ret;
1211 static void remove_folder( MSIFOLDER *folder )
1213 FolderList *fl;
1215 LIST_FOR_EACH_ENTRY( fl, &folder->children, FolderList, entry )
1217 remove_folder( fl->folder );
1219 if (!folder->persistent && folder->State != FOLDER_STATE_REMOVED)
1221 if (RemoveDirectoryW( folder->ResolvedTarget )) folder->State = FOLDER_STATE_REMOVED;
1225 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1227 static const WCHAR query[] = {
1228 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1229 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1230 MSIQUERY *view;
1231 MSICOMPONENT *comp;
1232 MSIFILE *file;
1233 UINT r;
1235 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1236 if (r == ERROR_SUCCESS)
1238 MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1239 msiobj_release(&view->hdr);
1242 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1244 MSIRECORD *uirow;
1245 VS_FIXEDFILEINFO *ver;
1247 comp = file->Component;
1248 msi_file_update_ui( package, file, szRemoveFiles );
1250 comp->Action = msi_get_component_action( package, comp );
1251 if (comp->Action != INSTALLSTATE_ABSENT || comp->Installed == INSTALLSTATE_SOURCE)
1252 continue;
1254 if (comp->assembly && !comp->assembly->application)
1255 continue;
1257 if (comp->Attributes & msidbComponentAttributesPermanent)
1259 TRACE("permanent component, not removing file\n");
1260 continue;
1263 if (file->Version)
1265 ver = msi_get_disk_file_version( file->TargetPath );
1266 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1268 TRACE("newer version detected, not removing file\n");
1269 msi_free( ver );
1270 continue;
1272 msi_free( ver );
1275 if (file->state == msifs_installed)
1276 WARN("removing installed file %s\n", debugstr_w(file->TargetPath));
1278 TRACE("removing %s\n", debugstr_w(file->File) );
1280 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1281 if (!DeleteFileW( file->TargetPath ))
1283 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1285 file->state = msifs_missing;
1287 uirow = MSI_CreateRecord( 9 );
1288 MSI_RecordSetStringW( uirow, 1, file->FileName );
1289 MSI_RecordSetStringW( uirow, 9, comp->Directory );
1290 msi_ui_actiondata( package, szRemoveFiles, uirow );
1291 msiobj_release( &uirow->hdr );
1293 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
1295 MSIFOLDER *folder;
1297 comp->Action = msi_get_component_action( package, comp );
1298 if (comp->Action != INSTALLSTATE_ABSENT) continue;
1300 if (comp->assembly && !comp->assembly->application) continue;
1302 if (comp->Attributes & msidbComponentAttributesPermanent)
1304 TRACE("permanent component, not removing directory\n");
1305 continue;
1307 folder = msi_get_loaded_folder( package, comp->Directory );
1308 remove_folder( folder );
1310 return ERROR_SUCCESS;