install.c 49.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Implementation of the Microsoft Installer (msi.dll)
 *
 * Copyright 2005 Aric Stewart for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
 */

/* Msi top level apis directly related to installs */

23 24
#define COBJMACROS

25 26 27 28 29 30 31 32
#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "winerror.h"
#include "wine/debug.h"
#include "msi.h"
#include "msidefs.h"
James Hawkins's avatar
James Hawkins committed
33 34
#include "objbase.h"
#include "oleauto.h"
35

36
#include "msipriv.h"
37
#include "msiserver.h"
38
#include "wine/unicode.h"
39 40 41 42 43 44 45 46 47

WINE_DEFAULT_DEBUG_CHANNEL(msi);

/***********************************************************************
 * MsiDoActionA       (MSI.@)
 */
UINT WINAPI MsiDoActionA( MSIHANDLE hInstall, LPCSTR szAction )
{
    LPWSTR szwAction;
48
    UINT ret;
49

50
    TRACE("%s\n", debugstr_a(szAction));
51 52

    szwAction = strdupAtoW(szAction);
53
    if (szAction && !szwAction)
54 55
        return ERROR_FUNCTION_FAILED; 

56
    ret = MsiDoActionW( hInstall, szwAction );
57
    msi_free( szwAction );
58
    return ret;
59 60 61 62 63 64 65 66
}

/***********************************************************************
 * MsiDoActionW       (MSI.@)
 */
UINT WINAPI MsiDoActionW( MSIHANDLE hInstall, LPCWSTR szAction )
{
    MSIPACKAGE *package;
67 68 69 70 71 72
    UINT ret;

    TRACE("%s\n",debugstr_w(szAction));

    if (!szAction)
        return ERROR_INVALID_PARAMETER;
73

74 75
    package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
    if (!package)
76 77
    {
        HRESULT hr;
James Hawkins's avatar
James Hawkins committed
78
        BSTR action;
79 80 81 82 83 84
        IWineMsiRemotePackage *remote_package;

        remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
        if (!remote_package)
            return ERROR_INVALID_HANDLE;

James Hawkins's avatar
James Hawkins committed
85 86 87 88 89 90
        action = SysAllocString( szAction );
        if (!action)
        {
            IWineMsiRemotePackage_Release( remote_package );
            return ERROR_OUTOFMEMORY;
        }
91

James Hawkins's avatar
James Hawkins committed
92 93 94
        hr = IWineMsiRemotePackage_DoAction( remote_package, action );

        SysFreeString( action );
95 96 97 98 99 100 101 102 103 104 105 106
        IWineMsiRemotePackage_Release( remote_package );

        if (FAILED(hr))
        {
            if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
                return HRESULT_CODE(hr);

            return ERROR_FUNCTION_FAILED;
        }

        return ERROR_SUCCESS;
    }
107
 
108
    ret = ACTION_PerformUIAction( package, szAction, SCRIPT_NONE );
109
    msiobj_release( &package->hdr );
110 111 112 113

    return ret;
}

114 115 116
/***********************************************************************
 * MsiSequenceA       (MSI.@)
 */
117
UINT WINAPI MsiSequenceA( MSIHANDLE hInstall, LPCSTR szTable, INT iSequenceMode )
118
{
119 120 121
    LPWSTR szwTable;
    UINT ret;

122
    TRACE("%s, %d\n", debugstr_a(szTable), iSequenceMode);
123 124 125 126 127 128 129 130

    szwTable = strdupAtoW(szTable);
    if (szTable && !szwTable)
        return ERROR_FUNCTION_FAILED; 

    ret = MsiSequenceW( hInstall, szwTable, iSequenceMode );
    msi_free( szwTable );
    return ret;
131 132 133 134 135
}

/***********************************************************************
 * MsiSequenceW       (MSI.@)
 */
136
UINT WINAPI MsiSequenceW( MSIHANDLE hInstall, LPCWSTR szTable, INT iSequenceMode )
137
{
138 139 140
    MSIPACKAGE *package;
    UINT ret;

141
    TRACE("%s, %d\n", debugstr_w(szTable), iSequenceMode);
142 143 144

    package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
    if (!package)
145 146
    {
        HRESULT hr;
James Hawkins's avatar
James Hawkins committed
147
        BSTR table;
148 149 150 151 152 153
        IWineMsiRemotePackage *remote_package;

        remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
        if (!remote_package)
            return ERROR_INVALID_HANDLE;

James Hawkins's avatar
James Hawkins committed
154 155 156 157 158 159 160 161
        table = SysAllocString( szTable );
        if (!table)
        {
            IWineMsiRemotePackage_Release( remote_package );
            return ERROR_OUTOFMEMORY;
        }

        hr = IWineMsiRemotePackage_Sequence( remote_package, table, iSequenceMode );
162

James Hawkins's avatar
James Hawkins committed
163
        SysFreeString( table );
164 165 166 167 168 169 170 171 172 173 174 175
        IWineMsiRemotePackage_Release( remote_package );

        if (FAILED(hr))
        {
            if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
                return HRESULT_CODE(hr);

            return ERROR_FUNCTION_FAILED;
        }

        return ERROR_SUCCESS;
    }
176
    ret = MSI_Sequence( package, szTable );
177 178
    msiobj_release( &package->hdr );
    return ret;
179 180
}

181
UINT msi_strcpy_to_awstring( const WCHAR *str, int len, awstring *awbuf, DWORD *sz )
182
{
183
    UINT r = ERROR_SUCCESS;
184

185
    if (awbuf->str.w && !sz)
186 187
        return ERROR_INVALID_PARAMETER;
    if (!sz)
188 189 190
        return ERROR_SUCCESS;

    if (len < 0) len = strlenW( str );
191
 
192 193
    if (awbuf->unicode && awbuf->str.w)
        memcpy( awbuf->str.w, str, min(len + 1, *sz) * sizeof(WCHAR) );
194 195
    else
    {
196 197 198 199
        int lenA = WideCharToMultiByte( CP_ACP, 0, str, len + 1, NULL, 0, NULL, NULL );
        if (lenA) lenA--;
        WideCharToMultiByte( CP_ACP, 0, str, len + 1, awbuf->str.a, *sz, NULL, NULL );
        if (awbuf->str.a && *sz && lenA >= *sz)
200
            awbuf->str.a[*sz - 1] = 0;
201
        len = lenA;
202
    }
203
    if (awbuf->str.w && len >= *sz)
204 205 206 207 208
        r = ERROR_MORE_DATA;
    *sz = len;
    return r;
}

209 210
const WCHAR *msi_get_target_folder( MSIPACKAGE *package, const WCHAR *name )
{
211
    MSIFOLDER *folder = msi_get_loaded_folder( package, name );
212 213 214 215 216 217 218 219 220 221 222 223

    if (!folder) return NULL;
    if (!folder->ResolvedTarget)
    {
        MSIFOLDER *parent = folder;
        while (parent->Parent && strcmpW( parent->Parent, parent->Directory ))
        {
            parent = msi_get_loaded_folder( package, parent->Parent );
        }
        msi_resolve_target_folder( package, parent->Directory, TRUE );
    }
    return folder->ResolvedTarget;
224 225
}

226 227 228
/***********************************************************************
 * MsiGetTargetPath   (internal)
 */
229 230
static UINT MSI_GetTargetPath( MSIHANDLE hInstall, LPCWSTR szFolder,
                               awstring *szPathBuf, LPDWORD pcchPathBuf )
