cache.c 16.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * IAssemblyCache implementation
 *
 * Copyright 2010 Hans Leidekker 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>

#define COBJMACROS
#define INITGUID

#include "windef.h"
#include "winbase.h"
#include "ole2.h"
#include "winsxs.h"
30 31
#include "msxml2.h"

32
#include "wine/debug.h"
33 34
#include "wine/list.h"
#include "wine/unicode.h"
35 36 37

WINE_DEFAULT_DEBUG_CHANNEL(sxs);

38 39 40 41 42 43 44 45
static inline WCHAR *strdupW( const WCHAR *s )
{
    WCHAR *t;
    if (!s) return NULL;
    if ((t = HeapAlloc( GetProcessHeap(), 0, (strlenW( s ) + 1) * sizeof(WCHAR) ))) strcpyW( t, s );
    return t;
}

46 47
struct cache
{
48
    IAssemblyCache IAssemblyCache_iface;
49 50 51
    LONG refs;
};

52 53 54 55 56
static inline struct cache *impl_from_IAssemblyCache(IAssemblyCache *iface)
{
    return CONTAINING_RECORD(iface, struct cache, IAssemblyCache_iface);
}

57 58 59 60 61
static HRESULT WINAPI cache_QueryInterface(
    IAssemblyCache *iface,
    REFIID riid,
    void **obj )
{
62
    struct cache *cache = impl_from_IAssemblyCache(iface);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

    TRACE("%p, %s, %p\n", cache, debugstr_guid(riid), obj);

    *obj = NULL;

    if (IsEqualIID(riid, &IID_IUnknown) ||
        IsEqualIID(riid, &IID_IAssemblyCache))
    {
        IUnknown_AddRef( iface );
        *obj = cache;
        return S_OK;
    }

    return E_NOINTERFACE;
}

static ULONG WINAPI cache_AddRef( IAssemblyCache *iface )
{
81
    struct cache *cache = impl_from_IAssemblyCache(iface);
82 83 84 85 86
    return InterlockedIncrement( &cache->refs );
}

static ULONG WINAPI cache_Release( IAssemblyCache *iface )
{
87
    struct cache *cache = impl_from_IAssemblyCache(iface);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    ULONG refs = InterlockedDecrement( &cache->refs );

    if (!refs)
    {
        TRACE("destroying %p\n", cache);
        HeapFree( GetProcessHeap(), 0, cache );
    }
    return refs;
}

static HRESULT WINAPI cache_UninstallAssembly(
    IAssemblyCache *iface,
    DWORD flags,
    LPCWSTR name,
    LPCFUSION_INSTALL_REFERENCE ref,
    ULONG *disp )
{
    FIXME("%p, 0x%08x, %s, %p, %p\n", iface, flags, debugstr_w(name), ref, disp);
    return E_NOTIMPL;
}

static HRESULT WINAPI cache_QueryAssemblyInfo(
    IAssemblyCache *iface,
    DWORD flags,
    LPCWSTR name,
    ASSEMBLY_INFO *info )
{
    FIXME("%p, 0x%08x, %s, %p\n", iface, flags, debugstr_w(name), info);
    return E_NOTIMPL;
}

static HRESULT WINAPI cache_CreateAssemblyCacheItem(
    IAssemblyCache *iface,
    DWORD flags,
    PVOID reserved,
    IAssemblyCacheItem **item,
    LPCWSTR name )
{
    FIXME("%p, 0x%08x, %p, %p, %s\n", iface, flags, reserved, item, debugstr_w(name));
    return E_NOTIMPL;
}

