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
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
35 static BOOL
match_language( MSIPACKAGE
*package
, LANGID langid
)
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
;
49 WCHAR
*product_code_from
;
50 WCHAR
*product_code_to
;
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
);
66 static struct transform_desc
*parse_transform_desc( const WCHAR
*str
)
68 struct transform_desc
*ret
;
69 const WCHAR
*p
= str
, *q
;
72 if (!(ret
= msi_alloc_zero( sizeof(*ret
) ))) return NULL
;
74 q
= strchrW( p
, '}' );
75 if (*p
!= '{' || !q
) goto error
;
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;
83 if (!(q
= strchrW( p
, ';' ))) goto error
;
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;
90 q
= strchrW( p
, '}' );
91 if (*p
!= '{' || !q
) goto error
;
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;
99 if (!(q
= strchrW( p
, ';' ))) goto error
;
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;
106 q
= strchrW( p
, '}' );
107 if (*p
!= '{' || !q
) goto error
;
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;
117 free_transform_desc( ret
);
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
;
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");
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 if (wanted_flags
& ~supported_flags
)
144 FIXME("unsupported validation flags 0x%04x\n", wanted_flags
);
145 msiobj_release( &si
->hdr
);
146 return ERROR_FUNCTION_FAILED
;
148 if (!(template = msi_suminfo_dup_string( si
, PID_TEMPLATE
)))
150 WARN("no template property!\n");
151 msiobj_release( &si
->hdr
);
152 return ERROR_FUNCTION_FAILED
;
154 TRACE("template property: %s\n", debugstr_w(template));
155 if (!(product
= msi_get_suminfo_product( transform
)))
157 WARN("no product property!\n");
158 msi_free( template );
159 msiobj_release( &si
->hdr
);
160 return ERROR_FUNCTION_FAILED
;
162 TRACE("product property: %s\n", debugstr_w(product
));
163 if (!(desc
= parse_transform_desc( product
)))
165 msi_free( template );
166 msiobj_release( &si
->hdr
);
167 return ERROR_FUNCTION_FAILED
;
171 if (wanted_flags
& MSITRANSFORM_VALIDATE_LANGUAGE
)
173 if (!template[0] || ((p
= strchrW( template, ';' )) && match_language( package
, atoiW( p
+ 1 ) )))
175 valid_flags
|= MSITRANSFORM_VALIDATE_LANGUAGE
;
178 if (wanted_flags
& MSITRANSFORM_VALIDATE_PRODUCT
)
180 WCHAR
*product_code_installed
= msi_dup_property( package
->db
, szProductCode
);
182 if (!product_code_installed
)
184 msi_free( template );
185 free_transform_desc( desc
);
186 msiobj_release( &si
->hdr
);
187 return ERROR_INSTALL_PACKAGE_INVALID
;
189 if (!strcmpW( desc
->product_code_from
, product_code_installed
))
191 valid_flags
|= MSITRANSFORM_VALIDATE_PRODUCT
;
193 msi_free( product_code_installed
);
195 if (wanted_flags
& MSITRANSFORM_VALIDATE_PLATFORM
)
197 if ((p
= strchrW( template, ';' )))
200 if (package
->platform
== parse_platform( template ))
201 valid_flags
|= MSITRANSFORM_VALIDATE_PLATFORM
;
204 msi_free( template );
205 if (wanted_flags
& MSITRANSFORM_VALIDATE_MAJORVERSION
)
207 WCHAR
*product_version_installed
= msi_dup_property( package
->db
, szProductVersion
);
208 DWORD major_installed
, minor_installed
, major
, minor
;
210 if (!product_version_installed
)
212 free_transform_desc( desc
);
213 msiobj_release( &si
->hdr
);
214 return ERROR_INSTALL_PACKAGE_INVALID
;
216 msi_parse_version_string( product_version_installed
, &major_installed
, &minor_installed
);
217 msi_parse_version_string( desc
->version_from
, &major
, &minor
);
219 if (major_installed
== major
)
221 valid_flags
|= MSITRANSFORM_VALIDATE_MAJORVERSION
;
222 wanted_flags
&= ~MSITRANSFORM_VALIDATE_MINORVERSION
;
224 msi_free( product_version_installed
);
226 else if (wanted_flags
& MSITRANSFORM_VALIDATE_MINORVERSION
)
228 WCHAR
*product_version_installed
= msi_dup_property( package
->db
, szProductVersion
);
229 DWORD major_installed
, minor_installed
, major
, minor
;
231 if (!product_version_installed
)
233 free_transform_desc( desc
);
234 msiobj_release( &si
->hdr
);
235 return ERROR_INSTALL_PACKAGE_INVALID
;
237 msi_parse_version_string( product_version_installed
, &major_installed
, &minor_installed
);
238 msi_parse_version_string( desc
->version_from
, &major
, &minor
);
240 if (major_installed
== major
&& minor_installed
== minor
)
241 valid_flags
|= MSITRANSFORM_VALIDATE_MINORVERSION
;
242 msi_free( product_version_installed
);
244 if (wanted_flags
& MSITRANSFORM_VALIDATE_UPGRADECODE
)
246 WCHAR
*upgrade_code_installed
= msi_dup_property( package
->db
, szUpgradeCode
);
248 if (!upgrade_code_installed
)
250 free_transform_desc( desc
);
251 msiobj_release( &si
->hdr
);
252 return ERROR_INSTALL_PACKAGE_INVALID
;
254 if (!strcmpW( desc
->upgrade_code
, upgrade_code_installed
))
255 valid_flags
|= MSITRANSFORM_VALIDATE_UPGRADECODE
;
256 msi_free( upgrade_code_installed
);
259 free_transform_desc( desc
);
260 msiobj_release( &si
->hdr
);
261 if ((valid_flags
& wanted_flags
) != wanted_flags
) return ERROR_FUNCTION_FAILED
;
262 TRACE("applicable transform\n");
263 return ERROR_SUCCESS
;
266 static UINT
apply_substorage_transform( MSIPACKAGE
*package
, MSIDATABASE
*patch_db
, LPCWSTR name
)
268 UINT ret
= ERROR_FUNCTION_FAILED
;
269 IStorage
*stg
= NULL
;
272 TRACE("%p %s\n", package
, debugstr_w(name
));
276 ERR("expected a colon in %s\n", debugstr_w(name
));
277 return ERROR_FUNCTION_FAILED
;
279 r
= IStorage_OpenStorage( patch_db
->storage
, name
, NULL
, STGM_SHARE_EXCLUSIVE
, NULL
, 0, &stg
);
282 ret
= check_transform_applicable( package
, stg
);
283 if (ret
== ERROR_SUCCESS
)
284 msi_table_apply_transform( package
->db
, stg
);
286 TRACE("substorage transform %s wasn't applicable\n", debugstr_w(name
));
287 IStorage_Release( stg
);
291 ERR("failed to open substorage %s\n", debugstr_w(name
));
293 return ERROR_SUCCESS
;
296 UINT
msi_check_patch_applicable( MSIPACKAGE
*package
, MSISUMMARYINFO
*si
)
298 LPWSTR guid_list
, *guids
, product_code
;
299 UINT i
, ret
= ERROR_FUNCTION_FAILED
;
301 product_code
= msi_dup_property( package
->db
, szProductCode
);
304 /* FIXME: the property ProductCode should be written into the DB somewhere */
305 ERR("no product code to check\n");
306 return ERROR_SUCCESS
;
308 guid_list
= msi_suminfo_dup_string( si
, PID_TEMPLATE
);
309 guids
= msi_split_string( guid_list
, ';' );
310 for (i
= 0; guids
[i
] && ret
!= ERROR_SUCCESS
; i
++)
312 if (!strcmpW( guids
[i
], product_code
)) ret
= ERROR_SUCCESS
;
315 msi_free( guid_list
);
316 msi_free( product_code
);
320 static UINT
msi_parse_patch_summary( MSISUMMARYINFO
*si
, MSIPATCHINFO
**patch
)
323 UINT r
= ERROR_SUCCESS
;
326 if (!(pi
= msi_alloc_zero( sizeof(MSIPATCHINFO
) )))
328 return ERROR_OUTOFMEMORY
;
330 if (!(pi
->patchcode
= msi_suminfo_dup_string( si
, PID_REVNUMBER
)))
333 return ERROR_OUTOFMEMORY
;
338 msi_free( pi
->patchcode
);
340 return ERROR_PATCH_PACKAGE_INVALID
;
342 if (!(p
= strchrW( p
+ 1, '}' )))
344 msi_free( pi
->patchcode
);
346 return ERROR_PATCH_PACKAGE_INVALID
;
350 FIXME("patch obsoletes %s\n", debugstr_w(p
+ 1));
353 TRACE("patch code %s\n", debugstr_w(pi
->patchcode
));
354 if (!(pi
->products
= msi_suminfo_dup_string( si
, PID_TEMPLATE
)))
356 msi_free( pi
->patchcode
);
358 return ERROR_OUTOFMEMORY
;
360 if (!(pi
->transforms
= msi_suminfo_dup_string( si
, PID_LASTAUTHOR
)))
362 msi_free( pi
->patchcode
);
363 msi_free( pi
->products
);
365 return ERROR_OUTOFMEMORY
;
371 static UINT
patch_set_media_source_prop( MSIPACKAGE
*package
)
373 static const WCHAR query
[] = {
374 'S','E','L','E','C','T',' ','`','S','o','u','r','c','e','`',' ','F','R','O','M',' ',
375 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ','`','S','o','u','r','c','e','`',' ',
376 'I','S',' ','N','O','T',' ','N','U','L','L',0};
379 const WCHAR
*property
;
383 r
= MSI_DatabaseOpenViewW( package
->db
, query
, &view
);
384 if (r
!= ERROR_SUCCESS
)
387 r
= MSI_ViewExecute( view
, 0 );
388 if (r
!= ERROR_SUCCESS
)
391 if (MSI_ViewFetch( view
, &rec
) == ERROR_SUCCESS
)
393 property
= MSI_RecordGetString( rec
, 1 );
394 patch
= msi_dup_property( package
->db
, szPatch
);
395 msi_set_property( package
->db
, property
, patch
, -1 );
397 msiobj_release( &rec
->hdr
);
401 msiobj_release( &view
->hdr
);
412 struct patch_offset_list
416 UINT count
, min
, max
;
417 UINT offset_to_apply
;
420 static struct patch_offset_list
*patch_offset_list_create( void )
422 struct patch_offset_list
*pos
= msi_alloc( sizeof(struct patch_offset_list
) );
423 list_init( &pos
->files
);
424 list_init( &pos
->patches
);
425 pos
->count
= pos
->max
= 0;
430 static void patch_offset_list_free( struct patch_offset_list
*pos
)
432 struct patch_offset
*po
, *po2
;
434 LIST_FOR_EACH_ENTRY_SAFE( po
, po2
, &pos
->files
, struct patch_offset
, entry
)
436 msi_free( po
->name
);
439 LIST_FOR_EACH_ENTRY_SAFE( po
, po2
, &pos
->patches
, struct patch_offset
, entry
)
441 msi_free( po
->name
);
447 static void patch_offset_get_filepatches( MSIDATABASE
*db
, UINT last_sequence
, struct patch_offset_list
*pos
)
449 static const WCHAR query
[] = {
450 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','P','a','t','c','h',' ',
451 'W','H','E','R','E',' ','S','e','q','u','e','n','c','e',' ','<','=',' ','?',' ',
452 'O','R','D','E','R',' ','B','Y',' ','S','e','q','u','e','n','c','e',0};
457 r
= MSI_DatabaseOpenViewW( db
, query
, &view
);
458 if (r
!= ERROR_SUCCESS
)
461 rec
= MSI_CreateRecord( 1 );
462 MSI_RecordSetInteger( rec
, 1, last_sequence
);
464 r
= MSI_ViewExecute( view
, rec
);
465 msiobj_release( &rec
->hdr
);
466 if (r
!= ERROR_SUCCESS
)
469 while (MSI_ViewFetch( view
, &rec
) == ERROR_SUCCESS
)
471 struct patch_offset
*po
= msi_alloc( sizeof(struct patch_offset
) );
473 po
->name
= msi_dup_record_field( rec
, 1 );
474 po
->sequence
= MSI_RecordGetInteger( rec
, 2 );
475 pos
->min
= min( pos
->min
, po
->sequence
);
476 pos
->max
= max( pos
->max
, po
->sequence
);
477 list_add_tail( &pos
->patches
, &po
->entry
);
480 msiobj_release( &rec
->hdr
);
482 msiobj_release( &view
->hdr
);
485 static void patch_offset_get_files( MSIDATABASE
*db
, UINT last_sequence
, struct patch_offset_list
*pos
)
487 static const WCHAR query
[] = {
488 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','F','i','l','e',' ',
489 'W','H','E','R','E',' ','S','e','q','u','e','n','c','e',' ','<','=',' ','?',' ',
490 'O','R','D','E','R',' ','B','Y',' ','S','e','q','u','e','n','c','e',0};
495 r
= MSI_DatabaseOpenViewW( db
, query
, &view
);
496 if (r
!= ERROR_SUCCESS
)
499 rec
= MSI_CreateRecord( 1 );
500 MSI_RecordSetInteger( rec
, 1, last_sequence
);
502 r
= MSI_ViewExecute( view
, rec
);
503 msiobj_release( &rec
->hdr
);
504 if (r
!= ERROR_SUCCESS
)
507 while (MSI_ViewFetch( view
, &rec
) == ERROR_SUCCESS
)
509 UINT attributes
= MSI_RecordGetInteger( rec
, 7 );
510 if (attributes
& msidbFileAttributesPatchAdded
)
512 struct patch_offset
*po
= msi_alloc( sizeof(struct patch_offset
) );
514 po
->name
= msi_dup_record_field( rec
, 1 );
515 po
->sequence
= MSI_RecordGetInteger( rec
, 8 );
516 pos
->min
= min( pos
->min
, po
->sequence
);
517 pos
->max
= max( pos
->max
, po
->sequence
);
518 list_add_tail( &pos
->files
, &po
->entry
);
521 msiobj_release( &rec
->hdr
);
523 msiobj_release( &view
->hdr
);
526 static UINT
patch_update_file_sequence( MSIDATABASE
*db
, const struct patch_offset_list
*pos
,
527 MSIQUERY
*view
, MSIRECORD
*rec
)
529 struct patch_offset
*po
;
530 const WCHAR
*file
= MSI_RecordGetString( rec
, 1 );
531 UINT r
= ERROR_SUCCESS
, seq
= MSI_RecordGetInteger( rec
, 8 );
533 LIST_FOR_EACH_ENTRY( po
, &pos
->files
, struct patch_offset
, entry
)
535 if (!strcmpiW( file
, po
->name
))
537 MSI_RecordSetInteger( rec
, 8, seq
+ pos
->offset_to_apply
);
538 r
= MSI_ViewModify( view
, MSIMODIFY_UPDATE
, rec
);
539 if (r
!= ERROR_SUCCESS
)
540 ERR("Failed to update offset for file %s (%u)\n", debugstr_w(file
), r
);
547 static UINT
patch_update_filepatch_sequence( MSIDATABASE
*db
, const struct patch_offset_list
*pos
,
548 MSIQUERY
*view
, MSIRECORD
*rec
)
550 static const WCHAR delete_query
[] = {
551 'D','E','L','E','T','E',' ','F','R','O','M',' ','`','P','a','t','c','h','`',' ',
552 'W','H','E','R','E',' ','`','F','i','l','e','_','`',' ','=',' ','?',' ',
553 'A','N','D',' ','`','S','e','q','u','e','n','c','e','`',' ','=',' ','?',0};
554 static const WCHAR insert_query
[] = {
555 'I','N','S','E','R','T',' ','I','N','T','O',' ','`','P','a','t','c','h','`',' ',
556 '(','`','F','i','l','e','_','`',',','`','S','e','q','u','e','n','c','e','`',',',
557 '`','P','a','t','c','h','S','i','z','e','`',',','`','A','t','t','r','i','b','u','t','e','s','`',',',
558 '`','H','e','a','d','e','r','`',',','`','S','t','r','e','a','m','R','e','f','_','`',')',' ',
559 'V','A','L','U','E','S',' ','(','?',',','?',',','?',',','?',',','?',',','?',')',0};
560 struct patch_offset
*po
;
561 const WCHAR
*file
= MSI_RecordGetString( rec
, 1 );
562 UINT r
= ERROR_SUCCESS
, seq
= MSI_RecordGetInteger( rec
, 2 );
564 LIST_FOR_EACH_ENTRY( po
, &pos
->patches
, struct patch_offset
, entry
)
566 if (seq
== po
->sequence
&& !strcmpiW( file
, po
->name
))
568 MSIQUERY
*delete_view
, *insert_view
;
571 r
= MSI_DatabaseOpenViewW( db
, delete_query
, &delete_view
);
572 if (r
!= ERROR_SUCCESS
) return r
;
574 rec2
= MSI_CreateRecord( 2 );
575 MSI_RecordSetStringW( rec2
, 1, po
->name
);
576 MSI_RecordSetInteger( rec2
, 2, po
->sequence
);
577 r
= MSI_ViewExecute( delete_view
, rec2
);
578 msiobj_release( &delete_view
->hdr
);
579 msiobj_release( &rec2
->hdr
);
580 if (r
!= ERROR_SUCCESS
) return r
;
582 r
= MSI_DatabaseOpenViewW( db
, insert_query
, &insert_view
);
583 if (r
!= ERROR_SUCCESS
) return r
;
585 MSI_RecordSetInteger( rec
, 2, po
->sequence
+ pos
->offset_to_apply
);
587 r
= MSI_ViewExecute( insert_view
, rec
);
588 msiobj_release( &insert_view
->hdr
);
589 if (r
!= ERROR_SUCCESS
)
590 ERR("Failed to update offset for filepatch %s (%u)\n", debugstr_w(file
), r
);
597 static UINT
patch_offset_modify_db( MSIDATABASE
*db
, struct patch_offset_list
*pos
)
599 static const WCHAR file_query
[] = {
600 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','F','i','l','e','`',' ',
601 'W','H','E','R','E',' ','`','S','e','q','u','e','n','c','e','`',' ','>','=',' ','?',' ',
602 'A','N','D',' ','`','S','e','q','u','e','n','c','e','`',' ','<','=',' ','?',' ',
603 'O','R','D','E','R',' ','B','Y',' ','`','S','e','q','u','e','n','c','e','`',0};
604 static const WCHAR patch_query
[] = {
605 'S','E','L','E','C','T',' ','*','F','R','O','M',' ','`','P','a','t','c','h','`',' ',
606 'W','H','E','R','E',' ','`','S','e','q','u','e','n','c','e','`',' ','>','=',' ','?',' ',
607 'A','N','D',' ','`','S','e','q','u','e','n','c','e','`',' ','<','=',' ','?',' ',
608 'O','R','D','E','R',' ','B','Y',' ','`','S','e','q','u','e','n','c','e','`',0};
611 UINT r
, min
= pos
->min
, max
= pos
->max
, r_fetch
;
613 r
= MSI_DatabaseOpenViewW( db
, file_query
, &view
);
614 if (r
!= ERROR_SUCCESS
)
615 return ERROR_SUCCESS
;
617 rec
= MSI_CreateRecord( 2 );
618 MSI_RecordSetInteger( rec
, 1, min
);
619 MSI_RecordSetInteger( rec
, 2, max
);
621 r
= MSI_ViewExecute( view
, rec
);
622 msiobj_release( &rec
->hdr
);
623 if (r
!= ERROR_SUCCESS
)
626 while ((r_fetch
= MSI_ViewFetch( view
, &rec
)) == ERROR_SUCCESS
)
628 r
= patch_update_file_sequence( db
, pos
, view
, rec
);
629 msiobj_release( &rec
->hdr
);
630 if (r
!= ERROR_SUCCESS
) goto done
;
632 msiobj_release( &view
->hdr
);
634 r
= MSI_DatabaseOpenViewW( db
, patch_query
, &view
);
635 if (r
!= ERROR_SUCCESS
)
636 return ERROR_SUCCESS
;
638 rec
= MSI_CreateRecord( 2 );
639 MSI_RecordSetInteger( rec
, 1, min
);
640 MSI_RecordSetInteger( rec
, 2, max
);
642 r
= MSI_ViewExecute( view
, rec
);
643 msiobj_release( &rec
->hdr
);
644 if (r
!= ERROR_SUCCESS
)
647 while ((r_fetch
= MSI_ViewFetch( view
, &rec
)) == ERROR_SUCCESS
)
649 r
= patch_update_filepatch_sequence( db
, pos
, view
, rec
);
650 msiobj_release( &rec
->hdr
);
651 if (r
!= ERROR_SUCCESS
) goto done
;
655 msiobj_release( &view
->hdr
);
659 static const WCHAR patch_media_query
[] = {
660 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
661 'W','H','E','R','E',' ','`','S','o','u','r','c','e','`',' ','I','S',' ','N','O','T',' ','N','U','L','L',' ',
662 'A','N','D',' ','`','C','a','b','i','n','e','t','`',' ','I','S',' ','N','O','T',' ','N','U','L','L',' ',
663 'O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
676 static UINT
patch_add_media( MSIPACKAGE
*package
, IStorage
*storage
, MSIPATCHINFO
*patch
)
678 static const WCHAR delete_query
[] = {
679 'D','E','L','E','T','E',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
680 'W','H','E','R','E',' ','`','D','i','s','k','I','d','`','=','?',0};
681 static const WCHAR insert_query
[] = {
682 'I','N','S','E','R','T',' ','I','N','T','O',' ','`','M','e','d','i','a','`',' ',
683 '(','`','D','i','s','k','I','d','`',',','`','L','a','s','t','S','e','q','u','e','n','c','e','`',',',
684 '`','D','i','s','k','P','r','o','m','p','t','`',',','`','C','a','b','i','n','e','t','`',',',
685 '`','V','o','l','u','m','e','L','a','b','e','l','`',',','`','S','o','u','r','c','e','`',')',' ',
686 'V','A','L','U','E','S',' ','(','?',',','?',',','?',',','?',',','?',',','?',')',0};
690 struct list media_list
;
691 struct patch_media
*media
, *next
;
693 r
= MSI_DatabaseOpenViewW( package
->db
, patch_media_query
, &view
);
694 if (r
!= ERROR_SUCCESS
) return r
;
696 r
= MSI_ViewExecute( view
, 0 );
697 if (r
!= ERROR_SUCCESS
)
699 msiobj_release( &view
->hdr
);
700 TRACE("query failed %u\n", r
);
703 list_init( &media_list
);
704 while (MSI_ViewFetch( view
, &rec
) == ERROR_SUCCESS
)
706 disk_id
= MSI_RecordGetInteger( rec
, 1 );
707 TRACE("disk_id %u\n", disk_id
);
708 if (disk_id
>= MSI_INITIAL_MEDIA_TRANSFORM_DISKID
)
710 msiobj_release( &rec
->hdr
);
713 if (!(media
= msi_alloc( sizeof( *media
)))) goto done
;
714 media
->disk_id
= disk_id
;
715 media
->last_sequence
= MSI_RecordGetInteger( rec
, 2 );
716 media
->prompt
= msi_dup_record_field( rec
, 3 );
717 media
->cabinet
= msi_dup_record_field( rec
, 4 );
718 media
->volume
= msi_dup_record_field( rec
, 5 );
719 media
->source
= msi_dup_record_field( rec
, 6 );
721 list_add_tail( &media_list
, &media
->entry
);
722 msiobj_release( &rec
->hdr
);
724 LIST_FOR_EACH_ENTRY( media
, &media_list
, struct patch_media
, entry
)
726 MSIQUERY
*delete_view
, *insert_view
;
728 r
= MSI_DatabaseOpenViewW( package
->db
, delete_query
, &delete_view
);
729 if (r
!= ERROR_SUCCESS
) goto done
;
731 rec
= MSI_CreateRecord( 1 );
732 MSI_RecordSetInteger( rec
, 1, media
->disk_id
);
734 r
= MSI_ViewExecute( delete_view
, rec
);
735 msiobj_release( &delete_view
->hdr
);
736 msiobj_release( &rec
->hdr
);
737 if (r
!= ERROR_SUCCESS
) goto done
;
739 r
= MSI_DatabaseOpenViewW( package
->db
, insert_query
, &insert_view
);
740 if (r
!= ERROR_SUCCESS
) goto done
;
742 disk_id
= package
->db
->media_transform_disk_id
;
743 TRACE("disk id %u\n", disk_id
);
744 TRACE("last sequence %u\n", patch
->last_sequence
);
745 TRACE("prompt %s\n", debugstr_w(media
->prompt
));
746 TRACE("cabinet %s\n", debugstr_w(media
->cabinet
));
747 TRACE("volume %s\n", debugstr_w(media
->volume
));
748 TRACE("source %s\n", debugstr_w(media
->source
));
750 rec
= MSI_CreateRecord( 6 );
751 MSI_RecordSetInteger( rec
, 1, disk_id
);
752 MSI_RecordSetInteger( rec
, 2, patch
->last_sequence
);
753 MSI_RecordSetStringW( rec
, 3, media
->prompt
);
754 MSI_RecordSetStringW( rec
, 4, media
->cabinet
);
755 MSI_RecordSetStringW( rec
, 5, media
->volume
);
756 MSI_RecordSetStringW( rec
, 6, media
->source
);
758 r
= MSI_ViewExecute( insert_view
, rec
);
759 msiobj_release( &insert_view
->hdr
);
760 msiobj_release( &rec
->hdr
);
761 if (r
!= ERROR_SUCCESS
) goto done
;
763 r
= msi_add_cabinet_stream( package
, disk_id
, storage
, media
->cabinet
);
764 if (r
!= ERROR_SUCCESS
) ERR("failed to add cabinet stream %u\n", r
);
767 patch
->disk_id
= disk_id
;
768 package
->db
->media_transform_disk_id
++;
773 msiobj_release( &view
->hdr
);
774 LIST_FOR_EACH_ENTRY_SAFE( media
, next
, &media_list
, struct patch_media
, entry
)
776 list_remove( &media
->entry
);
777 msi_free( media
->prompt
);
778 msi_free( media
->cabinet
);
779 msi_free( media
->volume
);
780 msi_free( media
->source
);
786 static UINT
patch_set_offsets( MSIDATABASE
*db
, MSIPATCHINFO
*patch
)
792 r
= MSI_DatabaseOpenViewW( db
, patch_media_query
, &view
);
793 if (r
!= ERROR_SUCCESS
)
796 r
= MSI_ViewExecute( view
, 0 );
797 if (r
!= ERROR_SUCCESS
)
800 while (MSI_ViewFetch( view
, &rec
) == ERROR_SUCCESS
)
802 UINT offset
, last_sequence
= MSI_RecordGetInteger( rec
, 2 );
803 struct patch_offset_list
*pos
;
805 /* FIXME: set/check Source field instead? */
806 if (last_sequence
>= MSI_INITIAL_MEDIA_TRANSFORM_OFFSET
)
808 msiobj_release( &rec
->hdr
);
811 pos
= patch_offset_list_create();
812 patch_offset_get_files( db
, last_sequence
, pos
);
813 patch_offset_get_filepatches( db
, last_sequence
, pos
);
815 offset
= db
->media_transform_offset
- pos
->min
;
816 last_sequence
= offset
+ pos
->max
;
818 last_sequence
+= pos
->min
;
819 pos
->offset_to_apply
= offset
;
822 r
= patch_offset_modify_db( db
, pos
);
823 if (r
!= ERROR_SUCCESS
)
824 ERR("Failed to set offsets, expect breakage (%u)\n", r
);
826 MSI_RecordSetInteger( rec
, 2, last_sequence
);
827 r
= MSI_ViewModify( view
, MSIMODIFY_UPDATE
, rec
);
828 if (r
!= ERROR_SUCCESS
)
829 ERR("Failed to update Media table entry, expect breakage (%u)\n", r
);
831 patch
->last_sequence
= last_sequence
;
832 db
->media_transform_offset
= last_sequence
+ 1;
834 patch_offset_list_free( pos
);
835 msiobj_release( &rec
->hdr
);
839 msiobj_release( &view
->hdr
);
843 static UINT
msi_apply_patch_db( MSIPACKAGE
*package
, MSIDATABASE
*patch_db
, MSIPATCHINFO
*patch
)
845 UINT i
, r
= ERROR_SUCCESS
;
848 /* apply substorage transforms */
849 substorage
= msi_split_string( patch
->transforms
, ';' );
850 for (i
= 0; substorage
&& substorage
[i
] && r
== ERROR_SUCCESS
; i
++)
852 r
= apply_substorage_transform( package
, patch_db
, substorage
[i
] );
853 if (r
== ERROR_SUCCESS
)
855 r
= patch_set_offsets( package
->db
, patch
);
856 if (r
== ERROR_SUCCESS
)
857 r
= patch_add_media( package
, patch_db
->storage
, patch
);
860 msi_free( substorage
);
861 if (r
!= ERROR_SUCCESS
)
864 r
= patch_set_media_source_prop( package
);
865 if (r
!= ERROR_SUCCESS
)
868 patch
->state
= MSIPATCHSTATE_APPLIED
;
869 list_add_tail( &package
->patches
, &patch
->entry
);
870 return ERROR_SUCCESS
;
873 void msi_free_patchinfo( MSIPATCHINFO
*patch
)
875 msi_free( patch
->patchcode
);
876 msi_free( patch
->products
);
877 msi_free( patch
->transforms
);
878 msi_free( patch
->filename
);
879 msi_free( patch
->localfile
);
883 static UINT
msi_apply_patch_package( MSIPACKAGE
*package
, const WCHAR
*file
)
885 static const WCHAR dotmsp
[] = {'.','m','s','p',0};
886 MSIDATABASE
*patch_db
= NULL
;
887 WCHAR localfile
[MAX_PATH
];
889 MSIPATCHINFO
*patch
= NULL
;
892 TRACE("%p, %s\n", package
, debugstr_w(file
));
894 r
= MSI_OpenDatabaseW( file
, MSIDBOPEN_READONLY
+ MSIDBOPEN_PATCHFILE
, &patch_db
);
895 if (r
!= ERROR_SUCCESS
)
897 ERR("failed to open patch collection %s\n", debugstr_w( file
) );
900 r
= msi_get_suminfo( patch_db
->storage
, 0, &si
);
901 if (r
!= ERROR_SUCCESS
)
903 msiobj_release( &patch_db
->hdr
);
906 r
= msi_check_patch_applicable( package
, si
);
907 if (r
!= ERROR_SUCCESS
)
909 TRACE("patch not applicable\n");
913 r
= msi_parse_patch_summary( si
, &patch
);
914 if ( r
!= ERROR_SUCCESS
)
917 r
= msi_create_empty_local_file( localfile
, dotmsp
);
918 if ( r
!= ERROR_SUCCESS
)
921 r
= ERROR_OUTOFMEMORY
;
922 if (!(patch
->filename
= strdupW( file
))) goto done
;
923 if (!(patch
->localfile
= strdupW( localfile
))) goto done
;
925 r
= msi_apply_patch_db( package
, patch_db
, patch
);
926 if (r
!= ERROR_SUCCESS
) WARN("patch failed to apply %u\n", r
);
929 msiobj_release( &si
->hdr
);
930 msiobj_release( &patch_db
->hdr
);
931 if (patch
&& r
!= ERROR_SUCCESS
)
933 DeleteFileW( patch
->localfile
);
934 msi_free_patchinfo( patch
);
939 /* get the PATCH property, and apply all the patches it specifies */
940 UINT
msi_apply_patches( MSIPACKAGE
*package
)
942 LPWSTR patch_list
, *patches
;
943 UINT i
, r
= ERROR_SUCCESS
;
945 patch_list
= msi_dup_property( package
->db
, szPatch
);
947 TRACE("patches to be applied: %s\n", debugstr_w(patch_list
));
949 patches
= msi_split_string( patch_list
, ';' );
950 for (i
= 0; patches
&& patches
[i
] && r
== ERROR_SUCCESS
; i
++)
951 r
= msi_apply_patch_package( package
, patches
[i
] );
954 msi_free( patch_list
);
958 UINT
msi_apply_transforms( MSIPACKAGE
*package
)
960 static const WCHAR szTransforms
[] = {'T','R','A','N','S','F','O','R','M','S',0};
961 LPWSTR xform_list
, *xforms
;
962 UINT i
, r
= ERROR_SUCCESS
;
964 xform_list
= msi_dup_property( package
->db
, szTransforms
);
965 xforms
= msi_split_string( xform_list
, ';' );
967 for (i
= 0; xforms
&& xforms
[i
] && r
== ERROR_SUCCESS
; i
++)
969 if (xforms
[i
][0] == ':')
970 r
= apply_substorage_transform( package
, package
->db
, xforms
[i
] );
975 if (!PathIsRelativeW( xforms
[i
] )) transform
= xforms
[i
];
978 WCHAR
*p
= strrchrW( package
->PackagePath
, '\\' );
979 DWORD len
= p
- package
->PackagePath
+ 1;
981 if (!(transform
= msi_alloc( (len
+ strlenW( xforms
[i
] ) + 1) * sizeof(WCHAR
)) ))
984 msi_free( xform_list
);
985 return ERROR_OUTOFMEMORY
;
987 memcpy( transform
, package
->PackagePath
, len
* sizeof(WCHAR
) );
988 memcpy( transform
+ len
, xforms
[i
], (strlenW( xforms
[i
] ) + 1) * sizeof(WCHAR
) );
990 r
= MSI_DatabaseApplyTransformW( package
->db
, transform
, 0 );
991 if (transform
!= xforms
[i
]) msi_free( transform
);
995 msi_free( xform_list
);
999 UINT
msi_apply_registered_patch( MSIPACKAGE
*package
, LPCWSTR patch_code
)
1003 WCHAR patch_file
[MAX_PATH
];
1004 MSIDATABASE
*patch_db
;
1005 MSIPATCHINFO
*patch_info
;
1008 TRACE("%p, %s\n", package
, debugstr_w(patch_code
));
1010 len
= sizeof(patch_file
) / sizeof(WCHAR
);
1011 r
= MsiGetPatchInfoExW( patch_code
, package
->ProductCode
, NULL
, package
->Context
,
1012 INSTALLPROPERTY_LOCALPACKAGEW
, patch_file
, &len
);
1013 if (r
!= ERROR_SUCCESS
)
1015 ERR("failed to get patch filename %u\n", r
);
1018 r
= MSI_OpenDatabaseW( patch_file
, MSIDBOPEN_READONLY
+ MSIDBOPEN_PATCHFILE
, &patch_db
);
1019 if (r
!= ERROR_SUCCESS
)
1021 ERR("failed to open patch database %s\n", debugstr_w( patch_file
));
1024 r
= msi_get_suminfo( patch_db
->storage
, 0, &si
);
1025 if (r
!= ERROR_SUCCESS
)
1027 msiobj_release( &patch_db
->hdr
);
1030 r
= msi_parse_patch_summary( si
, &patch_info
);
1031 msiobj_release( &si
->hdr
);
1032 if (r
!= ERROR_SUCCESS
)
1034 ERR("failed to parse patch summary %u\n", r
);
1035 msiobj_release( &patch_db
->hdr
);
1038 patch_info
->localfile
= strdupW( patch_file
);
1039 if (!patch_info
->localfile
)
1041 msiobj_release( &patch_db
->hdr
);
1042 msi_free_patchinfo( patch_info
);
1043 return ERROR_OUTOFMEMORY
;
1045 r
= msi_apply_patch_db( package
, patch_db
, patch_info
);
1046 msiobj_release( &patch_db
->hdr
);
1047 if (r
!= ERROR_SUCCESS
)
1049 ERR("failed to apply patch %u\n", r
);
1050 msi_free_patchinfo( patch_info
);