msi: Only append a backslash to directories.
[wine/multimedia.git] / dlls / msi / appsearch.c
blob056cd246995a4c219aa63efb79c1961cb9fa0cfa
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 "shlwapi.h"
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
34 #include "msipriv.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 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, LPCWSTR name)
86 static const WCHAR query[] = {
87 's','e','l','e','c','t',' ','*',' ',
88 'f','r','o','m',' ',
89 'S','i','g','n','a','t','u','r','e',' ',
90 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e',' ','=',' ',
91 '\'','%','s','\'',0};
92 LPWSTR minVersion, maxVersion;
93 MSIRECORD *row;
94 DWORD time;
96 TRACE("package %p, sig %p\n", package, sig);
98 memset(sig, 0, sizeof(*sig));
99 sig->Name = name;
100 row = MSI_QueryGetRecord( package->db, query, name );
101 if (!row)
103 TRACE("failed to query signature for %s\n", debugstr_w(name));
104 return ERROR_SUCCESS;
107 /* get properties */
108 sig->File = msi_dup_record_field(row,2);
109 minVersion = msi_dup_record_field(row,3);
110 if (minVersion)
112 ACTION_VerStrToInteger(minVersion, &sig->MinVersionMS, &sig->MinVersionLS);
113 msi_free( minVersion );
115 maxVersion = msi_dup_record_field(row,4);
116 if (maxVersion)
118 ACTION_VerStrToInteger(maxVersion, &sig->MaxVersionMS, &sig->MaxVersionLS);
119 msi_free( maxVersion );
121 sig->MinSize = MSI_RecordGetInteger(row,5);
122 if (sig->MinSize == MSI_NULL_INTEGER)
123 sig->MinSize = 0;
124 sig->MaxSize = MSI_RecordGetInteger(row,6);
125 if (sig->MaxSize == MSI_NULL_INTEGER)
126 sig->MaxSize = 0;
127 sig->Languages = msi_dup_record_field(row,9);
128 time = MSI_RecordGetInteger(row,7);
129 if (time != MSI_NULL_INTEGER)
130 DosDateTimeToFileTime(HIWORD(time), LOWORD(time), &sig->MinTime);
131 time = MSI_RecordGetInteger(row,8);
132 if (time != MSI_NULL_INTEGER)
133 DosDateTimeToFileTime(HIWORD(time), LOWORD(time), &sig->MaxTime);
135 TRACE("Found file name %s for Signature_ %s;\n",
136 debugstr_w(sig->File), debugstr_w(name));
137 TRACE("MinVersion is %d.%d.%d.%d\n", HIWORD(sig->MinVersionMS),
138 LOWORD(sig->MinVersionMS), HIWORD(sig->MinVersionLS),
139 LOWORD(sig->MinVersionLS));
140 TRACE("MaxVersion is %d.%d.%d.%d\n", HIWORD(sig->MaxVersionMS),
141 LOWORD(sig->MaxVersionMS), HIWORD(sig->MaxVersionLS),
142 LOWORD(sig->MaxVersionLS));
143 TRACE("MinSize is %d, MaxSize is %d;\n", sig->MinSize, sig->MaxSize);
144 TRACE("Languages is %s\n", debugstr_w(sig->Languages));
146 msiobj_release( &row->hdr );
148 return ERROR_SUCCESS;
151 /* Frees any memory allocated in sig */
152 static void ACTION_FreeSignature(MSISIGNATURE *sig)
154 msi_free(sig->File);
155 msi_free(sig->Languages);
158 static LPWSTR app_search_file(LPWSTR path, MSISIGNATURE *sig)
160 VS_FIXEDFILEINFO *info;
161 DWORD attr, handle, size;
162 LPWSTR val = NULL;
163 LPBYTE buffer;
165 static const WCHAR root[] = {'\\',0};
167 if (!sig->File)
169 PathRemoveFileSpecW(path);
170 PathAddBackslashW(path);
172 attr = GetFileAttributesW(path);
173 if (attr != INVALID_FILE_ATTRIBUTES &&
174 (attr & FILE_ATTRIBUTE_DIRECTORY))
175 return strdupW(path);
177 return NULL;
180 attr = GetFileAttributesW(path);
181 if (attr == INVALID_FILE_ATTRIBUTES || attr == FILE_ATTRIBUTE_DIRECTORY)
182 return NULL;
184 size = GetFileVersionInfoSizeW(path, &handle);
185 if (!size)
186 return strdupW(path);
188 buffer = msi_alloc(size);
189 if (!buffer)
190 return NULL;
192 if (!GetFileVersionInfoW(path, 0, size, buffer))
193 goto done;
195 if (!VerQueryValueW(buffer, root, (LPVOID)&info, &size) || !info)
196 goto done;
198 if (sig->MinVersionLS || sig->MinVersionMS)
200 if (info->dwFileVersionMS < sig->MinVersionMS)
201 goto done;
203 if (info->dwFileVersionMS == sig->MinVersionMS &&
204 info->dwFileVersionLS < sig->MinVersionLS)
205 goto done;
208 if (sig->MaxVersionLS || sig->MaxVersionMS)
210 if (info->dwFileVersionMS > sig->MaxVersionMS)
211 goto done;
213 if (info->dwFileVersionMS == sig->MaxVersionMS &&
214 info->dwFileVersionLS > sig->MaxVersionLS)
215 goto done;
218 val = strdupW(path);
220 done:
221 msi_free(buffer);
222 return val;
225 static UINT ACTION_AppSearchComponents(MSIPACKAGE *package, LPWSTR *appValue, MSISIGNATURE *sig)
227 static const WCHAR query[] = {
228 'S','E','L','E','C','T',' ','*',' ',
229 'F','R','O','M',' ',
230 '`','C','o','m','p','L','o','c','a','t','o','r','`',' ',
231 'W','H','E','R','E',' ','`','S','i','g','n','a','t','u','r','e','_','`',' ','=',' ',
232 '\'','%','s','\'',0};
233 static const WCHAR sigquery[] = {
234 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
235 '`','S','i','g','n','a','t','u','r','e','`',' ',
236 'W','H','E','R','E',' ','`','S','i','g','n','a','t','u','r','e','`',' ','=',' ',
237 '\'','%','s','\'',0};
239 MSIRECORD *row, *rec;
240 LPCWSTR signature, guid;
241 BOOL sigpresent = TRUE;
242 BOOL isdir;
243 UINT type;
244 WCHAR path[MAX_PATH];
245 DWORD size = MAX_PATH;
246 LPWSTR ptr;
247 DWORD attr;
249 TRACE("%s\n", debugstr_w(sig->Name));
251 *appValue = NULL;
253 row = MSI_QueryGetRecord(package->db, query, sig->Name);
254 if (!row)
256 TRACE("failed to query CompLocator for %s\n", debugstr_w(sig->Name));
257 return ERROR_SUCCESS;
260 signature = MSI_RecordGetString(row, 1);
261 guid = MSI_RecordGetString(row, 2);
262 type = MSI_RecordGetInteger(row, 3);
264 rec = MSI_QueryGetRecord(package->db, sigquery, signature);
265 if (!rec)
266 sigpresent = FALSE;
268 *path = '\0';
269 MsiLocateComponentW(guid, path, &size);
270 if (!*path)
271 goto done;
273 attr = GetFileAttributesW(path);
274 if (attr == INVALID_FILE_ATTRIBUTES)
275 goto done;
277 isdir = (attr & FILE_ATTRIBUTE_DIRECTORY);
279 if (type != msidbLocatorTypeDirectory && sigpresent && !isdir)
281 *appValue = app_search_file(path, sig);
283 else if (!sigpresent && (type != msidbLocatorTypeDirectory || isdir))
285 if (type == msidbLocatorTypeFileName)
287 ptr = strrchrW(path, '\\');
288 *(ptr + 1) = '\0';
290 else
291 PathAddBackslashW(path);
293 *appValue = strdupW(path);
295 else if (sigpresent)
297 PathAddBackslashW(path);
298 lstrcatW(path, MSI_RecordGetString(rec, 2));
300 attr = GetFileAttributesW(path);
301 if (attr != INVALID_FILE_ATTRIBUTES && attr != FILE_ATTRIBUTE_DIRECTORY)
302 *appValue = strdupW(path);
305 done:
306 if (rec) msiobj_release(&rec->hdr);
307 msiobj_release(&row->hdr);
308 return ERROR_SUCCESS;
311 static void ACTION_ConvertRegValue(DWORD regType, const BYTE *value, DWORD sz,
312 LPWSTR *appValue)
314 static const WCHAR dwordFmt[] = { '#','%','d','\0' };
315 static const WCHAR binPre[] = { '#','x','\0' };
316 static const WCHAR binFmt[] = { '%','0','2','X','\0' };
317 LPWSTR ptr;
318 DWORD i;
320 switch (regType)
322 case REG_SZ:
323 if (*(LPCWSTR)value == '#')
325 /* escape leading pound with another */
326 *appValue = msi_alloc(sz + sizeof(WCHAR));
327 (*appValue)[0] = '#';
328 strcpyW(*appValue + 1, (LPCWSTR)value);
330 else
332 *appValue = msi_alloc(sz);
333 strcpyW(*appValue, (LPCWSTR)value);
335 break;
336 case REG_DWORD:
337 /* 7 chars for digits, 1 for NULL, 1 for #, and 1 for sign
338 * char if needed
340 *appValue = msi_alloc(10 * sizeof(WCHAR));
341 sprintfW(*appValue, dwordFmt, *(const DWORD *)value);
342 break;
343 case REG_EXPAND_SZ:
344 sz = ExpandEnvironmentStringsW((LPCWSTR)value, NULL, 0);
345 *appValue = msi_alloc(sz * sizeof(WCHAR));
346 ExpandEnvironmentStringsW((LPCWSTR)value, *appValue, sz);
347 break;
348 case REG_BINARY:
349 /* #x<nibbles>\0 */
350 *appValue = msi_alloc((sz * 2 + 3) * sizeof(WCHAR));
351 lstrcpyW(*appValue, binPre);
352 ptr = *appValue + lstrlenW(binPre);
353 for (i = 0; i < sz; i++, ptr += 2)
354 sprintfW(ptr, binFmt, value[i]);
355 break;
356 default:
357 WARN("unimplemented for values of type %d\n", regType);
358 *appValue = NULL;
362 static UINT ACTION_SearchDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
363 LPCWSTR path, int depth, LPWSTR *appValue);
365 static UINT ACTION_AppSearchReg(MSIPACKAGE *package, LPWSTR *appValue, MSISIGNATURE *sig)
367 static const WCHAR query[] = {
368 's','e','l','e','c','t',' ','*',' ',
369 'f','r','o','m',' ',
370 'R','e','g','L','o','c','a','t','o','r',' ',
371 'w','h','e','r','e',' ',
372 'S','i','g','n','a','t','u','r','e','_',' ','=',' ', '\'','%','s','\'',0};
373 LPWSTR keyPath = NULL, valueName = NULL;
374 LPWSTR deformatted = NULL;
375 int root, type;
376 HKEY rootKey, key = NULL;
377 DWORD sz = 0, regType;
378 LPBYTE value = NULL;
379 MSIRECORD *row;
380 UINT rc;
382 TRACE("%s\n", debugstr_w(sig->Name));
384 *appValue = NULL;
386 row = MSI_QueryGetRecord( package->db, query, sig->Name );
387 if (!row)
389 TRACE("failed to query RegLocator for %s\n", debugstr_w(sig->Name));
390 return ERROR_SUCCESS;
393 root = MSI_RecordGetInteger(row,2);
394 keyPath = msi_dup_record_field(row,3);
395 valueName = msi_dup_record_field(row,4);
396 type = MSI_RecordGetInteger(row,5);
398 deformat_string(package, keyPath, &deformatted);
400 switch (root)
402 case msidbRegistryRootClassesRoot:
403 rootKey = HKEY_CLASSES_ROOT;
404 break;
405 case msidbRegistryRootCurrentUser:
406 rootKey = HKEY_CURRENT_USER;
407 break;
408 case msidbRegistryRootLocalMachine:
409 rootKey = HKEY_LOCAL_MACHINE;
410 break;
411 case msidbRegistryRootUsers:
412 rootKey = HKEY_USERS;
413 break;
414 default:
415 WARN("Unknown root key %d\n", root);
416 goto end;
419 rc = RegOpenKeyW(rootKey, deformatted, &key);
420 if (rc)
422 TRACE("RegOpenKeyW returned %d\n", rc);
423 goto end;
426 rc = RegQueryValueExW(key, valueName, NULL, NULL, NULL, &sz);
427 if (rc)
429 TRACE("RegQueryValueExW returned %d\n", rc);
430 goto end;
432 /* FIXME: sanity-check sz before allocating (is there an upper-limit
433 * on the value of a property?)
435 value = msi_alloc( sz );
436 rc = RegQueryValueExW(key, valueName, NULL, &regType, value, &sz);
437 if (rc)
439 TRACE("RegQueryValueExW returned %d\n", rc);
440 goto end;
443 /* bail out if the registry key is empty */
444 if (sz == 0)
445 goto end;
447 switch (type & 0x0f)
449 case msidbLocatorTypeDirectory:
450 rc = ACTION_SearchDirectory(package, sig, (LPWSTR)value, 0, appValue);
451 break;
452 case msidbLocatorTypeFileName:
453 *appValue = app_search_file((LPWSTR)value, sig);
454 break;
455 case msidbLocatorTypeRawValue:
456 ACTION_ConvertRegValue(regType, value, sz, appValue);
457 break;
458 default:
459 FIXME("AppSearch unimplemented for type %d (key path %s, value %s)\n",
460 type, debugstr_w(keyPath), debugstr_w(valueName));
462 end:
463 msi_free( value );
464 RegCloseKey( key );
466 msi_free( keyPath );
467 msi_free( valueName );
468 msi_free( deformatted );
470 msiobj_release(&row->hdr);
472 return ERROR_SUCCESS;
475 static LPWSTR get_ini_field(LPWSTR buf, int field)
477 LPWSTR beg, end;
478 int i = 1;
480 if (field == 0)
481 return strdupW(buf);
483 beg = buf;
484 while ((end = strchrW(beg, ',')) && i < field)
486 beg = end + 1;
487 while (*beg && *beg == ' ')
488 beg++;
490 i++;
493 end = strchrW(beg, ',');
494 if (!end)
495 end = beg + lstrlenW(beg);
497 *end = '\0';
498 return strdupW(beg);
501 static UINT ACTION_AppSearchIni(MSIPACKAGE *package, LPWSTR *appValue,
502 MSISIGNATURE *sig)
504 static const WCHAR query[] = {
505 's','e','l','e','c','t',' ','*',' ',
506 'f','r','o','m',' ',
507 'I','n','i','L','o','c','a','t','o','r',' ',
508 'w','h','e','r','e',' ',
509 'S','i','g','n','a','t','u','r','e','_',' ','=',' ','\'','%','s','\'',0};
510 MSIRECORD *row;
511 LPWSTR fileName, section, key;
512 int field, type;
513 WCHAR buf[MAX_PATH];
515 TRACE("%s\n", debugstr_w(sig->Name));
517 *appValue = NULL;
519 row = MSI_QueryGetRecord( package->db, query, sig->Name );
520 if (!row)
522 TRACE("failed to query IniLocator for %s\n", debugstr_w(sig->Name));
523 return ERROR_SUCCESS;
526 fileName = msi_dup_record_field(row, 2);
527 section = msi_dup_record_field(row, 3);
528 key = msi_dup_record_field(row, 4);
529 field = MSI_RecordGetInteger(row, 5);
530 type = MSI_RecordGetInteger(row, 6);
531 if (field == MSI_NULL_INTEGER)
532 field = 0;
533 if (type == MSI_NULL_INTEGER)
534 type = 0;
536 GetPrivateProfileStringW(section, key, NULL, buf, MAX_PATH, fileName);
537 if (buf[0])
539 switch (type & 0x0f)
541 case msidbLocatorTypeDirectory:
542 ACTION_SearchDirectory(package, sig, buf, 0, appValue);
543 break;
544 case msidbLocatorTypeFileName:
545 *appValue = app_search_file(buf, sig);
546 break;
547 case msidbLocatorTypeRawValue:
548 *appValue = get_ini_field(buf, field);
549 break;
553 msi_free(fileName);
554 msi_free(section);
555 msi_free(key);
557 msiobj_release(&row->hdr);
559 return ERROR_SUCCESS;
562 /* Expands the value in src into a path without property names and only
563 * containing long path names into dst. Replaces at most len characters of dst,
564 * and always NULL-terminates dst if dst is not NULL and len >= 1.
565 * May modify src.
566 * Assumes src and dst are non-overlapping.
567 * FIXME: return code probably needed:
568 * - what does AppSearch return if the table values are invalid?
569 * - what if dst is too small?
571 static void ACTION_ExpandAnyPath(MSIPACKAGE *package, WCHAR *src, WCHAR *dst,
572 size_t len)
574 WCHAR *ptr, *deformatted;
576 if (!src || !dst || !len)
578 if (dst) *dst = '\0';
579 return;
582 dst[0] = '\0';
584 /* Ignore the short portion of the path */
585 if ((ptr = strchrW(src, '|')))
586 ptr++;
587 else
588 ptr = src;
590 deformat_string(package, ptr, &deformatted);
591 if (!deformatted || lstrlenW(deformatted) > len - 1)
593 msi_free(deformatted);
594 return;
597 lstrcpyW(dst, deformatted);
598 dst[lstrlenW(deformatted)] = '\0';
599 msi_free(deformatted);
602 /* Sets *matches to whether the file (whose path is filePath) matches the
603 * versions set in sig.
604 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
605 * something else if an install-halting error occurs.
607 static UINT ACTION_FileVersionMatches(const MSISIGNATURE *sig, LPCWSTR filePath,
608 BOOL *matches)
610 UINT rc = ERROR_SUCCESS;
612 *matches = FALSE;
613 if (sig->Languages)
615 FIXME(": need to check version for languages %s\n",
616 debugstr_w(sig->Languages));
618 else
620 DWORD zero, size = GetFileVersionInfoSizeW(filePath, &zero);
622 if (size)
624 LPVOID buf = msi_alloc( size);
626 if (buf)
628 static const WCHAR rootW[] = { '\\',0 };
629 UINT versionLen;
630 LPVOID subBlock = NULL;
632 if (GetFileVersionInfoW(filePath, 0, size, buf))
633 VerQueryValueW(buf, rootW, &subBlock, &versionLen);
634 if (subBlock)
636 VS_FIXEDFILEINFO *info =
637 (VS_FIXEDFILEINFO *)subBlock;
639 TRACE("Comparing file version %d.%d.%d.%d:\n",
640 HIWORD(info->dwFileVersionMS),
641 LOWORD(info->dwFileVersionMS),
642 HIWORD(info->dwFileVersionLS),
643 LOWORD(info->dwFileVersionLS));
644 if (info->dwFileVersionMS < sig->MinVersionMS
645 || (info->dwFileVersionMS == sig->MinVersionMS &&
646 info->dwFileVersionLS < sig->MinVersionLS))
648 TRACE("Less than minimum version %d.%d.%d.%d\n",
649 HIWORD(sig->MinVersionMS),
650 LOWORD(sig->MinVersionMS),
651 HIWORD(sig->MinVersionLS),
652 LOWORD(sig->MinVersionLS));
654 else if (info->dwFileVersionMS < sig->MinVersionMS
655 || (info->dwFileVersionMS == sig->MinVersionMS &&
656 info->dwFileVersionLS < sig->MinVersionLS))
658 TRACE("Greater than minimum version %d.%d.%d.%d\n",
659 HIWORD(sig->MaxVersionMS),
660 LOWORD(sig->MaxVersionMS),
661 HIWORD(sig->MaxVersionLS),
662 LOWORD(sig->MaxVersionLS));
664 else
665 *matches = TRUE;
667 msi_free( buf);
669 else
670 rc = ERROR_OUTOFMEMORY;
673 return rc;
676 /* Sets *matches to whether the file in findData matches that in sig.
677 * fullFilePath is assumed to be the full path of the file specified in
678 * findData, which may be necessary to compare the version.
679 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
680 * something else if an install-halting error occurs.
682 static UINT ACTION_FileMatchesSig(const MSISIGNATURE *sig,
683 const WIN32_FIND_DATAW *findData, LPCWSTR fullFilePath, BOOL *matches)
685 UINT rc = ERROR_SUCCESS;
687 *matches = TRUE;
688 /* assumes the caller has already ensured the filenames match, so check
689 * the other fields..
691 if (sig->MinTime.dwLowDateTime || sig->MinTime.dwHighDateTime)
693 if (findData->ftCreationTime.dwHighDateTime <
694 sig->MinTime.dwHighDateTime ||
695 (findData->ftCreationTime.dwHighDateTime == sig->MinTime.dwHighDateTime
696 && findData->ftCreationTime.dwLowDateTime <
697 sig->MinTime.dwLowDateTime))
698 *matches = FALSE;
700 if (*matches && (sig->MaxTime.dwLowDateTime || sig->MaxTime.dwHighDateTime))
702 if (findData->ftCreationTime.dwHighDateTime >
703 sig->MaxTime.dwHighDateTime ||
704 (findData->ftCreationTime.dwHighDateTime == sig->MaxTime.dwHighDateTime
705 && findData->ftCreationTime.dwLowDateTime >
706 sig->MaxTime.dwLowDateTime))
707 *matches = FALSE;
709 if (*matches && sig->MinSize && findData->nFileSizeLow < sig->MinSize)
710 *matches = FALSE;
711 if (*matches && sig->MaxSize && findData->nFileSizeLow > sig->MaxSize)
712 *matches = FALSE;
713 if (*matches && (sig->MinVersionMS || sig->MinVersionLS ||
714 sig->MaxVersionMS || sig->MaxVersionLS))
715 rc = ACTION_FileVersionMatches(sig, fullFilePath, matches);
716 return rc;
719 /* Recursively searches the directory dir for files that match the signature
720 * sig, up to (depth + 1) levels deep. That is, if depth is 0, it searches dir
721 * (and only dir). If depth is 1, searches dir and its immediate
722 * subdirectories.
723 * Assumes sig->File is not NULL.
724 * Returns ERROR_SUCCESS on success (which may include non-critical errors),
725 * something else on failures which should halt the install.
727 static UINT ACTION_RecurseSearchDirectory(MSIPACKAGE *package, LPWSTR *appValue,
728 MSISIGNATURE *sig, LPCWSTR dir, int depth)
730 HANDLE hFind;
731 WIN32_FIND_DATAW findData;
732 UINT rc = ERROR_SUCCESS;
733 size_t dirLen = lstrlenW(dir), fileLen = lstrlenW(sig->File);
734 WCHAR *buf;
736 static const WCHAR starDotStarW[] = { '*','.','*',0 };
738 TRACE("Searching directory %s for file %s, depth %d\n", debugstr_w(dir),
739 debugstr_w(sig->File), depth);
741 if (depth < 0)
742 return ERROR_INVALID_PARAMETER;
744 *appValue = NULL;
745 /* We need the buffer in both paths below, so go ahead and allocate it
746 * here. Add two because we might need to add a backslash if the dir name
747 * isn't backslash-terminated.
749 buf = msi_alloc( (dirLen + max(fileLen, lstrlenW(starDotStarW)) + 2) * sizeof(WCHAR));
750 if (!buf)
751 return ERROR_OUTOFMEMORY;
753 lstrcpyW(buf, dir);
754 PathAddBackslashW(buf);
755 lstrcatW(buf, sig->File);
757 hFind = FindFirstFileW(buf, &findData);
758 if (hFind != INVALID_HANDLE_VALUE)
760 BOOL matches;
762 /* assuming Signature can't contain wildcards for the file name,
763 * so don't bother with FindNextFileW here.
765 rc = ACTION_FileMatchesSig(sig, &findData, buf, &matches);
766 if (rc == ERROR_SUCCESS && matches)
768 TRACE("found file, returning %s\n", debugstr_w(buf));
769 *appValue = buf;
772 FindClose(hFind);
775 if (rc == ERROR_SUCCESS && !*appValue && depth > 0)
777 lstrcpyW(buf, dir);
778 PathAddBackslashW(buf);
779 lstrcatW(buf, starDotStarW);
781 hFind = FindFirstFileW(buf, &findData);
782 if (hFind != INVALID_HANDLE_VALUE)
784 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
785 rc = ACTION_RecurseSearchDirectory(package, appValue, sig,
786 findData.cFileName,
787 depth - 1);
789 while (rc == ERROR_SUCCESS && !*appValue &&
790 FindNextFileW(hFind, &findData) != 0)
792 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
793 rc = ACTION_RecurseSearchDirectory(package, appValue,
794 sig, findData.cFileName,
795 depth - 1);
798 FindClose(hFind);
802 if (!*appValue)
803 msi_free(buf);
805 return rc;
808 static UINT ACTION_CheckDirectory(MSIPACKAGE *package, LPCWSTR dir,
809 LPWSTR *appValue)
811 DWORD attr = GetFileAttributesW(dir);
813 if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY))
815 TRACE("directory exists, returning %s\n", debugstr_w(dir));
816 *appValue = strdupW(dir);
819 return ERROR_SUCCESS;
822 static BOOL ACTION_IsFullPath(LPCWSTR path)
824 WCHAR first = toupperW(path[0]);
825 BOOL ret;
827 if (first >= 'A' && first <= 'Z' && path[1] == ':')
828 ret = TRUE;
829 else if (path[0] == '\\' && path[1] == '\\')
830 ret = TRUE;
831 else
832 ret = FALSE;
833 return ret;
836 static UINT ACTION_SearchDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
837 LPCWSTR path, int depth, LPWSTR *appValue)
839 UINT rc;
840 DWORD attr;
841 LPWSTR val = NULL;
843 TRACE("%p, %p, %s, %d, %p\n", package, sig, debugstr_w(path), depth,
844 appValue);
846 if (ACTION_IsFullPath(path))
848 if (sig->File)
849 rc = ACTION_RecurseSearchDirectory(package, &val, sig, path, depth);
850 else
852 /* Recursively searching a directory makes no sense when the
853 * directory to search is the thing you're trying to find.
855 rc = ACTION_CheckDirectory(package, path, &val);
858 else
860 WCHAR pathWithDrive[MAX_PATH] = { 'C',':','\\',0 };
861 DWORD drives = GetLogicalDrives();
862 int i;
864 rc = ERROR_SUCCESS;
865 for (i = 0; rc == ERROR_SUCCESS && !val && i < 26; i++)
867 if (!(drives & (1 << i)))
868 continue;
870 pathWithDrive[0] = 'A' + i;
871 if (GetDriveTypeW(pathWithDrive) != DRIVE_FIXED)
872 continue;
874 lstrcpynW(pathWithDrive + 3, path,
875 sizeof(pathWithDrive) / sizeof(pathWithDrive[0]) - 3);
877 if (sig->File)
878 rc = ACTION_RecurseSearchDirectory(package, &val, sig,
879 pathWithDrive, depth);
880 else
881 rc = ACTION_CheckDirectory(package, pathWithDrive, &val);
885 attr = GetFileAttributesW(val);
886 if ((attr & FILE_ATTRIBUTE_DIRECTORY) &&
887 val && val[lstrlenW(val) - 1] != '\\')
889 val = msi_realloc(val, (lstrlenW(val) + 2) * sizeof(WCHAR));
890 if (!val)
891 rc = ERROR_OUTOFMEMORY;
892 else
893 PathAddBackslashW(val);
896 *appValue = val;
898 TRACE("returning %d\n", rc);
899 return rc;
902 static UINT ACTION_AppSearchSigName(MSIPACKAGE *package, LPCWSTR sigName,
903 MSISIGNATURE *sig, LPWSTR *appValue);
905 static UINT ACTION_AppSearchDr(MSIPACKAGE *package, LPWSTR *appValue, MSISIGNATURE *sig)
907 static const WCHAR query[] = {
908 's','e','l','e','c','t',' ','*',' ',
909 'f','r','o','m',' ',
910 'D','r','L','o','c','a','t','o','r',' ',
911 'w','h','e','r','e',' ',
912 'S','i','g','n','a','t','u','r','e','_',' ','=',' ', '\'','%','s','\'',0};
913 LPWSTR parentName = NULL, parent = NULL;
914 WCHAR path[MAX_PATH];
915 WCHAR expanded[MAX_PATH];
916 MSIRECORD *row;
917 int depth;
918 DWORD sz;
919 UINT rc;
921 TRACE("%s\n", debugstr_w(sig->Name));
923 *appValue = NULL;
925 row = MSI_QueryGetRecord( package->db, query, sig->Name );
926 if (!row)
928 TRACE("failed to query DrLocator for %s\n", debugstr_w(sig->Name));
929 return ERROR_SUCCESS;
932 /* check whether parent is set */
933 parentName = msi_dup_record_field(row,2);
934 if (parentName)
936 MSISIGNATURE parentSig;
938 rc = ACTION_AppSearchSigName(package, parentName, &parentSig, &parent);
939 ACTION_FreeSignature(&parentSig);
940 msi_free(parentName);
943 sz = MAX_PATH;
944 MSI_RecordGetStringW(row, 3, path, &sz);
946 if (MSI_RecordIsNull(row,4))
947 depth = 0;
948 else
949 depth = MSI_RecordGetInteger(row,4);
951 ACTION_ExpandAnyPath(package, path, expanded, MAX_PATH);
953 if (parent)
955 strcpyW(path, parent);
956 strcatW(path, expanded);
958 else
959 strcpyW(path, expanded);
961 PathAddBackslashW(path);
963 rc = ACTION_SearchDirectory(package, sig, path, depth, appValue);
965 msi_free(parent);
966 msiobj_release(&row->hdr);
968 TRACE("returning %d\n", rc);
969 return rc;
972 static UINT ACTION_AppSearchSigName(MSIPACKAGE *package, LPCWSTR sigName,
973 MSISIGNATURE *sig, LPWSTR *appValue)
975 UINT rc;
977 *appValue = NULL;
978 rc = ACTION_AppSearchGetSignature(package, sig, sigName);
979 if (rc == ERROR_SUCCESS)
981 rc = ACTION_AppSearchComponents(package, appValue, sig);
982 if (rc == ERROR_SUCCESS && !*appValue)
984 rc = ACTION_AppSearchReg(package, appValue, sig);
985 if (rc == ERROR_SUCCESS && !*appValue)
987 rc = ACTION_AppSearchIni(package, appValue, sig);
988 if (rc == ERROR_SUCCESS && !*appValue)
989 rc = ACTION_AppSearchDr(package, appValue, sig);
993 return rc;
996 static UINT iterate_appsearch(MSIRECORD *row, LPVOID param)
998 MSIPACKAGE *package = param;
999 LPWSTR propName, sigName, value = NULL;
1000 MSISIGNATURE sig;
1001 UINT r;
1003 /* get property and signature */
1004 propName = msi_dup_record_field(row,1);
1005 sigName = msi_dup_record_field(row,2);
1007 TRACE("%s %s\n", debugstr_w(propName), debugstr_w(sigName));
1009 r = 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);
1019 return r;
1022 UINT ACTION_AppSearch(MSIPACKAGE *package)
1024 static const WCHAR query[] = {
1025 's','e','l','e','c','t',' ','*',' ',
1026 'f','r','o','m',' ',
1027 'A','p','p','S','e','a','r','c','h',0};
1028 MSIQUERY *view = NULL;
1029 UINT r;
1031 r = MSI_OpenQuery( package->db, &view, query );
1032 if (r != ERROR_SUCCESS)
1033 return ERROR_SUCCESS;
1035 r = MSI_IterateRecords( view, NULL, iterate_appsearch, package );
1036 msiobj_release( &view->hdr );
1038 return r;
1041 static UINT ITERATE_CCPSearch(MSIRECORD *row, LPVOID param)
1043 MSIPACKAGE *package = param;
1044 LPCWSTR signature;
1045 LPWSTR value = NULL;
1046 MSISIGNATURE sig;
1047 UINT r = ERROR_SUCCESS;
1049 static const WCHAR success[] = {'C','C','P','_','S','u','c','c','e','s','s',0};
1050 static const WCHAR one[] = {'1',0};
1052 signature = MSI_RecordGetString(row, 1);
1054 TRACE("%s\n", debugstr_w(signature));
1056 ACTION_AppSearchSigName(package, signature, &sig, &value);
1057 if (value)
1059 TRACE("Found signature %s\n", debugstr_w(signature));
1060 MSI_SetPropertyW(package, success, one);
1061 msi_free(value);
1062 r = ERROR_NO_MORE_ITEMS;
1065 ACTION_FreeSignature(&sig);
1067 return r;
1070 UINT ACTION_CCPSearch(MSIPACKAGE *package)
1072 static const WCHAR query[] = {
1073 's','e','l','e','c','t',' ','*',' ',
1074 'f','r','o','m',' ',
1075 'C','C','P','S','e','a','r','c','h',0};
1076 MSIQUERY *view = NULL;
1077 UINT r;
1079 r = MSI_OpenQuery(package->db, &view, query);
1080 if (r != ERROR_SUCCESS)
1081 return ERROR_SUCCESS;
1083 r = MSI_IterateRecords(view, NULL, ITERATE_CCPSearch, package);
1084 msiobj_release(&view->hdr);
1086 return r;