helpers.c 28.4 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
 */

/*
Austin English's avatar
Austin English committed
22
 * Here are helper functions formally in action.c that are used by a variety of
23 24 25 26 27 28 29 30 31 32
 * actions and functions.
 */

#include <stdarg.h>

#include "windef.h"
#include "wine/debug.h"
#include "msipriv.h"
#include "winuser.h"
#include "wine/unicode.h"
33
#include "msidefs.h"
34 35 36 37 38 39

WINE_DEFAULT_DEBUG_CHANNEL(msi);

static const WCHAR cszTargetDir[] = {'T','A','R','G','E','T','D','I','R',0};
static const WCHAR cszDatabase[]={'D','A','T','A','B','A','S','E',0};

40
LPWSTR build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name )
41
{
42
    LPWSTR SystemFolder, dest, FilePath;
43 44 45 46 47 48 49

    static const WCHAR szInstaller[] = 
        {'M','i','c','r','o','s','o','f','t','\\',
         'I','n','s','t','a','l','l','e','r','\\',0};
    static const WCHAR szFolder[] =
        {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};

50
    SystemFolder = msi_dup_property( package, szFolder );
51

52
    dest = build_directory_name(3, SystemFolder, szInstaller, package->ProductCode);
53 54 55

    create_full_pathW(dest);

56
    FilePath = build_directory_name(2, dest, icon_name);
57

58 59
    msi_free(SystemFolder);
    msi_free(dest);
60
    return FilePath;
61 62
}

63
LPWSTR msi_dup_record_field( MSIRECORD *rec, INT field )
64
{
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    DWORD sz = 0;
    LPWSTR str;
    UINT r;

    if (MSI_RecordIsNull( rec, field ))
        return NULL;

    r = MSI_RecordGetStringW( rec, field, NULL, &sz );
    if (r != ERROR_SUCCESS)
        return NULL;

    sz ++;
    str = msi_alloc( sz * sizeof (WCHAR) );
    if (!str)
        return str;
    str[0] = 0;
    r = MSI_RecordGetStringW( rec, field, str, &sz );
    if (r != ERROR_SUCCESS)
    {
        ERR("failed to get string!\n");
        msi_free( str );
        return NULL;
    }
    return str;
89 90
}

91
MSICOMPONENT* get_loaded_component( MSIPACKAGE* package, LPCWSTR Component )
92
{
93
    MSICOMPONENT *comp;
94

95
    LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
96
    {
97 98
        if (lstrcmpW(Component,comp->Component)==0)
            return comp;
99
    }
100
    return NULL;
101 102
}

103
MSIFEATURE* get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
104
{
105
    MSIFEATURE *feature;
106

107
    LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
108
    {
109 110
        if (lstrcmpW( Feature, feature->Feature )==0)
            return feature;
111
    }
112
    return NULL;
113 114
}

115
MSIFILE* get_loaded_file( MSIPACKAGE* package, LPCWSTR key )
116
{
117
    MSIFILE *file;
118

119
    LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
120
    {
121 122
        if (lstrcmpW( key, file->File )==0)
            return file;
123
    }
124
    return NULL;
125 126
}

127
int track_tempfile( MSIPACKAGE *package, LPCWSTR path )
128
{
129
    MSITEMPFILE *temp;
130

131 132
    TRACE("%s\n", debugstr_w(path));

133
    LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
134 135
        if (!lstrcmpW( path, temp->Path ))
            return 0;
136

137
    temp = msi_alloc_zero( sizeof (MSITEMPFILE) );
138
    if (!temp)
139
        return -1;
140

141 142
    list_add_head( &package->tempfiles, &temp->entry );
    temp->Path = strdupW( path );
143 144 145 146

    return 0;
}