static HRESULT WINAPI cache_Reserved(
    IAssemblyCache *iface,
    IUnknown **reserved)
{
    FIXME("%p\n", reserved);
    return E_NOTIMPL;
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 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 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
static BSTR get_attribute_value( IXMLDOMNamedNodeMap *map, const WCHAR *value_name )
{
    HRESULT hr;
    IXMLDOMNode *attr;
    VARIANT var;
    BSTR str;

    str = SysAllocString( value_name );
    hr = IXMLDOMNamedNodeMap_getNamedItem( map, str, &attr );
    SysFreeString( str );
    if (hr != S_OK) return NULL;

    hr = IXMLDOMNode_get_nodeValue( attr, &var );
    IXMLDOMNode_Release( attr );
    if (hr != S_OK) return NULL;
    if (V_VT(&var) != VT_BSTR)
    {
        VariantClear( &var );
        return NULL;
    }
    TRACE("%s=%s\n", debugstr_w(value_name), debugstr_w(V_BSTR( &var )));
    return V_BSTR( &var );
}

struct file
{
    struct list entry;
    BSTR name;
};

struct assembly
{
    BSTR type;
    BSTR name;
    BSTR version;
    BSTR arch;
    BSTR token;
    struct list files;
};

static void free_assembly( struct assembly *assembly )
{
    struct list *item, *cursor;

    if (!assembly) return;
    SysFreeString( assembly->type );
    SysFreeString( assembly->name );
    SysFreeString( assembly->version );
    SysFreeString( assembly->arch );
    SysFreeString( assembly->token );
    LIST_FOR_EACH_SAFE( item, cursor, &assembly->files )
    {
        struct file *file = LIST_ENTRY( item, struct file, entry );
        list_remove( &file->entry );
        SysFreeString( file->name );
        HeapFree( GetProcessHeap(), 0, file );
    }
    HeapFree( GetProcessHeap(), 0, assembly );
}

static HRESULT parse_files( IXMLDOMDocument *doc, struct assembly *assembly )
{
    static const WCHAR fileW[] = {'f','i','l','e',0};
    static const WCHAR nameW[] = {'n','a','m','e',0};
    IXMLDOMNamedNodeMap *attrs;
    IXMLDOMNodeList *list;
    IXMLDOMNode *node;
    struct file *f;
    BSTR str;
    HRESULT hr;
    LONG len;

    str = SysAllocString( fileW );
    hr = IXMLDOMDocument_getElementsByTagName( doc, str, &list );
    SysFreeString( str );
    if (hr != S_OK) return hr;

    hr = IXMLDOMNodeList_get_length( list, &len );
    if (hr != S_OK) goto done;
    TRACE("found %d files\n", len);
    if (!len)
    {
        hr = ERROR_SXS_MANIFEST_FORMAT_ERROR;
        goto done;
    }

    for (;;)
    {
        hr = IXMLDOMNodeList_nextNode( list, &node );
        if (hr != S_OK || !node)
        {
            hr = S_OK;
            break;
        }

        /* FIXME: validate node type */

        hr = IXMLDOMNode_get_attributes( node, &attrs );
        IXMLDOMNode_Release( node );
        if (hr != S_OK)
            goto done;

        if (!(f = HeapAlloc( GetProcessHeap(), 0, sizeof(struct file) )))
        {
            IXMLDOMNamedNodeMap_Release( attrs );
            hr = E_OUTOFMEMORY;
            goto done;
        }

        f->name = get_attribute_value( attrs, nameW );
        IXMLDOMNamedNodeMap_Release( attrs );
        if (!f->name)
        {
            HeapFree( GetProcessHeap(), 0, f );
            hr = ERROR_SXS_MANIFEST_FORMAT_ERROR;
            goto done;
        }
        list_add_tail( &assembly->files, &f->entry );
    }

    if (list_empty( &assembly->files ))
    {
        WARN("no files found\n");
        hr = ERROR_SXS_MANIFEST_FORMAT_ERROR;
    }

done:
    IXMLDOMNodeList_Release( list );
    return hr;
}

static HRESULT parse_assembly( IXMLDOMDocument *doc, struct assembly **assembly )
{
    static const WCHAR identityW[] = {'a','s','s','e','m','b','l','y','I','d','e','n','t','i','t','y',0};
    static const WCHAR typeW[] = {'t','y','p','e',0};
    static const WCHAR nameW[] = {'n','a','m','e',0};
    static const WCHAR versionW[] = {'v','e','r','s','i','o','n',0};
    static const WCHAR architectureW[] = {'p','r','o','c','e','s','s','o','r','A','r','c','h','i','t','e','c','t','u','r','e',0};
    static const WCHAR tokenW[] = {'p','u','b','l','i','c','K','e','y','T','o','k','e','n',0};
    static const WCHAR win32W[] = {'w','i','n','3','2',0};
    static const WCHAR policyW[] = {'w','i','n','3','2','-','p','o','l','i','c','y',0};
    IXMLDOMNodeList *list = NULL;
    IXMLDOMNode *node = NULL;
    IXMLDOMNamedNodeMap *attrs = NULL;
    struct assembly *a = NULL;
    BSTR str;
    HRESULT hr;
    LONG len;

    str = SysAllocString( identityW );
    hr = IXMLDOMDocument_getElementsByTagName( doc, str, &list );
    SysFreeString( str );
    if (hr != S_OK) goto done;

    hr = IXMLDOMNodeList_get_length( list, &len );
    if (hr != S_OK) goto done;
    if (!len)
    {
        hr = ERROR_SXS_MANIFEST_FORMAT_ERROR;
        goto done;
    }
    hr = IXMLDOMNodeList_nextNode( list, &node );
    if (hr != S_OK) goto done;
    if (!node)
    {
        hr = ERROR_SXS_MANIFEST_FORMAT_ERROR;
        goto done;
    }
    if (!(a = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct assembly) )))
    {
        hr = E_OUTOFMEMORY;
        goto done;
    }
    list_init( &a->files );

    hr = IXMLDOMNode_get_attributes( node, &attrs );
    if (hr != S_OK) goto done;

