webservices: Accept zero write option in WsWriteType.
[wine.git] / dlls / msi / source.c
blob8f5d1658aa09e9078135fa97f841cf04b80f70b8
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
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "winnls.h"
29 #include "shlwapi.h"
30 #include "wine/debug.h"
31 #include "msi.h"
32 #include "msiquery.h"
33 #include "msipriv.h"
34 #include "wincrypt.h"
35 #include "winver.h"
36 #include "winuser.h"
37 #include "wine/unicode.h"
38 #include "sddl.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msi);
43 * These apis are defined in MSI 3.0
46 typedef struct tagMediaInfo
48 struct list entry;
49 LPWSTR path;
50 WCHAR szIndex[10];
51 DWORD index;
52 } media_info;
54 static UINT OpenSourceKey(LPCWSTR szProduct, HKEY* key, DWORD dwOptions,
55 MSIINSTALLCONTEXT context, BOOL create)
57 HKEY rootkey = 0;
58 UINT rc = ERROR_FUNCTION_FAILED;
60 if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
62 if (dwOptions & MSICODE_PATCH)
63 rc = MSIREG_OpenUserPatchesKey(szProduct, &rootkey, create);
64 else
65 rc = MSIREG_OpenProductKey(szProduct, NULL, context,
66 &rootkey, create);
68 else if (context == MSIINSTALLCONTEXT_USERMANAGED)
70 if (dwOptions & MSICODE_PATCH)
71 rc = MSIREG_OpenUserPatchesKey(szProduct, &rootkey, create);
72 else
73 rc = MSIREG_OpenProductKey(szProduct, NULL, context,
74 &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, NULL, context,
82 &rootkey, create);
85 if (rc != ERROR_SUCCESS)
87 if (dwOptions & MSICODE_PATCH)
88 return ERROR_UNKNOWN_PATCH;
89 else
90 return ERROR_UNKNOWN_PRODUCT;
93 if (create)
94 rc = RegCreateKeyW(rootkey, szSourceList, key);
95 else
97 rc = RegOpenKeyW(rootkey,szSourceList, key);
98 if (rc != ERROR_SUCCESS)
99 rc = ERROR_BAD_CONFIGURATION;
102 return rc;
105 static UINT OpenMediaSubkey(HKEY rootkey, HKEY *key, BOOL create)
107 UINT rc;
108 static const WCHAR media[] = {'M','e','d','i','a',0};
110 if (create)
111 rc = RegCreateKeyW(rootkey, media, key);
112 else
113 rc = RegOpenKeyW(rootkey,media, key);
115 return rc;
118 static UINT OpenNetworkSubkey(HKEY rootkey, HKEY *key, BOOL create)
120 UINT rc;
121 static const WCHAR net[] = {'N','e','t',0};
123 if (create)
124 rc = RegCreateKeyW(rootkey, net, key);
125 else
126 rc = RegOpenKeyW(rootkey, net, key);
128 return rc;
131 static UINT OpenURLSubkey(HKEY rootkey, HKEY *key, BOOL create)
133 UINT rc;
134 static const WCHAR URL[] = {'U','R','L',0};
136 if (create)
137 rc = RegCreateKeyW(rootkey, URL, key);
138 else
139 rc = RegOpenKeyW(rootkey, URL, key);
141 return rc;
144 /******************************************************************
145 * MsiSourceListEnumMediaDisksA (MSI.@)
147 UINT WINAPI MsiSourceListEnumMediaDisksA(LPCSTR szProductCodeOrPatchCode,
148 LPCSTR szUserSid, MSIINSTALLCONTEXT dwContext,
149 DWORD dwOptions, DWORD dwIndex, LPDWORD pdwDiskId,
150 LPSTR szVolumeLabel, LPDWORD pcchVolumeLabel,
151 LPSTR szDiskPrompt, LPDWORD pcchDiskPrompt)
153 LPWSTR product = NULL;
154 LPWSTR usersid = NULL;
155 LPWSTR volume = NULL;
156 LPWSTR prompt = NULL;
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 WideCharToMultiByte(CP_ACP, 0, volume, -1, szVolumeLabel,
187 *pcchVolumeLabel + 1, NULL, NULL);
189 if (szDiskPrompt)
190 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, LPDWORD pdwDiskId,
208 LPWSTR szVolumeLabel, LPDWORD pcchVolumeLabel,
209 LPWSTR szDiskPrompt, LPDWORD pcchDiskPrompt)
211 WCHAR squished_pc[GUID_SIZE];
212 WCHAR convert[11];
213 LPWSTR value = NULL;
214 LPWSTR data = NULL;
215 LPWSTR ptr, ptr2;
216 HKEY source, media;
217 DWORD valuesz, datasz = 0;
218 DWORD type;
219 DWORD numvals, size;
220 LONG res;
221 UINT r;
222 static DWORD index = 0;
224 static const WCHAR fmt[] = {'#','%','d',0};
226 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p)\n", debugstr_w(szProductCodeOrPatchCode),
227 debugstr_w(szUserSid), dwContext, dwOptions, dwIndex, szVolumeLabel,
228 pcchVolumeLabel, szDiskPrompt, pcchDiskPrompt);
230 if (!szProductCodeOrPatchCode ||
231 !squash_guid(szProductCodeOrPatchCode, squished_pc))
232 return ERROR_INVALID_PARAMETER;
234 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
235 return ERROR_INVALID_PARAMETER;
237 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
238 return ERROR_INVALID_PARAMETER;
240 if (szDiskPrompt && !pcchDiskPrompt)
241 return ERROR_INVALID_PARAMETER;
243 if (dwIndex == 0)
244 index = 0;
246 if (dwIndex != index)
247 return ERROR_INVALID_PARAMETER;
249 r = OpenSourceKey(szProductCodeOrPatchCode, &source,
250 dwOptions, dwContext, FALSE);
251 if (r != ERROR_SUCCESS)
252 return r;
254 r = OpenMediaSubkey(source, &media, FALSE);
255 if (r != ERROR_SUCCESS)
257 RegCloseKey(source);
258 return ERROR_NO_MORE_ITEMS;
261 res = RegQueryInfoKeyW(media, NULL, NULL, NULL, NULL, NULL,
262 NULL, &numvals, &valuesz, &datasz, NULL, NULL);
263 if (res != ERROR_SUCCESS)
265 r = ERROR_BAD_CONFIGURATION;
266 goto done;
269 value = msi_alloc(++valuesz * sizeof(WCHAR));
270 data = msi_alloc(++datasz * sizeof(WCHAR));
271 if (!value || !data)
273 r = ERROR_OUTOFMEMORY;
274 goto done;
277 r = RegEnumValueW(media, dwIndex, value, &valuesz,
278 NULL, &type, (LPBYTE)data, &datasz);
279 if (r != ERROR_SUCCESS)
280 goto done;
282 if (pdwDiskId)
283 *pdwDiskId = atolW(value);
285 ptr2 = data;
286 ptr = strchrW(data, ';');
287 if (!ptr)
288 ptr = data;
289 else
290 *ptr = '\0';
292 if (pcchVolumeLabel)
294 if (type == REG_DWORD)
296 sprintfW(convert, fmt, *data);
297 size = lstrlenW(convert);
298 ptr2 = convert;
300 else
301 size = lstrlenW(data);
303 if (size >= *pcchVolumeLabel)
304 r = ERROR_MORE_DATA;
305 else if (szVolumeLabel)
306 lstrcpyW(szVolumeLabel, ptr2);
308 *pcchVolumeLabel = size;
311 if (pcchDiskPrompt)
313 if (!*ptr)
314 ptr++;
316 if (type == REG_DWORD)
318 sprintfW(convert, fmt, *ptr);
319 size = lstrlenW(convert);
320 ptr = convert;
322 else
323 size = lstrlenW(ptr);
325 if (size >= *pcchDiskPrompt)
326 r = ERROR_MORE_DATA;
327 else if (szDiskPrompt)
328 lstrcpyW(szDiskPrompt, ptr);
330 *pcchDiskPrompt = size;
333 index++;
335 done:
336 msi_free(value);
337 msi_free(data);
338 RegCloseKey(source);
340 return r;
343 /******************************************************************
344 * MsiSourceListEnumSourcesA (MSI.@)
346 UINT WINAPI MsiSourceListEnumSourcesA(LPCSTR szProductCodeOrPatch, LPCSTR szUserSid,
347 MSIINSTALLCONTEXT dwContext,
348 DWORD dwOptions, DWORD dwIndex,
349 LPSTR szSource, LPDWORD pcchSource)
351 LPWSTR product = NULL;
352 LPWSTR usersid = NULL;
353 LPWSTR source = NULL;
354 DWORD len = 0;
355 UINT r = ERROR_INVALID_PARAMETER;
356 static DWORD index = 0;
358 TRACE("(%s, %s, %d, %d, %d, %p, %p)\n", debugstr_a(szProductCodeOrPatch),
359 debugstr_a(szUserSid), dwContext, dwOptions, dwIndex, szSource, pcchSource);
361 if (dwIndex == 0)
362 index = 0;
364 if (szSource && !pcchSource)
365 goto done;
367 if (dwIndex != index)
368 goto done;
370 if (szProductCodeOrPatch) product = strdupAtoW(szProductCodeOrPatch);
371 if (szUserSid) usersid = strdupAtoW(szUserSid);
373 r = MsiSourceListEnumSourcesW(product, usersid, dwContext, dwOptions,
374 dwIndex, NULL, &len);
375 if (r != ERROR_SUCCESS)
376 goto done;
378 source = msi_alloc(++len * sizeof(WCHAR));
379 if (!source)
381 r = ERROR_OUTOFMEMORY;
382 goto done;
385 *source = '\0';
386 r = MsiSourceListEnumSourcesW(product, usersid, dwContext, dwOptions,
387 dwIndex, source, &len);
388 if (r != ERROR_SUCCESS)
389 goto done;
391 len = WideCharToMultiByte(CP_ACP, 0, source, -1, NULL, 0, NULL, NULL);
392 if (pcchSource && *pcchSource >= len)
393 WideCharToMultiByte(CP_ACP, 0, source, -1, szSource, len, NULL, NULL);
394 else if (szSource)
395 r = ERROR_MORE_DATA;
397 if (pcchSource)
398 *pcchSource = len - 1;
400 done:
401 msi_free(product);
402 msi_free(usersid);
403 msi_free(source);
405 if (r == ERROR_SUCCESS)
407 if (szSource || !pcchSource) index++;
409 else if (dwIndex > index)
410 index = 0;
412 return r;
415 /******************************************************************
416 * MsiSourceListEnumSourcesW (MSI.@)
418 UINT WINAPI MsiSourceListEnumSourcesW(LPCWSTR szProductCodeOrPatch, LPCWSTR szUserSid,
419 MSIINSTALLCONTEXT dwContext,
420 DWORD dwOptions, DWORD dwIndex,
421 LPWSTR szSource, LPDWORD pcchSource)
423 WCHAR squished_pc[GUID_SIZE];
424 WCHAR name[32];
425 HKEY source = NULL;
426 HKEY subkey = NULL;
427 LONG res;
428 UINT r = ERROR_INVALID_PARAMETER;
429 static DWORD index = 0;
431 static const WCHAR format[] = {'%','d',0};
433 TRACE("(%s, %s, %d, %d, %d, %p, %p)\n", debugstr_w(szProductCodeOrPatch),
434 debugstr_w(szUserSid), dwContext, dwOptions, dwIndex, szSource, pcchSource);
436 if (dwIndex == 0)
437 index = 0;
439 if (!szProductCodeOrPatch || !squash_guid(szProductCodeOrPatch, squished_pc))
440 goto done;
442 if (szSource && !pcchSource)
443 goto done;
445 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
446 goto done;
448 if ((dwOptions & MSISOURCETYPE_NETWORK) && (dwOptions & MSISOURCETYPE_URL))
449 goto done;
451 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
452 goto done;
454 if (dwIndex != index)
455 goto done;
457 r = OpenSourceKey(szProductCodeOrPatch, &source,
458 dwOptions, dwContext, FALSE);
459 if (r != ERROR_SUCCESS)
460 goto done;
462 if (dwOptions & MSISOURCETYPE_NETWORK)
463 r = OpenNetworkSubkey(source, &subkey, FALSE);
464 else if (dwOptions & MSISOURCETYPE_URL)
465 r = OpenURLSubkey(source, &subkey, FALSE);
467 if (r != ERROR_SUCCESS)
469 r = ERROR_NO_MORE_ITEMS;
470 goto done;
473 sprintfW(name, format, dwIndex + 1);
475 res = RegQueryValueExW(subkey, name, 0, 0, (LPBYTE)szSource, pcchSource);
476 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA)
477 r = ERROR_NO_MORE_ITEMS;
479 done:
480 RegCloseKey(subkey);
481 RegCloseKey(source);
483 if (r == ERROR_SUCCESS)
485 if (szSource || !pcchSource) index++;
487 else if (dwIndex > index)
488 index = 0;
490 return r;
493 /******************************************************************
494 * MsiSourceListGetInfoA (MSI.@)
496 UINT WINAPI MsiSourceListGetInfoA( LPCSTR szProduct, LPCSTR szUserSid,
497 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
498 LPCSTR szProperty, LPSTR szValue,
499 LPDWORD pcchValue)
501 UINT ret;
502 LPWSTR product = NULL;
503 LPWSTR usersid = NULL;
504 LPWSTR property = NULL;
505 LPWSTR value = NULL;
506 DWORD len = 0;
508 if (szValue && !pcchValue)
509 return ERROR_INVALID_PARAMETER;
511 if (szProduct) product = strdupAtoW(szProduct);
512 if (szUserSid) usersid = strdupAtoW(szUserSid);
513 if (szProperty) property = strdupAtoW(szProperty);
515 ret = MsiSourceListGetInfoW(product, usersid, dwContext, dwOptions,
516 property, NULL, &len);
517 if (ret != ERROR_SUCCESS)
518 goto done;
520 value = msi_alloc(++len * sizeof(WCHAR));
521 if (!value)
522 return ERROR_OUTOFMEMORY;
524 *value = '\0';
525 ret = MsiSourceListGetInfoW(product, usersid, dwContext, dwOptions,
526 property, value, &len);
527 if (ret != ERROR_SUCCESS)
528 goto done;
530 len = WideCharToMultiByte(CP_ACP, 0, value, -1, NULL, 0, NULL, NULL);
531 if (*pcchValue >= len)
532 WideCharToMultiByte(CP_ACP, 0, value, -1, szValue, len, NULL, NULL);
533 else if (szValue)
534 ret = ERROR_MORE_DATA;
536 *pcchValue = len - 1;
538 done:
539 msi_free(product);
540 msi_free(usersid);
541 msi_free(property);
542 msi_free(value);
543 return ret;
546 /******************************************************************
547 * MsiSourceListGetInfoW (MSI.@)
549 UINT WINAPI MsiSourceListGetInfoW( LPCWSTR szProduct, LPCWSTR szUserSid,
550 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
551 LPCWSTR szProperty, LPWSTR szValue,
552 LPDWORD pcchValue)
554 WCHAR squished_pc[GUID_SIZE];
555 HKEY sourcekey, media;
556 LPWSTR source, ptr;
557 DWORD size;
558 UINT rc;
560 static const WCHAR mediapack[] = {
561 'M','e','d','i','a','P','a','c','k','a','g','e',0};
563 TRACE("%s %s\n", debugstr_w(szProduct), debugstr_w(szProperty));
565 if (!szProduct || !squash_guid(szProduct, squished_pc))
566 return ERROR_INVALID_PARAMETER;
568 if (szValue && !pcchValue)
569 return ERROR_INVALID_PARAMETER;
571 if (dwContext != MSIINSTALLCONTEXT_USERMANAGED &&
572 dwContext != MSIINSTALLCONTEXT_USERUNMANAGED &&
573 dwContext != MSIINSTALLCONTEXT_MACHINE)
574 return ERROR_INVALID_PARAMETER;
576 if (!szProperty)
577 return ERROR_INVALID_PARAMETER;
579 if (szUserSid)
580 FIXME("Unhandled UserSid %s\n",debugstr_w(szUserSid));
582 rc = OpenSourceKey(szProduct, &sourcekey, dwOptions, dwContext, FALSE);
583 if (rc != ERROR_SUCCESS)
584 return rc;
586 if (!strcmpW( szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW ) ||
587 !strcmpW( szProperty, INSTALLPROPERTY_DISKPROMPTW ))
589 rc = OpenMediaSubkey(sourcekey, &media, FALSE);
590 if (rc != ERROR_SUCCESS)
592 RegCloseKey(sourcekey);
593 return ERROR_SUCCESS;
596 if (!strcmpW( szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW ))
597 szProperty = mediapack;
599 RegQueryValueExW(media, szProperty, 0, 0, (LPBYTE)szValue, pcchValue);
600 RegCloseKey(media);
602 else if (!strcmpW( szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW ) ||
603 !strcmpW( szProperty, INSTALLPROPERTY_LASTUSEDTYPEW ))
605 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
606 0, 0, NULL, &size);
607 if (rc != ERROR_SUCCESS)
609 RegCloseKey(sourcekey);
610 return ERROR_SUCCESS;
613 source = msi_alloc(size);
614 RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
615 0, 0, (LPBYTE)source, &size);
617 if (!*source)
619 msi_free(source);
620 RegCloseKey(sourcekey);
621 return ERROR_SUCCESS;
624 if (!strcmpW( szProperty, INSTALLPROPERTY_LASTUSEDTYPEW ))
626 if (*source != 'n' && *source != 'u' && *source != 'm')
628 msi_free(source);
629 RegCloseKey(sourcekey);
630 return ERROR_SUCCESS;
633 ptr = source;
634 source[1] = '\0';
636 else
638 ptr = strrchrW(source, ';');
639 if (!ptr)
640 ptr = source;
641 else
642 ptr++;
645 if (szValue)
647 if (strlenW(ptr) < *pcchValue)
648 lstrcpyW(szValue, ptr);
649 else
650 rc = ERROR_MORE_DATA;
653 *pcchValue = lstrlenW(ptr);
654 msi_free(source);
656 else if (!strcmpW( szProperty, INSTALLPROPERTY_PACKAGENAMEW ))
658 *pcchValue = *pcchValue * sizeof(WCHAR);
659 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_PACKAGENAMEW, 0, 0,
660 (LPBYTE)szValue, pcchValue);
661 if (rc != ERROR_SUCCESS && rc != ERROR_MORE_DATA)
663 *pcchValue = 0;
664 rc = ERROR_SUCCESS;
666 else
668 if (*pcchValue)
669 *pcchValue = (*pcchValue - 1) / sizeof(WCHAR);
670 if (szValue)
671 szValue[*pcchValue] = '\0';
674 else
676 FIXME("Unknown property %s\n",debugstr_w(szProperty));
677 rc = ERROR_UNKNOWN_PROPERTY;
680 RegCloseKey(sourcekey);
681 return rc;
684 /******************************************************************
685 * MsiSourceListSetInfoA (MSI.@)
687 UINT WINAPI MsiSourceListSetInfoA(LPCSTR szProduct, LPCSTR szUserSid,
688 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
689 LPCSTR szProperty, LPCSTR szValue)
691 UINT ret;
692 LPWSTR product = NULL;
693 LPWSTR usersid = NULL;
694 LPWSTR property = NULL;
695 LPWSTR value = NULL;
697 if (szProduct) product = strdupAtoW(szProduct);
698 if (szUserSid) usersid = strdupAtoW(szUserSid);
699 if (szProperty) property = strdupAtoW(szProperty);
700 if (szValue) value = strdupAtoW(szValue);
702 ret = MsiSourceListSetInfoW(product, usersid, dwContext, dwOptions,
703 property, value);
705 msi_free(product);
706 msi_free(usersid);
707 msi_free(property);
708 msi_free(value);
710 return ret;
713 UINT msi_set_last_used_source(LPCWSTR product, LPCWSTR usersid,
714 MSIINSTALLCONTEXT context, DWORD options,
715 LPCWSTR value)
717 HKEY source;
718 LPWSTR buffer;
719 WCHAR typechar;
720 DWORD size;
721 UINT r;
722 int index = 1;
724 static const WCHAR format[] = {'%','c',';','%','i',';','%','s',0};
726 if (options & MSISOURCETYPE_NETWORK)
727 typechar = 'n';
728 else if (options & MSISOURCETYPE_URL)
729 typechar = 'u';
730 else if (options & MSISOURCETYPE_MEDIA)
731 typechar = 'm';
732 else
733 return ERROR_INVALID_PARAMETER;
735 if (!(options & MSISOURCETYPE_MEDIA))
737 r = MsiSourceListAddSourceExW(product, usersid, context,
738 options, value, 0);
739 if (r != ERROR_SUCCESS)
740 return r;
742 index = 0;
743 while ((r = MsiSourceListEnumSourcesW(product, usersid, context, options,
744 index, NULL, NULL)) == ERROR_SUCCESS)
745 index++;
747 if (r != ERROR_NO_MORE_ITEMS)
748 return r;
751 size = (lstrlenW(format) + lstrlenW(value) + 7) * sizeof(WCHAR);
752 buffer = msi_alloc(size);
753 if (!buffer)
754 return ERROR_OUTOFMEMORY;
756 r = OpenSourceKey(product, &source, MSICODE_PRODUCT, context, FALSE);
757 if (r != ERROR_SUCCESS)
759 msi_free(buffer);
760 return r;
763 sprintfW(buffer, format, typechar, index, value);
765 size = (lstrlenW(buffer) + 1) * sizeof(WCHAR);
766 r = RegSetValueExW(source, INSTALLPROPERTY_LASTUSEDSOURCEW, 0,
767 REG_SZ, (LPBYTE)buffer, size);
768 msi_free(buffer);
770 RegCloseKey(source);
771 return r;
774 /******************************************************************
775 * MsiSourceListSetInfoW (MSI.@)
777 UINT WINAPI MsiSourceListSetInfoW( LPCWSTR szProduct, LPCWSTR szUserSid,
778 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
779 LPCWSTR szProperty, LPCWSTR szValue)
781 WCHAR squished_pc[GUID_SIZE];
782 HKEY sourcekey, media;
783 LPCWSTR property;
784 UINT rc;
786 static const WCHAR media_package[] = {
787 'M','e','d','i','a','P','a','c','k','a','g','e',0
790 TRACE("%s %s %x %x %s %s\n", debugstr_w(szProduct), debugstr_w(szUserSid),
791 dwContext, dwOptions, debugstr_w(szProperty), debugstr_w(szValue));
793 if (!szProduct || !squash_guid(szProduct, squished_pc))
794 return ERROR_INVALID_PARAMETER;
796 if (!szProperty)
797 return ERROR_INVALID_PARAMETER;
799 if (!szValue)
800 return ERROR_UNKNOWN_PROPERTY;
802 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
803 return ERROR_INVALID_PARAMETER;
805 if (dwOptions & MSICODE_PATCH)
807 FIXME("Unhandled options MSICODE_PATCH\n");
808 return ERROR_UNKNOWN_PATCH;
811 property = szProperty;
812 if (!strcmpW( szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW ))
813 property = media_package;
815 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
816 if (rc != ERROR_SUCCESS)
817 return rc;
819 if (strcmpW( szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW ) &&
820 dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL))
822 RegCloseKey(sourcekey);
823 return ERROR_INVALID_PARAMETER;
826 if (!strcmpW( szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW ) ||
827 !strcmpW( szProperty, INSTALLPROPERTY_DISKPROMPTW ))
829 rc = OpenMediaSubkey(sourcekey, &media, TRUE);
830 if (rc == ERROR_SUCCESS)
832 rc = msi_reg_set_val_str(media, property, szValue);
833 RegCloseKey(media);
836 else if (!strcmpW( szProperty, INSTALLPROPERTY_PACKAGENAMEW ))
838 DWORD size = (lstrlenW(szValue) + 1) * sizeof(WCHAR);
839 rc = RegSetValueExW(sourcekey, INSTALLPROPERTY_PACKAGENAMEW, 0,
840 REG_SZ, (const BYTE *)szValue, size);
841 if (rc != ERROR_SUCCESS)
842 rc = ERROR_UNKNOWN_PROPERTY;
844 else if (!strcmpW( szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW ))
846 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
847 rc = ERROR_INVALID_PARAMETER;
848 else
849 rc = msi_set_last_used_source(szProduct, szUserSid, dwContext,
850 dwOptions, szValue);
852 else
853 rc = ERROR_UNKNOWN_PROPERTY;
855 RegCloseKey(sourcekey);
856 return rc;
859 /******************************************************************
860 * MsiSourceListAddSourceW (MSI.@)
862 UINT WINAPI MsiSourceListAddSourceW( LPCWSTR szProduct, LPCWSTR szUserName,
863 DWORD dwReserved, LPCWSTR szSource)
865 WCHAR squished_pc[GUID_SIZE];
866 INT ret;
867 LPWSTR sidstr = NULL;
868 DWORD sidsize = 0;
869 DWORD domsize = 0;
870 DWORD context;
871 HKEY hkey = 0;
872 UINT r;
874 TRACE("%s %s %s\n", debugstr_w(szProduct), debugstr_w(szUserName), debugstr_w(szSource));
876 if (!szSource || !*szSource)
877 return ERROR_INVALID_PARAMETER;
879 if (dwReserved != 0)
880 return ERROR_INVALID_PARAMETER;
882 if (!szProduct || !squash_guid(szProduct, squished_pc))
883 return ERROR_INVALID_PARAMETER;
885 if (!szUserName || !*szUserName)
886 context = MSIINSTALLCONTEXT_MACHINE;
887 else
889 if (LookupAccountNameW(NULL, szUserName, NULL, &sidsize, NULL, &domsize, NULL))
891 PSID psid = msi_alloc(sidsize);
893 if (LookupAccountNameW(NULL, szUserName, psid, &sidsize, NULL, &domsize, NULL))
894 ConvertSidToStringSidW(psid, &sidstr);
896 msi_free(psid);
899 r = MSIREG_OpenProductKey(szProduct, NULL,
900 MSIINSTALLCONTEXT_USERMANAGED, &hkey, FALSE);
901 if (r == ERROR_SUCCESS)
902 context = MSIINSTALLCONTEXT_USERMANAGED;
903 else
905 r = MSIREG_OpenProductKey(szProduct, NULL,
906 MSIINSTALLCONTEXT_USERUNMANAGED,
907 &hkey, FALSE);
908 if (r != ERROR_SUCCESS)
909 return ERROR_UNKNOWN_PRODUCT;
911 context = MSIINSTALLCONTEXT_USERUNMANAGED;
914 RegCloseKey(hkey);
917 ret = MsiSourceListAddSourceExW(szProduct, sidstr,
918 context, MSISOURCETYPE_NETWORK, szSource, 0);
920 if (sidstr)
921 LocalFree(sidstr);
923 return ret;
926 /******************************************************************
927 * MsiSourceListAddSourceA (MSI.@)
929 UINT WINAPI MsiSourceListAddSourceA( LPCSTR szProduct, LPCSTR szUserName,
930 DWORD dwReserved, LPCSTR szSource)
932 INT ret;
933 LPWSTR szwproduct;
934 LPWSTR szwusername;
935 LPWSTR szwsource;
937 szwproduct = strdupAtoW( szProduct );
938 szwusername = strdupAtoW( szUserName );
939 szwsource = strdupAtoW( szSource );
941 ret = MsiSourceListAddSourceW(szwproduct, szwusername, dwReserved, szwsource);
943 msi_free(szwproduct);
944 msi_free(szwusername);
945 msi_free(szwsource);
947 return ret;
950 /******************************************************************
951 * MsiSourceListAddSourceExA (MSI.@)
953 UINT WINAPI MsiSourceListAddSourceExA(LPCSTR szProduct, LPCSTR szUserSid,
954 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCSTR szSource, DWORD dwIndex)
956 UINT ret;
957 LPWSTR product, usersid, source;
959 product = strdupAtoW(szProduct);
960 usersid = strdupAtoW(szUserSid);
961 source = strdupAtoW(szSource);
963 ret = MsiSourceListAddSourceExW(product, usersid, dwContext,
964 dwOptions, source, dwIndex);
966 msi_free(product);
967 msi_free(usersid);
968 msi_free(source);
970 return ret;
973 static void free_source_list(struct list *sourcelist)
975 while (!list_empty(sourcelist))
977 media_info *info = LIST_ENTRY(list_head(sourcelist), media_info, entry);
978 list_remove(&info->entry);
979 msi_free(info->path);
980 msi_free(info);
984 static void add_source_to_list(struct list *sourcelist, media_info *info,
985 DWORD *index)
987 media_info *iter;
988 BOOL found = FALSE;
989 static const WCHAR fmt[] = {'%','i',0};
991 if (index) *index = 0;
993 if (list_empty(sourcelist))
995 list_add_head(sourcelist, &info->entry);
996 return;
999 LIST_FOR_EACH_ENTRY(iter, sourcelist, media_info, entry)
1001 if (!found && info->index < iter->index)
1003 found = TRUE;
1004 list_add_before(&iter->entry, &info->entry);
1007 /* update the rest of the list */
1008 if (found)
1009 sprintfW(iter->szIndex, fmt, ++iter->index);
1010 else if (index)
1011 (*index)++;
1014 if (!found)
1015 list_add_after(&iter->entry, &info->entry);
1018 static UINT fill_source_list(struct list *sourcelist, HKEY sourcekey, DWORD *count)
1020 UINT r = ERROR_SUCCESS;
1021 DWORD index = 0;
1022 WCHAR name[10];
1023 DWORD size, val_size;
1024 media_info *entry;
1026 *count = 0;
1028 while (r == ERROR_SUCCESS)
1030 size = sizeof(name) / sizeof(name[0]);
1031 r = RegEnumValueW(sourcekey, index, name, &size, NULL, NULL, NULL, &val_size);
1032 if (r != ERROR_SUCCESS)
1033 return r;
1035 entry = msi_alloc(sizeof(media_info));
1036 if (!entry)
1037 goto error;
1039 entry->path = msi_alloc(val_size);
1040 if (!entry->path)
1042 msi_free(entry);
1043 goto error;
1046 lstrcpyW(entry->szIndex, name);
1047 entry->index = atoiW(name);
1049 size++;
1050 r = RegEnumValueW(sourcekey, index, name, &size, NULL,
1051 NULL, (LPBYTE)entry->path, &val_size);
1052 if (r != ERROR_SUCCESS)
1054 msi_free(entry->path);
1055 msi_free(entry);
1056 goto error;
1059 index = ++(*count);
1060 add_source_to_list(sourcelist, entry, NULL);
1063 error:
1064 *count = -1;
1065 free_source_list(sourcelist);
1066 return ERROR_OUTOFMEMORY;
1069 /******************************************************************
1070 * MsiSourceListAddSourceExW (MSI.@)
1072 UINT WINAPI MsiSourceListAddSourceExW( LPCWSTR szProduct, LPCWSTR szUserSid,
1073 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCWSTR szSource,
1074 DWORD dwIndex)
1076 HKEY sourcekey;
1077 HKEY typekey;
1078 UINT rc;
1079 struct list sourcelist;
1080 media_info *info;
1081 WCHAR squished_pc[GUID_SIZE];
1082 WCHAR name[10];
1083 LPWSTR source;
1084 LPCWSTR postfix;
1085 DWORD size, count;
1086 DWORD index;
1088 static const WCHAR fmt[] = {'%','i',0};
1090 TRACE("%s %s %x %x %s %i\n", debugstr_w(szProduct), debugstr_w(szUserSid),
1091 dwContext, dwOptions, debugstr_w(szSource), dwIndex);
1093 if (!szProduct || !squash_guid(szProduct, squished_pc))
1094 return ERROR_INVALID_PARAMETER;
1096 if (!szSource || !*szSource)
1097 return ERROR_INVALID_PARAMETER;
1099 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
1100 return ERROR_INVALID_PARAMETER;
1102 if (dwOptions & MSICODE_PATCH)
1104 FIXME("Unhandled options MSICODE_PATCH\n");
1105 return ERROR_FUNCTION_FAILED;
1108 if (szUserSid && (dwContext & MSIINSTALLCONTEXT_MACHINE))
1109 return ERROR_INVALID_PARAMETER;
1111 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1112 if (rc != ERROR_SUCCESS)
1113 return rc;
1115 if (dwOptions & MSISOURCETYPE_NETWORK)
1116 rc = OpenNetworkSubkey(sourcekey, &typekey, TRUE);
1117 else if (dwOptions & MSISOURCETYPE_URL)
1118 rc = OpenURLSubkey(sourcekey, &typekey, TRUE);
1119 else if (dwOptions & MSISOURCETYPE_MEDIA)
1120 rc = OpenMediaSubkey(sourcekey, &typekey, TRUE);
1121 else
1123 ERR("unknown media type: %08x\n", dwOptions);
1124 RegCloseKey(sourcekey);
1125 return ERROR_FUNCTION_FAILED;
1127 if (rc != ERROR_SUCCESS)
1129 ERR("can't open subkey %u\n", rc);
1130 RegCloseKey(sourcekey);
1131 return rc;
1134 postfix = (dwOptions & MSISOURCETYPE_NETWORK) ? szBackSlash : szForwardSlash;
1135 if (szSource[lstrlenW(szSource) - 1] == *postfix)
1136 source = strdupW(szSource);
1137 else
1139 size = lstrlenW(szSource) + 2;
1140 source = msi_alloc(size * sizeof(WCHAR));
1141 lstrcpyW(source, szSource);
1142 lstrcatW(source, postfix);
1145 list_init(&sourcelist);
1146 rc = fill_source_list(&sourcelist, typekey, &count);
1147 if (rc != ERROR_NO_MORE_ITEMS)
1148 goto done;
1150 size = (lstrlenW(source) + 1) * sizeof(WCHAR);
1152 if (count == 0)
1154 rc = RegSetValueExW(typekey, szOne, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1155 goto done;
1157 else if (dwIndex > count || dwIndex == 0)
1159 sprintfW(name, fmt, count + 1);
1160 rc = RegSetValueExW(typekey, name, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1161 goto done;
1163 else
1165 sprintfW(name, fmt, dwIndex);
1166 info = msi_alloc(sizeof(media_info));
1167 if (!info)
1169 rc = ERROR_OUTOFMEMORY;
1170 goto done;
1173 info->path = strdupW(source);
1174 lstrcpyW(info->szIndex, name);
1175 info->index = dwIndex;
1176 add_source_to_list(&sourcelist, info, &index);
1178 LIST_FOR_EACH_ENTRY(info, &sourcelist, media_info, entry)
1180 if (info->index < index)
1181 continue;
1183 size = (lstrlenW(info->path) + 1) * sizeof(WCHAR);
1184 rc = RegSetValueExW(typekey, info->szIndex, 0,
1185 REG_EXPAND_SZ, (LPBYTE)info->path, size);
1186 if (rc != ERROR_SUCCESS)
1187 goto done;
1191 done:
1192 free_source_list(&sourcelist);
1193 msi_free(source);
1194 RegCloseKey(typekey);
1195 RegCloseKey(sourcekey);
1196 return rc;
1199 /******************************************************************
1200 * MsiSourceListAddMediaDiskA (MSI.@)
1202 UINT WINAPI MsiSourceListAddMediaDiskA(LPCSTR szProduct, LPCSTR szUserSid,
1203 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1204 LPCSTR szVolumeLabel, LPCSTR szDiskPrompt)
1206 UINT r;
1207 LPWSTR product = NULL;
1208 LPWSTR usersid = NULL;
1209 LPWSTR volume = NULL;
1210 LPWSTR prompt = NULL;
1212 if (szProduct) product = strdupAtoW(szProduct);
1213 if (szUserSid) usersid = strdupAtoW(szUserSid);
1214 if (szVolumeLabel) volume = strdupAtoW(szVolumeLabel);
1215 if (szDiskPrompt) prompt = strdupAtoW(szDiskPrompt);
1217 r = MsiSourceListAddMediaDiskW(product, usersid, dwContext, dwOptions,
1218 dwDiskId, volume, prompt);
1220 msi_free(product);
1221 msi_free(usersid);
1222 msi_free(volume);
1223 msi_free(prompt);
1225 return r;
1228 /******************************************************************
1229 * MsiSourceListAddMediaDiskW (MSI.@)
1231 UINT WINAPI MsiSourceListAddMediaDiskW(LPCWSTR szProduct, LPCWSTR szUserSid,
1232 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1233 LPCWSTR szVolumeLabel, LPCWSTR szDiskPrompt)
1235 HKEY sourcekey;
1236 HKEY mediakey;
1237 UINT rc;
1238 WCHAR szIndex[10];
1239 WCHAR squished_pc[GUID_SIZE];
1240 LPWSTR buffer;
1241 DWORD size;
1243 static const WCHAR fmt[] = {'%','i',0};
1245 TRACE("%s %s %x %x %i %s %s\n", debugstr_w(szProduct),
1246 debugstr_w(szUserSid), dwContext, dwOptions, dwDiskId,
1247 debugstr_w(szVolumeLabel), debugstr_w(szDiskPrompt));
1249 if (!szProduct || !squash_guid(szProduct, squished_pc))
1250 return ERROR_INVALID_PARAMETER;
1252 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
1253 return ERROR_INVALID_PARAMETER;
1255 if ((szVolumeLabel && !*szVolumeLabel) || (szDiskPrompt && !*szDiskPrompt))
1256 return ERROR_INVALID_PARAMETER;
1258 if ((dwContext & MSIINSTALLCONTEXT_MACHINE) && szUserSid)
1259 return ERROR_INVALID_PARAMETER;
1261 if (dwOptions & MSICODE_PATCH)
1263 FIXME("Unhandled options MSICODE_PATCH\n");
1264 return ERROR_FUNCTION_FAILED;
1267 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1268 if (rc != ERROR_SUCCESS)
1269 return rc;
1271 OpenMediaSubkey(sourcekey, &mediakey, TRUE);
1273 sprintfW(szIndex, fmt, dwDiskId);
1275 size = 2;
1276 if (szVolumeLabel) size += lstrlenW(szVolumeLabel);
1277 if (szDiskPrompt) size += lstrlenW(szDiskPrompt);
1279 size *= sizeof(WCHAR);
1280 buffer = msi_alloc(size);
1281 *buffer = '\0';
1283 if (szVolumeLabel) lstrcpyW(buffer, szVolumeLabel);
1284 lstrcatW(buffer, szSemiColon);
1285 if (szDiskPrompt) lstrcatW(buffer, szDiskPrompt);
1287 RegSetValueExW(mediakey, szIndex, 0, REG_SZ, (LPBYTE)buffer, size);
1288 msi_free(buffer);
1290 RegCloseKey(sourcekey);
1291 RegCloseKey(mediakey);
1293 return ERROR_SUCCESS;
1296 /******************************************************************
1297 * MsiSourceListClearAllA (MSI.@)
1299 UINT WINAPI MsiSourceListClearAllA( LPCSTR szProduct, LPCSTR szUserName, DWORD dwReserved )
1301 FIXME("(%s %s %d)\n", debugstr_a(szProduct), debugstr_a(szUserName), dwReserved);
1302 return ERROR_SUCCESS;
1305 /******************************************************************
1306 * MsiSourceListClearAllW (MSI.@)
1308 UINT WINAPI MsiSourceListClearAllW( LPCWSTR szProduct, LPCWSTR szUserName, DWORD dwReserved )
1310 FIXME("(%s %s %d)\n", debugstr_w(szProduct), debugstr_w(szUserName), dwReserved);
1311 return ERROR_SUCCESS;
1314 /******************************************************************
1315 * MsiSourceListClearAllExA (MSI.@)
1317 UINT WINAPI MsiSourceListClearAllExA( LPCSTR szProduct, LPCSTR szUserSid,
1318 MSIINSTALLCONTEXT dwContext, DWORD dwOptions )
1320 FIXME("(%s %s %d %08x)\n", debugstr_a(szProduct), debugstr_a(szUserSid),
1321 dwContext, dwOptions);
1322 return ERROR_SUCCESS;
1325 /******************************************************************
1326 * MsiSourceListClearAllExW (MSI.@)
1328 UINT WINAPI MsiSourceListClearAllExW( LPCWSTR szProduct, LPCWSTR szUserSid,
1329 MSIINSTALLCONTEXT dwContext, DWORD dwOptions )
1331 FIXME("(%s %s %d %08x)\n", debugstr_w(szProduct), debugstr_w(szUserSid),
1332 dwContext, dwOptions);
1333 return ERROR_SUCCESS;
1336 /******************************************************************
1337 * MsiSourceListClearSourceA (MSI.@)
1339 UINT WINAPI MsiSourceListClearSourceA(LPCSTR szProductCodeOrPatchCode, LPCSTR szUserSid,
1340 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
1341 LPCSTR szSource)
1343 FIXME("(%s %s %x %x %s)\n", debugstr_a(szProductCodeOrPatchCode), debugstr_a(szUserSid),
1344 dwContext, dwOptions, debugstr_a(szSource));
1345 return ERROR_SUCCESS;
1348 /******************************************************************
1349 * MsiSourceListClearSourceW (MSI.@)
1351 UINT WINAPI MsiSourceListClearSourceW(LPCWSTR szProductCodeOrPatchCode, LPCWSTR szUserSid,
1352 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
1353 LPCWSTR szSource)
1355 FIXME("(%s %s %x %x %s)\n", debugstr_w(szProductCodeOrPatchCode), debugstr_w(szUserSid),
1356 dwContext, dwOptions, debugstr_w(szSource));
1357 return ERROR_SUCCESS;