winecfg: Fix the Bulgarian translation.
[wine.git] / dlls / msi / source.c
blob4c3811661fdc358b8ca87c720804f953e05a96ff
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, LPDWORD pdwDiskId,
149 LPSTR szVolumeLabel, LPDWORD pcchVolumeLabel,
150 LPSTR szDiskPrompt, LPDWORD pcchDiskPrompt)
152 LPWSTR product = NULL;
153 LPWSTR usersid = NULL;
154 LPWSTR volume = NULL;
155 LPWSTR prompt = NULL;
156 UINT r = ERROR_INVALID_PARAMETER;
158 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n", debugstr_a(szProductCodeOrPatchCode),
159 debugstr_a(szUserSid), dwContext, dwOptions, dwIndex, pdwDiskId,
160 szVolumeLabel, pcchVolumeLabel, szDiskPrompt, pcchDiskPrompt);
162 if (szDiskPrompt && !pcchDiskPrompt)
163 return ERROR_INVALID_PARAMETER;
165 if (szProductCodeOrPatchCode) product = strdupAtoW(szProductCodeOrPatchCode);
166 if (szUserSid) usersid = strdupAtoW(szUserSid);
168 /* FIXME: add tests for an invalid format */
170 if (pcchVolumeLabel)
171 volume = msi_alloc(*pcchVolumeLabel * sizeof(WCHAR));
173 if (pcchDiskPrompt)
174 prompt = msi_alloc(*pcchDiskPrompt * sizeof(WCHAR));
176 if (volume) *volume = '\0';
177 if (prompt) *prompt = '\0';
178 r = MsiSourceListEnumMediaDisksW(product, usersid, dwContext, dwOptions,
179 dwIndex, pdwDiskId, volume, pcchVolumeLabel,
180 prompt, pcchDiskPrompt);
181 if (r != ERROR_SUCCESS)
182 goto done;
184 if (szVolumeLabel && pcchVolumeLabel)
185 WideCharToMultiByte(CP_ACP, 0, volume, -1, szVolumeLabel,
186 *pcchVolumeLabel + 1, NULL, NULL);
188 if (szDiskPrompt)
189 WideCharToMultiByte(CP_ACP, 0, prompt, -1, szDiskPrompt,
190 *pcchDiskPrompt + 1, NULL, NULL);
192 done:
193 msi_free(product);
194 msi_free(usersid);
195 msi_free(volume);
196 msi_free(prompt);
198 return r;
201 /******************************************************************
202 * MsiSourceListEnumMediaDisksW (MSI.@)
204 UINT WINAPI MsiSourceListEnumMediaDisksW(LPCWSTR szProductCodeOrPatchCode,
205 LPCWSTR szUserSid, MSIINSTALLCONTEXT dwContext,
206 DWORD dwOptions, DWORD dwIndex, LPDWORD pdwDiskId,
207 LPWSTR szVolumeLabel, LPDWORD pcchVolumeLabel,
208 LPWSTR szDiskPrompt, LPDWORD pcchDiskPrompt)
210 WCHAR squished_pc[GUID_SIZE];
211 LPWSTR value = NULL;
212 LPWSTR data = NULL;
213 LPWSTR ptr;
214 HKEY source, media;
215 DWORD valuesz, datasz = 0;
216 DWORD type;
217 DWORD numvals, size;
218 LONG res;
219 UINT r;
220 static int index = 0;
222 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p)\n", debugstr_w(szProductCodeOrPatchCode),
223 debugstr_w(szUserSid), dwContext, dwOptions, dwIndex, szVolumeLabel,
224 pcchVolumeLabel, szDiskPrompt, pcchDiskPrompt);
226 if (!szProductCodeOrPatchCode ||
227 !squash_guid(szProductCodeOrPatchCode, squished_pc))
228 return ERROR_INVALID_PARAMETER;
230 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
231 return ERROR_INVALID_PARAMETER;
233 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
234 return ERROR_INVALID_PARAMETER;
236 if (szDiskPrompt && !pcchDiskPrompt)
237 return ERROR_INVALID_PARAMETER;
239 if (dwIndex == 0)
240 index = 0;
242 if (dwIndex != index)
243 return ERROR_INVALID_PARAMETER;
245 r = OpenSourceKey(szProductCodeOrPatchCode, &source,
246 dwOptions, dwContext, FALSE);
247 if (r != ERROR_SUCCESS)
248 return r;
250 r = OpenMediaSubkey(source, &media, FALSE);
251 if (r != ERROR_SUCCESS)
253 RegCloseKey(source);
254 return ERROR_NO_MORE_ITEMS;
257 if (!pcchVolumeLabel && !pcchDiskPrompt)
259 r = RegEnumValueW(media, dwIndex, NULL, NULL, NULL,
260 &type, NULL, NULL);
261 goto done;
264 res = RegQueryInfoKeyW(media, NULL, NULL, NULL, NULL, NULL,
265 NULL, &numvals, &valuesz, &datasz, NULL, NULL);
266 if (res != ERROR_SUCCESS)
268 r = ERROR_BAD_CONFIGURATION;
269 goto done;
272 value = msi_alloc(++valuesz * sizeof(WCHAR));
273 data = msi_alloc(++datasz * sizeof(WCHAR));
274 if (!value || !data)
276 r = ERROR_OUTOFMEMORY;
277 goto done;
280 r = RegEnumValueW(media, dwIndex, value, &valuesz,
281 NULL, &type, (LPBYTE)data, &datasz);
282 if (r != ERROR_SUCCESS)
283 goto done;
285 if (pdwDiskId)
286 *pdwDiskId = atolW(value);
288 ptr = strchrW(data, ';');
289 if (!ptr)
290 ptr = data;
291 else
292 *ptr = '\0';
294 if (pcchVolumeLabel)
296 size = lstrlenW(data);
297 if (size >= *pcchVolumeLabel)
298 r = ERROR_MORE_DATA;
299 else if (szVolumeLabel)
300 lstrcpyW(szVolumeLabel, data);
302 *pcchVolumeLabel = size;
305 if (pcchDiskPrompt)
307 data = ptr;
308 if (!*data)
309 data++;
311 size = lstrlenW(data);
312 if (size >= *pcchDiskPrompt)
313 r = ERROR_MORE_DATA;
314 else if (szDiskPrompt)
315 lstrcpyW(szDiskPrompt, data);
317 *pcchDiskPrompt = size;
320 index++;
322 done:
323 msi_free(value);
324 msi_free(data);
325 RegCloseKey(source);
327 return r;
330 /******************************************************************
331 * MsiSourceListEnumSourcesA (MSI.@)
333 UINT WINAPI MsiSourceListEnumSourcesA(LPCSTR szProductCodeOrPatch, LPCSTR szUserSid,
334 MSIINSTALLCONTEXT dwContext,
335 DWORD dwOptions, DWORD dwIndex,
336 LPSTR szSource, LPDWORD pcchSource)
338 LPWSTR product = NULL;
339 LPWSTR usersid = NULL;
340 LPWSTR source = NULL;
341 DWORD len = 0;
342 UINT r = ERROR_INVALID_PARAMETER;
343 static int index = 0;
345 TRACE("(%s, %s, %d, %d, %d, %p, %p)\n", debugstr_a(szProductCodeOrPatch),
346 debugstr_a(szUserSid), dwContext, dwOptions, dwIndex, szSource, pcchSource);
348 if (dwIndex == 0)
349 index = 0;
351 if (szSource && !pcchSource)
352 goto done;
354 if (dwIndex != index)
355 goto done;
357 if (szProductCodeOrPatch) product = strdupAtoW(szProductCodeOrPatch);
358 if (szUserSid) usersid = strdupAtoW(szUserSid);
360 r = MsiSourceListEnumSourcesW(product, usersid, dwContext, dwOptions,
361 dwIndex, NULL, &len);
362 if (r != ERROR_SUCCESS)
363 goto done;
365 source = msi_alloc(++len * sizeof(WCHAR));
366 if (!source)
368 r = ERROR_OUTOFMEMORY;
369 goto done;
372 *source = '\0';
373 r = MsiSourceListEnumSourcesW(product, usersid, dwContext, dwOptions,
374 dwIndex, source, &len);
375 if (r != ERROR_SUCCESS)
376 goto done;
378 len = WideCharToMultiByte(CP_ACP, 0, source, -1, NULL, 0, NULL, NULL);
379 if (pcchSource && *pcchSource >= len)
380 WideCharToMultiByte(CP_ACP, 0, source, -1, szSource, len, NULL, NULL);
381 else if (szSource)
382 r = ERROR_MORE_DATA;
384 if (pcchSource)
385 *pcchSource = len - 1;
387 done:
388 msi_free(product);
389 msi_free(usersid);
390 msi_free(source);
392 if (r == ERROR_SUCCESS)
394 if (szSource || !pcchSource) index++;
396 else if (dwIndex > index)
397 index = 0;
399 return r;
402 /******************************************************************
403 * MsiSourceListEnumSourcesW (MSI.@)
405 UINT WINAPI MsiSourceListEnumSourcesW(LPCWSTR szProductCodeOrPatch, LPCWSTR szUserSid,
406 MSIINSTALLCONTEXT dwContext,
407 DWORD dwOptions, DWORD dwIndex,
408 LPWSTR szSource, LPDWORD pcchSource)
410 WCHAR squished_pc[GUID_SIZE];
411 WCHAR name[32];
412 HKEY source = NULL;
413 HKEY subkey = NULL;
414 LONG res;
415 UINT r = ERROR_INVALID_PARAMETER;
416 static int index = 0;
418 static const WCHAR format[] = {'%','d',0};
420 TRACE("(%s, %s, %d, %d, %d, %p, %p)\n", debugstr_w(szProductCodeOrPatch),
421 debugstr_w(szUserSid), dwContext, dwOptions, dwIndex, szSource, pcchSource);
423 if (dwIndex == 0)
424 index = 0;
426 if (!szProductCodeOrPatch || !squash_guid(szProductCodeOrPatch, squished_pc))
427 goto done;
429 if (szSource && !pcchSource)
430 goto done;
432 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
433 goto done;
435 if ((dwOptions & MSISOURCETYPE_NETWORK) && (dwOptions & MSISOURCETYPE_URL))
436 goto done;
438 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
439 goto done;
441 if (dwIndex != index)
442 goto done;
444 r = OpenSourceKey(szProductCodeOrPatch, &source,
445 dwOptions, dwContext, FALSE);
446 if (r != ERROR_SUCCESS)
447 goto done;
449 if (dwOptions & MSISOURCETYPE_NETWORK)
450 r = OpenNetworkSubkey(source, &subkey, FALSE);
451 else if (dwOptions & MSISOURCETYPE_URL)
452 r = OpenURLSubkey(source, &subkey, FALSE);
454 if (r != ERROR_SUCCESS)
456 r = ERROR_NO_MORE_ITEMS;
457 goto done;
460 sprintfW(name, format, dwIndex + 1);
462 res = RegQueryValueExW(subkey, name, 0, 0, (LPBYTE)szSource, pcchSource);
463 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA)
464 r = ERROR_NO_MORE_ITEMS;
466 done:
467 RegCloseKey(subkey);
468 RegCloseKey(source);
470 if (r == ERROR_SUCCESS)
472 if (szSource || !pcchSource) index++;
474 else if (dwIndex > index)
475 index = 0;
477 return r;
480 /******************************************************************
481 * MsiSourceListGetInfoA (MSI.@)
483 UINT WINAPI MsiSourceListGetInfoA( LPCSTR szProduct, LPCSTR szUserSid,
484 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
485 LPCSTR szProperty, LPSTR szValue,
486 LPDWORD pcchValue)
488 UINT ret;
489 LPWSTR product = NULL;
490 LPWSTR usersid = NULL;
491 LPWSTR property = NULL;
492 LPWSTR value = NULL;
493 DWORD len = 0;
495 if (szValue && !pcchValue)
496 return ERROR_INVALID_PARAMETER;
498 if (szProduct) product = strdupAtoW(szProduct);
499 if (szUserSid) usersid = strdupAtoW(szUserSid);
500 if (szProperty) property = strdupAtoW(szProperty);
502 ret = MsiSourceListGetInfoW(product, usersid, dwContext, dwOptions,
503 property, NULL, &len);
504 if (ret != ERROR_SUCCESS)
505 goto done;
507 value = msi_alloc(++len * sizeof(WCHAR));
508 if (!value)
509 return ERROR_OUTOFMEMORY;
511 *value = '\0';
512 ret = MsiSourceListGetInfoW(product, usersid, dwContext, dwOptions,
513 property, value, &len);
514 if (ret != ERROR_SUCCESS)
515 goto done;
517 len = WideCharToMultiByte(CP_ACP, 0, value, -1, NULL, 0, NULL, NULL);
518 if (*pcchValue >= len)
519 WideCharToMultiByte(CP_ACP, 0, value, -1, szValue, len, NULL, NULL);
520 else if (szValue)
521 ret = ERROR_MORE_DATA;
523 *pcchValue = len - 1;
525 done:
526 msi_free(product);
527 msi_free(usersid);
528 msi_free(property);
529 msi_free(value);
530 return ret;
533 /******************************************************************
534 * MsiSourceListGetInfoW (MSI.@)
536 UINT WINAPI MsiSourceListGetInfoW( LPCWSTR szProduct, LPCWSTR szUserSid,
537 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
538 LPCWSTR szProperty, LPWSTR szValue,
539 LPDWORD pcchValue)
541 WCHAR squished_pc[GUID_SIZE];
542 HKEY sourcekey, media;
543 LPWSTR source, ptr;
544 DWORD size;
545 UINT rc;
547 static const WCHAR mediapack[] = {
548 'M','e','d','i','a','P','a','c','k','a','g','e',0};
550 TRACE("%s %s\n", debugstr_w(szProduct), debugstr_w(szProperty));
552 if (!szProduct || !squash_guid(szProduct, squished_pc))
553 return ERROR_INVALID_PARAMETER;
555 if (szValue && !pcchValue)
556 return ERROR_INVALID_PARAMETER;
558 if (dwContext != MSIINSTALLCONTEXT_USERMANAGED &&
559 dwContext != MSIINSTALLCONTEXT_USERUNMANAGED &&
560 dwContext != MSIINSTALLCONTEXT_MACHINE)
561 return ERROR_INVALID_PARAMETER;
563 if (!szProperty)
564 return ERROR_INVALID_PARAMETER;
566 if (szUserSid)
567 FIXME("Unhandled UserSid %s\n",debugstr_w(szUserSid));
569 if (dwContext != MSIINSTALLCONTEXT_USERUNMANAGED)
570 FIXME("Unhandled context %d\n", dwContext);
572 rc = OpenSourceKey(szProduct, &sourcekey, dwOptions, dwContext, FALSE);
573 if (rc != ERROR_SUCCESS)
574 return rc;
576 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW) ||
577 !lstrcmpW(szProperty, INSTALLPROPERTY_DISKPROMPTW))
579 rc = OpenMediaSubkey(sourcekey, &media, FALSE);
580 if (rc != ERROR_SUCCESS)
582 RegCloseKey(sourcekey);
583 return ERROR_SUCCESS;
586 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW))
587 szProperty = mediapack;
589 RegQueryValueExW(media, szProperty, 0, 0, (LPBYTE)szValue, pcchValue);
590 RegCloseKey(media);
592 else if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW) ||
593 !lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDTYPEW))
595 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
596 0, 0, NULL, &size);
597 if (rc != ERROR_SUCCESS)
599 RegCloseKey(sourcekey);
600 return ERROR_SUCCESS;
603 source = msi_alloc(size);
604 RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
605 0, 0, (LPBYTE)source, &size);
607 if (!*source)
609 msi_free(source);
610 RegCloseKey(sourcekey);
611 return ERROR_SUCCESS;
614 if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDTYPEW))
616 if (*source != 'n' && *source != 'u' && *source != 'm')
618 msi_free(source);
619 RegCloseKey(sourcekey);
620 return ERROR_SUCCESS;
623 ptr = source;
624 source[1] = '\0';
626 else
628 ptr = strrchrW(source, ';');
629 if (!ptr)
630 ptr = source;
631 else
632 ptr++;
635 if (szValue)
637 if (lstrlenW(ptr) < *pcchValue)
638 lstrcpyW(szValue, ptr);
639 else
640 rc = ERROR_MORE_DATA;
643 *pcchValue = lstrlenW(ptr);
644 msi_free(source);
646 else if (strcmpW(INSTALLPROPERTY_PACKAGENAMEW, szProperty)==0)
648 *pcchValue = *pcchValue * sizeof(WCHAR);
649 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_PACKAGENAMEW, 0, 0,
650 (LPBYTE)szValue, pcchValue);
651 if (rc != ERROR_SUCCESS && rc != ERROR_MORE_DATA)
653 *pcchValue = 0;
654 rc = ERROR_SUCCESS;
656 else
658 if (*pcchValue)
659 *pcchValue = (*pcchValue - 1) / sizeof(WCHAR);
660 if (szValue)
661 szValue[*pcchValue] = '\0';
664 else
666 FIXME("Unknown property %s\n",debugstr_w(szProperty));
667 rc = ERROR_UNKNOWN_PROPERTY;
670 RegCloseKey(sourcekey);
671 return rc;
674 /******************************************************************
675 * MsiSourceListSetInfoA (MSI.@)
677 UINT WINAPI MsiSourceListSetInfoA(LPCSTR szProduct, LPCSTR szUserSid,
678 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
679 LPCSTR szProperty, LPCSTR szValue)
681 UINT ret;
682 LPWSTR product = NULL;
683 LPWSTR usersid = NULL;
684 LPWSTR property = NULL;
685 LPWSTR value = NULL;
687 if (szProduct) product = strdupAtoW(szProduct);
688 if (szUserSid) usersid = strdupAtoW(szUserSid);
689 if (szProperty) property = strdupAtoW(szProperty);
690 if (szValue) value = strdupAtoW(szValue);
692 ret = MsiSourceListSetInfoW(product, usersid, dwContext, dwOptions,
693 property, value);
695 msi_free(product);
696 msi_free(usersid);
697 msi_free(property);
698 msi_free(value);
700 return ret;
703 UINT msi_set_last_used_source(LPCWSTR product, LPCWSTR usersid,
704 MSIINSTALLCONTEXT context, DWORD options,
705 LPCWSTR value)
707 HKEY source;
708 LPWSTR buffer;
709 WCHAR typechar;
710 DWORD size;
711 UINT r;
712 int index = 1;
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 if (options & MSISOURCETYPE_MEDIA)
721 typechar = 'm';
722 else
723 return ERROR_INVALID_PARAMETER;
725 if (!(options & MSISOURCETYPE_MEDIA))
727 r = MsiSourceListAddSourceExW(product, usersid, context,
728 options, value, 0);
729 if (r != ERROR_SUCCESS)
730 return r;
732 index = 0;
733 while ((r = MsiSourceListEnumSourcesW(product, usersid, context, options,
734 index, NULL, NULL)) == ERROR_SUCCESS)
735 index++;
737 if (r != ERROR_NO_MORE_ITEMS)
738 return r;
741 size = (lstrlenW(format) + lstrlenW(value) + 7) * sizeof(WCHAR);
742 buffer = msi_alloc(size);
743 if (!buffer)
744 return ERROR_OUTOFMEMORY;
746 r = OpenSourceKey(product, &source, MSICODE_PRODUCT, context, FALSE);
747 if (r != ERROR_SUCCESS)
748 return r;
750 sprintfW(buffer, format, typechar, index, value);
752 size = (lstrlenW(buffer) + 1) * sizeof(WCHAR);
753 r = RegSetValueExW(source, INSTALLPROPERTY_LASTUSEDSOURCEW, 0,
754 REG_SZ, (LPBYTE)buffer, size);
755 msi_free(buffer);
757 RegCloseKey(source);
758 return r;
761 /******************************************************************
762 * MsiSourceListSetInfoW (MSI.@)
764 UINT WINAPI MsiSourceListSetInfoW( LPCWSTR szProduct, LPCWSTR szUserSid,
765 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
766 LPCWSTR szProperty, LPCWSTR szValue)
768 WCHAR squished_pc[GUID_SIZE];
769 HKEY sourcekey, media;
770 LPCWSTR property;
771 UINT rc;
773 static const WCHAR media_package[] = {
774 'M','e','d','i','a','P','a','c','k','a','g','e',0
777 TRACE("%s %s %x %x %s %s\n", debugstr_w(szProduct), debugstr_w(szUserSid),
778 dwContext, dwOptions, debugstr_w(szProperty), debugstr_w(szValue));
780 if (!szProduct || !squash_guid(szProduct, squished_pc))
781 return ERROR_INVALID_PARAMETER;
783 if (!szProperty)
784 return ERROR_INVALID_PARAMETER;
786 if (!szValue)
787 return ERROR_UNKNOWN_PROPERTY;
789 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
790 return ERROR_INVALID_PARAMETER;
792 if (dwOptions & MSICODE_PATCH)
794 FIXME("Unhandled options MSICODE_PATCH\n");
795 return ERROR_UNKNOWN_PATCH;
798 property = szProperty;
799 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW))
800 property = media_package;
802 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
803 if (rc != ERROR_SUCCESS)
804 return rc;
806 if (lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW) &&
807 dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL))
809 RegCloseKey(sourcekey);
810 return ERROR_INVALID_PARAMETER;
813 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW) ||
814 !lstrcmpW(szProperty, INSTALLPROPERTY_DISKPROMPTW))
816 rc = OpenMediaSubkey(sourcekey, &media, TRUE);
817 if (rc == ERROR_SUCCESS)
819 rc = msi_reg_set_val_str(media, property, szValue);
820 RegCloseKey(media);
823 else if (strcmpW(INSTALLPROPERTY_PACKAGENAMEW, szProperty)==0)
825 DWORD size = (lstrlenW(szValue) + 1) * sizeof(WCHAR);
826 rc = RegSetValueExW(sourcekey, INSTALLPROPERTY_PACKAGENAMEW, 0,
827 REG_SZ, (const BYTE *)szValue, size);
828 if (rc != ERROR_SUCCESS)
829 rc = ERROR_UNKNOWN_PROPERTY;
831 else if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW))
833 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
834 rc = ERROR_INVALID_PARAMETER;
835 else
836 rc = msi_set_last_used_source(szProduct, szUserSid, dwContext,
837 dwOptions, szValue);
839 else
840 rc = ERROR_UNKNOWN_PROPERTY;
842 RegCloseKey(sourcekey);
843 return rc;
846 /******************************************************************
847 * MsiSourceListAddSourceW (MSI.@)
849 UINT WINAPI MsiSourceListAddSourceW( LPCWSTR szProduct, LPCWSTR szUserName,
850 DWORD dwReserved, LPCWSTR szSource)
852 WCHAR squished_pc[GUID_SIZE];
853 INT ret;
854 LPWSTR sidstr = NULL;
855 DWORD sidsize = 0;
856 DWORD domsize = 0;
857 DWORD context;
858 HKEY hkey = 0;
859 UINT r;
861 TRACE("%s %s %s\n", debugstr_w(szProduct), debugstr_w(szUserName), debugstr_w(szSource));
863 if (!szSource || !*szSource)
864 return ERROR_INVALID_PARAMETER;
866 if (dwReserved != 0)
867 return ERROR_INVALID_PARAMETER;
869 if (!szProduct || !squash_guid(szProduct, squished_pc))
870 return ERROR_INVALID_PARAMETER;
872 if (!szUserName || !*szUserName)
873 context = MSIINSTALLCONTEXT_MACHINE;
874 else
876 if (LookupAccountNameW(NULL, szUserName, NULL, &sidsize, NULL, &domsize, NULL))
878 PSID psid = msi_alloc(sidsize);
880 if (LookupAccountNameW(NULL, szUserName, psid, &sidsize, NULL, &domsize, NULL))
881 ConvertSidToStringSidW(psid, &sidstr);
883 msi_free(psid);
886 r = MSIREG_OpenLocalManagedProductKey(szProduct, &hkey, FALSE);
887 if (r == ERROR_SUCCESS)
888 context = MSIINSTALLCONTEXT_USERMANAGED;
889 else
891 r = MSIREG_OpenUserProductsKey(szProduct, &hkey, FALSE);
892 if (r != ERROR_SUCCESS)
893 return ERROR_UNKNOWN_PRODUCT;
895 context = MSIINSTALLCONTEXT_USERUNMANAGED;
898 RegCloseKey(hkey);
901 ret = MsiSourceListAddSourceExW(szProduct, sidstr,
902 context, MSISOURCETYPE_NETWORK, szSource, 0);
904 if (sidstr)
905 LocalFree(sidstr);
907 return ret;
910 /******************************************************************
911 * MsiSourceListAddSourceA (MSI.@)
913 UINT WINAPI MsiSourceListAddSourceA( LPCSTR szProduct, LPCSTR szUserName,
914 DWORD dwReserved, LPCSTR szSource)
916 INT ret;
917 LPWSTR szwproduct;
918 LPWSTR szwusername;
919 LPWSTR szwsource;
921 szwproduct = strdupAtoW( szProduct );
922 szwusername = strdupAtoW( szUserName );
923 szwsource = strdupAtoW( szSource );
925 ret = MsiSourceListAddSourceW(szwproduct, szwusername, dwReserved, szwsource);
927 msi_free(szwproduct);
928 msi_free(szwusername);
929 msi_free(szwsource);
931 return ret;
934 /******************************************************************
935 * MsiSourceListAddSourceExA (MSI.@)
937 UINT WINAPI MsiSourceListAddSourceExA(LPCSTR szProduct, LPCSTR szUserSid,
938 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCSTR szSource, DWORD dwIndex)
940 UINT ret;
941 LPWSTR product, usersid, source;
943 product = strdupAtoW(szProduct);
944 usersid = strdupAtoW(szUserSid);
945 source = strdupAtoW(szSource);
947 ret = MsiSourceListAddSourceExW(product, usersid, dwContext,
948 dwOptions, source, dwIndex);
950 msi_free(product);
951 msi_free(usersid);
952 msi_free(source);
954 return ret;
957 static void free_source_list(struct list *sourcelist)
959 while (!list_empty(sourcelist))
961 media_info *info = LIST_ENTRY(list_head(sourcelist), media_info, entry);
962 list_remove(&info->entry);
963 msi_free(info->path);
964 msi_free(info);
968 static void add_source_to_list(struct list *sourcelist, media_info *info,
969 DWORD *index)
971 media_info *iter;
972 BOOL found = FALSE;
973 static const WCHAR fmt[] = {'%','i',0};
975 if (index) *index = 0;
977 if (list_empty(sourcelist))
979 list_add_head(sourcelist, &info->entry);
980 return;
983 LIST_FOR_EACH_ENTRY(iter, sourcelist, media_info, entry)
985 if (!found && info->index < iter->index)
987 found = TRUE;
988 list_add_before(&iter->entry, &info->entry);
991 /* update the rest of the list */
992 if (found)
993 sprintfW(iter->szIndex, fmt, ++iter->index);
994 else if (index)
995 (*index)++;
998 if (!found)
999 list_add_after(&iter->entry, &info->entry);
1002 static UINT fill_source_list(struct list *sourcelist, HKEY sourcekey, DWORD *count)
1004 UINT r = ERROR_SUCCESS;
1005 DWORD index = 0;
1006 WCHAR name[10];
1007 DWORD size, val_size;
1008 media_info *entry;
1010 *count = 0;
1012 while (r == ERROR_SUCCESS)
1014 size = sizeof(name) / sizeof(name[0]);
1015 r = RegEnumValueW(sourcekey, index, name, &size, NULL, NULL, NULL, &val_size);
1016 if (r != ERROR_SUCCESS)
1017 return r;
1019 entry = msi_alloc(sizeof(media_info));
1020 if (!entry)
1021 goto error;
1023 entry->path = msi_alloc(val_size);
1024 if (!entry->path)
1026 msi_free(entry);
1027 goto error;
1030 lstrcpyW(entry->szIndex, name);
1031 entry->index = atoiW(name);
1033 size++;
1034 r = RegEnumValueW(sourcekey, index, name, &size, NULL,
1035 NULL, (LPBYTE)entry->path, &val_size);
1036 if (r != ERROR_SUCCESS)
1038 msi_free(entry->path);
1039 msi_free(entry);
1040 goto error;
1043 index = ++(*count);
1044 add_source_to_list(sourcelist, entry, NULL);
1047 error:
1048 *count = -1;
1049 free_source_list(sourcelist);
1050 return ERROR_OUTOFMEMORY;
1053 /******************************************************************
1054 * MsiSourceListAddSourceExW (MSI.@)
1056 UINT WINAPI MsiSourceListAddSourceExW( LPCWSTR szProduct, LPCWSTR szUserSid,
1057 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCWSTR szSource,
1058 DWORD dwIndex)
1060 HKEY sourcekey;
1061 HKEY typekey;
1062 UINT rc;
1063 struct list sourcelist;
1064 media_info *info;
1065 WCHAR squished_pc[GUID_SIZE];
1066 WCHAR name[10];
1067 LPWSTR source;
1068 LPCWSTR postfix;
1069 DWORD size, count;
1070 DWORD index;
1072 static const WCHAR fmt[] = {'%','i',0};
1073 static const WCHAR one[] = {'1',0};
1074 static const WCHAR backslash[] = {'\\',0};
1075 static const WCHAR forwardslash[] = {'/',0};
1077 TRACE("%s %s %x %x %s %i\n", debugstr_w(szProduct), debugstr_w(szUserSid),
1078 dwContext, dwOptions, debugstr_w(szSource), dwIndex);
1080 if (!szProduct || !squash_guid(szProduct, squished_pc))
1081 return ERROR_INVALID_PARAMETER;
1083 if (!szSource || !*szSource)
1084 return ERROR_INVALID_PARAMETER;
1086 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
1087 return ERROR_INVALID_PARAMETER;
1089 if (dwOptions & MSICODE_PATCH)
1091 FIXME("Unhandled options MSICODE_PATCH\n");
1092 return ERROR_FUNCTION_FAILED;
1095 if (szUserSid && (dwContext & MSIINSTALLCONTEXT_MACHINE))
1096 return ERROR_INVALID_PARAMETER;
1098 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1099 if (rc != ERROR_SUCCESS)
1100 return rc;
1102 if (dwOptions & MSISOURCETYPE_NETWORK)
1103 rc = OpenNetworkSubkey(sourcekey, &typekey, TRUE);
1104 else if (dwOptions & MSISOURCETYPE_URL)
1105 rc = OpenURLSubkey(sourcekey, &typekey, TRUE);
1106 else if (dwOptions & MSISOURCETYPE_MEDIA)
1107 rc = OpenMediaSubkey(sourcekey, &typekey, TRUE);
1108 else
1110 ERR("unknown media type: %08x\n", dwOptions);
1111 RegCloseKey(sourcekey);
1112 return ERROR_FUNCTION_FAILED;
1115 postfix = (dwOptions & MSISOURCETYPE_NETWORK) ? backslash : forwardslash;
1116 if (szSource[lstrlenW(szSource) - 1] == *postfix)
1117 source = strdupW(szSource);
1118 else
1120 size = lstrlenW(szSource) + 2;
1121 source = msi_alloc(size * sizeof(WCHAR));
1122 lstrcpyW(source, szSource);
1123 lstrcatW(source, postfix);
1126 list_init(&sourcelist);
1127 rc = fill_source_list(&sourcelist, typekey, &count);
1128 if (rc != ERROR_NO_MORE_ITEMS)
1129 return rc;
1131 size = (lstrlenW(source) + 1) * sizeof(WCHAR);
1133 if (count == 0)
1135 rc = RegSetValueExW(typekey, one, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1136 goto done;
1138 else if (dwIndex > count || dwIndex == 0)
1140 sprintfW(name, fmt, count + 1);
1141 rc = RegSetValueExW(typekey, name, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1142 goto done;
1144 else
1146 sprintfW(name, fmt, dwIndex);
1147 info = msi_alloc(sizeof(media_info));
1148 if (!info)
1150 rc = ERROR_OUTOFMEMORY;
1151 goto done;
1154 info->path = strdupW(source);
1155 lstrcpyW(info->szIndex, name);
1156 info->index = dwIndex;
1157 add_source_to_list(&sourcelist, info, &index);
1159 LIST_FOR_EACH_ENTRY(info, &sourcelist, media_info, entry)
1161 if (info->index < index)
1162 continue;
1164 size = (lstrlenW(info->path) + 1) * sizeof(WCHAR);
1165 rc = RegSetValueExW(typekey, info->szIndex, 0,
1166 REG_EXPAND_SZ, (LPBYTE)info->path, size);
1167 if (rc != ERROR_SUCCESS)
1168 goto done;
1172 done:
1173 free_source_list(&sourcelist);
1174 msi_free(source);
1175 RegCloseKey(typekey);
1176 RegCloseKey(sourcekey);
1177 return rc;
1180 /******************************************************************
1181 * MsiSourceListAddMediaDiskA (MSI.@)
1183 UINT WINAPI MsiSourceListAddMediaDiskA(LPCSTR szProduct, LPCSTR szUserSid,
1184 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1185 LPCSTR szVolumeLabel, LPCSTR szDiskPrompt)
1187 UINT r;
1188 LPWSTR product = NULL;
1189 LPWSTR usersid = NULL;
1190 LPWSTR volume = NULL;
1191 LPWSTR prompt = NULL;
1193 if (szProduct) product = strdupAtoW(szProduct);
1194 if (szUserSid) usersid = strdupAtoW(szUserSid);
1195 if (szVolumeLabel) volume = strdupAtoW(szVolumeLabel);
1196 if (szDiskPrompt) prompt = strdupAtoW(szDiskPrompt);
1198 r = MsiSourceListAddMediaDiskW(product, usersid, dwContext, dwOptions,
1199 dwDiskId, volume, prompt);
1201 msi_free(product);
1202 msi_free(usersid);
1203 msi_free(volume);
1204 msi_free(prompt);
1206 return r;
1209 /******************************************************************
1210 * MsiSourceListAddMediaDiskW (MSI.@)
1212 UINT WINAPI MsiSourceListAddMediaDiskW(LPCWSTR szProduct, LPCWSTR szUserSid,
1213 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1214 LPCWSTR szVolumeLabel, LPCWSTR szDiskPrompt)
1216 HKEY sourcekey;
1217 HKEY mediakey;
1218 UINT rc;
1219 WCHAR szIndex[10];
1220 WCHAR squished_pc[GUID_SIZE];
1221 LPWSTR buffer;
1222 DWORD size;
1224 static const WCHAR fmt[] = {'%','i',0};
1225 static const WCHAR semicolon[] = {';',0};
1227 TRACE("%s %s %x %x %i %s %s\n", debugstr_w(szProduct),
1228 debugstr_w(szUserSid), dwContext, dwOptions, dwDiskId,
1229 debugstr_w(szVolumeLabel), debugstr_w(szDiskPrompt));
1231 if (!szProduct || !squash_guid(szProduct, squished_pc))
1232 return ERROR_INVALID_PARAMETER;
1234 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
1235 return ERROR_INVALID_PARAMETER;
1237 if ((szVolumeLabel && !*szVolumeLabel) || (szDiskPrompt && !*szDiskPrompt))
1238 return ERROR_INVALID_PARAMETER;
1240 if ((dwContext & MSIINSTALLCONTEXT_MACHINE) && szUserSid)
1241 return ERROR_INVALID_PARAMETER;
1243 if (dwOptions & MSICODE_PATCH)
1245 FIXME("Unhandled options MSICODE_PATCH\n");
1246 return ERROR_FUNCTION_FAILED;
1249 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1250 if (rc != ERROR_SUCCESS)
1251 return rc;
1253 OpenMediaSubkey(sourcekey, &mediakey, TRUE);
1255 sprintfW(szIndex, fmt, dwDiskId);
1257 size = 2;
1258 if (szVolumeLabel) size += lstrlenW(szVolumeLabel);
1259 if (szDiskPrompt) size += lstrlenW(szDiskPrompt);
1261 size *= sizeof(WCHAR);
1262 buffer = msi_alloc(size);
1263 *buffer = '\0';
1265 if (szVolumeLabel) lstrcpyW(buffer, szVolumeLabel);
1266 lstrcatW(buffer, semicolon);
1267 if (szDiskPrompt) lstrcatW(buffer, szDiskPrompt);
1269 RegSetValueExW(mediakey, szIndex, 0, REG_SZ, (LPBYTE)buffer, size);
1270 msi_free(buffer);
1272 RegCloseKey(sourcekey);
1273 RegCloseKey(mediakey);
1275 return ERROR_SUCCESS;
1278 /******************************************************************
1279 * MsiSourceListClearAllA (MSI.@)
1281 UINT WINAPI MsiSourceListClearAllA( LPCSTR szProduct, LPCSTR szUserName, DWORD dwReserved )
1283 FIXME("(%s %s %d)\n", debugstr_a(szProduct), debugstr_a(szUserName), dwReserved);
1284 return ERROR_SUCCESS;
1287 /******************************************************************
1288 * MsiSourceListClearAllW (MSI.@)
1290 UINT WINAPI MsiSourceListClearAllW( LPCWSTR szProduct, LPCWSTR szUserName, DWORD dwReserved )
1292 FIXME("(%s %s %d)\n", debugstr_w(szProduct), debugstr_w(szUserName), dwReserved);
1293 return ERROR_SUCCESS;