install.c 44.6 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
#include <stdarg.h>

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

35
#include "msipriv.h"
36
#include "winemsi.h"
37 38
#include "wine/heap.h"
#include "wine/debug.h"
39
#include "wine/exception.h"
40 41 42 43 44 45 46 47 48

WINE_DEFAULT_DEBUG_CHANNEL(msi);

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

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

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

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

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

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

    if (!szAction)
        return ERROR_INVALID_PARAMETER;
74

75 76
    package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
    if (!package)
77
    {
78
        MSIHANDLE remote;
79

80
        if (!(remote = msi_get_remote(hInstall)))
81 82
            return ERROR_INVALID_HANDLE;

83 84 85 86 87 88 89 90 91 92 93
        __TRY
        {
            ret = remote_DoAction(remote, szAction);
        }
        __EXCEPT(rpc_filter)
        {
            ret = GetExceptionCode();
        }
        __ENDTRY

        return ret;
94
    }
95
 
96
    ret = ACTION_PerformAction(package, szAction);
97
    msiobj_release( &package->hdr );
98 99 100 101

    return ret;
}

102 103 104
/***********************************************************************
 * MsiSequenceA       (MSI.@)
 */
105
UINT WINAPI MsiSequenceA( MSIHANDLE hInstall, LPCSTR szTable, INT iSequenceMode )
106
{
107 108 109
    LPWSTR szwTable;
    UINT ret;

110
    TRACE("%s, %d\n", debugstr_a(szTable), iSequenceMode);
111 112 113 114 115 116 117 118

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

    ret = MsiSequenceW( hInstall, szwTable, iSequenceMode );
    msi_free( szwTable );
    return ret;
119 120 121 122 123
}

/***********************************************************************
 * MsiSequenceW       (MSI.@)
 */
124
UINT WINAPI MsiSequenceW( MSIHANDLE hInstall, LPCWSTR szTable, INT iSequenceMode )
125
{
126 127 128
    MSIPACKAGE *package;
    UINT ret;

129
    TRACE("%s, %d\n", debugstr_w(szTable), iSequenceMode);
130

131 132 133
    if (!szTable)
        return ERROR_INVALID_PARAMETER;

134 135
    package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
    if (!package)
136
    {
137
        MSIHANDLE remote;
138

139
        if (!(remote = msi_get_remote(hInstall)))
140 141
            return ERROR_INVALID_HANDLE;

142 143 144 145 146 147 148 149 150 151 152
        __TRY
        {
            ret = remote_Sequence(remote, szTable, iSequenceMode);
        }
        __EXCEPT(rpc_filter)
        {
            ret = GetExceptionCode();
        }
        __ENDTRY

        return ret;
153
    }
154
    ret = MSI_Sequence( package, szTable );
155 156
    msiobj_release( &package->hdr );
    return ret;
157 158
}

159
UINT msi_strcpy_to_awstring( const WCHAR *str, int len, awstring *awbuf, DWORD *sz )
160
{
161
    UINT r = ERROR_SUCCESS;
162

163
    if (awbuf->str.w && !sz)
164 165
        return ERROR_INVALID_PARAMETER;
    if (!sz)
166 167
        return ERROR_SUCCESS;

168
    if (len < 0) len = lstrlenW( str );
169
 
170
    if (awbuf->unicode && awbuf->str.w)
171
    {
172
        memcpy( awbuf->str.w, str, min(len + 1, *sz) * sizeof(WCHAR) );
173 174 175
        if (*sz && len >= *sz)
            awbuf->str.w[*sz - 1] = 0;
    }
176 177
    else
    {
178 179 180 181
        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)
182
            awbuf->str.a[*sz - 1] = 0;
183
        len = lenA;
184
    }
185
    if (awbuf->str.w && len >= *sz)
186 187 188 189 190
        r = ERROR_MORE_DATA;
    *sz = len;
    return r;
}

191 192 193 194 195 196 197 198
UINT msi_strncpyWtoA(const WCHAR *str, int lenW, char *buf, DWORD *sz, BOOL remote)
{
    UINT r = ERROR_SUCCESS;
    DWORD lenA;

    if (!sz)
        return buf ? ERROR_INVALID_PARAMETER : ERROR_SUCCESS;

199
    if (lenW < 0) lenW = lstrlenW(str);
200 201 202 203 204 205 206 207 208 209 210 211 212 213
    lenA = WideCharToMultiByte(CP_ACP, 0, str, lenW + 1, NULL, 0, NULL, NULL);
    WideCharToMultiByte(CP_ACP, 0, str, lenW + 1, buf, *sz, NULL, NULL);
    lenA--;
    if (buf && lenA >= *sz)
    {
        if (*sz) buf[*sz - 1] = 0;
        r = ERROR_MORE_DATA;
    }
    if (remote && lenA >= *sz)
        lenA *= 2;
    *sz = lenA;
    return r;
}