147 148 149
MSIFOLDER *get_loaded_folder( MSIPACKAGE *package, LPCWSTR dir )
{
    MSIFOLDER *folder;
150

151 152 153 154 155 156 157 158
    LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
    {
        if (lstrcmpW( dir, folder->Directory )==0)
            return folder;
    }
    return NULL;
}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
void msi_reset_folders( MSIPACKAGE *package, BOOL source )
{
    MSIFOLDER *folder;

    LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
    {
        if ( source )
        {
            msi_free( folder->ResolvedSource );
            folder->ResolvedSource = NULL;
        }
        else
        {
            msi_free( folder->ResolvedTarget );
            folder->ResolvedTarget = NULL;
        }
    }
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
static LPWSTR get_source_root( MSIPACKAGE *package )
{
    LPWSTR path, p;

    path = msi_dup_property( package, cszSourceDir );
    if (path)
        return path;

    path = msi_dup_property( package, cszDatabase );
    if (path)
    {
        p = strrchrW(path,'\\');
        if (p)
            *(p+1) = 0;
    }
    return path;
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
/*
 * clean_spaces_from_path()
 *
 * removes spaces from the beginning and end of path segments
 * removes multiple \\ characters
 */
static void clean_spaces_from_path( LPWSTR p )
{
    LPWSTR q = p;
    int n, len = 0;

    while (1)
    {
        /* copy until the end of the string or a space */
        while (*p != ' ' && (*q = *p))
        {
            p++, len++;
            /* reduce many backslashes to one */
            if (*p != '\\' || *q != '\\')
                q++;
        }

        /* quit at the end of the string */
        if (!*p)
            break;

        /* count the number of spaces */
        n = 0;
        while (p[n] == ' ')
            n++;

        /* if it's leading or trailing space, skip it */
        if ( len == 0 || p[-1] == '\\' || p[n] == '\\' )
            p += n;
        else  /* copy n spaces */
            while (n && (*q++ = *p++)) n--;
    }
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
LPWSTR resolve_file_source(MSIPACKAGE *package, MSIFILE *file)
{
    LPWSTR p, path;

    TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));

    if (file->IsCompressed)
        return NULL;

    p = resolve_folder(package, file->Component->Directory,
                       TRUE, FALSE, TRUE, NULL);
    path = build_directory_name(2, p, file->ShortName);

    if (file->LongName &&
        GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
    {
        msi_free(path);
        path = build_directory_name(2, p, file->LongName);
    }

    msi_free(p);

    TRACE("file %s source resolves to %s\n", debugstr_w(file->File),
          debugstr_w(path));

    return path;
}

263
LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source, 
264
                      BOOL set_prop, BOOL load_prop, MSIFOLDER **folder)
265
{
266
    MSIFOLDER *f;
267
    LPWSTR p, path = NULL, parent;
268 269 270 271 272 273

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

    if (!name)
        return NULL;

274 275 276
    if (!lstrcmpW(name,cszSourceDir))
        name = cszTargetDir;

277 278 279 280
    f = get_loaded_folder( package, name );
    if (!f)
        return NULL;

281
    /* special resolving for Target and Source root dir */
282
    if (!strcmpW(name,cszTargetDir))
283
    {
284
        if (!f->ResolvedTarget && !f->Property)
285
        {
286 287 288
            LPWSTR check_path;
            check_path = msi_dup_property( package, cszTargetDir );
            if (!check_path)
289
            {
290 291 292
                check_path = msi_dup_property( package, cszRootDrive );
                if (set_prop)
                    MSI_SetPropertyW(package,cszTargetDir,check_path);
293
            }
294

295 296
            /* correct misbuilt target dir */
            path = build_directory_name(2, check_path, NULL);
297
            clean_spaces_from_path( path );
298 299 300
            if (strcmpiW(path,check_path)!=0)
                MSI_SetPropertyW(package,cszTargetDir,path);
            msi_free(check_path);
301 302

            f->ResolvedTarget = path;
303 304
        }

305 306 307
        if (!f->ResolvedSource)
            f->ResolvedSource = get_source_root( package );
    }
308 309

    if (folder)
310
        *folder = f;
311

312
    if (!source && f->ResolvedTarget)
313
    {
314
        path = strdupW( f->ResolvedTarget );
315 316 317
        TRACE("   already resolved to %s\n",debugstr_w(path));
        return path;
    }
318 319

    if (source && f->ResolvedSource)
320 321 322 323 324
    {
        path = strdupW( f->ResolvedSource );
        TRACE("   (source)already resolved to %s\n",debugstr_w(path));
        return path;
    }
325 326

    if (!source && f->Property)
327
    {
328
        path = build_directory_name( 2, f->Property, NULL );
329

330 331
        TRACE("   internally set to %s\n",debugstr_w(path));
        if (set_prop)
332
            MSI_SetPropertyW( package, name, path );
333 334 335
        return path;
    }

336 337 338 339 340 341 342
    if (!source && load_prop && (path = msi_dup_property( package, name )))
    {
        f->ResolvedTarget = strdupW( path );
        TRACE("   property set to %s\n", debugstr_w(path));
        return path;
    }

343 344
    if (!f->Parent)
        return path;
345

346
    parent = f->Parent;
347

348
    TRACE(" ! Parent is %s\n", debugstr_w(parent));
349

350
    p = resolve_folder(package, parent, source, set_prop, load_prop, NULL);
351 352 353
    if (!source)
    {
        TRACE("   TargetDefault = %s\n", debugstr_w(f->TargetDefault));
354

355 356 357 358 359 360 361 362 363 364 365
        path = build_directory_name( 3, p, f->TargetDefault, NULL );
        clean_spaces_from_path( path );
        f->ResolvedTarget = strdupW( path );
        TRACE("target -> %s\n", debugstr_w(path));
        if (set_prop)
            MSI_SetPropertyW(package,name,path);
    }
    else
    {
        path = NULL;

366 367
        if (package->WordCount & msidbSumInfoSourceTypeCompressed)
            path = get_source_root( package );
368 369
        else if (package->WordCount & msidbSumInfoSourceTypeSFN)
            path = build_directory_name( 3, p, f->SourceShortPath, NULL );
370
        else
371
            path = build_directory_name( 3, p, f->SourceLongPath, NULL );
372

373 374
        TRACE("source -> %s\n", debugstr_w(path));
        f->ResolvedSource = strdupW( path );
375
    }
376 377
    msi_free(p);

378 379 380 381 382 383 384 385 386 387 388 389 390
    return path;
}

/* wrapper to resist a need for a full rewrite right now */
DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
{
    if (ptr)
    {
        MSIRECORD *rec = MSI_CreateRecord(1);
        DWORD size = 0;

        MSI_RecordSetStringW(rec,0,ptr);
        MSI_FormatRecordW(package,rec,NULL,&size);
391 392 393 394 395 396 397 398

        size++;
        *data = msi_alloc(size*sizeof(WCHAR));
        if (size > 1)
            MSI_FormatRecordW(package,rec,*data,&size);
        else
            *data[0] = 0;

399
        msiobj_release( &rec->hdr );
400
        return sizeof(WCHAR)*size;
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    }

    *data = NULL;
    return 0;
}

UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
{
    UINT count;
    LPWSTR *newbuf = NULL;
    if (script >= TOTAL_SCRIPTS)
    {
        FIXME("Unknown script requested %i\n",script);
        return ERROR_FUNCTION_FAILED;
    }
    TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
    
    count = package->script->ActionCount[script];
    package->script->ActionCount[script]++;
    if (count != 0)
421
        newbuf = msi_realloc( package->script->Actions[script],
422 423
                        package->script->ActionCount[script]* sizeof(LPWSTR));
    else
424
        newbuf = msi_alloc( sizeof(LPWSTR));
425 426 427 428 429 430 431

    newbuf[count] = strdupW(action);
    package->script->Actions[script] = newbuf;

   return ERROR_SUCCESS;
}

432 433
void msi_free_action_script(MSIPACKAGE *package, UINT script)
{
434
    UINT i;
435 436 437 438 439 440 441 442
    for (i = 0; i < package->script->ActionCount[script]; i++)
        msi_free(package->script->Actions[script][i]);

    msi_free(package->script->Actions[script]);
    package->script->Actions[script] = NULL;
    package->script->ActionCount[script] = 0;
}

443 444
static void remove_tracked_tempfiles(MSIPACKAGE* package)
{
445
    struct list *item, *cursor;
446

447
    LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
448
    {
449 450 451 452
        MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );

        list_remove( &temp->entry );
        TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
453 454
        if (!DeleteFileW( temp->Path ))
            ERR("failed to delete %s\n", debugstr_w( temp->Path ));
455 456
        msi_free( temp->Path );
        msi_free( temp );
457 458 459
    }
}

