msi: Use msi_alloc/free, not HeapAlloc/Free.
[wine.git] / dlls / msi / appsearch.c
blobb47ecec6848a61a4b08adb78816ccbe83fb2e88b
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 LPCWSTR Name; /* NOT owned by this structure */
41 LPWSTR File;
42 DWORD MinVersionMS;
43 DWORD MinVersionLS;
44 DWORD MaxVersionMS;
45 DWORD MaxVersionLS;
46 DWORD MinSize;
47 DWORD MaxSize;
48 FILETIME MinTime;
49 FILETIME MaxTime;
50 LPWSTR Languages;
51 }MSISIGNATURE;
53 static void ACTION_VerStrToInteger(LPCWSTR verStr, PDWORD ms, PDWORD ls)
55 const WCHAR *ptr;
56 int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
58 x1 = atoiW(verStr);
59 ptr = strchrW(verStr, '.');
60 if (ptr)
62 x2 = atoiW(ptr + 1);
63 ptr = strchrW(ptr + 1, '.');
65 if (ptr)
67 x3 = atoiW(ptr + 1);
68 ptr = strchrW(ptr + 1, '.');
70 if (ptr)
71 x4 = atoiW(ptr + 1);
72 /* FIXME: byte-order dependent? */
73 *ms = x1 << 16 | x2;
74 *ls = x3 << 16 | x4;
77 /* Fills in sig with the the values from the Signature table, where name is the
78 * signature to find. Upon return, sig->File will be NULL if the record is not
79 * found, and not NULL if it is found.
80 * Warning: clears all fields in sig!
81 * Returns ERROR_SUCCESS upon success (where not finding the record counts as
82 * success), something else on error.
84 static UINT ACTION_AppSearchGetSignature(MSIPACKAGE *package, MSISIGNATURE *sig,
85 LPCWSTR name)
87 MSIQUERY *view;
88 UINT rc;
89 static const WCHAR ExecSeqQuery[] = {
90 's','e','l','e','c','t',' ','*',' ',
91 'f','r','o','m',' ',
92 'S','i','g','n','a','t','u','r','e',' ',
93 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e',' ','=',' ',
94 '\'','%','s','\'',0};
96 TRACE("(package %p, sig %p)\n", package, sig);
97 memset(sig, 0, sizeof(*sig));
98 sig->Name = name;
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 = msi_dup_record_field(row,2);
122 minVersion = msi_dup_record_field(row,3);
123 if (minVersion)
125 ACTION_VerStrToInteger(minVersion, &sig->MinVersionMS,
126 &sig->MinVersionLS);
127 msi_free( minVersion);
129 maxVersion = msi_dup_record_field(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 = msi_dup_record_field(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 if (row)
162 msiobj_release(&row->hdr);
163 MSI_ViewClose(view);
164 msiobj_release(&view->hdr);
166 else
168 TRACE("MSI_OpenQuery returned %d\n", rc);
169 rc = ERROR_SUCCESS;
172 TRACE("returning %d\n", rc);
173 return rc;
176 /* Frees any memory allocated in sig */
177 static void ACTION_FreeSignature(MSISIGNATURE *sig)
179 msi_free(sig->File);
180 msi_free(sig->Languages);
183 static UINT ACTION_AppSearchComponents(MSIPACKAGE *package, LPWSTR *appValue,
184 MSISIGNATURE *sig)
186 MSIQUERY *view;
187 UINT rc;
188 static const WCHAR ExecSeqQuery[] = {
189 's','e','l','e','c','t',' ','*',' ',
190 'f','r','o','m',' ',
191 'C','o','m','p','L','o','c','a','t','o','r',' ',
192 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e','_',' ','=',' ',
193 '\'','%','s','\'',0};
195 TRACE("(package %p, appValue %p, sig %p)\n", package, appValue, sig);
196 *appValue = NULL;
197 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, sig->Name);
198 if (rc == ERROR_SUCCESS)
200 MSIRECORD *row = 0;
201 WCHAR guid[50];
202 DWORD sz;
204 rc = MSI_ViewExecute(view, 0);
205 if (rc != ERROR_SUCCESS)
207 TRACE("MSI_ViewExecute returned %d\n", rc);
208 goto end;
210 rc = MSI_ViewFetch(view,&row);
211 if (rc != ERROR_SUCCESS)
213 TRACE("MSI_ViewFetch returned %d\n", rc);
214 rc = ERROR_SUCCESS;
215 goto end;
218 /* get GUID */
219 guid[0] = 0;
220 sz=sizeof(guid)/sizeof(guid[0]);
221 rc = MSI_RecordGetStringW(row,2,guid,&sz);
222 if (rc != ERROR_SUCCESS)
224 ERR("Error is %x\n",rc);
225 goto end;
227 FIXME("AppSearch unimplemented for CompLocator table (GUID %s)\n",
228 debugstr_w(guid));
230 end:
231 if (row)
232 msiobj_release(&row->hdr);
233 MSI_ViewClose(view);
234 msiobj_release(&view->hdr);
236 else
238 TRACE("MSI_OpenQuery returned %d\n", rc);
239 rc = ERROR_SUCCESS;
242 TRACE("returning %d\n", rc);
243 return rc;
246 static void ACTION_ConvertRegValue(DWORD regType, const BYTE *value, DWORD sz,
247 LPWSTR *appValue)
249 static const WCHAR dwordFmt[] = { '#','%','d','\0' };
250 static const WCHAR expandSzFmt[] = { '#','%','%','%','s','\0' };
251 static const WCHAR binFmt[] = { '#','x','%','x','\0' };
252 DWORD i;
254 switch (regType)
256 case REG_SZ:
257 if (*(LPWSTR)value == '#')
259 /* escape leading pound with another */
260 *appValue = msi_alloc(sz + sizeof(WCHAR));
261 (*appValue)[0] = '#';
262 strcpyW(*appValue + 1, (LPCWSTR)value);
264 else
266 *appValue = msi_alloc(sz);
267 strcpyW(*appValue, (LPCWSTR)value);
269 break;
270 case REG_DWORD:
271 /* 7 chars for digits, 1 for NULL, 1 for #, and 1 for sign
272 * char if needed
274 *appValue = msi_alloc(10 * sizeof(WCHAR));
275 sprintfW(*appValue, dwordFmt, *(const DWORD *)value);
276 break;
277 case REG_EXPAND_SZ:
278 /* space for extra #% characters in front */
279 *appValue = msi_alloc(sz + 2 * sizeof(WCHAR));
280 sprintfW(*appValue, expandSzFmt, (LPCWSTR)value);
281 break;
282 case REG_BINARY:
283 /* 3 == length of "#x<nibble>" */
284 *appValue = msi_alloc((sz * 3 + 1) * sizeof(WCHAR));
285 for (i = 0; i < sz; i++)
286 sprintfW(*appValue + i * 3, binFmt, value[i]);
287 break;
288 default:
289 WARN("unimplemented for values of type %ld\n", regType);
290 *appValue = NULL;
294 static UINT ACTION_SearchDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
295 LPCWSTR path, int depth, LPWSTR *appValue);
297 static UINT ACTION_AppSearchReg(MSIPACKAGE *package, LPWSTR *appValue,
298 MSISIGNATURE *sig)
300 MSIQUERY *view;
301 UINT rc;
302 static const WCHAR ExecSeqQuery[] = {
303 's','e','l','e','c','t',' ','*',' ',
304 'f','r','o','m',' ',
305 'R','e','g','L','o','c','a','t','o','r',' ',
306 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e','_',' ','=',' ',
307 '\'','%','s','\'',0};
309 TRACE("(package %p, appValue %p, sig %p)\n", package, appValue, sig);
310 *appValue = NULL;
311 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, sig->Name);
312 if (rc == ERROR_SUCCESS)
314 MSIRECORD *row = 0;
315 LPWSTR keyPath = NULL, valueName = NULL;
316 int root, type;
317 HKEY rootKey, key = NULL;
318 DWORD sz = 0, regType;
319 LPBYTE value = NULL;
321 rc = MSI_ViewExecute(view, 0);
322 if (rc != ERROR_SUCCESS)
324 TRACE("MSI_ViewExecute returned %d\n", rc);
325 goto end;
327 rc = MSI_ViewFetch(view,&row);
328 if (rc != ERROR_SUCCESS)
330 TRACE("MSI_ViewFetch returned %d\n", rc);
331 rc = ERROR_SUCCESS;
332 goto end;
335 root = MSI_RecordGetInteger(row,2);
336 keyPath = msi_dup_record_field(row,3);
337 /* FIXME: keyPath needs to be expanded for properties */
338 valueName = msi_dup_record_field(row,4);
339 /* FIXME: valueName probably does too */
340 type = MSI_RecordGetInteger(row,5);
342 switch (root)
344 case msidbRegistryRootClassesRoot:
345 rootKey = HKEY_CLASSES_ROOT;
346 break;
347 case msidbRegistryRootCurrentUser:
348 rootKey = HKEY_CURRENT_USER;
349 break;
350 case msidbRegistryRootLocalMachine:
351 rootKey = HKEY_LOCAL_MACHINE;
352 break;
353 case msidbRegistryRootUsers:
354 rootKey = HKEY_USERS;
355 break;
356 default:
357 WARN("Unknown root key %d\n", root);
358 goto end;
361 rc = RegOpenKeyW(rootKey, keyPath, &key);
362 if (rc)
364 TRACE("RegOpenKeyW returned %d\n", rc);
365 rc = ERROR_SUCCESS;
366 goto end;
368 rc = RegQueryValueExW(key, valueName, NULL, NULL, NULL, &sz);
369 if (rc)
371 TRACE("RegQueryValueExW returned %d\n", rc);
372 rc = ERROR_SUCCESS;
373 goto end;
375 /* FIXME: sanity-check sz before allocating (is there an upper-limit
376 * on the value of a property?)
378 value = msi_alloc( sz);
379 rc = RegQueryValueExW(key, valueName, NULL, &regType, value, &sz);
380 if (rc)
382 TRACE("RegQueryValueExW returned %d\n", rc);
383 rc = ERROR_SUCCESS;
384 goto end;
387 /* bail out if the registry key is empty */
388 if (sz == 0)
390 rc = ERROR_SUCCESS;
391 goto end;
394 switch (type & 0x0f)
396 case msidbLocatorTypeDirectory:
397 rc = ACTION_SearchDirectory(package, sig, (LPCWSTR)value, 0,
398 appValue);
399 break;
400 case msidbLocatorTypeRawValue:
401 ACTION_ConvertRegValue(regType, value, sz, appValue);
402 break;
403 default:
404 FIXME("AppSearch unimplemented for type %d (key path %s, value %s)\n",
405 type, debugstr_w(keyPath), debugstr_w(valueName));
407 end:
408 msi_free( value);
409 RegCloseKey(key);
411 msi_free( keyPath);
412 msi_free( valueName);
414 if (row)
415 msiobj_release(&row->hdr);
416 MSI_ViewClose(view);
417 msiobj_release(&view->hdr);
419 else
421 TRACE("MSI_OpenQuery returned %d\n", rc);
422 rc = ERROR_SUCCESS;
425 TRACE("returning %d\n", rc);
426 return rc;
429 static UINT ACTION_AppSearchIni(MSIPACKAGE *package, LPWSTR *appValue,
430 MSISIGNATURE *sig)
432 MSIQUERY *view;
433 UINT rc;
434 static const WCHAR ExecSeqQuery[] = {
435 's','e','l','e','c','t',' ','*',' ',
436 'f','r','o','m',' ',
437 'I','n','i','L','o','c','a','t','o','r',' ',
438 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e','_',' ','=',' ',
439 '\'','%','s','\'',0};
441 TRACE("(package %p, appValue %p, sig %p)\n", package, appValue, sig);
442 *appValue = NULL;
443 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, sig->Name);
444 if (rc == ERROR_SUCCESS)
446 MSIRECORD *row = 0;
447 LPWSTR fileName, section, key;
448 int field, type;
449 WCHAR buf[MAX_PATH];
451 rc = MSI_ViewExecute(view, 0);
452 if (rc != ERROR_SUCCESS)
454 TRACE("MSI_ViewExecute returned %d\n", rc);
455 goto end;
457 rc = MSI_ViewFetch(view,&row);
458 if (rc != ERROR_SUCCESS)
460 TRACE("MSI_ViewFetch returned %d\n", rc);
461 rc = ERROR_SUCCESS;
462 goto end;
465 fileName = msi_dup_record_field(row, 2);
466 section = msi_dup_record_field(row, 3);
467 key = msi_dup_record_field(row, 4);
468 if ((field = MSI_RecordGetInteger(row, 5)) == MSI_NULL_INTEGER)
469 field = 0;
470 if ((type = MSI_RecordGetInteger(row, 6)) == MSI_NULL_INTEGER)
471 type = 0;
473 GetPrivateProfileStringW(section, key, NULL, buf,
474 sizeof(buf) / sizeof(WCHAR), fileName);
475 if (buf[0])
477 switch (type & 0x0f)
479 case msidbLocatorTypeDirectory:
480 FIXME("unimplemented for type Directory (dir: %s)\n",
481 debugstr_w(buf));
482 break;
483 case msidbLocatorTypeFileName:
484 FIXME("unimplemented for type File (file: %s)\n",
485 debugstr_w(buf));
486 break;
487 case msidbLocatorTypeRawValue:
488 *appValue = strdupW(buf);
489 break;
493 msi_free(fileName);
494 msi_free(section);
495 msi_free(key);
497 end:
498 if (row)
499 msiobj_release(&row->hdr);
500 MSI_ViewClose(view);
501 msiobj_release(&view->hdr);
503 else
505 TRACE("MSI_OpenQuery returned %d\n", rc);
506 rc = ERROR_SUCCESS;
510 TRACE("returning %d\n", rc);
511 return rc;
514 /* Expands the value in src into a path without property names and only
515 * containing long path names into dst. Replaces at most len characters of dst,
516 * and always NULL-terminates dst if dst is not NULL and len >= 1.
517 * May modify src.
518 * Assumes src and dst are non-overlapping.
519 * FIXME: return code probably needed:
520 * - what does AppSearch return if the table values are invalid?
521 * - what if dst is too small?
523 static void ACTION_ExpandAnyPath(MSIPACKAGE *package, WCHAR *src, WCHAR *dst,
524 size_t len)
526 WCHAR *ptr;
527 size_t copied = 0;
529 if (!src || !dst || !len)
530 return;
532 /* Ignore the short portion of the path, don't think we can use it anyway */
533 if ((ptr = strchrW(src, '|')))
534 ptr++;
535 else
536 ptr = src;
537 while (*ptr && copied < len - 1)
539 WCHAR *prop = strchrW(ptr, '[');
541 if (prop)
543 WCHAR *propEnd = strchrW(prop + 1, ']');
545 if (!propEnd)
547 WARN("Unterminated property name in AnyPath: %s\n",
548 debugstr_w(prop));
549 break;
551 else
553 DWORD propLen;
555 *propEnd = 0;
556 propLen = len - copied - 1;
557 MSI_GetPropertyW(package, prop + 1, dst + copied, &propLen);
558 ptr = propEnd + 1;
559 copied += propLen;
562 else
564 size_t toCopy = min(strlenW(ptr) + 1, len - copied - 1);
566 memcpy(dst + copied, ptr, toCopy * sizeof(WCHAR));
567 ptr += toCopy;
568 copied += toCopy;
571 *(dst + copied) = '\0';
574 /* Sets *matches to whether the file (whose path is filePath) matches the
575 * versions set in sig.
576 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
577 * something else if an install-halting error occurs.
579 static UINT ACTION_FileVersionMatches(MSISIGNATURE *sig, LPCWSTR filePath,
580 BOOL *matches)
582 UINT rc = ERROR_SUCCESS;
584 *matches = FALSE;
585 if (sig->Languages)
587 FIXME(": need to check version for languages %s\n",
588 debugstr_w(sig->Languages));
590 else
592 DWORD zero, size = GetFileVersionInfoSizeW(filePath, &zero);
594 if (size)
596 LPVOID buf = msi_alloc( size);
598 if (buf)
600 static WCHAR rootW[] = { '\\',0 };
601 UINT versionLen;
602 LPVOID subBlock = NULL;
604 if (GetFileVersionInfoW(filePath, 0, size, buf))
605 VerQueryValueW(buf, rootW, &subBlock, &versionLen);
606 if (subBlock)
608 VS_FIXEDFILEINFO *info =
609 (VS_FIXEDFILEINFO *)subBlock;
611 TRACE("Comparing file version %d.%d.%d.%d:\n",
612 HIWORD(info->dwFileVersionMS),
613 LOWORD(info->dwFileVersionMS),
614 HIWORD(info->dwFileVersionLS),
615 LOWORD(info->dwFileVersionLS));
616 if (info->dwFileVersionMS < sig->MinVersionMS
617 || (info->dwFileVersionMS == sig->MinVersionMS &&
618 info->dwFileVersionLS < sig->MinVersionLS))
620 TRACE("Less than minimum version %d.%d.%d.%d\n",
621 HIWORD(sig->MinVersionMS),
622 LOWORD(sig->MinVersionMS),
623 HIWORD(sig->MinVersionLS),
624 LOWORD(sig->MinVersionLS));
626 else if (info->dwFileVersionMS < sig->MinVersionMS
627 || (info->dwFileVersionMS == sig->MinVersionMS &&
628 info->dwFileVersionLS < sig->MinVersionLS))
630 TRACE("Greater than minimum version %d.%d.%d.%d\n",
631 HIWORD(sig->MaxVersionMS),
632 LOWORD(sig->MaxVersionMS),
633 HIWORD(sig->MaxVersionLS),
634 LOWORD(sig->MaxVersionLS));
636 else
637 *matches = TRUE;
639 msi_free( buf);
641 else
642 rc = ERROR_OUTOFMEMORY;
645 return rc;
648 /* Sets *matches to whether the file in findData matches that in sig.
649 * fullFilePath is assumed to be the full path of the file specified in
650 * findData, which may be necessary to compare the version.
651 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
652 * something else if an install-halting error occurs.
654 static UINT ACTION_FileMatchesSig(MSISIGNATURE *sig,
655 LPWIN32_FIND_DATAW findData, LPCWSTR fullFilePath, BOOL *matches)
657 UINT rc = ERROR_SUCCESS;
659 *matches = TRUE;
660 /* assumes the caller has already ensured the filenames match, so check
661 * the other fields..
663 if (sig->MinTime.dwLowDateTime || sig->MinTime.dwHighDateTime)
665 if (findData->ftCreationTime.dwHighDateTime <
666 sig->MinTime.dwHighDateTime ||
667 (findData->ftCreationTime.dwHighDateTime == sig->MinTime.dwHighDateTime
668 && findData->ftCreationTime.dwLowDateTime <
669 sig->MinTime.dwLowDateTime))
670 *matches = FALSE;
672 if (*matches && (sig->MaxTime.dwLowDateTime || sig->MaxTime.dwHighDateTime))
674 if (findData->ftCreationTime.dwHighDateTime >
675 sig->MaxTime.dwHighDateTime ||
676 (findData->ftCreationTime.dwHighDateTime == sig->MaxTime.dwHighDateTime
677 && findData->ftCreationTime.dwLowDateTime >
678 sig->MaxTime.dwLowDateTime))
679 *matches = FALSE;
681 if (*matches && sig->MinSize && findData->nFileSizeLow < sig->MinSize)
682 *matches = FALSE;
683 if (*matches && sig->MaxSize && findData->nFileSizeLow > sig->MaxSize)
684 *matches = FALSE;
685 if (*matches && (sig->MinVersionMS || sig->MinVersionLS ||
686 sig->MaxVersionMS || sig->MaxVersionLS))
687 rc = ACTION_FileVersionMatches(sig, fullFilePath, matches);
688 return rc;
691 /* Recursively searches the directory dir for files that match the signature
692 * sig, up to (depth + 1) levels deep. That is, if depth is 0, it searches dir
693 * (and only dir). If depth is 1, searches dir and its immediate
694 * subdirectories.
695 * Assumes sig->File is not NULL.
696 * Returns ERROR_SUCCESS on success (which may include non-critical errors),
697 * something else on failures which should halt the install.
699 static UINT ACTION_RecurseSearchDirectory(MSIPACKAGE *package, LPWSTR *appValue,
700 MSISIGNATURE *sig, LPCWSTR dir, int depth)
702 static const WCHAR starDotStarW[] = { '*','.','*',0 };
703 UINT rc = ERROR_SUCCESS;
704 size_t dirLen = lstrlenW(dir), fileLen = lstrlenW(sig->File);
705 WCHAR *buf;
707 TRACE("Searching directory %s for file %s, depth %d\n", debugstr_w(dir),
708 debugstr_w(sig->File), depth);
710 if (depth < 0)
711 return ERROR_INVALID_PARAMETER;
713 *appValue = NULL;
714 /* We need the buffer in both paths below, so go ahead and allocate it
715 * here. Add two because we might need to add a backslash if the dir name
716 * isn't backslash-terminated.
718 buf = msi_alloc( (dirLen + max(fileLen, lstrlenW(starDotStarW)) + 2) * sizeof(WCHAR));
719 if (buf)
721 /* a depth of 0 implies we should search dir, so go ahead and search */
722 HANDLE hFind;
723 WIN32_FIND_DATAW findData;
725 memcpy(buf, dir, dirLen * sizeof(WCHAR));
726 if (buf[dirLen - 1] != '\\')
727 buf[dirLen++ - 1] = '\\';
728 memcpy(buf + dirLen, sig->File, (fileLen + 1) * sizeof(WCHAR));
729 hFind = FindFirstFileW(buf, &findData);
730 if (hFind != INVALID_HANDLE_VALUE)
732 BOOL matches;
734 /* assuming Signature can't contain wildcards for the file name,
735 * so don't bother with FindNextFileW here.
737 if (!(rc = ACTION_FileMatchesSig(sig, &findData, buf, &matches))
738 && matches)
740 TRACE("found file, returning %s\n", debugstr_w(buf));
741 *appValue = buf;
743 FindClose(hFind);
745 if (rc == ERROR_SUCCESS && !*appValue && depth > 0)
747 HANDLE hFind;
748 WIN32_FIND_DATAW findData;
750 memcpy(buf, dir, dirLen * sizeof(WCHAR));
751 if (buf[dirLen - 1] != '\\')
752 buf[dirLen++ - 1] = '\\';
753 lstrcpyW(buf + dirLen, starDotStarW);
754 hFind = FindFirstFileW(buf, &findData);
755 if (hFind != INVALID_HANDLE_VALUE)
757 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
758 rc = ACTION_RecurseSearchDirectory(package, appValue, sig,
759 findData.cFileName, depth - 1);
760 while (rc == ERROR_SUCCESS && !*appValue &&
761 FindNextFileW(hFind, &findData) != 0)
763 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
764 rc = ACTION_RecurseSearchDirectory(package, appValue,
765 sig, findData.cFileName, depth - 1);
767 FindClose(hFind);
770 if (!*appValue)
771 msi_free(buf);
773 else
774 rc = ERROR_OUTOFMEMORY;
776 return rc;
779 static UINT ACTION_CheckDirectory(MSIPACKAGE *package, LPCWSTR dir,
780 LPWSTR *appValue)
782 UINT rc = ERROR_SUCCESS;
784 if (GetFileAttributesW(dir) & FILE_ATTRIBUTE_DIRECTORY)
786 TRACE("directory exists, returning %s\n", debugstr_w(dir));
787 *appValue = strdupW(dir);
789 return rc;
792 static BOOL ACTION_IsFullPath(LPCWSTR path)
794 WCHAR first = toupperW(path[0]);
795 BOOL ret;
797 if (first >= 'A' && first <= 'Z' && path[1] == ':')
798 ret = TRUE;
799 else if (path[0] == '\\' && path[1] == '\\')
800 ret = TRUE;
801 else
802 ret = FALSE;
803 return ret;
806 static UINT ACTION_SearchDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
807 LPCWSTR path, int depth, LPWSTR *appValue)
809 UINT rc;
811 TRACE("%p, %p, %s, %d, %p\n", package, sig, debugstr_w(path), depth,
812 appValue);
813 if (ACTION_IsFullPath(path))
815 if (sig->File)
816 rc = ACTION_RecurseSearchDirectory(package, appValue, sig,
817 path, depth);
818 else
820 /* Recursively searching a directory makes no sense when the
821 * directory to search is the thing you're trying to find.
823 rc = ACTION_CheckDirectory(package, path, appValue);
826 else
828 WCHAR pathWithDrive[MAX_PATH] = { 'C',':','\\',0 };
829 DWORD drives = GetLogicalDrives();
830 int i;
832 rc = ERROR_SUCCESS;
833 *appValue = NULL;
834 for (i = 0; rc == ERROR_SUCCESS && !*appValue && i < 26; i++)
835 if (drives & (1 << drives))
837 pathWithDrive[0] = 'A' + i;
838 if (GetDriveTypeW(pathWithDrive) == DRIVE_FIXED)
840 lstrcpynW(pathWithDrive + 3, path,
841 sizeof(pathWithDrive) / sizeof(pathWithDrive[0]) - 3);
842 if (sig->File)
843 rc = ACTION_RecurseSearchDirectory(package, appValue,
844 sig, pathWithDrive, depth);
845 else
846 rc = ACTION_CheckDirectory(package, pathWithDrive,
847 appValue);
851 TRACE("returning %d\n", rc);
852 return rc;
855 static UINT ACTION_AppSearchSigName(MSIPACKAGE *package, LPCWSTR sigName,
856 MSISIGNATURE *sig, LPWSTR *appValue);
858 static UINT ACTION_AppSearchDr(MSIPACKAGE *package, LPWSTR *appValue,
859 MSISIGNATURE *sig)
861 MSIQUERY *view;
862 UINT rc;
863 static const WCHAR ExecSeqQuery[] = {
864 's','e','l','e','c','t',' ','*',' ',
865 'f','r','o','m',' ',
866 'D','r','L','o','c','a','t','o','r',' ',
867 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e','_',' ','=',' ',
868 '\'','%','s','\'',0};
870 TRACE("(package %p, sig %p)\n", package, sig);
871 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, sig->Name);
872 if (rc == ERROR_SUCCESS)
874 MSIRECORD *row = 0;
875 WCHAR expanded[MAX_PATH];
876 LPWSTR parentName = NULL, path = NULL, parent = NULL;
877 int depth;
879 rc = MSI_ViewExecute(view, 0);
880 if (rc != ERROR_SUCCESS)
882 TRACE("MSI_ViewExecute returned %d\n", rc);
883 goto end;
885 rc = MSI_ViewFetch(view,&row);
886 if (rc != ERROR_SUCCESS)
888 TRACE("MSI_ViewFetch returned %d\n", rc);
889 rc = ERROR_SUCCESS;
890 goto end;
893 /* check whether parent is set */
894 parentName = msi_dup_record_field(row,2);
895 if (parentName)
897 MSISIGNATURE parentSig;
899 rc = ACTION_AppSearchSigName(package, parentName, &parentSig,
900 &parent);
901 ACTION_FreeSignature(&parentSig);
902 msi_free(parentName);
904 /* now look for path */
905 path = msi_dup_record_field(row,3);
906 if (MSI_RecordIsNull(row,4))
907 depth = 0;
908 else
909 depth = MSI_RecordGetInteger(row,4);
910 ACTION_ExpandAnyPath(package, path, expanded,
911 sizeof(expanded) / sizeof(expanded[0]));
912 msi_free(path);
913 if (parent)
915 path = msi_alloc(strlenW(parent) + strlenW(expanded) + 1);
916 if (!path)
917 goto end;
918 strcpyW(path, parent);
919 strcatW(path, expanded);
921 else
922 path = expanded;
923 rc = ACTION_SearchDirectory(package, sig, path, depth, appValue);
925 end:
926 if (path != expanded)
927 msi_free(path);
928 msi_free(parent);
929 if (row)
930 msiobj_release(&row->hdr);
931 MSI_ViewClose(view);
932 msiobj_release(&view->hdr);
934 else
936 TRACE("MSI_OpenQuery returned %d\n", rc);
937 rc = ERROR_SUCCESS;
940 TRACE("returning %d\n", rc);
941 return rc;
944 static UINT ACTION_AppSearchSigName(MSIPACKAGE *package, LPCWSTR sigName,
945 MSISIGNATURE *sig, LPWSTR *appValue)
947 UINT rc;
949 *appValue = NULL;
950 rc = ACTION_AppSearchGetSignature(package, sig, sigName);
951 if (rc == ERROR_SUCCESS)
953 rc = ACTION_AppSearchComponents(package, appValue, sig);
954 if (rc == ERROR_SUCCESS && !*appValue)
956 rc = ACTION_AppSearchReg(package, appValue, sig);
957 if (rc == ERROR_SUCCESS && !*appValue)
959 rc = ACTION_AppSearchIni(package, appValue, sig);
960 if (rc == ERROR_SUCCESS && !*appValue)
961 rc = ACTION_AppSearchDr(package, appValue, sig);
965 return rc;
968 /* http://msdn.microsoft.com/library/en-us/msi/setup/appsearch_table.asp
969 * is the best reference for the AppSearch table and how it's used.
971 UINT ACTION_AppSearch(MSIPACKAGE *package)
973 MSIQUERY *view;
974 UINT rc;
975 static const WCHAR ExecSeqQuery[] = {
976 's','e','l','e','c','t',' ','*',' ',
977 'f','r','o','m',' ',
978 'A','p','p','S','e','a','r','c','h',0};
980 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery);
981 if (rc == ERROR_SUCCESS)
983 MSIRECORD *row = 0;
984 LPWSTR propName, sigName;
986 rc = MSI_ViewExecute(view, 0);
987 if (rc != ERROR_SUCCESS)
988 goto end;
990 while (!rc)
992 MSISIGNATURE sig;
993 LPWSTR value;
995 rc = MSI_ViewFetch(view,&row);
996 if (rc != ERROR_SUCCESS)
998 rc = ERROR_SUCCESS;
999 break;
1002 /* get property and signature */
1003 propName = msi_dup_record_field(row,1);
1004 sigName = msi_dup_record_field(row,2);
1006 TRACE("Searching for Property %s, Signature_ %s\n",
1007 debugstr_w(propName), debugstr_w(sigName));
1009 rc = ACTION_AppSearchSigName(package, sigName, &sig, &value);
1010 if (value)
1012 MSI_SetPropertyW(package, propName, value);
1013 msi_free(value);
1015 ACTION_FreeSignature(&sig);
1016 msi_free(propName);
1017 msi_free(sigName);
1018 msiobj_release(&row->hdr);
1021 end:
1022 MSI_ViewClose(view);
1023 msiobj_release(&view->hdr);
1025 else
1026 rc = ERROR_SUCCESS;
1028 return rc;