214 215 216 217 218 219 220
UINT msi_strncpyW(const WCHAR *str, int len, WCHAR *buf, DWORD *sz)
{
    UINT r = ERROR_SUCCESS;

    if (!sz)
        return buf ? ERROR_INVALID_PARAMETER : ERROR_SUCCESS;

221
    if (len < 0) len = lstrlenW(str);
222 223 224 225 226 227 228 229 230 231 232
    if (buf)
        memcpy(buf, str, min(len + 1, *sz) * sizeof(WCHAR));
    if (buf && len >= *sz)
    {
        if (*sz) buf[*sz - 1] = 0;
        r = ERROR_MORE_DATA;
    }
    *sz = len;
    return r;
}

233 234
const WCHAR *msi_get_target_folder( MSIPACKAGE *package, const WCHAR *name )
{
235
    MSIFOLDER *folder = msi_get_loaded_folder( package, name );
236 237 238 239 240

    if (!folder) return NULL;
    if (!folder->ResolvedTarget)
    {
        MSIFOLDER *parent = folder;
241
        while (parent->Parent && wcscmp( parent->Parent, parent->Directory ))
242 243 244 245 246 247
        {
            parent = msi_get_loaded_folder( package, parent->Parent );
        }
        msi_resolve_target_folder( package, parent->Directory, TRUE );
    }
    return folder->ResolvedTarget;
248 249
}

250 251 252
/***********************************************************************
 * MsiGetTargetPathA        (MSI.@)
 */
253
UINT WINAPI MsiGetTargetPathA(MSIHANDLE hinst, const char *folder, char *buf, DWORD *sz)
254
{
255 256 257
    MSIPACKAGE *package;
    const WCHAR *path;
    WCHAR *folderW;
258
    UINT r;
259

260
    TRACE("%s %p %p\n", debugstr_a(folder), buf, sz);
261

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    if (!folder)
        return ERROR_INVALID_PARAMETER;

    if (!(folderW = strdupAtoW(folder)))
        return ERROR_OUTOFMEMORY;

    package = msihandle2msiinfo(hinst, MSIHANDLETYPE_PACKAGE);
    if (!package)
    {
        WCHAR *path = NULL;
        MSIHANDLE remote;

        if (!(remote = msi_get_remote(hinst)))
        {
            heap_free(folderW);
            return ERROR_INVALID_HANDLE;
        }
279

280 281 282 283 284 285 286 287 288 289
        __TRY
        {
            r = remote_GetTargetPath(remote, folderW, &path);
        }
        __EXCEPT(rpc_filter)
        {
            r = GetExceptionCode();
        }
        __ENDTRY

290 291
        if (!r)
            r = msi_strncpyWtoA(path, -1, buf, sz, TRUE);
292

293 294 295 296
        midl_user_free(path);
        heap_free(folderW);
        return r;
    }
297

298 299 300 301 302
    path = msi_get_target_folder(package, folderW);
    if (path)
        r = msi_strncpyWtoA(path, -1, buf, sz, FALSE);
    else
        r = ERROR_DIRECTORY;
303

304 305
    heap_free(folderW);
    msiobj_release(&package->hdr);
306
    return r;
307 308 309
}

/***********************************************************************
310 311
 * MsiGetTargetPathW        (MSI.@)
 */
312
UINT WINAPI MsiGetTargetPathW(MSIHANDLE hinst, const WCHAR *folder, WCHAR *buf, DWORD *sz)
313
{
314 315 316 317 318 319 320 321 322 323 324 325 326 327
    MSIPACKAGE *package;
    const WCHAR *path;
    UINT r;

    TRACE("%s %p %p\n", debugstr_w(folder), buf, sz);

    if (!folder)
        return ERROR_INVALID_PARAMETER;

    package = msihandle2msiinfo(hinst, MSIHANDLETYPE_PACKAGE);
    if (!package)
    {
        WCHAR *path = NULL;
        MSIHANDLE remote;
328

329 330 331
        if (!(remote = msi_get_remote(hinst)))
            return ERROR_INVALID_HANDLE;

332 333 334 335 336 337 338 339 340 341
        __TRY
        {
            r = remote_GetTargetPath(remote, folder, &path);
        }
        __EXCEPT(rpc_filter)
        {
            r = GetExceptionCode();
        }
        __ENDTRY

342 343
        if (!r)
            r = msi_strncpyW(path, -1, buf, sz);
344

345 346 347
        midl_user_free(path);
        return r;
    }
348

349 350 351 352 353 354 355 356
    path = msi_get_target_folder(package, folder);
    if (path)
        r = msi_strncpyW(path, -1, buf, sz);
    else
        r = ERROR_DIRECTORY;

    msiobj_release(&package->hdr);
    return r;
357 358
}

