mshtml/tests: Fix a typo in ok() message.
[wine.git] / dlls / msi / appsearch.c
blobdd58ccad702325f44c7ece62cced394fde4b0180
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 static const WCHAR query[] = {
86 's','e','l','e','c','t',' ','*',' ',
87 'f','r','o','m',' ',
88 'S','i','g','n','a','t','u','r','e',' ',
89 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e',' ','=',' ',
90 '\'','%','s','\'',0};
91 LPWSTR minVersion, maxVersion, p;
92 MSIRECORD *row;
93 DWORD time;
95 TRACE("package %p, sig %p\n", package, sig);
97 memset(sig, 0, sizeof(*sig));
98 sig->Name = name;
99 row = MSI_QueryGetRecord( package->db, query, name );
100 if (!row)
102 TRACE("failed to query signature for %s\n", debugstr_w(name));
103 return ERROR_SUCCESS;
106 /* get properties */
107 sig->File = msi_dup_record_field(row,2);
108 if ((p = wcschr(sig->File, '|')))
110 p++;
111 memmove(sig->File, p, (lstrlenW(p) + 1) * sizeof(WCHAR));
114 minVersion = msi_dup_record_field(row,3);
115 if (minVersion)
117 msi_parse_version_string( minVersion, &sig->MinVersionMS, &sig->MinVersionLS );
118 msi_free( minVersion );
120 maxVersion = msi_dup_record_field(row,4);
121 if (maxVersion)
123 msi_parse_version_string( maxVersion, &sig->MaxVersionMS, &sig->MaxVersionLS );
124 msi_free( maxVersion );
126 sig->MinSize = MSI_RecordGetInteger(row,5);
127 if (sig->MinSize == MSI_NULL_INTEGER)
128 sig->MinSize = 0;
129 sig->MaxSize = MSI_RecordGetInteger(row,6);
130 if (sig->MaxSize == MSI_NULL_INTEGER)
131 sig->MaxSize = 0;
132 sig->Languages = msi_dup_record_field(row,9);
133 time = MSI_RecordGetInteger(row,7);
134 if (time != MSI_NULL_INTEGER)
135 DosDateTimeToFileTime(HIWORD(time), LOWORD(time), &sig->MinTime);
136 time = MSI_RecordGetInteger(row,8);
137 if (time != MSI_NULL_INTEGER)
138 DosDateTimeToFileTime(HIWORD(time), LOWORD(time), &sig->MaxTime);
140 TRACE("Found file name %s for Signature_ %s;\n",
141 debugstr_w(sig->File), debugstr_w(name));
142 TRACE("MinVersion is %d.%d.%d.%d\n", HIWORD(sig->MinVersionMS),
143 LOWORD(sig->MinVersionMS), HIWORD(sig->MinVersionLS),
144 LOWORD(sig->MinVersionLS));
145 TRACE("MaxVersion is %d.%d.%d.%d\n", HIWORD(sig->MaxVersionMS),
146 LOWORD(sig->MaxVersionMS), HIWORD(sig->MaxVersionLS),
147 LOWORD(sig->MaxVersionLS));
148 TRACE("MinSize is %d, MaxSize is %d;\n", sig->MinSize, sig->MaxSize);
149 TRACE("Languages is %s\n", debugstr_w(sig->Languages));
151 msiobj_release( &row->hdr );
153 return ERROR_SUCCESS;
156 /* Frees any memory allocated in sig */
157 static void free_signature( MSISIGNATURE *sig )
159 msi_free(sig->File);
160 msi_free(sig->Languages);
163 static WCHAR *search_file( MSIPACKAGE *package, WCHAR *path, MSISIGNATURE *sig )
165 VS_FIXEDFILEINFO *info;
166 DWORD attr, size;
167 LPWSTR val = NULL;
168 LPBYTE buffer;
170 if (!sig->File)
172 PathRemoveFileSpecW(path);
173 PathAddBackslashW(path);
175 attr = msi_get_file_attributes( package, path );
176 if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY))
177 return strdupW(path);
179 return NULL;
182 attr = msi_get_file_attributes( package, path );
183 if (attr == INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY))
184 return NULL;
186 size = msi_get_file_version_info( package, path, 0, NULL );
187 if (!size)
188 return strdupW(path);
190 buffer = msi_alloc(size);
191 if (!buffer)
192 return NULL;
194 size = msi_get_file_version_info( package, path, size, buffer );
195 if (!size)
196 goto done;
198 if (!VerQueryValueW(buffer, szBackSlash, (LPVOID)&info, &size) || !info)
199 goto done;
201 if (sig->MinVersionLS || sig->MinVersionMS)
203 if (info->dwFileVersionMS < sig->MinVersionMS)
204 goto done;
206 if (info->dwFileVersionMS == sig->MinVersionMS &&
207 info->dwFileVersionLS < sig->MinVersionLS)
208 goto done;
211 if (sig->MaxVersionLS || sig->MaxVersionMS)
213 if (info->dwFileVersionMS > sig->MaxVersionMS)
214 goto done;
216 if (info->dwFileVersionMS == sig->MaxVersionMS &&
217 info->dwFileVersionLS > sig->MaxVersionLS)
218 goto done;
221 val = strdupW(path);
223 done:
224 msi_free(buffer);
225 return val;
228 static UINT search_components( MSIPACKAGE *package, WCHAR **appValue, MSISIGNATURE *sig )
230 static const WCHAR query[] = {
231 'S','E','L','E','C','T',' ','*',' ',
232 'F','R','O','M',' ',
233 '`','C','o','m','p','L','o','c','a','t','o','r','`',' ',
234 'W','H','E','R','E',' ','`','S','i','g','n','a','t','u','r','e','_','`',' ','=',' ',
235 '\'','%','s','\'',0};
236 static const WCHAR sigquery[] = {
237 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
238 '`','S','i','g','n','a','t','u','r','e','`',' ',
239 'W','H','E','R','E',' ','`','S','i','g','n','a','t','u','r','e','`',' ','=',' ',
240 '\'','%','s','\'',0};
241 MSIRECORD *row, *rec;
242 LPCWSTR signature, guid;
243 BOOL sigpresent = TRUE;
244 BOOL isdir;
245 UINT type;
246 WCHAR path[MAX_PATH];
247 DWORD size = MAX_PATH;
248 LPWSTR ptr;
249 DWORD attr;
251 TRACE("%s\n", debugstr_w(sig->Name));
253 *appValue = NULL;
255 row = MSI_QueryGetRecord(package->db, query, sig->Name);
256 if (!row)
258 TRACE("failed to query CompLocator for %s\n", debugstr_w(sig->Name));
259 return ERROR_SUCCESS;
262 signature = MSI_RecordGetString(row, 1);
263 guid = MSI_RecordGetString(row, 2);
264 type = MSI_RecordGetInteger(row, 3);
266 rec = MSI_QueryGetRecord(package->db, sigquery, signature);
267 if (!rec)
268 sigpresent = FALSE;
270 *path = '\0';
271 MsiLocateComponentW(guid, path, &size);
272 if (!*path)
273 goto done;
275 attr = msi_get_file_attributes( package, path );
276 if (attr == INVALID_FILE_ATTRIBUTES)
277 goto done;
279 isdir = (attr & FILE_ATTRIBUTE_DIRECTORY);
281 if (type != msidbLocatorTypeDirectory && sigpresent && !isdir)
283 *appValue = search_file( package, path, sig );
285 else if (!sigpresent && (type != msidbLocatorTypeDirectory || isdir))
287 if (type == msidbLocatorTypeFileName)
289 ptr = wcsrchr(path, '\\');
290 *(ptr + 1) = '\0';
292 else
293 PathAddBackslashW(path);
295 *appValue = strdupW(path);
297 else if (sigpresent)
299 PathAddBackslashW(path);
300 lstrcatW(path, MSI_RecordGetString(rec, 2));
302 attr = msi_get_file_attributes( package, path );
303 if (attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY))
304 *appValue = strdupW(path);
307 done:
308 if (rec) msiobj_release(&rec->hdr);
309 msiobj_release(&row->hdr);
310 return ERROR_SUCCESS;
313 static void convert_reg_value( DWORD regType, const BYTE *value, DWORD sz, WCHAR **appValue )
315 static const WCHAR dwordFmt[] = { '#','%','d','\0' };
316 static const WCHAR binPre[] = { '#','x','\0' };
317 static const WCHAR binFmt[] = { '%','0','2','X','\0' };
318 LPWSTR ptr;
319 DWORD i;
321 switch (regType)
323 case REG_SZ:
324 if (*(LPCWSTR)value == '#')
326 /* escape leading pound with another */
327 *appValue = msi_alloc(sz + sizeof(WCHAR));
328 (*appValue)[0] = '#';
329 lstrcpyW(*appValue + 1, (LPCWSTR)value);
331 else
333 *appValue = msi_alloc(sz);
334 lstrcpyW(*appValue, (LPCWSTR)value);
336 break;
337 case REG_DWORD:
338 /* 7 chars for digits, 1 for NULL, 1 for #, and 1 for sign
339 * char if needed
341 *appValue = msi_alloc(10 * sizeof(WCHAR));
342 swprintf(*appValue, 10, dwordFmt, *(const DWORD *)value);
343 break;
344 case REG_EXPAND_SZ:
345 sz = ExpandEnvironmentStringsW((LPCWSTR)value, NULL, 0);
346 *appValue = msi_alloc(sz * sizeof(WCHAR));
347 ExpandEnvironmentStringsW((LPCWSTR)value, *appValue, sz);
348 break;
349 case REG_BINARY:
350 /* #x<nibbles>\0 */
351 *appValue = msi_alloc((sz * 2 + 3) * sizeof(WCHAR));
352 lstrcpyW(*appValue, binPre);
353 ptr = *appValue + lstrlenW(binPre);
354 for (i = 0; i < sz; i++, ptr += 2)
355 swprintf(ptr, 3, binFmt, value[i]);
356 break;
357 default:
358 WARN("unimplemented for values of type %d\n", regType);
359 *appValue = NULL;
363 static UINT search_directory( MSIPACKAGE *, MSISIGNATURE *, const WCHAR *, int, WCHAR ** );
365 static UINT search_reg( MSIPACKAGE *package, WCHAR **appValue, MSISIGNATURE *sig )
367 static const WCHAR query[] = {
368 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
369 'R','e','g','L','o','c','a','t','o','r',' ','W','H','E','R','E',' ',
370 'S','i','g','n','a','t','u','r','e','_',' ','=',' ', '\'','%','s','\'',0};
371 const WCHAR *keyPath, *valueName;
372 WCHAR *deformatted = NULL, *ptr = NULL, *end;
373 int root, type;
374 REGSAM access = KEY_READ;
375 HKEY rootKey, key = NULL;
376 DWORD sz = 0, regType;
377 LPBYTE value = NULL;
378 MSIRECORD *row;
379 UINT rc;
381 TRACE("%s\n", debugstr_w(sig->Name));
383 *appValue = NULL;
385 row = MSI_QueryGetRecord( package->db, query, sig->Name );
386 if (!row)
388 TRACE("failed to query RegLocator for %s\n", debugstr_w(sig->Name));
389 return ERROR_SUCCESS;
392 root = MSI_RecordGetInteger(row, 2);
393 keyPath = MSI_RecordGetString(row, 3);
394 valueName = MSI_RecordGetString(row, 4);
395 type = MSI_RecordGetInteger(row, 5);
397 deformat_string(package, keyPath, &deformatted);
399 switch (root)
401 case msidbRegistryRootClassesRoot:
402 rootKey = HKEY_CLASSES_ROOT;
403 break;
404 case msidbRegistryRootCurrentUser:
405 rootKey = HKEY_CURRENT_USER;
406 break;
407 case msidbRegistryRootLocalMachine:
408 rootKey = HKEY_LOCAL_MACHINE;
409 if (type & msidbLocatorType64bit) access |= KEY_WOW64_64KEY;
410 else access |= KEY_WOW64_32KEY;
411 break;
412 case msidbRegistryRootUsers:
413 rootKey = HKEY_USERS;
414 break;
415 default:
416 WARN("Unknown root key %d\n", root);
417 goto end;
420 rc = RegOpenKeyExW( rootKey, deformatted, 0, access, &key );
421 if (rc)
423 TRACE("RegOpenKeyExW returned %d\n", rc);
424 goto end;
427 msi_free(deformatted);
428 deformat_string(package, valueName, &deformatted);
430 rc = RegQueryValueExW(key, deformatted, NULL, NULL, NULL, &sz);
431 if (rc)
433 TRACE("RegQueryValueExW returned %d\n", rc);
434 goto end;
436 /* FIXME: sanity-check sz before allocating (is there an upper-limit
437 * on the value of a property?)
439 value = msi_alloc( sz );
440 rc = RegQueryValueExW(key, deformatted, NULL, &regType, value, &sz);
441 if (rc)
443 TRACE("RegQueryValueExW returned %d\n", rc);
444 goto end;
447 /* bail out if the registry key is empty */
448 if (sz == 0)
449 goto end;
451 /* expand if needed */
452 if (regType == REG_EXPAND_SZ)
454 sz = ExpandEnvironmentStringsW((LPCWSTR)value, NULL, 0);
455 if (sz)
457 LPWSTR buf = msi_alloc(sz * sizeof(WCHAR));
458 ExpandEnvironmentStringsW((LPCWSTR)value, buf, sz);
459 msi_free(value);
460 value = (LPBYTE)buf;
464 if ((regType == REG_SZ || regType == REG_EXPAND_SZ) &&
465 (ptr = wcschr((LPWSTR)value, '"')) && (end = wcschr(++ptr, '"')))
466 *end = '\0';
467 else
468 ptr = (LPWSTR)value;
470 switch (type & 0x0f)
472 case msidbLocatorTypeDirectory:
473 search_directory( package, sig, ptr, 0, appValue );
474 break;
475 case msidbLocatorTypeFileName:
476 *appValue = search_file( package, ptr, sig );
477 break;
478 case msidbLocatorTypeRawValue:
479 convert_reg_value( regType, value, sz, appValue );
480 break;
481 default:
482 FIXME("unimplemented for type %d (key path %s, value %s)\n",
483 type, debugstr_w(keyPath), debugstr_w(valueName));
485 end:
486 msi_free( value );
487 RegCloseKey( key );
488 msi_free( deformatted );
490 msiobj_release(&row->hdr);
491 return ERROR_SUCCESS;
494 static LPWSTR get_ini_field(LPWSTR buf, int field)
496 LPWSTR beg, end;
497 int i = 1;
499 if (field == 0)
500 return strdupW(buf);
502 beg = buf;
503 while ((end = wcschr(beg, ',')) && i < field)
505 beg = end + 1;
506 while (*beg == ' ')
507 beg++;
509 i++;
512 end = wcschr(beg, ',');
513 if (!end)
514 end = beg + lstrlenW(beg);
516 *end = '\0';
517 return strdupW(beg);
520 static UINT search_ini( MSIPACKAGE *package, WCHAR **appValue, MSISIGNATURE *sig )
522 static const WCHAR query[] = {
523 's','e','l','e','c','t',' ','*',' ',
524 'f','r','o','m',' ',
525 'I','n','i','L','o','c','a','t','o','r',' ',
526 'w','h','e','r','e',' ',
527 'S','i','g','n','a','t','u','r','e','_',' ','=',' ','\'','%','s','\'',0};
528 MSIRECORD *row;
529 LPWSTR fileName, section, key;
530 int field, type;
531 WCHAR buf[MAX_PATH];
533 TRACE("%s\n", debugstr_w(sig->Name));
535 *appValue = NULL;
537 row = MSI_QueryGetRecord( package->db, query, sig->Name );
538 if (!row)
540 TRACE("failed to query IniLocator for %s\n", debugstr_w(sig->Name));
541 return ERROR_SUCCESS;
544 fileName = msi_dup_record_field(row, 2);
545 section = msi_dup_record_field(row, 3);
546 key = msi_dup_record_field(row, 4);
547 field = MSI_RecordGetInteger(row, 5);
548 type = MSI_RecordGetInteger(row, 6);
549 if (field == MSI_NULL_INTEGER)
550 field = 0;
551 if (type == MSI_NULL_INTEGER)
552 type = 0;
554 GetPrivateProfileStringW(section, key, NULL, buf, MAX_PATH, fileName);
555 if (buf[0])
557 switch (type & 0x0f)
559 case msidbLocatorTypeDirectory:
560 search_directory( package, sig, buf, 0, appValue );
561 break;
562 case msidbLocatorTypeFileName:
563 *appValue = search_file( package, buf, sig );
564 break;
565 case msidbLocatorTypeRawValue:
566 *appValue = get_ini_field(buf, field);
567 break;
571 msi_free(fileName);
572 msi_free(section);
573 msi_free(key);
575 msiobj_release(&row->hdr);
577 return ERROR_SUCCESS;
580 /* Expands the value in src into a path without property names and only
581 * containing long path names into dst. Replaces at most len characters of dst,
582 * and always NULL-terminates dst if dst is not NULL and len >= 1.
583 * May modify src.
584 * Assumes src and dst are non-overlapping.
585 * FIXME: return code probably needed:
586 * - what does AppSearch return if the table values are invalid?
587 * - what if dst is too small?
589 static void expand_any_path( MSIPACKAGE *package, WCHAR *src, WCHAR *dst, size_t len )
591 WCHAR *ptr, *deformatted;
593 if (!src || !dst || !len)
595 if (dst) *dst = '\0';
596 return;
599 dst[0] = '\0';
601 /* Ignore the short portion of the path */
602 if ((ptr = wcschr(src, '|')))
603 ptr++;
604 else
605 ptr = src;
607 deformat_string(package, ptr, &deformatted);
608 if (!deformatted || lstrlenW(deformatted) > len - 1)
610 msi_free(deformatted);
611 return;
614 lstrcpyW(dst, deformatted);
615 dst[lstrlenW(deformatted)] = '\0';
616 msi_free(deformatted);
619 static LANGID *parse_languages( const WCHAR *languages, DWORD *num_ids )
621 UINT i, count = 1;
622 WCHAR *str = strdupW( languages ), *p, *q;
623 LANGID *ret;
625 if (!str) return NULL;
626 for (p = q = str; (q = wcschr( q, ',' )); q++) count++;
628 if (!(ret = msi_alloc( count * sizeof(LANGID) )))
630 msi_free( str );
631 return NULL;
633 i = 0;
634 while (*p)
636 q = wcschr( p, ',' );
637 if (q) *q = 0;
638 ret[i] = wcstol( p, NULL, 10 );
639 if (!q) break;
640 p = q + 1;
641 i++;
643 msi_free( str );
644 *num_ids = count;
645 return ret;
648 static BOOL match_languages( const void *version, const WCHAR *languages )
650 struct lang
652 USHORT id;
653 USHORT codepage;
654 } *lang;
655 DWORD len, num_ids, i, j;
656 BOOL found = FALSE;
657 LANGID *ids;
659 if (!languages || !languages[0]) return TRUE;
660 if (!VerQueryValueW( version, szLangResource, (void **)&lang, &len )) return FALSE;
661 if (!(ids = parse_languages( languages, &num_ids ))) return FALSE;
663 for (i = 0; i < num_ids; i++)
665 found = FALSE;
666 for (j = 0; j < len / sizeof(struct lang); j++)
668 if (!ids[i] || ids[i] == lang[j].id) found = TRUE;
670 if (!found) goto done;
673 done:
674 msi_free( ids );
675 return found;
678 /* Sets *matches to whether the file (whose path is filePath) matches the
679 * versions set in sig.
680 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
681 * something else if an install-halting error occurs.
683 static UINT file_version_matches( MSIPACKAGE *package, const MSISIGNATURE *sig, const WCHAR *filePath,
684 BOOL *matches )
686 UINT len;
687 void *version;
688 VS_FIXEDFILEINFO *info = NULL;
689 DWORD size = msi_get_file_version_info( package, filePath, 0, NULL );
691 *matches = FALSE;
693 if (!size) return ERROR_SUCCESS;
694 if (!(version = msi_alloc( size ))) return ERROR_OUTOFMEMORY;
696 if (msi_get_file_version_info( package, filePath, size, version ))
697 VerQueryValueW( version, szBackSlash, (void **)&info, &len );
699 if (info)
701 TRACE("comparing file version %d.%d.%d.%d:\n",
702 HIWORD(info->dwFileVersionMS),
703 LOWORD(info->dwFileVersionMS),
704 HIWORD(info->dwFileVersionLS),
705 LOWORD(info->dwFileVersionLS));
706 if (info->dwFileVersionMS < sig->MinVersionMS
707 || (info->dwFileVersionMS == sig->MinVersionMS &&
708 info->dwFileVersionLS < sig->MinVersionLS))
710 TRACE("less than minimum version %d.%d.%d.%d\n",
711 HIWORD(sig->MinVersionMS),
712 LOWORD(sig->MinVersionMS),
713 HIWORD(sig->MinVersionLS),
714 LOWORD(sig->MinVersionLS));
716 else if ((sig->MaxVersionMS || sig->MaxVersionLS) &&
717 (info->dwFileVersionMS > sig->MaxVersionMS ||
718 (info->dwFileVersionMS == sig->MaxVersionMS &&
719 info->dwFileVersionLS > sig->MaxVersionLS)))
721 TRACE("greater than maximum version %d.%d.%d.%d\n",
722 HIWORD(sig->MaxVersionMS),
723 LOWORD(sig->MaxVersionMS),
724 HIWORD(sig->MaxVersionLS),
725 LOWORD(sig->MaxVersionLS));
727 else if (!match_languages( version, sig->Languages ))
729 TRACE("languages %s not supported\n", debugstr_w( sig->Languages ));
731 else *matches = TRUE;
733 msi_free( version );
734 return ERROR_SUCCESS;
737 /* Sets *matches to whether the file in findData matches that in sig.
738 * fullFilePath is assumed to be the full path of the file specified in
739 * findData, which may be necessary to compare the version.
740 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
741 * something else if an install-halting error occurs.
743 static UINT file_matches_sig( MSIPACKAGE *package, const MSISIGNATURE *sig, const WIN32_FIND_DATAW *findData,
744 const WCHAR *fullFilePath, BOOL *matches )
746 UINT rc = ERROR_SUCCESS;
748 *matches = TRUE;
749 /* assumes the caller has already ensured the filenames match, so check
750 * the other fields..
752 if (sig->MinTime.dwLowDateTime || sig->MinTime.dwHighDateTime)
754 if (findData->ftCreationTime.dwHighDateTime <
755 sig->MinTime.dwHighDateTime ||
756 (findData->ftCreationTime.dwHighDateTime == sig->MinTime.dwHighDateTime
757 && findData->ftCreationTime.dwLowDateTime <
758 sig->MinTime.dwLowDateTime))
759 *matches = FALSE;
761 if (*matches && (sig->MaxTime.dwLowDateTime || sig->MaxTime.dwHighDateTime))
763 if (findData->ftCreationTime.dwHighDateTime >
764 sig->MaxTime.dwHighDateTime ||
765 (findData->ftCreationTime.dwHighDateTime == sig->MaxTime.dwHighDateTime
766 && findData->ftCreationTime.dwLowDateTime >
767 sig->MaxTime.dwLowDateTime))
768 *matches = FALSE;
770 if (*matches && sig->MinSize && findData->nFileSizeLow < sig->MinSize)
771 *matches = FALSE;
772 if (*matches && sig->MaxSize && findData->nFileSizeLow > sig->MaxSize)
773 *matches = FALSE;
774 if (*matches && (sig->MinVersionMS || sig->MinVersionLS ||
775 sig->MaxVersionMS || sig->MaxVersionLS))
776 rc = file_version_matches( package, sig, fullFilePath, matches );
777 return rc;
780 /* Recursively searches the directory dir for files that match the signature
781 * sig, up to (depth + 1) levels deep. That is, if depth is 0, it searches dir
782 * (and only dir). If depth is 1, searches dir and its immediate
783 * subdirectories.
784 * Assumes sig->File is not NULL.
785 * Returns ERROR_SUCCESS on success (which may include non-critical errors),
786 * something else on failures which should halt the install.
788 static UINT recurse_search_directory( MSIPACKAGE *package, WCHAR **appValue, MSISIGNATURE *sig, const WCHAR *dir,
789 int depth )
791 static const WCHAR starDotStarW[] = { '*','.','*',0 };
792 HANDLE hFind;
793 WIN32_FIND_DATAW findData;
794 UINT rc = ERROR_SUCCESS;
795 size_t dirLen = lstrlenW(dir), fileLen = lstrlenW(sig->File);
796 WCHAR subpath[MAX_PATH];
797 WCHAR *buf;
798 DWORD len;
800 TRACE("Searching directory %s for file %s, depth %d\n", debugstr_w(dir), debugstr_w(sig->File), depth);
802 if (depth < 0)
803 return ERROR_SUCCESS;
805 *appValue = NULL;
806 /* We need the buffer in both paths below, so go ahead and allocate it
807 * here. Add two because we might need to add a backslash if the dir name
808 * isn't backslash-terminated.
810 len = dirLen + max(fileLen, lstrlenW(starDotStarW)) + 2;
811 buf = msi_alloc(len * sizeof(WCHAR));
812 if (!buf)
813 return ERROR_OUTOFMEMORY;
815 lstrcpyW(buf, dir);
816 PathAddBackslashW(buf);
817 lstrcatW(buf, sig->File);
819 hFind = msi_find_first_file( package, buf, &findData );
820 if (hFind != INVALID_HANDLE_VALUE)
822 if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
824 BOOL matches;
826 rc = file_matches_sig( package, sig, &findData, buf, &matches );
827 if (rc == ERROR_SUCCESS && matches)
829 TRACE("found file, returning %s\n", debugstr_w(buf));
830 *appValue = buf;
833 FindClose(hFind);
836 if (rc == ERROR_SUCCESS && !*appValue)
838 lstrcpyW(buf, dir);
839 PathAddBackslashW(buf);
840 lstrcatW(buf, starDotStarW);
842 hFind = msi_find_first_file( package, buf, &findData );
843 if (hFind != INVALID_HANDLE_VALUE)
845 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
846 wcscmp( findData.cFileName, szDot ) &&
847 wcscmp( findData.cFileName, szDotDot ))
849 lstrcpyW(subpath, dir);
850 PathAppendW(subpath, findData.cFileName);
851 rc = recurse_search_directory( package, appValue, sig, subpath, depth - 1 );
854 while (rc == ERROR_SUCCESS && !*appValue && msi_find_next_file( package, hFind, &findData ))
856 if (!wcscmp( findData.cFileName, szDot ) ||
857 !wcscmp( findData.cFileName, szDotDot ))
858 continue;
860 lstrcpyW(subpath, dir);
861 PathAppendW(subpath, findData.cFileName);
862 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
863 rc = recurse_search_directory( package, appValue, sig, subpath, depth - 1 );
866 FindClose(hFind);
870 if (*appValue != buf)
871 msi_free(buf);
873 return rc;
876 static UINT check_directory( MSIPACKAGE *package, const WCHAR *dir, WCHAR **appValue )
878 DWORD attr = msi_get_file_attributes( package, dir );
880 if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY))
882 TRACE("directory exists, returning %s\n", debugstr_w(dir));
883 *appValue = strdupW(dir);
886 return ERROR_SUCCESS;
889 static BOOL is_full_path( const WCHAR *path )
891 WCHAR first = towupper(path[0]);
892 BOOL ret;
894 if (first >= 'A' && first <= 'Z' && path[1] == ':')
895 ret = TRUE;
896 else if (path[0] == '\\' && path[1] == '\\')
897 ret = TRUE;
898 else
899 ret = FALSE;
900 return ret;
903 static UINT search_directory( MSIPACKAGE *package, MSISIGNATURE *sig, const WCHAR *path, int depth, WCHAR **appValue )
905 UINT rc;
906 DWORD attr;
907 LPWSTR val = NULL;
909 TRACE("%p, %p, %s, %d, %p\n", package, sig, debugstr_w(path), depth, appValue);
911 if (is_full_path( path ))
913 if (sig->File)
914 rc = recurse_search_directory( package, &val, sig, path, depth );
915 else
917 /* Recursively searching a directory makes no sense when the
918 * directory to search is the thing you're trying to find.
920 rc = check_directory( package, path, &val );
923 else
925 WCHAR pathWithDrive[MAX_PATH] = { 'C',':','\\',0 };
926 DWORD drives = GetLogicalDrives();
927 int i;
929 rc = ERROR_SUCCESS;
930 for (i = 0; rc == ERROR_SUCCESS && !val && i < 26; i++)
932 if (!(drives & (1 << i)))
933 continue;
935 pathWithDrive[0] = 'A' + i;
936 if (GetDriveTypeW(pathWithDrive) != DRIVE_FIXED)
937 continue;
939 lstrcpynW(pathWithDrive + 3, path, ARRAY_SIZE(pathWithDrive) - 3);
941 if (sig->File)
942 rc = recurse_search_directory( package, &val, sig, pathWithDrive, depth );
943 else
944 rc = check_directory( package, pathWithDrive, &val );
948 attr = msi_get_file_attributes( package, val );
949 if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY) &&
950 val && val[lstrlenW(val) - 1] != '\\')
952 val = msi_realloc(val, (lstrlenW(val) + 2) * sizeof(WCHAR));
953 if (!val)
954 rc = ERROR_OUTOFMEMORY;
955 else
956 PathAddBackslashW(val);
959 *appValue = val;
961 TRACE("returning %d\n", rc);
962 return rc;
965 static UINT search_sig_name( MSIPACKAGE *, const WCHAR *, MSISIGNATURE *, WCHAR ** );
967 static UINT search_dr( MSIPACKAGE *package, WCHAR **appValue, MSISIGNATURE *sig )
969 static const WCHAR query[] = {
970 's','e','l','e','c','t',' ','*',' ',
971 'f','r','o','m',' ',
972 'D','r','L','o','c','a','t','o','r',' ',
973 'w','h','e','r','e',' ',
974 'S','i','g','n','a','t','u','r','e','_',' ','=',' ', '\'','%','s','\'',0};
975 LPWSTR parent = NULL;
976 LPCWSTR parentName;
977 WCHAR path[MAX_PATH];
978 WCHAR expanded[MAX_PATH];
979 MSIRECORD *row;
980 int depth;
981 DWORD sz, attr;
982 UINT rc;
984 TRACE("%s\n", debugstr_w(sig->Name));
986 *appValue = NULL;
988 row = MSI_QueryGetRecord( package->db, query, sig->Name );
989 if (!row)
991 TRACE("failed to query DrLocator for %s\n", debugstr_w(sig->Name));
992 return ERROR_SUCCESS;
995 /* check whether parent is set */
996 parentName = MSI_RecordGetString(row, 2);
997 if (parentName)
999 MSISIGNATURE parentSig;
1001 search_sig_name( package, parentName, &parentSig, &parent );
1002 free_signature( &parentSig );
1003 if (!parent)
1005 msiobj_release(&row->hdr);
1006 return ERROR_SUCCESS;
1010 sz = MAX_PATH;
1011 MSI_RecordGetStringW(row, 3, path, &sz);
1013 if (MSI_RecordIsNull(row,4))
1014 depth = 0;
1015 else
1016 depth = MSI_RecordGetInteger(row,4);
1018 if (sz)
1019 expand_any_path( package, path, expanded, MAX_PATH );
1020 else
1021 lstrcpyW(expanded, path);
1023 if (parent)
1025 attr = msi_get_file_attributes( package, parent );
1026 if (attr != INVALID_FILE_ATTRIBUTES &&
1027 !(attr & FILE_ATTRIBUTE_DIRECTORY))
1029 PathRemoveFileSpecW(parent);
1030 PathAddBackslashW(parent);
1033 lstrcpyW(path, parent);
1034 lstrcatW(path, expanded);
1036 else if (sz) lstrcpyW(path, expanded);
1038 PathAddBackslashW(path);
1040 rc = search_directory( package, sig, path, depth, appValue );
1042 msi_free(parent);
1043 msiobj_release(&row->hdr);
1044 TRACE("returning %d\n", rc);
1045 return rc;
1048 static UINT search_sig_name( MSIPACKAGE *package, const WCHAR *sigName, MSISIGNATURE *sig, WCHAR **appValue )
1050 UINT rc;
1052 *appValue = NULL;
1053 rc = get_signature( package, sig, sigName );
1054 if (rc == ERROR_SUCCESS)
1056 rc = search_components( package, appValue, sig );
1057 if (rc == ERROR_SUCCESS && !*appValue)
1059 rc = search_reg( package, appValue, sig );
1060 if (rc == ERROR_SUCCESS && !*appValue)
1062 rc = search_ini( package, appValue, sig );
1063 if (rc == ERROR_SUCCESS && !*appValue)
1064 rc = search_dr( package, appValue, sig );
1068 return rc;
1071 static UINT ITERATE_AppSearch(MSIRECORD *row, LPVOID param)
1073 MSIPACKAGE *package = param;
1074 LPCWSTR propName, sigName;
1075 LPWSTR value = NULL;
1076 MSISIGNATURE sig;
1077 MSIRECORD *uirow;
1078 UINT r;
1080 /* get property and signature */
1081 propName = MSI_RecordGetString(row, 1);
1082 sigName = MSI_RecordGetString(row, 2);
1084 TRACE("%s %s\n", debugstr_w(propName), debugstr_w(sigName));
1086 r = search_sig_name( package, sigName, &sig, &value );
1087 if (value)
1089 r = msi_set_property( package->db, propName, value, -1 );
1090 if (r == ERROR_SUCCESS && !wcscmp( propName, szSourceDir ))
1091 msi_reset_source_folders( package );
1093 msi_free(value);
1095 free_signature( &sig );
1097 uirow = MSI_CreateRecord( 2 );
1098 MSI_RecordSetStringW( uirow, 1, propName );
1099 MSI_RecordSetStringW( uirow, 2, sigName );
1100 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1101 msiobj_release( &uirow->hdr );
1103 return r;
1106 UINT ACTION_AppSearch(MSIPACKAGE *package)
1108 static const WCHAR query[] = {
1109 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1110 'A','p','p','S','e','a','r','c','h',0};
1111 MSIQUERY *view;
1112 UINT r;
1114 if (msi_action_is_unique(package, szAppSearch))
1116 TRACE("Skipping AppSearch action: already done in UI sequence\n");
1117 return ERROR_SUCCESS;
1119 else
1120 msi_register_unique_action(package, szAppSearch);
1122 r = MSI_OpenQuery( package->db, &view, query );
1123 if (r != ERROR_SUCCESS)
1124 return ERROR_SUCCESS;
1126 r = MSI_IterateRecords( view, NULL, ITERATE_AppSearch, package );
1127 msiobj_release( &view->hdr );
1128 return r;
1131 static UINT ITERATE_CCPSearch(MSIRECORD *row, LPVOID param)
1133 MSIPACKAGE *package = param;
1134 LPCWSTR signature;
1135 LPWSTR value = NULL;
1136 MSISIGNATURE sig;
1137 UINT r = ERROR_SUCCESS;
1139 static const WCHAR success[] = {'C','C','P','_','S','u','c','c','e','s','s',0};
1141 signature = MSI_RecordGetString(row, 1);
1143 TRACE("%s\n", debugstr_w(signature));
1145 search_sig_name( package, signature, &sig, &value );
1146 if (value)
1148 TRACE("Found signature %s\n", debugstr_w(signature));
1149 msi_set_property( package->db, success, szOne, -1 );
1150 msi_free(value);
1151 r = ERROR_NO_MORE_ITEMS;
1154 free_signature(&sig);
1155 return r;
1158 UINT ACTION_CCPSearch(MSIPACKAGE *package)
1160 static const WCHAR query[] = {
1161 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1162 'C','C','P','S','e','a','r','c','h',0};
1163 MSIQUERY *view;
1164 UINT r;
1166 if (msi_action_is_unique(package, szCCPSearch))
1168 TRACE("Skipping AppSearch action: already done in UI sequence\n");
1169 return ERROR_SUCCESS;
1171 else
1172 msi_register_unique_action(package, szCCPSearch);
1174 r = MSI_OpenQuery(package->db, &view, query);
1175 if (r != ERROR_SUCCESS)
1176 return ERROR_SUCCESS;
1178 r = MSI_IterateRecords(view, NULL, ITERATE_CCPSearch, package);
1179 msiobj_release(&view->hdr);
1180 return r;