316
    a->type    = get_attribute_value( attrs, typeW );
317 318 319 320 321
    a->name    = get_attribute_value( attrs, nameW );
    a->version = get_attribute_value( attrs, versionW );
    a->arch    = get_attribute_value( attrs, architectureW );
    a->token   = get_attribute_value( attrs, tokenW );

322 323
    if (!a->type || (strcmpW( a->type, win32W ) && strcmpW( a->type, policyW )) ||
        !a->name || !a->version || !a->arch || !a->token)
324 325 326 327 328
    {
        WARN("invalid win32 assembly\n");
        hr = ERROR_SXS_MANIFEST_FORMAT_ERROR;
        goto done;
    }
329
    if (!strcmpW( a->type, win32W )) hr = parse_files( doc, a );
330 331 332 333 334 335 336 337 338 339

done:
    if (attrs) IXMLDOMNamedNodeMap_Release( attrs );
    if (node) IXMLDOMNode_Release( node );
    if (list) IXMLDOMNodeList_Release( list );
    if (hr == S_OK) *assembly = a;
    else free_assembly( a );
    return hr;
}

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
static WCHAR *build_sxs_path( void )
{
    static const WCHAR winsxsW[] = {'\\','w','i','n','s','x','s','\\',0};
    WCHAR sxsdir[MAX_PATH];

    GetWindowsDirectoryW( sxsdir, MAX_PATH );
    strcatW( sxsdir, winsxsW );
    return strdupW( sxsdir );
}