359
static WCHAR *get_source_root( MSIPACKAGE *package )
360
{
361 362
    msi_set_sourcedir_props( package, FALSE );
    return msi_dup_property( package->db, szSourceDir );
363 364 365 366 367 368 369 370 371
}

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));

372
    if (!wcscmp( name, szSourceDir )) name = szTargetDir;
373 374 375
    if (!(f = msi_get_loaded_folder( package, name ))) return NULL;

    /* special resolving for root dir */
376
    if (!wcscmp( name, szTargetDir ) && !f->ResolvedSource)
377
    {
378
        f->ResolvedSource = get_source_root( package );
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
    }
    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)
394
        path = get_source_root( package );
395 396 397 398 399 400 401 402 403 404 405 406
    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;
}

407
/***********************************************************************
408 409
 * MsiGetSourcePathA     (MSI.@)
 */
410
UINT WINAPI MsiGetSourcePathA(MSIHANDLE hinst, const char *folder, char *buf, DWORD *sz)
411
{
412
    MSIPACKAGE *package;
413
    WCHAR *path, *folderW;
414
    UINT r;
415

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    TRACE("%s %p %p\n", debugstr_a(folder), buf, sz);

    if (!folder)
        return ERROR_INVALID_PARAMETER;

    if (!(folderW = strdupAtoW(folder)))
        return ERROR_OUTOFMEMORY;

    package = msihandle2msiinfo(hinst, MSIHANDLETYPE_PACKAGE);
    if (!package)
    {
        WCHAR *path = NULL;
        MSIHANDLE remote;

        if (!(remote = msi_get_remote(hinst)))
        {
            heap_free(folderW);
            return ERROR_INVALID_HANDLE;
        }
435

436 437 438 439 440 441 442 443 444 445
        __TRY
        {
            r = remote_GetSourcePath(remote, folderW, &path);
        }
        __EXCEPT(rpc_filter)
        {
            r = GetExceptionCode();
        }
        __ENDTRY

446 447 448 449 450 451 452
        if (!r)
            r = msi_strncpyWtoA(path, -1, buf, sz, TRUE);

        midl_user_free(path);
        heap_free(folderW);
        return r;
    }
453

454 455 456 457 458
    path = msi_resolve_source_folder(package, folderW, NULL);
    if (path)
        r = msi_strncpyWtoA(path, -1, buf, sz, FALSE);
    else
        r = ERROR_DIRECTORY;
459

460
    heap_free(path);
461 462
    heap_free(folderW);
    msiobj_release(&package->hdr);
463
    return r;
464 465
}

466 467 468
/***********************************************************************
 * MsiGetSourcePathW     (MSI.@)
 */
469
UINT WINAPI MsiGetSourcePathW(MSIHANDLE hinst, const WCHAR *folder, WCHAR *buf, DWORD *sz)
470
{
471
    MSIPACKAGE *package;
472
    WCHAR *path;
473 474 475 476 477 478 479 480 481 482 483 484
    UINT r;

    TRACE("%s %p %p\n", debugstr_w(folder), buf, sz);

    if (!folder)
        return ERROR_INVALID_PARAMETER;

    package = msihandle2msiinfo(hinst, MSIHANDLETYPE_PACKAGE);
    if (!package)
    {
        WCHAR *path = NULL;
        MSIHANDLE remote;
485

486 487 488
        if (!(remote = msi_get_remote(hinst)))
            return ERROR_INVALID_HANDLE;

489 490 491 492 493 494 495 496 497 498
        __TRY
        {
            r = remote_GetSourcePath(remote, folder, &path);
        }
        __EXCEPT(rpc_filter)
        {
            r = GetExceptionCode();
        }
        __ENDTRY

499 500
        if (!r)
            r = msi_strncpyW(path, -1, buf, sz);
501

502 503 504
        midl_user_free(path);
        return r;
    }
505

506 507 508 509 510 511
    path = msi_resolve_source_folder(package, folder, NULL);
    if (path)
        r = msi_strncpyW(path, -1, buf, sz);
    else
        r = ERROR_DIRECTORY;

512
    heap_free(path);
513 514
    msiobj_release(&package->hdr);
    return r;
515
}
516 517 518 519

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

526 527
    if ( !szFolder || !szFolderPath )
        return ERROR_INVALID_PARAMETER;
528 529 530

    szwFolder = strdupAtoW(szFolder);
    szwFolderPath = strdupAtoW(szFolderPath);