460 461 462 463
static void free_feature( MSIFEATURE *feature )
{
    struct list *item, *cursor;

464 465 466 467 468 469 470
    LIST_FOR_EACH_SAFE( item, cursor, &feature->Children )
    {
        FeatureList *fl = LIST_ENTRY( item, FeatureList, entry );
        list_remove( &fl->entry );
        msi_free( fl );
    }

471 472 473 474
    LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
    {
        ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
        list_remove( &cl->entry );
475 476 477 478 479 480 481 482
        msi_free( cl );
    }
    msi_free( feature->Feature );
    msi_free( feature->Feature_Parent );
    msi_free( feature->Directory );
    msi_free( feature->Description );
    msi_free( feature->Title );
    msi_free( feature );
483 484
}

485
static void free_extension( MSIEXTENSION *ext )
486 487 488 489 490 491 492 493
{
    struct list *item, *cursor;

    LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
    {
        MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );

        list_remove( &verb->entry );
494 495 496 497
        msi_free( verb->Verb );
        msi_free( verb->Command );
        msi_free( verb->Argument );
        msi_free( verb );
498 499
    }

500 501 502
    msi_free( ext->Extension );
    msi_free( ext->ProgIDText );
    msi_free( ext );
503 504
}

505 506 507 508
/* Called when the package is being closed */
void ACTION_free_package_structures( MSIPACKAGE* package)
{
    INT i;
509
    struct list *item, *cursor;
510

511 512 513 514
    TRACE("Freeing package action data\n");

    remove_tracked_tempfiles(package);

515
    LIST_FOR_EACH_SAFE( item, cursor, &package->features )
516
    {
517 518 519
        MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
        list_remove( &feature->entry );
        free_feature( feature );
520
    }
521

522
    LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
523
    {
524 525 526
        MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );

        list_remove( &folder->entry );
527
        msi_free( folder->Parent );
528 529
        msi_free( folder->Directory );
        msi_free( folder->TargetDefault );
530 531
        msi_free( folder->SourceLongPath );
        msi_free( folder->SourceShortPath );
532 533 534
        msi_free( folder->ResolvedTarget );
        msi_free( folder->ResolvedSource );
        msi_free( folder->Property );
535
        msi_free( folder );
536 537
    }