static WCHAR *build_assembly_name( struct assembly *assembly )
{
    static const WCHAR fmtW[] =
        {'%','s','_','%','s','_','%','s','_','%','s','_','n','o','n','e','_','d','e','a','d','b','e','e','f',0};
    WCHAR *ret, *p;
    int len;

    len = strlenW( fmtW );
    len += strlenW( assembly->arch );
    len += strlenW( assembly->name );
    len += strlenW( assembly->token );
    len += strlenW( assembly->version );

    if (!(ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return NULL;
    sprintfW( ret, fmtW, assembly->arch, assembly->name, assembly->token, assembly->version );
    for (p = ret; *p; p++) *p = tolowerW( *p );
    return ret;
}

static WCHAR *build_policy_name( struct assembly *assembly )
{
    static const WCHAR fmtW[] =
        {'%','s','_','%','s','_','%','s','_','n','o','n','e','_','d','e','a','d','b','e','e','f',0};
    WCHAR *ret, *p;
    int len;

    len = strlenW( fmtW );
    len += strlenW( assembly->arch );
    len += strlenW( assembly->name );
    len += strlenW( assembly->token );

    if (!(ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return NULL;
    sprintfW( ret, fmtW, assembly->arch, assembly->name, assembly->token );
    for (p = ret; *p; p++) *p = tolowerW( *p );
    return ret;
}

static HRESULT install_policy( const WCHAR *manifest, struct assembly *assembly )
{
    static const WCHAR policiesW[] = {'p','o','l','i','c','i','e','s','\\',0};
    static const WCHAR suffixW[] = {'.','p','o','l','i','c','y',0};
    static const WCHAR backslashW[] = {'\\',0};
    WCHAR *sxsdir, *name, *dst;
    HRESULT hr = E_OUTOFMEMORY;
    BOOL ret;
    int len;

    /* FIXME: handle catalog file */

    if (!(sxsdir = build_sxs_path())) return E_OUTOFMEMORY;
    if (!(name = build_policy_name( assembly ))) goto done;

    len = strlenW( sxsdir );
    len += strlenW( policiesW );
    len += strlenW( name ) + 1;
    len += strlenW( assembly->version );
    len += strlenW( suffixW );

    if (!(dst = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) goto done;
    strcpyW( dst, sxsdir );
    strcatW( dst, policiesW );
    CreateDirectoryW( dst, NULL );
    strcatW( dst, name );
    CreateDirectoryW( dst, NULL );
    strcatW( dst, backslashW );
    strcatW( dst, assembly->version );
    strcatW( dst, suffixW );

    ret = CopyFileW( manifest, dst, FALSE );
    HeapFree( GetProcessHeap(), 0, dst );
    if (!ret)
    {
        hr = HRESULT_FROM_WIN32( GetLastError() );
        WARN("failed to copy policy manifest file 0x%08x\n", hr);
    }
    hr = S_OK;

done:
    HeapFree( GetProcessHeap(), 0, sxsdir );
    HeapFree( GetProcessHeap(), 0, name );
    return hr;
}

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
static WCHAR *build_source_filename( const WCHAR *manifest, struct file *file )
{
    WCHAR *src;
    const WCHAR *p;
    int len;

    p = strrchrW( manifest, '\\' );
    if (!p) p = strrchrW( manifest, '/' );
    if (!p) return strdupW( manifest );

    len = p - manifest + 1;
    if (!(src = HeapAlloc( GetProcessHeap(), 0, (len + strlenW( file->name ) + 1) * sizeof(WCHAR) )))
        return NULL;

    memcpy( src, manifest, len * sizeof(WCHAR) );
    strcpyW( src + len, file->name );
    return src;
}

static HRESULT install_assembly( const WCHAR *manifest, struct assembly *assembly )
{
    static const WCHAR manifestsW[] = {'m','a','n','i','f','e','s','t','s','\\',0};
    static const WCHAR suffixW[] = {'.','m','a','n','i','f','e','s','t',0};
    static const WCHAR backslashW[] = {'\\',0};
457
    WCHAR *sxsdir, *p, *name, *dst, *src;
458
    struct file *file;
459
    HRESULT hr = E_OUTOFMEMORY;
460
    BOOL ret;
461
    int len;
462

463 464
    if (!(sxsdir = build_sxs_path())) return E_OUTOFMEMORY;
    if (!(name = build_assembly_name( assembly ))) goto done;
465

466
    len = strlenW( sxsdir );
467
    len += strlenW( manifestsW );
468
    len += strlenW( name );
469
    len += strlenW( suffixW );
470
    if (!(dst = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) goto done;
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
    strcpyW( dst, sxsdir );
    strcatW( dst, manifestsW );
    strcatW( dst, name );
    strcatW( dst, suffixW );

    ret = CopyFileW( manifest, dst, FALSE );
    HeapFree( GetProcessHeap(), 0, dst );
    if (!ret)
    {
        hr = HRESULT_FROM_WIN32( GetLastError() );
        WARN("failed to copy manifest file 0x%08x\n", hr);
        goto done;
    }

    /* FIXME: this should be a transaction */
    LIST_FOR_EACH_ENTRY( file, &assembly->files, struct file, entry )
    {
        if (!(src = build_source_filename( manifest, file )))
        {
            hr = E_OUTOFMEMORY;
            goto done;
        }
        len = strlenW( sxsdir ) + strlenW( name ) + strlenW( file->name );
        if (!(dst = HeapAlloc( GetProcessHeap(), 0, (len + 2) * sizeof(WCHAR) )))
        {
            HeapFree( GetProcessHeap(), 0, src );
            hr = E_OUTOFMEMORY;
            goto done;
        }
        strcpyW( dst, sxsdir );
        strcatW( dst, name );
        CreateDirectoryW( dst, NULL );

        strcatW( dst, backslashW );
        strcatW( dst, file->name );
        for (p = dst; *p; p++) *p = tolowerW( *p );

        ret = CopyFileW( src, dst, FALSE );
        HeapFree( GetProcessHeap(), 0, src );
        HeapFree( GetProcessHeap(), 0, dst );
        if (!ret)
        {
            hr = HRESULT_FROM_WIN32( GetLastError() );
            WARN("failed to copy file 0x%08x\n", hr);
            goto done;
        }
    }
518
    hr = S_OK;
519 520

done:
521
    HeapFree( GetProcessHeap(), 0, sxsdir );
522 523 524 525
    HeapFree( GetProcessHeap(), 0, name );
    return hr;
}

526 527 528 529 530 531
static HRESULT WINAPI cache_InstallAssembly(
    IAssemblyCache *iface,
    DWORD flags,
    LPCWSTR path,
    LPCFUSION_INSTALL_REFERENCE ref )
{
532
    static const WCHAR policyW[] = {'w','i','n','3','2','-','p','o','l','i','c','y',0};
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
    HRESULT hr, init;
    IXMLDOMDocument *doc = NULL;
    struct assembly *assembly = NULL;
    BSTR str;
    VARIANT var;
    VARIANT_BOOL b;

    TRACE("%p, 0x%08x, %s, %p\n", iface, flags, debugstr_w(path), ref);

    init = CoInitialize( NULL );

    hr = CoCreateInstance( &CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER, &IID_IXMLDOMDocument, (void **)&doc );
    if (hr != S_OK)
        goto done;

    str = SysAllocString( path );
    VariantInit( &var );
    V_VT( &var ) = VT_BSTR;
    V_BSTR( &var ) = str;
    hr = IXMLDOMDocument_load( doc, var, &b );
    SysFreeString( str );
    if (hr != S_OK) goto done;
    if (!b)
    {
        WARN("failed to load manifest\n");
        hr = S_FALSE;
        goto done;
    }

    hr = parse_assembly( doc, &assembly );
    if (hr != S_OK)
        goto done;

    /* FIXME: verify name attributes */

568 569 570 571
    if (!strcmpW( assembly->type, policyW ))
        hr = install_policy( path, assembly );
    else
        hr = install_assembly( path, assembly );
572 573 574 575 576 577 578 579 580

done:
    free_assembly( assembly );
    if (doc) IXMLDOMDocument_Release( doc );

    if (SUCCEEDED(init))
        CoUninitialize();

    return hr;
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
}

static const IAssemblyCacheVtbl cache_vtbl =
{
    cache_QueryInterface,
    cache_AddRef,
    cache_Release,
    cache_UninstallAssembly,
    cache_QueryAssemblyInfo,
    cache_CreateAssemblyCacheItem,
    cache_Reserved,
    cache_InstallAssembly
};

/******************************************************************
 *  CreateAssemblyCache   (SXS.@)
 */
HRESULT WINAPI CreateAssemblyCache( IAssemblyCache **obj, DWORD reserved )
{
    struct cache *cache;

    TRACE("%p, %u\n", obj, reserved);

    if (!obj)
        return E_INVALIDARG;

    *obj = NULL;

    cache = HeapAlloc( GetProcessHeap(), 0, sizeof(struct cache) );
    if (!cache)
        return E_OUTOFMEMORY;

613
    cache->IAssemblyCache_iface.lpVtbl = &cache_vtbl;
614 615
    cache->refs = 1;

616
    *obj = &cache->IAssemblyCache_iface;
617 618
    return S_OK;
}