winmm: Fix a failing mixer test on 98 and ME.
[wine] / dlls / msi / files.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2005 Aric Stewart for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21
22 /*
23  * Actions dealing with files These are
24  *
25  * InstallFiles
26  * DuplicateFiles
27  * MoveFiles (TODO)
28  * PatchFiles (TODO)
29  * RemoveDuplicateFiles(TODO)
30  * RemoveFiles(TODO)
31  */
32
33 #include <stdarg.h>
34
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"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(msi);
50
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[];
57
58 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
59 {
60     MSIRECORD *uirow;
61     LPWSTR uipath, p;
62
63     /* the UI chunk */
64     uirow = MSI_CreateRecord( 9 );
65     MSI_RecordSetStringW( uirow, 1, f->FileName );
66     uipath = strdupW( f->TargetPath );
67     p = strrchrW(uipath,'\\');
68     if (p)
69         p[1]=0;
70     MSI_RecordSetStringW( uirow, 9, uipath);
71     MSI_RecordSetInteger( uirow, 6, f->FileSize );
72     ui_actiondata( package, action, uirow);
73     msiobj_release( &uirow->hdr );
74     msi_free( uipath );
75     ui_progress( package, 2, f->FileSize, 0, 0);
76 }
77
78 /* compares the version of a file read from the filesystem and
79  * the version specified in the File table
80  */
81 static int msi_compare_file_version(MSIFILE *file)
82 {
83     WCHAR version[MAX_PATH];
84     DWORD size;
85     UINT r;
86
87     size = MAX_PATH;
88     version[0] = '\0';
89     r = MsiGetFileVersionW(file->TargetPath, version, &size, NULL, NULL);
90     if (r != ERROR_SUCCESS)
91         return 0;
92
93     return lstrcmpW(version, file->Version);
94 }
95
96 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key, 
97                             MSIFILE** file)
98 {
99     LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
100     {
101         if (lstrcmpW( file_key, (*file)->File )==0)
102         {
103             if ((*file)->state >= msifs_overwrite)
104                 return ERROR_SUCCESS;
105             else
106                 return ERROR_FILE_NOT_FOUND;
107         }
108     }
109
110     return ERROR_FUNCTION_FAILED;
111 }
112
113 static void schedule_install_files(MSIPACKAGE *package)
114 {
115     MSIFILE *file;
116
117     LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
118     {
119         if (!ACTION_VerifyComponentForAction(file->Component, INSTALLSTATE_LOCAL))
120         {
121             TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
122
123             ui_progress(package,2,file->FileSize,0,0);
124             file->state = msifs_skipped;
125         }
126     }
127 }
128
129 static UINT copy_file(MSIFILE *file, LPWSTR source)
130 {
131     BOOL ret;
132
133     ret = CopyFileW(source, file->TargetPath, FALSE);
134     if (!ret)
135         return GetLastError();
136
137     SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
138
139     file->state = msifs_installed;
140     return ERROR_SUCCESS;
141 }
142
143 static UINT copy_install_file(MSIFILE *file, LPWSTR source)
144 {
145     UINT gle;
146
147     TRACE("Copying %s to %s\n", debugstr_w(source),
148           debugstr_w(file->TargetPath));
149
150     gle = copy_file(file, source);
151     if (gle == ERROR_SUCCESS)
152         return gle;
153
154     if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
155     {
156         TRACE("overwriting existing file\n");
157         gle = ERROR_SUCCESS;
158     }
159     else if (gle == ERROR_ACCESS_DENIED)
160     {
161         SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
162
163         gle = copy_file(file, source);
164         TRACE("Overwriting existing file: %d\n", gle);
165     }
166
167     return gle;
168 }
169
170 static BOOL check_dest_hash_matches(MSIFILE *file)
171 {
172     MSIFILEHASHINFO hash;
173     UINT r;
174
175     if (!file->hash.dwFileHashInfoSize)
176         return FALSE;
177
178     hash.dwFileHashInfoSize = sizeof(MSIFILEHASHINFO);
179     r = MsiGetFileHashW(file->TargetPath, 0, &hash);
180     if (r != ERROR_SUCCESS)
181         return FALSE;
182
183     return !memcmp(&hash, &file->hash, sizeof(MSIFILEHASHINFO));
184 }
185
186 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
187                             LPWSTR *path, DWORD *attrs, PVOID user)
188 {
189     static MSIFILE *f = NULL;
190
191     if (action == MSICABEXTRACT_BEGINEXTRACT)
192     {
193         f = get_loaded_file(package, file);
194         if (!f)
195         {
196             WARN("unknown file in cabinet (%s)\n", debugstr_w(file));
197             return FALSE;
198         }
199
200         if (f->state != msifs_missing && f->state != msifs_overwrite)
201         {
202             TRACE("Skipping extraction of %s\n", debugstr_w(file));
203             return FALSE;
204         }
205
206         msi_file_update_ui(package, f, szInstallFiles);
207
208         *path = strdupW(f->TargetPath);
209         *attrs = f->Attributes;
210     }
211     else if (action == MSICABEXTRACT_FILEEXTRACTED)
212     {
213         f->state = msifs_installed;
214         f = NULL;
215     }
216
217     return TRUE;
218 }
219
220 /*
221  * ACTION_InstallFiles()
222  * 
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.
226  */
227 UINT ACTION_InstallFiles(MSIPACKAGE *package)
228 {
229     MSIMEDIAINFO *mi;
230     UINT rc = ERROR_SUCCESS;
231     MSIFILE *file;
232
233     /* increment progress bar each time action data is sent */
234     ui_progress(package,1,1,0,0);
235
236     schedule_install_files(package);
237
238     /*
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.
243      */
244     msi_create_component_directories( package );
245
246     mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
247
248     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
249     {
250         if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
251             continue;
252
253         if (check_dest_hash_matches(file))
254         {
255             TRACE("File hashes match, not overwriting\n");
256             continue;
257         }
258
259         if (MsiGetFileVersionW(file->TargetPath, NULL, NULL, NULL, NULL) == ERROR_SUCCESS &&
260             msi_compare_file_version(file) >= 0)
261         {
262             TRACE("Destination file version greater, not overwriting\n");
263             continue;
264         }
265
266         if (file->Sequence > mi->last_sequence || mi->is_continuous ||
267             (file->IsCompressed && !mi->is_extracted))
268         {
269             MSICABDATA data;
270
271             rc = ready_media(package, file, mi);
272             if (rc != ERROR_SUCCESS)
273             {
274                 ERR("Failed to ready media\n");
275                 break;
276             }
277
278             data.mi = mi;
279             data.package = package;
280             data.cb = installfiles_cb;
281             data.user = NULL;
282
283             if (file->IsCompressed &&
284                 !msi_cabextract(package, mi, &data))
285             {
286                 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
287                 rc = ERROR_FUNCTION_FAILED;
288                 break;
289             }
290         }
291
292         if (!file->IsCompressed)
293         {
294             LPWSTR source = resolve_file_source(package, file);
295
296             TRACE("file paths %s to %s\n", debugstr_w(source),
297                   debugstr_w(file->TargetPath));
298
299             msi_file_update_ui(package, file, szInstallFiles);
300             rc = copy_install_file(file, source);
301             if (rc != ERROR_SUCCESS)
302             {
303                 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
304                     debugstr_w(file->TargetPath), rc);
305                 rc = ERROR_INSTALL_FAILURE;
306                 msi_free(source);
307                 break;
308             }
309
310             msi_free(source);
311         }
312         else if (file->state != msifs_installed)
313         {
314             ERR("compressed file wasn't extracted (%s)\n",
315                 debugstr_w(file->TargetPath));
316             rc = ERROR_INSTALL_FAILURE;
317             break;
318         }
319     }
320
321     msi_free_media_info(mi);
322     return rc;
323 }
324
325 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
326 {
327     MSIPACKAGE *package = (MSIPACKAGE*)param;
328     WCHAR dest_name[0x100];
329     LPWSTR dest_path, dest;
330     LPCWSTR file_key, component;
331     DWORD sz;
332     DWORD rc;
333     MSICOMPONENT *comp;
334     MSIFILE *file;
335
336     component = MSI_RecordGetString(row,2);
337     comp = get_loaded_component(package,component);
338
339     if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
340     {
341         TRACE("Skipping copy due to disabled component %s\n",
342                         debugstr_w(component));
343
344         /* the action taken was the same as the current install state */        
345         comp->Action = comp->Installed;
346
347         return ERROR_SUCCESS;
348     }
349
350     comp->Action = INSTALLSTATE_LOCAL;
351
352     file_key = MSI_RecordGetString(row,3);
353     if (!file_key)
354     {
355         ERR("Unable to get file key\n");
356         return ERROR_FUNCTION_FAILED;
357     }
358
359     rc = get_file_target(package,file_key,&file);
360
361     if (rc != ERROR_SUCCESS)
362     {
363         ERR("Original file unknown %s\n",debugstr_w(file_key));
364         return ERROR_SUCCESS;
365     }
366
367     if (MSI_RecordIsNull(row,4))
368         strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
369     else
370     {
371         sz=0x100;
372         MSI_RecordGetStringW(row,4,dest_name,&sz);
373         reduce_to_longfilename(dest_name);
374     }
375
376     if (MSI_RecordIsNull(row,5))
377     {
378         LPWSTR p;
379         dest_path = strdupW(file->TargetPath);
380         p = strrchrW(dest_path,'\\');
381         if (p)
382             *p=0;
383     }
384     else
385     {
386         LPCWSTR destkey;
387         destkey = MSI_RecordGetString(row,5);
388         dest_path = resolve_folder(package, destkey, FALSE, FALSE, TRUE, NULL);
389         if (!dest_path)
390         {
391             /* try a Property */
392             dest_path = msi_dup_property( package, destkey );
393             if (!dest_path)
394             {
395                 FIXME("Unable to get destination folder, try AppSearch properties\n");
396                 return ERROR_SUCCESS;
397             }
398         }
399     }
400
401     dest = build_directory_name(2, dest_path, dest_name);
402     create_full_pathW(dest_path);
403
404     TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
405                     debugstr_w(dest)); 
406
407     if (strcmpW(file->TargetPath,dest))
408         rc = !CopyFileW(file->TargetPath,dest,TRUE);
409     else
410         rc = ERROR_SUCCESS;
411
412     if (rc != ERROR_SUCCESS)
413         ERR("Failed to copy file %s -> %s, last error %d\n",
414             debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
415
416     FIXME("We should track these duplicate files as well\n");   
417
418     msi_free(dest_path);
419     msi_free(dest);
420
421     msi_file_update_ui(package, file, szDuplicateFiles);
422
423     return ERROR_SUCCESS;
424 }
425
426 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
427 {
428     UINT rc;
429     MSIQUERY * view;
430     static const WCHAR ExecSeqQuery[] =
431         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
432          '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
433
434     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
435     if (rc != ERROR_SUCCESS)
436         return ERROR_SUCCESS;
437
438     rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
439     msiobj_release(&view->hdr);
440
441     return rc;
442 }
443
444 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
445 {
446     INSTALLSTATE request = comp->ActionRequest;
447
448     if (request == INSTALLSTATE_UNKNOWN)
449         return FALSE;
450
451     if (install_mode == msidbRemoveFileInstallModeOnInstall &&
452         (request == INSTALLSTATE_LOCAL || request == INSTALLSTATE_SOURCE))
453         return TRUE;
454
455     if (request == INSTALLSTATE_ABSENT)
456     {
457         if (!comp->ComponentId)
458             return FALSE;
459
460         if (install_mode == msidbRemoveFileInstallModeOnRemove)
461             return TRUE;
462     }
463
464     if (install_mode == msidbRemoveFileInstallModeOnBoth)
465         return TRUE;
466
467     return FALSE;
468 }
469
470 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
471 {
472     MSIPACKAGE *package = (MSIPACKAGE*)param;
473     MSICOMPONENT *comp;
474     LPCWSTR component, filename, dirprop;
475     UINT install_mode;
476     LPWSTR dir = NULL, path = NULL;
477     DWORD size;
478     UINT r;
479
480     component = MSI_RecordGetString(row, 2);
481     filename = MSI_RecordGetString(row, 3);
482     dirprop = MSI_RecordGetString(row, 4);
483     install_mode = MSI_RecordGetInteger(row, 5);
484
485     comp = get_loaded_component(package, component);
486     if (!comp)
487     {
488         ERR("Invalid component: %s\n", debugstr_w(component));
489         return ERROR_FUNCTION_FAILED;
490     }
491
492     if (!verify_comp_for_removal(comp, install_mode))
493     {
494         TRACE("Skipping removal due to missing conditions\n");
495         comp->Action = comp->Installed;
496         return ERROR_SUCCESS;
497     }
498
499     dir = msi_dup_property(package, dirprop);
500     if (!dir)
501         return ERROR_OUTOFMEMORY;
502
503     size = (filename != NULL) ? lstrlenW(filename) : 0;
504     size += lstrlenW(dir) + 2;
505     path = msi_alloc(size * sizeof(WCHAR));
506     if (!path)
507     {
508         r = ERROR_OUTOFMEMORY;
509         goto done;
510     }
511
512     lstrcpyW(path, dir);
513     PathAddBackslashW(path);
514
515     if (filename)
516     {
517         lstrcatW(path, filename);
518
519         TRACE("Deleting misc file: %s\n", debugstr_w(path));
520         DeleteFileW(path);
521     }
522     else
523     {
524         TRACE("Removing misc directory: %s\n", debugstr_w(path));
525         RemoveDirectoryW(path);
526     }
527
528 done:
529     msi_free(path);
530     msi_free(dir);
531     return ERROR_SUCCESS;
532 }
533
534 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
535 {
536     MSIQUERY *view;
537     MSIFILE *file;
538     UINT r;
539
540     static const WCHAR query[] = {
541         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
542         '`','R','e','m','o','v','e','F','i','l','e','`',0};
543
544     r = MSI_DatabaseOpenViewW(package->db, query, &view);
545     if (r == ERROR_SUCCESS)
546     {
547         MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
548         msiobj_release(&view->hdr);
549     }
550
551     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
552     {
553         MSIRECORD *uirow;
554         LPWSTR uipath, p;
555
556         if ( file->state == msifs_installed )
557             ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
558
559         if ( file->Component->ActionRequest != INSTALLSTATE_ABSENT ||
560              file->Component->Installed == INSTALLSTATE_SOURCE )
561             continue;
562
563         /* don't remove a file if the old file
564          * is strictly newer than the version to be installed
565          */
566         if ( msi_compare_file_version( file ) < 0 )
567             continue;
568
569         TRACE("removing %s\n", debugstr_w(file->File) );
570         if ( !DeleteFileW( file->TargetPath ) )
571             TRACE("failed to delete %s\n",  debugstr_w(file->TargetPath));
572         file->state = msifs_missing;
573
574         /* the UI chunk */
575         uirow = MSI_CreateRecord( 9 );
576         MSI_RecordSetStringW( uirow, 1, file->FileName );
577         uipath = strdupW( file->TargetPath );
578         p = strrchrW(uipath,'\\');
579         if (p)
580             p[1]=0;
581         MSI_RecordSetStringW( uirow, 9, uipath);
582         ui_actiondata( package, szRemoveFiles, uirow);
583         msiobj_release( &uirow->hdr );
584         msi_free( uipath );
585         /* FIXME: call ui_progress here? */
586     }
587
588     return ERROR_SUCCESS;
589 }