538 539 540
    LIST_FOR_EACH_SAFE( item, cursor, &package->components )
    {
        MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
541

542
        list_remove( &comp->entry );
543 544 545 546 547 548 549
        msi_free( comp->Component );
        msi_free( comp->ComponentId );
        msi_free( comp->Directory );
        msi_free( comp->Condition );
        msi_free( comp->KeyPath );
        msi_free( comp->FullKeypath );
        msi_free( comp );
550
    }
551

552
    LIST_FOR_EACH_SAFE( item, cursor, &package->files )
553
    {
554 555 556
        MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );

        list_remove( &file->entry );
557 558 559
        msi_free( file->File );
        msi_free( file->FileName );
        msi_free( file->ShortName );
560
        msi_free( file->LongName );
561 562 563 564
        msi_free( file->Version );
        msi_free( file->Language );
        msi_free( file->TargetPath );
        msi_free( file );
565 566 567
    }

    /* clean up extension, progid, class and verb structures */
568
    LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
569
    {
570 571 572
        MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );

        list_remove( &cls->entry );
573 574 575 576 577 578 579 580 581 582
        msi_free( cls->clsid );
        msi_free( cls->Context );
        msi_free( cls->Description );
        msi_free( cls->FileTypeMask );
        msi_free( cls->IconPath );
        msi_free( cls->DefInprocHandler );
        msi_free( cls->DefInprocHandler32 );
        msi_free( cls->Argument );
        msi_free( cls->ProgIDText );
        msi_free( cls );
583 584
    }

585
    LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
586
    {
587
        MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
588

589
        list_remove( &ext->entry );
590
        free_extension( ext );
591
    }
592

593
    LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
594
    {
595
        MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
596

597
        list_remove( &progid->entry );
598 599 600 601
        msi_free( progid->ProgID );
        msi_free( progid->Description );
        msi_free( progid->IconPath );
        msi_free( progid );
602
    }
603

604 605 606
    LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
    {
        MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
607

608
        list_remove( &mt->entry );
609 610 611
        msi_free( mt->clsid );
        msi_free( mt->ContentType );
        msi_free( mt );
612
    }
613

614
    LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
615
    {
616 617 618
        MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );

        list_remove( &appid->entry );
619 620 621 622 623 624
        msi_free( appid->AppID );
        msi_free( appid->RemoteServerName );
        msi_free( appid->LocalServer );
        msi_free( appid->ServiceParameters );
        msi_free( appid->DllSurrogate );
        msi_free( appid );