231 232
{
    MSIPACKAGE *package;
233
    const WCHAR *path;
234
    UINT r = ERROR_FUNCTION_FAILED;
235 236 237 238 239 240

    if (!szFolder)
        return ERROR_INVALID_PARAMETER;

    package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
    if (!package)
241 242 243 244
    {
        HRESULT hr;
        IWineMsiRemotePackage *remote_package;
        LPWSTR value = NULL;
James Hawkins's avatar
James Hawkins committed
245
        BSTR folder;
246 247 248 249 250 251
        DWORD len;

        remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
        if (!remote_package)
            return ERROR_INVALID_HANDLE;

James Hawkins's avatar
James Hawkins committed
252 253 254 255 256 257 258
        folder = SysAllocString( szFolder );
        if (!folder)
        {
            IWineMsiRemotePackage_Release( remote_package );
            return ERROR_OUTOFMEMORY;
        }

259
        len = 0;
260
        hr = IWineMsiRemotePackage_GetTargetPath( remote_package, folder, NULL, &len );
261 262 263 264 265 266 267 268 269 270 271
        if (FAILED(hr))
            goto done;

        len++;
        value = msi_alloc(len * sizeof(WCHAR));
        if (!value)
        {
            r = ERROR_OUTOFMEMORY;
            goto done;
        }

272
        hr = IWineMsiRemotePackage_GetTargetPath( remote_package, folder, value, &len );
273 274 275
        if (FAILED(hr))
            goto done;

276
        r = msi_strcpy_to_awstring( value, len, szPathBuf, pcchPathBuf );
277 278 279

done:
        IWineMsiRemotePackage_Release( remote_package );
James Hawkins's avatar
James Hawkins committed
280
        SysFreeString( folder );
281 282 283 284 285 286 287 288 289 290 291 292
        msi_free( value );

        if (FAILED(hr))
        {
            if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
                return HRESULT_CODE(hr);

            return ERROR_FUNCTION_FAILED;
        }

        return r;
    }
293

294
    path = msi_get_target_folder( package, szFolder );
295 296 297 298 299
    msiobj_release( &package->hdr );

    if (!path)
        return ERROR_DIRECTORY;

300
    return msi_strcpy_to_awstring( path, -1, szPathBuf, pcchPathBuf );
301 302
}

303 304 305 306
/***********************************************************************
 * MsiGetTargetPathA        (MSI.@)
 */
UINT WINAPI MsiGetTargetPathA( MSIHANDLE hInstall, LPCSTR szFolder, 
307
                               LPSTR szPathBuf, LPDWORD pcchPathBuf )
308 309
{
    LPWSTR szwFolder;
310 311
    awstring path;
    UINT r;
312

313
    TRACE("%s %p %p\n", debugstr_a(szFolder), szPathBuf, pcchPathBuf);
314 315

    szwFolder = strdupAtoW(szFolder);
316
    if (szFolder && !szwFolder)
317 318
        return ERROR_FUNCTION_FAILED; 

319 320
    path.unicode = FALSE;
    path.str.a = szPathBuf;
321

322
    r = MSI_GetTargetPath( hInstall, szwFolder, &path, pcchPathBuf );
323

324
    msi_free( szwFolder );
325

326
    return r;
327 328 329
}

/***********************************************************************
330 331 332
 * MsiGetTargetPathW        (MSI.@)
 */
UINT WINAPI MsiGetTargetPathW( MSIHANDLE hInstall, LPCWSTR szFolder,
333
                               LPWSTR szPathBuf, LPDWORD pcchPathBuf )
334
{
335
    awstring path;
336

337
    TRACE("%s %p %p\n", debugstr_w(szFolder), szPathBuf, pcchPathBuf);
338

339 340
    path.unicode = TRUE;
    path.str.w = szPathBuf;
341

342
    return MSI_GetTargetPath( hInstall, szFolder, &path, pcchPathBuf );
343 344
}

345
static WCHAR *get_source_root( MSIPACKAGE *package )
346
{
347 348
    msi_set_sourcedir_props( package, FALSE );
    return msi_dup_property( package->db, szSourceDir );
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
}

WCHAR *msi_resolve_source_folder( MSIPACKAGE *package, const WCHAR *name, MSIFOLDER **folder )
{
    MSIFOLDER *f;
    LPWSTR p, path = NULL, parent;

    TRACE("working to resolve %s\n", debugstr_w(name));

    if (!strcmpW( name, szSourceDir )) name = szTargetDir;
    if (!(f = msi_get_loaded_folder( package, name ))) return NULL;

    /* special resolving for root dir */
    if (!strcmpW( name, szTargetDir ) && !f->ResolvedSource)
    {
364
        f->ResolvedSource = get_source_root( package );
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
    }
    if (folder) *folder = f;
    if (f->ResolvedSource)
    {
        path = strdupW( f->ResolvedSource );
        TRACE("   already resolved to %s\n", debugstr_w(path));
        return path;
    }
    if (!f->Parent) return path;
    parent = f->Parent;
    TRACE(" ! parent is %s\n", debugstr_w(parent));

    p = msi_resolve_source_folder( package, parent, NULL );

    if (package->WordCount & msidbSumInfoSourceTypeCompressed)
380
        path = get_source_root( package );
381 382 383 384 385 386 387 388 389 390 391 392
    else if (package->WordCount & msidbSumInfoSourceTypeSFN)
        path = msi_build_directory_name( 3, p, f->SourceShortPath, NULL );
    else
        path = msi_build_directory_name( 3, p, f->SourceLongPath, NULL );

    TRACE("-> %s\n", debugstr_w(path));
    f->ResolvedSource = strdupW( path );
    msi_free( p );

    return path;
}

393
/***********************************************************************
394
 * MSI_GetSourcePath   (internal)
395 396
 */
static UINT MSI_GetSourcePath( MSIHANDLE hInstall, LPCWSTR szFolder,
397
                               awstring *szPathBuf, LPDWORD pcchPathBuf )
398
{
399
    MSIPACKAGE *package;
400
    LPWSTR path;
401
    UINT r = ERROR_FUNCTION_FAILED;
402

403
    TRACE("%s %p %p\n", debugstr_w(szFolder), szPathBuf, pcchPathBuf );
404 405

    if (!szFolder)
406
        return ERROR_INVALID_PARAMETER;
407

408
    package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
409
    if (!package)
410 411 412 413
    {
        HRESULT hr;
        IWineMsiRemotePackage *remote_package;
        LPWSTR value = NULL;
James Hawkins's avatar
James Hawkins committed
414
        BSTR folder;
415 416 417 418 419 420
        DWORD len;

        remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
        if (!remote_package)
            return ERROR_INVALID_HANDLE;

James Hawkins's avatar
James Hawkins committed
421 422 423 424 425 426 427
        folder = SysAllocString( szFolder );
        if (!folder)
        {
            IWineMsiRemotePackage_Release( remote_package );
            return ERROR_OUTOFMEMORY;
        }

428
        len = 0;
429
        hr = IWineMsiRemotePackage_GetSourcePath( remote_package, folder, NULL, &len );
430 431 432 433 434 435 436 437 438 439 440
        if (FAILED(hr))
            goto done;

        len++;
        value = msi_alloc(len * sizeof(WCHAR));
        if (!value)
        {
            r = ERROR_OUTOFMEMORY;
            goto done;
        }

441
        hr = IWineMsiRemotePackage_GetSourcePath( remote_package, folder, value, &len );
442 443 444
        if (FAILED(hr))
            goto done;

445
        r = msi_strcpy_to_awstring( value, len, szPathBuf, pcchPathBuf );
446 447 448

done:
        IWineMsiRemotePackage_Release( remote_package );
James Hawkins's avatar
James Hawkins committed
449
        SysFreeString( folder );
450 451 452 453 454 455 456 457 458 459 460 461
        msi_free( value );

        if (FAILED(hr))
        {
            if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
                return HRESULT_CODE(hr);

            return ERROR_FUNCTION_FAILED;
        }

        return r;
    }
462

463 464 465 466 467
    if (szPathBuf->str.w && !pcchPathBuf )
    {
        msiobj_release( &package->hdr );
        return ERROR_INVALID_PARAMETER;
    }
468

469
    path = msi_resolve_source_folder( package, szFolder, NULL );
470
    msiobj_release( &package->hdr );
471

472
    TRACE("path = %s\n", debugstr_w(path));
473 474
    if (!path)
        return ERROR_DIRECTORY;
475

476
    r = msi_strcpy_to_awstring( path, -1, szPathBuf, pcchPathBuf );
477
    msi_free( path );
478
    return r;
479 480 481
}

