kernel32/tests: Fix some test failures on Win9x and WinME.
[wine/hacks.git] / dlls / msi / source.c
blobdf5905da3d4649465c657214b04434cf2f19dc70
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, NULL, context,
68 &rootkey, create);
70 else if (context == MSIINSTALLCONTEXT_USERMANAGED)
72 if (dwOptions & MSICODE_PATCH)
73 rc = MSIREG_OpenUserPatchesKey(szProduct, &rootkey, create);
74 else
75 rc = MSIREG_OpenProductKey(szProduct, NULL, context,
76 &rootkey, create);
78 else if (context == MSIINSTALLCONTEXT_MACHINE)
80 if (dwOptions & MSICODE_PATCH)
81 rc = MSIREG_OpenPatchesKey(szProduct, &rootkey, create);
82 else
83 rc = MSIREG_OpenProductKey(szProduct, NULL, context,
84 &rootkey, create);
87 if (rc != ERROR_SUCCESS)
89 if (dwOptions & MSICODE_PATCH)
90 return ERROR_UNKNOWN_PATCH;
91 else
92 return ERROR_UNKNOWN_PRODUCT;
95 if (create)
96 rc = RegCreateKeyW(rootkey, szSourceList, key);
97 else
99 rc = RegOpenKeyW(rootkey,szSourceList, key);
100 if (rc != ERROR_SUCCESS)
101 rc = ERROR_BAD_CONFIGURATION;
104 return rc;
107 static UINT OpenMediaSubkey(HKEY rootkey, HKEY *key, BOOL create)
109 UINT rc;
110 static const WCHAR media[] = {'M','e','d','i','a',0};
112 if (create)
113 rc = RegCreateKeyW(rootkey, media, key);
114 else
115 rc = RegOpenKeyW(rootkey,media, key);
117 return rc;
120 static UINT OpenNetworkSubkey(HKEY rootkey, HKEY *key, BOOL create)
122 UINT rc;
123 static const WCHAR net[] = {'N','e','t',0};
125 if (create)
126 rc = RegCreateKeyW(rootkey, net, key);
127 else
128 rc = RegOpenKeyW(rootkey, net, key);
130 return rc;
133 static UINT OpenURLSubkey(HKEY rootkey, HKEY *key, BOOL create)
135 UINT rc;
136 static const WCHAR URL[] = {'U','R','L',0};
138 if (create)
139 rc = RegCreateKeyW(rootkey, URL, key);
140 else
141 rc = RegOpenKeyW(rootkey, URL, key);
143 return rc;
146 /******************************************************************
147 * MsiSourceListEnumMediaDisksA (MSI.@)
149 UINT WINAPI MsiSourceListEnumMediaDisksA(LPCSTR szProductCodeOrPatchCode,
150 LPCSTR szUserSid, MSIINSTALLCONTEXT dwContext,
151 DWORD dwOptions, DWORD dwIndex, LPDWORD pdwDiskId,
152 LPSTR szVolumeLabel, LPDWORD pcchVolumeLabel,
153 LPSTR szDiskPrompt, LPDWORD pcchDiskPrompt)
155 LPWSTR product = NULL;
156 LPWSTR usersid = NULL;
157 LPWSTR volume = NULL;
158 LPWSTR prompt = NULL;
159 UINT r = ERROR_INVALID_PARAMETER;
161 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n", debugstr_a(szProductCodeOrPatchCode),
162 debugstr_a(szUserSid), dwContext, dwOptions, dwIndex, pdwDiskId,
163 szVolumeLabel, pcchVolumeLabel, szDiskPrompt, pcchDiskPrompt);
165 if (szDiskPrompt && !pcchDiskPrompt)
166 return ERROR_INVALID_PARAMETER;
168 if (szProductCodeOrPatchCode) product = strdupAtoW(szProductCodeOrPatchCode);
169 if (szUserSid) usersid = strdupAtoW(szUserSid);
171 /* FIXME: add tests for an invalid format */
173 if (pcchVolumeLabel)
174 volume = msi_alloc(*pcchVolumeLabel * sizeof(WCHAR));
176 if (pcchDiskPrompt)
177 prompt = msi_alloc(*pcchDiskPrompt * sizeof(WCHAR));
179 if (volume) *volume = '\0';
180 if (prompt) *prompt = '\0';
181 r = MsiSourceListEnumMediaDisksW(product, usersid, dwContext, dwOptions,
182 dwIndex, pdwDiskId, volume, pcchVolumeLabel,
183 prompt, pcchDiskPrompt);
184 if (r != ERROR_SUCCESS)
185 goto done;
187 if (szVolumeLabel && pcchVolumeLabel)
188 WideCharToMultiByte(CP_ACP, 0, volume, -1, szVolumeLabel,
189 *pcchVolumeLabel + 1, NULL, NULL);
191 if (szDiskPrompt)
192 WideCharToMultiByte(CP_ACP, 0, prompt, -1, szDiskPrompt,
193 *pcchDiskPrompt + 1, NULL, NULL);
195 done:
196 msi_free(product);
197 msi_free(usersid);
198 msi_free(volume);
199 msi_free(prompt);
201 return r;
204 /******************************************************************
205 * MsiSourceListEnumMediaDisksW (MSI.@)
207 UINT WINAPI MsiSourceListEnumMediaDisksW(LPCWSTR szProductCodeOrPatchCode,
208 LPCWSTR szUserSid, MSIINSTALLCONTEXT dwContext,
209 DWORD dwOptions, DWORD dwIndex, LPDWORD pdwDiskId,
210 LPWSTR szVolumeLabel, LPDWORD pcchVolumeLabel,
211 LPWSTR szDiskPrompt, LPDWORD pcchDiskPrompt)
213 WCHAR squished_pc[GUID_SIZE];
214 WCHAR convert[11];
215 LPWSTR value = NULL;
216 LPWSTR data = NULL;
217 LPWSTR ptr, ptr2;
218 HKEY source, media;
219 DWORD valuesz, datasz = 0;
220 DWORD type;
221 DWORD numvals, size;
222 LONG res;
223 UINT r;
224 static DWORD index = 0;
226 static const WCHAR fmt[] = {'#','%','d',0};
228 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p)\n", debugstr_w(szProductCodeOrPatchCode),
229 debugstr_w(szUserSid), dwContext, dwOptions, dwIndex, szVolumeLabel,
230 pcchVolumeLabel, szDiskPrompt, pcchDiskPrompt);
232 if (!szProductCodeOrPatchCode ||
233 !squash_guid(szProductCodeOrPatchCode, squished_pc))
234 return ERROR_INVALID_PARAMETER;
236 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
237 return ERROR_INVALID_PARAMETER;
239 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
240 return ERROR_INVALID_PARAMETER;
242 if (szDiskPrompt && !pcchDiskPrompt)
243 return ERROR_INVALID_PARAMETER;
245 if (dwIndex == 0)
246 index = 0;
248 if (dwIndex != index)
249 return ERROR_INVALID_PARAMETER;
251 r = OpenSourceKey(szProductCodeOrPatchCode, &source,
252 dwOptions, dwContext, FALSE);
253 if (r != ERROR_SUCCESS)
254 return r;
256 r = OpenMediaSubkey(source, &media, FALSE);
257 if (r != ERROR_SUCCESS)
259 RegCloseKey(source);
260 return ERROR_NO_MORE_ITEMS;
263 if (!pcchVolumeLabel && !pcchDiskPrompt)
265 r = RegEnumValueW(media, dwIndex, NULL, NULL, NULL,
266 &type, NULL, NULL);
267 goto done;
270 res = RegQueryInfoKeyW(media, NULL, NULL, NULL, NULL, NULL,
271 NULL, &numvals, &valuesz, &datasz, NULL, NULL);
272 if (res != ERROR_SUCCESS)
274 r = ERROR_BAD_CONFIGURATION;
275 goto done;
278 value = msi_alloc(++valuesz * sizeof(WCHAR));
279 data = msi_alloc(++datasz * sizeof(WCHAR));
280 if (!value || !data)
282 r = ERROR_OUTOFMEMORY;
283 goto done;
286 r = RegEnumValueW(media, dwIndex, value, &valuesz,
287 NULL, &type, (LPBYTE)data, &datasz);
288 if (r != ERROR_SUCCESS)
289 goto done;
291 if (pdwDiskId)
292 *pdwDiskId = atolW(value);
294 ptr2 = data;
295 ptr = strchrW(data, ';');
296 if (!ptr)
297 ptr = data;
298 else
299 *ptr = '\0';
301 if (pcchVolumeLabel)
303 if (type == REG_DWORD)
305 sprintfW(convert, fmt, *data);
306 size = lstrlenW(convert);
307 ptr2 = convert;
309 else
310 size = lstrlenW(data);
312 if (size >= *pcchVolumeLabel)
313 r = ERROR_MORE_DATA;
314 else if (szVolumeLabel)
315 lstrcpyW(szVolumeLabel, ptr2);
317 *pcchVolumeLabel = size;
320 if (pcchDiskPrompt)
322 if (!*ptr)
323 ptr++;
325 if (type == REG_DWORD)
327 sprintfW(convert, fmt, *ptr);
328 size = lstrlenW(convert);
329 ptr = convert;
331 else
332 size = lstrlenW(ptr);
334 size = lstrlenW(ptr);
335 if (size >= *pcchDiskPrompt)
336 r = ERROR_MORE_DATA;
337 else if (szDiskPrompt)
338 lstrcpyW(szDiskPrompt, ptr);
340 *pcchDiskPrompt = size;
343 index++;
345 done:
346 msi_free(value);
347 msi_free(data);
348 RegCloseKey(source);
350 return r;
353 /******************************************************************
354 * MsiSourceListEnumSourcesA (MSI.@)
356 UINT WINAPI MsiSourceListEnumSourcesA(LPCSTR szProductCodeOrPatch, LPCSTR szUserSid,
357 MSIINSTALLCONTEXT dwContext,
358 DWORD dwOptions, DWORD dwIndex,
359 LPSTR szSource, LPDWORD pcchSource)
361 LPWSTR product = NULL;
362 LPWSTR usersid = NULL;
363 LPWSTR source = NULL;
364 DWORD len = 0;
365 UINT r = ERROR_INVALID_PARAMETER;
366 static DWORD index = 0;
368 TRACE("(%s, %s, %d, %d, %d, %p, %p)\n", debugstr_a(szProductCodeOrPatch),
369 debugstr_a(szUserSid), dwContext, dwOptions, dwIndex, szSource, pcchSource);
371 if (dwIndex == 0)
372 index = 0;
374 if (szSource && !pcchSource)
375 goto done;
377 if (dwIndex != index)
378 goto done;
380 if (szProductCodeOrPatch) product = strdupAtoW(szProductCodeOrPatch);
381 if (szUserSid) usersid = strdupAtoW(szUserSid);
383 r = MsiSourceListEnumSourcesW(product, usersid, dwContext, dwOptions,
384 dwIndex, NULL, &len);
385 if (r != ERROR_SUCCESS)
386 goto done;
388 source = msi_alloc(++len * sizeof(WCHAR));
389 if (!source)
391 r = ERROR_OUTOFMEMORY;
392 goto done;
395 *source = '\0';
396 r = MsiSourceListEnumSourcesW(product, usersid, dwContext, dwOptions,
397 dwIndex, source, &len);
398 if (r != ERROR_SUCCESS)
399 goto done;
401 len = WideCharToMultiByte(CP_ACP, 0, source, -1, NULL, 0, NULL, NULL);
402 if (pcchSource && *pcchSource >= len)
403 WideCharToMultiByte(CP_ACP, 0, source, -1, szSource, len, NULL, NULL);
404 else if (szSource)
405 r = ERROR_MORE_DATA;
407 if (pcchSource)
408 *pcchSource = len - 1;
410 done:
411 msi_free(product);
412 msi_free(usersid);
413 msi_free(source);
415 if (r == ERROR_SUCCESS)
417 if (szSource || !pcchSource) index++;
419 else if (dwIndex > index)
420 index = 0;
422 return r;
425 /******************************************************************
426 * MsiSourceListEnumSourcesW (MSI.@)
428 UINT WINAPI MsiSourceListEnumSourcesW(LPCWSTR szProductCodeOrPatch, LPCWSTR szUserSid,
429 MSIINSTALLCONTEXT dwContext,
430 DWORD dwOptions, DWORD dwIndex,
431 LPWSTR szSource, LPDWORD pcchSource)
433 WCHAR squished_pc[GUID_SIZE];
434 WCHAR name[32];
435 HKEY source = NULL;
436 HKEY subkey = NULL;
437 LONG res;
438 UINT r = ERROR_INVALID_PARAMETER;
439 static DWORD index = 0;
441 static const WCHAR format[] = {'%','d',0};
443 TRACE("(%s, %s, %d, %d, %d, %p, %p)\n", debugstr_w(szProductCodeOrPatch),
444 debugstr_w(szUserSid), dwContext, dwOptions, dwIndex, szSource, pcchSource);
446 if (dwIndex == 0)
447 index = 0;
449 if (!szProductCodeOrPatch || !squash_guid(szProductCodeOrPatch, squished_pc))
450 goto done;
452 if (szSource && !pcchSource)
453 goto done;
455 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
456 goto done;
458 if ((dwOptions & MSISOURCETYPE_NETWORK) && (dwOptions & MSISOURCETYPE_URL))
459 goto done;
461 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
462 goto done;
464 if (dwIndex != index)
465 goto done;
467 r = OpenSourceKey(szProductCodeOrPatch, &source,
468 dwOptions, dwContext, FALSE);
469 if (r != ERROR_SUCCESS)
470 goto done;
472 if (dwOptions & MSISOURCETYPE_NETWORK)
473 r = OpenNetworkSubkey(source, &subkey, FALSE);
474 else if (dwOptions & MSISOURCETYPE_URL)
475 r = OpenURLSubkey(source, &subkey, FALSE);
477 if (r != ERROR_SUCCESS)
479 r = ERROR_NO_MORE_ITEMS;
480 goto done;
483 sprintfW(name, format, dwIndex + 1);
485 res = RegQueryValueExW(subkey, name, 0, 0, (LPBYTE)szSource, pcchSource);
486 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA)
487 r = ERROR_NO_MORE_ITEMS;
489 done:
490 RegCloseKey(subkey);
491 RegCloseKey(source);
493 if (r == ERROR_SUCCESS)
495 if (szSource || !pcchSource) index++;
497 else if (dwIndex > index)
498 index = 0;
500 return r;
503 /******************************************************************
504 * MsiSourceListGetInfoA (MSI.@)
506 UINT WINAPI MsiSourceListGetInfoA( LPCSTR szProduct, LPCSTR szUserSid,
507 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
508 LPCSTR szProperty, LPSTR szValue,
509 LPDWORD pcchValue)
511 UINT ret;
512 LPWSTR product = NULL;
513 LPWSTR usersid = NULL;
514 LPWSTR property = NULL;
515 LPWSTR value = NULL;
516 DWORD len = 0;
518 if (szValue && !pcchValue)
519 return ERROR_INVALID_PARAMETER;
521 if (szProduct) product = strdupAtoW(szProduct);
522 if (szUserSid) usersid = strdupAtoW(szUserSid);
523 if (szProperty) property = strdupAtoW(szProperty);
525 ret = MsiSourceListGetInfoW(product, usersid, dwContext, dwOptions,
526 property, NULL, &len);
527 if (ret != ERROR_SUCCESS)
528 goto done;
530 value = msi_alloc(++len * sizeof(WCHAR));
531 if (!value)
532 return ERROR_OUTOFMEMORY;
534 *value = '\0';
535 ret = MsiSourceListGetInfoW(product, usersid, dwContext, dwOptions,
536 property, value, &len);
537 if (ret != ERROR_SUCCESS)
538 goto done;
540 len = WideCharToMultiByte(CP_ACP, 0, value, -1, NULL, 0, NULL, NULL);
541 if (*pcchValue >= len)
542 WideCharToMultiByte(CP_ACP, 0, value, -1, szValue, len, NULL, NULL);
543 else if (szValue)
544 ret = ERROR_MORE_DATA;
546 *pcchValue = len - 1;
548 done:
549 msi_free(product);
550 msi_free(usersid);
551 msi_free(property);
552 msi_free(value);
553 return ret;
556 /******************************************************************
557 * MsiSourceListGetInfoW (MSI.@)
559 UINT WINAPI MsiSourceListGetInfoW( LPCWSTR szProduct, LPCWSTR szUserSid,
560 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
561 LPCWSTR szProperty, LPWSTR szValue,
562 LPDWORD pcchValue)
564 WCHAR squished_pc[GUID_SIZE];
565 HKEY sourcekey, media;
566 LPWSTR source, ptr;
567 DWORD size;
568 UINT rc;
570 static const WCHAR mediapack[] = {
571 'M','e','d','i','a','P','a','c','k','a','g','e',0};
573 TRACE("%s %s\n", debugstr_w(szProduct), debugstr_w(szProperty));
575 if (!szProduct || !squash_guid(szProduct, squished_pc))
576 return ERROR_INVALID_PARAMETER;
578 if (szValue && !pcchValue)
579 return ERROR_INVALID_PARAMETER;
581 if (dwContext != MSIINSTALLCONTEXT_USERMANAGED &&
582 dwContext != MSIINSTALLCONTEXT_USERUNMANAGED &&
583 dwContext != MSIINSTALLCONTEXT_MACHINE)
584 return ERROR_INVALID_PARAMETER;
586 if (!szProperty)
587 return ERROR_INVALID_PARAMETER;
589 if (szUserSid)
590 FIXME("Unhandled UserSid %s\n",debugstr_w(szUserSid));
592 if (dwContext != MSIINSTALLCONTEXT_USERUNMANAGED)
593 FIXME("Unhandled context %d\n", dwContext);
595 rc = OpenSourceKey(szProduct, &sourcekey, dwOptions, dwContext, FALSE);
596 if (rc != ERROR_SUCCESS)
597 return rc;
599 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW) ||
600 !lstrcmpW(szProperty, INSTALLPROPERTY_DISKPROMPTW))
602 rc = OpenMediaSubkey(sourcekey, &media, FALSE);
603 if (rc != ERROR_SUCCESS)
605 RegCloseKey(sourcekey);
606 return ERROR_SUCCESS;
609 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW))
610 szProperty = mediapack;
612 RegQueryValueExW(media, szProperty, 0, 0, (LPBYTE)szValue, pcchValue);
613 RegCloseKey(media);
615 else if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW) ||
616 !lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDTYPEW))
618 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
619 0, 0, NULL, &size);
620 if (rc != ERROR_SUCCESS)
622 RegCloseKey(sourcekey);
623 return ERROR_SUCCESS;
626 source = msi_alloc(size);
627 RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
628 0, 0, (LPBYTE)source, &size);
630 if (!*source)
632 msi_free(source);
633 RegCloseKey(sourcekey);
634 return ERROR_SUCCESS;
637 if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDTYPEW))
639 if (*source != 'n' && *source != 'u' && *source != 'm')
641 msi_free(source);
642 RegCloseKey(sourcekey);
643 return ERROR_SUCCESS;
646 ptr = source;
647 source[1] = '\0';
649 else
651 ptr = strrchrW(source, ';');
652 if (!ptr)
653 ptr = source;
654 else
655 ptr++;
658 if (szValue)
660 if (strlenW(ptr) < *pcchValue)
661 lstrcpyW(szValue, ptr);
662 else
663 rc = ERROR_MORE_DATA;
666 *pcchValue = lstrlenW(ptr);
667 msi_free(source);
669 else if (strcmpW(INSTALLPROPERTY_PACKAGENAMEW, szProperty)==0)
671 *pcchValue = *pcchValue * sizeof(WCHAR);
672 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_PACKAGENAMEW, 0, 0,
673 (LPBYTE)szValue, pcchValue);
674 if (rc != ERROR_SUCCESS && rc != ERROR_MORE_DATA)
676 *pcchValue = 0;
677 rc = ERROR_SUCCESS;
679 else
681 if (*pcchValue)
682 *pcchValue = (*pcchValue - 1) / sizeof(WCHAR);
683 if (szValue)
684 szValue[*pcchValue] = '\0';
687 else
689 FIXME("Unknown property %s\n",debugstr_w(szProperty));
690 rc = ERROR_UNKNOWN_PROPERTY;
693 RegCloseKey(sourcekey);
694 return rc;
697 /******************************************************************
698 * MsiSourceListSetInfoA (MSI.@)
700 UINT WINAPI MsiSourceListSetInfoA(LPCSTR szProduct, LPCSTR szUserSid,
701 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
702 LPCSTR szProperty, LPCSTR szValue)
704 UINT ret;
705 LPWSTR product = NULL;
706 LPWSTR usersid = NULL;
707 LPWSTR property = NULL;
708 LPWSTR value = NULL;
710 if (szProduct) product = strdupAtoW(szProduct);
711 if (szUserSid) usersid = strdupAtoW(szUserSid);
712 if (szProperty) property = strdupAtoW(szProperty);
713 if (szValue) value = strdupAtoW(szValue);
715 ret = MsiSourceListSetInfoW(product, usersid, dwContext, dwOptions,
716 property, value);
718 msi_free(product);
719 msi_free(usersid);
720 msi_free(property);
721 msi_free(value);
723 return ret;
726 UINT msi_set_last_used_source(LPCWSTR product, LPCWSTR usersid,
727 MSIINSTALLCONTEXT context, DWORD options,
728 LPCWSTR value)
730 HKEY source;
731 LPWSTR buffer;
732 WCHAR typechar;
733 DWORD size;
734 UINT r;
735 int index = 1;
737 static const WCHAR format[] = {'%','c',';','%','i',';','%','s',0};
739 if (options & MSISOURCETYPE_NETWORK)
740 typechar = 'n';
741 else if (options & MSISOURCETYPE_URL)
742 typechar = 'u';
743 else if (options & MSISOURCETYPE_MEDIA)
744 typechar = 'm';
745 else
746 return ERROR_INVALID_PARAMETER;
748 if (!(options & MSISOURCETYPE_MEDIA))
750 r = MsiSourceListAddSourceExW(product, usersid, context,
751 options, value, 0);
752 if (r != ERROR_SUCCESS)
753 return r;
755 index = 0;
756 while ((r = MsiSourceListEnumSourcesW(product, usersid, context, options,
757 index, NULL, NULL)) == ERROR_SUCCESS)
758 index++;
760 if (r != ERROR_NO_MORE_ITEMS)
761 return r;
764 size = (lstrlenW(format) + lstrlenW(value) + 7) * sizeof(WCHAR);
765 buffer = msi_alloc(size);
766 if (!buffer)
767 return ERROR_OUTOFMEMORY;
769 r = OpenSourceKey(product, &source, MSICODE_PRODUCT, context, FALSE);
770 if (r != ERROR_SUCCESS)
771 return r;
773 sprintfW(buffer, format, typechar, index, value);
775 size = (lstrlenW(buffer) + 1) * sizeof(WCHAR);
776 r = RegSetValueExW(source, INSTALLPROPERTY_LASTUSEDSOURCEW, 0,
777 REG_SZ, (LPBYTE)buffer, size);
778 msi_free(buffer);
780 RegCloseKey(source);
781 return r;
784 /******************************************************************
785 * MsiSourceListSetInfoW (MSI.@)
787 UINT WINAPI MsiSourceListSetInfoW( LPCWSTR szProduct, LPCWSTR szUserSid,
788 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
789 LPCWSTR szProperty, LPCWSTR szValue)
791 WCHAR squished_pc[GUID_SIZE];
792 HKEY sourcekey, media;
793 LPCWSTR property;
794 UINT rc;
796 static const WCHAR media_package[] = {
797 'M','e','d','i','a','P','a','c','k','a','g','e',0
800 TRACE("%s %s %x %x %s %s\n", debugstr_w(szProduct), debugstr_w(szUserSid),
801 dwContext, dwOptions, debugstr_w(szProperty), debugstr_w(szValue));
803 if (!szProduct || !squash_guid(szProduct, squished_pc))
804 return ERROR_INVALID_PARAMETER;
806 if (!szProperty)
807 return ERROR_INVALID_PARAMETER;
809 if (!szValue)
810 return ERROR_UNKNOWN_PROPERTY;
812 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
813 return ERROR_INVALID_PARAMETER;
815 if (dwOptions & MSICODE_PATCH)
817 FIXME("Unhandled options MSICODE_PATCH\n");
818 return ERROR_UNKNOWN_PATCH;
821 property = szProperty;
822 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW))
823 property = media_package;
825 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
826 if (rc != ERROR_SUCCESS)
827 return rc;
829 if (lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW) &&
830 dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL))
832 RegCloseKey(sourcekey);
833 return ERROR_INVALID_PARAMETER;
836 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW) ||
837 !lstrcmpW(szProperty, INSTALLPROPERTY_DISKPROMPTW))
839 rc = OpenMediaSubkey(sourcekey, &media, TRUE);
840 if (rc == ERROR_SUCCESS)
842 rc = msi_reg_set_val_str(media, property, szValue);
843 RegCloseKey(media);
846 else if (strcmpW(INSTALLPROPERTY_PACKAGENAMEW, szProperty)==0)
848 DWORD size = (lstrlenW(szValue) + 1) * sizeof(WCHAR);
849 rc = RegSetValueExW(sourcekey, INSTALLPROPERTY_PACKAGENAMEW, 0,
850 REG_SZ, (const BYTE *)szValue, size);
851 if (rc != ERROR_SUCCESS)
852 rc = ERROR_UNKNOWN_PROPERTY;
854 else if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW))
856 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
857 rc = ERROR_INVALID_PARAMETER;
858 else
859 rc = msi_set_last_used_source(szProduct, szUserSid, dwContext,
860 dwOptions, szValue);
862 else
863 rc = ERROR_UNKNOWN_PROPERTY;
865 RegCloseKey(sourcekey);
866 return rc;
869 /******************************************************************
870 * MsiSourceListAddSourceW (MSI.@)
872 UINT WINAPI MsiSourceListAddSourceW( LPCWSTR szProduct, LPCWSTR szUserName,
873 DWORD dwReserved, LPCWSTR szSource)
875 WCHAR squished_pc[GUID_SIZE];
876 INT ret;
877 LPWSTR sidstr = NULL;
878 DWORD sidsize = 0;
879 DWORD domsize = 0;
880 DWORD context;
881 HKEY hkey = 0;
882 UINT r;
884 TRACE("%s %s %s\n", debugstr_w(szProduct), debugstr_w(szUserName), debugstr_w(szSource));
886 if (!szSource || !*szSource)
887 return ERROR_INVALID_PARAMETER;
889 if (dwReserved != 0)
890 return ERROR_INVALID_PARAMETER;
892 if (!szProduct || !squash_guid(szProduct, squished_pc))
893 return ERROR_INVALID_PARAMETER;
895 if (!szUserName || !*szUserName)
896 context = MSIINSTALLCONTEXT_MACHINE;
897 else
899 if (LookupAccountNameW(NULL, szUserName, NULL, &sidsize, NULL, &domsize, NULL))
901 PSID psid = msi_alloc(sidsize);
903 if (LookupAccountNameW(NULL, szUserName, psid, &sidsize, NULL, &domsize, NULL))
904 ConvertSidToStringSidW(psid, &sidstr);
906 msi_free(psid);
909 r = MSIREG_OpenProductKey(szProduct, NULL,
910 MSIINSTALLCONTEXT_USERMANAGED, &hkey, FALSE);
911 if (r == ERROR_SUCCESS)
912 context = MSIINSTALLCONTEXT_USERMANAGED;
913 else
915 r = MSIREG_OpenProductKey(szProduct, NULL,
916 MSIINSTALLCONTEXT_USERUNMANAGED,
917 &hkey, FALSE);
918 if (r != ERROR_SUCCESS)
919 return ERROR_UNKNOWN_PRODUCT;
921 context = MSIINSTALLCONTEXT_USERUNMANAGED;
924 RegCloseKey(hkey);
927 ret = MsiSourceListAddSourceExW(szProduct, sidstr,
928 context, MSISOURCETYPE_NETWORK, szSource, 0);
930 if (sidstr)
931 LocalFree(sidstr);
933 return ret;
936 /******************************************************************
937 * MsiSourceListAddSourceA (MSI.@)
939 UINT WINAPI MsiSourceListAddSourceA( LPCSTR szProduct, LPCSTR szUserName,
940 DWORD dwReserved, LPCSTR szSource)
942 INT ret;
943 LPWSTR szwproduct;
944 LPWSTR szwusername;
945 LPWSTR szwsource;
947 szwproduct = strdupAtoW( szProduct );
948 szwusername = strdupAtoW( szUserName );
949 szwsource = strdupAtoW( szSource );
951 ret = MsiSourceListAddSourceW(szwproduct, szwusername, dwReserved, szwsource);
953 msi_free(szwproduct);
954 msi_free(szwusername);
955 msi_free(szwsource);
957 return ret;
960 /******************************************************************
961 * MsiSourceListAddSourceExA (MSI.@)
963 UINT WINAPI MsiSourceListAddSourceExA(LPCSTR szProduct, LPCSTR szUserSid,
964 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCSTR szSource, DWORD dwIndex)
966 UINT ret;
967 LPWSTR product, usersid, source;
969 product = strdupAtoW(szProduct);
970 usersid = strdupAtoW(szUserSid);
971 source = strdupAtoW(szSource);
973 ret = MsiSourceListAddSourceExW(product, usersid, dwContext,
974 dwOptions, source, dwIndex);
976 msi_free(product);
977 msi_free(usersid);
978 msi_free(source);
980 return ret;
983 static void free_source_list(struct list *sourcelist)
985 while (!list_empty(sourcelist))
987 media_info *info = LIST_ENTRY(list_head(sourcelist), media_info, entry);
988 list_remove(&info->entry);
989 msi_free(info->path);
990 msi_free(info);
994 static void add_source_to_list(struct list *sourcelist, media_info *info,
995 DWORD *index)
997 media_info *iter;
998 BOOL found = FALSE;
999 static const WCHAR fmt[] = {'%','i',0};
1001 if (index) *index = 0;
1003 if (list_empty(sourcelist))
1005 list_add_head(sourcelist, &info->entry);
1006 return;
1009 LIST_FOR_EACH_ENTRY(iter, sourcelist, media_info, entry)
1011 if (!found && info->index < iter->index)
1013 found = TRUE;
1014 list_add_before(&iter->entry, &info->entry);
1017 /* update the rest of the list */
1018 if (found)
1019 sprintfW(iter->szIndex, fmt, ++iter->index);
1020 else if (index)
1021 (*index)++;
1024 if (!found)
1025 list_add_after(&iter->entry, &info->entry);
1028 static UINT fill_source_list(struct list *sourcelist, HKEY sourcekey, DWORD *count)
1030 UINT r = ERROR_SUCCESS;
1031 DWORD index = 0;
1032 WCHAR name[10];
1033 DWORD size, val_size;
1034 media_info *entry;
1036 *count = 0;
1038 while (r == ERROR_SUCCESS)
1040 size = sizeof(name) / sizeof(name[0]);
1041 r = RegEnumValueW(sourcekey, index, name, &size, NULL, NULL, NULL, &val_size);
1042 if (r != ERROR_SUCCESS)
1043 return r;
1045 entry = msi_alloc(sizeof(media_info));
1046 if (!entry)
1047 goto error;
1049 entry->path = msi_alloc(val_size);
1050 if (!entry->path)
1052 msi_free(entry);
1053 goto error;
1056 lstrcpyW(entry->szIndex, name);
1057 entry->index = atoiW(name);
1059 size++;
1060 r = RegEnumValueW(sourcekey, index, name, &size, NULL,
1061 NULL, (LPBYTE)entry->path, &val_size);
1062 if (r != ERROR_SUCCESS)
1064 msi_free(entry->path);
1065 msi_free(entry);
1066 goto error;
1069 index = ++(*count);
1070 add_source_to_list(sourcelist, entry, NULL);
1073 error:
1074 *count = -1;
1075 free_source_list(sourcelist);
1076 return ERROR_OUTOFMEMORY;
1079 /******************************************************************
1080 * MsiSourceListAddSourceExW (MSI.@)
1082 UINT WINAPI MsiSourceListAddSourceExW( LPCWSTR szProduct, LPCWSTR szUserSid,
1083 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCWSTR szSource,
1084 DWORD dwIndex)
1086 HKEY sourcekey;
1087 HKEY typekey;
1088 UINT rc;
1089 struct list sourcelist;
1090 media_info *info;
1091 WCHAR squished_pc[GUID_SIZE];
1092 WCHAR name[10];
1093 LPWSTR source;
1094 LPCWSTR postfix;
1095 DWORD size, count;
1096 DWORD index;
1098 static const WCHAR fmt[] = {'%','i',0};
1099 static const WCHAR one[] = {'1',0};
1100 static const WCHAR backslash[] = {'\\',0};
1101 static const WCHAR forwardslash[] = {'/',0};
1103 TRACE("%s %s %x %x %s %i\n", debugstr_w(szProduct), debugstr_w(szUserSid),
1104 dwContext, dwOptions, debugstr_w(szSource), dwIndex);
1106 if (!szProduct || !squash_guid(szProduct, squished_pc))
1107 return ERROR_INVALID_PARAMETER;
1109 if (!szSource || !*szSource)
1110 return ERROR_INVALID_PARAMETER;
1112 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
1113 return ERROR_INVALID_PARAMETER;
1115 if (dwOptions & MSICODE_PATCH)
1117 FIXME("Unhandled options MSICODE_PATCH\n");
1118 return ERROR_FUNCTION_FAILED;
1121 if (szUserSid && (dwContext & MSIINSTALLCONTEXT_MACHINE))
1122 return ERROR_INVALID_PARAMETER;
1124 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1125 if (rc != ERROR_SUCCESS)
1126 return rc;
1128 if (dwOptions & MSISOURCETYPE_NETWORK)
1129 rc = OpenNetworkSubkey(sourcekey, &typekey, TRUE);
1130 else if (dwOptions & MSISOURCETYPE_URL)
1131 rc = OpenURLSubkey(sourcekey, &typekey, TRUE);
1132 else if (dwOptions & MSISOURCETYPE_MEDIA)
1133 rc = OpenMediaSubkey(sourcekey, &typekey, TRUE);
1134 else
1136 ERR("unknown media type: %08x\n", dwOptions);
1137 RegCloseKey(sourcekey);
1138 return ERROR_FUNCTION_FAILED;
1141 postfix = (dwOptions & MSISOURCETYPE_NETWORK) ? backslash : forwardslash;
1142 if (szSource[lstrlenW(szSource) - 1] == *postfix)
1143 source = strdupW(szSource);
1144 else
1146 size = lstrlenW(szSource) + 2;
1147 source = msi_alloc(size * sizeof(WCHAR));
1148 lstrcpyW(source, szSource);
1149 lstrcatW(source, postfix);
1152 list_init(&sourcelist);
1153 rc = fill_source_list(&sourcelist, typekey, &count);
1154 if (rc != ERROR_NO_MORE_ITEMS)
1155 return rc;
1157 size = (lstrlenW(source) + 1) * sizeof(WCHAR);
1159 if (count == 0)
1161 rc = RegSetValueExW(typekey, one, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1162 goto done;
1164 else if (dwIndex > count || dwIndex == 0)
1166 sprintfW(name, fmt, count + 1);
1167 rc = RegSetValueExW(typekey, name, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1168 goto done;
1170 else
1172 sprintfW(name, fmt, dwIndex);
1173 info = msi_alloc(sizeof(media_info));
1174 if (!info)
1176 rc = ERROR_OUTOFMEMORY;
1177 goto done;
1180 info->path = strdupW(source);
1181 lstrcpyW(info->szIndex, name);
1182 info->index = dwIndex;
1183 add_source_to_list(&sourcelist, info, &index);
1185 LIST_FOR_EACH_ENTRY(info, &sourcelist, media_info, entry)
1187 if (info->index < index)
1188 continue;
1190 size = (lstrlenW(info->path) + 1) * sizeof(WCHAR);
1191 rc = RegSetValueExW(typekey, info->szIndex, 0,
1192 REG_EXPAND_SZ, (LPBYTE)info->path, size);
1193 if (rc != ERROR_SUCCESS)
1194 goto done;
1198 done:
1199 free_source_list(&sourcelist);
1200 msi_free(source);
1201 RegCloseKey(typekey);
1202 RegCloseKey(sourcekey);
1203 return rc;
1206 /******************************************************************
1207 * MsiSourceListAddMediaDiskA (MSI.@)
1209 UINT WINAPI MsiSourceListAddMediaDiskA(LPCSTR szProduct, LPCSTR szUserSid,
1210 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1211 LPCSTR szVolumeLabel, LPCSTR szDiskPrompt)
1213 UINT r;
1214 LPWSTR product = NULL;
1215 LPWSTR usersid = NULL;
1216 LPWSTR volume = NULL;
1217 LPWSTR prompt = NULL;
1219 if (szProduct) product = strdupAtoW(szProduct);
1220 if (szUserSid) usersid = strdupAtoW(szUserSid);
1221 if (szVolumeLabel) volume = strdupAtoW(szVolumeLabel);
1222 if (szDiskPrompt) prompt = strdupAtoW(szDiskPrompt);
1224 r = MsiSourceListAddMediaDiskW(product, usersid, dwContext, dwOptions,
1225 dwDiskId, volume, prompt);
1227 msi_free(product);
1228 msi_free(usersid);
1229 msi_free(volume);
1230 msi_free(prompt);
1232 return r;
1235 /******************************************************************
1236 * MsiSourceListAddMediaDiskW (MSI.@)
1238 UINT WINAPI MsiSourceListAddMediaDiskW(LPCWSTR szProduct, LPCWSTR szUserSid,
1239 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1240 LPCWSTR szVolumeLabel, LPCWSTR szDiskPrompt)
1242 HKEY sourcekey;
1243 HKEY mediakey;
1244 UINT rc;
1245 WCHAR szIndex[10];
1246 WCHAR squished_pc[GUID_SIZE];
1247 LPWSTR buffer;
1248 DWORD size;
1250 static const WCHAR fmt[] = {'%','i',0};
1251 static const WCHAR semicolon[] = {';',0};
1253 TRACE("%s %s %x %x %i %s %s\n", debugstr_w(szProduct),
1254 debugstr_w(szUserSid), dwContext, dwOptions, dwDiskId,
1255 debugstr_w(szVolumeLabel), debugstr_w(szDiskPrompt));
1257 if (!szProduct || !squash_guid(szProduct, squished_pc))
1258 return ERROR_INVALID_PARAMETER;
1260 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
1261 return ERROR_INVALID_PARAMETER;
1263 if ((szVolumeLabel && !*szVolumeLabel) || (szDiskPrompt && !*szDiskPrompt))
1264 return ERROR_INVALID_PARAMETER;
1266 if ((dwContext & MSIINSTALLCONTEXT_MACHINE) && szUserSid)
1267 return ERROR_INVALID_PARAMETER;
1269 if (dwOptions & MSICODE_PATCH)
1271 FIXME("Unhandled options MSICODE_PATCH\n");
1272 return ERROR_FUNCTION_FAILED;
1275 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1276 if (rc != ERROR_SUCCESS)
1277 return rc;
1279 OpenMediaSubkey(sourcekey, &mediakey, TRUE);
1281 sprintfW(szIndex, fmt, dwDiskId);
1283 size = 2;
1284 if (szVolumeLabel) size += lstrlenW(szVolumeLabel);
1285 if (szDiskPrompt) size += lstrlenW(szDiskPrompt);
1287 size *= sizeof(WCHAR);
1288 buffer = msi_alloc(size);
1289 *buffer = '\0';
1291 if (szVolumeLabel) lstrcpyW(buffer, szVolumeLabel);
1292 lstrcatW(buffer, semicolon);
1293 if (szDiskPrompt) lstrcatW(buffer, szDiskPrompt);
1295 RegSetValueExW(mediakey, szIndex, 0, REG_SZ, (LPBYTE)buffer, size);
1296 msi_free(buffer);
1298 RegCloseKey(sourcekey);
1299 RegCloseKey(mediakey);
1301 return ERROR_SUCCESS;
1304 /******************************************************************
1305 * MsiSourceListClearAllA (MSI.@)
1307 UINT WINAPI MsiSourceListClearAllA( LPCSTR szProduct, LPCSTR szUserName, DWORD dwReserved )
1309 FIXME("(%s %s %d)\n", debugstr_a(szProduct), debugstr_a(szUserName), dwReserved);
1310 return ERROR_SUCCESS;
1313 /******************************************************************
1314 * MsiSourceListClearAllW (MSI.@)
1316 UINT WINAPI MsiSourceListClearAllW( LPCWSTR szProduct, LPCWSTR szUserName, DWORD dwReserved )
1318 FIXME("(%s %s %d)\n", debugstr_w(szProduct), debugstr_w(szUserName), dwReserved);
1319 return ERROR_SUCCESS;
1322 /******************************************************************
1323 * MsiSourceListClearAllExA (MSI.@)
1325 UINT WINAPI MsiSourceListClearAllExA( LPCSTR szProduct, LPCSTR szUserSid,
1326 MSIINSTALLCONTEXT dwContext, DWORD dwOptions )
1328 FIXME("(%s %s %d %08x)\n", debugstr_a(szProduct), debugstr_a(szUserSid),
1329 dwContext, dwOptions);
1330 return ERROR_SUCCESS;
1333 /******************************************************************
1334 * MsiSourceListClearAllExW (MSI.@)
1336 UINT WINAPI MsiSourceListClearAllExW( LPCWSTR szProduct, LPCWSTR szUserSid,
1337 MSIINSTALLCONTEXT dwContext, DWORD dwOptions )
1339 FIXME("(%s %s %d %08x)\n", debugstr_w(szProduct), debugstr_w(szUserSid),
1340 dwContext, dwOptions);
1341 return ERROR_SUCCESS;
1344 /******************************************************************
1345 * MsiSourceListClearSourceA (MSI.@)
1347 UINT WINAPI MsiSourceListClearSourceA(LPCSTR szProductCodeOrPatchCode, LPCSTR szUserSid,
1348 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
1349 LPCSTR szSource)
1351 FIXME("(%s %s %x %x %s)\n", debugstr_a(szProductCodeOrPatchCode), debugstr_a(szUserSid),
1352 dwContext, dwOptions, debugstr_a(szSource));
1353 return ERROR_SUCCESS;
1356 /******************************************************************
1357 * MsiSourceListClearSourceW (MSI.@)
1359 UINT WINAPI MsiSourceListClearSourceW(LPCWSTR szProductCodeOrPatchCode, LPCWSTR szUserSid,
1360 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
1361 LPCWSTR szSource)
1363 FIXME("(%s %s %x %x %s)\n", debugstr_w(szProductCodeOrPatchCode), debugstr_w(szUserSid),
1364 dwContext, dwOptions, debugstr_w(szSource));
1365 return ERROR_SUCCESS;