msi: Read the disk prompt source list property from the user-unmanaged context.
[wine.git] / dlls / msi / source.c
blobb5085694365d7c6b6630789fb6561961ebc03009
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_OpenUserProductsKey(szProduct, &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_OpenLocalManagedProductKey(szProduct, &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_OpenLocalClassesProductKey(szProduct, &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, LPWORD 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 DWORD volumesz, promptsz;
157 UINT r = ERROR_INVALID_PARAMETER;
159 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n", debugstr_a(szProductCodeOrPatchCode),
160 debugstr_a(szUserSid), dwContext, dwOptions, dwIndex, pdwDiskId,
161 szVolumeLabel, pcchVolumeLabel, szDiskPrompt, pcchDiskPrompt);
163 if (szDiskPrompt && !pcchDiskPrompt)
164 return ERROR_INVALID_PARAMETER;
166 if (szProductCodeOrPatchCode) product = strdupAtoW(szProductCodeOrPatchCode);
167 if (szUserSid) usersid = strdupAtoW(szUserSid);
169 /* FIXME: add tests for an invalid format */
171 if (pcchVolumeLabel)
172 volume = msi_alloc(*pcchVolumeLabel * sizeof(WCHAR));
174 if (pcchDiskPrompt)
175 prompt = msi_alloc(*pcchDiskPrompt * sizeof(WCHAR));
177 if (volume) *volume = '\0';
178 if (prompt) *prompt = '\0';
179 r = MsiSourceListEnumMediaDisksW(product, usersid, dwContext, dwOptions,
180 dwIndex, pdwDiskId, volume, pcchVolumeLabel,
181 prompt, pcchDiskPrompt);
182 if (r != ERROR_SUCCESS)
183 goto done;
185 if (szVolumeLabel && pcchVolumeLabel)
186 volumesz = WideCharToMultiByte(CP_ACP, 0, volume, -1, szVolumeLabel,
187 *pcchVolumeLabel + 1, NULL, NULL);
189 if (szDiskPrompt)
190 promptsz = WideCharToMultiByte(CP_ACP, 0, prompt, -1, szDiskPrompt,
191 *pcchDiskPrompt + 1, NULL, NULL);
193 done:
194 msi_free(product);
195 msi_free(usersid);
196 msi_free(volume);
197 msi_free(prompt);
199 return r;
202 /******************************************************************
203 * MsiSourceListEnumMediaDisksW (MSI.@)
205 UINT WINAPI MsiSourceListEnumMediaDisksW(LPCWSTR szProductCodeOrPatchCode,
206 LPCWSTR szUserSid, MSIINSTALLCONTEXT dwContext,
207 DWORD dwOptions, DWORD dwIndex, LPWORD pdwDiskId,
208 LPWSTR szVolumeLabel, LPDWORD pcchVolumeLabel,
209 LPWSTR szDiskPrompt, LPDWORD pcchDiskPrompt)
211 WCHAR squished_pc[GUID_SIZE];
212 LPWSTR value = NULL;
213 LPWSTR data = NULL;
214 LPWSTR ptr;
215 HKEY source, media;
216 DWORD valuesz, datasz = 0;
217 DWORD type;
218 DWORD numvals, size;
219 LONG res;
220 UINT r;
221 static int index = 0;
223 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p)\n", debugstr_w(szProductCodeOrPatchCode),
224 debugstr_w(szUserSid), dwContext, dwOptions, dwIndex, szVolumeLabel,
225 pcchVolumeLabel, szDiskPrompt, pcchDiskPrompt);
227 if (!szProductCodeOrPatchCode ||
228 !squash_guid(szProductCodeOrPatchCode, squished_pc))
229 return ERROR_INVALID_PARAMETER;
231 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
232 return ERROR_INVALID_PARAMETER;
234 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
235 return ERROR_INVALID_PARAMETER;
237 if (szDiskPrompt && !pcchDiskPrompt)
238 return ERROR_INVALID_PARAMETER;
240 if (dwIndex == 0)
241 index = 0;
243 if (dwIndex != index)
244 return ERROR_INVALID_PARAMETER;
246 r = OpenSourceKey(szProductCodeOrPatchCode, &source,
247 dwOptions, dwContext, FALSE);
248 if (r != ERROR_SUCCESS)
249 return r;
251 r = OpenMediaSubkey(source, &media, FALSE);
252 if (r != ERROR_SUCCESS)
254 RegCloseKey(source);
255 return ERROR_NO_MORE_ITEMS;
258 if (!pcchVolumeLabel && !pcchDiskPrompt)
260 r = RegEnumValueW(media, dwIndex, NULL, NULL, NULL,
261 &type, NULL, NULL);
262 goto done;
265 res = RegQueryInfoKeyW(media, NULL, NULL, NULL, NULL, NULL,
266 NULL, &numvals, &valuesz, &datasz, NULL, NULL);
267 if (res != ERROR_SUCCESS)
269 r = ERROR_BAD_CONFIGURATION;
270 goto done;
273 value = msi_alloc(++valuesz * sizeof(WCHAR));
274 data = msi_alloc(++datasz * sizeof(WCHAR));
275 if (!value || !data)
277 r = ERROR_OUTOFMEMORY;
278 goto done;
281 r = RegEnumValueW(media, dwIndex, value, &valuesz,
282 NULL, &type, (LPBYTE)data, &datasz);
283 if (r != ERROR_SUCCESS)
284 goto done;
286 if (pdwDiskId)
287 *pdwDiskId = atolW(value);
289 ptr = strchrW(data, ';');
290 if (!ptr)
291 ptr = data;
292 else
293 *ptr = '\0';
295 if (pcchVolumeLabel)
297 size = lstrlenW(data);
298 if (size >= *pcchVolumeLabel)
299 r = ERROR_MORE_DATA;
300 else if (szVolumeLabel)
301 lstrcpyW(szVolumeLabel, data);
303 *pcchVolumeLabel = size;
306 if (pcchDiskPrompt)
308 data = ptr;
309 if (!*data)
310 data++;
312 size = lstrlenW(data);
313 if (size >= *pcchDiskPrompt)
314 r = ERROR_MORE_DATA;
315 else if (szDiskPrompt)
316 lstrcpyW(szDiskPrompt, data);
318 *pcchDiskPrompt = size;
321 index++;
323 done:
324 msi_free(value);
325 msi_free(data);
326 RegCloseKey(source);
328 return r;
331 /******************************************************************
332 * MsiSourceListEnumSourcesA (MSI.@)
334 UINT WINAPI MsiSourceListEnumSourcesA(LPCSTR szProductCodeOrPatch, LPCSTR szUserSid,
335 MSIINSTALLCONTEXT dwContext,
336 DWORD dwOptions, DWORD dwIndex,
337 LPSTR szSource, LPDWORD pcchSource)
339 LPWSTR product = NULL;
340 LPWSTR usersid = NULL;
341 LPWSTR source = NULL;
342 DWORD len = 0;
343 UINT r = ERROR_INVALID_PARAMETER;
344 static int index = 0;
346 TRACE("(%s, %s, %d, %d, %d, %p, %p)\n", debugstr_a(szProductCodeOrPatch),
347 debugstr_a(szUserSid), dwContext, dwOptions, dwIndex, szSource, pcchSource);
349 if (dwIndex == 0)
350 index = 0;
352 if (szSource && !pcchSource)
353 goto done;
355 if (dwIndex != index)
356 goto done;
358 if (szProductCodeOrPatch) product = strdupAtoW(szProductCodeOrPatch);
359 if (szUserSid) usersid = strdupAtoW(szUserSid);
361 r = MsiSourceListEnumSourcesW(product, usersid, dwContext, dwOptions,
362 dwIndex, NULL, &len);
363 if (r != ERROR_SUCCESS)
364 goto done;
366 source = msi_alloc(++len * sizeof(WCHAR));
367 if (!source)
369 r = ERROR_OUTOFMEMORY;
370 goto done;
373 *source = '\0';
374 r = MsiSourceListEnumSourcesW(product, usersid, dwContext, dwOptions,
375 dwIndex, source, &len);
376 if (r != ERROR_SUCCESS)
377 goto done;
379 len = WideCharToMultiByte(CP_ACP, 0, source, -1, NULL, 0, NULL, NULL);
380 if (pcchSource && *pcchSource >= len)
381 WideCharToMultiByte(CP_ACP, 0, source, -1, szSource, len, NULL, NULL);
382 else if (szSource)
383 r = ERROR_MORE_DATA;
385 if (pcchSource)
386 *pcchSource = len - 1;
388 done:
389 msi_free(product);
390 msi_free(usersid);
391 msi_free(source);
393 if (r == ERROR_SUCCESS)
395 if (szSource || !pcchSource) index++;
397 else if (dwIndex > index)
398 index = 0;
400 return r;
403 /******************************************************************
404 * MsiSourceListEnumSourcesW (MSI.@)
406 UINT WINAPI MsiSourceListEnumSourcesW(LPCWSTR szProductCodeOrPatch, LPCWSTR szUserSid,
407 MSIINSTALLCONTEXT dwContext,
408 DWORD dwOptions, DWORD dwIndex,
409 LPWSTR szSource, LPDWORD pcchSource)
411 WCHAR squished_pc[GUID_SIZE];
412 WCHAR name[32];
413 HKEY source = NULL;
414 HKEY subkey = NULL;
415 LONG res;
416 UINT r = ERROR_INVALID_PARAMETER;
417 static int index = 0;
419 static const WCHAR format[] = {'%','d',0};
421 TRACE("(%s, %s, %d, %d, %d, %p, %p)\n", debugstr_w(szProductCodeOrPatch),
422 debugstr_w(szUserSid), dwContext, dwOptions, dwIndex, szSource, pcchSource);
424 if (dwIndex == 0)
425 index = 0;
427 if (!szProductCodeOrPatch || !squash_guid(szProductCodeOrPatch, squished_pc))
428 goto done;
430 if (szSource && !pcchSource)
431 goto done;
433 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
434 goto done;
436 if ((dwOptions & MSISOURCETYPE_NETWORK) && (dwOptions & MSISOURCETYPE_URL))
437 goto done;
439 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
440 goto done;
442 if (dwIndex != index)
443 goto done;
445 r = OpenSourceKey(szProductCodeOrPatch, &source,
446 dwOptions, dwContext, FALSE);
447 if (r != ERROR_SUCCESS)
448 goto done;
450 if (dwOptions & MSISOURCETYPE_NETWORK)
451 r = OpenNetworkSubkey(source, &subkey, FALSE);
452 else if (dwOptions & MSISOURCETYPE_URL)
453 r = OpenURLSubkey(source, &subkey, FALSE);
455 if (r != ERROR_SUCCESS)
457 r = ERROR_NO_MORE_ITEMS;
458 goto done;
461 sprintfW(name, format, dwIndex + 1);
463 res = RegQueryValueExW(subkey, name, 0, 0, (LPBYTE)szSource, pcchSource);
464 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA)
465 r = ERROR_NO_MORE_ITEMS;
467 done:
468 RegCloseKey(subkey);
469 RegCloseKey(source);
471 if (r == ERROR_SUCCESS)
473 if (szSource || !pcchSource) index++;
475 else if (dwIndex > index)
476 index = 0;
478 return r;
481 /******************************************************************
482 * MsiSourceListGetInfoA (MSI.@)
484 UINT WINAPI MsiSourceListGetInfoA( LPCSTR szProduct, LPCSTR szUserSid,
485 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
486 LPCSTR szProperty, LPSTR szValue,
487 LPDWORD pcchValue)
489 UINT ret;
490 LPWSTR product = NULL;
491 LPWSTR usersid = NULL;
492 LPWSTR property = NULL;
493 LPWSTR value = NULL;
494 DWORD len = 0;
496 if (szValue && !pcchValue)
497 return ERROR_INVALID_PARAMETER;
499 if (szProduct) product = strdupAtoW(szProduct);
500 if (szUserSid) usersid = strdupAtoW(szUserSid);
501 if (szProperty) property = strdupAtoW(szProperty);
503 ret = MsiSourceListGetInfoW(product, usersid, dwContext, dwOptions,
504 property, NULL, &len);
505 if (ret != ERROR_SUCCESS)
506 goto done;
508 value = msi_alloc(++len * sizeof(WCHAR));
509 if (!value)
510 return ERROR_OUTOFMEMORY;
512 *value = '\0';
513 ret = MsiSourceListGetInfoW(product, usersid, dwContext, dwOptions,
514 property, value, &len);
515 if (ret != ERROR_SUCCESS)
516 goto done;
518 len = WideCharToMultiByte(CP_ACP, 0, value, -1, NULL, 0, NULL, NULL);
519 if (*pcchValue >= len)
520 WideCharToMultiByte(CP_ACP, 0, value, -1, szValue, len, NULL, NULL);
521 else if (szValue)
522 ret = ERROR_MORE_DATA;
524 *pcchValue = len - 1;
526 done:
527 msi_free(product);
528 msi_free(usersid);
529 msi_free(property);
530 msi_free(value);
531 return ret;
534 /******************************************************************
535 * MsiSourceListGetInfoW (MSI.@)
537 UINT WINAPI MsiSourceListGetInfoW( LPCWSTR szProduct, LPCWSTR szUserSid,
538 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
539 LPCWSTR szProperty, LPWSTR szValue,
540 LPDWORD pcchValue)
542 WCHAR squished_pc[GUID_SIZE];
543 HKEY sourcekey, media;
544 LPWSTR source, ptr;
545 DWORD size;
546 UINT rc;
548 static const WCHAR mediapack[] = {
549 'M','e','d','i','a','P','a','c','k','a','g','e',0};
551 TRACE("%s %s\n", debugstr_w(szProduct), debugstr_w(szProperty));
553 if (!szProduct || !squash_guid(szProduct, squished_pc))
554 return ERROR_INVALID_PARAMETER;
556 if (szValue && !pcchValue)
557 return ERROR_INVALID_PARAMETER;
559 if (dwContext != MSIINSTALLCONTEXT_USERMANAGED &&
560 dwContext != MSIINSTALLCONTEXT_USERUNMANAGED &&
561 dwContext != MSIINSTALLCONTEXT_MACHINE)
562 return ERROR_INVALID_PARAMETER;
564 if (!szProperty)
565 return ERROR_INVALID_PARAMETER;
567 if (szUserSid)
568 FIXME("Unhandled UserSid %s\n",debugstr_w(szUserSid));
570 if (dwContext != MSIINSTALLCONTEXT_USERUNMANAGED)
571 FIXME("Unhandled context %d\n", dwContext);
573 rc = OpenSourceKey(szProduct, &sourcekey, dwOptions, dwContext, FALSE);
574 if (rc != ERROR_SUCCESS)
575 return rc;
577 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW) ||
578 !lstrcmpW(szProperty, INSTALLPROPERTY_DISKPROMPTW))
580 rc = OpenMediaSubkey(sourcekey, &media, FALSE);
581 if (rc != ERROR_SUCCESS)
583 RegCloseKey(sourcekey);
584 return ERROR_SUCCESS;
587 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW))
588 szProperty = mediapack;
590 RegQueryValueExW(media, szProperty, 0, 0, (LPBYTE)szValue, pcchValue);
591 RegCloseKey(media);
593 else if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW) ||
594 !lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDTYPEW))
596 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
597 0, 0, NULL, &size);
598 if (rc != ERROR_SUCCESS)
600 RegCloseKey(sourcekey);
601 return ERROR_SUCCESS;
604 source = msi_alloc(size);
605 RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
606 0, 0, (LPBYTE)source, &size);
608 if (!*source)
610 msi_free(source);
611 RegCloseKey(sourcekey);
612 return ERROR_SUCCESS;
615 if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDTYPEW))
617 if (*source != 'n' && *source != 'u' && *source != 'm')
619 msi_free(source);
620 RegCloseKey(sourcekey);
621 return ERROR_SUCCESS;
624 ptr = source;
625 source[1] = '\0';
627 else
629 ptr = strrchrW(source, ';');
630 if (!ptr)
631 ptr = source;
632 else
633 ptr++;
636 if (szValue)
638 if (lstrlenW(ptr) < *pcchValue)
639 lstrcpyW(szValue, ptr);
640 else
641 rc = ERROR_MORE_DATA;
644 *pcchValue = lstrlenW(ptr);
645 msi_free(source);
647 else if (strcmpW(INSTALLPROPERTY_PACKAGENAMEW, szProperty)==0)
649 *pcchValue = *pcchValue * sizeof(WCHAR);
650 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_PACKAGENAMEW, 0, 0,
651 (LPBYTE)szValue, pcchValue);
652 if (rc != ERROR_SUCCESS && rc != ERROR_MORE_DATA)
654 *pcchValue = 0;
655 rc = ERROR_SUCCESS;
657 else
659 if (*pcchValue)
660 *pcchValue = (*pcchValue - 1) / sizeof(WCHAR);
661 if (szValue)
662 szValue[*pcchValue] = '\0';
665 else
667 FIXME("Unknown property %s\n",debugstr_w(szProperty));
668 rc = ERROR_UNKNOWN_PROPERTY;
671 RegCloseKey(sourcekey);
672 return rc;
675 /******************************************************************
676 * MsiSourceListSetInfoA (MSI.@)
678 UINT WINAPI MsiSourceListSetInfoA(LPCSTR szProduct, LPCSTR szUserSid,
679 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
680 LPCSTR szProperty, LPCSTR szValue)
682 UINT ret;
683 LPWSTR product = NULL;
684 LPWSTR usersid = NULL;
685 LPWSTR property = NULL;
686 LPWSTR value = NULL;
688 if (szProduct) product = strdupAtoW(szProduct);
689 if (szUserSid) usersid = strdupAtoW(szUserSid);
690 if (szProperty) property = strdupAtoW(szProperty);
691 if (szValue) value = strdupAtoW(szValue);
693 ret = MsiSourceListSetInfoW(product, usersid, dwContext, dwOptions,
694 property, value);
696 msi_free(product);
697 msi_free(usersid);
698 msi_free(property);
699 msi_free(value);
701 return ret;
704 static UINT set_last_used_source(HKEY source, LPCWSTR product, LPCWSTR usersid,
705 MSIINSTALLCONTEXT context, DWORD options,
706 LPCWSTR value)
708 LPWSTR buffer;
709 WCHAR typechar;
710 DWORD size;
711 UINT r;
712 int index = 0;
714 static const WCHAR format[] = {'%','c',';','%','i',';','%','s',0};
716 if (options & MSISOURCETYPE_NETWORK)
717 typechar = 'n';
718 else if (options & MSISOURCETYPE_URL)
719 typechar = 'u';
720 else
721 return ERROR_INVALID_PARAMETER;
723 /* make sure the source is registered */
724 r = MsiSourceListAddSourceExW(product, usersid, context,
725 options, value, 0);
726 if (r != ERROR_SUCCESS)
727 return r;
729 while ((r = MsiSourceListEnumSourcesW(product, usersid, context, options,
730 index, NULL, NULL)) == ERROR_SUCCESS)
731 index++;
733 if (r != ERROR_NO_MORE_ITEMS)
734 return r;
736 size = (lstrlenW(format) + lstrlenW(value) + 7) * sizeof(WCHAR);
737 buffer = msi_alloc(size);
738 if (!buffer)
739 return ERROR_OUTOFMEMORY;
741 sprintfW(buffer, format, typechar, index, value);
743 size = (lstrlenW(buffer) + 1) * sizeof(WCHAR);
744 r = RegSetValueExW(source, INSTALLPROPERTY_LASTUSEDSOURCEW, 0,
745 REG_SZ, (LPBYTE)buffer, size);
746 msi_free(buffer);
748 return r;
751 /******************************************************************
752 * MsiSourceListSetInfoW (MSI.@)
754 UINT WINAPI MsiSourceListSetInfoW( LPCWSTR szProduct, LPCWSTR szUserSid,
755 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
756 LPCWSTR szProperty, LPCWSTR szValue)
758 WCHAR squished_pc[GUID_SIZE];
759 HKEY sourcekey, media;
760 LPCWSTR property;
761 UINT rc;
763 static const WCHAR media_package[] = {
764 'M','e','d','i','a','P','a','c','k','a','g','e',0
767 TRACE("%s %s %x %x %s %s\n", debugstr_w(szProduct), debugstr_w(szUserSid),
768 dwContext, dwOptions, debugstr_w(szProperty), debugstr_w(szValue));
770 if (!szProduct || !squash_guid(szProduct, squished_pc))
771 return ERROR_INVALID_PARAMETER;
773 if (!szProperty)
774 return ERROR_INVALID_PARAMETER;
776 if (!szValue)
777 return ERROR_UNKNOWN_PROPERTY;
779 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
780 return ERROR_INVALID_PARAMETER;
782 if (dwOptions & MSICODE_PATCH)
784 FIXME("Unhandled options MSICODE_PATCH\n");
785 return ERROR_UNKNOWN_PATCH;
788 property = szProperty;
789 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW))
790 property = media_package;
792 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
793 if (rc != ERROR_SUCCESS)
794 return rc;
796 if (lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW) &&
797 dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL))
799 RegCloseKey(sourcekey);
800 return ERROR_INVALID_PARAMETER;
803 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW) ||
804 !lstrcmpW(szProperty, INSTALLPROPERTY_DISKPROMPTW))
806 rc = OpenMediaSubkey(sourcekey, &media, TRUE);
807 if (rc == ERROR_SUCCESS)
809 rc = msi_reg_set_val_str(media, property, szValue);
810 RegCloseKey(media);
813 else if (strcmpW(INSTALLPROPERTY_PACKAGENAMEW, szProperty)==0)
815 DWORD size = lstrlenW(szValue)*sizeof(WCHAR);
816 rc = RegSetValueExW(sourcekey, INSTALLPROPERTY_PACKAGENAMEW, 0,
817 REG_SZ, (const BYTE *)szValue, size);
818 if (rc != ERROR_SUCCESS)
819 rc = ERROR_UNKNOWN_PROPERTY;
821 else if (strcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW)==0)
822 rc = set_last_used_source(sourcekey, szProduct, szUserSid, dwContext,
823 dwOptions, szValue);
824 else
825 rc = ERROR_UNKNOWN_PROPERTY;
827 RegCloseKey(sourcekey);
828 return rc;
832 /******************************************************************
833 * MsiSourceListAddSourceW (MSI.@)
835 UINT WINAPI MsiSourceListAddSourceW( LPCWSTR szProduct, LPCWSTR szUserName,
836 DWORD dwReserved, LPCWSTR szSource)
838 INT ret;
839 LPWSTR sidstr = NULL;
840 DWORD sidsize = 0;
841 DWORD domsize = 0;
843 TRACE("%s %s %s\n", debugstr_w(szProduct), debugstr_w(szUserName), debugstr_w(szSource));
845 if (LookupAccountNameW(NULL, szUserName, NULL, &sidsize, NULL, &domsize, NULL))
847 PSID psid = msi_alloc(sidsize);
849 if (LookupAccountNameW(NULL, szUserName, psid, &sidsize, NULL, &domsize, NULL))
850 ConvertSidToStringSidW(psid, &sidstr);
852 msi_free(psid);
855 ret = MsiSourceListAddSourceExW(szProduct, sidstr,
856 MSIINSTALLCONTEXT_USERMANAGED, MSISOURCETYPE_NETWORK, szSource, 0);
858 if (sidstr)
859 LocalFree(sidstr);
861 return ret;
864 /******************************************************************
865 * MsiSourceListAddSourceA (MSI.@)
867 UINT WINAPI MsiSourceListAddSourceA( LPCSTR szProduct, LPCSTR szUserName,
868 DWORD dwReserved, LPCSTR szSource)
870 INT ret;
871 LPWSTR szwproduct;
872 LPWSTR szwusername;
873 LPWSTR szwsource;
875 szwproduct = strdupAtoW( szProduct );
876 szwusername = strdupAtoW( szUserName );
877 szwsource = strdupAtoW( szSource );
879 ret = MsiSourceListAddSourceW(szwproduct, szwusername, 0, szwsource);
881 msi_free(szwproduct);
882 msi_free(szwusername);
883 msi_free(szwsource);
885 return ret;
888 /******************************************************************
889 * MsiSourceListAddSourceExA (MSI.@)
891 UINT WINAPI MsiSourceListAddSourceExA(LPCSTR szProduct, LPCSTR szUserSid,
892 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCSTR szSource, DWORD dwIndex)
894 UINT ret;
895 LPWSTR product, usersid, source;
897 product = strdupAtoW(szProduct);
898 usersid = strdupAtoW(szUserSid);
899 source = strdupAtoW(szSource);
901 ret = MsiSourceListAddSourceExW(product, usersid, dwContext,
902 dwOptions, source, dwIndex);
904 msi_free(product);
905 msi_free(usersid);
906 msi_free(source);
908 return ret;
911 static void free_source_list(struct list *sourcelist)
913 while (!list_empty(sourcelist))
915 media_info *info = LIST_ENTRY(list_head(sourcelist), media_info, entry);
916 list_remove(&info->entry);
917 msi_free(info->path);
918 msi_free(info);
922 static void add_source_to_list(struct list *sourcelist, media_info *info)
924 media_info *iter;
925 BOOL found = FALSE;
926 static const WCHAR fmt[] = {'%','i',0};
928 if (list_empty(sourcelist))
930 list_add_head(sourcelist, &info->entry);
931 return;
934 LIST_FOR_EACH_ENTRY(iter, sourcelist, media_info, entry)
936 if (!found && info->index < iter->index)
938 found = TRUE;
939 list_add_before(&iter->entry, &info->entry);
942 /* update the rest of the list */
943 if (found)
944 sprintfW(iter->szIndex, fmt, ++iter->index);
947 if (!found)
948 list_add_after(&iter->entry, &info->entry);
951 static UINT fill_source_list(struct list *sourcelist, HKEY sourcekey, DWORD *count)
953 UINT r = ERROR_SUCCESS;
954 DWORD index = 0;
955 WCHAR name[10];
956 DWORD size, val_size;
957 media_info *entry;
959 *count = 0;
961 while (r == ERROR_SUCCESS)
963 size = sizeof(name) / sizeof(name[0]);
964 r = RegEnumValueW(sourcekey, index, name, &size, NULL, NULL, NULL, &val_size);
965 if (r != ERROR_SUCCESS)
966 return r;
968 entry = msi_alloc(sizeof(media_info));
969 if (!entry)
970 goto error;
972 entry->path = msi_alloc(val_size);
973 if (!entry->path)
975 msi_free(entry);
976 goto error;
979 lstrcpyW(entry->szIndex, name);
980 entry->index = atoiW(name);
982 size++;
983 r = RegEnumValueW(sourcekey, index, name, &size, NULL,
984 NULL, (LPBYTE)entry->path, &val_size);
985 if (r != ERROR_SUCCESS)
987 msi_free(entry->path);
988 msi_free(entry);
989 goto error;
992 index = ++(*count);
993 add_source_to_list(sourcelist, entry);
996 error:
997 *count = -1;
998 free_source_list(sourcelist);
999 return ERROR_OUTOFMEMORY;
1002 /******************************************************************
1003 * MsiSourceListAddSourceExW (MSI.@)
1005 UINT WINAPI MsiSourceListAddSourceExW( LPCWSTR szProduct, LPCWSTR szUserSid,
1006 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCWSTR szSource,
1007 DWORD dwIndex)
1009 HKEY sourcekey;
1010 HKEY typekey;
1011 UINT rc;
1012 struct list sourcelist;
1013 media_info *info;
1014 WCHAR squished_pc[GUID_SIZE];
1015 WCHAR name[10];
1016 LPWSTR source;
1017 LPCWSTR postfix;
1018 DWORD size, count;
1020 static const WCHAR fmt[] = {'%','i',0};
1021 static const WCHAR one[] = {'1',0};
1022 static const WCHAR backslash[] = {'\\',0};
1023 static const WCHAR forwardslash[] = {'/',0};
1025 TRACE("%s %s %x %x %s %i\n", debugstr_w(szProduct), debugstr_w(szUserSid),
1026 dwContext, dwOptions, debugstr_w(szSource), dwIndex);
1028 if (!szProduct || !squash_guid(szProduct, squished_pc))
1029 return ERROR_INVALID_PARAMETER;
1031 if (!szSource || !*szSource)
1032 return ERROR_INVALID_PARAMETER;
1034 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
1035 return ERROR_INVALID_PARAMETER;
1037 if (dwOptions & MSICODE_PATCH)
1039 FIXME("Unhandled options MSICODE_PATCH\n");
1040 return ERROR_FUNCTION_FAILED;
1043 if (szUserSid && (dwContext & MSIINSTALLCONTEXT_MACHINE))
1044 return ERROR_INVALID_PARAMETER;
1046 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1047 if (rc != ERROR_SUCCESS)
1048 return rc;
1050 if (dwOptions & MSISOURCETYPE_NETWORK)
1051 rc = OpenNetworkSubkey(sourcekey, &typekey, TRUE);
1052 else if (dwOptions & MSISOURCETYPE_URL)
1053 rc = OpenURLSubkey(sourcekey, &typekey, TRUE);
1054 else if (dwOptions & MSISOURCETYPE_MEDIA)
1055 rc = OpenMediaSubkey(sourcekey, &typekey, TRUE);
1056 else
1058 ERR("unknown media type: %08x\n", dwOptions);
1059 RegCloseKey(sourcekey);
1060 return ERROR_FUNCTION_FAILED;
1063 postfix = (dwOptions & MSISOURCETYPE_NETWORK) ? backslash : forwardslash;
1064 if (szSource[lstrlenW(szSource) - 1] == *postfix)
1065 source = strdupW(szSource);
1066 else
1068 size = lstrlenW(szSource) + 2;
1069 source = msi_alloc(size * sizeof(WCHAR));
1070 lstrcpyW(source, szSource);
1071 lstrcatW(source, postfix);
1074 list_init(&sourcelist);
1075 rc = fill_source_list(&sourcelist, typekey, &count);
1076 if (rc != ERROR_NO_MORE_ITEMS)
1077 return rc;
1079 size = (lstrlenW(source) + 1) * sizeof(WCHAR);
1081 if (count == 0)
1083 rc = RegSetValueExW(typekey, one, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1084 goto done;
1086 else if (dwIndex > count)
1088 sprintfW(name, fmt, count + 1);
1089 rc = RegSetValueExW(typekey, name, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1090 goto done;
1092 else
1094 /* add to the end of the list */
1095 if (dwIndex == 0)
1096 dwIndex = count + 1;
1098 sprintfW(name, fmt, dwIndex);
1099 info = msi_alloc(sizeof(media_info));
1100 if (!info)
1102 rc = ERROR_OUTOFMEMORY;
1103 goto done;
1106 info->path = strdupW(source);
1107 lstrcpyW(info->szIndex, name);
1108 info->index = dwIndex;
1109 add_source_to_list(&sourcelist, info);
1111 LIST_FOR_EACH_ENTRY(info, &sourcelist, media_info, entry)
1113 size = (lstrlenW(info->path) + 1) * sizeof(WCHAR);
1114 rc = RegSetValueExW(typekey, info->szIndex, 0,
1115 REG_EXPAND_SZ, (LPBYTE)info->path, size);
1116 if (rc != ERROR_SUCCESS)
1117 goto done;
1121 done:
1122 free_source_list(&sourcelist);
1123 msi_free(source);
1124 RegCloseKey(typekey);
1125 RegCloseKey(sourcekey);
1126 return rc;
1129 /******************************************************************
1130 * MsiSourceListAddMediaDiskA (MSI.@)
1132 UINT WINAPI MsiSourceListAddMediaDiskA(LPCSTR szProduct, LPCSTR szUserSid,
1133 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1134 LPCSTR szVolumeLabel, LPCSTR szDiskPrompt)
1136 UINT r;
1137 LPWSTR product = NULL;
1138 LPWSTR usersid = NULL;
1139 LPWSTR volume = NULL;
1140 LPWSTR prompt = NULL;
1142 if (szProduct) product = strdupAtoW(szProduct);
1143 if (szUserSid) usersid = strdupAtoW(szUserSid);
1144 if (szVolumeLabel) volume = strdupAtoW(szVolumeLabel);
1145 if (szDiskPrompt) prompt = strdupAtoW(szDiskPrompt);
1147 r = MsiSourceListAddMediaDiskW(product, usersid, dwContext, dwOptions,
1148 dwDiskId, volume, prompt);
1150 msi_free(product);
1151 msi_free(usersid);
1152 msi_free(volume);
1153 msi_free(prompt);
1155 return r;
1158 /******************************************************************
1159 * MsiSourceListAddMediaDiskW (MSI.@)
1161 UINT WINAPI MsiSourceListAddMediaDiskW(LPCWSTR szProduct, LPCWSTR szUserSid,
1162 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1163 LPCWSTR szVolumeLabel, LPCWSTR szDiskPrompt)
1165 HKEY sourcekey;
1166 HKEY mediakey;
1167 UINT rc;
1168 WCHAR szIndex[10];
1169 WCHAR squished_pc[GUID_SIZE];
1170 LPWSTR buffer;
1171 DWORD size;
1173 static const WCHAR fmt[] = {'%','i',0};
1174 static const WCHAR semicolon[] = {';',0};
1176 TRACE("%s %s %x %x %i %s %s\n", debugstr_w(szProduct),
1177 debugstr_w(szUserSid), dwContext, dwOptions, dwDiskId,
1178 debugstr_w(szVolumeLabel), debugstr_w(szDiskPrompt));
1180 if (!szProduct || !squash_guid(szProduct, squished_pc))
1181 return ERROR_INVALID_PARAMETER;
1183 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
1184 return ERROR_INVALID_PARAMETER;
1186 if ((szVolumeLabel && !*szVolumeLabel) || (szDiskPrompt && !*szDiskPrompt))
1187 return ERROR_INVALID_PARAMETER;
1189 if ((dwContext & MSIINSTALLCONTEXT_MACHINE) && szUserSid)
1190 return ERROR_INVALID_PARAMETER;
1192 if (dwOptions & MSICODE_PATCH)
1194 FIXME("Unhandled options MSICODE_PATCH\n");
1195 return ERROR_FUNCTION_FAILED;
1198 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1199 if (rc != ERROR_SUCCESS)
1200 return rc;
1202 OpenMediaSubkey(sourcekey, &mediakey, TRUE);
1204 sprintfW(szIndex, fmt, dwDiskId);
1206 size = 2;
1207 if (szVolumeLabel) size += lstrlenW(szVolumeLabel);
1208 if (szDiskPrompt) size += lstrlenW(szDiskPrompt);
1210 size *= sizeof(WCHAR);
1211 buffer = msi_alloc(size);
1212 *buffer = '\0';
1214 if (szVolumeLabel) lstrcpyW(buffer, szVolumeLabel);
1215 lstrcatW(buffer, semicolon);
1216 if (szDiskPrompt) lstrcatW(buffer, szDiskPrompt);
1218 RegSetValueExW(mediakey, szIndex, 0, REG_SZ, (LPBYTE)buffer, size);
1219 msi_free(buffer);
1221 RegCloseKey(sourcekey);
1222 RegCloseKey(mediakey);
1224 return ERROR_SUCCESS;
1227 /******************************************************************
1228 * MsiSourceListClearAllA (MSI.@)
1230 UINT WINAPI MsiSourceListClearAllA( LPCSTR szProduct, LPCSTR szUserName, DWORD dwReserved )
1232 FIXME("(%s %s %d)\n", debugstr_a(szProduct), debugstr_a(szUserName), dwReserved);
1233 return ERROR_SUCCESS;
1236 /******************************************************************
1237 * MsiSourceListClearAllW (MSI.@)
1239 UINT WINAPI MsiSourceListClearAllW( LPCWSTR szProduct, LPCWSTR szUserName, DWORD dwReserved )
1241 FIXME("(%s %s %d)\n", debugstr_w(szProduct), debugstr_w(szUserName), dwReserved);
1242 return ERROR_SUCCESS;