/***********************************************************************
482 483 484
 * MsiGetSourcePathA     (MSI.@)
 */
UINT WINAPI MsiGetSourcePathA( MSIHANDLE hInstall, LPCSTR szFolder, 
485
                               LPSTR szPathBuf, LPDWORD pcchPathBuf )
486
{
487 488 489
    LPWSTR folder;
    awstring str;
    UINT r;
490

491
    TRACE("%s %p %p\n", debugstr_a(szFolder), szPathBuf, pcchPathBuf);
492

493 494
    str.unicode = FALSE;
    str.str.a = szPathBuf;
495

496 497
    folder = strdupAtoW( szFolder );
    r = MSI_GetSourcePath( hInstall, folder, &str, pcchPathBuf );
498
    msi_free( folder );
499 500

    return r;
501 502
}

503 504 505 506
/***********************************************************************
 * MsiGetSourcePathW     (MSI.@)
 */
UINT WINAPI MsiGetSourcePathW( MSIHANDLE hInstall, LPCWSTR szFolder,
507
                               LPWSTR szPathBuf, LPDWORD pcchPathBuf )
508 509 510 511 512 513 514 515 516 517
{
    awstring str;

    TRACE("%s %p %p\n", debugstr_w(szFolder), szPathBuf, pcchPathBuf );

    str.unicode = TRUE;
    str.str.w = szPathBuf;

    return MSI_GetSourcePath( hInstall, szFolder, &str, pcchPathBuf );
}
518 519 520 521

/***********************************************************************
 * MsiSetTargetPathA  (MSI.@)
 */
522 523
UINT WINAPI MsiSetTargetPathA( MSIHANDLE hInstall, LPCSTR szFolder,
                               LPCSTR szFolderPath )
524
{
525 526
    LPWSTR szwFolder = NULL, szwFolderPath = NULL;
    UINT rc = ERROR_OUTOFMEMORY;
527

528 529
    if ( !szFolder || !szFolderPath )
        return ERROR_INVALID_PARAMETER;
530 531 532

    szwFolder = strdupAtoW(szFolder);
    szwFolderPath = strdupAtoW(szFolderPath);
533 534
    if (!szwFolder || !szwFolderPath)
        goto end;
535

536
    rc = MsiSetTargetPathW( hInstall, szwFolder, szwFolderPath );
537

538
end:
539 540
    msi_free(szwFolder);
    msi_free(szwFolderPath);
541 542 543 544

    return rc;
}

545
static void set_target_path( MSIPACKAGE *package, MSIFOLDER *folder, const WCHAR *path )
546
{
547 548
    FolderList *fl;
    MSIFOLDER *child;
549
    WCHAR *target_path;
550

551
    if (!(target_path = msi_normalize_path( path ))) return;
552
    if (strcmpW( target_path, folder->ResolvedTarget ))
553
    {
554 555
        msi_free( folder->ResolvedTarget );
        folder->ResolvedTarget = target_path;
556
        msi_set_property( package->db, folder->Directory, folder->ResolvedTarget, -1 );
557 558 559 560 561 562

        LIST_FOR_EACH_ENTRY( fl, &folder->children, FolderList, entry )
        {
            child = fl->folder;
            msi_resolve_target_folder( package, child->Directory, FALSE );
        }
563
    }
564
    else msi_free( target_path );
565 566 567 568
}

UINT MSI_SetTargetPathW( MSIPACKAGE *package, LPCWSTR szFolder, LPCWSTR szFolderPath )
{
569
    DWORD attrib;
570
    MSIFOLDER *folder;
571
    MSIFILE *file;
572

573
    TRACE("%p %s %s\n", package, debugstr_w(szFolder), debugstr_w(szFolderPath));
574

575
    attrib = GetFileAttributesW(szFolderPath);
576
    /* native MSI tests writeability by making temporary files at each drive */
577 578 579
    if (attrib != INVALID_FILE_ATTRIBUTES &&
        (attrib & FILE_ATTRIBUTE_OFFLINE || attrib & FILE_ATTRIBUTE_READONLY))
    {
580
        return ERROR_FUNCTION_FAILED;
581
    }
582
    if (!(folder = msi_get_loaded_folder( package, szFolder ))) return ERROR_DIRECTORY;
583

584
    set_target_path( package, folder, szFolderPath );
585

586 587 588 589
    LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
    {
        const WCHAR *dir;
        MSICOMPONENT *comp = file->Component;
590

591
        if (!comp->Enabled || msi_is_global_assembly( comp )) continue;
592

593 594
        dir = msi_get_target_folder( package, comp->Directory );
        msi_free( file->TargetPath );
595
        file->TargetPath = msi_build_directory_name( 2, dir, file->FileName );
596 597 598 599 600 601 602 603 604 605 606 607 608
    }
    return ERROR_SUCCESS;
}

/***********************************************************************
 * MsiSetTargetPathW  (MSI.@)
 */
UINT WINAPI MsiSetTargetPathW(MSIHANDLE hInstall, LPCWSTR szFolder, 
                             LPCWSTR szFolderPath)
{
    MSIPACKAGE *package;
    UINT ret;

609
    TRACE("%s %s\n",debugstr_w(szFolder),debugstr_w(szFolderPath));
610

611 612 613
    if ( !szFolder || !szFolderPath )
        return ERROR_INVALID_PARAMETER;

614
    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
615
    if (!package)
616 617
    {
        HRESULT hr;
James Hawkins's avatar
James Hawkins committed
618
        BSTR folder, path;
619 620 621 622 623 624
        IWineMsiRemotePackage *remote_package;

        remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
        if (!remote_package)
            return ERROR_INVALID_HANDLE;

James Hawkins's avatar
James Hawkins committed
625 626 627 628 629 630 631 632 633
        folder = SysAllocString( szFolder );
        path = SysAllocString( szFolderPath );
        if (!folder || !path)
        {
            SysFreeString(folder);
            SysFreeString(path);
            IWineMsiRemotePackage_Release( remote_package );
            return ERROR_OUTOFMEMORY;
        }
634

James Hawkins's avatar
James Hawkins committed
635 636 637 638
        hr = IWineMsiRemotePackage_SetTargetPath( remote_package, folder, path );

        SysFreeString(folder);
        SysFreeString(path);
639 640 641 642 643 644 645 646 647 648 649 650
        IWineMsiRemotePackage_Release( remote_package );

        if (FAILED(hr))
        {
            if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
                return HRESULT_CODE(hr);

            return ERROR_FUNCTION_FAILED;
        }

        return ERROR_SUCCESS;
    }
651

652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
    ret = MSI_SetTargetPathW( package, szFolder, szFolderPath );
    msiobj_release( &package->hdr );
    return ret;
}

