Added memory allocation inline functions (part 2).
[wine/wine-kai.git] / dlls / msi / appsearch.c
blobba6a9bd663ae4aa3ae5c2b10f4d9f5a8f0e2b772
1 /*
2 * Implementation of the AppSearch action of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Juan Lang
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <stdarg.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winreg.h"
27 #include "msi.h"
28 #include "msiquery.h"
29 #include "msidefs.h"
30 #include "winver.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
33 #include "msipriv.h"
34 #include "action.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38 typedef struct tagMSISIGNATURE
40 LPWSTR Name; /* NOT owned by this structure */
41 LPWSTR Property; /* NOT owned by this structure */
42 LPWSTR File;
43 DWORD MinVersionMS;
44 DWORD MinVersionLS;
45 DWORD MaxVersionMS;
46 DWORD MaxVersionLS;
47 DWORD MinSize;
48 DWORD MaxSize;
49 FILETIME MinTime;
50 FILETIME MaxTime;
51 LPWSTR Languages;
52 }MSISIGNATURE;
54 static void ACTION_VerStrToInteger(LPCWSTR verStr, PDWORD ms, PDWORD ls)
56 const WCHAR *ptr;
57 int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
59 x1 = atoiW(verStr);
60 ptr = strchrW(verStr, '.');
61 if (ptr)
63 x2 = atoiW(ptr + 1);
64 ptr = strchrW(ptr + 1, '.');
66 if (ptr)
68 x3 = atoiW(ptr + 1);
69 ptr = strchrW(ptr + 1, '.');
71 if (ptr)
72 x4 = atoiW(ptr + 1);
73 /* FIXME: byte-order dependent? */
74 *ms = x1 << 16 | x2;
75 *ls = x3 << 16 | x4;
78 /* Fills in sig with the the values from the Signature table, where name is the
79 * signature to find. Upon return, sig->File will be NULL if the record is not
80 * found, and not NULL if it is found.
81 * Warning: clears all fields in sig!
82 * Returns ERROR_SUCCESS upon success (where not finding the record counts as
83 * success), something else on error.
85 static UINT ACTION_AppSearchGetSignature(MSIPACKAGE *package, MSISIGNATURE *sig,
86 LPCWSTR name)
88 MSIQUERY *view;
89 UINT rc;
90 static const WCHAR ExecSeqQuery[] = {
91 's','e','l','e','c','t',' ','*',' ',
92 'f','r','o','m',' ',
93 'S','i','g','n','a','t','u','r','e',' ',
94 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e',' ','=',' ',
95 '\'','%','s','\'',0};
97 TRACE("(package %p, sig %p)\n", package, sig);
98 memset(sig, 0, sizeof(*sig));
99 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, name);
100 if (rc == ERROR_SUCCESS)
102 MSIRECORD *row = 0;
103 DWORD time;
104 WCHAR *minVersion, *maxVersion;
106 rc = MSI_ViewExecute(view, 0);
107 if (rc != ERROR_SUCCESS)
109 TRACE("MSI_ViewExecute returned %d\n", rc);
110 goto end;
112 rc = MSI_ViewFetch(view,&row);
113 if (rc != ERROR_SUCCESS)
115 TRACE("MSI_ViewFetch returned %d\n", rc);
116 rc = ERROR_SUCCESS;
117 goto end;
120 /* get properties */
121 sig->File = load_dynamic_stringW(row,2);
122 minVersion = load_dynamic_stringW(row,3);
123 if (minVersion)
125 ACTION_VerStrToInteger(minVersion, &sig->MinVersionMS,
126 &sig->MinVersionLS);
127 msi_free( minVersion);
129 maxVersion = load_dynamic_stringW(row,4);
130 if (maxVersion)
132 ACTION_VerStrToInteger(maxVersion, &sig->MaxVersionMS,
133 &sig->MaxVersionLS);
134 msi_free( maxVersion);
136 sig->MinSize = MSI_RecordGetInteger(row,5);
137 if (sig->MinSize == MSI_NULL_INTEGER)
138 sig->MinSize = 0;
139 sig->MaxSize = MSI_RecordGetInteger(row,6);
140 if (sig->MaxSize == MSI_NULL_INTEGER)
141 sig->MaxSize = 0;
142 sig->Languages = load_dynamic_stringW(row,9);
143 time = MSI_RecordGetInteger(row,7);
144 if (time != MSI_NULL_INTEGER)
145 DosDateTimeToFileTime(HIWORD(time), LOWORD(time), &sig->MinTime);
146 time = MSI_RecordGetInteger(row,8);
147 if (time != MSI_NULL_INTEGER)
148 DosDateTimeToFileTime(HIWORD(time), LOWORD(time), &sig->MaxTime);
149 TRACE("Found file name %s for Signature_ %s;\n",
150 debugstr_w(sig->File), debugstr_w(name));
151 TRACE("MinVersion is %d.%d.%d.%d\n", HIWORD(sig->MinVersionMS),
152 LOWORD(sig->MinVersionMS), HIWORD(sig->MinVersionLS),
153 LOWORD(sig->MinVersionLS));
154 TRACE("MaxVersion is %d.%d.%d.%d\n", HIWORD(sig->MaxVersionMS),
155 LOWORD(sig->MaxVersionMS), HIWORD(sig->MaxVersionLS),
156 LOWORD(sig->MaxVersionLS));
157 TRACE("MinSize is %ld, MaxSize is %ld;\n", sig->MinSize, sig->MaxSize);
158 TRACE("Languages is %s\n", debugstr_w(sig->Languages));
160 end:
161 msiobj_release(&row->hdr);
162 MSI_ViewClose(view);
163 msiobj_release(&view->hdr);
165 else
167 TRACE("MSI_OpenQuery returned %d\n", rc);
168 rc = ERROR_SUCCESS;
171 TRACE("returning %d\n", rc);
172 return rc;
175 static UINT ACTION_AppSearchComponents(MSIPACKAGE *package, BOOL *appFound,
176 MSISIGNATURE *sig)
178 MSIQUERY *view;
179 UINT rc;
180 static const WCHAR ExecSeqQuery[] = {
181 's','e','l','e','c','t',' ','*',' ',
182 'f','r','o','m',' ',
183 'C','o','m','p','L','o','c','a','t','o','r',' ',
184 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e','_',' ','=',' ',
185 '\'','%','s','\'',0};
187 TRACE("(package %p, appFound %p, sig %p)\n", package, appFound, sig);
188 *appFound = FALSE;
189 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, sig->Name);
190 if (rc == ERROR_SUCCESS)
192 MSIRECORD *row = 0;
193 WCHAR guid[50];
194 DWORD sz;
196 rc = MSI_ViewExecute(view, 0);
197 if (rc != ERROR_SUCCESS)
199 TRACE("MSI_ViewExecute returned %d\n", rc);
200 goto end;
202 rc = MSI_ViewFetch(view,&row);
203 if (rc != ERROR_SUCCESS)
205 TRACE("MSI_ViewFetch returned %d\n", rc);
206 rc = ERROR_SUCCESS;
207 goto end;
210 /* get GUID */
211 guid[0] = 0;
212 sz=sizeof(guid)/sizeof(guid[0]);
213 rc = MSI_RecordGetStringW(row,2,guid,&sz);
214 if (rc != ERROR_SUCCESS)
216 ERR("Error is %x\n",rc);
217 goto end;
219 FIXME("AppSearch unimplemented for CompLocator table (GUID %s)\n",
220 debugstr_w(guid));
222 end:
223 msiobj_release(&row->hdr);
224 MSI_ViewClose(view);
225 msiobj_release(&view->hdr);
227 else
229 TRACE("MSI_OpenQuery returned %d\n", rc);
230 rc = ERROR_SUCCESS;
233 TRACE("returning %d\n", rc);
234 return rc;
237 static UINT ACTION_AppSearchReg(MSIPACKAGE *package, BOOL *appFound,
238 MSISIGNATURE *sig)
240 MSIQUERY *view;
241 UINT rc;
242 static const WCHAR ExecSeqQuery[] = {
243 's','e','l','e','c','t',' ','*',' ',
244 'f','r','o','m',' ',
245 'R','e','g','L','o','c','a','t','o','r',' ',
246 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e','_',' ','=',' ',
247 '\'','%','s','\'',0};
248 static const WCHAR dwordFmt[] = { '#','%','d','\0' };
249 static const WCHAR expandSzFmt[] = { '#','%','%','%','s','\0' };
250 static const WCHAR binFmt[] = { '#','x','%','x','\0' };
252 TRACE("(package %p, appFound %p, sig %p)\n", package, appFound, sig);
253 *appFound = FALSE;
254 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, sig->Name);
255 if (rc == ERROR_SUCCESS)
257 MSIRECORD *row = 0;
258 LPWSTR keyPath = NULL, valueName = NULL, propertyValue = NULL;
259 int root, type;
260 HKEY rootKey, key = NULL;
261 DWORD sz = 0, regType, i;
262 LPBYTE value = NULL;
264 rc = MSI_ViewExecute(view, 0);
265 if (rc != ERROR_SUCCESS)
267 TRACE("MSI_ViewExecute returned %d\n", rc);
268 goto end;
270 rc = MSI_ViewFetch(view,&row);
271 if (rc != ERROR_SUCCESS)
273 TRACE("MSI_ViewFetch returned %d\n", rc);
274 rc = ERROR_SUCCESS;
275 goto end;
278 root = MSI_RecordGetInteger(row,2);
279 keyPath = load_dynamic_stringW(row,3);
280 /* FIXME: keyPath needs to be expanded for properties */
281 valueName = load_dynamic_stringW(row,4);
282 /* FIXME: valueName probably does too */
283 type = MSI_RecordGetInteger(row,5);
285 if ((type & 0x0f) != msidbLocatorTypeRawValue)
287 FIXME("AppSearch unimplemented for type %d (key path %s, value %s)\n",
288 type, debugstr_w(keyPath), debugstr_w(valueName));
289 goto end;
292 switch (root)
294 case msidbRegistryRootClassesRoot:
295 rootKey = HKEY_CLASSES_ROOT;
296 break;
297 case msidbRegistryRootCurrentUser:
298 rootKey = HKEY_CURRENT_USER;
299 break;
300 case msidbRegistryRootLocalMachine:
301 rootKey = HKEY_LOCAL_MACHINE;
302 break;
303 case msidbRegistryRootUsers:
304 rootKey = HKEY_USERS;
305 break;
306 default:
307 WARN("Unknown root key %d\n", root);
308 goto end;
311 rc = RegCreateKeyW(rootKey, keyPath, &key);
312 if (rc)
314 TRACE("RegCreateKeyW returned %d\n", rc);
315 rc = ERROR_SUCCESS;
316 goto end;
318 rc = RegQueryValueExW(key, valueName, NULL, NULL, NULL, &sz);
319 if (rc)
321 TRACE("RegQueryValueExW returned %d\n", rc);
322 rc = ERROR_SUCCESS;
323 goto end;
325 /* FIXME: sanity-check sz before allocating (is there an upper-limit
326 * on the value of a property?)
328 value = msi_alloc( sz);
329 rc = RegQueryValueExW(key, valueName, NULL, &regType, value, &sz);
330 if (rc)
332 TRACE("RegQueryValueExW returned %d\n", rc);
333 rc = ERROR_SUCCESS;
334 goto end;
337 /* bail out if the registry key is empty */
338 if (sz == 0)
340 rc = ERROR_SUCCESS;
341 goto end;
344 switch (regType)
346 case REG_SZ:
347 if (*(LPWSTR)value == '#')
349 /* escape leading pound with another */
350 propertyValue = msi_alloc( sz + sizeof(WCHAR));
351 propertyValue[0] = '#';
352 strcpyW(propertyValue + 1, (LPWSTR)value);
354 else
356 propertyValue = msi_alloc( sz);
357 strcpyW(propertyValue, (LPWSTR)value);
359 break;
360 case REG_DWORD:
361 /* 7 chars for digits, 1 for NULL, 1 for #, and 1 for sign
362 * char if needed
364 propertyValue = msi_alloc( 10 * sizeof(WCHAR));
365 sprintfW(propertyValue, dwordFmt, *(DWORD *)value);
366 break;
367 case REG_EXPAND_SZ:
368 /* space for extra #% characters in front */
369 propertyValue = msi_alloc( sz + 2 * sizeof(WCHAR));
370 sprintfW(propertyValue, expandSzFmt, (LPWSTR)value);
371 break;
372 case REG_BINARY:
373 /* 3 == length of "#x<nibble>" */
374 propertyValue = msi_alloc( (sz * 3 + 1) * sizeof(WCHAR));
375 for (i = 0; i < sz; i++)
376 sprintfW(propertyValue + i * 3, binFmt, value[i]);
377 break;
378 default:
379 WARN("unimplemented for values of type %ld\n", regType);
380 goto end;
383 TRACE("found registry value, setting %s to %s\n",
384 debugstr_w(sig->Property), debugstr_w(propertyValue));
385 rc = MSI_SetPropertyW(package, sig->Property, propertyValue);
386 *appFound = TRUE;
388 end:
389 msi_free( propertyValue);
390 msi_free( value);
391 RegCloseKey(key);
393 msi_free( keyPath);
394 msi_free( valueName);
396 msiobj_release(&row->hdr);
397 MSI_ViewClose(view);
398 msiobj_release(&view->hdr);
400 else
402 TRACE("MSI_OpenQuery returned %d\n", rc);
403 rc = ERROR_SUCCESS;
406 TRACE("returning %d\n", rc);
407 return rc;
410 static UINT ACTION_AppSearchIni(MSIPACKAGE *package, BOOL *appFound,
411 MSISIGNATURE *sig)
413 MSIQUERY *view;
414 UINT rc;
415 static const WCHAR ExecSeqQuery[] = {
416 's','e','l','e','c','t',' ','*',' ',
417 'f','r','o','m',' ',
418 'I','n','i','L','o','c','a','t','o','r',' ',
419 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e','_',' ','=',' ',
420 '\'','%','s','\'',0};
422 TRACE("(package %p, appFound %p, sig %p)\n", package, appFound, sig);
423 *appFound = FALSE;
424 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, sig->Name);
425 if (rc == ERROR_SUCCESS)
427 MSIRECORD *row = 0;
428 LPWSTR fileName;
430 rc = MSI_ViewExecute(view, 0);
431 if (rc != ERROR_SUCCESS)
433 TRACE("MSI_ViewExecute returned %d\n", rc);
434 goto end;
436 rc = MSI_ViewFetch(view,&row);
437 if (rc != ERROR_SUCCESS)
439 TRACE("MSI_ViewFetch returned %d\n", rc);
440 rc = ERROR_SUCCESS;
441 goto end;
444 /* get file name */
445 fileName = load_dynamic_stringW(row,2);
446 FIXME("AppSearch unimplemented for IniLocator (ini file name %s)\n",
447 debugstr_w(fileName));
448 msi_free( fileName);
450 end:
451 msiobj_release(&row->hdr);
452 MSI_ViewClose(view);
453 msiobj_release(&view->hdr);
455 else
457 TRACE("MSI_OpenQuery returned %d\n", rc);
458 rc = ERROR_SUCCESS;
462 TRACE("returning %d\n", rc);
463 return rc;
466 /* Expands the value in src into a path without property names and only
467 * containing long path names into dst. Replaces at most len characters of dst,
468 * and always NULL-terminates dst if dst is not NULL and len >= 1.
469 * May modify src.
470 * Assumes src and dst are non-overlapping.
471 * FIXME: return code probably needed:
472 * - what does AppSearch return if the table values are invalid?
473 * - what if dst is too small?
475 static void ACTION_ExpandAnyPath(MSIPACKAGE *package, WCHAR *src, WCHAR *dst,
476 size_t len)
478 WCHAR *ptr;
479 size_t copied = 0;
481 if (!src || !dst || !len)
482 return;
484 /* Ignore the short portion of the path, don't think we can use it anyway */
485 if ((ptr = strchrW(src, '|')))
486 ptr++;
487 else
488 ptr = src;
489 while (*ptr && copied < len - 1)
491 WCHAR *prop = strchrW(ptr, '[');
493 if (prop)
495 WCHAR *propEnd = strchrW(prop + 1, ']');
497 if (!propEnd)
499 WARN("Unterminated property name in AnyPath: %s\n",
500 debugstr_w(prop));
501 break;
503 else
505 DWORD propLen;
507 *propEnd = 0;
508 propLen = len - copied - 1;
509 MSI_GetPropertyW(package, prop + 1, dst + copied, &propLen);
510 ptr = propEnd + 1;
511 copied += propLen;
514 else
516 size_t toCopy = min(strlenW(ptr) + 1, len - copied - 1);
518 memcpy(dst + copied, ptr, toCopy * sizeof(WCHAR));
519 ptr += toCopy;
520 copied += toCopy;
523 *(dst + copied) = '\0';
526 /* Sets *matches to whether the file (whose path is filePath) matches the
527 * versions set in sig.
528 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
529 * something else if an install-halting error occurs.
531 static UINT ACTION_FileVersionMatches(MSISIGNATURE *sig, LPCWSTR filePath,
532 BOOL *matches)
534 UINT rc = ERROR_SUCCESS;
536 *matches = FALSE;
537 if (sig->Languages)
539 FIXME(": need to check version for languages %s\n",
540 debugstr_w(sig->Languages));
542 else
544 DWORD zero, size = GetFileVersionInfoSizeW(filePath, &zero);
546 if (size)
548 LPVOID buf = msi_alloc( size);
550 if (buf)
552 static const WCHAR rootW[] = { '\\',0 };
553 UINT versionLen;
554 LPVOID subBlock = NULL;
556 if (GetFileVersionInfoW(filePath, 0, size, buf))
557 VerQueryValueW(buf, rootW, &subBlock, &versionLen);
558 if (subBlock)
560 VS_FIXEDFILEINFO *info =
561 (VS_FIXEDFILEINFO *)subBlock;
563 TRACE("Comparing file version %d.%d.%d.%d:\n",
564 HIWORD(info->dwFileVersionMS),
565 LOWORD(info->dwFileVersionMS),
566 HIWORD(info->dwFileVersionLS),
567 LOWORD(info->dwFileVersionLS));
568 if (info->dwFileVersionMS < sig->MinVersionMS
569 || (info->dwFileVersionMS == sig->MinVersionMS &&
570 info->dwFileVersionLS < sig->MinVersionLS))
572 TRACE("Less than minimum version %d.%d.%d.%d\n",
573 HIWORD(sig->MinVersionMS),
574 LOWORD(sig->MinVersionMS),
575 HIWORD(sig->MinVersionLS),
576 LOWORD(sig->MinVersionLS));
578 else if (info->dwFileVersionMS < sig->MinVersionMS
579 || (info->dwFileVersionMS == sig->MinVersionMS &&
580 info->dwFileVersionLS < sig->MinVersionLS))
582 TRACE("Greater than minimum version %d.%d.%d.%d\n",
583 HIWORD(sig->MaxVersionMS),
584 LOWORD(sig->MaxVersionMS),
585 HIWORD(sig->MaxVersionLS),
586 LOWORD(sig->MaxVersionLS));
588 else
589 *matches = TRUE;
591 msi_free( buf);
593 else
594 rc = ERROR_OUTOFMEMORY;
597 return rc;
600 /* Sets *matches to whether the file in findData matches that in sig.
601 * fullFilePath is assumed to be the full path of the file specified in
602 * findData, which may be necessary to compare the version.
603 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
604 * something else if an install-halting error occurs.
606 static UINT ACTION_FileMatchesSig(MSISIGNATURE *sig,
607 LPWIN32_FIND_DATAW findData, LPCWSTR fullFilePath, BOOL *matches)
609 UINT rc = ERROR_SUCCESS;
611 *matches = TRUE;
612 /* assumes the caller has already ensured the filenames match, so check
613 * the other fields..
615 if (sig->MinTime.dwLowDateTime || sig->MinTime.dwHighDateTime)
617 if (findData->ftCreationTime.dwHighDateTime <
618 sig->MinTime.dwHighDateTime ||
619 (findData->ftCreationTime.dwHighDateTime == sig->MinTime.dwHighDateTime
620 && findData->ftCreationTime.dwLowDateTime <
621 sig->MinTime.dwLowDateTime))
622 *matches = FALSE;
624 if (*matches && (sig->MaxTime.dwLowDateTime || sig->MaxTime.dwHighDateTime))
626 if (findData->ftCreationTime.dwHighDateTime >
627 sig->MaxTime.dwHighDateTime ||
628 (findData->ftCreationTime.dwHighDateTime == sig->MaxTime.dwHighDateTime
629 && findData->ftCreationTime.dwLowDateTime >
630 sig->MaxTime.dwLowDateTime))
631 *matches = FALSE;
633 if (*matches && sig->MinSize && findData->nFileSizeLow < sig->MinSize)
634 *matches = FALSE;
635 if (*matches && sig->MaxSize && findData->nFileSizeLow > sig->MaxSize)
636 *matches = FALSE;
637 if (*matches && (sig->MinVersionMS || sig->MinVersionLS ||
638 sig->MaxVersionMS || sig->MaxVersionLS))
639 rc = ACTION_FileVersionMatches(sig, fullFilePath, matches);
640 return rc;
643 /* Recursively searches the directory dir for files that match the signature
644 * sig, up to (depth + 1) levels deep. That is, if depth is 0, it searches dir
645 * (and only dir). If depth is 1, searches dir and its immediate
646 * subdirectories.
647 * Assumes sig->File is not NULL.
648 * Returns ERROR_SUCCESS on success (which may include non-critical errors),
649 * something else on failures which should halt the install.
651 static UINT ACTION_RecurseSearchDirectory(MSIPACKAGE *package, BOOL *appFound,
652 MSISIGNATURE *sig, LPCWSTR dir, int depth)
654 static const WCHAR starDotStarW[] = { '*','.','*',0 };
655 UINT rc = ERROR_SUCCESS;
656 size_t dirLen = lstrlenW(dir), fileLen = lstrlenW(sig->File);
657 WCHAR *buf;
659 TRACE("Searching directory %s for file %s, depth %d\n", debugstr_w(dir),
660 debugstr_w(sig->File), depth);
662 if (depth < 0)
663 return ERROR_INVALID_PARAMETER;
665 *appFound = FALSE;
666 /* We need the buffer in both paths below, so go ahead and allocate it
667 * here. Add two because we might need to add a backslash if the dir name
668 * isn't backslash-terminated.
670 buf = msi_alloc( (dirLen + max(fileLen, lstrlenW(starDotStarW)) + 2) * sizeof(WCHAR));
671 if (buf)
673 /* a depth of 0 implies we should search dir, so go ahead and search */
674 HANDLE hFind;
675 WIN32_FIND_DATAW findData;
677 memcpy(buf, dir, dirLen * sizeof(WCHAR));
678 if (buf[dirLen - 1] != '\\')
679 buf[dirLen++ - 1] = '\\';
680 memcpy(buf + dirLen, sig->File, (fileLen + 1) * sizeof(WCHAR));
681 hFind = FindFirstFileW(buf, &findData);
682 if (hFind != INVALID_HANDLE_VALUE)
684 BOOL matches;
686 /* assuming Signature can't contain wildcards for the file name,
687 * so don't bother with FindNextFileW here.
689 if (!(rc = ACTION_FileMatchesSig(sig, &findData, buf, &matches))
690 && matches)
692 TRACE("found file, setting %s to %s\n",
693 debugstr_w(sig->Property), debugstr_w(buf));
694 rc = MSI_SetPropertyW(package, sig->Property, buf);
695 *appFound = TRUE;
697 FindClose(hFind);
699 if (rc == ERROR_SUCCESS && !*appFound && depth > 0)
701 HANDLE hFind;
702 WIN32_FIND_DATAW findData;
704 memcpy(buf, dir, dirLen * sizeof(WCHAR));
705 if (buf[dirLen - 1] != '\\')
706 buf[dirLen++ - 1] = '\\';
707 lstrcpyW(buf + dirLen, starDotStarW);
708 hFind = FindFirstFileW(buf, &findData);
709 if (hFind != INVALID_HANDLE_VALUE)
711 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
712 rc = ACTION_RecurseSearchDirectory(package, appFound,
713 sig, findData.cFileName, depth - 1);
714 while (rc == ERROR_SUCCESS && !*appFound &&
715 FindNextFileW(hFind, &findData) != 0)
717 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
718 rc = ACTION_RecurseSearchDirectory(package, appFound,
719 sig, findData.cFileName, depth - 1);
721 FindClose(hFind);
724 msi_free(buf);
726 else
727 rc = ERROR_OUTOFMEMORY;
729 return rc;
732 static UINT ACTION_CheckDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
733 LPCWSTR dir)
735 UINT rc = ERROR_SUCCESS;
737 if (GetFileAttributesW(dir) & FILE_ATTRIBUTE_DIRECTORY)
739 TRACE("directory exists, setting %s to %s\n",
740 debugstr_w(sig->Property), debugstr_w(dir));
741 rc = MSI_SetPropertyW(package, sig->Property, dir);
743 return rc;
746 static BOOL ACTION_IsFullPath(LPCWSTR path)
748 WCHAR first = toupperW(path[0]);
749 BOOL ret;
751 if (first >= 'A' && first <= 'Z' && path[1] == ':')
752 ret = TRUE;
753 else if (path[0] == '\\' && path[1] == '\\')
754 ret = TRUE;
755 else
756 ret = FALSE;
757 return ret;
760 static UINT ACTION_SearchDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
761 LPCWSTR expanded, int depth)
763 UINT rc;
764 BOOL found;
766 TRACE("%p, %p, %s, %d\n", package, sig, debugstr_w(expanded), depth);
767 if (ACTION_IsFullPath(expanded))
769 if (sig->File)
770 rc = ACTION_RecurseSearchDirectory(package, &found, sig,
771 expanded, depth);
772 else
774 /* Recursively searching a directory makes no sense when the
775 * directory to search is the thing you're trying to find.
777 rc = ACTION_CheckDirectory(package, sig, expanded);
780 else
782 WCHAR pathWithDrive[MAX_PATH] = { 'C',':','\\',0 };
783 DWORD drives = GetLogicalDrives();
784 int i;
786 rc = ERROR_SUCCESS;
787 found = FALSE;
788 for (i = 0; rc == ERROR_SUCCESS && !found && i < 26; i++)
789 if (drives & (1 << drives))
791 pathWithDrive[0] = 'A' + i;
792 if (GetDriveTypeW(pathWithDrive) == DRIVE_FIXED)
794 lstrcpynW(pathWithDrive + 3, expanded,
795 sizeof(pathWithDrive) / sizeof(pathWithDrive[0]) - 3);
796 if (sig->File)
797 rc = ACTION_RecurseSearchDirectory(package, &found, sig,
798 pathWithDrive, depth);
799 else
800 rc = ACTION_CheckDirectory(package, sig, pathWithDrive);
804 TRACE("returning %d\n", rc);
805 return rc;
808 static UINT ACTION_AppSearchDr(MSIPACKAGE *package, MSISIGNATURE *sig)
810 MSIQUERY *view;
811 UINT rc;
812 static const WCHAR ExecSeqQuery[] = {
813 's','e','l','e','c','t',' ','*',' ',
814 'f','r','o','m',' ',
815 'D','r','L','o','c','a','t','o','r',' ',
816 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e','_',' ','=',' ',
817 '\'','%','s','\'',0};
819 TRACE("(package %p, sig %p)\n", package, sig);
820 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, sig->Name);
821 if (rc == ERROR_SUCCESS)
823 MSIRECORD *row = 0;
824 WCHAR buffer[MAX_PATH], expanded[MAX_PATH];
825 DWORD sz;
826 int depth;
828 rc = MSI_ViewExecute(view, 0);
829 if (rc != ERROR_SUCCESS)
831 TRACE("MSI_ViewExecute returned %d\n", rc);
832 goto end;
834 rc = MSI_ViewFetch(view,&row);
835 if (rc != ERROR_SUCCESS)
837 TRACE("MSI_ViewFetch returned %d\n", rc);
838 rc = ERROR_SUCCESS;
839 goto end;
842 /* check whether parent is set */
843 buffer[0] = 0;
844 sz=sizeof(buffer)/sizeof(buffer[0]);
845 rc = MSI_RecordGetStringW(row,2,buffer,&sz);
846 if (rc != ERROR_SUCCESS)
848 ERR("Error is %x\n",rc);
849 goto end;
851 else if (buffer[0])
853 FIXME(": searching parent (%s) unimplemented\n",
854 debugstr_w(buffer));
855 goto end;
857 /* no parent, now look for path */
858 buffer[0] = 0;
859 sz=sizeof(buffer)/sizeof(buffer[0]);
860 rc = MSI_RecordGetStringW(row,3,buffer,&sz);
861 if (rc != ERROR_SUCCESS)
863 ERR("Error is %x\n",rc);
864 goto end;
866 if (MSI_RecordIsNull(row,4))
867 depth = 0;
868 else
869 depth = MSI_RecordGetInteger(row,4);
870 ACTION_ExpandAnyPath(package, buffer, expanded,
871 sizeof(expanded) / sizeof(expanded[0]));
872 rc = ACTION_SearchDirectory(package, sig, expanded, depth);
874 end:
875 msiobj_release(&row->hdr);
876 MSI_ViewClose(view);
877 msiobj_release(&view->hdr);
879 else
881 TRACE("MSI_OpenQuery returned %d\n", rc);
882 rc = ERROR_SUCCESS;
886 TRACE("returning %d\n", rc);
887 return rc;
890 /* http://msdn.microsoft.com/library/en-us/msi/setup/appsearch_table.asp
891 * is the best reference for the AppSearch table and how it's used.
893 UINT ACTION_AppSearch(MSIPACKAGE *package)
895 MSIQUERY *view;
896 UINT rc;
897 static const WCHAR ExecSeqQuery[] = {
898 's','e','l','e','c','t',' ','*',' ',
899 'f','r','o','m',' ',
900 'A','p','p','S','e','a','r','c','h',0};
902 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery);
903 if (rc == ERROR_SUCCESS)
905 MSIRECORD *row = 0;
906 WCHAR propBuf[0x100], sigBuf[0x100];
907 DWORD sz;
908 MSISIGNATURE sig;
909 BOOL appFound = FALSE;
911 rc = MSI_ViewExecute(view, 0);
912 if (rc != ERROR_SUCCESS)
913 goto end;
915 while (!rc)
917 rc = MSI_ViewFetch(view,&row);
918 if (rc != ERROR_SUCCESS)
920 rc = ERROR_SUCCESS;
921 break;
924 /* get property and signature */
925 propBuf[0] = 0;
926 sz=sizeof(propBuf)/sizeof(propBuf[0]);
927 rc = MSI_RecordGetStringW(row,1,propBuf,&sz);
928 if (rc != ERROR_SUCCESS)
930 ERR("Error is %x\n",rc);
931 msiobj_release(&row->hdr);
932 break;
934 sigBuf[0] = 0;
935 sz=sizeof(sigBuf)/sizeof(sigBuf[0]);
936 rc = MSI_RecordGetStringW(row,2,sigBuf,&sz);
937 if (rc != ERROR_SUCCESS)
939 ERR("Error is %x\n",rc);
940 msiobj_release(&row->hdr);
941 break;
943 TRACE("Searching for Property %s, Signature_ %s\n",
944 debugstr_w(propBuf), debugstr_w(sigBuf));
945 /* This clears all the fields, so set Name and Property afterward */
946 rc = ACTION_AppSearchGetSignature(package, &sig, sigBuf);
947 sig.Name = sigBuf;
948 sig.Property = propBuf;
949 if (rc == ERROR_SUCCESS)
951 rc = ACTION_AppSearchComponents(package, &appFound, &sig);
952 if (rc == ERROR_SUCCESS && !appFound)
954 rc = ACTION_AppSearchReg(package, &appFound, &sig);
955 if (rc == ERROR_SUCCESS && !appFound)
957 rc = ACTION_AppSearchIni(package, &appFound, &sig);
958 if (rc == ERROR_SUCCESS && !appFound)
959 rc = ACTION_AppSearchDr(package, &sig);
963 msi_free( sig.File);
964 msi_free( sig.Languages);
965 msiobj_release(&row->hdr);
968 end:
969 MSI_ViewClose(view);
970 msiobj_release(&view->hdr);
972 else
973 rc = ERROR_SUCCESS;
975 return rc;