winspool/tests: Add tests for GetFormA().
[wine.git] / dlls / msi / appsearch.c
blobabd3c9d3fd9f12064a9ebf7bb43c65786be3075e
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/debug.h"
33 #include "msipriv.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msi);
37 typedef struct tagMSISIGNATURE
39 LPCWSTR Name; /* NOT owned by this structure */
40 LPWSTR File;
41 DWORD MinVersionMS;
42 DWORD MinVersionLS;
43 DWORD MaxVersionMS;
44 DWORD MaxVersionLS;
45 DWORD MinSize;
46 DWORD MaxSize;
47 FILETIME MinTime;
48 FILETIME MaxTime;
49 LPWSTR Languages;
50 }MSISIGNATURE;
52 void msi_parse_version_string(LPCWSTR verStr, PDWORD ms, PDWORD ls)
54 const WCHAR *ptr;
55 int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
57 x1 = wcstol(verStr, NULL, 10);
58 ptr = wcschr(verStr, '.');
59 if (ptr)
61 x2 = wcstol(ptr + 1, NULL, 10);
62 ptr = wcschr(ptr + 1, '.');
64 if (ptr)
66 x3 = wcstol(ptr + 1, NULL, 10);
67 ptr = wcschr(ptr + 1, '.');
69 if (ptr)
70 x4 = wcstol(ptr + 1, NULL, 10);
71 /* FIXME: byte-order dependent? */
72 *ms = x1 << 16 | x2;
73 if (ls) *ls = x3 << 16 | x4;
76 /* Fills in sig with the values from the Signature table, where name is the
77 * signature to find. Upon return, sig->File will be NULL if the record is not
78 * found, and not NULL if it is found.
79 * Warning: clears all fields in sig!
80 * Returns ERROR_SUCCESS upon success (where not finding the record counts as
81 * success), something else on error.
83 static UINT get_signature( MSIPACKAGE *package, MSISIGNATURE *sig, const WCHAR *name )
85 WCHAR *minVersion, *maxVersion, *p;
86 MSIRECORD *row;
87 DWORD time;
89 TRACE("package %p, sig %p\n", package, sig);
91 memset(sig, 0, sizeof(*sig));
92 sig->Name = name;
93 row = MSI_QueryGetRecord( package->db, L"SELECT * FROM `Signature` WHERE `Signature` = '%s'", name );
94 if (!row)
96 TRACE("failed to query signature for %s\n", debugstr_w(name));
97 return ERROR_SUCCESS;
100 /* get properties */
101 sig->File = msi_dup_record_field(row,2);
102 if ((p = wcschr(sig->File, '|')))
104 p++;
105 memmove(sig->File, p, (lstrlenW(p) + 1) * sizeof(WCHAR));
108 minVersion = msi_dup_record_field(row,3);
109 if (minVersion)
111 msi_parse_version_string( minVersion, &sig->MinVersionMS, &sig->MinVersionLS );
112 msi_free( minVersion );
114 maxVersion = msi_dup_record_field(row,4);
115 if (maxVersion)
117 msi_parse_version_string( maxVersion, &sig->MaxVersionMS, &sig->MaxVersionLS );
118 msi_free( maxVersion );
120 sig->MinSize = MSI_RecordGetInteger(row,5);
121 if (sig->MinSize == MSI_NULL_INTEGER)
122 sig->MinSize = 0;
123 sig->MaxSize = MSI_RecordGetInteger(row,6);
124 if (sig->MaxSize == MSI_NULL_INTEGER)
125 sig->MaxSize = 0;
126 sig->Languages = msi_dup_record_field(row,9);
127 time = MSI_RecordGetInteger(row,7);
128 if (time != MSI_NULL_INTEGER)
129 DosDateTimeToFileTime(HIWORD(time), LOWORD(time), &sig->MinTime);
130 time = MSI_RecordGetInteger(row,8);
131 if (time != MSI_NULL_INTEGER)
132 DosDateTimeToFileTime(HIWORD(time), LOWORD(time), &sig->MaxTime);
134 TRACE("Found file name %s for Signature_ %s;\n",
135 debugstr_w(sig->File), debugstr_w(name));
136 TRACE("MinVersion is %d.%d.%d.%d\n", HIWORD(sig->MinVersionMS),
137 LOWORD(sig->MinVersionMS), HIWORD(sig->MinVersionLS),
138 LOWORD(sig->MinVersionLS));
139 TRACE("MaxVersion is %d.%d.%d.%d\n", HIWORD(sig->MaxVersionMS),
140 LOWORD(sig->MaxVersionMS), HIWORD(sig->MaxVersionLS),
141 LOWORD(sig->MaxVersionLS));
142 TRACE("MinSize is %d, MaxSize is %d;\n", sig->MinSize, sig->MaxSize);
143 TRACE("Languages is %s\n", debugstr_w(sig->Languages));
145 msiobj_release( &row->hdr );
147 return ERROR_SUCCESS;
150 /* Frees any memory allocated in sig */
151 static void free_signature( MSISIGNATURE *sig )
153 msi_free(sig->File);
154 msi_free(sig->Languages);
157 static WCHAR *search_file( MSIPACKAGE *package, WCHAR *path, MSISIGNATURE *sig )
159 VS_FIXEDFILEINFO *info;
160 DWORD attr, size;
161 LPWSTR val = NULL;
162 LPBYTE buffer;
164 if (!sig->File)
166 PathRemoveFileSpecW(path);
167 PathAddBackslashW(path);
169 attr = msi_get_file_attributes( package, path );
170 if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY))
171 return strdupW(path);
173 return NULL;
176 attr = msi_get_file_attributes( package, path );
177 if (attr == INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY))
178 return NULL;
180 size = msi_get_file_version_info( package, path, 0, NULL );
181 if (!size)
182 return strdupW(path);
184 buffer = msi_alloc(size);
185 if (!buffer)
186 return NULL;
188 size = msi_get_file_version_info( package, path, size, buffer );
189 if (!size)
190 goto done;
192 if (!VerQueryValueW(buffer, L"\\", (LPVOID)&info, &size) || !info)
193 goto done;
195 if (sig->MinVersionLS || sig->MinVersionMS)
197 if (info->dwFileVersionMS < sig->MinVersionMS)
198 goto done;
200 if (info->dwFileVersionMS == sig->MinVersionMS &&
201 info->dwFileVersionLS < sig->MinVersionLS)
202 goto done;
205 if (sig->MaxVersionLS || sig->MaxVersionMS)
207 if (info->dwFileVersionMS > sig->MaxVersionMS)
208 goto done;
210 if (info->dwFileVersionMS == sig->MaxVersionMS &&
211 info->dwFileVersionLS > sig->MaxVersionLS)
212 goto done;
215 val = strdupW(path);
217 done:
218 msi_free(buffer);
219 return val;
222 static UINT search_components( MSIPACKAGE *package, WCHAR **appValue, MSISIGNATURE *sig )
224 MSIRECORD *row, *rec;
225 LPCWSTR signature, guid;
226 BOOL sigpresent = TRUE;
227 BOOL isdir;
228 UINT type;
229 WCHAR path[MAX_PATH];
230 DWORD size = MAX_PATH;
231 LPWSTR ptr;
232 DWORD attr;
234 TRACE("%s\n", debugstr_w(sig->Name));
236 *appValue = NULL;
238 row = MSI_QueryGetRecord(package->db, L"SELECT * FROM `CompLocator` WHERE `Signature_` = '%s'", sig->Name);
239 if (!row)
241 TRACE("failed to query CompLocator for %s\n", debugstr_w(sig->Name));
242 return ERROR_SUCCESS;
245 signature = MSI_RecordGetString(row, 1);
246 guid = MSI_RecordGetString(row, 2);
247 type = MSI_RecordGetInteger(row, 3);
249 rec = MSI_QueryGetRecord(package->db, L"SELECT * FROM `Signature` WHERE `Signature` = '%s'", signature);
250 if (!rec)
251 sigpresent = FALSE;
253 *path = '\0';
254 MsiLocateComponentW(guid, path, &size);
255 if (!*path)
256 goto done;
258 attr = msi_get_file_attributes( package, path );
259 if (attr == INVALID_FILE_ATTRIBUTES)
260 goto done;
262 isdir = (attr & FILE_ATTRIBUTE_DIRECTORY);
264 if (type != msidbLocatorTypeDirectory && sigpresent && !isdir)
266 *appValue = search_file( package, path, sig );
268 else if (!sigpresent && (type != msidbLocatorTypeDirectory || isdir))
270 if (type == msidbLocatorTypeFileName)
272 ptr = wcsrchr(path, '\\');
273 *(ptr + 1) = '\0';
275 else
276 PathAddBackslashW(path);
278 *appValue = strdupW(path);
280 else if (sigpresent)
282 PathAddBackslashW(path);
283 lstrcatW(path, MSI_RecordGetString(rec, 2));
285 attr = msi_get_file_attributes( package, path );
286 if (attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY))
287 *appValue = strdupW(path);
290 done:
291 if (rec) msiobj_release(&rec->hdr);
292 msiobj_release(&row->hdr);
293 return ERROR_SUCCESS;
296 static void convert_reg_value( DWORD regType, const BYTE *value, DWORD sz, WCHAR **appValue )
298 LPWSTR ptr;
299 DWORD i;
301 switch (regType)
303 case REG_SZ:
304 if (*(LPCWSTR)value == '#')
306 /* escape leading pound with another */
307 *appValue = msi_alloc(sz + sizeof(WCHAR));
308 (*appValue)[0] = '#';
309 lstrcpyW(*appValue + 1, (LPCWSTR)value);
311 else
313 *appValue = msi_alloc(sz);
314 lstrcpyW(*appValue, (LPCWSTR)value);
316 break;
317 case REG_DWORD:
318 /* 7 chars for digits, 1 for NULL, 1 for #, and 1 for sign
319 * char if needed
321 *appValue = msi_alloc(10 * sizeof(WCHAR));
322 swprintf(*appValue, 10, L"#%d", *(const DWORD *)value);
323 break;
324 case REG_EXPAND_SZ:
325 sz = ExpandEnvironmentStringsW((LPCWSTR)value, NULL, 0);
326 *appValue = msi_alloc(sz * sizeof(WCHAR));
327 ExpandEnvironmentStringsW((LPCWSTR)value, *appValue, sz);
328 break;
329 case REG_BINARY:
330 /* #x<nibbles>\0 */
331 *appValue = msi_alloc((sz * 2 + 3) * sizeof(WCHAR));
332 lstrcpyW(*appValue, L"#x");
333 ptr = *appValue + lstrlenW(L"#x");
334 for (i = 0; i < sz; i++, ptr += 2)
335 swprintf(ptr, 3, L"%02X", value[i]);
336 break;
337 default:
338 WARN("unimplemented for values of type %d\n", regType);
339 *appValue = NULL;
343 static UINT search_directory( MSIPACKAGE *, MSISIGNATURE *, const WCHAR *, int, WCHAR ** );
345 static UINT search_reg( MSIPACKAGE *package, WCHAR **appValue, MSISIGNATURE *sig )
347 const WCHAR *keyPath, *valueName;
348 WCHAR *deformatted = NULL, *ptr = NULL, *end;
349 int root, type;
350 REGSAM access = KEY_READ;
351 HKEY rootKey, key = NULL;
352 DWORD sz = 0, regType;
353 LPBYTE value = NULL;
354 MSIRECORD *row;
355 UINT rc;
357 TRACE("%s\n", debugstr_w(sig->Name));
359 *appValue = NULL;
361 row = MSI_QueryGetRecord( package->db, L"SELECT * FROM `RegLocator` WHERE `Signature_` = '%s'", sig->Name );
362 if (!row)
364 TRACE("failed to query RegLocator for %s\n", debugstr_w(sig->Name));
365 return ERROR_SUCCESS;
368 root = MSI_RecordGetInteger(row, 2);
369 keyPath = MSI_RecordGetString(row, 3);
370 valueName = MSI_RecordGetString(row, 4);
371 type = MSI_RecordGetInteger(row, 5);
373 deformat_string(package, keyPath, &deformatted);
375 switch (root)
377 case msidbRegistryRootClassesRoot:
378 rootKey = HKEY_CLASSES_ROOT;
379 break;
380 case msidbRegistryRootCurrentUser:
381 rootKey = HKEY_CURRENT_USER;
382 break;
383 case msidbRegistryRootLocalMachine:
384 rootKey = HKEY_LOCAL_MACHINE;
385 if (type & msidbLocatorType64bit) access |= KEY_WOW64_64KEY;
386 else access |= KEY_WOW64_32KEY;
387 break;
388 case msidbRegistryRootUsers:
389 rootKey = HKEY_USERS;
390 break;
391 default:
392 WARN("Unknown root key %d\n", root);
393 goto end;
396 rc = RegOpenKeyExW( rootKey, deformatted, 0, access, &key );
397 if (rc)
399 TRACE("RegOpenKeyExW returned %d\n", rc);
400 goto end;
403 msi_free(deformatted);
404 deformat_string(package, valueName, &deformatted);
406 rc = RegQueryValueExW(key, deformatted, NULL, NULL, NULL, &sz);
407 if (rc)
409 TRACE("RegQueryValueExW returned %d\n", rc);
410 goto end;
412 /* FIXME: sanity-check sz before allocating (is there an upper-limit
413 * on the value of a property?)
415 value = msi_alloc( sz );
416 rc = RegQueryValueExW(key, deformatted, NULL, &regType, value, &sz);
417 if (rc)
419 TRACE("RegQueryValueExW returned %d\n", rc);
420 goto end;
423 /* bail out if the registry key is empty */
424 if (sz == 0)
425 goto end;
427 /* expand if needed */
428 if (regType == REG_EXPAND_SZ)
430 sz = ExpandEnvironmentStringsW((LPCWSTR)value, NULL, 0);
431 if (sz)
433 LPWSTR buf = msi_alloc(sz * sizeof(WCHAR));
434 ExpandEnvironmentStringsW((LPCWSTR)value, buf, sz);
435 msi_free(value);
436 value = (LPBYTE)buf;
440 if ((regType == REG_SZ || regType == REG_EXPAND_SZ) &&
441 (ptr = wcschr((LPWSTR)value, '"')) && (end = wcschr(++ptr, '"')))
442 *end = '\0';
443 else
444 ptr = (LPWSTR)value;
446 switch (type & 0x0f)
448 case msidbLocatorTypeDirectory:
449 search_directory( package, sig, ptr, 0, appValue );
450 break;
451 case msidbLocatorTypeFileName:
452 *appValue = search_file( package, ptr, sig );
453 break;
454 case msidbLocatorTypeRawValue:
455 convert_reg_value( regType, value, sz, appValue );
456 break;
457 default:
458 FIXME("unimplemented for type %d (key path %s, value %s)\n",
459 type, debugstr_w(keyPath), debugstr_w(valueName));
461 end:
462 msi_free( value );
463 RegCloseKey( key );
464 msi_free( deformatted );
466 msiobj_release(&row->hdr);
467 return ERROR_SUCCESS;
470 static LPWSTR get_ini_field(LPWSTR buf, int field)
472 LPWSTR beg, end;
473 int i = 1;
475 if (field == 0)
476 return strdupW(buf);
478 beg = buf;
479 while ((end = wcschr(beg, ',')) && i < field)
481 beg = end + 1;
482 while (*beg == ' ')
483 beg++;
485 i++;
488 end = wcschr(beg, ',');
489 if (!end)
490 end = beg + lstrlenW(beg);
492 *end = '\0';
493 return strdupW(beg);
496 static UINT search_ini( MSIPACKAGE *package, WCHAR **appValue, MSISIGNATURE *sig )
498 MSIRECORD *row;
499 LPWSTR fileName, section, key;
500 int field, type;
501 WCHAR buf[MAX_PATH];
503 TRACE("%s\n", debugstr_w(sig->Name));
505 *appValue = NULL;
507 row = MSI_QueryGetRecord( package->db, L"SELECT * FROM `IniLocator` WHERE `Signature_` = '%s'", sig->Name );
508 if (!row)
510 TRACE("failed to query IniLocator for %s\n", debugstr_w(sig->Name));
511 return ERROR_SUCCESS;
514 fileName = msi_dup_record_field(row, 2);
515 section = msi_dup_record_field(row, 3);
516 key = msi_dup_record_field(row, 4);
517 field = MSI_RecordGetInteger(row, 5);
518 type = MSI_RecordGetInteger(row, 6);
519 if (field == MSI_NULL_INTEGER)
520 field = 0;
521 if (type == MSI_NULL_INTEGER)
522 type = 0;
524 GetPrivateProfileStringW(section, key, NULL, buf, MAX_PATH, fileName);
525 if (buf[0])
527 switch (type & 0x0f)
529 case msidbLocatorTypeDirectory:
530 search_directory( package, sig, buf, 0, appValue );
531 break;
532 case msidbLocatorTypeFileName:
533 *appValue = search_file( package, buf, sig );
534 break;
535 case msidbLocatorTypeRawValue:
536 *appValue = get_ini_field(buf, field);
537 break;
541 msi_free(fileName);
542 msi_free(section);
543 msi_free(key);
545 msiobj_release(&row->hdr);
547 return ERROR_SUCCESS;
550 /* Expands the value in src into a path without property names and only
551 * containing long path names into dst. Replaces at most len characters of dst,
552 * and always NULL-terminates dst if dst is not NULL and len >= 1.
553 * May modify src.
554 * Assumes src and dst are non-overlapping.
555 * FIXME: return code probably needed:
556 * - what does AppSearch return if the table values are invalid?
557 * - what if dst is too small?
559 static void expand_any_path( MSIPACKAGE *package, WCHAR *src, WCHAR *dst, size_t len )
561 WCHAR *ptr, *deformatted;
563 if (!src || !dst || !len)
565 if (dst) *dst = '\0';
566 return;
569 dst[0] = '\0';
571 /* Ignore the short portion of the path */
572 if ((ptr = wcschr(src, '|')))
573 ptr++;
574 else
575 ptr = src;
577 deformat_string(package, ptr, &deformatted);
578 if (!deformatted || lstrlenW(deformatted) > len - 1)
580 msi_free(deformatted);
581 return;
584 lstrcpyW(dst, deformatted);
585 dst[lstrlenW(deformatted)] = '\0';
586 msi_free(deformatted);
589 static LANGID *parse_languages( const WCHAR *languages, DWORD *num_ids )
591 UINT i, count = 1;
592 WCHAR *str = strdupW( languages ), *p, *q;
593 LANGID *ret;
595 if (!str) return NULL;
596 for (p = q = str; (q = wcschr( q, ',' )); q++) count++;
598 if (!(ret = msi_alloc( count * sizeof(LANGID) )))
600 msi_free( str );
601 return NULL;
603 i = 0;
604 while (*p)
606 q = wcschr( p, ',' );
607 if (q) *q = 0;
608 ret[i] = wcstol( p, NULL, 10 );
609 if (!q) break;
610 p = q + 1;
611 i++;
613 msi_free( str );
614 *num_ids = count;
615 return ret;
618 static BOOL match_languages( const void *version, const WCHAR *languages )
620 struct lang
622 USHORT id;
623 USHORT codepage;
624 } *lang;
625 DWORD len, num_ids, i, j;
626 BOOL found = FALSE;
627 LANGID *ids;
629 if (!languages || !languages[0]) return TRUE;
630 if (!VerQueryValueW( version, L"\\VarFileInfo\\Translation", (void **)&lang, &len )) return FALSE;
631 if (!(ids = parse_languages( languages, &num_ids ))) return FALSE;
633 for (i = 0; i < num_ids; i++)
635 found = FALSE;
636 for (j = 0; j < len / sizeof(struct lang); j++)
638 if (!ids[i] || ids[i] == lang[j].id) found = TRUE;
640 if (!found) goto done;
643 done:
644 msi_free( ids );
645 return found;
648 /* Sets *matches to whether the file (whose path is filePath) matches the
649 * versions set in sig.
650 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
651 * something else if an install-halting error occurs.
653 static UINT file_version_matches( MSIPACKAGE *package, const MSISIGNATURE *sig, const WCHAR *filePath,
654 BOOL *matches )
656 UINT len;
657 void *version;
658 VS_FIXEDFILEINFO *info = NULL;
659 DWORD size = msi_get_file_version_info( package, filePath, 0, NULL );
661 *matches = FALSE;
663 if (!size) return ERROR_SUCCESS;
664 if (!(version = msi_alloc( size ))) return ERROR_OUTOFMEMORY;
666 if (msi_get_file_version_info( package, filePath, size, version ))
667 VerQueryValueW( version, L"\\", (void **)&info, &len );
669 if (info)
671 TRACE("comparing file version %d.%d.%d.%d:\n",
672 HIWORD(info->dwFileVersionMS),
673 LOWORD(info->dwFileVersionMS),
674 HIWORD(info->dwFileVersionLS),
675 LOWORD(info->dwFileVersionLS));
676 if (info->dwFileVersionMS < sig->MinVersionMS
677 || (info->dwFileVersionMS == sig->MinVersionMS &&
678 info->dwFileVersionLS < sig->MinVersionLS))
680 TRACE("less than minimum version %d.%d.%d.%d\n",
681 HIWORD(sig->MinVersionMS),
682 LOWORD(sig->MinVersionMS),
683 HIWORD(sig->MinVersionLS),
684 LOWORD(sig->MinVersionLS));
686 else if ((sig->MaxVersionMS || sig->MaxVersionLS) &&
687 (info->dwFileVersionMS > sig->MaxVersionMS ||
688 (info->dwFileVersionMS == sig->MaxVersionMS &&
689 info->dwFileVersionLS > sig->MaxVersionLS)))
691 TRACE("greater than maximum version %d.%d.%d.%d\n",
692 HIWORD(sig->MaxVersionMS),
693 LOWORD(sig->MaxVersionMS),
694 HIWORD(sig->MaxVersionLS),
695 LOWORD(sig->MaxVersionLS));
697 else if (!match_languages( version, sig->Languages ))
699 TRACE("languages %s not supported\n", debugstr_w( sig->Languages ));
701 else *matches = TRUE;
703 msi_free( version );
704 return ERROR_SUCCESS;
707 /* Sets *matches to whether the file in findData matches that in sig.
708 * fullFilePath is assumed to be the full path of the file specified in
709 * findData, which may be necessary to compare the version.
710 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
711 * something else if an install-halting error occurs.
713 static UINT file_matches_sig( MSIPACKAGE *package, const MSISIGNATURE *sig, const WIN32_FIND_DATAW *findData,
714 const WCHAR *fullFilePath, BOOL *matches )
716 UINT rc = ERROR_SUCCESS;
718 *matches = TRUE;
719 /* assumes the caller has already ensured the filenames match, so check
720 * the other fields..
722 if (sig->MinTime.dwLowDateTime || sig->MinTime.dwHighDateTime)
724 if (findData->ftCreationTime.dwHighDateTime <
725 sig->MinTime.dwHighDateTime ||
726 (findData->ftCreationTime.dwHighDateTime == sig->MinTime.dwHighDateTime
727 && findData->ftCreationTime.dwLowDateTime <
728 sig->MinTime.dwLowDateTime))
729 *matches = FALSE;
731 if (*matches && (sig->MaxTime.dwLowDateTime || sig->MaxTime.dwHighDateTime))
733 if (findData->ftCreationTime.dwHighDateTime >
734 sig->MaxTime.dwHighDateTime ||
735 (findData->ftCreationTime.dwHighDateTime == sig->MaxTime.dwHighDateTime
736 && findData->ftCreationTime.dwLowDateTime >
737 sig->MaxTime.dwLowDateTime))
738 *matches = FALSE;
740 if (*matches && sig->MinSize && findData->nFileSizeLow < sig->MinSize)
741 *matches = FALSE;
742 if (*matches && sig->MaxSize && findData->nFileSizeLow > sig->MaxSize)
743 *matches = FALSE;
744 if (*matches && (sig->MinVersionMS || sig->MinVersionLS ||
745 sig->MaxVersionMS || sig->MaxVersionLS))
746 rc = file_version_matches( package, sig, fullFilePath, matches );
747 return rc;
750 /* Recursively searches the directory dir for files that match the signature
751 * sig, up to (depth + 1) levels deep. That is, if depth is 0, it searches dir
752 * (and only dir). If depth is 1, searches dir and its immediate
753 * subdirectories.
754 * Assumes sig->File is not NULL.
755 * Returns ERROR_SUCCESS on success (which may include non-critical errors),
756 * something else on failures which should halt the install.
758 static UINT recurse_search_directory( MSIPACKAGE *package, WCHAR **appValue, MSISIGNATURE *sig, const WCHAR *dir,
759 int depth )
761 HANDLE hFind;
762 WIN32_FIND_DATAW findData;
763 UINT rc = ERROR_SUCCESS;
764 size_t dirLen = lstrlenW(dir), fileLen = lstrlenW(sig->File);
765 WCHAR subpath[MAX_PATH];
766 WCHAR *buf;
767 DWORD len;
769 TRACE("Searching directory %s for file %s, depth %d\n", debugstr_w(dir), debugstr_w(sig->File), depth);
771 if (depth < 0)
772 return ERROR_SUCCESS;
774 *appValue = NULL;
775 /* We need the buffer in both paths below, so go ahead and allocate it
776 * here. Add two because we might need to add a backslash if the dir name
777 * isn't backslash-terminated.
779 len = dirLen + max(fileLen, lstrlenW(L"*.*")) + 2;
780 buf = msi_alloc(len * sizeof(WCHAR));
781 if (!buf)
782 return ERROR_OUTOFMEMORY;
784 lstrcpyW(buf, dir);
785 PathAddBackslashW(buf);
786 lstrcatW(buf, sig->File);
788 hFind = msi_find_first_file( package, buf, &findData );
789 if (hFind != INVALID_HANDLE_VALUE)
791 if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
793 BOOL matches;
795 rc = file_matches_sig( package, sig, &findData, buf, &matches );
796 if (rc == ERROR_SUCCESS && matches)
798 TRACE("found file, returning %s\n", debugstr_w(buf));
799 *appValue = buf;
802 FindClose(hFind);
805 if (rc == ERROR_SUCCESS && !*appValue)
807 lstrcpyW(buf, dir);
808 PathAddBackslashW(buf);
809 lstrcatW(buf, L"*.*");
811 hFind = msi_find_first_file( package, buf, &findData );
812 if (hFind != INVALID_HANDLE_VALUE)
814 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
815 wcscmp( findData.cFileName, L"." ) &&
816 wcscmp( findData.cFileName, L".." ))
818 lstrcpyW(subpath, dir);
819 PathAppendW(subpath, findData.cFileName);
820 rc = recurse_search_directory( package, appValue, sig, subpath, depth - 1 );
823 while (rc == ERROR_SUCCESS && !*appValue && msi_find_next_file( package, hFind, &findData ))
825 if (!wcscmp( findData.cFileName, L"." ) ||
826 !wcscmp( findData.cFileName, L".." ))
827 continue;
829 lstrcpyW(subpath, dir);
830 PathAppendW(subpath, findData.cFileName);
831 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
832 rc = recurse_search_directory( package, appValue, sig, subpath, depth - 1 );
835 FindClose(hFind);
839 if (*appValue != buf)
840 msi_free(buf);
842 return rc;
845 static UINT check_directory( MSIPACKAGE *package, const WCHAR *dir, WCHAR **appValue )
847 DWORD attr = msi_get_file_attributes( package, dir );
849 if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY))
851 TRACE("directory exists, returning %s\n", debugstr_w(dir));
852 *appValue = strdupW(dir);
855 return ERROR_SUCCESS;
858 static BOOL is_full_path( const WCHAR *path )
860 WCHAR first = towupper(path[0]);
861 BOOL ret;
863 if (first >= 'A' && first <= 'Z' && path[1] == ':')
864 ret = TRUE;
865 else if (path[0] == '\\' && path[1] == '\\')
866 ret = TRUE;
867 else
868 ret = FALSE;
869 return ret;
872 static UINT search_directory( MSIPACKAGE *package, MSISIGNATURE *sig, const WCHAR *path, int depth, WCHAR **appValue )
874 UINT rc;
875 DWORD attr;
876 LPWSTR val = NULL;
878 TRACE("%p, %p, %s, %d, %p\n", package, sig, debugstr_w(path), depth, appValue);
880 if (is_full_path( path ))
882 if (sig->File)
883 rc = recurse_search_directory( package, &val, sig, path, depth );
884 else
886 /* Recursively searching a directory makes no sense when the
887 * directory to search is the thing you're trying to find.
889 rc = check_directory( package, path, &val );
892 else
894 WCHAR pathWithDrive[MAX_PATH] = { 'C',':','\\',0 };
895 DWORD drives = GetLogicalDrives();
896 int i;
898 rc = ERROR_SUCCESS;
899 for (i = 0; rc == ERROR_SUCCESS && !val && i < 26; i++)
901 if (!(drives & (1 << i)))
902 continue;
904 pathWithDrive[0] = 'A' + i;
905 if (GetDriveTypeW(pathWithDrive) != DRIVE_FIXED)
906 continue;
908 lstrcpynW(pathWithDrive + 3, path, ARRAY_SIZE(pathWithDrive) - 3);
910 if (sig->File)
911 rc = recurse_search_directory( package, &val, sig, pathWithDrive, depth );
912 else
913 rc = check_directory( package, pathWithDrive, &val );
917 attr = msi_get_file_attributes( package, val );
918 if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY) &&
919 val && val[lstrlenW(val) - 1] != '\\')
921 val = msi_realloc(val, (lstrlenW(val) + 2) * sizeof(WCHAR));
922 if (!val)
923 rc = ERROR_OUTOFMEMORY;
924 else
925 PathAddBackslashW(val);
928 *appValue = val;
930 TRACE("returning %d\n", rc);
931 return rc;
934 static UINT search_sig_name( MSIPACKAGE *, const WCHAR *, MSISIGNATURE *, WCHAR ** );
936 static UINT search_dr( MSIPACKAGE *package, WCHAR **appValue, MSISIGNATURE *sig )
938 LPWSTR parent = NULL;
939 LPCWSTR parentName;
940 WCHAR path[MAX_PATH];
941 WCHAR expanded[MAX_PATH];
942 MSIRECORD *row;
943 int depth;
944 DWORD sz, attr;
945 UINT rc;
947 TRACE("%s\n", debugstr_w(sig->Name));
949 *appValue = NULL;
951 row = MSI_QueryGetRecord( package->db, L"SELECT * FROM `DrLocator` WHERE `Signature_` = '%s'", sig->Name );
952 if (!row)
954 TRACE("failed to query DrLocator for %s\n", debugstr_w(sig->Name));
955 return ERROR_SUCCESS;
958 /* check whether parent is set */
959 parentName = MSI_RecordGetString(row, 2);
960 if (parentName)
962 MSISIGNATURE parentSig;
964 search_sig_name( package, parentName, &parentSig, &parent );
965 free_signature( &parentSig );
966 if (!parent)
968 msiobj_release(&row->hdr);
969 return ERROR_SUCCESS;
973 sz = MAX_PATH;
974 MSI_RecordGetStringW(row, 3, path, &sz);
976 if (MSI_RecordIsNull(row,4))
977 depth = 0;
978 else
979 depth = MSI_RecordGetInteger(row,4);
981 if (sz)
982 expand_any_path( package, path, expanded, MAX_PATH );
983 else
984 lstrcpyW(expanded, path);
986 if (parent)
988 attr = msi_get_file_attributes( package, parent );
989 if (attr != INVALID_FILE_ATTRIBUTES &&
990 !(attr & FILE_ATTRIBUTE_DIRECTORY))
992 PathRemoveFileSpecW(parent);
993 PathAddBackslashW(parent);
996 lstrcpyW(path, parent);
997 lstrcatW(path, expanded);
999 else if (sz) lstrcpyW(path, expanded);
1001 PathAddBackslashW(path);
1003 rc = search_directory( package, sig, path, depth, appValue );
1005 msi_free(parent);
1006 msiobj_release(&row->hdr);
1007 TRACE("returning %d\n", rc);
1008 return rc;
1011 static UINT search_sig_name( MSIPACKAGE *package, const WCHAR *sigName, MSISIGNATURE *sig, WCHAR **appValue )
1013 UINT rc;
1015 *appValue = NULL;
1016 rc = get_signature( package, sig, sigName );
1017 if (rc == ERROR_SUCCESS)
1019 rc = search_components( package, appValue, sig );
1020 if (rc == ERROR_SUCCESS && !*appValue)
1022 rc = search_reg( package, appValue, sig );
1023 if (rc == ERROR_SUCCESS && !*appValue)
1025 rc = search_ini( package, appValue, sig );
1026 if (rc == ERROR_SUCCESS && !*appValue)
1027 rc = search_dr( package, appValue, sig );
1031 return rc;
1034 static UINT ITERATE_AppSearch(MSIRECORD *row, LPVOID param)
1036 MSIPACKAGE *package = param;
1037 LPCWSTR propName, sigName;
1038 LPWSTR value = NULL;
1039 MSISIGNATURE sig;
1040 MSIRECORD *uirow;
1041 UINT r;
1043 /* get property and signature */
1044 propName = MSI_RecordGetString(row, 1);
1045 sigName = MSI_RecordGetString(row, 2);
1047 TRACE("%s %s\n", debugstr_w(propName), debugstr_w(sigName));
1049 r = search_sig_name( package, sigName, &sig, &value );
1050 if (value)
1052 r = msi_set_property( package->db, propName, value, -1 );
1053 if (r == ERROR_SUCCESS && !wcscmp( propName, L"SourceDir" ))
1054 msi_reset_source_folders( package );
1056 msi_free(value);
1058 free_signature( &sig );
1060 uirow = MSI_CreateRecord( 2 );
1061 MSI_RecordSetStringW( uirow, 1, propName );
1062 MSI_RecordSetStringW( uirow, 2, sigName );
1063 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1064 msiobj_release( &uirow->hdr );
1066 return r;
1069 UINT ACTION_AppSearch(MSIPACKAGE *package)
1071 MSIQUERY *view;
1072 UINT r;
1074 if (msi_action_is_unique(package, L"AppSearch"))
1076 TRACE("Skipping AppSearch action: already done in UI sequence\n");
1077 return ERROR_SUCCESS;
1079 else
1080 msi_register_unique_action(package, L"AppSearch");
1082 r = MSI_OpenQuery( package->db, &view, L"SELECT * FROM `AppSearch`" );
1083 if (r != ERROR_SUCCESS)
1084 return ERROR_SUCCESS;
1086 r = MSI_IterateRecords( view, NULL, ITERATE_AppSearch, package );
1087 msiobj_release( &view->hdr );
1088 return r;
1091 static UINT ITERATE_CCPSearch(MSIRECORD *row, LPVOID param)
1093 MSIPACKAGE *package = param;
1094 LPCWSTR signature;
1095 LPWSTR value = NULL;
1096 MSISIGNATURE sig;
1097 UINT r = ERROR_SUCCESS;
1099 signature = MSI_RecordGetString(row, 1);
1101 TRACE("%s\n", debugstr_w(signature));
1103 search_sig_name( package, signature, &sig, &value );
1104 if (value)
1106 TRACE("Found signature %s\n", debugstr_w(signature));
1107 msi_set_property( package->db, L"CCP_Success", L"1", -1 );
1108 msi_free(value);
1109 r = ERROR_NO_MORE_ITEMS;
1112 free_signature(&sig);
1113 return r;
1116 UINT ACTION_CCPSearch(MSIPACKAGE *package)
1118 MSIQUERY *view;
1119 UINT r;
1121 if (msi_action_is_unique(package, L"CCPSearch"))
1123 TRACE("Skipping AppSearch action: already done in UI sequence\n");
1124 return ERROR_SUCCESS;
1126 else
1127 msi_register_unique_action(package, L"CCPSearch");
1129 r = MSI_OpenQuery(package->db, &view, L"SELECT * FROM `CCPSearch`");
1130 if (r != ERROR_SUCCESS)
1131 return ERROR_SUCCESS;
1133 r = MSI_IterateRecords(view, NULL, ITERATE_CCPSearch, package);
1134 msiobj_release(&view->hdr);
1135 return r;