rsaenh: Simplify store_key_container_permissions.
[wine] / dlls / msi / install.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 /* Msi top level apis directly related to installs */
22
23 #define COBJMACROS
24
25 #include <stdarg.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "wine/debug.h"
31 #include "msi.h"
32 #include "msidefs.h"
33 #include "objbase.h"
34 #include "oleauto.h"
35
36 #include "msipriv.h"
37 #include "msiserver.h"
38 #include "wine/unicode.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(msi);
41
42 /***********************************************************************
43  * MsiDoActionA       (MSI.@)
44  */
45 UINT WINAPI MsiDoActionA( MSIHANDLE hInstall, LPCSTR szAction )
46 {
47     LPWSTR szwAction;
48     UINT ret;
49
50     TRACE("%s\n", debugstr_a(szAction));
51
52     szwAction = strdupAtoW(szAction);
53     if (szAction && !szwAction)
54         return ERROR_FUNCTION_FAILED; 
55
56     ret = MsiDoActionW( hInstall, szwAction );
57     msi_free( szwAction );
58     return ret;
59 }
60
61 /***********************************************************************
62  * MsiDoActionW       (MSI.@)
63  */
64 UINT WINAPI MsiDoActionW( MSIHANDLE hInstall, LPCWSTR szAction )
65 {
66     MSIPACKAGE *package;
67     UINT ret;
68
69     TRACE("%s\n",debugstr_w(szAction));
70
71     if (!szAction)
72         return ERROR_INVALID_PARAMETER;
73
74     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
75     if (!package)
76     {
77         HRESULT hr;
78         BSTR action;
79         IWineMsiRemotePackage *remote_package;
80
81         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
82         if (!remote_package)
83             return ERROR_INVALID_HANDLE;
84
85         action = SysAllocString( szAction );
86         if (!action)
87         {
88             IWineMsiRemotePackage_Release( remote_package );
89             return ERROR_OUTOFMEMORY;
90         }
91
92         hr = IWineMsiRemotePackage_DoAction( remote_package, action );
93
94         SysFreeString( action );
95         IWineMsiRemotePackage_Release( remote_package );
96
97         if (FAILED(hr))
98         {
99             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
100                 return HRESULT_CODE(hr);
101
102             return ERROR_FUNCTION_FAILED;
103         }
104
105         return ERROR_SUCCESS;
106     }
107  
108     ret = ACTION_PerformUIAction( package, szAction, -1 );
109     msiobj_release( &package->hdr );
110
111     return ret;
112 }
113
114 /***********************************************************************
115  * MsiSequenceA       (MSI.@)
116  */
117 UINT WINAPI MsiSequenceA( MSIHANDLE hInstall, LPCSTR szTable, INT iSequenceMode )
118 {
119     LPWSTR szwTable;
120     UINT ret;
121
122     TRACE("%s\n", debugstr_a(szTable));
123
124     szwTable = strdupAtoW(szTable);
125     if (szTable && !szwTable)
126         return ERROR_FUNCTION_FAILED; 
127
128     ret = MsiSequenceW( hInstall, szwTable, iSequenceMode );
129     msi_free( szwTable );
130     return ret;
131 }
132
133 /***********************************************************************
134  * MsiSequenceW       (MSI.@)
135  */
136 UINT WINAPI MsiSequenceW( MSIHANDLE hInstall, LPCWSTR szTable, INT iSequenceMode )
137 {
138     MSIPACKAGE *package;
139     UINT ret;
140
141     TRACE("%s\n", debugstr_w(szTable));
142
143     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
144     if (!package)
145     {
146         HRESULT hr;
147         BSTR table;
148         IWineMsiRemotePackage *remote_package;
149
150         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
151         if (!remote_package)
152             return ERROR_INVALID_HANDLE;
153
154         table = SysAllocString( szTable );
155         if (!table)
156         {
157             IWineMsiRemotePackage_Release( remote_package );
158             return ERROR_OUTOFMEMORY;
159         }
160
161         hr = IWineMsiRemotePackage_Sequence( remote_package, table, iSequenceMode );
162
163         SysFreeString( table );
164         IWineMsiRemotePackage_Release( remote_package );
165
166         if (FAILED(hr))
167         {
168             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
169                 return HRESULT_CODE(hr);
170
171             return ERROR_FUNCTION_FAILED;
172         }
173
174         return ERROR_SUCCESS;
175     }
176
177     ret = MSI_Sequence( package, szTable, iSequenceMode );
178     msiobj_release( &package->hdr );
179  
180     return ret;
181 }
182
183 UINT msi_strcpy_to_awstring( LPCWSTR str, awstring *awbuf, DWORD *sz )
184 {
185     UINT len, r = ERROR_SUCCESS;
186
187     if (awbuf->str.w && !sz )
188         return ERROR_INVALID_PARAMETER;
189
190     if (!sz)
191         return r;
192  
193     if (awbuf->unicode)
194     {
195         len = lstrlenW( str );
196         if (awbuf->str.w) 
197             lstrcpynW( awbuf->str.w, str, *sz );
198     }
199     else
200     {
201         len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
202         if (len)
203             len--;
204         WideCharToMultiByte( CP_ACP, 0, str, -1, awbuf->str.a, *sz, NULL, NULL );
205         if ( awbuf->str.a && *sz && (len >= *sz) )
206             awbuf->str.a[*sz - 1] = 0;
207     }
208
209     if (awbuf->str.w && len >= *sz)
210         r = ERROR_MORE_DATA;
211     *sz = len;
212     return r;
213 }
214
215 /***********************************************************************
216  * MsiGetTargetPath   (internal)
217  */
218 static UINT MSI_GetTargetPath( MSIHANDLE hInstall, LPCWSTR szFolder,
219                                awstring *szPathBuf, LPDWORD pcchPathBuf )
220 {
221     MSIPACKAGE *package;
222     LPWSTR path;
223     UINT r = ERROR_FUNCTION_FAILED;
224
225     if (!szFolder)
226         return ERROR_INVALID_PARAMETER;
227
228     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
229     if (!package)
230     {
231         HRESULT hr;
232         IWineMsiRemotePackage *remote_package;
233         LPWSTR value = NULL;
234         BSTR folder;
235         DWORD len;
236
237         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
238         if (!remote_package)
239             return ERROR_INVALID_HANDLE;
240
241         folder = SysAllocString( szFolder );
242         if (!folder)
243         {
244             IWineMsiRemotePackage_Release( remote_package );
245             return ERROR_OUTOFMEMORY;
246         }
247
248         len = 0;
249         hr = IWineMsiRemotePackage_GetTargetPath( remote_package, folder,
250                                                   NULL, &len );
251         if (FAILED(hr))
252             goto done;
253
254         len++;
255         value = msi_alloc(len * sizeof(WCHAR));
256         if (!value)
257         {
258             r = ERROR_OUTOFMEMORY;
259             goto done;
260         }
261
262         hr = IWineMsiRemotePackage_GetTargetPath( remote_package, folder,
263                                                   (BSTR *)value, &len);
264         if (FAILED(hr))
265             goto done;
266
267         r = msi_strcpy_to_awstring( value, szPathBuf, pcchPathBuf );
268
269 done:
270         IWineMsiRemotePackage_Release( remote_package );
271         SysFreeString( folder );
272         msi_free( value );
273
274         if (FAILED(hr))
275         {
276             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
277                 return HRESULT_CODE(hr);
278
279             return ERROR_FUNCTION_FAILED;
280         }
281
282         return r;
283     }
284
285     path = resolve_folder( package, szFolder, FALSE, FALSE, TRUE, NULL );
286     msiobj_release( &package->hdr );
287
288     if (!path)
289         return ERROR_DIRECTORY;
290
291     r = msi_strcpy_to_awstring( path, szPathBuf, pcchPathBuf );
292     msi_free( path );
293     return r;
294 }
295
296 /***********************************************************************
297  * MsiGetTargetPathA        (MSI.@)
298  */
299 UINT WINAPI MsiGetTargetPathA( MSIHANDLE hInstall, LPCSTR szFolder, 
300                                LPSTR szPathBuf, LPDWORD pcchPathBuf )
301 {
302     LPWSTR szwFolder;
303     awstring path;
304     UINT r;
305
306     TRACE("%s %p %p\n", debugstr_a(szFolder), szPathBuf, pcchPathBuf);
307
308     szwFolder = strdupAtoW(szFolder);
309     if (szFolder && !szwFolder)
310         return ERROR_FUNCTION_FAILED; 
311
312     path.unicode = FALSE;
313     path.str.a = szPathBuf;
314
315     r = MSI_GetTargetPath( hInstall, szwFolder, &path, pcchPathBuf );
316
317     msi_free( szwFolder );
318
319     return r;
320 }
321
322 /***********************************************************************
323  * MsiGetTargetPathW        (MSI.@)
324  */
325 UINT WINAPI MsiGetTargetPathW( MSIHANDLE hInstall, LPCWSTR szFolder,
326                                LPWSTR szPathBuf, LPDWORD pcchPathBuf )
327 {
328     awstring path;
329
330     TRACE("%s %p %p\n", debugstr_w(szFolder), szPathBuf, pcchPathBuf);
331
332     path.unicode = TRUE;
333     path.str.w = szPathBuf;
334
335     return MSI_GetTargetPath( hInstall, szFolder, &path, pcchPathBuf );
336 }
337
338 /***********************************************************************
339  * MsiGetSourcePath   (internal)
340  */
341 static UINT MSI_GetSourcePath( MSIHANDLE hInstall, LPCWSTR szFolder,
342                                awstring *szPathBuf, LPDWORD pcchPathBuf )
343 {
344     MSIPACKAGE *package;
345     LPWSTR path;
346     UINT r = ERROR_FUNCTION_FAILED;
347
348     TRACE("%s %p %p\n", debugstr_w(szFolder), szPathBuf, pcchPathBuf );
349
350     if (!szFolder)
351         return ERROR_INVALID_PARAMETER;
352
353     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
354     if (!package)
355     {
356         HRESULT hr;
357         IWineMsiRemotePackage *remote_package;
358         LPWSTR value = NULL;
359         BSTR folder;
360         DWORD len;
361
362         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
363         if (!remote_package)
364             return ERROR_INVALID_HANDLE;
365
366         folder = SysAllocString( szFolder );
367         if (!folder)
368         {
369             IWineMsiRemotePackage_Release( remote_package );
370             return ERROR_OUTOFMEMORY;
371         }
372
373         len = 0;
374         hr = IWineMsiRemotePackage_GetSourcePath( remote_package, folder,
375                                                   NULL, &len );
376         if (FAILED(hr))
377             goto done;
378
379         len++;
380         value = msi_alloc(len * sizeof(WCHAR));
381         if (!value)
382         {
383             r = ERROR_OUTOFMEMORY;
384             goto done;
385         }
386
387         hr = IWineMsiRemotePackage_GetSourcePath( remote_package, folder,
388                                                   (BSTR *)value, &len);
389         if (FAILED(hr))
390             goto done;
391
392         r = msi_strcpy_to_awstring( value, szPathBuf, pcchPathBuf );
393
394 done:
395         IWineMsiRemotePackage_Release( remote_package );
396         SysFreeString( folder );
397         msi_free( value );
398
399         if (FAILED(hr))
400         {
401             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
402                 return HRESULT_CODE(hr);
403
404             return ERROR_FUNCTION_FAILED;
405         }
406
407         return r;
408     }
409
410     if (szPathBuf->str.w && !pcchPathBuf )
411     {
412         msiobj_release( &package->hdr );
413         return ERROR_INVALID_PARAMETER;
414     }
415
416     path = resolve_folder(package, szFolder, TRUE, FALSE, TRUE, NULL);
417     msiobj_release( &package->hdr );
418
419     TRACE("path = %s\n",debugstr_w(path));
420     if (!path)
421         return ERROR_DIRECTORY;
422
423     r = msi_strcpy_to_awstring( path, szPathBuf, pcchPathBuf );
424     msi_free( path );
425     return r;
426 }
427
428 /***********************************************************************
429  * MsiGetSourcePathA     (MSI.@)
430  */
431 UINT WINAPI MsiGetSourcePathA( MSIHANDLE hInstall, LPCSTR szFolder, 
432                                LPSTR szPathBuf, LPDWORD pcchPathBuf )
433 {
434     LPWSTR folder;
435     awstring str;
436     UINT r;
437
438     TRACE("%s %p %p\n", szFolder, debugstr_a(szPathBuf), pcchPathBuf);
439
440     str.unicode = FALSE;
441     str.str.a = szPathBuf;
442
443     folder = strdupAtoW( szFolder );
444     r = MSI_GetSourcePath( hInstall, folder, &str, pcchPathBuf );
445     msi_free( folder );
446
447     return r;
448 }
449
450 /***********************************************************************
451  * MsiGetSourcePathW     (MSI.@)
452  */
453 UINT WINAPI MsiGetSourcePathW( MSIHANDLE hInstall, LPCWSTR szFolder,
454                                LPWSTR szPathBuf, LPDWORD pcchPathBuf )
455 {
456     awstring str;
457
458     TRACE("%s %p %p\n", debugstr_w(szFolder), szPathBuf, pcchPathBuf );
459
460     str.unicode = TRUE;
461     str.str.w = szPathBuf;
462
463     return MSI_GetSourcePath( hInstall, szFolder, &str, pcchPathBuf );
464 }
465
466 /***********************************************************************
467  * MsiSetTargetPathA  (MSI.@)
468  */
469 UINT WINAPI MsiSetTargetPathA( MSIHANDLE hInstall, LPCSTR szFolder,
470                                LPCSTR szFolderPath )
471 {
472     LPWSTR szwFolder = NULL, szwFolderPath = NULL;
473     UINT rc = ERROR_OUTOFMEMORY;
474
475     if ( !szFolder || !szFolderPath )
476         return ERROR_INVALID_PARAMETER;
477
478     szwFolder = strdupAtoW(szFolder);
479     szwFolderPath = strdupAtoW(szFolderPath);
480     if (!szwFolder || !szwFolderPath)
481         goto end;
482
483     rc = MsiSetTargetPathW( hInstall, szwFolder, szwFolderPath );
484
485 end:
486     msi_free(szwFolder);
487     msi_free(szwFolderPath);
488
489     return rc;
490 }
491
492 /*
493  * Ok my original interpretation of this was wrong. And it looks like msdn has
494  * changed a bit also. The given folder path does not have to actually already
495  * exist, it just cannot be read only and must be a legal folder path.
496  */
497 UINT MSI_SetTargetPathW(MSIPACKAGE *package, LPCWSTR szFolder, 
498                              LPCWSTR szFolderPath)
499 {
500     DWORD attrib;
501     LPWSTR path = NULL;
502     LPWSTR path2 = NULL;
503     MSIFOLDER *folder;
504     MSIFILE *file;
505
506     TRACE("%p %s %s\n",package, debugstr_w(szFolder),debugstr_w(szFolderPath));
507
508     attrib = GetFileAttributesW(szFolderPath);
509     /* native MSI tests writeability by making temporary files at each drive */
510     if ( attrib != INVALID_FILE_ATTRIBUTES &&
511           (attrib & FILE_ATTRIBUTE_OFFLINE ||
512            attrib & FILE_ATTRIBUTE_READONLY))
513         return ERROR_FUNCTION_FAILED;
514
515     path = resolve_folder(package,szFolder,FALSE,FALSE,FALSE,&folder);
516     if (!path)
517         return ERROR_DIRECTORY;
518
519     msi_free(folder->Property);
520     folder->Property = build_directory_name(2, szFolderPath, NULL);
521
522     if (lstrcmpiW(path, folder->Property) == 0)
523     {
524         /*
525          *  Resolved Target has not really changed, so just 
526          *  set this folder and do not recalculate everything.
527          */
528         msi_free(folder->ResolvedTarget);
529         folder->ResolvedTarget = NULL;
530         path2 = resolve_folder(package,szFolder,FALSE,TRUE,FALSE,NULL);
531         msi_free(path2);
532     }
533     else
534     {
535         MSIFOLDER *f;
536
537         LIST_FOR_EACH_ENTRY( f, &package->folders, MSIFOLDER, entry )
538         {
539             msi_free(f->ResolvedTarget);
540             f->ResolvedTarget=NULL;
541         }
542
543         LIST_FOR_EACH_ENTRY( f, &package->folders, MSIFOLDER, entry )
544         {
545             path2 = resolve_folder(package, f->Directory, FALSE, TRUE, FALSE, NULL);
546             msi_free(path2);
547         }
548
549         LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
550         {
551             MSICOMPONENT *comp = file->Component;
552             LPWSTR p;
553
554             if (!comp)
555                 continue;
556
557             p = resolve_folder(package, comp->Directory, FALSE, FALSE, FALSE, NULL);
558             msi_free(file->TargetPath);
559
560             file->TargetPath = build_directory_name(2, p, file->FileName);
561             msi_free(p);
562         }
563     }
564     msi_free(path);
565
566     return ERROR_SUCCESS;
567 }
568
569 /***********************************************************************
570  * MsiSetTargetPathW  (MSI.@)
571  */
572 UINT WINAPI MsiSetTargetPathW(MSIHANDLE hInstall, LPCWSTR szFolder, 
573                              LPCWSTR szFolderPath)
574 {
575     MSIPACKAGE *package;
576     UINT ret;
577
578     TRACE("%s %s\n",debugstr_w(szFolder),debugstr_w(szFolderPath));
579
580     if ( !szFolder || !szFolderPath )
581         return ERROR_INVALID_PARAMETER;
582
583     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
584     if (!package)
585     {
586         HRESULT hr;
587         BSTR folder, path;
588         IWineMsiRemotePackage *remote_package;
589
590         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
591         if (!remote_package)
592             return ERROR_INVALID_HANDLE;
593
594         folder = SysAllocString( szFolder );
595         path = SysAllocString( szFolderPath );
596         if (!folder || !path)
597         {
598             SysFreeString(folder);
599             SysFreeString(path);
600             IWineMsiRemotePackage_Release( remote_package );
601             return ERROR_OUTOFMEMORY;
602         }
603
604         hr = IWineMsiRemotePackage_SetTargetPath( remote_package, folder, path );
605
606         SysFreeString(folder);
607         SysFreeString(path);
608         IWineMsiRemotePackage_Release( remote_package );
609
610         if (FAILED(hr))
611         {
612             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
613                 return HRESULT_CODE(hr);
614
615             return ERROR_FUNCTION_FAILED;
616         }
617
618         return ERROR_SUCCESS;
619     }
620
621     ret = MSI_SetTargetPathW( package, szFolder, szFolderPath );
622     msiobj_release( &package->hdr );
623     return ret;
624 }
625
626 /***********************************************************************
627  *           MsiGetMode    (MSI.@)
628  *
629  * Returns an internal installer state (if it is running in a mode iRunMode)
630  *
631  * PARAMS
632  *   hInstall    [I]  Handle to the installation
633  *   hRunMode    [I]  Checking run mode
634  *        MSIRUNMODE_ADMIN             Administrative mode
635  *        MSIRUNMODE_ADVERTISE         Advertisement mode
636  *        MSIRUNMODE_MAINTENANCE       Maintenance mode
637  *        MSIRUNMODE_ROLLBACKENABLED   Rollback is enabled
638  *        MSIRUNMODE_LOGENABLED        Log file is writing
639  *        MSIRUNMODE_OPERATIONS        Operations in progress??
640  *        MSIRUNMODE_REBOOTATEND       We need to reboot after installation completed
641  *        MSIRUNMODE_REBOOTNOW         We need to reboot to continue the installation
642  *        MSIRUNMODE_CABINET           Files from cabinet are installed
643  *        MSIRUNMODE_SOURCESHORTNAMES  Long names in source files is suppressed
644  *        MSIRUNMODE_TARGETSHORTNAMES  Long names in destination files is suppressed
645  *        MSIRUNMODE_RESERVED11        Reserved
646  *        MSIRUNMODE_WINDOWS9X         Running under Windows95/98
647  *        MSIRUNMODE_ZAWENABLED        Demand installation is supported
648  *        MSIRUNMODE_RESERVED14        Reserved
649  *        MSIRUNMODE_RESERVED15        Reserved
650  *        MSIRUNMODE_SCHEDULED         called from install script
651  *        MSIRUNMODE_ROLLBACK          called from rollback script
652  *        MSIRUNMODE_COMMIT            called from commit script
653  *
654  * RETURNS
655  *    In the state: TRUE
656  *    Not in the state: FALSE
657  *
658  */
659 BOOL WINAPI MsiGetMode(MSIHANDLE hInstall, MSIRUNMODE iRunMode)
660 {
661     MSIPACKAGE *package;
662     BOOL r = FALSE;
663
664     TRACE("%d %d\n", hInstall, iRunMode);
665
666     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
667     if (!package)
668     {
669         BOOL ret;
670         HRESULT hr;
671         IWineMsiRemotePackage *remote_package;
672
673         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
674         if (!remote_package)
675             return FALSE;
676
677         hr = IWineMsiRemotePackage_GetMode(remote_package, iRunMode, &ret);
678         IWineMsiRemotePackage_Release(remote_package);
679
680         if (hr == S_OK)
681             return ret;
682
683         return FALSE;
684     }
685
686     switch (iRunMode)
687     {
688     case MSIRUNMODE_WINDOWS9X:
689         if (GetVersion() & 0x80000000)
690             r = TRUE;
691         break;
692
693     case MSIRUNMODE_OPERATIONS:
694     case MSIRUNMODE_RESERVED11:
695     case MSIRUNMODE_RESERVED14:
696     case MSIRUNMODE_RESERVED15:
697         break;
698
699     case MSIRUNMODE_SCHEDULED:
700         r = package->scheduled_action_running;
701         break;
702
703     case MSIRUNMODE_ROLLBACK:
704         r = package->rollback_action_running;
705         break;
706
707     case MSIRUNMODE_COMMIT:
708         r = package->commit_action_running;
709         break;
710
711     case MSIRUNMODE_MAINTENANCE:
712         r = msi_get_property_int( package->db, szInstalled, 0 ) != 0;
713         break;
714
715     case MSIRUNMODE_REBOOTATEND:
716         r = package->need_reboot;
717         break;
718
719     default:
720         FIXME("unimplemented run mode: %d\n", iRunMode);
721         r = TRUE;
722     }
723
724     msiobj_release( &package->hdr );
725     return r;
726 }
727
728 /***********************************************************************
729  *           MsiSetMode    (MSI.@)
730  */
731 UINT WINAPI MsiSetMode(MSIHANDLE hInstall, MSIRUNMODE iRunMode, BOOL fState)
732 {
733     MSIPACKAGE *package;
734     UINT r;
735
736     TRACE("%d %d %d\n", hInstall, iRunMode, fState);
737
738     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
739     if (!package)
740     {
741         HRESULT hr;
742         IWineMsiRemotePackage *remote_package;
743
744         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
745         if (!remote_package)
746             return FALSE;
747
748         hr = IWineMsiRemotePackage_SetMode( remote_package, iRunMode, fState );
749         IWineMsiRemotePackage_Release( remote_package );
750
751         if (FAILED(hr))
752         {
753             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
754                 return HRESULT_CODE(hr);
755
756             return ERROR_FUNCTION_FAILED;
757         }
758
759         return ERROR_SUCCESS;
760     }
761
762     switch (iRunMode)
763     {
764     case MSIRUNMODE_REBOOTATEND:
765         package->need_reboot = 1;
766         r = ERROR_SUCCESS;
767         break;
768
769     case MSIRUNMODE_REBOOTNOW:
770         FIXME("unimplemented run mode: %d\n", iRunMode);
771         r = ERROR_FUNCTION_FAILED;
772         break;
773
774     default:
775         r = ERROR_ACCESS_DENIED;
776     }
777
778     msiobj_release( &package->hdr );
779     return r;
780 }
781
782 /***********************************************************************
783  * MsiSetFeatureStateA (MSI.@)
784  *
785  * According to the docs, when this is called it immediately recalculates
786  * all the component states as well
787  */
788 UINT WINAPI MsiSetFeatureStateA(MSIHANDLE hInstall, LPCSTR szFeature,
789                                 INSTALLSTATE iState)
790 {
791     LPWSTR szwFeature = NULL;
792     UINT rc;
793
794     szwFeature = strdupAtoW(szFeature);
795
796     if (!szwFeature)
797         return ERROR_FUNCTION_FAILED;
798    
799     rc = MsiSetFeatureStateW(hInstall,szwFeature, iState); 
800
801     msi_free(szwFeature);
802
803     return rc;
804 }
805
806
807
808 UINT WINAPI MSI_SetFeatureStateW(MSIPACKAGE* package, LPCWSTR szFeature,
809                                 INSTALLSTATE iState)
810 {
811     UINT rc = ERROR_SUCCESS;
812     MSIFEATURE *feature, *child;
813
814     TRACE("%s %i\n", debugstr_w(szFeature), iState);
815
816     feature = get_loaded_feature(package,szFeature);
817     if (!feature)
818         return ERROR_UNKNOWN_FEATURE;
819
820     if (iState == INSTALLSTATE_ADVERTISED && 
821         feature->Attributes & msidbFeatureAttributesDisallowAdvertise)
822         return ERROR_FUNCTION_FAILED;
823
824     msi_feature_set_state(package, feature, iState);
825
826     ACTION_UpdateComponentStates(package,szFeature);
827
828     /* update all the features that are children of this feature */
829     LIST_FOR_EACH_ENTRY( child, &package->features, MSIFEATURE, entry )
830     {
831         if (lstrcmpW(szFeature, child->Feature_Parent) == 0)
832             MSI_SetFeatureStateW(package, child->Feature, iState);
833     }
834     
835     return rc;
836 }
837
838 /***********************************************************************
839  * MsiSetFeatureStateW (MSI.@)
840  */
841 UINT WINAPI MsiSetFeatureStateW(MSIHANDLE hInstall, LPCWSTR szFeature,
842                                 INSTALLSTATE iState)
843 {
844     MSIPACKAGE* package;
845     UINT rc = ERROR_SUCCESS;
846
847     TRACE("%s %i\n",debugstr_w(szFeature), iState);
848
849     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
850     if (!package)
851     {
852         HRESULT hr;
853         BSTR feature;
854         IWineMsiRemotePackage *remote_package;
855
856         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
857         if (!remote_package)
858             return ERROR_INVALID_HANDLE;
859
860         feature = SysAllocString(szFeature);
861         if (!feature)
862         {
863             IWineMsiRemotePackage_Release(remote_package);
864             return ERROR_OUTOFMEMORY;
865         }
866
867         hr = IWineMsiRemotePackage_SetFeatureState(remote_package, feature, iState);
868
869         SysFreeString(feature);
870         IWineMsiRemotePackage_Release(remote_package);
871
872         if (FAILED(hr))
873         {
874             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
875                 return HRESULT_CODE(hr);
876
877             return ERROR_FUNCTION_FAILED;
878         }
879
880         return ERROR_SUCCESS;
881     }
882
883     rc = MSI_SetFeatureStateW(package,szFeature,iState);
884
885     msiobj_release( &package->hdr );
886     return rc;
887 }
888
889 /***********************************************************************
890 * MsiGetFeatureStateA   (MSI.@)
891 */
892 UINT WINAPI MsiGetFeatureStateA(MSIHANDLE hInstall, LPCSTR szFeature,
893                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
894 {
895     LPWSTR szwFeature = NULL;
896     UINT rc;
897     
898     szwFeature = strdupAtoW(szFeature);
899
900     rc = MsiGetFeatureStateW(hInstall,szwFeature,piInstalled, piAction);
901
902     msi_free( szwFeature);
903
904     return rc;
905 }
906
907 UINT MSI_GetFeatureStateW(MSIPACKAGE *package, LPCWSTR szFeature,
908                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
909 {
910     MSIFEATURE *feature;
911
912     feature = get_loaded_feature(package,szFeature);
913     if (!feature)
914         return ERROR_UNKNOWN_FEATURE;
915
916     if (piInstalled)
917         *piInstalled = feature->Installed;
918
919     if (piAction)
920         *piAction = feature->Action;
921
922     TRACE("returning %i %i\n", feature->Installed, feature->Action);
923
924     return ERROR_SUCCESS;
925 }
926
927 /***********************************************************************
928 * MsiGetFeatureStateW   (MSI.@)
929 */
930 UINT WINAPI MsiGetFeatureStateW(MSIHANDLE hInstall, LPCWSTR szFeature,
931                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
932 {
933     MSIPACKAGE* package;
934     UINT ret;
935
936     TRACE("%d %s %p %p\n", hInstall, debugstr_w(szFeature), piInstalled, piAction);
937
938     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
939     if (!package)
940     {
941         HRESULT hr;
942         BSTR feature;
943         IWineMsiRemotePackage *remote_package;
944
945         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
946         if (!remote_package)
947             return ERROR_INVALID_HANDLE;
948
949         feature = SysAllocString(szFeature);
950         if (!feature)
951         {
952             IWineMsiRemotePackage_Release(remote_package);
953             return ERROR_OUTOFMEMORY;
954         }
955
956         hr = IWineMsiRemotePackage_GetFeatureState(remote_package, feature,
957                                                    piInstalled, piAction);
958
959         SysFreeString(feature);
960         IWineMsiRemotePackage_Release(remote_package);
961
962         if (FAILED(hr))
963         {
964             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
965                 return HRESULT_CODE(hr);
966
967             return ERROR_FUNCTION_FAILED;
968         }
969
970         return ERROR_SUCCESS;
971     }
972
973     ret = MSI_GetFeatureStateW(package, szFeature, piInstalled, piAction);
974     msiobj_release( &package->hdr );
975     return ret;
976 }
977
978 /***********************************************************************
979 * MsiGetFeatureCostA   (MSI.@)
980 */
981 UINT WINAPI MsiGetFeatureCostA(MSIHANDLE hInstall, LPCSTR szFeature,
982                   MSICOSTTREE iCostTree, INSTALLSTATE iState, LPINT piCost)
983 {
984     LPWSTR szwFeature = NULL;
985     UINT rc;
986
987     szwFeature = strdupAtoW(szFeature);
988
989     rc = MsiGetFeatureCostW(hInstall, szwFeature, iCostTree, iState, piCost);
990
991     msi_free(szwFeature);
992
993     return rc;
994 }
995
996 UINT MSI_GetFeatureCost(MSIPACKAGE *package, MSIFEATURE *feature,
997                         MSICOSTTREE iCostTree, INSTALLSTATE iState,
998                         LPINT piCost)
999 {
1000     FIXME("(%s %i %i %p): not implemented yet\n",
1001         debugstr_w(feature->Feature), iCostTree, iState, piCost);
1002     if (piCost) *piCost = 0;
1003     return ERROR_SUCCESS;
1004 }
1005
1006 /***********************************************************************
1007 * MsiGetFeatureCostW   (MSI.@)
1008 */
1009 UINT WINAPI MsiGetFeatureCostW(MSIHANDLE hInstall, LPCWSTR szFeature,
1010                   MSICOSTTREE iCostTree, INSTALLSTATE iState, LPINT piCost)
1011 {
1012     MSIPACKAGE *package;
1013     MSIFEATURE *feature;
1014     UINT ret;
1015
1016     TRACE("(%d %s %i %i %p)\n", hInstall, debugstr_w(szFeature),
1017           iCostTree, iState, piCost);
1018
1019     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1020     if (!package)
1021     {
1022         HRESULT hr;
1023         BSTR feature;
1024         IWineMsiRemotePackage *remote_package;
1025
1026         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1027         if (!remote_package)
1028             return ERROR_INVALID_HANDLE;
1029
1030         feature = SysAllocString(szFeature);
1031         if (!feature)
1032         {
1033             IWineMsiRemotePackage_Release(remote_package);
1034             return ERROR_OUTOFMEMORY;
1035         }
1036
1037         hr = IWineMsiRemotePackage_GetFeatureCost(remote_package, feature,
1038                                                   iCostTree, iState, piCost);
1039
1040         SysFreeString(feature);
1041         IWineMsiRemotePackage_Release(remote_package);
1042
1043         if (FAILED(hr))
1044         {
1045             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1046                 return HRESULT_CODE(hr);
1047
1048             return ERROR_FUNCTION_FAILED;
1049         }
1050
1051         return ERROR_SUCCESS;
1052     }
1053
1054     feature = get_loaded_feature(package, szFeature);
1055
1056     if (feature)
1057         ret = MSI_GetFeatureCost(package, feature, iCostTree, iState, piCost);
1058     else
1059         ret = ERROR_UNKNOWN_FEATURE;
1060
1061     msiobj_release( &package->hdr );
1062     return ret;
1063 }
1064
1065 /***********************************************************************
1066  * MsiSetComponentStateA (MSI.@)
1067  */
1068 UINT WINAPI MsiSetComponentStateA(MSIHANDLE hInstall, LPCSTR szComponent,
1069                                   INSTALLSTATE iState)
1070 {
1071     UINT rc;
1072     LPWSTR szwComponent = strdupAtoW(szComponent);
1073
1074     rc = MsiSetComponentStateW(hInstall, szwComponent, iState);
1075
1076     msi_free(szwComponent);
1077
1078     return rc;
1079 }
1080
1081 /***********************************************************************
1082  * MsiGetComponentStateA (MSI.@)
1083  */
1084 UINT WINAPI MsiGetComponentStateA(MSIHANDLE hInstall, LPCSTR szComponent,
1085                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1086 {
1087     LPWSTR szwComponent= NULL;
1088     UINT rc;
1089     
1090     szwComponent= strdupAtoW(szComponent);
1091
1092     rc = MsiGetComponentStateW(hInstall,szwComponent,piInstalled, piAction);
1093
1094     msi_free( szwComponent);
1095
1096     return rc;
1097 }
1098
1099 static UINT MSI_SetComponentStateW(MSIPACKAGE *package, LPCWSTR szComponent,
1100                                    INSTALLSTATE iState)
1101 {
1102     MSICOMPONENT *comp;
1103
1104     TRACE("%p %s %d\n", package, debugstr_w(szComponent), iState);
1105
1106     comp = get_loaded_component(package, szComponent);
1107     if (!comp)
1108         return ERROR_UNKNOWN_COMPONENT;
1109
1110     comp->Installed = iState;
1111
1112     return ERROR_SUCCESS;
1113 }
1114
1115 UINT MSI_GetComponentStateW(MSIPACKAGE *package, LPCWSTR szComponent,
1116                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1117 {
1118     MSICOMPONENT *comp;
1119
1120     TRACE("%p %s %p %p\n", package, debugstr_w(szComponent),
1121            piInstalled, piAction);
1122
1123     comp = get_loaded_component(package,szComponent);
1124     if (!comp)
1125         return ERROR_UNKNOWN_COMPONENT;
1126
1127     if (piInstalled)
1128         *piInstalled = comp->Installed;
1129
1130     if (piAction)
1131         *piAction = comp->Action;
1132
1133     TRACE("states (%i, %i)\n", comp->Installed, comp->Action );
1134
1135     return ERROR_SUCCESS;
1136 }
1137
1138 /***********************************************************************
1139  * MsiSetComponentStateW (MSI.@)
1140  */
1141 UINT WINAPI MsiSetComponentStateW(MSIHANDLE hInstall, LPCWSTR szComponent,
1142                                   INSTALLSTATE iState)
1143 {
1144     MSIPACKAGE* package;
1145     UINT ret;
1146
1147     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1148     if (!package)
1149     {
1150         HRESULT hr;
1151         BSTR component;
1152         IWineMsiRemotePackage *remote_package;
1153
1154         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1155         if (!remote_package)
1156             return ERROR_INVALID_HANDLE;
1157
1158         component = SysAllocString(szComponent);
1159         if (!component)
1160         {
1161             IWineMsiRemotePackage_Release(remote_package);
1162             return ERROR_OUTOFMEMORY;
1163         }
1164
1165         hr = IWineMsiRemotePackage_SetComponentState(remote_package, component, iState);
1166
1167         SysFreeString(component);
1168         IWineMsiRemotePackage_Release(remote_package);
1169
1170         if (FAILED(hr))
1171         {
1172             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1173                 return HRESULT_CODE(hr);
1174
1175             return ERROR_FUNCTION_FAILED;
1176         }
1177
1178         return ERROR_SUCCESS;
1179     }
1180
1181     ret = MSI_SetComponentStateW(package, szComponent, iState);
1182     msiobj_release(&package->hdr);
1183     return ret;
1184 }
1185
1186 /***********************************************************************
1187  * MsiGetComponentStateW (MSI.@)
1188  */
1189 UINT WINAPI MsiGetComponentStateW(MSIHANDLE hInstall, LPCWSTR szComponent,
1190                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1191 {
1192     MSIPACKAGE* package;
1193     UINT ret;
1194
1195     TRACE("%d %s %p %p\n", hInstall, debugstr_w(szComponent),
1196            piInstalled, piAction);
1197
1198     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1199     if (!package)
1200     {
1201         HRESULT hr;
1202         BSTR component;
1203         IWineMsiRemotePackage *remote_package;
1204
1205         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1206         if (!remote_package)
1207             return ERROR_INVALID_HANDLE;
1208
1209         component = SysAllocString(szComponent);
1210         if (!component)
1211         {
1212             IWineMsiRemotePackage_Release(remote_package);
1213             return ERROR_OUTOFMEMORY;
1214         }
1215
1216         hr = IWineMsiRemotePackage_GetComponentState(remote_package, component,
1217                                                      piInstalled, piAction);
1218
1219         SysFreeString(component);
1220         IWineMsiRemotePackage_Release(remote_package);
1221
1222         if (FAILED(hr))
1223         {
1224             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1225                 return HRESULT_CODE(hr);
1226
1227             return ERROR_FUNCTION_FAILED;
1228         }
1229
1230         return ERROR_SUCCESS;
1231     }
1232
1233     ret = MSI_GetComponentStateW( package, szComponent, piInstalled, piAction);
1234     msiobj_release( &package->hdr );
1235     return ret;
1236 }
1237
1238 /***********************************************************************
1239  * MsiGetLanguage (MSI.@)
1240  */
1241 LANGID WINAPI MsiGetLanguage(MSIHANDLE hInstall)
1242 {
1243     MSIPACKAGE* package;
1244     LANGID langid;
1245     static const WCHAR szProductLanguage[] =
1246         {'P','r','o','d','u','c','t','L','a','n','g','u','a','g','e',0};
1247     
1248     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1249     if (!package)
1250     {
1251         HRESULT hr;
1252         LANGID lang;
1253         IWineMsiRemotePackage *remote_package;
1254
1255         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1256         if (!remote_package)
1257             return ERROR_INVALID_HANDLE;
1258
1259         hr = IWineMsiRemotePackage_GetLanguage(remote_package, &lang);
1260
1261         if (SUCCEEDED(hr))
1262             return lang;
1263
1264         return 0;
1265     }
1266
1267     langid = msi_get_property_int( package->db, szProductLanguage, 0 );
1268     msiobj_release( &package->hdr );
1269     return langid;
1270 }
1271
1272 UINT MSI_SetInstallLevel( MSIPACKAGE *package, int iInstallLevel )
1273 {
1274     static const WCHAR szInstallLevel[] = {
1275         'I','N','S','T','A','L','L','L','E','V','E','L',0 };
1276     static const WCHAR fmt[] = { '%','d',0 };
1277     WCHAR level[6];
1278     UINT r;
1279
1280     TRACE("%p %i\n", package, iInstallLevel);
1281
1282     if (iInstallLevel > 32767)
1283         return ERROR_INVALID_PARAMETER;
1284
1285     if (iInstallLevel < 1)
1286         return MSI_SetFeatureStates( package );
1287
1288     sprintfW( level, fmt, iInstallLevel );
1289     r = msi_set_property( package->db, szInstallLevel, level );
1290     if ( r == ERROR_SUCCESS )
1291         r = MSI_SetFeatureStates( package );
1292
1293     return r;
1294 }
1295
1296 /***********************************************************************
1297  * MsiSetInstallLevel (MSI.@)
1298  */
1299 UINT WINAPI MsiSetInstallLevel(MSIHANDLE hInstall, int iInstallLevel)
1300 {
1301     MSIPACKAGE* package;
1302     UINT r;
1303
1304     TRACE("%d %i\n", hInstall, iInstallLevel);
1305
1306     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1307     if (!package)
1308     {
1309         HRESULT hr;
1310         IWineMsiRemotePackage *remote_package;
1311
1312         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1313         if (!remote_package)
1314             return ERROR_INVALID_HANDLE;
1315
1316         hr = IWineMsiRemotePackage_SetInstallLevel(remote_package, iInstallLevel);
1317
1318         IWineMsiRemotePackage_Release(remote_package);
1319
1320         if (FAILED(hr))
1321         {
1322             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1323                 return HRESULT_CODE(hr);
1324
1325             return ERROR_FUNCTION_FAILED;
1326         }
1327
1328         return ERROR_SUCCESS;
1329     }
1330
1331     r = MSI_SetInstallLevel( package, iInstallLevel );
1332
1333     msiobj_release( &package->hdr );
1334
1335     return r;
1336 }
1337
1338 /***********************************************************************
1339  * MsiGetFeatureValidStatesW (MSI.@)
1340  */
1341 UINT WINAPI MsiGetFeatureValidStatesW(MSIHANDLE hInstall, LPCWSTR szFeature,
1342                   LPDWORD pInstallState)
1343 {
1344     if(pInstallState) *pInstallState = 1<<INSTALLSTATE_LOCAL;
1345     FIXME("%d %s %p stub returning %d\n",
1346         hInstall, debugstr_w(szFeature), pInstallState, pInstallState ? *pInstallState : 0);
1347
1348     return ERROR_SUCCESS;
1349 }
1350
1351 /***********************************************************************
1352  * MsiGetFeatureValidStatesA (MSI.@)
1353  */
1354 UINT WINAPI MsiGetFeatureValidStatesA(MSIHANDLE hInstall, LPCSTR szFeature,
1355                   LPDWORD pInstallState)
1356 {
1357     UINT ret;
1358     LPWSTR szwFeature = strdupAtoW(szFeature);
1359
1360     ret = MsiGetFeatureValidStatesW(hInstall, szwFeature, pInstallState);
1361
1362     msi_free(szwFeature);
1363
1364     return ret;
1365 }