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
29 * RemoveDuplicateFiles(TODO)
38 #include "wine/debug.h"
42 #include "msvcrt/fcntl.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 void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
64 uirow = MSI_CreateRecord( 9 );
65 MSI_RecordSetStringW( uirow, 1, f->FileName );
66 uipath = strdupW( f->TargetPath );
67 p = strrchrW(uipath,'\\');
70 MSI_RecordSetStringW( uirow, 9, uipath);
71 MSI_RecordSetInteger( uirow, 6, f->FileSize );
72 ui_actiondata( package, action, uirow);
73 msiobj_release( &uirow->hdr );
75 ui_progress( package, 2, f->FileSize, 0, 0);
78 /* compares the version of a file read from the filesystem and
79 * the version specified in the File table
81 static int msi_compare_file_version(MSIFILE *file)
83 WCHAR version[MAX_PATH];
89 r = MsiGetFileVersionW(file->TargetPath, version, &size, NULL, NULL);
90 if (r != ERROR_SUCCESS)
93 return lstrcmpW(version, file->Version);
96 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
99 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
101 if (lstrcmpW( file_key, (*file)->File )==0)
103 if ((*file)->state >= msifs_overwrite)
104 return ERROR_SUCCESS;
106 return ERROR_FILE_NOT_FOUND;
110 return ERROR_FUNCTION_FAILED;
113 static void schedule_install_files(MSIPACKAGE *package)
117 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
119 if (!ACTION_VerifyComponentForAction(file->Component, INSTALLSTATE_LOCAL))
121 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
123 ui_progress(package,2,file->FileSize,0,0);
124 file->state = msifs_skipped;
129 static UINT copy_file(MSIFILE *file)
133 ret = CopyFileW(file->SourcePath, file->TargetPath, FALSE);
136 file->state = msifs_installed;
137 return ERROR_SUCCESS;
140 return GetLastError();
143 static UINT copy_install_file(MSIFILE *file)
147 TRACE("Copying %s to %s\n", debugstr_w(file->SourcePath),
148 debugstr_w(file->TargetPath));
150 gle = copy_file(file);
151 if (gle == ERROR_SUCCESS)
154 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
156 TRACE("overwriting existing file\n");
159 else if (gle == ERROR_ACCESS_DENIED)
161 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
163 gle = copy_file(file);
164 TRACE("Overwriting existing file: %d\n", gle);
170 static BOOL check_dest_hash_matches(MSIFILE *file)
172 MSIFILEHASHINFO hash;
175 if (!file->hash.dwFileHashInfoSize)
178 hash.dwFileHashInfoSize = sizeof(MSIFILEHASHINFO);
179 r = MsiGetFileHashW(file->TargetPath, 0, &hash);
180 if (r != ERROR_SUCCESS)
183 return !memcmp(&hash, &file->hash, sizeof(MSIFILEHASHINFO));
186 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
187 LPWSTR *path, DWORD *attrs, PVOID user)
189 static MSIFILE *f = NULL;
191 if (action == MSICABEXTRACT_BEGINEXTRACT)
193 f = get_loaded_file(package, file);
196 WARN("unknown file in cabinet (%s)\n", debugstr_w(file));
200 if (f->state != msifs_missing && f->state != msifs_overwrite)
202 TRACE("Skipping extraction of %s\n", debugstr_w(file));
206 msi_file_update_ui(package, f, szInstallFiles);
208 *path = strdupW(f->TargetPath);
209 *attrs = f->Attributes;
211 else if (action == MSICABEXTRACT_FILEEXTRACTED)
213 f->state = msifs_installed;
221 * ACTION_InstallFiles()
223 * For efficiency, this is done in two passes:
224 * 1) Correct all the TargetPaths and determine what files are to be installed.
225 * 2) Extract Cabinets and copy files.
227 UINT ACTION_InstallFiles(MSIPACKAGE *package)
230 UINT rc = ERROR_SUCCESS;
233 /* increment progress bar each time action data is sent */
234 ui_progress(package,1,1,0,0);
236 schedule_install_files(package);
239 * Despite MSDN specifying that the CreateFolders action
240 * should be called before InstallFiles, some installers don't
241 * do that, and they seem to work correctly. We need to create
242 * directories here to make sure that the files can be copied.
244 msi_create_component_directories( package );
246 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
248 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
250 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
253 if (check_dest_hash_matches(file))
255 TRACE("File hashes match, not overwriting\n");
259 if (MsiGetFileVersionW(file->TargetPath, NULL, NULL, NULL, NULL) == ERROR_SUCCESS &&
260 msi_compare_file_version(file) >= 0)
262 TRACE("Destination file version greater, not overwriting\n");
266 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
267 (file->IsCompressed && !mi->is_extracted))
271 rc = ready_media(package, file, mi);
272 if (rc != ERROR_SUCCESS)
274 ERR("Failed to ready media\n");
279 data.package = package;
280 data.cb = installfiles_cb;
283 if (file->IsCompressed &&
284 !msi_cabextract(package, mi, &data))
286 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
287 rc = ERROR_FUNCTION_FAILED;
292 if (!file->IsCompressed)
294 TRACE("file paths %s to %s\n", debugstr_w(file->SourcePath),
295 debugstr_w(file->TargetPath));
297 msi_file_update_ui(package, file, szInstallFiles);
298 rc = copy_install_file(file);
299 if (rc != ERROR_SUCCESS)
301 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(file->SourcePath),
302 debugstr_w(file->TargetPath), rc);
303 rc = ERROR_INSTALL_FAILURE;
307 else if (file->state != msifs_installed)
309 ERR("compressed file wasn't extracted (%s)\n", debugstr_w(file->TargetPath));
310 rc = ERROR_INSTALL_FAILURE;
315 msi_free_media_info(mi);
319 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
321 MSIPACKAGE *package = (MSIPACKAGE*)param;
322 WCHAR dest_name[0x100];
323 LPWSTR dest_path, dest;
324 LPCWSTR file_key, component;
330 component = MSI_RecordGetString(row,2);
331 comp = get_loaded_component(package,component);
333 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
335 TRACE("Skipping copy due to disabled component %s\n",
336 debugstr_w(component));
338 /* the action taken was the same as the current install state */
339 comp->Action = comp->Installed;
341 return ERROR_SUCCESS;
344 comp->Action = INSTALLSTATE_LOCAL;
346 file_key = MSI_RecordGetString(row,3);
349 ERR("Unable to get file key\n");
350 return ERROR_FUNCTION_FAILED;
353 rc = get_file_target(package,file_key,&file);
355 if (rc != ERROR_SUCCESS)
357 ERR("Original file unknown %s\n",debugstr_w(file_key));
358 return ERROR_SUCCESS;
361 if (MSI_RecordIsNull(row,4))
362 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
366 MSI_RecordGetStringW(row,4,dest_name,&sz);
367 reduce_to_longfilename(dest_name);
370 if (MSI_RecordIsNull(row,5))
373 dest_path = strdupW(file->TargetPath);
374 p = strrchrW(dest_path,'\\');
381 destkey = MSI_RecordGetString(row,5);
382 dest_path = resolve_folder(package, destkey, FALSE, FALSE, TRUE, NULL);
386 dest_path = msi_dup_property( package, destkey );
389 FIXME("Unable to get destination folder, try AppSearch properties\n");
390 return ERROR_SUCCESS;
395 dest = build_directory_name(2, dest_path, dest_name);
396 create_full_pathW(dest_path);
398 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
401 if (strcmpW(file->TargetPath,dest))
402 rc = !CopyFileW(file->TargetPath,dest,TRUE);
406 if (rc != ERROR_SUCCESS)
407 ERR("Failed to copy file %s -> %s, last error %d\n",
408 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
410 FIXME("We should track these duplicate files as well\n");
415 msi_file_update_ui(package, file, szDuplicateFiles);
417 return ERROR_SUCCESS;
420 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
424 static const WCHAR ExecSeqQuery[] =
425 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
426 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
428 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
429 if (rc != ERROR_SUCCESS)
430 return ERROR_SUCCESS;
432 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
433 msiobj_release(&view->hdr);
438 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
442 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
447 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
450 if ( file->state == msifs_installed )
451 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
453 if ( file->state != msifs_present )
456 /* don't remove a file if the old file
457 * is strictly newer than the version to be installed
459 if ( msi_compare_file_version( file ) < 0 )
462 TRACE("removing %s\n", debugstr_w(file->File) );
463 if ( !DeleteFileW( file->TargetPath ) )
464 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
465 file->state = msifs_missing;
468 uirow = MSI_CreateRecord( 9 );
469 MSI_RecordSetStringW( uirow, 1, file->FileName );
470 uipath = strdupW( file->TargetPath );
471 p = strrchrW(uipath,'\\');
474 MSI_RecordSetStringW( uirow, 9, uipath);
475 ui_actiondata( package, szRemoveFiles, uirow);
476 msiobj_release( &uirow->hdr );
478 /* FIXME: call ui_progress here? */
481 return ERROR_SUCCESS;