/***********************************************************************
 *           MsiGetMode    (MSI.@)
 *
 * Returns an internal installer state (if it is running in a mode iRunMode)
 *
 * PARAMS
 *   hInstall    [I]  Handle to the installation
 *   hRunMode    [I]  Checking run mode
 *        MSIRUNMODE_ADMIN             Administrative mode
 *        MSIRUNMODE_ADVERTISE         Advertisement mode
 *        MSIRUNMODE_MAINTENANCE       Maintenance mode
 *        MSIRUNMODE_ROLLBACKENABLED   Rollback is enabled
 *        MSIRUNMODE_LOGENABLED        Log file is writing
 *        MSIRUNMODE_OPERATIONS        Operations in progress??
 *        MSIRUNMODE_REBOOTATEND       We need to reboot after installation completed
 *        MSIRUNMODE_REBOOTNOW         We need to reboot to continue the installation
 *        MSIRUNMODE_CABINET           Files from cabinet are installed
 *        MSIRUNMODE_SOURCESHORTNAMES  Long names in source files is suppressed
 *        MSIRUNMODE_TARGETSHORTNAMES  Long names in destination files is suppressed
 *        MSIRUNMODE_RESERVED11        Reserved
 *        MSIRUNMODE_WINDOWS9X         Running under Windows95/98
 *        MSIRUNMODE_ZAWENABLED        Demand installation is supported
 *        MSIRUNMODE_RESERVED14        Reserved
 *        MSIRUNMODE_RESERVED15        Reserved
 *        MSIRUNMODE_SCHEDULED         called from install script
 *        MSIRUNMODE_ROLLBACK          called from rollback script
 *        MSIRUNMODE_COMMIT            called from commit script
 *
 * RETURNS
 *    In the state: TRUE
 *    Not in the state: FALSE
 *
 */
BOOL WINAPI MsiGetMode(MSIHANDLE hInstall, MSIRUNMODE iRunMode)
{
692
    MSIPACKAGE *package;
693 694
    BOOL r = FALSE;

695 696
    TRACE("%d %d\n", hInstall, iRunMode);

697 698
    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
    {
        BOOL ret;
        HRESULT hr;
        IWineMsiRemotePackage *remote_package;

        remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
        if (!remote_package)
            return FALSE;

        hr = IWineMsiRemotePackage_GetMode(remote_package, iRunMode, &ret);
        IWineMsiRemotePackage_Release(remote_package);

        if (hr == S_OK)
            return ret;

714
        return FALSE;
715
    }
716

717 718
    switch (iRunMode)
    {
719 720
    case MSIRUNMODE_ADMIN:
        FIXME("no support for administrative installs\n");
721 722 723 724
        break;

    case MSIRUNMODE_ADVERTISE:
        FIXME("no support for advertised installs\n");
725 726
        break;

727 728 729 730 731
    case MSIRUNMODE_WINDOWS9X:
        if (GetVersion() & 0x80000000)
            r = TRUE;
        break;

732
    case MSIRUNMODE_OPERATIONS:
733 734 735 736 737 738
    case MSIRUNMODE_RESERVED11:
    case MSIRUNMODE_RESERVED14:
    case MSIRUNMODE_RESERVED15:
        break;

    case MSIRUNMODE_SCHEDULED:
739 740 741
        r = package->scheduled_action_running;
        break;

742
    case MSIRUNMODE_ROLLBACK:
743 744 745
        r = package->rollback_action_running;
        break;

746
    case MSIRUNMODE_COMMIT:
747
        r = package->commit_action_running;
748 749
        break;

750
    case MSIRUNMODE_MAINTENANCE:
751
        r = msi_get_property_int( package->db, szInstalled, 0 ) != 0;
752 753
        break;

754 755 756 757
    case MSIRUNMODE_ROLLBACKENABLED:
        r = msi_get_property_int( package->db, szRollbackDisabled, 0 ) == 0;
        break;

758
    case MSIRUNMODE_REBOOTATEND:
759
        r = package->need_reboot_at_end;
760 761
        break;

762 763 764 765
    case MSIRUNMODE_REBOOTNOW:
        r = package->need_reboot_now;
        break;

766 767 768 769
    case MSIRUNMODE_LOGENABLED:
        r = (package->log_file != INVALID_HANDLE_VALUE);
        break;

770
    default:
771
        FIXME("unimplemented run mode: %d\n", iRunMode);
772 773 774
        r = TRUE;
    }

775
    msiobj_release( &package->hdr );
776
    return r;
777 778
}

779 780 781
/***********************************************************************
 *           MsiSetMode    (MSI.@)
 */
782
UINT WINAPI MsiSetMode(MSIHANDLE hInstall, MSIRUNMODE iRunMode, BOOL fState)
783
{
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
    MSIPACKAGE *package;
    UINT r;

    TRACE("%d %d %d\n", hInstall, iRunMode, fState);

    package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
    if (!package)
    {
        HRESULT hr;
        IWineMsiRemotePackage *remote_package;

        remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
        if (!remote_package)
            return FALSE;

        hr = IWineMsiRemotePackage_SetMode( remote_package, iRunMode, fState );
        IWineMsiRemotePackage_Release( remote_package );

        if (FAILED(hr))
        {
            if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
                return HRESULT_CODE(hr);

            return ERROR_FUNCTION_FAILED;
        }

        return ERROR_SUCCESS;
    }

    switch (iRunMode)
    {
    case MSIRUNMODE_REBOOTATEND:
816
        package->need_reboot_at_end = (fState != 0);
817 818 819 820
        r = ERROR_SUCCESS;
        break;

    case MSIRUNMODE_REBOOTNOW:
821 822
        package->need_reboot_now = (fState != 0);
        r = ERROR_SUCCESS;
823 824 825 826 827 828
        break;

    default:
        r = ERROR_ACCESS_DENIED;
    }

829
    msiobj_release( &package->hdr );
830
    return r;
831 832
}

833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
/***********************************************************************
 * MsiSetFeatureStateA (MSI.@)
 *
 * According to the docs, when this is called it immediately recalculates
 * all the component states as well
 */
UINT WINAPI MsiSetFeatureStateA(MSIHANDLE hInstall, LPCSTR szFeature,
                                INSTALLSTATE iState)
{
    LPWSTR szwFeature = NULL;
    UINT rc;

    szwFeature = strdupAtoW(szFeature);

    if (!szwFeature)
        return ERROR_FUNCTION_FAILED;
   
    rc = MsiSetFeatureStateW(hInstall,szwFeature, iState); 

852
    msi_free(szwFeature);
853 854 855 856

    return rc;
}