625 626
    }

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
    LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_info )
    {
        MSISOURCELISTINFO *info = LIST_ENTRY( item, MSISOURCELISTINFO, entry );

        list_remove( &info->entry );
        msi_free( info->value );
	msi_free( info );
    }

    LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_media )
    {
        MSIMEDIADISK *info = LIST_ENTRY( item, MSIMEDIADISK, entry );

        list_remove( &info->entry );
        msi_free( info->volume_label );
        msi_free( info->disk_prompt );
	msi_free( info );
    }

646 647 648
    if (package->script)
    {
        for (i = 0; i < TOTAL_SCRIPTS; i++)
649
            msi_free_action_script(package, i);
650 651

        for (i = 0; i < package->script->UniqueActionsCount; i++)
652
            msi_free(package->script->UniqueActions[i]);
653

654 655
        msi_free(package->script->UniqueActions);
        msi_free(package->script);
656 657
    }

658 659 660 661 662 663 664
    if (package->patch)
    {
        msi_free(package->patch->patchcode);
        msi_free(package->patch->transforms);
        msi_free(package->patch);
    }

665
    msi_free(package->BaseURL);
666 667
    msi_free(package->PackagePath);
    msi_free(package->ProductCode);
668 669
    msi_free(package->ActionFormat);
    msi_free(package->LastAction);
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709

    /* cleanup control event subscriptions */
    ControlEvent_CleanupSubscriptions(package);
}

/*
 *  build_directory_name()
 *
 *  This function is to save messing round with directory names
 *  It handles adding backslashes between path segments, 
 *   and can add \ at the end of the directory name if told to.
 *
 *  It takes a variable number of arguments.
 *  It always allocates a new string for the result, so make sure
 *   to free the return value when finished with it.
 *
 *  The first arg is the number of path segments that follow.
 *  The arguments following count are a list of path segments.
 *  A path segment may be NULL.
 *
 *  Path segments will be added with a \ separating them.
 *  A \ will not be added after the last segment, however if the
 *    last segment is NULL, then the last character will be a \
 * 
 */
LPWSTR build_directory_name(DWORD count, ...)
{
    DWORD sz = 1, i;
    LPWSTR dir;
    va_list va;

    va_start(va,count);
    for(i=0; i<count; i++)
    {
        LPCWSTR str = va_arg(va,LPCWSTR);
        if (str)
            sz += strlenW(str) + 1;
    }
    va_end(va);

710
    dir = msi_alloc(sz*sizeof(WCHAR));
711 712 713 714 715 716 717 718 719 720
    dir[0]=0;

    va_start(va,count);
    for(i=0; i<count; i++)
    {
        LPCWSTR str = va_arg(va,LPCWSTR);
        if (!str)
            continue;
        strcatW(dir, str);
        if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
721
            strcatW(dir, szBackSlash);
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
    }
    return dir;
}

/***********************************************************************
 *            create_full_pathW
 *
 * Recursively create all directories in the path.
 *
 * shamelessly stolen from setupapi/queue.c
 */
BOOL create_full_pathW(const WCHAR *path)
{
    BOOL ret = TRUE;
    int len;
    WCHAR *new_path;

739
    new_path = msi_alloc( (strlenW(path) + 1) * sizeof(WCHAR));
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774

    strcpyW(new_path, path);

    while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
    new_path[len - 1] = 0;

    while(!CreateDirectoryW(new_path, NULL))
    {
        WCHAR *slash;
        DWORD last_error = GetLastError();
        if(last_error == ERROR_ALREADY_EXISTS)
            break;

        if(last_error != ERROR_PATH_NOT_FOUND)
        {
            ret = FALSE;
            break;
        }

        if(!(slash = strrchrW(new_path, '\\')))
        {
            ret = FALSE;
            break;
        }

        len = slash - new_path;
        new_path[len] = 0;
        if(!create_full_pathW(new_path))
        {
            ret = FALSE;
            break;
        }
        new_path[len] = '\\';
    }

775
    msi_free(new_path);
776 777 778 779 780 781 782 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 816 817
    return ret;
}