531 532
    if (!szwFolder || !szwFolderPath)
        goto end;
533

534
    rc = MsiSetTargetPathW( hInstall, szwFolder, szwFolderPath );
535

536
end:
537 538
    msi_free(szwFolder);
    msi_free(szwFolderPath);
539 540 541 542

    return rc;
}

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

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

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

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

571
    TRACE("%p %s %s\n", package, debugstr_w(szFolder), debugstr_w(szFolderPath));
572

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

582
    set_target_path( package, folder, szFolderPath );
583

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

589
        if (!comp->Enabled || msi_is_global_assembly( comp )) continue;
590

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

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

607
    TRACE("%s %s\n",debugstr_w(szFolder),debugstr_w(szFolderPath));
608

609 610 611
    if ( !szFolder || !szFolderPath )
        return ERROR_INVALID_PARAMETER;

612
    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
613
    if (!package)
614
    {
615
        MSIHANDLE remote;
616

617
        if (!(remote = msi_get_remote(hInstall)))
618 619
            return ERROR_INVALID_HANDLE;

620 621 622 623 624 625 626 627 628 629 630
        __TRY
        {
            ret = remote_SetTargetPath(remote, szFolder, szFolderPath);
        }
        __EXCEPT(rpc_filter)
        {
            ret = GetExceptionCode();
        }
        __ENDTRY

        return ret;
631
    }
632

633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
    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)
{
673
    MSIPACKAGE *package;
674 675
    BOOL r = FALSE;

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

678 679
    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
680
    {
681
        MSIHANDLE remote;
682

683
        if (!(remote = msi_get_remote(hInstall)))
684 685
            return FALSE;

686 687 688 689 690 691 692 693 694 695 696
        __TRY
        {
            r = remote_GetMode(remote, iRunMode);
        }
        __EXCEPT(rpc_filter)
        {
            r = FALSE;
        }
        __ENDTRY

        return r;
697
    }
698

699 700
    switch (iRunMode)
    {
701 702
    case MSIRUNMODE_ADMIN:
        FIXME("no support for administrative installs\n");
703 704 705 706
        break;

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

709 710 711 712 713
    case MSIRUNMODE_WINDOWS9X:
        if (GetVersion() & 0x80000000)
            r = TRUE;
        break;

714
    case MSIRUNMODE_OPERATIONS:
715 716 717 718 719 720
    case MSIRUNMODE_RESERVED11:
    case MSIRUNMODE_RESERVED14:
    case MSIRUNMODE_RESERVED15:
        break;

    case MSIRUNMODE_SCHEDULED:
721
        r = package->scheduled_action_running;
722 723
        break;

724
    case MSIRUNMODE_ROLLBACK:
725
        r = package->rollback_action_running;
726 727
        break;

728
    case MSIRUNMODE_COMMIT:
729
        r = package->commit_action_running;
730 731
        break;

732
    case MSIRUNMODE_MAINTENANCE:
733
        r = msi_get_property_int( package->db, szInstalled, 0 ) != 0;
734 735
        break;

736 737 738 739
    case MSIRUNMODE_ROLLBACKENABLED:
        r = msi_get_property_int( package->db, szRollbackDisabled, 0 ) == 0;
        break;

740
    case MSIRUNMODE_REBOOTATEND:
741
        r = package->need_reboot_at_end;
742 743
        break;

744 745 746 747
    case MSIRUNMODE_REBOOTNOW:
        r = package->need_reboot_now;
        break;

748 749 750 751
    case MSIRUNMODE_LOGENABLED:
        r = (package->log_file != INVALID_HANDLE_VALUE);
        break;

752
    default:
753
        FIXME("unimplemented run mode: %d\n", iRunMode);
754 755 756
        r = TRUE;
    }

757
    msiobj_release( &package->hdr );
758
    return r;
759 760
}

761 762 763
/***********************************************************************
 *           MsiSetMode    (MSI.@)
 */
764
UINT WINAPI MsiSetMode(MSIHANDLE hInstall, MSIRUNMODE iRunMode, BOOL fState)
765
{
766 767 768 769 770 771 772 773
    MSIPACKAGE *package;
    UINT r;

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

    package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
    if (!package)
    {
774
        MSIHANDLE remote;
775

776
        if (!(remote = msi_get_remote(hInstall)))
777 778
            return FALSE;

779 780 781 782 783 784 785 786 787 788 789
        __TRY
        {
            r = remote_SetMode(remote, iRunMode, fState);
        }
        __EXCEPT(rpc_filter)
        {
            r = GetExceptionCode();
        }
        __ENDTRY

        return r;
790 791 792 793 794
    }

    switch (iRunMode)
    {
    case MSIRUNMODE_REBOOTATEND:
795
        package->need_reboot_at_end = (fState != 0);
796 797 798 799
        r = ERROR_SUCCESS;
        break;

    case MSIRUNMODE_REBOOTNOW:
800 801
        package->need_reboot_now = (fState != 0);
        r = ERROR_SUCCESS;
802 803 804 805 806 807
        break;

    default:
        r = ERROR_ACCESS_DENIED;
    }

808
    msiobj_release( &package->hdr );
809
    return r;
810 811
}