857 858 859 860 861
/* update component state based on a feature change */
void ACTION_UpdateComponentStates( MSIPACKAGE *package, MSIFEATURE *feature )
{
    INSTALLSTATE newstate;
    ComponentList *cl;
862

863 864
    newstate = feature->ActionRequest;
    if (newstate == INSTALLSTATE_ABSENT) newstate = INSTALLSTATE_UNKNOWN;
865

866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
    LIST_FOR_EACH_ENTRY(cl, &feature->Components, ComponentList, entry)
    {
        MSICOMPONENT *component = cl->component;

        if (!component->Enabled) continue;

        TRACE("Modifying (%d): Component %s (Installed %d, Action %d, Request %d)\n",
            newstate, debugstr_w(component->Component), component->Installed,
            component->Action, component->ActionRequest);

        if (newstate == INSTALLSTATE_LOCAL)
        {
            component->Action = INSTALLSTATE_LOCAL;
            component->ActionRequest = INSTALLSTATE_LOCAL;
        }
        else
        {
            ComponentList *clist;
            MSIFEATURE *f;

            component->hasLocalFeature = FALSE;

            component->Action = newstate;
            component->ActionRequest = newstate;
            /* if any other feature wants it local we need to set it local */
            LIST_FOR_EACH_ENTRY(f, &package->features, MSIFEATURE, entry)
            {
                if ( f->ActionRequest != INSTALLSTATE_LOCAL &&
                     f->ActionRequest != INSTALLSTATE_SOURCE )
                {
                    continue;
                }
                LIST_FOR_EACH_ENTRY(clist, &f->Components, ComponentList, entry)
                {
                    if (clist->component == component &&
                        (f->ActionRequest == INSTALLSTATE_LOCAL ||
                         f->ActionRequest == INSTALLSTATE_SOURCE))
                    {
                        TRACE("Saved by %s\n", debugstr_w(f->Feature));
                        component->hasLocalFeature = TRUE;

                        if (component->Attributes & msidbComponentAttributesOptional)
                        {
                            if (f->Attributes & msidbFeatureAttributesFavorSource)
                            {
                                component->Action = INSTALLSTATE_SOURCE;
                                component->ActionRequest = INSTALLSTATE_SOURCE;
                            }
                            else
                            {
                                component->Action = INSTALLSTATE_LOCAL;
                                component->ActionRequest = INSTALLSTATE_LOCAL;
                            }
                        }
                        else if (component->Attributes & msidbComponentAttributesSourceOnly)
                        {
                            component->Action = INSTALLSTATE_SOURCE;
                            component->ActionRequest = INSTALLSTATE_SOURCE;
                        }
                        else
                        {
                            component->Action = INSTALLSTATE_LOCAL;
                            component->ActionRequest = INSTALLSTATE_LOCAL;
                        }
                    }
                }
            }
        }
        TRACE("Result (%d): Component %s (Installed %d, Action %d, Request %d)\n",
            newstate, debugstr_w(component->Component), component->Installed,
            component->Action, component->ActionRequest);
    }
}

940
UINT MSI_SetFeatureStateW( MSIPACKAGE *package, LPCWSTR szFeature, INSTALLSTATE iState )
941 942
{
    UINT rc = ERROR_SUCCESS;
943
    MSIFEATURE *feature, *child;
944

945
    TRACE("%s %i\n", debugstr_w(szFeature), iState);
946

947
    feature = msi_get_loaded_feature( package, szFeature );
948
    if (!feature)
949 950 951
        return ERROR_UNKNOWN_FEATURE;

    if (iState == INSTALLSTATE_ADVERTISED && 
952
        feature->Attributes & msidbFeatureAttributesDisallowAdvertise)
953 954
        return ERROR_FUNCTION_FAILED;

955
    feature->ActionRequest = iState;
956

957
    ACTION_UpdateComponentStates( package, feature );
958 959

    /* update all the features that are children of this feature */
960
    LIST_FOR_EACH_ENTRY( child, &package->features, MSIFEATURE, entry )
961
    {
962
        if (child->Feature_Parent && !strcmpW( szFeature, child->Feature_Parent ))
963
            MSI_SetFeatureStateW(package, child->Feature, iState);
964 965 966 967 968 969 970 971 972 973 974 975 976 977
    }
    
    return rc;
}

/***********************************************************************
 * MsiSetFeatureStateW (MSI.@)
 */
UINT WINAPI MsiSetFeatureStateW(MSIHANDLE hInstall, LPCWSTR szFeature,
                                INSTALLSTATE iState)
{
    MSIPACKAGE* package;
    UINT rc = ERROR_SUCCESS;

978
    TRACE("%s %i\n",debugstr_w(szFeature), iState);
979 980 981

    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
982 983
    {
        HRESULT hr;
James Hawkins's avatar
James Hawkins committed
984
        BSTR feature;
985 986 987 988 989 990
        IWineMsiRemotePackage *remote_package;

        remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
        if (!remote_package)
            return ERROR_INVALID_HANDLE;

James Hawkins's avatar
James Hawkins committed
991 992 993 994 995 996 997 998
        feature = SysAllocString(szFeature);
        if (!feature)
        {
            IWineMsiRemotePackage_Release(remote_package);
            return ERROR_OUTOFMEMORY;
        }

        hr = IWineMsiRemotePackage_SetFeatureState(remote_package, feature, iState);
999

James Hawkins's avatar
James Hawkins committed
1000
        SysFreeString(feature);
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
        IWineMsiRemotePackage_Release(remote_package);

        if (FAILED(hr))
        {
            if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
                return HRESULT_CODE(hr);

            return ERROR_FUNCTION_FAILED;
        }

        return ERROR_SUCCESS;
    }
1013 1014 1015 1016 1017 1018 1019

    rc = MSI_SetFeatureStateW(package,szFeature,iState);

    msiobj_release( &package->hdr );
    return rc;
}

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
/***********************************************************************
* MsiSetFeatureAttributesA   (MSI.@)
*/
UINT WINAPI MsiSetFeatureAttributesA( MSIHANDLE handle, LPCSTR feature, DWORD attrs )
{
    UINT r;
    WCHAR *featureW = NULL;

    TRACE("%u, %s, 0x%08x\n", handle, debugstr_a(feature), attrs);

    if (feature && !(featureW = strdupAtoW( feature ))) return ERROR_OUTOFMEMORY;

    r = MsiSetFeatureAttributesW( handle, featureW, attrs );
    msi_free( featureW );
    return r;
}

static DWORD unmap_feature_attributes( DWORD attrs )
{
    DWORD ret = 0;

    if (attrs & INSTALLFEATUREATTRIBUTE_FAVORLOCAL)             ret = msidbFeatureAttributesFavorLocal;
    if (attrs & INSTALLFEATUREATTRIBUTE_FAVORSOURCE)            ret |= msidbFeatureAttributesFavorSource;
    if (attrs & INSTALLFEATUREATTRIBUTE_FOLLOWPARENT)           ret |= msidbFeatureAttributesFollowParent;
    if (attrs & INSTALLFEATUREATTRIBUTE_FAVORADVERTISE)         ret |= msidbFeatureAttributesFavorAdvertise;
    if (attrs & INSTALLFEATUREATTRIBUTE_DISALLOWADVERTISE)      ret |= msidbFeatureAttributesDisallowAdvertise;
    if (attrs & INSTALLFEATUREATTRIBUTE_NOUNSUPPORTEDADVERTISE) ret |= msidbFeatureAttributesNoUnsupportedAdvertise;
    return ret;
}

/***********************************************************************
* MsiSetFeatureAttributesW   (MSI.@)
*/
UINT WINAPI MsiSetFeatureAttributesW( MSIHANDLE handle, LPCWSTR name, DWORD attrs )
{
    MSIPACKAGE *package;
    MSIFEATURE *feature;
    WCHAR *costing;

    TRACE("%u, %s, 0x%08x\n", handle, debugstr_w(name), attrs);

    if (!name || !name[0]) return ERROR_UNKNOWN_FEATURE;

    if (!(package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE )))
        return ERROR_INVALID_HANDLE;

    costing = msi_dup_property( package->db, szCostingComplete );
    if (!costing || !strcmpW( costing, szOne ))
    {
        msi_free( costing );
        msiobj_release( &package->hdr );
        return ERROR_FUNCTION_FAILED;
    }
    msi_free( costing );
    if (!(feature = msi_get_loaded_feature( package, name )))
    {
        msiobj_release( &package->hdr );
        return ERROR_UNKNOWN_FEATURE;
    }
    feature->Attributes = unmap_feature_attributes( attrs );
    msiobj_release( &package->hdr );
    return ERROR_SUCCESS;
}