void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
{
    MSIRECORD * row;

    row = MSI_CreateRecord(4);
    MSI_RecordSetInteger(row,1,a);
    MSI_RecordSetInteger(row,2,b);
    MSI_RecordSetInteger(row,3,c);
    MSI_RecordSetInteger(row,4,d);
    MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
    msiobj_release(&row->hdr);

    msi_dialog_check_messages(NULL);
}

void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
{
    static const WCHAR Query_t[] = 
        {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
         '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
         'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=', 
         ' ','\'','%','s','\'',0};
    WCHAR message[1024];
    MSIRECORD * row = 0;
    DWORD size;

    if (!package->LastAction || strcmpW(package->LastAction,action))
    {
        row = MSI_QueryGetRecord(package->db, Query_t, action);
        if (!row)
            return;

        if (MSI_RecordIsNull(row,3))
        {
            msiobj_release(&row->hdr);
            return;
        }

        /* update the cached actionformat */
818
        msi_free(package->ActionFormat);
819
        package->ActionFormat = msi_dup_record_field(row,3);
820

821
        msi_free(package->LastAction);
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
        package->LastAction = strdupW(action);

        msiobj_release(&row->hdr);
    }

    MSI_RecordSetStringW(record,0,package->ActionFormat);
    size = 1024;
    MSI_FormatRecordW(package,record,message,&size);

    row = MSI_CreateRecord(1);
    MSI_RecordSetStringW(row,1,message);
 
    MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);

    msiobj_release(&row->hdr);
}

839
BOOL ACTION_VerifyComponentForAction( const MSICOMPONENT* comp, INSTALLSTATE check )
840
{
841 842 843
    if (!comp)
        return FALSE;

844
    if (comp->ActionRequest == check)
845 846 847 848 849
        return TRUE;
    else
        return FALSE;
}

850
BOOL ACTION_VerifyFeatureForAction( const MSIFEATURE* feature, INSTALLSTATE check )
851
{
852 853 854
    if (!feature)
        return FALSE;

855
    if (feature->ActionRequest == check)
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
        return TRUE;
    else
        return FALSE;
}

void reduce_to_longfilename(WCHAR* filename)
{
    LPWSTR p = strchrW(filename,'|');
    if (p)
        memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
}

LPWSTR create_component_advertise_string(MSIPACKAGE* package, 
                MSICOMPONENT* component, LPCWSTR feature)
{
871 872
    static const WCHAR fmt[] = {'%','s','%','s','%','c','%','s',0};
    WCHAR productid_85[21], component_85[21];
873 874
    LPWSTR output = NULL;
    DWORD sz = 0;
875 876 877
    GUID clsid;

    /* > is used if there is a component GUID and < if not.  */
878

879 880
    productid_85[0] = 0;
    component_85[0] = 0;
881

882
    CLSIDFromString(package->ProductCode, &clsid);
883
    encode_base85_guid(&clsid, productid_85);
884

885 886 887 888 889
    if (component)
    {
        CLSIDFromString(component->ComponentId, &clsid);
        encode_base85_guid(&clsid, component_85);
    }
890

891 892
    TRACE("prod=%s feat=%s comp=%s\n", debugstr_w(productid_85),
          debugstr_w(feature), debugstr_w(component_85));
893
 
894
    sz = 20 + lstrlenW(feature) + 20 + 3;
895

896
    output = msi_alloc_zero(sz*sizeof(WCHAR));
897

898 899
    sprintfW(output, fmt, productid_85, feature,
             component?'>':'<', component_85);
900 901 902 903
    
    return output;
}

