services: Check for services without lpBinaryPathName in get_winedevice_process.
[wine.git] / dlls / msi / patch.c
blobeb91e06e9a9fbfab2cc824e23d52a1a7bacfcc2b
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2004,2005 Aric Stewart for CodeWeavers
5 * Copyright 2011 Hans Leidekker for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
23 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winreg.h"
27 #include "objbase.h"
28 #include "shlwapi.h"
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31 #include "msipriv.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msi);
35 static BOOL match_language( MSIPACKAGE *package, LANGID langid )
37 UINT i;
39 if (!package->num_langids || !langid) return TRUE;
40 for (i = 0; i < package->num_langids; i++)
42 if (package->langids[i] == langid) return TRUE;
44 return FALSE;
47 struct transform_desc
49 WCHAR *product_code_from;
50 WCHAR *product_code_to;
51 WCHAR *version_from;
52 WCHAR *version_to;
53 WCHAR *upgrade_code;
56 static void free_transform_desc( struct transform_desc *desc )
58 msi_free( desc->product_code_from );
59 msi_free( desc->product_code_to );
60 msi_free( desc->version_from );
61 msi_free( desc->version_to );
62 msi_free( desc->upgrade_code );
63 msi_free( desc );
66 static struct transform_desc *parse_transform_desc( const WCHAR *str )
68 struct transform_desc *ret;
69 const WCHAR *p = str, *q;
70 UINT len;
72 if (!(ret = msi_alloc_zero( sizeof(*ret) ))) return NULL;
74 q = strchrW( p, '}' );
75 if (*p != '{' || !q) goto error;
77 len = q - p + 1;
78 if (!(ret->product_code_from = msi_alloc( (len + 1) * sizeof(WCHAR) ))) goto error;
79 memcpy( ret->product_code_from, p, len * sizeof(WCHAR) );
80 ret->product_code_from[len] = 0;
82 p = q + 1;
83 if (!(q = strchrW( p, ';' ))) goto error;
84 len = q - p;
85 if (!(ret->version_from = msi_alloc( (len + 1) * sizeof(WCHAR) ))) goto error;
86 memcpy( ret->version_from, p, len * sizeof(WCHAR) );
87 ret->version_from[len] = 0;
89 p = q + 1;
90 q = strchrW( p, '}' );
91 if (*p != '{' || !q) goto error;
93 len = q - p + 1;
94 if (!(ret->product_code_to = msi_alloc( (len + 1) * sizeof(WCHAR) ))) goto error;
95 memcpy( ret->product_code_to, p, len * sizeof(WCHAR) );
96 ret->product_code_to[len] = 0;
98 p = q + 1;
99 if (!(q = strchrW( p, ';' ))) goto error;
100 len = q - p;
101 if (!(ret->version_to = msi_alloc( (len + 1) * sizeof(WCHAR) ))) goto error;
102 memcpy( ret->version_to, p, len * sizeof(WCHAR) );
103 ret->version_to[len] = 0;
105 p = q + 1;
106 q = strchrW( p, '}' );
107 if (*p != '{' || !q) goto error;
109 len = q - p + 1;
110 if (!(ret->upgrade_code = msi_alloc( (len + 1) * sizeof(WCHAR) ))) goto error;
111 memcpy( ret->upgrade_code, p, len * sizeof(WCHAR) );
112 ret->upgrade_code[len] = 0;
114 return ret;
116 error:
117 free_transform_desc( ret );
118 return NULL;
121 static UINT check_transform_applicable( MSIPACKAGE *package, IStorage *transform )
123 static const UINT supported_flags =
124 MSITRANSFORM_VALIDATE_PRODUCT | MSITRANSFORM_VALIDATE_LANGUAGE |
125 MSITRANSFORM_VALIDATE_PLATFORM | MSITRANSFORM_VALIDATE_MAJORVERSION |
126 MSITRANSFORM_VALIDATE_MINORVERSION | MSITRANSFORM_VALIDATE_UPGRADECODE;
127 MSISUMMARYINFO *si;
128 UINT r, valid_flags = 0, wanted_flags = 0;
129 WCHAR *template, *product, *p;
130 struct transform_desc *desc;
132 r = msi_get_suminfo( transform, 0, &si );
133 if (r != ERROR_SUCCESS)
135 WARN("no summary information!\n");
136 return r;
138 wanted_flags = msi_suminfo_get_int32( si, PID_CHARCOUNT );
139 wanted_flags &= 0xffff; /* mask off error condition flags */
140 TRACE("validation flags 0x%04x\n", wanted_flags);
142 /* native is not validating platform */
143 wanted_flags &= ~MSITRANSFORM_VALIDATE_PLATFORM;
145 if (wanted_flags & ~supported_flags)
147 FIXME("unsupported validation flags 0x%04x\n", wanted_flags);
148 msiobj_release( &si->hdr );
149 return ERROR_FUNCTION_FAILED;
151 if (!(template = msi_suminfo_dup_string( si, PID_TEMPLATE )))
153 WARN("no template property!\n");
154 msiobj_release( &si->hdr );
155 return ERROR_FUNCTION_FAILED;
157 TRACE("template property: %s\n", debugstr_w(template));
158 if (!(product = msi_get_suminfo_product( transform )))
160 WARN("no product property!\n");
161 msi_free( template );
162 msiobj_release( &si->hdr );
163 return ERROR_FUNCTION_FAILED;
165 TRACE("product property: %s\n", debugstr_w(product));
166 if (!(desc = parse_transform_desc( product )))
168 msi_free( template );
169 msiobj_release( &si->hdr );
170 return ERROR_FUNCTION_FAILED;
172 msi_free( product );
174 if (wanted_flags & MSITRANSFORM_VALIDATE_LANGUAGE)
176 if (!template[0] || ((p = strchrW( template, ';' )) && match_language( package, atoiW( p + 1 ) )))
178 valid_flags |= MSITRANSFORM_VALIDATE_LANGUAGE;
181 if (wanted_flags & MSITRANSFORM_VALIDATE_PRODUCT)
183 WCHAR *product_code_installed = msi_dup_property( package->db, szProductCode );
185 if (!product_code_installed)
187 msi_free( template );
188 free_transform_desc( desc );
189 msiobj_release( &si->hdr );
190 return ERROR_INSTALL_PACKAGE_INVALID;
192 if (!strcmpW( desc->product_code_from, product_code_installed ))
194 valid_flags |= MSITRANSFORM_VALIDATE_PRODUCT;
196 msi_free( product_code_installed );
198 msi_free( template );
199 if (wanted_flags & MSITRANSFORM_VALIDATE_MAJORVERSION)
201 WCHAR *product_version_installed = msi_dup_property( package->db, szProductVersion );
202 DWORD major_installed, minor_installed, major, minor;
204 if (!product_version_installed)
206 free_transform_desc( desc );
207 msiobj_release( &si->hdr );
208 return ERROR_INSTALL_PACKAGE_INVALID;
210 msi_parse_version_string( product_version_installed, &major_installed, &minor_installed );
211 msi_parse_version_string( desc->version_from, &major, &minor );
213 if (major_installed == major)
215 valid_flags |= MSITRANSFORM_VALIDATE_MAJORVERSION;
216 wanted_flags &= ~MSITRANSFORM_VALIDATE_MINORVERSION;
218 msi_free( product_version_installed );
220 else if (wanted_flags & MSITRANSFORM_VALIDATE_MINORVERSION)
222 WCHAR *product_version_installed = msi_dup_property( package->db, szProductVersion );
223 DWORD major_installed, minor_installed, major, minor;
225 if (!product_version_installed)
227 free_transform_desc( desc );
228 msiobj_release( &si->hdr );
229 return ERROR_INSTALL_PACKAGE_INVALID;
231 msi_parse_version_string( product_version_installed, &major_installed, &minor_installed );
232 msi_parse_version_string( desc->version_from, &major, &minor );
234 if (major_installed == major && minor_installed == minor)
235 valid_flags |= MSITRANSFORM_VALIDATE_MINORVERSION;
236 msi_free( product_version_installed );
238 if (wanted_flags & MSITRANSFORM_VALIDATE_UPGRADECODE)
240 WCHAR *upgrade_code_installed = msi_dup_property( package->db, szUpgradeCode );
242 if (!upgrade_code_installed)
244 free_transform_desc( desc );
245 msiobj_release( &si->hdr );
246 return ERROR_INSTALL_PACKAGE_INVALID;
248 if (!strcmpW( desc->upgrade_code, upgrade_code_installed ))
249 valid_flags |= MSITRANSFORM_VALIDATE_UPGRADECODE;
250 msi_free( upgrade_code_installed );
253 free_transform_desc( desc );
254 msiobj_release( &si->hdr );
255 if ((valid_flags & wanted_flags) != wanted_flags) return ERROR_FUNCTION_FAILED;
256 TRACE("applicable transform\n");
257 return ERROR_SUCCESS;
260 static UINT apply_substorage_transform( MSIPACKAGE *package, MSIDATABASE *patch_db, LPCWSTR name )
262 UINT ret = ERROR_FUNCTION_FAILED;
263 IStorage *stg = NULL;
264 HRESULT r;
266 TRACE("%p %s\n", package, debugstr_w(name));
268 if (*name++ != ':')
270 ERR("expected a colon in %s\n", debugstr_w(name));
271 return ERROR_FUNCTION_FAILED;
273 r = IStorage_OpenStorage( patch_db->storage, name, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &stg );
274 if (SUCCEEDED(r))
276 ret = check_transform_applicable( package, stg );
277 if (ret == ERROR_SUCCESS)
278 msi_table_apply_transform( package->db, stg );
279 else
280 TRACE("substorage transform %s wasn't applicable\n", debugstr_w(name));
281 IStorage_Release( stg );
283 else
285 ERR("failed to open substorage %s\n", debugstr_w(name));
287 return ERROR_SUCCESS;
290 UINT msi_check_patch_applicable( MSIPACKAGE *package, MSISUMMARYINFO *si )
292 LPWSTR guid_list, *guids, product_code;
293 UINT i, ret = ERROR_FUNCTION_FAILED;
295 product_code = msi_dup_property( package->db, szProductCode );
296 if (!product_code)
298 /* FIXME: the property ProductCode should be written into the DB somewhere */
299 ERR("no product code to check\n");
300 return ERROR_SUCCESS;
302 guid_list = msi_suminfo_dup_string( si, PID_TEMPLATE );
303 guids = msi_split_string( guid_list, ';' );
304 for (i = 0; guids[i] && ret != ERROR_SUCCESS; i++)
306 if (!strcmpW( guids[i], product_code )) ret = ERROR_SUCCESS;
308 msi_free( guids );
309 msi_free( guid_list );
310 msi_free( product_code );
311 return ret;
314 static UINT msi_parse_patch_summary( MSISUMMARYINFO *si, MSIPATCHINFO **patch )
316 MSIPATCHINFO *pi;
317 UINT r = ERROR_SUCCESS;
318 WCHAR *p;
320 if (!(pi = msi_alloc_zero( sizeof(MSIPATCHINFO) )))
322 return ERROR_OUTOFMEMORY;
324 if (!(pi->patchcode = msi_suminfo_dup_string( si, PID_REVNUMBER )))
326 msi_free( pi );
327 return ERROR_OUTOFMEMORY;
329 p = pi->patchcode;
330 if (*p != '{')
332 msi_free( pi->patchcode );
333 msi_free( pi );
334 return ERROR_PATCH_PACKAGE_INVALID;
336 if (!(p = strchrW( p + 1, '}' )))
338 msi_free( pi->patchcode );
339 msi_free( pi );
340 return ERROR_PATCH_PACKAGE_INVALID;
342 if (p[1])
344 FIXME("patch obsoletes %s\n", debugstr_w(p + 1));
345 p[1] = 0;
347 TRACE("patch code %s\n", debugstr_w(pi->patchcode));
348 if (!(pi->products = msi_suminfo_dup_string( si, PID_TEMPLATE )))
350 msi_free( pi->patchcode );
351 msi_free( pi );
352 return ERROR_OUTOFMEMORY;
354 if (!(pi->transforms = msi_suminfo_dup_string( si, PID_LASTAUTHOR )))
356 msi_free( pi->patchcode );
357 msi_free( pi->products );
358 msi_free( pi );
359 return ERROR_OUTOFMEMORY;
361 *patch = pi;
362 return r;
365 static UINT patch_set_media_source_prop( MSIPACKAGE *package )
367 static const WCHAR query[] = {
368 'S','E','L','E','C','T',' ','`','S','o','u','r','c','e','`',' ','F','R','O','M',' ',
369 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ','`','S','o','u','r','c','e','`',' ',
370 'I','S',' ','N','O','T',' ','N','U','L','L',0};
371 MSIQUERY *view;
372 MSIRECORD *rec;
373 const WCHAR *property;
374 WCHAR *patch;
375 UINT r;
377 r = MSI_DatabaseOpenViewW( package->db, query, &view );
378 if (r != ERROR_SUCCESS)
379 return r;
381 r = MSI_ViewExecute( view, 0 );
382 if (r != ERROR_SUCCESS)
383 goto done;
385 if (MSI_ViewFetch( view, &rec ) == ERROR_SUCCESS)
387 property = MSI_RecordGetString( rec, 1 );
388 patch = msi_dup_property( package->db, szPatch );
389 msi_set_property( package->db, property, patch, -1 );
390 msi_free( patch );
391 msiobj_release( &rec->hdr );
394 done:
395 msiobj_release( &view->hdr );
396 return r;
399 struct patch_offset
401 struct list entry;
402 WCHAR *name;
403 UINT sequence;
406 struct patch_offset_list
408 struct list files;
409 struct list patches;
410 UINT count, min, max;
411 UINT offset_to_apply;
414 static struct patch_offset_list *patch_offset_list_create( void )
416 struct patch_offset_list *pos = msi_alloc( sizeof(struct patch_offset_list) );
417 list_init( &pos->files );
418 list_init( &pos->patches );
419 pos->count = pos->max = 0;
420 pos->min = 999999;
421 return pos;
424 static void patch_offset_list_free( struct patch_offset_list *pos )
426 struct patch_offset *po, *po2;
428 LIST_FOR_EACH_ENTRY_SAFE( po, po2, &pos->files, struct patch_offset, entry )
430 msi_free( po->name );
431 msi_free( po );
433 LIST_FOR_EACH_ENTRY_SAFE( po, po2, &pos->patches, struct patch_offset, entry )
435 msi_free( po->name );
436 msi_free( po );
438 msi_free( pos );
441 static void patch_offset_get_filepatches( MSIDATABASE *db, UINT last_sequence, struct patch_offset_list *pos )
443 static const WCHAR query[] = {
444 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','P','a','t','c','h',' ',
445 'W','H','E','R','E',' ','S','e','q','u','e','n','c','e',' ','<','=',' ','?',' ',
446 'O','R','D','E','R',' ','B','Y',' ','S','e','q','u','e','n','c','e',0};
447 MSIQUERY *view;
448 MSIRECORD *rec;
449 UINT r;
451 r = MSI_DatabaseOpenViewW( db, query, &view );
452 if (r != ERROR_SUCCESS)
453 return;
455 rec = MSI_CreateRecord( 1 );
456 MSI_RecordSetInteger( rec, 1, last_sequence );
458 r = MSI_ViewExecute( view, rec );
459 msiobj_release( &rec->hdr );
460 if (r != ERROR_SUCCESS)
461 return;
463 while (MSI_ViewFetch( view, &rec ) == ERROR_SUCCESS)
465 struct patch_offset *po = msi_alloc( sizeof(struct patch_offset) );
467 po->name = msi_dup_record_field( rec, 1 );
468 po->sequence = MSI_RecordGetInteger( rec, 2 );
469 pos->min = min( pos->min, po->sequence );
470 pos->max = max( pos->max, po->sequence );
471 list_add_tail( &pos->patches, &po->entry );
472 pos->count++;
474 msiobj_release( &rec->hdr );
476 msiobj_release( &view->hdr );
479 static void patch_offset_get_files( MSIDATABASE *db, UINT last_sequence, struct patch_offset_list *pos )
481 static const WCHAR query[] = {
482 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','F','i','l','e',' ',
483 'W','H','E','R','E',' ','S','e','q','u','e','n','c','e',' ','<','=',' ','?',' ',
484 'O','R','D','E','R',' ','B','Y',' ','S','e','q','u','e','n','c','e',0};
485 MSIQUERY *view;
486 MSIRECORD *rec;
487 UINT r;
489 r = MSI_DatabaseOpenViewW( db, query, &view );
490 if (r != ERROR_SUCCESS)
491 return;
493 rec = MSI_CreateRecord( 1 );
494 MSI_RecordSetInteger( rec, 1, last_sequence );
496 r = MSI_ViewExecute( view, rec );
497 msiobj_release( &rec->hdr );
498 if (r != ERROR_SUCCESS)
499 return;
501 while (MSI_ViewFetch( view, &rec ) == ERROR_SUCCESS)
503 UINT attributes = MSI_RecordGetInteger( rec, 7 );
504 if (attributes & msidbFileAttributesPatchAdded)
506 struct patch_offset *po = msi_alloc( sizeof(struct patch_offset) );
508 po->name = msi_dup_record_field( rec, 1 );
509 po->sequence = MSI_RecordGetInteger( rec, 8 );
510 pos->min = min( pos->min, po->sequence );
511 pos->max = max( pos->max, po->sequence );
512 list_add_tail( &pos->files, &po->entry );
513 pos->count++;
515 msiobj_release( &rec->hdr );
517 msiobj_release( &view->hdr );
520 static UINT patch_update_file_sequence( MSIDATABASE *db, const struct patch_offset_list *pos,
521 MSIQUERY *view, MSIRECORD *rec )
523 struct patch_offset *po;
524 const WCHAR *file = MSI_RecordGetString( rec, 1 );
525 UINT r = ERROR_SUCCESS, seq = MSI_RecordGetInteger( rec, 8 );
527 LIST_FOR_EACH_ENTRY( po, &pos->files, struct patch_offset, entry )
529 if (!strcmpiW( file, po->name ))
531 MSI_RecordSetInteger( rec, 8, seq + pos->offset_to_apply );
532 r = MSI_ViewModify( view, MSIMODIFY_UPDATE, rec );
533 if (r != ERROR_SUCCESS)
534 ERR("Failed to update offset for file %s (%u)\n", debugstr_w(file), r);
535 break;
538 return r;
541 static UINT patch_update_filepatch_sequence( MSIDATABASE *db, const struct patch_offset_list *pos,
542 MSIQUERY *view, MSIRECORD *rec )
544 static const WCHAR delete_query[] = {
545 'D','E','L','E','T','E',' ','F','R','O','M',' ','`','P','a','t','c','h','`',' ',
546 'W','H','E','R','E',' ','`','F','i','l','e','_','`',' ','=',' ','?',' ',
547 'A','N','D',' ','`','S','e','q','u','e','n','c','e','`',' ','=',' ','?',0};
548 static const WCHAR insert_query[] = {
549 'I','N','S','E','R','T',' ','I','N','T','O',' ','`','P','a','t','c','h','`',' ',
550 '(','`','F','i','l','e','_','`',',','`','S','e','q','u','e','n','c','e','`',',',
551 '`','P','a','t','c','h','S','i','z','e','`',',','`','A','t','t','r','i','b','u','t','e','s','`',',',
552 '`','H','e','a','d','e','r','`',',','`','S','t','r','e','a','m','R','e','f','_','`',')',' ',
553 'V','A','L','U','E','S',' ','(','?',',','?',',','?',',','?',',','?',',','?',')',0};
554 struct patch_offset *po;
555 const WCHAR *file = MSI_RecordGetString( rec, 1 );
556 UINT r = ERROR_SUCCESS, seq = MSI_RecordGetInteger( rec, 2 );
558 LIST_FOR_EACH_ENTRY( po, &pos->patches, struct patch_offset, entry )
560 if (seq == po->sequence && !strcmpiW( file, po->name ))
562 MSIQUERY *delete_view, *insert_view;
563 MSIRECORD *rec2;
565 r = MSI_DatabaseOpenViewW( db, delete_query, &delete_view );
566 if (r != ERROR_SUCCESS) return r;
568 rec2 = MSI_CreateRecord( 2 );
569 MSI_RecordSetStringW( rec2, 1, po->name );
570 MSI_RecordSetInteger( rec2, 2, po->sequence );
571 r = MSI_ViewExecute( delete_view, rec2 );
572 msiobj_release( &delete_view->hdr );
573 msiobj_release( &rec2->hdr );
574 if (r != ERROR_SUCCESS) return r;
576 r = MSI_DatabaseOpenViewW( db, insert_query, &insert_view );
577 if (r != ERROR_SUCCESS) return r;
579 MSI_RecordSetInteger( rec, 2, po->sequence + pos->offset_to_apply );
581 r = MSI_ViewExecute( insert_view, rec );
582 msiobj_release( &insert_view->hdr );
583 if (r != ERROR_SUCCESS)
584 ERR("Failed to update offset for filepatch %s (%u)\n", debugstr_w(file), r);
585 break;
588 return r;
591 static UINT patch_offset_modify_db( MSIDATABASE *db, struct patch_offset_list *pos )
593 static const WCHAR file_query[] = {
594 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','F','i','l','e','`',' ',
595 'W','H','E','R','E',' ','`','S','e','q','u','e','n','c','e','`',' ','>','=',' ','?',' ',
596 'A','N','D',' ','`','S','e','q','u','e','n','c','e','`',' ','<','=',' ','?',' ',
597 'O','R','D','E','R',' ','B','Y',' ','`','S','e','q','u','e','n','c','e','`',0};
598 static const WCHAR patch_query[] = {
599 'S','E','L','E','C','T',' ','*','F','R','O','M',' ','`','P','a','t','c','h','`',' ',
600 'W','H','E','R','E',' ','`','S','e','q','u','e','n','c','e','`',' ','>','=',' ','?',' ',
601 'A','N','D',' ','`','S','e','q','u','e','n','c','e','`',' ','<','=',' ','?',' ',
602 'O','R','D','E','R',' ','B','Y',' ','`','S','e','q','u','e','n','c','e','`',0};
603 MSIRECORD *rec;
604 MSIQUERY *view;
605 UINT r, min = pos->min, max = pos->max, r_fetch;
607 r = MSI_DatabaseOpenViewW( db, file_query, &view );
608 if (r != ERROR_SUCCESS)
609 return ERROR_SUCCESS;
611 rec = MSI_CreateRecord( 2 );
612 MSI_RecordSetInteger( rec, 1, min );
613 MSI_RecordSetInteger( rec, 2, max );
615 r = MSI_ViewExecute( view, rec );
616 msiobj_release( &rec->hdr );
617 if (r != ERROR_SUCCESS)
618 goto done;
620 while ((r_fetch = MSI_ViewFetch( view, &rec )) == ERROR_SUCCESS)
622 r = patch_update_file_sequence( db, pos, view, rec );
623 msiobj_release( &rec->hdr );
624 if (r != ERROR_SUCCESS) goto done;
626 msiobj_release( &view->hdr );
628 r = MSI_DatabaseOpenViewW( db, patch_query, &view );
629 if (r != ERROR_SUCCESS)
630 return ERROR_SUCCESS;
632 rec = MSI_CreateRecord( 2 );
633 MSI_RecordSetInteger( rec, 1, min );
634 MSI_RecordSetInteger( rec, 2, max );
636 r = MSI_ViewExecute( view, rec );
637 msiobj_release( &rec->hdr );
638 if (r != ERROR_SUCCESS)
639 goto done;
641 while ((r_fetch = MSI_ViewFetch( view, &rec )) == ERROR_SUCCESS)
643 r = patch_update_filepatch_sequence( db, pos, view, rec );
644 msiobj_release( &rec->hdr );
645 if (r != ERROR_SUCCESS) goto done;
648 done:
649 msiobj_release( &view->hdr );
650 return r;
653 static const WCHAR patch_media_query[] = {
654 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
655 'W','H','E','R','E',' ','`','S','o','u','r','c','e','`',' ','I','S',' ','N','O','T',' ','N','U','L','L',' ',
656 'A','N','D',' ','`','C','a','b','i','n','e','t','`',' ','I','S',' ','N','O','T',' ','N','U','L','L',' ',
657 'O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
659 struct patch_media
661 struct list entry;
662 UINT disk_id;
663 UINT last_sequence;
664 WCHAR *prompt;
665 WCHAR *cabinet;
666 WCHAR *volume;
667 WCHAR *source;
670 static UINT patch_add_media( MSIPACKAGE *package, IStorage *storage, MSIPATCHINFO *patch )
672 static const WCHAR delete_query[] = {
673 'D','E','L','E','T','E',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
674 'W','H','E','R','E',' ','`','D','i','s','k','I','d','`','=','?',0};
675 static const WCHAR insert_query[] = {
676 'I','N','S','E','R','T',' ','I','N','T','O',' ','`','M','e','d','i','a','`',' ',
677 '(','`','D','i','s','k','I','d','`',',','`','L','a','s','t','S','e','q','u','e','n','c','e','`',',',
678 '`','D','i','s','k','P','r','o','m','p','t','`',',','`','C','a','b','i','n','e','t','`',',',
679 '`','V','o','l','u','m','e','L','a','b','e','l','`',',','`','S','o','u','r','c','e','`',')',' ',
680 'V','A','L','U','E','S',' ','(','?',',','?',',','?',',','?',',','?',',','?',')',0};
681 MSIQUERY *view;
682 MSIRECORD *rec;
683 UINT r, disk_id;
684 struct list media_list;
685 struct patch_media *media, *next;
687 r = MSI_DatabaseOpenViewW( package->db, patch_media_query, &view );
688 if (r != ERROR_SUCCESS) return r;
690 r = MSI_ViewExecute( view, 0 );
691 if (r != ERROR_SUCCESS)
693 msiobj_release( &view->hdr );
694 TRACE("query failed %u\n", r);
695 return r;
697 list_init( &media_list );
698 while (MSI_ViewFetch( view, &rec ) == ERROR_SUCCESS)
700 disk_id = MSI_RecordGetInteger( rec, 1 );
701 TRACE("disk_id %u\n", disk_id);
702 if (disk_id >= MSI_INITIAL_MEDIA_TRANSFORM_DISKID)
704 msiobj_release( &rec->hdr );
705 continue;
707 if (!(media = msi_alloc( sizeof( *media )))) {
708 msiobj_release( &rec->hdr );
709 goto done;
711 media->disk_id = disk_id;
712 media->last_sequence = MSI_RecordGetInteger( rec, 2 );
713 media->prompt = msi_dup_record_field( rec, 3 );
714 media->cabinet = msi_dup_record_field( rec, 4 );
715 media->volume = msi_dup_record_field( rec, 5 );
716 media->source = msi_dup_record_field( rec, 6 );
718 list_add_tail( &media_list, &media->entry );
719 msiobj_release( &rec->hdr );
721 LIST_FOR_EACH_ENTRY( media, &media_list, struct patch_media, entry )
723 MSIQUERY *delete_view, *insert_view;
725 r = MSI_DatabaseOpenViewW( package->db, delete_query, &delete_view );
726 if (r != ERROR_SUCCESS) goto done;
728 rec = MSI_CreateRecord( 1 );
729 MSI_RecordSetInteger( rec, 1, media->disk_id );
731 r = MSI_ViewExecute( delete_view, rec );
732 msiobj_release( &delete_view->hdr );
733 msiobj_release( &rec->hdr );
734 if (r != ERROR_SUCCESS) goto done;
736 r = MSI_DatabaseOpenViewW( package->db, insert_query, &insert_view );
737 if (r != ERROR_SUCCESS) goto done;
739 disk_id = package->db->media_transform_disk_id;
740 TRACE("disk id %u\n", disk_id);
741 TRACE("last sequence %u\n", media->last_sequence);
742 TRACE("prompt %s\n", debugstr_w(media->prompt));
743 TRACE("cabinet %s\n", debugstr_w(media->cabinet));
744 TRACE("volume %s\n", debugstr_w(media->volume));
745 TRACE("source %s\n", debugstr_w(media->source));
747 rec = MSI_CreateRecord( 6 );
748 MSI_RecordSetInteger( rec, 1, disk_id );
749 MSI_RecordSetInteger( rec, 2, media->last_sequence );
750 MSI_RecordSetStringW( rec, 3, media->prompt );
751 MSI_RecordSetStringW( rec, 4, media->cabinet );
752 MSI_RecordSetStringW( rec, 5, media->volume );
753 MSI_RecordSetStringW( rec, 6, media->source );
755 r = MSI_ViewExecute( insert_view, rec );
756 msiobj_release( &insert_view->hdr );
757 msiobj_release( &rec->hdr );
758 if (r != ERROR_SUCCESS) goto done;
760 r = msi_add_cabinet_stream( package, disk_id, storage, media->cabinet );
761 if (r != ERROR_SUCCESS) ERR("failed to add cabinet stream %u\n", r);
762 else
764 patch->disk_id = disk_id;
765 package->db->media_transform_disk_id++;
769 done:
770 msiobj_release( &view->hdr );
771 LIST_FOR_EACH_ENTRY_SAFE( media, next, &media_list, struct patch_media, entry )
773 list_remove( &media->entry );
774 msi_free( media->prompt );
775 msi_free( media->cabinet );
776 msi_free( media->volume );
777 msi_free( media->source );
778 msi_free( media );
780 return r;
783 static UINT patch_set_offsets( MSIDATABASE *db, MSIPATCHINFO *patch )
785 MSIQUERY *view;
786 MSIRECORD *rec;
787 UINT r;
789 r = MSI_DatabaseOpenViewW( db, patch_media_query, &view );
790 if (r != ERROR_SUCCESS)
791 return r;
793 r = MSI_ViewExecute( view, 0 );
794 if (r != ERROR_SUCCESS)
795 goto done;
797 while (MSI_ViewFetch( view, &rec ) == ERROR_SUCCESS)
799 UINT offset, last_sequence = MSI_RecordGetInteger( rec, 2 );
800 struct patch_offset_list *pos;
802 /* FIXME: set/check Source field instead? */
803 if (last_sequence >= MSI_INITIAL_MEDIA_TRANSFORM_OFFSET)
805 msiobj_release( &rec->hdr );
806 continue;
808 pos = patch_offset_list_create();
809 patch_offset_get_files( db, last_sequence, pos );
810 patch_offset_get_filepatches( db, last_sequence, pos );
812 offset = db->media_transform_offset - pos->min;
813 last_sequence = offset + pos->max;
815 last_sequence += pos->min;
816 pos->offset_to_apply = offset;
817 if (pos->count)
819 r = patch_offset_modify_db( db, pos );
820 if (r != ERROR_SUCCESS)
821 ERR("Failed to set offsets, expect breakage (%u)\n", r);
823 MSI_RecordSetInteger( rec, 2, last_sequence );
824 r = MSI_ViewModify( view, MSIMODIFY_UPDATE, rec );
825 if (r != ERROR_SUCCESS)
826 ERR("Failed to update Media table entry, expect breakage (%u)\n", r);
828 db->media_transform_offset = last_sequence + 1;
830 patch_offset_list_free( pos );
831 msiobj_release( &rec->hdr );
834 done:
835 msiobj_release( &view->hdr );
836 return r;
839 static UINT msi_apply_patch_db( MSIPACKAGE *package, MSIDATABASE *patch_db, MSIPATCHINFO *patch )
841 UINT i, r = ERROR_SUCCESS;
842 WCHAR **substorage;
844 /* apply substorage transforms */
845 substorage = msi_split_string( patch->transforms, ';' );
846 for (i = 0; substorage && substorage[i] && r == ERROR_SUCCESS; i++)
848 r = apply_substorage_transform( package, patch_db, substorage[i] );
849 if (r == ERROR_SUCCESS)
851 r = patch_set_offsets( package->db, patch );
852 if (r == ERROR_SUCCESS)
853 r = patch_add_media( package, patch_db->storage, patch );
856 msi_free( substorage );
857 if (r != ERROR_SUCCESS)
858 return r;
860 r = patch_set_media_source_prop( package );
861 if (r != ERROR_SUCCESS)
862 return r;
864 patch->state = MSIPATCHSTATE_APPLIED;
865 list_add_tail( &package->patches, &patch->entry );
866 return ERROR_SUCCESS;
869 void msi_free_patchinfo( MSIPATCHINFO *patch )
871 msi_free( patch->patchcode );
872 msi_free( patch->products );
873 msi_free( patch->transforms );
874 msi_free( patch->filename );
875 msi_free( patch->localfile );
876 msi_free( patch );
879 static UINT msi_apply_patch_package( MSIPACKAGE *package, const WCHAR *file )
881 static const WCHAR dotmsp[] = {'.','m','s','p',0};
882 MSIDATABASE *patch_db = NULL;
883 WCHAR localfile[MAX_PATH];
884 MSISUMMARYINFO *si;
885 MSIPATCHINFO *patch = NULL;
886 UINT r;
888 TRACE("%p, %s\n", package, debugstr_w(file));
890 r = MSI_OpenDatabaseW( file, MSIDBOPEN_READONLY + MSIDBOPEN_PATCHFILE, &patch_db );
891 if (r != ERROR_SUCCESS)
893 ERR("failed to open patch collection %s\n", debugstr_w( file ) );
894 return r;
896 r = msi_get_suminfo( patch_db->storage, 0, &si );
897 if (r != ERROR_SUCCESS)
899 msiobj_release( &patch_db->hdr );
900 return r;
902 r = msi_check_patch_applicable( package, si );
903 if (r != ERROR_SUCCESS)
905 TRACE("patch not applicable\n");
906 r = ERROR_SUCCESS;
907 goto done;
909 r = msi_parse_patch_summary( si, &patch );
910 if ( r != ERROR_SUCCESS )
911 goto done;
913 r = msi_create_empty_local_file( localfile, dotmsp );
914 if ( r != ERROR_SUCCESS )
915 goto done;
917 r = ERROR_OUTOFMEMORY;
918 patch->registered = FALSE;
919 if (!(patch->filename = strdupW( file ))) goto done;
920 if (!(patch->localfile = strdupW( localfile ))) goto done;
922 r = msi_apply_patch_db( package, patch_db, patch );
923 if (r != ERROR_SUCCESS) WARN("patch failed to apply %u\n", r);
925 done:
926 msiobj_release( &si->hdr );
927 msiobj_release( &patch_db->hdr );
928 if (patch && r != ERROR_SUCCESS)
930 DeleteFileW( patch->localfile );
931 msi_free_patchinfo( patch );
933 return r;
936 /* get the PATCH property, and apply all the patches it specifies */
937 UINT msi_apply_patches( MSIPACKAGE *package )
939 LPWSTR patch_list, *patches;
940 UINT i, r = ERROR_SUCCESS;
942 patch_list = msi_dup_property( package->db, szPatch );
944 TRACE("patches to be applied: %s\n", debugstr_w(patch_list));
946 patches = msi_split_string( patch_list, ';' );
947 for (i = 0; patches && patches[i] && r == ERROR_SUCCESS; i++)
948 r = msi_apply_patch_package( package, patches[i] );
950 msi_free( patches );
951 msi_free( patch_list );
952 return r;
955 UINT msi_apply_transforms( MSIPACKAGE *package )
957 static const WCHAR szTransforms[] = {'T','R','A','N','S','F','O','R','M','S',0};
958 LPWSTR xform_list, *xforms;
959 UINT i, r = ERROR_SUCCESS;
961 xform_list = msi_dup_property( package->db, szTransforms );
962 xforms = msi_split_string( xform_list, ';' );
964 for (i = 0; xforms && xforms[i] && r == ERROR_SUCCESS; i++)
966 if (xforms[i][0] == ':')
967 r = apply_substorage_transform( package, package->db, xforms[i] );
968 else
970 WCHAR *transform;
972 if (!PathIsRelativeW( xforms[i] )) transform = xforms[i];
973 else
975 WCHAR *p = strrchrW( package->PackagePath, '\\' );
976 DWORD len = p - package->PackagePath + 1;
978 if (!(transform = msi_alloc( (len + strlenW( xforms[i] ) + 1) * sizeof(WCHAR)) ))
980 msi_free( xforms );
981 msi_free( xform_list );
982 return ERROR_OUTOFMEMORY;
984 memcpy( transform, package->PackagePath, len * sizeof(WCHAR) );
985 memcpy( transform + len, xforms[i], (strlenW( xforms[i] ) + 1) * sizeof(WCHAR) );
987 r = MSI_DatabaseApplyTransformW( package->db, transform, 0 );
988 if (transform != xforms[i]) msi_free( transform );
991 msi_free( xforms );
992 msi_free( xform_list );
993 return r;
996 UINT msi_apply_registered_patch( MSIPACKAGE *package, LPCWSTR patch_code )
998 UINT r;
999 DWORD len;
1000 WCHAR patch_file[MAX_PATH];
1001 MSIDATABASE *patch_db;
1002 MSIPATCHINFO *patch_info;
1003 MSISUMMARYINFO *si;
1005 TRACE("%p, %s\n", package, debugstr_w(patch_code));
1007 len = sizeof(patch_file) / sizeof(WCHAR);
1008 r = MsiGetPatchInfoExW( patch_code, package->ProductCode, NULL, package->Context,
1009 INSTALLPROPERTY_LOCALPACKAGEW, patch_file, &len );
1010 if (r != ERROR_SUCCESS)
1012 ERR("failed to get patch filename %u\n", r);
1013 return r;
1015 r = MSI_OpenDatabaseW( patch_file, MSIDBOPEN_READONLY + MSIDBOPEN_PATCHFILE, &patch_db );
1016 if (r != ERROR_SUCCESS)
1018 ERR("failed to open patch database %s\n", debugstr_w( patch_file ));
1019 return r;
1021 r = msi_get_suminfo( patch_db->storage, 0, &si );
1022 if (r != ERROR_SUCCESS)
1024 msiobj_release( &patch_db->hdr );
1025 return r;
1027 r = msi_parse_patch_summary( si, &patch_info );
1028 msiobj_release( &si->hdr );
1029 if (r != ERROR_SUCCESS)
1031 ERR("failed to parse patch summary %u\n", r);
1032 msiobj_release( &patch_db->hdr );
1033 return r;
1035 patch_info->registered = TRUE;
1036 patch_info->localfile = strdupW( patch_file );
1037 if (!patch_info->localfile)
1039 msiobj_release( &patch_db->hdr );
1040 msi_free_patchinfo( patch_info );
1041 return ERROR_OUTOFMEMORY;
1043 r = msi_apply_patch_db( package, patch_db, patch_info );
1044 msiobj_release( &patch_db->hdr );
1045 if (r != ERROR_SUCCESS)
1047 ERR("failed to apply patch %u\n", r);
1048 msi_free_patchinfo( patch_info );
1050 return r;