812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
/***********************************************************************
 * 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);

    rc = MsiSetFeatureStateW(hInstall,szwFeature, iState); 

828
    msi_free(szwFeature);
829 830 831 832

    return rc;
}

833 834 835 836 837
/* update component state based on a feature change */
void ACTION_UpdateComponentStates( MSIPACKAGE *package, MSIFEATURE *feature )
{
    INSTALLSTATE newstate;
    ComponentList *cl;
838

839 840
    newstate = feature->ActionRequest;
    if (newstate == INSTALLSTATE_ABSENT) newstate = INSTALLSTATE_UNKNOWN;
841

842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 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
    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);
    }
}

916
UINT MSI_SetFeatureStateW( MSIPACKAGE *package, LPCWSTR szFeature, INSTALLSTATE iState )
917 918
{
    UINT rc = ERROR_SUCCESS;
919
    MSIFEATURE *feature, *child;
920

921
    TRACE("%s %i\n", debugstr_w(szFeature), iState);
922

923
    feature = msi_get_loaded_feature( package, szFeature );
924
    if (!feature)
925 926 927
        return ERROR_UNKNOWN_FEATURE;

    if (iState == INSTALLSTATE_ADVERTISED && 
928
        feature->Attributes & msidbFeatureAttributesDisallowAdvertise)
929 930
        return ERROR_FUNCTION_FAILED;

931
    feature->ActionRequest = iState;
932

933
    ACTION_UpdateComponentStates( package, feature );
934 935

    /* update all the features that are children of this feature */
936
    LIST_FOR_EACH_ENTRY( child, &package->features, MSIFEATURE, entry )
937
    {
938
        if (child->Feature_Parent && !wcscmp( szFeature, child->Feature_Parent ))
939
            MSI_SetFeatureStateW(package, child->Feature, iState);
940 941 942 943 944 945 946 947 948 949 950 951 952 953
    }
    
    return rc;
}

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

954
    TRACE("%s %i\n",debugstr_w(szFeature), iState);
955

956 957 958
    if (!szFeature)
        return ERROR_UNKNOWN_FEATURE;

959 960
    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
961
    {
962
        MSIHANDLE remote;
963

964
        if (!(remote = msi_get_remote(hInstall)))
965 966
            return ERROR_INVALID_HANDLE;

967 968 969 970 971 972 973 974 975 976 977
        __TRY
        {
            rc = remote_SetFeatureState(remote, szFeature, iState);
        }
        __EXCEPT(rpc_filter)
        {
            rc = GetExceptionCode();
        }
        __ENDTRY

        return rc;
978
    }
979 980 981 982 983 984 985

    rc = MSI_SetFeatureStateW(package,szFeature,iState);

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