Austin English's avatar
Austin English committed
904
/* update component state based on a feature change */
905 906 907 908
void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
{
    INSTALLSTATE newstate;
    MSIFEATURE *feature;
909
    ComponentList *cl;
910

911 912
    feature = get_loaded_feature(package,szFeature);
    if (!feature)
913 914 915 916
        return;

    newstate = feature->ActionRequest;

917 918 919
    if (newstate == INSTALLSTATE_ABSENT)
        newstate = INSTALLSTATE_UNKNOWN;

920
    LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
921
    {
922 923
        MSICOMPONENT* component = cl->component;
    
924 925 926 927 928 929
        TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
            newstate, debugstr_w(component->Component), component->Installed, 
            component->Action, component->ActionRequest);
        
        if (!component->Enabled)
            continue;
930 931
 
        if (newstate == INSTALLSTATE_LOCAL)
932
            msi_component_set_state(package, component, INSTALLSTATE_LOCAL);
933 934 935 936
        else 
        {
            ComponentList *clist;
            MSIFEATURE *f;
937

938 939
            component->hasLocalFeature = FALSE;

940
            msi_component_set_state(package, component, newstate);
941

942 943 944
            /*if any other feature wants is local we need to set it local*/
            LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
            {
945 946 947
                if ( f->ActionRequest != INSTALLSTATE_LOCAL &&
                     f->ActionRequest != INSTALLSTATE_SOURCE )
                {
948
                    continue;
949
                }
950

951 952
                LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
                {
953 954 955
                    if ( clist->component == component &&
                         (f->ActionRequest == INSTALLSTATE_LOCAL ||
                          f->ActionRequest == INSTALLSTATE_SOURCE) )
956
                    {
957
                        TRACE("Saved by %s\n", debugstr_w(f->Feature));
958
                        component->hasLocalFeature = TRUE;
959 960 961 962

                        if (component->Attributes & msidbComponentAttributesOptional)
                        {
                            if (f->Attributes & msidbFeatureAttributesFavorSource)
963
                                msi_component_set_state(package, component, INSTALLSTATE_SOURCE);
964
                            else
965
                                msi_component_set_state(package, component, INSTALLSTATE_LOCAL);
966 967
                        }
                        else if (component->Attributes & msidbComponentAttributesSourceOnly)
968
                            msi_component_set_state(package, component, INSTALLSTATE_SOURCE);
969
                        else
970
                            msi_component_set_state(package, component, INSTALLSTATE_LOCAL);
971
                    }
972 973 974 975 976 977 978 979
                }
            }
        }
        TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
            newstate, debugstr_w(component->Component), component->Installed, 
            component->Action, component->ActionRequest);
    } 
}
980

981 982 983 984 985
UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
{
    UINT count;
    LPWSTR *newbuf = NULL;

986
    if (!package->script)
987 988 989 990 991 992 993
        return FALSE;

    TRACE("Registering Action %s as having fun\n",debugstr_w(action));
    
    count = package->script->UniqueActionsCount;
    package->script->UniqueActionsCount++;
    if (count != 0)
994
        newbuf = msi_realloc( package->script->UniqueActions,
995 996
                        package->script->UniqueActionsCount* sizeof(LPWSTR));
    else
997
        newbuf = msi_alloc( sizeof(LPWSTR));
998 999 1000 1001

    newbuf[count] = strdupW(action);
    package->script->UniqueActions = newbuf;

1002
    return ERROR_SUCCESS;
1003 1004
}

1005
BOOL check_unique_action(const MSIPACKAGE *package, LPCWSTR action)
1006
{
1007
    UINT i;
1008

1009
    if (!package->script)
1010 1011 1012 1013 1014 1015 1016 1017 1018
        return FALSE;

    for (i = 0; i < package->script->UniqueActionsCount; i++)
        if (!strcmpW(package->script->UniqueActions[i],action))
            return TRUE;

    return FALSE;
}

1019 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
WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
{
    static const WCHAR query[] = {'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ','F','R','O','M',' ','`','E','r','r','o','r','`',' ','W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ','%','i',0};

    MSIRECORD *rec;
    MSIRECORD *row;
    DWORD size = 0;
    DWORD i;
    va_list va;
    LPCWSTR str;
    LPWSTR data;

    row = MSI_QueryGetRecord(package->db, query, error);
    if (!row)
        return 0;

    rec = MSI_CreateRecord(count+2);

    str = MSI_RecordGetString(row,1);
    MSI_RecordSetStringW(rec,0,str);
    msiobj_release( &row->hdr );
    MSI_RecordSetInteger(rec,1,error);

    va_start(va,count);
    for (i = 0; i < count; i++)
    {
        str = va_arg(va,LPCWSTR);
        MSI_RecordSetStringW(rec,(i+2),str);
    }
    va_end(va);

    MSI_FormatRecordW(package,rec,NULL,&size);

1052 1053 1054 1055 1056 1057
    size++;
    data = msi_alloc(size*sizeof(WCHAR));
    if (size > 1)
        MSI_FormatRecordW(package,rec,data,&size);
    else
        data[0] = 0;
1058 1059 1060
    msiobj_release( &rec->hdr );
    return data;
}