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"
31 #include "wine/unicode.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
35 /* from msvcrt/fcntl.h */
39 #define _O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR)
40 #define _O_APPEND 0x0008
41 #define _O_RANDOM 0x0010
42 #define _O_SEQUENTIAL 0x0020
43 #define _O_TEMPORARY 0x0040
44 #define _O_NOINHERIT 0x0080
45 #define _O_CREAT 0x0100
46 #define _O_TRUNC 0x0200
47 #define _O_EXCL 0x0400
48 #define _O_SHORT_LIVED 0x1000
49 #define _O_TEXT 0x4000
50 #define _O_BINARY 0x8000
52 static BOOL
source_matches_volume(MSIMEDIAINFO
*mi
, LPCWSTR source_root
)
54 WCHAR volume_name
[MAX_PATH
+ 1];
55 WCHAR root
[MAX_PATH
+ 1];
57 strcpyW(root
, source_root
);
58 PathStripToRootW(root
);
59 PathAddBackslashW(root
);
61 if (!GetVolumeInformationW(root
, volume_name
, MAX_PATH
+ 1,
62 NULL
, NULL
, NULL
, NULL
, 0))
64 ERR("Failed to get volume information\n");
68 return !lstrcmpW(mi
->volume_label
, volume_name
);
71 static UINT
msi_change_media(MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
)
74 LPWSTR error
, error_dialog
;
76 UINT r
= ERROR_SUCCESS
;
78 static const WCHAR szUILevel
[] = {'U','I','L','e','v','e','l',0};
79 static const WCHAR error_prop
[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
81 if ((msi_get_property_int(package
, szUILevel
, 0) & INSTALLUILEVEL_MASK
) ==
82 INSTALLUILEVEL_NONE
&& !gUIHandlerA
)
85 error
= generate_error_string(package
, 1302, 1, mi
->disk_prompt
);
86 error_dialog
= msi_dup_property(package
, error_prop
);
87 source_dir
= msi_dup_property(package
, cszSourceDir
);
89 while (r
== ERROR_SUCCESS
&&
90 !source_matches_volume(mi
, source_dir
))
92 r
= msi_spawn_error_dialog(package
, error_dialog
, error
);
96 msg
= strdupWtoA(error
);
97 gUIHandlerA(gUIContext
, MB_RETRYCANCEL
| INSTALLMESSAGE_ERROR
, msg
);
103 msi_free(error_dialog
);
104 msi_free(source_dir
);
109 static UINT
writeout_cabinet_stream(MSIPACKAGE
*package
, LPCWSTR stream
,
119 static const WCHAR cszTempFolder
[]= {
120 'T','e','m','p','F','o','l','d','e','r',0};
122 rc
= read_raw_stream_data(package
->db
, stream
, &data
, &size
);
123 if (rc
!= ERROR_SUCCESS
)
127 if (MSI_GetPropertyW(package
, cszTempFolder
, tmp
, &write
))
128 GetTempPathW(MAX_PATH
, tmp
);
130 GetTempFileNameW(tmp
, stream
, 0, source
);
132 track_tempfile(package
, source
);
133 hfile
= CreateFileW(source
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
134 FILE_ATTRIBUTE_NORMAL
, NULL
);
136 if (hfile
== INVALID_HANDLE_VALUE
)
138 ERR("Unable to create file %s\n", debugstr_w(source
));
139 rc
= ERROR_FUNCTION_FAILED
;
143 WriteFile(hfile
, data
, size
, &write
, NULL
);
145 TRACE("wrote %i bytes to %s\n", write
, debugstr_w(source
));
152 static void * CDECL
cabinet_alloc(ULONG cb
)
154 return msi_alloc(cb
);
157 static void CDECL
cabinet_free(void *pv
)
162 static INT_PTR CDECL
cabinet_open(char *pszFile
, int oflag
, int pmode
)
166 DWORD dwShareMode
= 0;
167 DWORD dwCreateDisposition
= OPEN_EXISTING
;
169 switch (oflag
& _O_ACCMODE
)
172 dwAccess
= GENERIC_READ
;
173 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_DELETE
;
176 dwAccess
= GENERIC_WRITE
;
177 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
180 dwAccess
= GENERIC_READ
| GENERIC_WRITE
;
181 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
185 if ((oflag
& (_O_CREAT
| _O_EXCL
)) == (_O_CREAT
| _O_EXCL
))
186 dwCreateDisposition
= CREATE_NEW
;
187 else if (oflag
& _O_CREAT
)
188 dwCreateDisposition
= CREATE_ALWAYS
;
190 handle
= CreateFileA(pszFile
, dwAccess
, dwShareMode
, NULL
,
191 dwCreateDisposition
, 0, NULL
);
192 if (handle
== INVALID_HANDLE_VALUE
)
195 return (INT_PTR
)handle
;
198 static UINT CDECL
cabinet_read(INT_PTR hf
, void *pv
, UINT cb
)
200 HANDLE handle
= (HANDLE
)hf
;
203 if (ReadFile(handle
, pv
, cb
, &read
, NULL
))
209 static UINT CDECL
cabinet_write(INT_PTR hf
, void *pv
, UINT cb
)
211 HANDLE handle
= (HANDLE
)hf
;
214 if (WriteFile(handle
, pv
, cb
, &written
, NULL
))
220 static int CDECL
cabinet_close(INT_PTR hf
)
222 HANDLE handle
= (HANDLE
)hf
;
223 return CloseHandle(handle
) ? 0 : -1;
226 static LONG CDECL
cabinet_seek(INT_PTR hf
, LONG dist
, int seektype
)
228 HANDLE handle
= (HANDLE
)hf
;
229 /* flags are compatible and so are passed straight through */
230 return SetFilePointer(handle
, dist
, NULL
, seektype
);
233 static UINT CDECL
msi_media_get_disk_info(MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
)
238 static const WCHAR query
[] = {
239 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
240 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
241 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
243 row
= MSI_QueryGetRecord(package
->db
, query
, mi
->disk_id
);
246 TRACE("Unable to query row\n");
247 return ERROR_FUNCTION_FAILED
;
250 mi
->disk_prompt
= strdupW(MSI_RecordGetString(row
, 3));
251 mi
->cabinet
= strdupW(MSI_RecordGetString(row
, 4));
252 mi
->volume_label
= strdupW(MSI_RecordGetString(row
, 5));
254 if (!mi
->first_volume
)
255 mi
->first_volume
= strdupW(mi
->volume_label
);
257 ptr
= strrchrW(mi
->source
, '\\') + 1;
258 lstrcpyW(ptr
, mi
->cabinet
);
259 msiobj_release(&row
->hdr
);
261 return ERROR_SUCCESS
;
264 static INT_PTR
cabinet_partial_file(FDINOTIFICATIONTYPE fdint
,
265 PFDINOTIFICATION pfdin
)
267 MSICABDATA
*data
= pfdin
->pv
;
268 data
->mi
->is_continuous
= FALSE
;
272 static INT_PTR
cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint
,
273 PFDINOTIFICATION pfdin
)
275 MSICABDATA
*data
= pfdin
->pv
;
276 MSIMEDIAINFO
*mi
= data
->mi
;
277 LPWSTR cab
= strdupAtoW(pfdin
->psz1
);
281 msi_free(mi
->disk_prompt
);
282 msi_free(mi
->cabinet
);
283 msi_free(mi
->volume_label
);
284 mi
->disk_prompt
= NULL
;
286 mi
->volume_label
= NULL
;
289 mi
->is_continuous
= TRUE
;
291 rc
= msi_media_get_disk_info(data
->package
, mi
);
292 if (rc
!= ERROR_SUCCESS
)
294 ERR("Failed to get next cabinet information: %d\n", rc
);
298 if (lstrcmpiW(mi
->cabinet
, cab
))
300 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
304 TRACE("Searching for %s\n", debugstr_w(mi
->source
));
307 if (GetFileAttributesW(mi
->source
) == INVALID_FILE_ATTRIBUTES
)
309 if (msi_change_media(data
->package
, mi
) != ERROR_SUCCESS
)
318 static INT_PTR
cabinet_copy_file(FDINOTIFICATIONTYPE fdint
,
319 PFDINOTIFICATION pfdin
)
321 MSICABDATA
*data
= pfdin
->pv
;
326 data
->curfile
= strdupAtoW(pfdin
->psz1
);
327 if (!data
->cb(data
->package
, data
->curfile
, MSICABEXTRACT_BEGINEXTRACT
, &path
,
331 TRACE("extracting %s\n", debugstr_w(path
));
333 attrs
= attrs
& (FILE_ATTRIBUTE_READONLY
|FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
);
334 if (!attrs
) attrs
= FILE_ATTRIBUTE_NORMAL
;
336 handle
= CreateFileW(path
, GENERIC_READ
| GENERIC_WRITE
, 0,
337 NULL
, CREATE_ALWAYS
, attrs
, NULL
);
338 if (handle
== INVALID_HANDLE_VALUE
)
340 DWORD err
= GetLastError();
341 DWORD attrs2
= GetFileAttributesW(path
);
343 if (attrs2
== INVALID_FILE_ATTRIBUTES
)
345 ERR("failed to create %s (error %d)\n", debugstr_w(path
), err
);
348 else if (err
== ERROR_ACCESS_DENIED
&& (attrs2
& FILE_ATTRIBUTE_READONLY
))
350 TRACE("removing read-only attribute on %s\n", debugstr_w(path
));
351 SetFileAttributesW( path
, attrs2
& ~FILE_ATTRIBUTE_READONLY
);
352 handle
= CreateFileW(path
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, attrs2
, NULL
);
354 if (handle
!= INVALID_HANDLE_VALUE
) goto done
;
355 err
= GetLastError();
357 if (err
== ERROR_SHARING_VIOLATION
)
359 static const WCHAR msiW
[] = {'m','s','i',0};
360 static const WCHAR slashW
[] = {'\\',0};
361 WCHAR tmpfileW
[MAX_PATH
], *tmppathW
, *p
;
364 TRACE("file in use, scheduling rename operation\n");
366 GetTempFileNameW(slashW
, msiW
, 0, tmpfileW
);
367 len
= strlenW(path
) + strlenW(tmpfileW
) + 1;
368 if (!(tmppathW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
))))
369 return ERROR_OUTOFMEMORY
;
371 strcpyW(tmppathW
, path
);
372 if ((p
= strrchrW(tmppathW
, '\\'))) *p
= 0;
373 strcatW(tmppathW
, tmpfileW
);
375 handle
= CreateFileW(tmppathW
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, attrs
, NULL
);
377 if (handle
!= INVALID_HANDLE_VALUE
&&
378 MoveFileExW(path
, NULL
, MOVEFILE_DELAY_UNTIL_REBOOT
) &&
379 MoveFileExW(tmppathW
, path
, MOVEFILE_DELAY_UNTIL_REBOOT
))
381 data
->package
->need_reboot
= 1;
384 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path
), GetLastError());
386 HeapFree(GetProcessHeap(), 0, tmppathW
);
389 WARN("failed to create %s (error %d)\n", debugstr_w(path
), err
);
395 return (INT_PTR
)handle
;
398 static INT_PTR
cabinet_close_file_info(FDINOTIFICATIONTYPE fdint
,
399 PFDINOTIFICATION pfdin
)
401 MSICABDATA
*data
= pfdin
->pv
;
404 HANDLE handle
= (HANDLE
)pfdin
->hf
;
406 data
->mi
->is_continuous
= FALSE
;
408 if (!DosDateTimeToFileTime(pfdin
->date
, pfdin
->time
, &ft
))
410 if (!LocalFileTimeToFileTime(&ft
, &ftLocal
))
412 if (!SetFileTime(handle
, &ftLocal
, 0, &ftLocal
))
417 data
->cb(data
->package
, data
->curfile
, MSICABEXTRACT_FILEEXTRACTED
, NULL
, NULL
,
420 msi_free(data
->curfile
);
421 data
->curfile
= NULL
;
426 static INT_PTR CDECL
cabinet_notify(FDINOTIFICATIONTYPE fdint
, PFDINOTIFICATION pfdin
)
428 TRACE("(%d)\n", fdint
);
432 case fdintPARTIAL_FILE
:
433 return cabinet_partial_file(fdint
, pfdin
);
435 case fdintNEXT_CABINET
:
436 return cabinet_next_cabinet(fdint
, pfdin
);
439 return cabinet_copy_file(fdint
, pfdin
);
441 case fdintCLOSE_FILE_INFO
:
442 return cabinet_close_file_info(fdint
, pfdin
);
449 /***********************************************************************
452 * Extract files from a cab file.
454 BOOL
msi_cabextract(MSIPACKAGE
* package
, MSIMEDIAINFO
*mi
, LPVOID data
)
456 LPSTR cabinet
, cab_path
= NULL
;
462 TRACE("Extracting %s\n", debugstr_w(mi
->source
));
464 hfdi
= FDICreate(cabinet_alloc
, cabinet_free
, cabinet_open
, cabinet_read
,
465 cabinet_write
, cabinet_close
, cabinet_seek
, 0, &erf
);
468 ERR("FDICreate failed\n");
472 ptr
= strrchrW(mi
->source
, '\\') + 1;
473 cabinet
= strdupWtoA(ptr
);
477 cab_path
= strdupWtoA(mi
->source
);
481 cab_path
[ptr
- mi
->source
] = '\0';
483 ret
= FDICopy(hfdi
, cabinet
, cab_path
, 0, cabinet_notify
, NULL
, data
);
485 ERR("FDICopy failed\n");
493 mi
->is_extracted
= TRUE
;
498 void msi_free_media_info(MSIMEDIAINFO
*mi
)
500 msi_free(mi
->disk_prompt
);
501 msi_free(mi
->cabinet
);
502 msi_free(mi
->volume_label
);
503 msi_free(mi
->first_volume
);
507 static UINT
get_drive_type(const WCHAR
*path
)
509 WCHAR root
[MAX_PATH
+ 1];
512 PathStripToRootW(root
);
513 PathAddBackslashW(root
);
515 return GetDriveTypeW(root
);
518 static UINT
msi_load_media_info(MSIPACKAGE
*package
, MSIFILE
*file
, MSIMEDIAINFO
*mi
)
526 static const WCHAR query
[] = {
527 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
528 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
529 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
530 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
531 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
532 '`','D','i','s','k','I','d','`',0};
534 row
= MSI_QueryGetRecord(package
->db
, query
, file
->Sequence
, mi
->disk_id
);
537 TRACE("Unable to query row\n");
538 return ERROR_FUNCTION_FAILED
;
541 mi
->is_extracted
= FALSE
;
542 mi
->disk_id
= MSI_RecordGetInteger(row
, 1);
543 mi
->last_sequence
= MSI_RecordGetInteger(row
, 2);
544 msi_free(mi
->disk_prompt
);
545 mi
->disk_prompt
= strdupW(MSI_RecordGetString(row
, 3));
546 msi_free(mi
->cabinet
);
547 mi
->cabinet
= strdupW(MSI_RecordGetString(row
, 4));
548 msi_free(mi
->volume_label
);
549 mi
->volume_label
= strdupW(MSI_RecordGetString(row
, 5));
550 msiobj_release(&row
->hdr
);
552 if (!mi
->first_volume
)
553 mi
->first_volume
= strdupW(mi
->volume_label
);
555 source_dir
= msi_dup_property(package
, cszSourceDir
);
556 lstrcpyW(mi
->source
, source_dir
);
557 mi
->type
= get_drive_type(source_dir
);
559 if (file
->IsCompressed
&& mi
->cabinet
)
561 if (mi
->cabinet
[0] == '#')
563 r
= writeout_cabinet_stream(package
, &mi
->cabinet
[1], mi
->source
);
564 if (r
!= ERROR_SUCCESS
)
566 ERR("Failed to extract cabinet stream\n");
567 return ERROR_FUNCTION_FAILED
;
571 lstrcatW(mi
->source
, mi
->cabinet
);
574 options
= MSICODE_PRODUCT
;
575 if (mi
->type
== DRIVE_CDROM
|| mi
->type
== DRIVE_REMOVABLE
)
578 options
|= MSISOURCETYPE_MEDIA
;
580 else if (package
->BaseURL
&& UrlIsW(package
->BaseURL
, URLIS_URL
))
582 source
= package
->BaseURL
;
583 options
|= MSISOURCETYPE_URL
;
588 options
|= MSISOURCETYPE_NETWORK
;
591 msi_package_add_media_disk(package
, package
->Context
,
592 MSICODE_PRODUCT
, mi
->disk_id
,
593 mi
->volume_label
, mi
->disk_prompt
);
595 msi_package_add_info(package
, package
->Context
,
596 options
, INSTALLPROPERTY_LASTUSEDSOURCEW
, source
);
598 msi_free(source_dir
);
599 return ERROR_SUCCESS
;
602 /* FIXME: search NETWORK and URL sources as well */
603 static UINT
find_published_source(MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
)
605 WCHAR source
[MAX_PATH
];
606 WCHAR volume
[MAX_PATH
];
607 WCHAR prompt
[MAX_PATH
];
608 DWORD volumesz
, promptsz
;
609 DWORD index
, size
, id
;
613 r
= MsiSourceListGetInfoW(package
->ProductCode
, NULL
,
614 package
->Context
, MSICODE_PRODUCT
,
615 INSTALLPROPERTY_LASTUSEDSOURCEW
, source
, &size
);
616 if (r
!= ERROR_SUCCESS
)
622 while (MsiSourceListEnumMediaDisksW(package
->ProductCode
, NULL
,
624 MSICODE_PRODUCT
, index
++, &id
,
625 volume
, &volumesz
, prompt
, &promptsz
) == ERROR_SUCCESS
)
628 mi
->volume_label
= msi_realloc(mi
->volume_label
, ++volumesz
* sizeof(WCHAR
));
629 lstrcpyW(mi
->volume_label
, volume
);
630 mi
->disk_prompt
= msi_realloc(mi
->disk_prompt
, ++promptsz
* sizeof(WCHAR
));
631 lstrcpyW(mi
->disk_prompt
, prompt
);
633 if (source_matches_volume(mi
, source
))
635 /* FIXME: what about SourceDir */
636 lstrcpyW(mi
->source
, source
);
637 return ERROR_SUCCESS
;
641 return ERROR_FUNCTION_FAILED
;
644 UINT
ready_media(MSIPACKAGE
*package
, MSIFILE
*file
, MSIMEDIAINFO
*mi
)
646 UINT rc
= ERROR_SUCCESS
;
648 /* media info for continuous cabinet is already loaded */
649 if (mi
->is_continuous
)
650 return ERROR_SUCCESS
;
652 rc
= msi_load_media_info(package
, file
, mi
);
653 if (rc
!= ERROR_SUCCESS
)
655 ERR("Unable to load media info\n");
656 return ERROR_FUNCTION_FAILED
;
659 /* cabinet is internal, no checks needed */
660 if (!mi
->cabinet
|| mi
->cabinet
[0] == '#')
661 return ERROR_SUCCESS
;
663 /* package should be downloaded */
664 if (file
->IsCompressed
&&
665 GetFileAttributesW(mi
->source
) == INVALID_FILE_ATTRIBUTES
&&
666 package
->BaseURL
&& UrlIsW(package
->BaseURL
, URLIS_URL
))
668 WCHAR temppath
[MAX_PATH
];
670 msi_download_file(mi
->source
, temppath
);
671 lstrcpyW(mi
->source
, temppath
);
672 return ERROR_SUCCESS
;
675 /* check volume matches, change media if not */
676 if (mi
->volume_label
&& mi
->disk_id
> 1 &&
677 lstrcmpW(mi
->first_volume
, mi
->volume_label
))
679 LPWSTR source
= msi_dup_property(package
, cszSourceDir
);
682 matches
= source_matches_volume(mi
, source
);
685 if ((mi
->type
== DRIVE_CDROM
|| mi
->type
== DRIVE_REMOVABLE
) && !matches
)
687 rc
= msi_change_media(package
, mi
);
688 if (rc
!= ERROR_SUCCESS
)
693 if (file
->IsCompressed
&&
694 GetFileAttributesW(mi
->source
) == INVALID_FILE_ATTRIBUTES
)
696 rc
= find_published_source(package
, mi
);
697 if (rc
!= ERROR_SUCCESS
)
699 ERR("Cabinet not found: %s\n", debugstr_w(mi
->source
));
700 return ERROR_INSTALL_FAILURE
;
704 return ERROR_SUCCESS
;