986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
/***********************************************************************
* 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 );
1033
    if (!costing || !wcscmp( costing, szOne ))
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
    {
        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;
}

1050 1051 1052
/***********************************************************************
* MsiGetFeatureStateA   (MSI.@)
*/
1053
UINT WINAPI MsiGetFeatureStateA(MSIHANDLE hInstall, LPCSTR szFeature,
1054 1055 1056 1057 1058
                  INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
{
    LPWSTR szwFeature = NULL;
    UINT rc;
    
1059
    if (szFeature && !(szwFeature = strdupAtoW(szFeature))) return ERROR_OUTOFMEMORY;
1060

1061
    rc = MsiGetFeatureStateW(hInstall, szwFeature, piInstalled, piAction);
1062
    msi_free( szwFeature);
1063 1064 1065
    return rc;
}

1066
UINT MSI_GetFeatureStateW(MSIPACKAGE *package, LPCWSTR szFeature,
1067 1068
                  INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
{
1069
    MSIFEATURE *feature;
1070

1071
    feature = msi_get_loaded_feature(package,szFeature);
1072
    if (!feature)
1073 1074 1075
        return ERROR_UNKNOWN_FEATURE;

    if (piInstalled)
1076
        *piInstalled = feature->Installed;
1077 1078

    if (piAction)
1079
        *piAction = feature->ActionRequest;
1080

1081
    TRACE("returning %i %i\n", feature->Installed, feature->ActionRequest);
1082 1083 1084 1085 1086 1087 1088

    return ERROR_SUCCESS;
}

/***********************************************************************
* MsiGetFeatureStateW   (MSI.@)
*/
1089
UINT WINAPI MsiGetFeatureStateW(MSIHANDLE hInstall, LPCWSTR szFeature,
1090 1091 1092 1093 1094
                  INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
{
    MSIPACKAGE* package;
    UINT ret;

1095
    TRACE("%d %s %p %p\n", hInstall, debugstr_w(szFeature), piInstalled, piAction);
1096

1097 1098 1099
    if (!szFeature)
        return ERROR_UNKNOWN_FEATURE;

1100 1101
    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
1102
    {
1103
        MSIHANDLE remote;
1104

1105
        if (!(remote = msi_get_remote(hInstall)))
1106 1107
            return ERROR_INVALID_HANDLE;

1108 1109 1110 1111 1112 1113 1114 1115 1116
        __TRY
        {
            ret = remote_GetFeatureState(remote, szFeature, piInstalled, piAction);
        }
        __EXCEPT(rpc_filter)
        {
            ret = GetExceptionCode();
        }
        __ENDTRY
James Hawkins's avatar
James Hawkins committed
1117

1118
        return ret;
1119 1120
    }

1121 1122 1123 1124 1125
    ret = MSI_GetFeatureStateW(package, szFeature, piInstalled, piAction);
    msiobj_release( &package->hdr );
    return ret;
}

1126 1127 1128 1129
/***********************************************************************
* MsiGetFeatureCostA   (MSI.@)
*/
UINT WINAPI MsiGetFeatureCostA(MSIHANDLE hInstall, LPCSTR szFeature,
1130
                  MSICOSTTREE iCostTree, INSTALLSTATE iState, LPINT piCost)
1131
{
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
    LPWSTR szwFeature = NULL;
    UINT rc;

    szwFeature = strdupAtoW(szFeature);

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

    msi_free(szwFeature);

    return rc;
}

1144
static INT feature_cost( MSIFEATURE *feature )
1145
{
1146
    INT cost = 0;
1147
    ComponentList *cl;
1148

1149
    LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
1150
    {
1151
        cost += cl->component->Cost;
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
    }
    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 (;;)
        {
1180
            MSIFEATURE *parent = msi_get_loaded_feature( package, feature_parent );
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
            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;
1202 1203 1204 1205 1206 1207 1208
    return ERROR_SUCCESS;
}

/***********************************************************************
* MsiGetFeatureCostW   (MSI.@)
*/
UINT WINAPI MsiGetFeatureCostW(MSIHANDLE hInstall, LPCWSTR szFeature,
1209
                  MSICOSTTREE iCostTree, INSTALLSTATE iState, LPINT piCost)
1210
{
1211 1212 1213 1214 1215
    MSIPACKAGE *package;
    MSIFEATURE *feature;
    UINT ret;

    TRACE("(%d %s %i %i %p)\n", hInstall, debugstr_w(szFeature),
1216
          iCostTree, iState, piCost);
1217

1218 1219 1220
    if (!szFeature)
        return ERROR_INVALID_PARAMETER;

1221 1222 1223
    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
    {
1224
        MSIHANDLE remote;
1225

1226
        if (!(remote = msi_get_remote(hInstall)))
1227 1228
            return ERROR_INVALID_HANDLE;

1229 1230 1231 1232 1233 1234 1235 1236 1237
        __TRY
        {
            ret = remote_GetFeatureCost(remote, szFeature, iCostTree, iState, piCost);
        }
        __EXCEPT(rpc_filter)
        {
            ret = GetExceptionCode();
        }
        __ENDTRY
1238

1239
        return ret;
1240 1241
    }

1242 1243 1244 1245 1246 1247
    if (!piCost)
    {
        msiobj_release( &package->hdr );
        return ERROR_INVALID_PARAMETER;
    }

1248
    feature = msi_get_loaded_feature(package, szFeature);
1249 1250 1251 1252 1253 1254 1255 1256

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

    msiobj_release( &package->hdr );
    return ret;
1257 1258
}

1259
/***********************************************************************
1260 1261 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 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
* 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)
    {
1320
        if (feature->Title) len = lstrlenW( feature->Title );
1321 1322 1323 1324 1325 1326 1327 1328
        else len = 0;
        if (*title_len <= len)
        {
            *title_len = len;
            if (title) r = ERROR_MORE_DATA;
        }
        else if (title)
        {
1329
            if (feature->Title) lstrcpyW( title, feature->Title );
1330 1331 1332 1333 1334 1335
            else *title = 0;
            *title_len = len;
        }
    }
    if (help_len)
    {
1336
        if (feature->Description) len = lstrlenW( feature->Description );
1337 1338 1339 1340 1341 1342 1343 1344
        else len = 0;
        if (*help_len <= len)
        {
            *help_len = len;
            if (help) r = ERROR_MORE_DATA;
        }
        else if (help)
        {
1345
            if (feature->Description) lstrcpyW( help, feature->Description );
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
            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;
}

/***********************************************************************
1380 1381 1382 1383 1384
 * MsiSetComponentStateA (MSI.@)
 */
UINT WINAPI MsiSetComponentStateA(MSIHANDLE hInstall, LPCSTR szComponent,
                                  INSTALLSTATE iState)
{
1385 1386 1387 1388 1389
    UINT rc;
    LPWSTR szwComponent = strdupAtoW(szComponent);

    rc = MsiSetComponentStateW(hInstall, szwComponent, iState);

1390
    msi_free(szwComponent);
1391 1392

    return rc;
1393 1394
}

1395 1396 1397
/***********************************************************************
 * MsiGetComponentStateA (MSI.@)
 */
1398
UINT WINAPI MsiGetComponentStateA(MSIHANDLE hInstall, LPCSTR szComponent,
1399 1400 1401 1402 1403 1404 1405 1406 1407
                  INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
{
    LPWSTR szwComponent= NULL;
    UINT rc;
    
    szwComponent= strdupAtoW(szComponent);

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

1408
    msi_free( szwComponent);
1409 1410 1411 1412

    return rc;
}

1413 1414 1415 1416 1417 1418 1419
static UINT MSI_SetComponentStateW(MSIPACKAGE *package, LPCWSTR szComponent,
                                   INSTALLSTATE iState)
{
    MSICOMPONENT *comp;

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

1420
    comp = msi_get_loaded_component(package, szComponent);
1421 1422 1423
    if (!comp)
        return ERROR_UNKNOWN_COMPONENT;

1424
    if (comp->Enabled)
1425
        comp->Action = iState;
1426 1427 1428 1429

    return ERROR_SUCCESS;
}

1430
UINT MSI_GetComponentStateW(MSIPACKAGE *package, LPCWSTR szComponent,
1431 1432
                  INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
{
1433
    MSICOMPONENT *comp;
1434

1435 1436
    TRACE("%p %s %p %p\n", package, debugstr_w(szComponent),
           piInstalled, piAction);
1437

1438
    comp = msi_get_loaded_component(package,szComponent);
1439
    if (!comp)
1440 1441 1442
        return ERROR_UNKNOWN_COMPONENT;

    if (piInstalled)
1443 1444 1445 1446 1447 1448
    {
        if (comp->Enabled)
            *piInstalled = comp->Installed;
        else
            *piInstalled = INSTALLSTATE_UNKNOWN;
    }
1449 1450

    if (piAction)
1451 1452 1453 1454 1455 1456
    {
        if (comp->Enabled)
            *piAction = comp->Action;
        else
            *piAction = INSTALLSTATE_UNKNOWN;
    }
1457

1458
    TRACE("states (%i, %i)\n", comp->Installed, comp->Action );
1459 1460 1461
    return ERROR_SUCCESS;
}

1462 1463 1464 1465 1466 1467
/***********************************************************************
 * MsiSetComponentStateW (MSI.@)
 */
UINT WINAPI MsiSetComponentStateW(MSIHANDLE hInstall, LPCWSTR szComponent,
                                  INSTALLSTATE iState)
{
1468 1469 1470
    MSIPACKAGE* package;
    UINT ret;

1471 1472 1473
    if (!szComponent)
        return ERROR_UNKNOWN_COMPONENT;

1474 1475
    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
1476
    {
1477
        MSIHANDLE remote;
1478

1479
        if (!(remote = msi_get_remote(hInstall)))
1480 1481
            return ERROR_INVALID_HANDLE;

1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
        __TRY
        {
            ret = remote_SetComponentState(remote, szComponent, iState);
        }
        __EXCEPT(rpc_filter)
        {
            ret = GetExceptionCode();
        }
        __ENDTRY

        return ret;
1493 1494
    }

1495 1496 1497
    ret = MSI_SetComponentStateW(package, szComponent, iState);
    msiobj_release(&package->hdr);
    return ret;
1498 1499
}

1500 1501 1502
/***********************************************************************
 * MsiGetComponentStateW (MSI.@)
 */
1503
UINT WINAPI MsiGetComponentStateW(MSIHANDLE hInstall, LPCWSTR szComponent,
1504 1505 1506 1507 1508
                  INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
{
    MSIPACKAGE* package;
    UINT ret;

1509
    TRACE("%d %s %p %p\n", hInstall, debugstr_w(szComponent),
1510 1511
           piInstalled, piAction);

1512 1513 1514
    if (!szComponent)
        return ERROR_UNKNOWN_COMPONENT;

1515 1516
    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
1517
    {
1518
        MSIHANDLE remote;
1519

1520
        if (!(remote = msi_get_remote(hInstall)))
1521 1522
            return ERROR_INVALID_HANDLE;

1523 1524 1525 1526 1527 1528 1529 1530 1531
        __TRY
        {
            ret = remote_GetComponentState(remote, szComponent, piInstalled, piAction);
        }
        __EXCEPT(rpc_filter)
        {
            ret = GetExceptionCode();
        }
        __ENDTRY
1532

1533
        return ret;
1534 1535
    }

1536 1537 1538 1539
    ret = MSI_GetComponentStateW( package, szComponent, piInstalled, piAction);
    msiobj_release( &package->hdr );
    return ret;
}
Aric Stewart's avatar
Aric Stewart committed
1540 1541 1542 1543 1544 1545 1546 1547

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

Aric Stewart's avatar
Aric Stewart committed
1549 1550
    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
1551
    {
1552
        MSIHANDLE remote;
1553

1554
        if (!(remote = msi_get_remote(hInstall)))
1555 1556
            return ERROR_INVALID_HANDLE;

1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
        __TRY
        {
            langid = remote_GetLanguage(remote);
        }
        __EXCEPT(rpc_filter)
        {
            langid = 0;
        }
        __ENDTRY

        return langid;
1568
    }
Aric Stewart's avatar
Aric Stewart committed
1569

1570
    langid = msi_get_property_int( package->db, szProductLanguage, 0 );
1571
    msiobj_release( &package->hdr );
Aric Stewart's avatar
Aric Stewart committed
1572 1573
    return langid;
}
1574

1575
UINT MSI_SetInstallLevel( MSIPACKAGE *package, int iInstallLevel )
1576 1577 1578
{
    static const WCHAR fmt[] = { '%','d',0 };
    WCHAR level[6];
1579
    int len;
1580 1581
    UINT r;

1582
    TRACE("%p %i\n", package, iInstallLevel);
1583

1584
    if (iInstallLevel > 32767)
1585 1586
        return ERROR_INVALID_PARAMETER;

1587 1588 1589
    if (iInstallLevel < 1)
        return MSI_SetFeatureStates( package );

1590
    len = swprintf( level, ARRAY_SIZE(level), fmt, iInstallLevel );
1591
    r = msi_set_property( package->db, szInstallLevel, level, len );
1592 1593 1594
    if ( r == ERROR_SUCCESS )
        r = MSI_SetFeatureStates( package );

1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
    return r;
}

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

1606
    TRACE("%d %i\n", hInstall, iInstallLevel);
1607

1608 1609 1610
    package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
    if (!package)
    {
1611
        MSIHANDLE remote;
1612

1613
        if (!(remote = msi_get_remote(hInstall)))
1614 1615
            return ERROR_INVALID_HANDLE;

1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
        __TRY
        {
            r = remote_SetInstallLevel(remote, iInstallLevel);
        }
        __EXCEPT(rpc_filter)
        {
            r = GetExceptionCode();
        }
        __ENDTRY

        return r;
1627
    }
1628 1629 1630

    r = MSI_SetInstallLevel( package, iInstallLevel );

1631
    msiobj_release( &package->hdr );
1632

1633 1634
    return r;
}
1635 1636 1637 1638 1639

/***********************************************************************
 * MsiGetFeatureValidStatesW (MSI.@)
 */
UINT WINAPI MsiGetFeatureValidStatesW(MSIHANDLE hInstall, LPCWSTR szFeature,
1640
                  LPDWORD pInstallState)
1641 1642
{
    if(pInstallState) *pInstallState = 1<<INSTALLSTATE_LOCAL;
1643
    FIXME("%d %s %p stub returning %d\n",
1644 1645 1646 1647 1648 1649 1650 1651 1652
        hInstall, debugstr_w(szFeature), pInstallState, pInstallState ? *pInstallState : 0);

    return ERROR_SUCCESS;
}

/***********************************************************************
 * MsiGetFeatureValidStatesA (MSI.@)
 */
UINT WINAPI MsiGetFeatureValidStatesA(MSIHANDLE hInstall, LPCSTR szFeature,
1653
                  LPDWORD pInstallState)
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
{
    UINT ret;
    LPWSTR szwFeature = strdupAtoW(szFeature);

    ret = MsiGetFeatureValidStatesW(hInstall, szwFeature, pInstallState);

    msi_free(szwFeature);

    return ret;
}