msvcrt: Use iob_func instead of p__iob on non-x86.
[wine/wine64.git] / dlls / msi / source.c
blobe8e4a113d5a6873a9a9fbef1822e9b1da97d3f16
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Aric Stewart for CodeWeavers
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
21 #include <stdarg.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "shlwapi.h"
31 #include "wine/debug.h"
32 #include "msi.h"
33 #include "msiquery.h"
34 #include "msipriv.h"
35 #include "wincrypt.h"
36 #include "winver.h"
37 #include "winuser.h"
38 #include "wine/unicode.h"
39 #include "sddl.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
44 * These apis are defined in MSI 3.0
47 typedef struct tagMediaInfo
49 struct list entry;
50 LPWSTR path;
51 WCHAR szIndex[10];
52 DWORD index;
53 } media_info;
55 static UINT OpenSourceKey(LPCWSTR szProduct, HKEY* key, DWORD dwOptions,
56 MSIINSTALLCONTEXT context, BOOL create)
58 HKEY rootkey = 0;
59 UINT rc = ERROR_FUNCTION_FAILED;
60 static const WCHAR szSourceList[] = {'S','o','u','r','c','e','L','i','s','t',0};
62 if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
64 if (dwOptions & MSICODE_PATCH)
65 rc = MSIREG_OpenUserPatchesKey(szProduct, &rootkey, create);
66 else
67 rc = MSIREG_OpenProductKey(szProduct, context, &rootkey, create);
69 else if (context == MSIINSTALLCONTEXT_USERMANAGED)
71 if (dwOptions & MSICODE_PATCH)
72 rc = MSIREG_OpenUserPatchesKey(szProduct, &rootkey, create);
73 else
74 rc = MSIREG_OpenProductKey(szProduct, context, &rootkey, create);
76 else if (context == MSIINSTALLCONTEXT_MACHINE)
78 if (dwOptions & MSICODE_PATCH)
79 rc = MSIREG_OpenPatchesKey(szProduct, &rootkey, create);
80 else
81 rc = MSIREG_OpenProductKey(szProduct, context, &rootkey, create);
84 if (rc != ERROR_SUCCESS)
86 if (dwOptions & MSICODE_PATCH)
87 return ERROR_UNKNOWN_PATCH;
88 else
89 return ERROR_UNKNOWN_PRODUCT;
92 if (create)
93 rc = RegCreateKeyW(rootkey, szSourceList, key);
94 else
96 rc = RegOpenKeyW(rootkey,szSourceList, key);
97 if (rc != ERROR_SUCCESS)
98 rc = ERROR_BAD_CONFIGURATION;
101 return rc;
104 static UINT OpenMediaSubkey(HKEY rootkey, HKEY *key, BOOL create)
106 UINT rc;
107 static const WCHAR media[] = {'M','e','d','i','a',0};
109 if (create)
110 rc = RegCreateKeyW(rootkey, media, key);
111 else
112 rc = RegOpenKeyW(rootkey,media, key);
114 return rc;
117 static UINT OpenNetworkSubkey(HKEY rootkey, HKEY *key, BOOL create)
119 UINT rc;
120 static const WCHAR net[] = {'N','e','t',0};
122 if (create)
123 rc = RegCreateKeyW(rootkey, net, key);
124 else
125 rc = RegOpenKeyW(rootkey, net, key);
127 return rc;
130 static UINT OpenURLSubkey(HKEY rootkey, HKEY *key, BOOL create)
132 UINT rc;
133 static const WCHAR URL[] = {'U','R','L',0};
135 if (create)
136 rc = RegCreateKeyW(rootkey, URL, key);
137 else
138 rc = RegOpenKeyW(rootkey, URL, key);
140 return rc;
143 /******************************************************************
144 * MsiSourceListEnumMediaDisksA (MSI.@)
146 UINT WINAPI MsiSourceListEnumMediaDisksA(LPCSTR szProductCodeOrPatchCode,
147 LPCSTR szUserSid, MSIINSTALLCONTEXT dwContext,
148 DWORD dwOptions, DWORD dwIndex, LPDWORD pdwDiskId,
149 LPSTR szVolumeLabel, LPDWORD pcchVolumeLabel,
150 LPSTR szDiskPrompt, LPDWORD pcchDiskPrompt)
152 LPWSTR product = NULL;
153 LPWSTR usersid = NULL;
154 LPWSTR volume = NULL;
155 LPWSTR prompt = NULL;
156 UINT r = ERROR_INVALID_PARAMETER;
158 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n", debugstr_a(szProductCodeOrPatchCode),
159 debugstr_a(szUserSid), dwContext, dwOptions, dwIndex, pdwDiskId,
160 szVolumeLabel, pcchVolumeLabel, szDiskPrompt, pcchDiskPrompt);
162 if (szDiskPrompt && !pcchDiskPrompt)
163 return ERROR_INVALID_PARAMETER;
165 if (szProductCodeOrPatchCode) product = strdupAtoW(szProductCodeOrPatchCode);
166 if (szUserSid) usersid = strdupAtoW(szUserSid);
168 /* FIXME: add tests for an invalid format */
170 if (pcchVolumeLabel)
171 volume = msi_alloc(*pcchVolumeLabel * sizeof(WCHAR));
173 if (pcchDiskPrompt)
174 prompt = msi_alloc(*pcchDiskPrompt * sizeof(WCHAR));
176 if (volume) *volume = '\0';
177 if (prompt) *prompt = '\0';
178 r = MsiSourceListEnumMediaDisksW(product, usersid, dwContext, dwOptions,
179 dwIndex, pdwDiskId, volume, pcchVolumeLabel,
180 prompt, pcchDiskPrompt);
181 if (r != ERROR_SUCCESS)
182 goto done;
184 if (szVolumeLabel && pcchVolumeLabel)
185 WideCharToMultiByte(CP_ACP, 0, volume, -1, szVolumeLabel,
186 *pcchVolumeLabel + 1, NULL, NULL);
188 if (szDiskPrompt)
189 WideCharToMultiByte(CP_ACP, 0, prompt, -1, szDiskPrompt,
190 *pcchDiskPrompt + 1, NULL, NULL);
192 done:
193 msi_free(product);
194 msi_free(usersid);
195 msi_free(volume);
196 msi_free(prompt);
198 return r;
201 /******************************************************************
202 * MsiSourceListEnumMediaDisksW (MSI.@)
204 UINT WINAPI MsiSourceListEnumMediaDisksW(LPCWSTR szProductCodeOrPatchCode,
205 LPCWSTR szUserSid, MSIINSTALLCONTEXT dwContext,
206 DWORD dwOptions, DWORD dwIndex, LPDWORD pdwDiskId,
207 LPWSTR szVolumeLabel, LPDWORD pcchVolumeLabel,
208 LPWSTR szDiskPrompt, LPDWORD pcchDiskPrompt)
210 WCHAR squished_pc[GUID_SIZE];
211 WCHAR convert[11];
212 LPWSTR value = NULL;
213 LPWSTR data = NULL;
214 LPWSTR ptr, ptr2;
215 HKEY source, media;
216 DWORD valuesz, datasz = 0;
217 DWORD type;
218 DWORD numvals, size;
219 LONG res;
220 UINT r;
221 static DWORD index = 0;
223 static const WCHAR fmt[] = {'#','%','d',0};
225 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p)\n", debugstr_w(szProductCodeOrPatchCode),
226 debugstr_w(szUserSid), dwContext, dwOptions, dwIndex, szVolumeLabel,
227 pcchVolumeLabel, szDiskPrompt, pcchDiskPrompt);
229 if (!szProductCodeOrPatchCode ||
230 !squash_guid(szProductCodeOrPatchCode, squished_pc))
231 return ERROR_INVALID_PARAMETER;
233 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
234 return ERROR_INVALID_PARAMETER;
236 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
237 return ERROR_INVALID_PARAMETER;
239 if (szDiskPrompt && !pcchDiskPrompt)
240 return ERROR_INVALID_PARAMETER;
242 if (dwIndex == 0)
243 index = 0;
245 if (dwIndex != index)
246 return ERROR_INVALID_PARAMETER;
248 r = OpenSourceKey(szProductCodeOrPatchCode, &source,
249 dwOptions, dwContext, FALSE);
250 if (r != ERROR_SUCCESS)
251 return r;
253 r = OpenMediaSubkey(source, &media, FALSE);
254 if (r != ERROR_SUCCESS)
256 RegCloseKey(source);
257 return ERROR_NO_MORE_ITEMS;
260 if (!pcchVolumeLabel && !pcchDiskPrompt)
262 r = RegEnumValueW(media, dwIndex, NULL, NULL, NULL,
263 &type, NULL, NULL);
264 goto done;
267 res = RegQueryInfoKeyW(media, NULL, NULL, NULL, NULL, NULL,
268 NULL, &numvals, &valuesz, &datasz, NULL, NULL);
269 if (res != ERROR_SUCCESS)
271 r = ERROR_BAD_CONFIGURATION;
272 goto done;
275 value = msi_alloc(++valuesz * sizeof(WCHAR));
276 data = msi_alloc(++datasz * sizeof(WCHAR));
277 if (!value || !data)
279 r = ERROR_OUTOFMEMORY;
280 goto done;
283 r = RegEnumValueW(media, dwIndex, value, &valuesz,
284 NULL, &type, (LPBYTE)data, &datasz);
285 if (r != ERROR_SUCCESS)
286 goto done;
288 if (pdwDiskId)
289 *pdwDiskId = atolW(value);
291 ptr2 = data;
292 ptr = strchrW(data, ';');
293 if (!ptr)
294 ptr = data;
295 else
296 *ptr = '\0';
298 if (pcchVolumeLabel)
300 if (type == REG_DWORD)
302 sprintfW(convert, fmt, *data);
303 size = lstrlenW(convert);
304 ptr2 = convert;
306 else
307 size = lstrlenW(data);
309 if (size >= *pcchVolumeLabel)
310 r = ERROR_MORE_DATA;
311 else if (szVolumeLabel)
312 lstrcpyW(szVolumeLabel, ptr2);
314 *pcchVolumeLabel = size;
317 if (pcchDiskPrompt)
319 if (!*ptr)
320 ptr++;
322 if (type == REG_DWORD)
324 sprintfW(convert, fmt, *ptr);
325 size = lstrlenW(convert);
326 ptr = convert;
328 else
329 size = lstrlenW(ptr);
331 size = lstrlenW(ptr);
332 if (size >= *pcchDiskPrompt)
333 r = ERROR_MORE_DATA;
334 else if (szDiskPrompt)
335 lstrcpyW(szDiskPrompt, ptr);
337 *pcchDiskPrompt = size;
340 index++;
342 done:
343 msi_free(value);
344 msi_free(data);
345 RegCloseKey(source);
347 return r;
350 /******************************************************************
351 * MsiSourceListEnumSourcesA (MSI.@)
353 UINT WINAPI MsiSourceListEnumSourcesA(LPCSTR szProductCodeOrPatch, LPCSTR szUserSid,
354 MSIINSTALLCONTEXT dwContext,
355 DWORD dwOptions, DWORD dwIndex,
356 LPSTR szSource, LPDWORD pcchSource)
358 LPWSTR product = NULL;
359 LPWSTR usersid = NULL;
360 LPWSTR source = NULL;
361 DWORD len = 0;
362 UINT r = ERROR_INVALID_PARAMETER;
363 static DWORD index = 0;
365 TRACE("(%s, %s, %d, %d, %d, %p, %p)\n", debugstr_a(szProductCodeOrPatch),
366 debugstr_a(szUserSid), dwContext, dwOptions, dwIndex, szSource, pcchSource);
368 if (dwIndex == 0)
369 index = 0;
371 if (szSource && !pcchSource)
372 goto done;
374 if (dwIndex != index)
375 goto done;
377 if (szProductCodeOrPatch) product = strdupAtoW(szProductCodeOrPatch);
378 if (szUserSid) usersid = strdupAtoW(szUserSid);
380 r = MsiSourceListEnumSourcesW(product, usersid, dwContext, dwOptions,
381 dwIndex, NULL, &len);
382 if (r != ERROR_SUCCESS)
383 goto done;
385 source = msi_alloc(++len * sizeof(WCHAR));
386 if (!source)
388 r = ERROR_OUTOFMEMORY;
389 goto done;
392 *source = '\0';
393 r = MsiSourceListEnumSourcesW(product, usersid, dwContext, dwOptions,
394 dwIndex, source, &len);
395 if (r != ERROR_SUCCESS)
396 goto done;
398 len = WideCharToMultiByte(CP_ACP, 0, source, -1, NULL, 0, NULL, NULL);
399 if (pcchSource && *pcchSource >= len)
400 WideCharToMultiByte(CP_ACP, 0, source, -1, szSource, len, NULL, NULL);
401 else if (szSource)
402 r = ERROR_MORE_DATA;
404 if (pcchSource)
405 *pcchSource = len - 1;
407 done:
408 msi_free(product);
409 msi_free(usersid);
410 msi_free(source);
412 if (r == ERROR_SUCCESS)
414 if (szSource || !pcchSource) index++;
416 else if (dwIndex > index)
417 index = 0;
419 return r;
422 /******************************************************************
423 * MsiSourceListEnumSourcesW (MSI.@)
425 UINT WINAPI MsiSourceListEnumSourcesW(LPCWSTR szProductCodeOrPatch, LPCWSTR szUserSid,
426 MSIINSTALLCONTEXT dwContext,
427 DWORD dwOptions, DWORD dwIndex,
428 LPWSTR szSource, LPDWORD pcchSource)
430 WCHAR squished_pc[GUID_SIZE];
431 WCHAR name[32];
432 HKEY source = NULL;
433 HKEY subkey = NULL;
434 LONG res;
435 UINT r = ERROR_INVALID_PARAMETER;
436 static DWORD index = 0;
438 static const WCHAR format[] = {'%','d',0};
440 TRACE("(%s, %s, %d, %d, %d, %p, %p)\n", debugstr_w(szProductCodeOrPatch),
441 debugstr_w(szUserSid), dwContext, dwOptions, dwIndex, szSource, pcchSource);
443 if (dwIndex == 0)
444 index = 0;
446 if (!szProductCodeOrPatch || !squash_guid(szProductCodeOrPatch, squished_pc))
447 goto done;
449 if (szSource && !pcchSource)
450 goto done;
452 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
453 goto done;
455 if ((dwOptions & MSISOURCETYPE_NETWORK) && (dwOptions & MSISOURCETYPE_URL))
456 goto done;
458 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
459 goto done;
461 if (dwIndex != index)
462 goto done;
464 r = OpenSourceKey(szProductCodeOrPatch, &source,
465 dwOptions, dwContext, FALSE);
466 if (r != ERROR_SUCCESS)
467 goto done;
469 if (dwOptions & MSISOURCETYPE_NETWORK)
470 r = OpenNetworkSubkey(source, &subkey, FALSE);
471 else if (dwOptions & MSISOURCETYPE_URL)
472 r = OpenURLSubkey(source, &subkey, FALSE);
474 if (r != ERROR_SUCCESS)
476 r = ERROR_NO_MORE_ITEMS;
477 goto done;
480 sprintfW(name, format, dwIndex + 1);
482 res = RegQueryValueExW(subkey, name, 0, 0, (LPBYTE)szSource, pcchSource);
483 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA)
484 r = ERROR_NO_MORE_ITEMS;
486 done:
487 RegCloseKey(subkey);
488 RegCloseKey(source);
490 if (r == ERROR_SUCCESS)
492 if (szSource || !pcchSource) index++;
494 else if (dwIndex > index)
495 index = 0;
497 return r;
500 /******************************************************************
501 * MsiSourceListGetInfoA (MSI.@)
503 UINT WINAPI MsiSourceListGetInfoA( LPCSTR szProduct, LPCSTR szUserSid,
504 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
505 LPCSTR szProperty, LPSTR szValue,
506 LPDWORD pcchValue)
508 UINT ret;
509 LPWSTR product = NULL;
510 LPWSTR usersid = NULL;
511 LPWSTR property = NULL;
512 LPWSTR value = NULL;
513 DWORD len = 0;
515 if (szValue && !pcchValue)
516 return ERROR_INVALID_PARAMETER;
518 if (szProduct) product = strdupAtoW(szProduct);
519 if (szUserSid) usersid = strdupAtoW(szUserSid);
520 if (szProperty) property = strdupAtoW(szProperty);
522 ret = MsiSourceListGetInfoW(product, usersid, dwContext, dwOptions,
523 property, NULL, &len);
524 if (ret != ERROR_SUCCESS)
525 goto done;
527 value = msi_alloc(++len * sizeof(WCHAR));
528 if (!value)
529 return ERROR_OUTOFMEMORY;
531 *value = '\0';
532 ret = MsiSourceListGetInfoW(product, usersid, dwContext, dwOptions,
533 property, value, &len);
534 if (ret != ERROR_SUCCESS)
535 goto done;
537 len = WideCharToMultiByte(CP_ACP, 0, value, -1, NULL, 0, NULL, NULL);
538 if (*pcchValue >= len)
539 WideCharToMultiByte(CP_ACP, 0, value, -1, szValue, len, NULL, NULL);
540 else if (szValue)
541 ret = ERROR_MORE_DATA;
543 *pcchValue = len - 1;
545 done:
546 msi_free(product);
547 msi_free(usersid);
548 msi_free(property);
549 msi_free(value);
550 return ret;
553 /******************************************************************
554 * MsiSourceListGetInfoW (MSI.@)
556 UINT WINAPI MsiSourceListGetInfoW( LPCWSTR szProduct, LPCWSTR szUserSid,
557 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
558 LPCWSTR szProperty, LPWSTR szValue,
559 LPDWORD pcchValue)
561 WCHAR squished_pc[GUID_SIZE];
562 HKEY sourcekey, media;
563 LPWSTR source, ptr;
564 DWORD size;
565 UINT rc;
567 static const WCHAR mediapack[] = {
568 'M','e','d','i','a','P','a','c','k','a','g','e',0};
570 TRACE("%s %s\n", debugstr_w(szProduct), debugstr_w(szProperty));
572 if (!szProduct || !squash_guid(szProduct, squished_pc))
573 return ERROR_INVALID_PARAMETER;
575 if (szValue && !pcchValue)
576 return ERROR_INVALID_PARAMETER;
578 if (dwContext != MSIINSTALLCONTEXT_USERMANAGED &&
579 dwContext != MSIINSTALLCONTEXT_USERUNMANAGED &&
580 dwContext != MSIINSTALLCONTEXT_MACHINE)
581 return ERROR_INVALID_PARAMETER;
583 if (!szProperty)
584 return ERROR_INVALID_PARAMETER;
586 if (szUserSid)
587 FIXME("Unhandled UserSid %s\n",debugstr_w(szUserSid));
589 if (dwContext != MSIINSTALLCONTEXT_USERUNMANAGED)
590 FIXME("Unhandled context %d\n", dwContext);
592 rc = OpenSourceKey(szProduct, &sourcekey, dwOptions, dwContext, FALSE);
593 if (rc != ERROR_SUCCESS)
594 return rc;
596 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW) ||
597 !lstrcmpW(szProperty, INSTALLPROPERTY_DISKPROMPTW))
599 rc = OpenMediaSubkey(sourcekey, &media, FALSE);
600 if (rc != ERROR_SUCCESS)
602 RegCloseKey(sourcekey);
603 return ERROR_SUCCESS;
606 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW))
607 szProperty = mediapack;
609 RegQueryValueExW(media, szProperty, 0, 0, (LPBYTE)szValue, pcchValue);
610 RegCloseKey(media);
612 else if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW) ||
613 !lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDTYPEW))
615 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
616 0, 0, NULL, &size);
617 if (rc != ERROR_SUCCESS)
619 RegCloseKey(sourcekey);
620 return ERROR_SUCCESS;
623 source = msi_alloc(size);
624 RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
625 0, 0, (LPBYTE)source, &size);
627 if (!*source)
629 msi_free(source);
630 RegCloseKey(sourcekey);
631 return ERROR_SUCCESS;
634 if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDTYPEW))
636 if (*source != 'n' && *source != 'u' && *source != 'm')
638 msi_free(source);
639 RegCloseKey(sourcekey);
640 return ERROR_SUCCESS;
643 ptr = source;
644 source[1] = '\0';
646 else
648 ptr = strrchrW(source, ';');
649 if (!ptr)
650 ptr = source;
651 else
652 ptr++;
655 if (szValue)
657 if (strlenW(ptr) < *pcchValue)
658 lstrcpyW(szValue, ptr);
659 else
660 rc = ERROR_MORE_DATA;
663 *pcchValue = lstrlenW(ptr);
664 msi_free(source);
666 else if (strcmpW(INSTALLPROPERTY_PACKAGENAMEW, szProperty)==0)
668 *pcchValue = *pcchValue * sizeof(WCHAR);
669 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_PACKAGENAMEW, 0, 0,
670 (LPBYTE)szValue, pcchValue);
671 if (rc != ERROR_SUCCESS && rc != ERROR_MORE_DATA)
673 *pcchValue = 0;
674 rc = ERROR_SUCCESS;
676 else
678 if (*pcchValue)
679 *pcchValue = (*pcchValue - 1) / sizeof(WCHAR);
680 if (szValue)
681 szValue[*pcchValue] = '\0';
684 else
686 FIXME("Unknown property %s\n",debugstr_w(szProperty));
687 rc = ERROR_UNKNOWN_PROPERTY;
690 RegCloseKey(sourcekey);
691 return rc;
694 /******************************************************************
695 * MsiSourceListSetInfoA (MSI.@)
697 UINT WINAPI MsiSourceListSetInfoA(LPCSTR szProduct, LPCSTR szUserSid,
698 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
699 LPCSTR szProperty, LPCSTR szValue)
701 UINT ret;
702 LPWSTR product = NULL;
703 LPWSTR usersid = NULL;
704 LPWSTR property = NULL;
705 LPWSTR value = NULL;
707 if (szProduct) product = strdupAtoW(szProduct);
708 if (szUserSid) usersid = strdupAtoW(szUserSid);
709 if (szProperty) property = strdupAtoW(szProperty);
710 if (szValue) value = strdupAtoW(szValue);
712 ret = MsiSourceListSetInfoW(product, usersid, dwContext, dwOptions,
713 property, value);
715 msi_free(product);
716 msi_free(usersid);
717 msi_free(property);
718 msi_free(value);
720 return ret;
723 UINT msi_set_last_used_source(LPCWSTR product, LPCWSTR usersid,
724 MSIINSTALLCONTEXT context, DWORD options,
725 LPCWSTR value)
727 HKEY source;
728 LPWSTR buffer;
729 WCHAR typechar;
730 DWORD size;
731 UINT r;
732 int index = 1;
734 static const WCHAR format[] = {'%','c',';','%','i',';','%','s',0};
736 if (options & MSISOURCETYPE_NETWORK)
737 typechar = 'n';
738 else if (options & MSISOURCETYPE_URL)
739 typechar = 'u';
740 else if (options & MSISOURCETYPE_MEDIA)
741 typechar = 'm';
742 else
743 return ERROR_INVALID_PARAMETER;
745 if (!(options & MSISOURCETYPE_MEDIA))
747 r = MsiSourceListAddSourceExW(product, usersid, context,
748 options, value, 0);
749 if (r != ERROR_SUCCESS)
750 return r;
752 index = 0;
753 while ((r = MsiSourceListEnumSourcesW(product, usersid, context, options,
754 index, NULL, NULL)) == ERROR_SUCCESS)
755 index++;
757 if (r != ERROR_NO_MORE_ITEMS)
758 return r;
761 size = (lstrlenW(format) + lstrlenW(value) + 7) * sizeof(WCHAR);
762 buffer = msi_alloc(size);
763 if (!buffer)
764 return ERROR_OUTOFMEMORY;
766 r = OpenSourceKey(product, &source, MSICODE_PRODUCT, context, FALSE);
767 if (r != ERROR_SUCCESS)
768 return r;
770 sprintfW(buffer, format, typechar, index, value);
772 size = (lstrlenW(buffer) + 1) * sizeof(WCHAR);
773 r = RegSetValueExW(source, INSTALLPROPERTY_LASTUSEDSOURCEW, 0,
774 REG_SZ, (LPBYTE)buffer, size);
775 msi_free(buffer);
777 RegCloseKey(source);
778 return r;
781 /******************************************************************
782 * MsiSourceListSetInfoW (MSI.@)
784 UINT WINAPI MsiSourceListSetInfoW( LPCWSTR szProduct, LPCWSTR szUserSid,
785 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
786 LPCWSTR szProperty, LPCWSTR szValue)
788 WCHAR squished_pc[GUID_SIZE];
789 HKEY sourcekey, media;
790 LPCWSTR property;
791 UINT rc;
793 static const WCHAR media_package[] = {
794 'M','e','d','i','a','P','a','c','k','a','g','e',0
797 TRACE("%s %s %x %x %s %s\n", debugstr_w(szProduct), debugstr_w(szUserSid),
798 dwContext, dwOptions, debugstr_w(szProperty), debugstr_w(szValue));
800 if (!szProduct || !squash_guid(szProduct, squished_pc))
801 return ERROR_INVALID_PARAMETER;
803 if (!szProperty)
804 return ERROR_INVALID_PARAMETER;
806 if (!szValue)
807 return ERROR_UNKNOWN_PROPERTY;
809 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
810 return ERROR_INVALID_PARAMETER;
812 if (dwOptions & MSICODE_PATCH)
814 FIXME("Unhandled options MSICODE_PATCH\n");
815 return ERROR_UNKNOWN_PATCH;
818 property = szProperty;
819 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW))
820 property = media_package;
822 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
823 if (rc != ERROR_SUCCESS)
824 return rc;
826 if (lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW) &&
827 dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL))
829 RegCloseKey(sourcekey);
830 return ERROR_INVALID_PARAMETER;
833 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW) ||
834 !lstrcmpW(szProperty, INSTALLPROPERTY_DISKPROMPTW))
836 rc = OpenMediaSubkey(sourcekey, &media, TRUE);
837 if (rc == ERROR_SUCCESS)
839 rc = msi_reg_set_val_str(media, property, szValue);
840 RegCloseKey(media);
843 else if (strcmpW(INSTALLPROPERTY_PACKAGENAMEW, szProperty)==0)
845 DWORD size = (lstrlenW(szValue) + 1) * sizeof(WCHAR);
846 rc = RegSetValueExW(sourcekey, INSTALLPROPERTY_PACKAGENAMEW, 0,
847 REG_SZ, (const BYTE *)szValue, size);
848 if (rc != ERROR_SUCCESS)
849 rc = ERROR_UNKNOWN_PROPERTY;
851 else if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW))
853 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
854 rc = ERROR_INVALID_PARAMETER;
855 else
856 rc = msi_set_last_used_source(szProduct, szUserSid, dwContext,
857 dwOptions, szValue);
859 else
860 rc = ERROR_UNKNOWN_PROPERTY;
862 RegCloseKey(sourcekey);
863 return rc;
866 /******************************************************************
867 * MsiSourceListAddSourceW (MSI.@)
869 UINT WINAPI MsiSourceListAddSourceW( LPCWSTR szProduct, LPCWSTR szUserName,
870 DWORD dwReserved, LPCWSTR szSource)
872 WCHAR squished_pc[GUID_SIZE];
873 INT ret;
874 LPWSTR sidstr = NULL;
875 DWORD sidsize = 0;
876 DWORD domsize = 0;
877 DWORD context;
878 HKEY hkey = 0;
879 UINT r;
881 TRACE("%s %s %s\n", debugstr_w(szProduct), debugstr_w(szUserName), debugstr_w(szSource));
883 if (!szSource || !*szSource)
884 return ERROR_INVALID_PARAMETER;
886 if (dwReserved != 0)
887 return ERROR_INVALID_PARAMETER;
889 if (!szProduct || !squash_guid(szProduct, squished_pc))
890 return ERROR_INVALID_PARAMETER;
892 if (!szUserName || !*szUserName)
893 context = MSIINSTALLCONTEXT_MACHINE;
894 else
896 if (LookupAccountNameW(NULL, szUserName, NULL, &sidsize, NULL, &domsize, NULL))
898 PSID psid = msi_alloc(sidsize);
900 if (LookupAccountNameW(NULL, szUserName, psid, &sidsize, NULL, &domsize, NULL))
901 ConvertSidToStringSidW(psid, &sidstr);
903 msi_free(psid);
906 r = MSIREG_OpenProductKey(szProduct, MSIINSTALLCONTEXT_USERMANAGED,
907 &hkey, FALSE);
908 if (r == ERROR_SUCCESS)
909 context = MSIINSTALLCONTEXT_USERMANAGED;
910 else
912 r = MSIREG_OpenProductKey(szProduct, MSIINSTALLCONTEXT_USERUNMANAGED,
913 &hkey, FALSE);
914 if (r != ERROR_SUCCESS)
915 return ERROR_UNKNOWN_PRODUCT;
917 context = MSIINSTALLCONTEXT_USERUNMANAGED;
920 RegCloseKey(hkey);
923 ret = MsiSourceListAddSourceExW(szProduct, sidstr,
924 context, MSISOURCETYPE_NETWORK, szSource, 0);
926 if (sidstr)
927 LocalFree(sidstr);
929 return ret;
932 /******************************************************************
933 * MsiSourceListAddSourceA (MSI.@)
935 UINT WINAPI MsiSourceListAddSourceA( LPCSTR szProduct, LPCSTR szUserName,
936 DWORD dwReserved, LPCSTR szSource)
938 INT ret;
939 LPWSTR szwproduct;
940 LPWSTR szwusername;
941 LPWSTR szwsource;
943 szwproduct = strdupAtoW( szProduct );
944 szwusername = strdupAtoW( szUserName );
945 szwsource = strdupAtoW( szSource );
947 ret = MsiSourceListAddSourceW(szwproduct, szwusername, dwReserved, szwsource);
949 msi_free(szwproduct);
950 msi_free(szwusername);
951 msi_free(szwsource);
953 return ret;
956 /******************************************************************
957 * MsiSourceListAddSourceExA (MSI.@)
959 UINT WINAPI MsiSourceListAddSourceExA(LPCSTR szProduct, LPCSTR szUserSid,
960 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCSTR szSource, DWORD dwIndex)
962 UINT ret;
963 LPWSTR product, usersid, source;
965 product = strdupAtoW(szProduct);
966 usersid = strdupAtoW(szUserSid);
967 source = strdupAtoW(szSource);
969 ret = MsiSourceListAddSourceExW(product, usersid, dwContext,
970 dwOptions, source, dwIndex);
972 msi_free(product);
973 msi_free(usersid);
974 msi_free(source);
976 return ret;
979 static void free_source_list(struct list *sourcelist)
981 while (!list_empty(sourcelist))
983 media_info *info = LIST_ENTRY(list_head(sourcelist), media_info, entry);
984 list_remove(&info->entry);
985 msi_free(info->path);
986 msi_free(info);
990 static void add_source_to_list(struct list *sourcelist, media_info *info,
991 DWORD *index)
993 media_info *iter;
994 BOOL found = FALSE;
995 static const WCHAR fmt[] = {'%','i',0};
997 if (index) *index = 0;
999 if (list_empty(sourcelist))
1001 list_add_head(sourcelist, &info->entry);
1002 return;
1005 LIST_FOR_EACH_ENTRY(iter, sourcelist, media_info, entry)
1007 if (!found && info->index < iter->index)
1009 found = TRUE;
1010 list_add_before(&iter->entry, &info->entry);
1013 /* update the rest of the list */
1014 if (found)
1015 sprintfW(iter->szIndex, fmt, ++iter->index);
1016 else if (index)
1017 (*index)++;
1020 if (!found)
1021 list_add_after(&iter->entry, &info->entry);
1024 static UINT fill_source_list(struct list *sourcelist, HKEY sourcekey, DWORD *count)
1026 UINT r = ERROR_SUCCESS;
1027 DWORD index = 0;
1028 WCHAR name[10];
1029 DWORD size, val_size;
1030 media_info *entry;
1032 *count = 0;
1034 while (r == ERROR_SUCCESS)
1036 size = sizeof(name) / sizeof(name[0]);
1037 r = RegEnumValueW(sourcekey, index, name, &size, NULL, NULL, NULL, &val_size);
1038 if (r != ERROR_SUCCESS)
1039 return r;
1041 entry = msi_alloc(sizeof(media_info));
1042 if (!entry)
1043 goto error;
1045 entry->path = msi_alloc(val_size);
1046 if (!entry->path)
1048 msi_free(entry);
1049 goto error;
1052 lstrcpyW(entry->szIndex, name);
1053 entry->index = atoiW(name);
1055 size++;
1056 r = RegEnumValueW(sourcekey, index, name, &size, NULL,
1057 NULL, (LPBYTE)entry->path, &val_size);
1058 if (r != ERROR_SUCCESS)
1060 msi_free(entry->path);
1061 msi_free(entry);
1062 goto error;
1065 index = ++(*count);
1066 add_source_to_list(sourcelist, entry, NULL);
1069 error:
1070 *count = -1;
1071 free_source_list(sourcelist);
1072 return ERROR_OUTOFMEMORY;
1075 /******************************************************************
1076 * MsiSourceListAddSourceExW (MSI.@)
1078 UINT WINAPI MsiSourceListAddSourceExW( LPCWSTR szProduct, LPCWSTR szUserSid,
1079 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCWSTR szSource,
1080 DWORD dwIndex)
1082 HKEY sourcekey;
1083 HKEY typekey;
1084 UINT rc;
1085 struct list sourcelist;
1086 media_info *info;
1087 WCHAR squished_pc[GUID_SIZE];
1088 WCHAR name[10];
1089 LPWSTR source;
1090 LPCWSTR postfix;
1091 DWORD size, count;
1092 DWORD index;
1094 static const WCHAR fmt[] = {'%','i',0};
1095 static const WCHAR one[] = {'1',0};
1096 static const WCHAR backslash[] = {'\\',0};
1097 static const WCHAR forwardslash[] = {'/',0};
1099 TRACE("%s %s %x %x %s %i\n", debugstr_w(szProduct), debugstr_w(szUserSid),
1100 dwContext, dwOptions, debugstr_w(szSource), dwIndex);
1102 if (!szProduct || !squash_guid(szProduct, squished_pc))
1103 return ERROR_INVALID_PARAMETER;
1105 if (!szSource || !*szSource)
1106 return ERROR_INVALID_PARAMETER;
1108 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
1109 return ERROR_INVALID_PARAMETER;
1111 if (dwOptions & MSICODE_PATCH)
1113 FIXME("Unhandled options MSICODE_PATCH\n");
1114 return ERROR_FUNCTION_FAILED;
1117 if (szUserSid && (dwContext & MSIINSTALLCONTEXT_MACHINE))
1118 return ERROR_INVALID_PARAMETER;
1120 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1121 if (rc != ERROR_SUCCESS)
1122 return rc;
1124 if (dwOptions & MSISOURCETYPE_NETWORK)
1125 rc = OpenNetworkSubkey(sourcekey, &typekey, TRUE);
1126 else if (dwOptions & MSISOURCETYPE_URL)
1127 rc = OpenURLSubkey(sourcekey, &typekey, TRUE);
1128 else if (dwOptions & MSISOURCETYPE_MEDIA)
1129 rc = OpenMediaSubkey(sourcekey, &typekey, TRUE);
1130 else
1132 ERR("unknown media type: %08x\n", dwOptions);
1133 RegCloseKey(sourcekey);
1134 return ERROR_FUNCTION_FAILED;
1137 postfix = (dwOptions & MSISOURCETYPE_NETWORK) ? backslash : forwardslash;
1138 if (szSource[lstrlenW(szSource) - 1] == *postfix)
1139 source = strdupW(szSource);
1140 else
1142 size = lstrlenW(szSource) + 2;
1143 source = msi_alloc(size * sizeof(WCHAR));
1144 lstrcpyW(source, szSource);
1145 lstrcatW(source, postfix);
1148 list_init(&sourcelist);
1149 rc = fill_source_list(&sourcelist, typekey, &count);
1150 if (rc != ERROR_NO_MORE_ITEMS)
1151 return rc;
1153 size = (lstrlenW(source) + 1) * sizeof(WCHAR);
1155 if (count == 0)
1157 rc = RegSetValueExW(typekey, one, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1158 goto done;
1160 else if (dwIndex > count || dwIndex == 0)
1162 sprintfW(name, fmt, count + 1);
1163 rc = RegSetValueExW(typekey, name, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1164 goto done;
1166 else
1168 sprintfW(name, fmt, dwIndex);
1169 info = msi_alloc(sizeof(media_info));
1170 if (!info)
1172 rc = ERROR_OUTOFMEMORY;
1173 goto done;
1176 info->path = strdupW(source);
1177 lstrcpyW(info->szIndex, name);
1178 info->index = dwIndex;
1179 add_source_to_list(&sourcelist, info, &index);
1181 LIST_FOR_EACH_ENTRY(info, &sourcelist, media_info, entry)
1183 if (info->index < index)
1184 continue;
1186 size = (lstrlenW(info->path) + 1) * sizeof(WCHAR);
1187 rc = RegSetValueExW(typekey, info->szIndex, 0,
1188 REG_EXPAND_SZ, (LPBYTE)info->path, size);
1189 if (rc != ERROR_SUCCESS)
1190 goto done;
1194 done:
1195 free_source_list(&sourcelist);
1196 msi_free(source);
1197 RegCloseKey(typekey);
1198 RegCloseKey(sourcekey);
1199 return rc;
1202 /******************************************************************
1203 * MsiSourceListAddMediaDiskA (MSI.@)
1205 UINT WINAPI MsiSourceListAddMediaDiskA(LPCSTR szProduct, LPCSTR szUserSid,
1206 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1207 LPCSTR szVolumeLabel, LPCSTR szDiskPrompt)
1209 UINT r;
1210 LPWSTR product = NULL;
1211 LPWSTR usersid = NULL;
1212 LPWSTR volume = NULL;
1213 LPWSTR prompt = NULL;
1215 if (szProduct) product = strdupAtoW(szProduct);
1216 if (szUserSid) usersid = strdupAtoW(szUserSid);
1217 if (szVolumeLabel) volume = strdupAtoW(szVolumeLabel);
1218 if (szDiskPrompt) prompt = strdupAtoW(szDiskPrompt);
1220 r = MsiSourceListAddMediaDiskW(product, usersid, dwContext, dwOptions,
1221 dwDiskId, volume, prompt);
1223 msi_free(product);
1224 msi_free(usersid);
1225 msi_free(volume);
1226 msi_free(prompt);
1228 return r;
1231 /******************************************************************
1232 * MsiSourceListAddMediaDiskW (MSI.@)
1234 UINT WINAPI MsiSourceListAddMediaDiskW(LPCWSTR szProduct, LPCWSTR szUserSid,
1235 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1236 LPCWSTR szVolumeLabel, LPCWSTR szDiskPrompt)
1238 HKEY sourcekey;
1239 HKEY mediakey;
1240 UINT rc;
1241 WCHAR szIndex[10];
1242 WCHAR squished_pc[GUID_SIZE];
1243 LPWSTR buffer;
1244 DWORD size;
1246 static const WCHAR fmt[] = {'%','i',0};
1247 static const WCHAR semicolon[] = {';',0};
1249 TRACE("%s %s %x %x %i %s %s\n", debugstr_w(szProduct),
1250 debugstr_w(szUserSid), dwContext, dwOptions, dwDiskId,
1251 debugstr_w(szVolumeLabel), debugstr_w(szDiskPrompt));
1253 if (!szProduct || !squash_guid(szProduct, squished_pc))
1254 return ERROR_INVALID_PARAMETER;
1256 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
1257 return ERROR_INVALID_PARAMETER;
1259 if ((szVolumeLabel && !*szVolumeLabel) || (szDiskPrompt && !*szDiskPrompt))
1260 return ERROR_INVALID_PARAMETER;
1262 if ((dwContext & MSIINSTALLCONTEXT_MACHINE) && szUserSid)
1263 return ERROR_INVALID_PARAMETER;
1265 if (dwOptions & MSICODE_PATCH)
1267 FIXME("Unhandled options MSICODE_PATCH\n");
1268 return ERROR_FUNCTION_FAILED;
1271 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1272 if (rc != ERROR_SUCCESS)
1273 return rc;
1275 OpenMediaSubkey(sourcekey, &mediakey, TRUE);
1277 sprintfW(szIndex, fmt, dwDiskId);
1279 size = 2;
1280 if (szVolumeLabel) size += lstrlenW(szVolumeLabel);
1281 if (szDiskPrompt) size += lstrlenW(szDiskPrompt);
1283 size *= sizeof(WCHAR);
1284 buffer = msi_alloc(size);
1285 *buffer = '\0';
1287 if (szVolumeLabel) lstrcpyW(buffer, szVolumeLabel);
1288 lstrcatW(buffer, semicolon);
1289 if (szDiskPrompt) lstrcatW(buffer, szDiskPrompt);
1291 RegSetValueExW(mediakey, szIndex, 0, REG_SZ, (LPBYTE)buffer, size);
1292 msi_free(buffer);
1294 RegCloseKey(sourcekey);
1295 RegCloseKey(mediakey);
1297 return ERROR_SUCCESS;
1300 /******************************************************************
1301 * MsiSourceListClearAllA (MSI.@)
1303 UINT WINAPI MsiSourceListClearAllA( LPCSTR szProduct, LPCSTR szUserName, DWORD dwReserved )
1305 FIXME("(%s %s %d)\n", debugstr_a(szProduct), debugstr_a(szUserName), dwReserved);
1306 return ERROR_SUCCESS;
1309 /******************************************************************
1310 * MsiSourceListClearAllW (MSI.@)
1312 UINT WINAPI MsiSourceListClearAllW( LPCWSTR szProduct, LPCWSTR szUserName, DWORD dwReserved )
1314 FIXME("(%s %s %d)\n", debugstr_w(szProduct), debugstr_w(szUserName), dwReserved);
1315 return ERROR_SUCCESS;