1084 1085 1086
/***********************************************************************
* MsiGetFeatureStateA   (MSI.@)
*/
1087
UINT WINAPI MsiGetFeatureStateA(MSIHANDLE hInstall, LPCSTR szFeature,
1088 1089 1090 1091 1092
                  INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
{
    LPWSTR szwFeature = NULL;
    UINT rc;
    
1093
    if (szFeature && !(szwFeature = strdupAtoW(szFeature))) return ERROR_OUTOFMEMORY;
1094

1095
    rc = MsiGetFeatureStateW(hInstall, szwFeature, piInstalled, piAction);
1096
    msi_free( szwFeature);
1097 1098 1099
    return rc;
}

1100
UINT MSI_GetFeatureStateW(MSIPACKAGE *package, LPCWSTR szFeature,
1101 1102
                  INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
{
1103
    MSIFEATURE *feature;
1104

1105
    feature = msi_get_loaded_feature(package,szFeature);
1106
    if (!feature)
1107 1108 1109
        return ERROR_UNKNOWN_FEATURE;

    if (piInstalled)
1110
        *piInstalled = feature->Installed;
1111 1112

    if (piAction)
1113
        *piAction = feature->ActionRequest;
1114

1115
    TRACE("returning %i %i\n", feature->Installed, feature->ActionRequest);
1116 1117 1118 1119 1120 1121 1122

    return ERROR_SUCCESS;
}

/***********************************************************************
* MsiGetFeatureStateW   (MSI.@)
*/
1123
UINT WINAPI MsiGetFeatureStateW(MSIHANDLE hInstall, LPCWSTR szFeature,
1124 1125 1126 1127 1128
                  INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
{
    MSIPACKAGE* package;
    UINT ret;

1129
    TRACE("%d %s %p %p\n", hInstall, debugstr_w(szFeature), piInstalled, piAction);
1130 1131 1132

    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
1133 1134
    {
        HRESULT hr;
James Hawkins's avatar
James Hawkins committed
1135
        BSTR feature;
1136 1137 1138 1139 1140 1141
        IWineMsiRemotePackage *remote_package;

        remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
        if (!remote_package)
            return ERROR_INVALID_HANDLE;

James Hawkins's avatar
James Hawkins committed
1142 1143 1144 1145 1146 1147 1148 1149
        feature = SysAllocString(szFeature);
        if (!feature)
        {
            IWineMsiRemotePackage_Release(remote_package);
            return ERROR_OUTOFMEMORY;
        }

        hr = IWineMsiRemotePackage_GetFeatureState(remote_package, feature,
1150 1151
                                                   piInstalled, piAction);

James Hawkins's avatar
James Hawkins committed
1152 1153 1154
        SysFreeString(feature);
        IWineMsiRemotePackage_Release(remote_package);

1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
        if (FAILED(hr))
        {
            if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
                return HRESULT_CODE(hr);

            return ERROR_FUNCTION_FAILED;
        }

        return ERROR_SUCCESS;
    }

1166 1167 1168 1169 1170
    ret = MSI_GetFeatureStateW(package, szFeature, piInstalled, piAction);
    msiobj_release( &package->hdr );
    return ret;
}

1171 1172 1173 1174
/***********************************************************************
* MsiGetFeatureCostA   (MSI.@)
*/
UINT WINAPI MsiGetFeatureCostA(MSIHANDLE hInstall, LPCSTR szFeature,
1175
                  MSICOSTTREE iCostTree, INSTALLSTATE iState, LPINT piCost)
1176
{
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
    LPWSTR szwFeature = NULL;
    UINT rc;

    szwFeature = strdupAtoW(szFeature);

    rc = MsiGetFeatureCostW(hInstall, szwFeature, iCostTree, iState, piCost);

    msi_free(szwFeature);

    return rc;
}

1189
static INT feature_cost( MSIFEATURE *feature )
1190
{
1191
    INT cost = 0;
1192
    ComponentList *cl;
1193

1194
    LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
1195
    {
1196
        cost += cl->component->Cost;
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
    }
    return cost;
}

UINT MSI_GetFeatureCost( MSIPACKAGE *package, MSIFEATURE *feature, MSICOSTTREE tree,
                         INSTALLSTATE state, LPINT cost )
{
    TRACE("%s, %u, %d, %p\n", debugstr_w(feature->Feature), tree, state, cost);

    *cost = 0;
    switch (tree)
    {
    case MSICOSTTREE_CHILDREN:
    {
        MSIFEATURE *child;

        LIST_FOR_EACH_ENTRY( child, &feature->Children, MSIFEATURE, entry )
        {
            if (child->ActionRequest == state)
                *cost += feature_cost( child );
        }
        break;
    }
    case MSICOSTTREE_PARENTS:
    {
        const WCHAR *feature_parent = feature->Feature_Parent;
        for (;;)
        {
1225
            MSIFEATURE *parent = msi_get_loaded_feature( package, feature_parent );
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
            if (!parent)
                break;

            if (parent->ActionRequest == state)
                *cost += feature_cost( parent );

            feature_parent = parent->Feature_Parent;
        }
        break;
    }
    case MSICOSTTREE_SELFONLY:
        if (feature->ActionRequest == state)
            *cost = feature_cost( feature );
        break;

    default:
        WARN("unhandled cost tree %u\n", tree);
        break;
    }

    *cost /= 512;
1247 1248 1249 1250 1251 1252 1253
    return ERROR_SUCCESS;
}

/***********************************************************************
* MsiGetFeatureCostW   (MSI.@)
*/
UINT WINAPI MsiGetFeatureCostW(MSIHANDLE hInstall, LPCWSTR szFeature,
1254
                  MSICOSTTREE iCostTree, INSTALLSTATE iState, LPINT piCost)
1255
{
1256 1257 1258 1259 1260
    MSIPACKAGE *package;
    MSIFEATURE *feature;
    UINT ret;

    TRACE("(%d %s %i %i %p)\n", hInstall, debugstr_w(szFeature),
1261
          iCostTree, iState, piCost);
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297

    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
    {
        HRESULT hr;
        BSTR feature;
        IWineMsiRemotePackage *remote_package;

        remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
        if (!remote_package)
            return ERROR_INVALID_HANDLE;

        feature = SysAllocString(szFeature);
        if (!feature)
        {
            IWineMsiRemotePackage_Release(remote_package);
            return ERROR_OUTOFMEMORY;
        }

        hr = IWineMsiRemotePackage_GetFeatureCost(remote_package, feature,
                                                  iCostTree, iState, piCost);

        SysFreeString(feature);
        IWineMsiRemotePackage_Release(remote_package);

        if (FAILED(hr))
        {
            if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
                return HRESULT_CODE(hr);

            return ERROR_FUNCTION_FAILED;
        }

        return ERROR_SUCCESS;
    }

1298
    feature = msi_get_loaded_feature(package, szFeature);
1299 1300 1301 1302 1303 1304 1305 1306

    if (feature)
        ret = MSI_GetFeatureCost(package, feature, iCostTree, iState, piCost);
    else
        ret = ERROR_UNKNOWN_FEATURE;

    msiobj_release( &package->hdr );
    return ret;
1307 1308
}

1309
/***********************************************************************
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
* MsiGetFeatureInfoA   (MSI.@)
*/
UINT WINAPI MsiGetFeatureInfoA( MSIHANDLE handle, LPCSTR feature, LPDWORD attrs,
                                LPSTR title, LPDWORD title_len, LPSTR help, LPDWORD help_len )
{
    UINT r;
    WCHAR *titleW = NULL, *helpW = NULL, *featureW = NULL;

    TRACE("%u, %s, %p, %p, %p, %p, %p\n", handle, debugstr_a(feature), attrs, title,
          title_len, help, help_len);

    if (feature && !(featureW = strdupAtoW( feature ))) return ERROR_OUTOFMEMORY;

    if (title && title_len && !(titleW = msi_alloc( *title_len * sizeof(WCHAR) )))
    {
        msi_free( featureW );
        return ERROR_OUTOFMEMORY;
    }
    if (help && help_len && !(helpW = msi_alloc( *help_len * sizeof(WCHAR) )))
    {
        msi_free( featureW );
        msi_free( titleW );
        return ERROR_OUTOFMEMORY;
    }
    r = MsiGetFeatureInfoW( handle, featureW, attrs, titleW, title_len, helpW, help_len );
    if (r == ERROR_SUCCESS)
    {
        if (titleW) WideCharToMultiByte( CP_ACP, 0, titleW, -1, title, *title_len + 1, NULL, NULL );
        if (helpW) WideCharToMultiByte( CP_ACP, 0, helpW, -1, help, *help_len + 1, NULL, NULL );
    }
    msi_free( titleW );
    msi_free( helpW );
    msi_free( featureW );
    return r;
}

static DWORD map_feature_attributes( DWORD attrs )
{
    DWORD ret = 0;

    if (attrs == msidbFeatureAttributesFavorLocal)            ret |= INSTALLFEATUREATTRIBUTE_FAVORLOCAL;
    if (attrs & msidbFeatureAttributesFavorSource)            ret |= INSTALLFEATUREATTRIBUTE_FAVORSOURCE;
    if (attrs & msidbFeatureAttributesFollowParent)           ret |= INSTALLFEATUREATTRIBUTE_FOLLOWPARENT;
    if (attrs & msidbFeatureAttributesFavorAdvertise)         ret |= INSTALLFEATUREATTRIBUTE_FAVORADVERTISE;
    if (attrs & msidbFeatureAttributesDisallowAdvertise)      ret |= INSTALLFEATUREATTRIBUTE_DISALLOWADVERTISE;
    if (attrs & msidbFeatureAttributesNoUnsupportedAdvertise) ret |= INSTALLFEATUREATTRIBUTE_NOUNSUPPORTEDADVERTISE;
    return ret;
}

static UINT MSI_GetFeatureInfo( MSIPACKAGE *package, LPCWSTR name, LPDWORD attrs,
                                LPWSTR title, LPDWORD title_len, LPWSTR help, LPDWORD help_len )
{
    UINT r = ERROR_SUCCESS;
    MSIFEATURE *feature = msi_get_loaded_feature( package, name );
    int len;

    if (!feature) return ERROR_UNKNOWN_FEATURE;
    if (attrs) *attrs = map_feature_attributes( feature->Attributes );
    if (title_len)
    {
        if (feature->Title) len = strlenW( feature->Title );
        else len = 0;
        if (*title_len <= len)
        {
            *title_len = len;
            if (title) r = ERROR_MORE_DATA;
        }
        else if (title)
        {
            if (feature->Title) strcpyW( title, feature->Title );
            else *title = 0;
            *title_len = len;
        }
    }
    if (help_len)
    {
        if (feature->Description) len = strlenW( feature->Description );
        else len = 0;
        if (*help_len <= len)
        {
            *help_len = len;
            if (help) r = ERROR_MORE_DATA;
        }
        else if (help)
        {
            if (feature->Description) strcpyW( help, feature->Description );
            else *help = 0;
            *help_len = len;
        }
    }
    return r;
}

/***********************************************************************
* MsiGetFeatureInfoW   (MSI.@)
*/
UINT WINAPI MsiGetFeatureInfoW( MSIHANDLE handle, LPCWSTR feature, LPDWORD attrs,
                                LPWSTR title, LPDWORD title_len, LPWSTR help, LPDWORD help_len )
{
    UINT r;
    MSIPACKAGE *package;

    TRACE("%u, %s, %p, %p, %p, %p, %p\n", handle, debugstr_w(feature), attrs, title,
          title_len, help, help_len);

    if (!feature) return ERROR_INVALID_PARAMETER;

    if (!(package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE )))
        return ERROR_INVALID_HANDLE;

    /* features may not have been loaded yet */
    msi_load_all_components( package );
    msi_load_all_features( package );

    r = MSI_GetFeatureInfo( package, feature, attrs, title, title_len, help, help_len );
    msiobj_release( &package->hdr );
    return r;
}

/***********************************************************************
1430 1431 1432 1433 1434
 * MsiSetComponentStateA (MSI.@)
 */
UINT WINAPI MsiSetComponentStateA(MSIHANDLE hInstall, LPCSTR szComponent,
                                  INSTALLSTATE iState)
{
1435 1436 1437 1438 1439
    UINT rc;
    LPWSTR szwComponent = strdupAtoW(szComponent);

    rc = MsiSetComponentStateW(hInstall, szwComponent, iState);

1440
    msi_free(szwComponent);
1441 1442

    return rc;
1443 1444
}

1445 1446 1447
/***********************************************************************
 * MsiGetComponentStateA (MSI.@)
 */
1448
UINT WINAPI MsiGetComponentStateA(MSIHANDLE hInstall, LPCSTR szComponent,
1449 1450 1451 1452 1453 1454 1455 1456 1457
                  INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
{
    LPWSTR szwComponent= NULL;
    UINT rc;
    
    szwComponent= strdupAtoW(szComponent);

    rc = MsiGetComponentStateW(hInstall,szwComponent,piInstalled, piAction);

1458
    msi_free( szwComponent);
1459 1460 1461 1462

    return rc;
}

1463 1464 1465 1466 1467 1468 1469
static UINT MSI_SetComponentStateW(MSIPACKAGE *package, LPCWSTR szComponent,
                                   INSTALLSTATE iState)
{
    MSICOMPONENT *comp;

    TRACE("%p %s %d\n", package, debugstr_w(szComponent), iState);

1470
    comp = msi_get_loaded_component(package, szComponent);
1471 1472 1473
    if (!comp)
        return ERROR_UNKNOWN_COMPONENT;

1474
    if (comp->Enabled)
1475
        comp->Action = iState;
1476 1477 1478 1479

    return ERROR_SUCCESS;
}

1480
UINT MSI_GetComponentStateW(MSIPACKAGE *package, LPCWSTR szComponent,
1481 1482
                  INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
{
1483
    MSICOMPONENT *comp;
1484

1485 1486
    TRACE("%p %s %p %p\n", package, debugstr_w(szComponent),
           piInstalled, piAction);
1487

1488
    comp = msi_get_loaded_component(package,szComponent);
1489
    if (!comp)
1490 1491 1492
        return ERROR_UNKNOWN_COMPONENT;

    if (piInstalled)
1493 1494 1495 1496 1497 1498
    {
        if (comp->Enabled)
            *piInstalled = comp->Installed;
        else
            *piInstalled = INSTALLSTATE_UNKNOWN;
    }
1499 1500

    if (piAction)
1501 1502 1503 1504 1505 1506
    {
        if (comp->Enabled)
            *piAction = comp->Action;
        else
            *piAction = INSTALLSTATE_UNKNOWN;
    }
1507

1508
    TRACE("states (%i, %i)\n", comp->Installed, comp->Action );
1509 1510 1511
    return ERROR_SUCCESS;
}

1512 1513 1514 1515 1516 1517
/***********************************************************************
 * MsiSetComponentStateW (MSI.@)
 */
UINT WINAPI MsiSetComponentStateW(MSIHANDLE hInstall, LPCWSTR szComponent,
                                  INSTALLSTATE iState)
{
1518 1519 1520 1521 1522
    MSIPACKAGE* package;
    UINT ret;

    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
1523 1524
    {
        HRESULT hr;
James Hawkins's avatar
James Hawkins committed
1525
        BSTR component;
1526 1527 1528 1529 1530 1531
        IWineMsiRemotePackage *remote_package;

        remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
        if (!remote_package)
            return ERROR_INVALID_HANDLE;

James Hawkins's avatar
James Hawkins committed
1532 1533 1534 1535 1536 1537 1538 1539
        component = SysAllocString(szComponent);
        if (!component)
        {
            IWineMsiRemotePackage_Release(remote_package);
            return ERROR_OUTOFMEMORY;
        }

        hr = IWineMsiRemotePackage_SetComponentState(remote_package, component, iState);
1540

James Hawkins's avatar
James Hawkins committed
1541
        SysFreeString(component);
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
        IWineMsiRemotePackage_Release(remote_package);

        if (FAILED(hr))
        {
            if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
                return HRESULT_CODE(hr);

            return ERROR_FUNCTION_FAILED;
        }

        return ERROR_SUCCESS;
    }

1555 1556 1557
    ret = MSI_SetComponentStateW(package, szComponent, iState);
    msiobj_release(&package->hdr);
    return ret;
1558 1559
}

1560 1561 1562
/***********************************************************************
 * MsiGetComponentStateW (MSI.@)
 */
1563
UINT WINAPI MsiGetComponentStateW(MSIHANDLE hInstall, LPCWSTR szComponent,
1564 1565 1566 1567 1568
                  INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
{
    MSIPACKAGE* package;
    UINT ret;

1569
    TRACE("%d %s %p %p\n", hInstall, debugstr_w(szComponent),
1570 1571 1572 1573
           piInstalled, piAction);

    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
1574 1575
    {
        HRESULT hr;
James Hawkins's avatar
James Hawkins committed
1576
        BSTR component;
1577 1578 1579 1580 1581 1582
        IWineMsiRemotePackage *remote_package;

        remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
        if (!remote_package)
            return ERROR_INVALID_HANDLE;

James Hawkins's avatar
James Hawkins committed
1583 1584 1585 1586 1587 1588 1589 1590
        component = SysAllocString(szComponent);
        if (!component)
        {
            IWineMsiRemotePackage_Release(remote_package);
            return ERROR_OUTOFMEMORY;
        }

        hr = IWineMsiRemotePackage_GetComponentState(remote_package, component,
1591 1592
                                                     piInstalled, piAction);

James Hawkins's avatar
James Hawkins committed
1593 1594 1595
        SysFreeString(component);
        IWineMsiRemotePackage_Release(remote_package);

1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
        if (FAILED(hr))
        {
            if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
                return HRESULT_CODE(hr);

            return ERROR_FUNCTION_FAILED;
        }

        return ERROR_SUCCESS;
    }

1607 1608 1609 1610
    ret = MSI_GetComponentStateW( package, szComponent, piInstalled, piAction);
    msiobj_release( &package->hdr );
    return ret;
}
Aric Stewart's avatar
Aric Stewart committed
1611 1612 1613 1614 1615 1616 1617 1618

/***********************************************************************
 * MsiGetLanguage (MSI.@)
 */
LANGID WINAPI MsiGetLanguage(MSIHANDLE hInstall)
{
    MSIPACKAGE* package;
    LANGID langid;
1619

Aric Stewart's avatar
Aric Stewart committed
1620 1621
    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
    {
        HRESULT hr;
        LANGID lang;
        IWineMsiRemotePackage *remote_package;

        remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
        if (!remote_package)
            return ERROR_INVALID_HANDLE;

        hr = IWineMsiRemotePackage_GetLanguage(remote_package, &lang);

        if (SUCCEEDED(hr))
            return lang;

        return 0;
    }
Aric Stewart's avatar
Aric Stewart committed
1638

1639
    langid = msi_get_property_int( package->db, szProductLanguage, 0 );
1640
    msiobj_release( &package->hdr );
Aric Stewart's avatar
Aric Stewart committed
1641 1642
    return langid;
}
1643

1644
UINT MSI_SetInstallLevel( MSIPACKAGE *package, int iInstallLevel )
1645 1646 1647
{
    static const WCHAR fmt[] = { '%','d',0 };
    WCHAR level[6];
1648
    int len;
1649 1650
    UINT r;

1651
    TRACE("%p %i\n", package, iInstallLevel);
1652

1653
    if (iInstallLevel > 32767)
1654 1655
        return ERROR_INVALID_PARAMETER;

1656 1657 1658
    if (iInstallLevel < 1)
        return MSI_SetFeatureStates( package );

1659 1660
    len = sprintfW( level, fmt, iInstallLevel );
    r = msi_set_property( package->db, szInstallLevel, level, len );
1661 1662 1663
    if ( r == ERROR_SUCCESS )
        r = MSI_SetFeatureStates( package );

1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
    return r;
}

/***********************************************************************
 * MsiSetInstallLevel (MSI.@)
 */
UINT WINAPI MsiSetInstallLevel(MSIHANDLE hInstall, int iInstallLevel)
{
    MSIPACKAGE* package;
    UINT r;

1675
    TRACE("%d %i\n", hInstall, iInstallLevel);
1676

1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
    {
        HRESULT hr;
        IWineMsiRemotePackage *remote_package;

        remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
        if (!remote_package)
            return ERROR_INVALID_HANDLE;

        hr = IWineMsiRemotePackage_SetInstallLevel(remote_package, iInstallLevel);

        IWineMsiRemotePackage_Release(remote_package);

        if (FAILED(hr))
        {
            if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
                return HRESULT_CODE(hr);

            return ERROR_FUNCTION_FAILED;
        }

        return ERROR_SUCCESS;
    }
1701 1702 1703

    r = MSI_SetInstallLevel( package, iInstallLevel );

1704
    msiobj_release( &package->hdr );
1705

1706 1707
    return r;
}
1708 1709 1710 1711 1712

/***********************************************************************
 * MsiGetFeatureValidStatesW (MSI.@)
 */
UINT WINAPI MsiGetFeatureValidStatesW(MSIHANDLE hInstall, LPCWSTR szFeature,
1713
                  LPDWORD pInstallState)
1714 1715
{
    if(pInstallState) *pInstallState = 1<<INSTALLSTATE_LOCAL;
1716
    FIXME("%d %s %p stub returning %d\n",
1717 1718 1719 1720 1721 1722 1723 1724 1725
        hInstall, debugstr_w(szFeature), pInstallState, pInstallState ? *pInstallState : 0);

    return ERROR_SUCCESS;
}

/***********************************************************************
 * MsiGetFeatureValidStatesA (MSI.@)
 */
UINT WINAPI MsiGetFeatureValidStatesA(MSIHANDLE hInstall, LPCSTR szFeature,
1726
                  LPDWORD pInstallState)
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
{
    UINT ret;
    LPWSTR szwFeature = strdupAtoW(szFeature);

    ret = MsiGetFeatureValidStatesW(hInstall, szwFeature, pInstallState);

    msi_free(szwFeature);

    return ret;
}