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
24 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
26 static BOOL
match_language( MSIPACKAGE
*package
, LANGID langid
)
30 if (!package
->num_langids
|| !langid
) return TRUE
;
31 for (i
= 0; i
< package
->num_langids
; i
++)
33 if (package
->langids
[i
] == langid
) return TRUE
;
40 WCHAR
*product_code_from
;
41 WCHAR
*product_code_to
;
47 static void free_transform_desc( struct transform_desc
*desc
)
49 msi_free( desc
->product_code_from
);
50 msi_free( desc
->product_code_to
);
51 msi_free( desc
->version_from
);
52 msi_free( desc
->version_to
);
53 msi_free( desc
->upgrade_code
);
57 static struct transform_desc
*parse_transform_desc( const WCHAR
*str
)
59 struct transform_desc
*ret
;
60 const WCHAR
*p
= str
, *q
;
63 if (!(ret
= msi_alloc_zero( sizeof(*ret
) ))) return NULL
;
65 q
= strchrW( p
, '}' );
66 if (*p
!= '{' || !q
) goto error
;
69 if (!(ret
->product_code_from
= msi_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto error
;
70 memcpy( ret
->product_code_from
, p
, len
* sizeof(WCHAR
) );
71 ret
->product_code_from
[len
] = 0;
74 if (!(q
= strchrW( p
, ';' ))) goto error
;
76 if (!(ret
->version_from
= msi_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto error
;
77 memcpy( ret
->version_from
, p
, len
* sizeof(WCHAR
) );
78 ret
->version_from
[len
] = 0;
81 q
= strchrW( p
, '}' );
82 if (*p
!= '{' || !q
) goto error
;
85 if (!(ret
->product_code_to
= msi_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto error
;
86 memcpy( ret
->product_code_to
, p
, len
* sizeof(WCHAR
) );
87 ret
->product_code_to
[len
] = 0;
90 if (!(q
= strchrW( p
, ';' ))) goto error
;
92 if (!(ret
->version_to
= msi_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto error
;
93 memcpy( ret
->version_to
, p
, len
* sizeof(WCHAR
) );
94 ret
->version_to
[len
] = 0;
97 q
= strchrW( p
, '}' );
98 if (*p
!= '{' || !q
) goto error
;
101 if (!(ret
->upgrade_code
= msi_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto error
;
102 memcpy( ret
->upgrade_code
, p
, len
* sizeof(WCHAR
) );
103 ret
->upgrade_code
[len
] = 0;
108 free_transform_desc( ret
);
112 static UINT
check_transform_applicable( MSIPACKAGE
*package
, IStorage
*transform
)
114 static const UINT supported_flags
=
115 MSITRANSFORM_VALIDATE_PRODUCT
| MSITRANSFORM_VALIDATE_LANGUAGE
|
116 MSITRANSFORM_VALIDATE_PLATFORM
| MSITRANSFORM_VALIDATE_MAJORVERSION
|
117 MSITRANSFORM_VALIDATE_MINORVERSION
| MSITRANSFORM_VALIDATE_UPGRADECODE
;
119 UINT r
, valid_flags
= 0, wanted_flags
= 0;
120 WCHAR
*template, *product
, *p
;
121 struct transform_desc
*desc
;
123 r
= msi_get_suminfo( transform
, 0, &si
);
124 if (r
!= ERROR_SUCCESS
)
126 WARN("no summary information!\n");
129 wanted_flags
= msi_suminfo_get_int32( si
, PID_CHARCOUNT
);
130 wanted_flags
&= 0xffff; /* mask off error condition flags */
131 TRACE("validation flags 0x%04x\n", wanted_flags
);
133 /* native is not validating platform */
134 wanted_flags
&= ~MSITRANSFORM_VALIDATE_PLATFORM
;
136 if (wanted_flags
& ~supported_flags
)
138 FIXME("unsupported validation flags 0x%04x\n", wanted_flags
);
139 msiobj_release( &si
->hdr
);
140 return ERROR_FUNCTION_FAILED
;
142 if (!(template = msi_suminfo_dup_string( si
, PID_TEMPLATE
)))
144 WARN("no template property!\n");
145 msiobj_release( &si
->hdr
);
146 return ERROR_FUNCTION_FAILED
;
148 TRACE("template property: %s\n", debugstr_w(template));
149 if (!(product
= msi_get_suminfo_product( transform
)))
151 WARN("no product property!\n");
152 msi_free( template );
153 msiobj_release( &si
->hdr
);
154 return ERROR_FUNCTION_FAILED
;
156 TRACE("product property: %s\n", debugstr_w(product
));
157 if (!(desc
= parse_transform_desc( product
)))
159 msi_free( template );
160 msiobj_release( &si
->hdr
);
161 return ERROR_FUNCTION_FAILED
;
165 if (wanted_flags
& MSITRANSFORM_VALIDATE_LANGUAGE
)
167 if (!template[0] || ((p
= strchrW( template, ';' )) && match_language( package
, atoiW( p
+ 1 ) )))
169 valid_flags
|= MSITRANSFORM_VALIDATE_LANGUAGE
;
172 if (wanted_flags
& MSITRANSFORM_VALIDATE_PRODUCT
)
174 WCHAR
*product_code_installed
= msi_dup_property( package
->db
, szProductCode
);
176 if (!product_code_installed
)
178 msi_free( template );
179 free_transform_desc( desc
);
180 msiobj_release( &si
->hdr
);
181 return ERROR_INSTALL_PACKAGE_INVALID
;
183 if (!strcmpW( desc
->product_code_from
, product_code_installed
))
185 valid_flags
|= MSITRANSFORM_VALIDATE_PRODUCT
;
187 msi_free( product_code_installed
);
189 msi_free( template );
190 if (wanted_flags
& MSITRANSFORM_VALIDATE_MAJORVERSION
)
192 WCHAR
*product_version_installed
= msi_dup_property( package
->db
, szProductVersion
);
193 DWORD major_installed
, minor_installed
, major
, minor
;
195 if (!product_version_installed
)
197 free_transform_desc( desc
);
198 msiobj_release( &si
->hdr
);
199 return ERROR_INSTALL_PACKAGE_INVALID
;
201 msi_parse_version_string( product_version_installed
, &major_installed
, &minor_installed
);
202 msi_parse_version_string( desc
->version_from
, &major
, &minor
);
204 if (major_installed
== major
)
206 valid_flags
|= MSITRANSFORM_VALIDATE_MAJORVERSION
;
207 wanted_flags
&= ~MSITRANSFORM_VALIDATE_MINORVERSION
;
209 msi_free( product_version_installed
);
211 else if (wanted_flags
& MSITRANSFORM_VALIDATE_MINORVERSION
)
213 WCHAR
*product_version_installed
= msi_dup_property( package
->db
, szProductVersion
);
214 DWORD major_installed
, minor_installed
, major
, minor
;
216 if (!product_version_installed
)
218 free_transform_desc( desc
);
219 msiobj_release( &si
->hdr
);
220 return ERROR_INSTALL_PACKAGE_INVALID
;
222 msi_parse_version_string( product_version_installed
, &major_installed
, &minor_installed
);
223 msi_parse_version_string( desc
->version_from
, &major
, &minor
);
225 if (major_installed
== major
&& minor_installed
== minor
)
226 valid_flags
|= MSITRANSFORM_VALIDATE_MINORVERSION
;
227 msi_free( product_version_installed
);
229 if (wanted_flags
& MSITRANSFORM_VALIDATE_UPGRADECODE
)
231 WCHAR
*upgrade_code_installed
= msi_dup_property( package
->db
, szUpgradeCode
);
233 if (!upgrade_code_installed
)
235 free_transform_desc( desc
);
236 msiobj_release( &si
->hdr
);
237 return ERROR_INSTALL_PACKAGE_INVALID
;
239 if (!strcmpW( desc
->upgrade_code
, upgrade_code_installed
))
240 valid_flags
|= MSITRANSFORM_VALIDATE_UPGRADECODE
;
241 msi_free( upgrade_code_installed
);
244 free_transform_desc( desc
);
245 msiobj_release( &si
->hdr
);
246 if ((valid_flags
& wanted_flags
) != wanted_flags
) return ERROR_FUNCTION_FAILED
;
247 TRACE("applicable transform\n");
248 return ERROR_SUCCESS
;
251 static UINT
apply_substorage_transform( MSIPACKAGE
*package
, MSIDATABASE
*patch_db
, LPCWSTR name
)
253 UINT ret
= ERROR_FUNCTION_FAILED
;
254 IStorage
*stg
= NULL
;
257 TRACE("%p %s\n", package
, debugstr_w(name
));
261 ERR("expected a colon in %s\n", debugstr_w(name
));
262 return ERROR_FUNCTION_FAILED
;
264 r
= IStorage_OpenStorage( patch_db
->storage
, name
, NULL
, STGM_SHARE_EXCLUSIVE
, NULL
, 0, &stg
);
267 ret
= check_transform_applicable( package
, stg
);
268 if (ret
== ERROR_SUCCESS
)
269 msi_table_apply_transform( package
->db
, stg
);
271 TRACE("substorage transform %s wasn't applicable\n", debugstr_w(name
));
272 IStorage_Release( stg
);
276 ERR("failed to open substorage %s\n", debugstr_w(name
));
278 return ERROR_SUCCESS
;
281 UINT
msi_check_patch_applicable( MSIPACKAGE
*package
, MSISUMMARYINFO
*si
)
283 LPWSTR guid_list
, *guids
, product_code
;
284 UINT i
, ret
= ERROR_FUNCTION_FAILED
;
286 product_code
= msi_dup_property( package
->db
, szProductCode
);
289 /* FIXME: the property ProductCode should be written into the DB somewhere */
290 ERR("no product code to check\n");
291 return ERROR_SUCCESS
;
293 guid_list
= msi_suminfo_dup_string( si
, PID_TEMPLATE
);
294 guids
= msi_split_string( guid_list
, ';' );
295 for (i
= 0; guids
[i
] && ret
!= ERROR_SUCCESS
; i
++)
297 if (!strcmpW( guids
[i
], product_code
)) ret
= ERROR_SUCCESS
;
300 msi_free( guid_list
);
301 msi_free( product_code
);
305 static UINT
msi_parse_patch_summary( MSISUMMARYINFO
*si
, MSIPATCHINFO
**patch
)
308 UINT r
= ERROR_SUCCESS
;
311 if (!(pi
= msi_alloc_zero( sizeof(MSIPATCHINFO
) )))
313 return ERROR_OUTOFMEMORY
;
315 if (!(pi
->patchcode
= msi_suminfo_dup_string( si
, PID_REVNUMBER
)))
318 return ERROR_OUTOFMEMORY
;
323 msi_free( pi
->patchcode
);
325 return ERROR_PATCH_PACKAGE_INVALID
;
327 if (!(p
= strchrW( p
+ 1, '}' )))
329 msi_free( pi
->patchcode
);
331 return ERROR_PATCH_PACKAGE_INVALID
;
335 FIXME("patch obsoletes %s\n", debugstr_w(p
+ 1));
338 TRACE("patch code %s\n", debugstr_w(pi
->patchcode
));
339 if (!(pi
->products
= msi_suminfo_dup_string( si
, PID_TEMPLATE
)))
341 msi_free( pi
->patchcode
);
343 return ERROR_OUTOFMEMORY
;
345 if (!(pi
->transforms
= msi_suminfo_dup_string( si
, PID_LASTAUTHOR
)))
347 msi_free( pi
->patchcode
);
348 msi_free( pi
->products
);
350 return ERROR_OUTOFMEMORY
;
356 static UINT
patch_set_media_source_prop( MSIPACKAGE
*package
)
358 static const WCHAR query
[] = {
359 'S','E','L','E','C','T',' ','`','S','o','u','r','c','e','`',' ','F','R','O','M',' ',
360 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ','`','S','o','u','r','c','e','`',' ',
361 'I','S',' ','N','O','T',' ','N','U','L','L',0};
364 const WCHAR
*property
;
368 r
= MSI_DatabaseOpenViewW( package
->db
, query
, &view
);
369 if (r
!= ERROR_SUCCESS
)
372 r
= MSI_ViewExecute( view
, 0 );
373 if (r
!= ERROR_SUCCESS
)
376 if (MSI_ViewFetch( view
, &rec
) == ERROR_SUCCESS
)
378 property
= MSI_RecordGetString( rec
, 1 );
379 patch
= msi_dup_property( package
->db
, szPatch
);
380 msi_set_property( package
->db
, property
, patch
, -1 );
382 msiobj_release( &rec
->hdr
);
386 msiobj_release( &view
->hdr
);
397 struct patch_offset_list
401 UINT count
, min
, max
;
402 UINT offset_to_apply
;
405 static struct patch_offset_list
*patch_offset_list_create( void )
407 struct patch_offset_list
*pos
= msi_alloc( sizeof(struct patch_offset_list
) );
408 list_init( &pos
->files
);
409 list_init( &pos
->patches
);
410 pos
->count
= pos
->max
= 0;
415 static void patch_offset_list_free( struct patch_offset_list
*pos
)
417 struct patch_offset
*po
, *po2
;
419 LIST_FOR_EACH_ENTRY_SAFE( po
, po2
, &pos
->files
, struct patch_offset
, entry
)
421 msi_free( po
->name
);
424 LIST_FOR_EACH_ENTRY_SAFE( po
, po2
, &pos
->patches
, struct patch_offset
, entry
)
426 msi_free( po
->name
);
432 static void patch_offset_get_filepatches( MSIDATABASE
*db
, UINT last_sequence
, struct patch_offset_list
*pos
)
434 static const WCHAR query
[] = {
435 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','P','a','t','c','h',' ',
436 'W','H','E','R','E',' ','S','e','q','u','e','n','c','e',' ','<','=',' ','?',' ',
437 'O','R','D','E','R',' ','B','Y',' ','S','e','q','u','e','n','c','e',0};
442 r
= MSI_DatabaseOpenViewW( db
, query
, &view
);
443 if (r
!= ERROR_SUCCESS
)
446 rec
= MSI_CreateRecord( 1 );
447 MSI_RecordSetInteger( rec
, 1, last_sequence
);
449 r
= MSI_ViewExecute( view
, rec
);
450 msiobj_release( &rec
->hdr
);
451 if (r
!= ERROR_SUCCESS
)
454 while (MSI_ViewFetch( view
, &rec
) == ERROR_SUCCESS
)
456 struct patch_offset
*po
= msi_alloc( sizeof(struct patch_offset
) );
458 po
->name
= msi_dup_record_field( rec
, 1 );
459 po
->sequence
= MSI_RecordGetInteger( rec
, 2 );
460 pos
->min
= min( pos
->min
, po
->sequence
);
461 pos
->max
= max( pos
->max
, po
->sequence
);
462 list_add_tail( &pos
->patches
, &po
->entry
);
465 msiobj_release( &rec
->hdr
);
467 msiobj_release( &view
->hdr
);
470 static void patch_offset_get_files( MSIDATABASE
*db
, UINT last_sequence
, struct patch_offset_list
*pos
)
472 static const WCHAR query
[] = {
473 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','F','i','l','e',' ',
474 'W','H','E','R','E',' ','S','e','q','u','e','n','c','e',' ','<','=',' ','?',' ',
475 'O','R','D','E','R',' ','B','Y',' ','S','e','q','u','e','n','c','e',0};
480 r
= MSI_DatabaseOpenViewW( db
, query
, &view
);
481 if (r
!= ERROR_SUCCESS
)
484 rec
= MSI_CreateRecord( 1 );
485 MSI_RecordSetInteger( rec
, 1, last_sequence
);
487 r
= MSI_ViewExecute( view
, rec
);
488 msiobj_release( &rec
->hdr
);
489 if (r
!= ERROR_SUCCESS
)
492 while (MSI_ViewFetch( view
, &rec
) == ERROR_SUCCESS
)
494 UINT attributes
= MSI_RecordGetInteger( rec
, 7 );
495 if (attributes
& msidbFileAttributesPatchAdded
)
497 struct patch_offset
*po
= msi_alloc( sizeof(struct patch_offset
) );
499 po
->name
= msi_dup_record_field( rec
, 1 );
500 po
->sequence
= MSI_RecordGetInteger( rec
, 8 );
501 pos
->min
= min( pos
->min
, po
->sequence
);
502 pos
->max
= max( pos
->max
, po
->sequence
);
503 list_add_tail( &pos
->files
, &po
->entry
);
506 msiobj_release( &rec
->hdr
);
508 msiobj_release( &view
->hdr
);
511 static UINT
patch_update_file_sequence( MSIDATABASE
*db
, const struct patch_offset_list
*pos
,
512 MSIQUERY
*view
, MSIRECORD
*rec
)
514 struct patch_offset
*po
;
515 const WCHAR
*file
= MSI_RecordGetString( rec
, 1 );
516 UINT r
= ERROR_SUCCESS
, seq
= MSI_RecordGetInteger( rec
, 8 );
518 LIST_FOR_EACH_ENTRY( po
, &pos
->files
, struct patch_offset
, entry
)
520 if (!strcmpiW( file
, po
->name
))
522 MSI_RecordSetInteger( rec
, 8, seq
+ pos
->offset_to_apply
);
523 r
= MSI_ViewModify( view
, MSIMODIFY_UPDATE
, rec
);
524 if (r
!= ERROR_SUCCESS
)
525 ERR("Failed to update offset for file %s (%u)\n", debugstr_w(file
), r
);
532 static UINT
patch_update_filepatch_sequence( MSIDATABASE
*db
, const struct patch_offset_list
*pos
,
533 MSIQUERY
*view
, MSIRECORD
*rec
)
535 static const WCHAR delete_query
[] = {
536 'D','E','L','E','T','E',' ','F','R','O','M',' ','`','P','a','t','c','h','`',' ',
537 'W','H','E','R','E',' ','`','F','i','l','e','_','`',' ','=',' ','?',' ',
538 'A','N','D',' ','`','S','e','q','u','e','n','c','e','`',' ','=',' ','?',0};
539 static const WCHAR insert_query
[] = {
540 'I','N','S','E','R','T',' ','I','N','T','O',' ','`','P','a','t','c','h','`',' ',
541 '(','`','F','i','l','e','_','`',',','`','S','e','q','u','e','n','c','e','`',',',
542 '`','P','a','t','c','h','S','i','z','e','`',',','`','A','t','t','r','i','b','u','t','e','s','`',',',
543 '`','H','e','a','d','e','r','`',',','`','S','t','r','e','a','m','R','e','f','_','`',')',' ',
544 'V','A','L','U','E','S',' ','(','?',',','?',',','?',',','?',',','?',',','?',')',0};
545 struct patch_offset
*po
;
546 const WCHAR
*file
= MSI_RecordGetString( rec
, 1 );
547 UINT r
= ERROR_SUCCESS
, seq
= MSI_RecordGetInteger( rec
, 2 );
549 LIST_FOR_EACH_ENTRY( po
, &pos
->patches
, struct patch_offset
, entry
)
551 if (seq
== po
->sequence
&& !strcmpiW( file
, po
->name
))
553 MSIQUERY
*delete_view
, *insert_view
;
556 r
= MSI_DatabaseOpenViewW( db
, delete_query
, &delete_view
);
557 if (r
!= ERROR_SUCCESS
) return r
;
559 rec2
= MSI_CreateRecord( 2 );
560 MSI_RecordSetStringW( rec2
, 1, po
->name
);
561 MSI_RecordSetInteger( rec2
, 2, po
->sequence
);
562 r
= MSI_ViewExecute( delete_view
, rec2
);
563 msiobj_release( &delete_view
->hdr
);
564 msiobj_release( &rec2
->hdr
);
565 if (r
!= ERROR_SUCCESS
) return r
;
567 r
= MSI_DatabaseOpenViewW( db
, insert_query
, &insert_view
);
568 if (r
!= ERROR_SUCCESS
) return r
;
570 MSI_RecordSetInteger( rec
, 2, po
->sequence
+ pos
->offset_to_apply
);
572 r
= MSI_ViewExecute( insert_view
, rec
);
573 msiobj_release( &insert_view
->hdr
);
574 if (r
!= ERROR_SUCCESS
)
575 ERR("Failed to update offset for filepatch %s (%u)\n", debugstr_w(file
), r
);
582 static UINT
patch_offset_modify_db( MSIDATABASE
*db
, struct patch_offset_list
*pos
)
584 static const WCHAR file_query
[] = {
585 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','F','i','l','e','`',' ',
586 'W','H','E','R','E',' ','`','S','e','q','u','e','n','c','e','`',' ','>','=',' ','?',' ',
587 'A','N','D',' ','`','S','e','q','u','e','n','c','e','`',' ','<','=',' ','?',' ',
588 'O','R','D','E','R',' ','B','Y',' ','`','S','e','q','u','e','n','c','e','`',0};
589 static const WCHAR patch_query
[] = {
590 'S','E','L','E','C','T',' ','*','F','R','O','M',' ','`','P','a','t','c','h','`',' ',
591 'W','H','E','R','E',' ','`','S','e','q','u','e','n','c','e','`',' ','>','=',' ','?',' ',
592 'A','N','D',' ','`','S','e','q','u','e','n','c','e','`',' ','<','=',' ','?',' ',
593 'O','R','D','E','R',' ','B','Y',' ','`','S','e','q','u','e','n','c','e','`',0};
596 UINT r
, min
= pos
->min
, max
= pos
->max
, r_fetch
;
598 r
= MSI_DatabaseOpenViewW( db
, file_query
, &view
);
599 if (r
!= ERROR_SUCCESS
)
600 return ERROR_SUCCESS
;
602 rec
= MSI_CreateRecord( 2 );
603 MSI_RecordSetInteger( rec
, 1, min
);
604 MSI_RecordSetInteger( rec
, 2, max
);
606 r
= MSI_ViewExecute( view
, rec
);
607 msiobj_release( &rec
->hdr
);
608 if (r
!= ERROR_SUCCESS
)
611 while ((r_fetch
= MSI_ViewFetch( view
, &rec
)) == ERROR_SUCCESS
)
613 r
= patch_update_file_sequence( db
, pos
, view
, rec
);
614 msiobj_release( &rec
->hdr
);
615 if (r
!= ERROR_SUCCESS
) goto done
;
617 msiobj_release( &view
->hdr
);
619 r
= MSI_DatabaseOpenViewW( db
, patch_query
, &view
);
620 if (r
!= ERROR_SUCCESS
)
621 return ERROR_SUCCESS
;
623 rec
= MSI_CreateRecord( 2 );
624 MSI_RecordSetInteger( rec
, 1, min
);
625 MSI_RecordSetInteger( rec
, 2, max
);
627 r
= MSI_ViewExecute( view
, rec
);
628 msiobj_release( &rec
->hdr
);
629 if (r
!= ERROR_SUCCESS
)
632 while ((r_fetch
= MSI_ViewFetch( view
, &rec
)) == ERROR_SUCCESS
)
634 r
= patch_update_filepatch_sequence( db
, pos
, view
, rec
);
635 msiobj_release( &rec
->hdr
);
636 if (r
!= ERROR_SUCCESS
) goto done
;
640 msiobj_release( &view
->hdr
);
644 static const WCHAR patch_media_query
[] = {
645 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
646 'W','H','E','R','E',' ','`','S','o','u','r','c','e','`',' ','I','S',' ','N','O','T',' ','N','U','L','L',' ',
647 'A','N','D',' ','`','C','a','b','i','n','e','t','`',' ','I','S',' ','N','O','T',' ','N','U','L','L',' ',
648 'O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
661 static UINT
patch_add_media( MSIPACKAGE
*package
, IStorage
*storage
, MSIPATCHINFO
*patch
)
663 static const WCHAR delete_query
[] = {
664 'D','E','L','E','T','E',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
665 'W','H','E','R','E',' ','`','D','i','s','k','I','d','`','=','?',0};
666 static const WCHAR insert_query
[] = {
667 'I','N','S','E','R','T',' ','I','N','T','O',' ','`','M','e','d','i','a','`',' ',
668 '(','`','D','i','s','k','I','d','`',',','`','L','a','s','t','S','e','q','u','e','n','c','e','`',',',
669 '`','D','i','s','k','P','r','o','m','p','t','`',',','`','C','a','b','i','n','e','t','`',',',
670 '`','V','o','l','u','m','e','L','a','b','e','l','`',',','`','S','o','u','r','c','e','`',')',' ',
671 'V','A','L','U','E','S',' ','(','?',',','?',',','?',',','?',',','?',',','?',')',0};
675 struct list media_list
;
676 struct patch_media
*media
, *next
;
678 r
= MSI_DatabaseOpenViewW( package
->db
, patch_media_query
, &view
);
679 if (r
!= ERROR_SUCCESS
) return r
;
681 r
= MSI_ViewExecute( view
, 0 );
682 if (r
!= ERROR_SUCCESS
)
684 msiobj_release( &view
->hdr
);
685 TRACE("query failed %u\n", r
);
688 list_init( &media_list
);
689 while (MSI_ViewFetch( view
, &rec
) == ERROR_SUCCESS
)
691 disk_id
= MSI_RecordGetInteger( rec
, 1 );
692 TRACE("disk_id %u\n", disk_id
);
693 if (disk_id
>= MSI_INITIAL_MEDIA_TRANSFORM_DISKID
)
695 msiobj_release( &rec
->hdr
);
698 if (!(media
= msi_alloc( sizeof( *media
)))) {
699 msiobj_release( &rec
->hdr
);
702 media
->disk_id
= disk_id
;
703 media
->last_sequence
= MSI_RecordGetInteger( rec
, 2 );
704 media
->prompt
= msi_dup_record_field( rec
, 3 );
705 media
->cabinet
= msi_dup_record_field( rec
, 4 );
706 media
->volume
= msi_dup_record_field( rec
, 5 );
707 media
->source
= msi_dup_record_field( rec
, 6 );
709 list_add_tail( &media_list
, &media
->entry
);
710 msiobj_release( &rec
->hdr
);
712 LIST_FOR_EACH_ENTRY( media
, &media_list
, struct patch_media
, entry
)
714 MSIQUERY
*delete_view
, *insert_view
;
716 r
= MSI_DatabaseOpenViewW( package
->db
, delete_query
, &delete_view
);
717 if (r
!= ERROR_SUCCESS
) goto done
;
719 rec
= MSI_CreateRecord( 1 );
720 MSI_RecordSetInteger( rec
, 1, media
->disk_id
);
722 r
= MSI_ViewExecute( delete_view
, rec
);
723 msiobj_release( &delete_view
->hdr
);
724 msiobj_release( &rec
->hdr
);
725 if (r
!= ERROR_SUCCESS
) goto done
;
727 r
= MSI_DatabaseOpenViewW( package
->db
, insert_query
, &insert_view
);
728 if (r
!= ERROR_SUCCESS
) goto done
;
730 disk_id
= package
->db
->media_transform_disk_id
;
731 TRACE("disk id %u\n", disk_id
);
732 TRACE("last sequence %u\n", media
->last_sequence
);
733 TRACE("prompt %s\n", debugstr_w(media
->prompt
));
734 TRACE("cabinet %s\n", debugstr_w(media
->cabinet
));
735 TRACE("volume %s\n", debugstr_w(media
->volume
));
736 TRACE("source %s\n", debugstr_w(media
->source
));
738 rec
= MSI_CreateRecord( 6 );
739 MSI_RecordSetInteger( rec
, 1, disk_id
);
740 MSI_RecordSetInteger( rec
, 2, media
->last_sequence
);
741 MSI_RecordSetStringW( rec
, 3, media
->prompt
);
742 MSI_RecordSetStringW( rec
, 4, media
->cabinet
);
743 MSI_RecordSetStringW( rec
, 5, media
->volume
);
744 MSI_RecordSetStringW( rec
, 6, media
->source
);
746 r
= MSI_ViewExecute( insert_view
, rec
);
747 msiobj_release( &insert_view
->hdr
);
748 msiobj_release( &rec
->hdr
);
749 if (r
!= ERROR_SUCCESS
) goto done
;
751 r
= msi_add_cabinet_stream( package
, disk_id
, storage
, media
->cabinet
);
752 if (r
!= ERROR_SUCCESS
) ERR("failed to add cabinet stream %u\n", r
);
755 patch
->disk_id
= disk_id
;
756 package
->db
->media_transform_disk_id
++;
761 msiobj_release( &view
->hdr
);
762 LIST_FOR_EACH_ENTRY_SAFE( media
, next
, &media_list
, struct patch_media
, entry
)
764 list_remove( &media
->entry
);
765 msi_free( media
->prompt
);
766 msi_free( media
->cabinet
);
767 msi_free( media
->volume
);
768 msi_free( media
->source
);
774 static UINT
patch_set_offsets( MSIDATABASE
*db
, MSIPATCHINFO
*patch
)
780 r
= MSI_DatabaseOpenViewW( db
, patch_media_query
, &view
);
781 if (r
!= ERROR_SUCCESS
)
784 r
= MSI_ViewExecute( view
, 0 );
785 if (r
!= ERROR_SUCCESS
)
788 while (MSI_ViewFetch( view
, &rec
) == ERROR_SUCCESS
)
790 UINT offset
, last_sequence
= MSI_RecordGetInteger( rec
, 2 );
791 struct patch_offset_list
*pos
;
793 /* FIXME: set/check Source field instead? */
794 if (last_sequence
>= MSI_INITIAL_MEDIA_TRANSFORM_OFFSET
)
796 msiobj_release( &rec
->hdr
);
799 pos
= patch_offset_list_create();
800 patch_offset_get_files( db
, last_sequence
, pos
);
801 patch_offset_get_filepatches( db
, last_sequence
, pos
);
803 offset
= db
->media_transform_offset
- pos
->min
;
804 last_sequence
= offset
+ pos
->max
;
806 last_sequence
+= pos
->min
;
807 pos
->offset_to_apply
= offset
;
810 r
= patch_offset_modify_db( db
, pos
);
811 if (r
!= ERROR_SUCCESS
)
812 ERR("Failed to set offsets, expect breakage (%u)\n", r
);
814 MSI_RecordSetInteger( rec
, 2, last_sequence
);
815 r
= MSI_ViewModify( view
, MSIMODIFY_UPDATE
, rec
);
816 if (r
!= ERROR_SUCCESS
)
817 ERR("Failed to update Media table entry, expect breakage (%u)\n", r
);
819 db
->media_transform_offset
= last_sequence
+ 1;
821 patch_offset_list_free( pos
);
822 msiobj_release( &rec
->hdr
);
826 msiobj_release( &view
->hdr
);
830 static UINT
msi_apply_patch_db( MSIPACKAGE
*package
, MSIDATABASE
*patch_db
, MSIPATCHINFO
*patch
)
832 UINT i
, r
= ERROR_SUCCESS
;
835 /* apply substorage transforms */
836 substorage
= msi_split_string( patch
->transforms
, ';' );
837 for (i
= 0; substorage
&& substorage
[i
] && r
== ERROR_SUCCESS
; i
++)
839 r
= apply_substorage_transform( package
, patch_db
, substorage
[i
] );
840 if (r
== ERROR_SUCCESS
)
842 r
= patch_set_offsets( package
->db
, patch
);
843 if (r
== ERROR_SUCCESS
)
844 r
= patch_add_media( package
, patch_db
->storage
, patch
);
847 msi_free( substorage
);
848 if (r
!= ERROR_SUCCESS
)
851 r
= patch_set_media_source_prop( package
);
852 if (r
!= ERROR_SUCCESS
)
855 patch
->state
= MSIPATCHSTATE_APPLIED
;
856 list_add_tail( &package
->patches
, &patch
->entry
);
857 return ERROR_SUCCESS
;
860 void msi_free_patchinfo( MSIPATCHINFO
*patch
)
862 msi_free( patch
->patchcode
);
863 msi_free( patch
->products
);
864 msi_free( patch
->transforms
);
865 msi_free( patch
->filename
);
866 msi_free( patch
->localfile
);
870 static UINT
msi_apply_patch_package( MSIPACKAGE
*package
, const WCHAR
*file
)
872 static const WCHAR dotmsp
[] = {'.','m','s','p',0};
873 MSIDATABASE
*patch_db
= NULL
;
874 WCHAR localfile
[MAX_PATH
];
876 MSIPATCHINFO
*patch
= NULL
;
879 TRACE("%p, %s\n", package
, debugstr_w(file
));
881 r
= MSI_OpenDatabaseW( file
, MSIDBOPEN_READONLY
+ MSIDBOPEN_PATCHFILE
, &patch_db
);
882 if (r
!= ERROR_SUCCESS
)
884 ERR("failed to open patch collection %s\n", debugstr_w( file
) );
887 r
= msi_get_suminfo( patch_db
->storage
, 0, &si
);
888 if (r
!= ERROR_SUCCESS
)
890 msiobj_release( &patch_db
->hdr
);
893 r
= msi_check_patch_applicable( package
, si
);
894 if (r
!= ERROR_SUCCESS
)
896 TRACE("patch not applicable\n");
900 r
= msi_parse_patch_summary( si
, &patch
);
901 if ( r
!= ERROR_SUCCESS
)
904 r
= msi_create_empty_local_file( localfile
, dotmsp
);
905 if ( r
!= ERROR_SUCCESS
)
908 r
= ERROR_OUTOFMEMORY
;
909 patch
->registered
= FALSE
;
910 if (!(patch
->filename
= strdupW( file
))) goto done
;
911 if (!(patch
->localfile
= strdupW( localfile
))) goto done
;
913 r
= msi_apply_patch_db( package
, patch_db
, patch
);
914 if (r
!= ERROR_SUCCESS
) WARN("patch failed to apply %u\n", r
);
917 msiobj_release( &si
->hdr
);
918 msiobj_release( &patch_db
->hdr
);
919 if (patch
&& r
!= ERROR_SUCCESS
)
921 DeleteFileW( patch
->localfile
);
922 msi_free_patchinfo( patch
);
927 /* get the PATCH property, and apply all the patches it specifies */
928 UINT
msi_apply_patches( MSIPACKAGE
*package
)
930 LPWSTR patch_list
, *patches
;
931 UINT i
, r
= ERROR_SUCCESS
;
933 patch_list
= msi_dup_property( package
->db
, szPatch
);
935 TRACE("patches to be applied: %s\n", debugstr_w(patch_list
));
937 patches
= msi_split_string( patch_list
, ';' );
938 for (i
= 0; patches
&& patches
[i
] && r
== ERROR_SUCCESS
; i
++)
939 r
= msi_apply_patch_package( package
, patches
[i
] );
942 msi_free( patch_list
);
946 UINT
msi_apply_transforms( MSIPACKAGE
*package
)
948 static const WCHAR szTransforms
[] = {'T','R','A','N','S','F','O','R','M','S',0};
949 LPWSTR xform_list
, *xforms
;
950 UINT i
, r
= ERROR_SUCCESS
;
952 xform_list
= msi_dup_property( package
->db
, szTransforms
);
953 xforms
= msi_split_string( xform_list
, ';' );
955 for (i
= 0; xforms
&& xforms
[i
] && r
== ERROR_SUCCESS
; i
++)
957 if (xforms
[i
][0] == ':')
958 r
= apply_substorage_transform( package
, package
->db
, xforms
[i
] );
963 if (!PathIsRelativeW( xforms
[i
] )) transform
= xforms
[i
];
966 WCHAR
*p
= strrchrW( package
->PackagePath
, '\\' );
967 DWORD len
= p
- package
->PackagePath
+ 1;
969 if (!(transform
= msi_alloc( (len
+ strlenW( xforms
[i
] ) + 1) * sizeof(WCHAR
)) ))
972 msi_free( xform_list
);
973 return ERROR_OUTOFMEMORY
;
975 memcpy( transform
, package
->PackagePath
, len
* sizeof(WCHAR
) );
976 memcpy( transform
+ len
, xforms
[i
], (strlenW( xforms
[i
] ) + 1) * sizeof(WCHAR
) );
978 r
= MSI_DatabaseApplyTransformW( package
->db
, transform
, 0 );
979 if (transform
!= xforms
[i
]) msi_free( transform
);
983 msi_free( xform_list
);
987 UINT
msi_apply_registered_patch( MSIPACKAGE
*package
, LPCWSTR patch_code
)
991 WCHAR patch_file
[MAX_PATH
];
992 MSIDATABASE
*patch_db
;
993 MSIPATCHINFO
*patch_info
;
996 TRACE("%p, %s\n", package
, debugstr_w(patch_code
));
998 len
= sizeof(patch_file
) / sizeof(WCHAR
);
999 r
= MsiGetPatchInfoExW( patch_code
, package
->ProductCode
, NULL
, package
->Context
,
1000 INSTALLPROPERTY_LOCALPACKAGEW
, patch_file
, &len
);
1001 if (r
!= ERROR_SUCCESS
)
1003 ERR("failed to get patch filename %u\n", r
);
1006 r
= MSI_OpenDatabaseW( patch_file
, MSIDBOPEN_READONLY
+ MSIDBOPEN_PATCHFILE
, &patch_db
);
1007 if (r
!= ERROR_SUCCESS
)
1009 ERR("failed to open patch database %s\n", debugstr_w( patch_file
));
1012 r
= msi_get_suminfo( patch_db
->storage
, 0, &si
);
1013 if (r
!= ERROR_SUCCESS
)
1015 msiobj_release( &patch_db
->hdr
);
1018 r
= msi_parse_patch_summary( si
, &patch_info
);
1019 msiobj_release( &si
->hdr
);
1020 if (r
!= ERROR_SUCCESS
)
1022 ERR("failed to parse patch summary %u\n", r
);
1023 msiobj_release( &patch_db
->hdr
);
1026 patch_info
->registered
= TRUE
;
1027 patch_info
->localfile
= strdupW( patch_file
);
1028 if (!patch_info
->localfile
)
1030 msiobj_release( &patch_db
->hdr
);
1031 msi_free_patchinfo( patch_info
);
1032 return ERROR_OUTOFMEMORY
;
1034 r
= msi_apply_patch_db( package
, patch_db
, patch_info
);
1035 msiobj_release( &patch_db
->hdr
);
1036 if (r
!= ERROR_SUCCESS
)
1038 ERR("failed to apply patch %u\n", r
);
1039 msi_free_patchinfo( patch_info
);