2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2008 James Hawkins
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/debug.h"
27 #include "msvcrt/fcntl.h"
32 #include "wine/unicode.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
36 static BOOL
source_matches_volume(MSIMEDIAINFO
*mi
, LPWSTR source_root
)
38 WCHAR volume_name
[MAX_PATH
+ 1];
40 if (!GetVolumeInformationW(source_root
, volume_name
, MAX_PATH
+ 1,
41 NULL
, NULL
, NULL
, NULL
, 0))
43 ERR("Failed to get volume information\n");
47 return !lstrcmpW(mi
->volume_label
, volume_name
);
50 static UINT
msi_change_media(MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
)
53 LPWSTR error
, error_dialog
;
55 UINT r
= ERROR_SUCCESS
;
57 static const WCHAR szUILevel
[] = {'U','I','L','e','v','e','l',0};
58 static const WCHAR error_prop
[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
60 if ((msi_get_property_int(package
, szUILevel
, 0) & INSTALLUILEVEL_MASK
) ==
61 INSTALLUILEVEL_NONE
&& !gUIHandlerA
)
64 error
= generate_error_string(package
, 1302, 1, mi
->disk_prompt
);
65 error_dialog
= msi_dup_property(package
, error_prop
);
66 source_dir
= msi_dup_property(package
, cszSourceDir
);
67 PathStripToRootW(source_dir
);
69 while (r
== ERROR_SUCCESS
&&
70 !source_matches_volume(mi
, source_dir
))
72 r
= msi_spawn_error_dialog(package
, error_dialog
, error
);
76 msg
= strdupWtoA(error
);
77 gUIHandlerA(gUIContext
, MB_RETRYCANCEL
| INSTALLMESSAGE_ERROR
, msg
);
83 msi_free(error_dialog
);
89 static UINT
writeout_cabinet_stream(MSIPACKAGE
*package
, LPCWSTR stream
,
99 static const WCHAR cszTempFolder
[]= {
100 'T','e','m','p','F','o','l','d','e','r',0};
102 rc
= read_raw_stream_data(package
->db
, stream
, &data
, &size
);
103 if (rc
!= ERROR_SUCCESS
)
107 if (MSI_GetPropertyW(package
, cszTempFolder
, tmp
, &write
))
108 GetTempPathW(MAX_PATH
, tmp
);
110 GetTempFileNameW(tmp
, stream
, 0, source
);
112 track_tempfile(package
, source
);
113 hfile
= CreateFileW(source
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
114 FILE_ATTRIBUTE_NORMAL
, NULL
);
116 if (hfile
== INVALID_HANDLE_VALUE
)
118 ERR("Unable to create file %s\n", debugstr_w(source
));
119 rc
= ERROR_FUNCTION_FAILED
;
123 WriteFile(hfile
, data
, size
, &write
, NULL
);
125 TRACE("wrote %i bytes to %s\n", write
, debugstr_w(source
));
132 static void *cabinet_alloc(ULONG cb
)
134 return msi_alloc(cb
);
137 static void cabinet_free(void *pv
)
142 static INT_PTR
cabinet_open(char *pszFile
, int oflag
, int pmode
)
146 DWORD dwShareMode
= 0;
147 DWORD dwCreateDisposition
= OPEN_EXISTING
;
149 switch (oflag
& _O_ACCMODE
)
152 dwAccess
= GENERIC_READ
;
153 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_DELETE
;
156 dwAccess
= GENERIC_WRITE
;
157 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
160 dwAccess
= GENERIC_READ
| GENERIC_WRITE
;
161 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
165 if ((oflag
& (_O_CREAT
| _O_EXCL
)) == (_O_CREAT
| _O_EXCL
))
166 dwCreateDisposition
= CREATE_NEW
;
167 else if (oflag
& _O_CREAT
)
168 dwCreateDisposition
= CREATE_ALWAYS
;
170 handle
= CreateFileA(pszFile
, dwAccess
, dwShareMode
, NULL
,
171 dwCreateDisposition
, 0, NULL
);
172 if (handle
== INVALID_HANDLE_VALUE
)
175 return (INT_PTR
)handle
;
178 static UINT
cabinet_read(INT_PTR hf
, void *pv
, UINT cb
)
180 HANDLE handle
= (HANDLE
)hf
;
183 if (ReadFile(handle
, pv
, cb
, &read
, NULL
))
189 static UINT
cabinet_write(INT_PTR hf
, void *pv
, UINT cb
)
191 HANDLE handle
= (HANDLE
)hf
;
194 if (WriteFile(handle
, pv
, cb
, &written
, NULL
))
200 static int cabinet_close(INT_PTR hf
)
202 HANDLE handle
= (HANDLE
)hf
;
203 return CloseHandle(handle
) ? 0 : -1;
206 static long cabinet_seek(INT_PTR hf
, long dist
, int seektype
)
208 HANDLE handle
= (HANDLE
)hf
;
209 /* flags are compatible and so are passed straight through */
210 return SetFilePointer(handle
, dist
, NULL
, seektype
);
213 static UINT
msi_media_get_disk_info(MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
)
218 static const WCHAR query
[] = {
219 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
220 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
221 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
223 row
= MSI_QueryGetRecord(package
->db
, query
, mi
->disk_id
);
226 TRACE("Unable to query row\n");
227 return ERROR_FUNCTION_FAILED
;
230 mi
->disk_prompt
= strdupW(MSI_RecordGetString(row
, 3));
231 mi
->cabinet
= strdupW(MSI_RecordGetString(row
, 4));
232 mi
->volume_label
= strdupW(MSI_RecordGetString(row
, 5));
234 if (!mi
->first_volume
)
235 mi
->first_volume
= strdupW(mi
->volume_label
);
237 ptr
= strrchrW(mi
->source
, '\\') + 1;
238 lstrcpyW(ptr
, mi
->cabinet
);
239 msiobj_release(&row
->hdr
);
241 return ERROR_SUCCESS
;
244 static INT_PTR
cabinet_partial_file(FDINOTIFICATIONTYPE fdint
,
245 PFDINOTIFICATION pfdin
)
247 MSICABDATA
*data
= (MSICABDATA
*)pfdin
->pv
;
248 data
->mi
->is_continuous
= FALSE
;
252 static INT_PTR
cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint
,
253 PFDINOTIFICATION pfdin
)
255 MSICABDATA
*data
= (MSICABDATA
*)pfdin
->pv
;
256 MSIMEDIAINFO
*mi
= data
->mi
;
257 LPWSTR cab
= strdupAtoW(pfdin
->psz1
);
261 msi_free(mi
->disk_prompt
);
262 msi_free(mi
->cabinet
);
263 msi_free(mi
->volume_label
);
264 mi
->disk_prompt
= NULL
;
266 mi
->volume_label
= NULL
;
269 mi
->is_continuous
= TRUE
;
271 rc
= msi_media_get_disk_info(data
->package
, mi
);
272 if (rc
!= ERROR_SUCCESS
)
274 ERR("Failed to get next cabinet information: %d\n", rc
);
278 if (lstrcmpiW(mi
->cabinet
, cab
))
280 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
284 TRACE("Searching for %s\n", debugstr_w(mi
->source
));
287 if (GetFileAttributesW(mi
->source
) == INVALID_FILE_ATTRIBUTES
)
289 if (msi_change_media(data
->package
, mi
) != ERROR_SUCCESS
)
298 static INT_PTR
cabinet_copy_file(FDINOTIFICATIONTYPE fdint
,
299 PFDINOTIFICATION pfdin
)
301 MSICABDATA
*data
= (MSICABDATA
*)pfdin
->pv
;
306 data
->curfile
= strdupAtoW(pfdin
->psz1
);
307 if (!data
->cb(data
->package
, data
->curfile
, MSICABEXTRACT_BEGINEXTRACT
, &path
,
311 TRACE("extracting %s\n", debugstr_w(path
));
313 attrs
= attrs
& (FILE_ATTRIBUTE_READONLY
|FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
);
314 if (!attrs
) attrs
= FILE_ATTRIBUTE_NORMAL
;
316 handle
= CreateFileW(path
, GENERIC_READ
| GENERIC_WRITE
, 0,
317 NULL
, CREATE_ALWAYS
, attrs
, NULL
);
318 if (handle
== INVALID_HANDLE_VALUE
)
320 if (GetFileAttributesW(path
) == INVALID_FILE_ATTRIBUTES
)
321 ERR("failed to create %s (error %d)\n",
322 debugstr_w(path
), GetLastError());
330 return (INT_PTR
)handle
;
333 static INT_PTR
cabinet_close_file_info(FDINOTIFICATIONTYPE fdint
,
334 PFDINOTIFICATION pfdin
)
336 MSICABDATA
*data
= (MSICABDATA
*)pfdin
->pv
;
339 HANDLE handle
= (HANDLE
)pfdin
->hf
;
341 data
->mi
->is_continuous
= FALSE
;
343 if (!DosDateTimeToFileTime(pfdin
->date
, pfdin
->time
, &ft
))
345 if (!LocalFileTimeToFileTime(&ft
, &ftLocal
))
347 if (!SetFileTime(handle
, &ftLocal
, 0, &ftLocal
))
352 data
->cb(data
->package
, data
->curfile
, MSICABEXTRACT_FILEEXTRACTED
, NULL
, NULL
,
355 msi_free(data
->curfile
);
356 data
->curfile
= NULL
;
361 static INT_PTR
cabinet_notify(FDINOTIFICATIONTYPE fdint
, PFDINOTIFICATION pfdin
)
363 TRACE("(%d)\n", fdint
);
367 case fdintPARTIAL_FILE
:
368 return cabinet_partial_file(fdint
, pfdin
);
370 case fdintNEXT_CABINET
:
371 return cabinet_next_cabinet(fdint
, pfdin
);
374 return cabinet_copy_file(fdint
, pfdin
);
376 case fdintCLOSE_FILE_INFO
:
377 return cabinet_close_file_info(fdint
, pfdin
);
384 /***********************************************************************
387 * Extract files from a cab file.
389 BOOL
msi_cabextract(MSIPACKAGE
* package
, MSIMEDIAINFO
*mi
, LPVOID data
)
391 LPSTR cabinet
, cab_path
= NULL
;
397 TRACE("Extracting %s\n", debugstr_w(mi
->source
));
399 hfdi
= FDICreate(cabinet_alloc
, cabinet_free
, cabinet_open
, cabinet_read
,
400 cabinet_write
, cabinet_close
, cabinet_seek
, 0, &erf
);
403 ERR("FDICreate failed\n");
407 ptr
= strrchrW(mi
->source
, '\\') + 1;
408 cabinet
= strdupWtoA(ptr
);
412 cab_path
= strdupWtoA(mi
->source
);
416 cab_path
[ptr
- mi
->source
] = '\0';
418 ret
= FDICopy(hfdi
, cabinet
, cab_path
, 0, cabinet_notify
, NULL
, data
);
420 ERR("FDICopy failed\n");
428 mi
->is_extracted
= TRUE
;
433 void msi_free_media_info(MSIMEDIAINFO
*mi
)
435 msi_free(mi
->disk_prompt
);
436 msi_free(mi
->cabinet
);
437 msi_free(mi
->volume_label
);
438 msi_free(mi
->first_volume
);
442 UINT
msi_load_media_info(MSIPACKAGE
*package
, MSIFILE
*file
, MSIMEDIAINFO
*mi
)
450 static const WCHAR query
[] = {
451 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
452 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
453 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
454 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
455 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
456 '`','D','i','s','k','I','d','`',0};
458 row
= MSI_QueryGetRecord(package
->db
, query
, file
->Sequence
, mi
->disk_id
);
461 TRACE("Unable to query row\n");
462 return ERROR_FUNCTION_FAILED
;
465 mi
->is_extracted
= FALSE
;
466 mi
->disk_id
= MSI_RecordGetInteger(row
, 1);
467 mi
->last_sequence
= MSI_RecordGetInteger(row
, 2);
468 msi_free(mi
->disk_prompt
);
469 mi
->disk_prompt
= strdupW(MSI_RecordGetString(row
, 3));
470 msi_free(mi
->cabinet
);
471 mi
->cabinet
= strdupW(MSI_RecordGetString(row
, 4));
472 msi_free(mi
->volume_label
);
473 mi
->volume_label
= strdupW(MSI_RecordGetString(row
, 5));
474 msiobj_release(&row
->hdr
);
476 if (!mi
->first_volume
)
477 mi
->first_volume
= strdupW(mi
->volume_label
);
479 source_dir
= msi_dup_property(package
, cszSourceDir
);
480 lstrcpyW(mi
->source
, source_dir
);
482 PathStripToRootW(source_dir
);
483 mi
->type
= GetDriveTypeW(source_dir
);
485 if (file
->IsCompressed
&& mi
->cabinet
)
487 if (mi
->cabinet
[0] == '#')
489 r
= writeout_cabinet_stream(package
, &mi
->cabinet
[1], mi
->source
);
490 if (r
!= ERROR_SUCCESS
)
492 ERR("Failed to extract cabinet stream\n");
493 return ERROR_FUNCTION_FAILED
;
497 lstrcatW(mi
->source
, mi
->cabinet
);
500 options
= MSICODE_PRODUCT
;
501 if (mi
->type
== DRIVE_CDROM
|| mi
->type
== DRIVE_REMOVABLE
)
504 options
|= MSISOURCETYPE_MEDIA
;
506 else if (package
->BaseURL
&& UrlIsW(package
->BaseURL
, URLIS_URL
))
508 source
= package
->BaseURL
;
509 options
|= MSISOURCETYPE_URL
;
514 options
|= MSISOURCETYPE_NETWORK
;
517 msi_package_add_media_disk(package
, package
->Context
,
518 MSICODE_PRODUCT
, mi
->disk_id
,
519 mi
->volume_label
, mi
->disk_prompt
);
521 msi_package_add_info(package
, package
->Context
,
522 options
, INSTALLPROPERTY_LASTUSEDSOURCEW
, source
);
524 msi_free(source_dir
);
525 return ERROR_SUCCESS
;
528 /* FIXME: search NETWORK and URL sources as well */
529 UINT
find_published_source(MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
)
531 WCHAR source
[MAX_PATH
];
532 WCHAR volume
[MAX_PATH
];
533 WCHAR prompt
[MAX_PATH
];
534 DWORD volumesz
, promptsz
;
535 DWORD index
, size
, id
;
539 r
= MsiSourceListGetInfoW(package
->ProductCode
, NULL
,
540 package
->Context
, MSICODE_PRODUCT
,
541 INSTALLPROPERTY_LASTUSEDSOURCEW
, source
, &size
);
542 if (r
!= ERROR_SUCCESS
)
548 while (MsiSourceListEnumMediaDisksW(package
->ProductCode
, NULL
,
550 MSICODE_PRODUCT
, index
++, &id
,
551 volume
, &volumesz
, prompt
, &promptsz
) == ERROR_SUCCESS
)
554 mi
->volume_label
= msi_realloc(mi
->volume_label
, ++volumesz
* sizeof(WCHAR
));
555 lstrcpyW(mi
->volume_label
, volume
);
556 mi
->disk_prompt
= msi_realloc(mi
->disk_prompt
, ++promptsz
* sizeof(WCHAR
));
557 lstrcpyW(mi
->disk_prompt
, prompt
);
559 if (source_matches_volume(mi
, source
))
561 /* FIXME: what about SourceDir */
562 lstrcpyW(mi
->source
, source
);
563 lstrcatW(mi
->source
, mi
->cabinet
);
564 return ERROR_SUCCESS
;
568 return ERROR_FUNCTION_FAILED
;
571 UINT
ready_media(MSIPACKAGE
*package
, MSIFILE
*file
, MSIMEDIAINFO
*mi
)
573 UINT rc
= ERROR_SUCCESS
;
575 /* media info for continuous cabinet is already loaded */
576 if (mi
->is_continuous
)
577 return ERROR_SUCCESS
;
579 rc
= msi_load_media_info(package
, file
, mi
);
580 if (rc
!= ERROR_SUCCESS
)
582 ERR("Unable to load media info\n");
583 return ERROR_FUNCTION_FAILED
;
586 /* cabinet is internal, no checks needed */
587 if (!mi
->cabinet
|| mi
->cabinet
[0] == '#')
588 return ERROR_SUCCESS
;
590 /* package should be downloaded */
591 if (file
->IsCompressed
&&
592 GetFileAttributesW(mi
->source
) == INVALID_FILE_ATTRIBUTES
&&
593 package
->BaseURL
&& UrlIsW(package
->BaseURL
, URLIS_URL
))
595 WCHAR temppath
[MAX_PATH
];
597 msi_download_file(mi
->source
, temppath
);
598 lstrcpyW(mi
->source
, temppath
);
599 return ERROR_SUCCESS
;
602 /* check volume matches, change media if not */
603 if (mi
->volume_label
&& mi
->disk_id
> 1 &&
604 lstrcmpW(mi
->first_volume
, mi
->volume_label
))
606 LPWSTR source
= msi_dup_property(package
, cszSourceDir
);
609 matches
= source_matches_volume(mi
, source
);
612 if ((mi
->type
== DRIVE_CDROM
|| mi
->type
== DRIVE_REMOVABLE
) && !matches
)
614 rc
= msi_change_media(package
, mi
);
615 if (rc
!= ERROR_SUCCESS
)
620 if (file
->IsCompressed
&&
621 GetFileAttributesW(mi
->source
) == INVALID_FILE_ATTRIBUTES
)
623 rc
= find_published_source(package
, mi
);
624 if (rc
!= ERROR_SUCCESS
)
626 ERR("Cabinet not found: %s\n", debugstr_w(mi
->source
));
627 return ERROR_INSTALL_FAILURE
;
631 return ERROR_SUCCESS
;