b38bf2781f97be610b940443376e3d12003d4e4e
[reactos.git] / dll / win32 / msi / appsearch.c
1 /*
2 * Implementation of the AppSearch action of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2005 Juan Lang
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "msipriv.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(msi);
24
25 typedef struct tagMSISIGNATURE
26 {
27 LPCWSTR Name; /* NOT owned by this structure */
28 LPWSTR File;
29 DWORD MinVersionMS;
30 DWORD MinVersionLS;
31 DWORD MaxVersionMS;
32 DWORD MaxVersionLS;
33 DWORD MinSize;
34 DWORD MaxSize;
35 FILETIME MinTime;
36 FILETIME MaxTime;
37 LPWSTR Languages;
38 }MSISIGNATURE;
39
40 void msi_parse_version_string(LPCWSTR verStr, PDWORD ms, PDWORD ls)
41 {
42 const WCHAR *ptr;
43 int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
44
45 x1 = atoiW(verStr);
46 ptr = strchrW(verStr, '.');
47 if (ptr)
48 {
49 x2 = atoiW(ptr + 1);
50 ptr = strchrW(ptr + 1, '.');
51 }
52 if (ptr)
53 {
54 x3 = atoiW(ptr + 1);
55 ptr = strchrW(ptr + 1, '.');
56 }
57 if (ptr)
58 x4 = atoiW(ptr + 1);
59 /* FIXME: byte-order dependent? */
60 *ms = x1 << 16 | x2;
61 if (ls) *ls = x3 << 16 | x4;
62 }
63
64 /* Fills in sig with the values from the Signature table, where name is the
65 * signature to find. Upon return, sig->File will be NULL if the record is not
66 * found, and not NULL if it is found.
67 * Warning: clears all fields in sig!
68 * Returns ERROR_SUCCESS upon success (where not finding the record counts as
69 * success), something else on error.
70 */
71 static UINT ACTION_AppSearchGetSignature(MSIPACKAGE *package, MSISIGNATURE *sig, LPCWSTR name)
72 {
73 static const WCHAR query[] = {
74 's','e','l','e','c','t',' ','*',' ',
75 'f','r','o','m',' ',
76 'S','i','g','n','a','t','u','r','e',' ',
77 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e',' ','=',' ',
78 '\'','%','s','\'',0};
79 LPWSTR minVersion, maxVersion, p;
80 MSIRECORD *row;
81 DWORD time;
82
83 TRACE("package %p, sig %p\n", package, sig);
84
85 memset(sig, 0, sizeof(*sig));
86 sig->Name = name;
87 row = MSI_QueryGetRecord( package->db, query, name );
88 if (!row)
89 {
90 TRACE("failed to query signature for %s\n", debugstr_w(name));
91 return ERROR_SUCCESS;
92 }
93
94 /* get properties */
95 sig->File = msi_dup_record_field(row,2);
96 if ((p = strchrW(sig->File, '|')))
97 {
98 p++;
99 memmove(sig->File, p, (strlenW(p) + 1) * sizeof(WCHAR));
100 }
101
102 minVersion = msi_dup_record_field(row,3);
103 if (minVersion)
104 {
105 msi_parse_version_string( minVersion, &sig->MinVersionMS, &sig->MinVersionLS );
106 msi_free( minVersion );
107 }
108 maxVersion = msi_dup_record_field(row,4);
109 if (maxVersion)
110 {
111 msi_parse_version_string( maxVersion, &sig->MaxVersionMS, &sig->MaxVersionLS );
112 msi_free( maxVersion );
113 }
114 sig->MinSize = MSI_RecordGetInteger(row,5);
115 if (sig->MinSize == MSI_NULL_INTEGER)
116 sig->MinSize = 0;
117 sig->MaxSize = MSI_RecordGetInteger(row,6);
118 if (sig->MaxSize == MSI_NULL_INTEGER)
119 sig->MaxSize = 0;
120 sig->Languages = msi_dup_record_field(row,9);
121 time = MSI_RecordGetInteger(row,7);
122 if (time != MSI_NULL_INTEGER)
123 DosDateTimeToFileTime(HIWORD(time), LOWORD(time), &sig->MinTime);
124 time = MSI_RecordGetInteger(row,8);
125 if (time != MSI_NULL_INTEGER)
126 DosDateTimeToFileTime(HIWORD(time), LOWORD(time), &sig->MaxTime);
127
128 TRACE("Found file name %s for Signature_ %s;\n",
129 debugstr_w(sig->File), debugstr_w(name));
130 TRACE("MinVersion is %d.%d.%d.%d\n", HIWORD(sig->MinVersionMS),
131 LOWORD(sig->MinVersionMS), HIWORD(sig->MinVersionLS),
132 LOWORD(sig->MinVersionLS));
133 TRACE("MaxVersion is %d.%d.%d.%d\n", HIWORD(sig->MaxVersionMS),
134 LOWORD(sig->MaxVersionMS), HIWORD(sig->MaxVersionLS),
135 LOWORD(sig->MaxVersionLS));
136 TRACE("MinSize is %d, MaxSize is %d;\n", sig->MinSize, sig->MaxSize);
137 TRACE("Languages is %s\n", debugstr_w(sig->Languages));
138
139 msiobj_release( &row->hdr );
140
141 return ERROR_SUCCESS;
142 }
143
144 /* Frees any memory allocated in sig */
145 static void ACTION_FreeSignature(MSISIGNATURE *sig)
146 {
147 msi_free(sig->File);
148 msi_free(sig->Languages);
149 }
150
151 static LPWSTR app_search_file(LPWSTR path, MSISIGNATURE *sig)
152 {
153 VS_FIXEDFILEINFO *info;
154 DWORD attr, handle, size;
155 LPWSTR val = NULL;
156 LPBYTE buffer;
157
158 if (!sig->File)
159 {
160 PathRemoveFileSpecW(path);
161 PathAddBackslashW(path);
162
163 attr = GetFileAttributesW(path);
164 if (attr != INVALID_FILE_ATTRIBUTES &&
165 (attr & FILE_ATTRIBUTE_DIRECTORY))
166 return strdupW(path);
167
168 return NULL;
169 }
170
171 attr = GetFileAttributesW(path);
172 if (attr == INVALID_FILE_ATTRIBUTES ||
173 (attr & FILE_ATTRIBUTE_DIRECTORY))
174 return NULL;
175
176 size = GetFileVersionInfoSizeW(path, &handle);
177 if (!size)
178 return strdupW(path);
179
180 buffer = msi_alloc(size);
181 if (!buffer)
182 return NULL;
183
184 if (!GetFileVersionInfoW(path, 0, size, buffer))
185 goto done;
186
187 if (!VerQueryValueW(buffer, szBackSlash, (LPVOID)&info, &size) || !info)
188 goto done;
189
190 if (sig->MinVersionLS || sig->MinVersionMS)
191 {
192 if (info->dwFileVersionMS < sig->MinVersionMS)
193 goto done;
194
195 if (info->dwFileVersionMS == sig->MinVersionMS &&
196 info->dwFileVersionLS < sig->MinVersionLS)
197 goto done;
198 }
199
200 if (sig->MaxVersionLS || sig->MaxVersionMS)
201 {
202 if (info->dwFileVersionMS > sig->MaxVersionMS)
203 goto done;
204
205 if (info->dwFileVersionMS == sig->MaxVersionMS &&
206 info->dwFileVersionLS > sig->MaxVersionLS)
207 goto done;
208 }
209
210 val = strdupW(path);
211
212 done:
213 msi_free(buffer);
214 return val;
215 }
216
217 static UINT ACTION_AppSearchComponents(MSIPACKAGE *package, LPWSTR *appValue, MSISIGNATURE *sig)
218 {
219 static const WCHAR query[] = {
220 'S','E','L','E','C','T',' ','*',' ',
221 'F','R','O','M',' ',
222 '`','C','o','m','p','L','o','c','a','t','o','r','`',' ',
223 'W','H','E','R','E',' ','`','S','i','g','n','a','t','u','r','e','_','`',' ','=',' ',
224 '\'','%','s','\'',0};
225 static const WCHAR sigquery[] = {
226 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
227 '`','S','i','g','n','a','t','u','r','e','`',' ',
228 'W','H','E','R','E',' ','`','S','i','g','n','a','t','u','r','e','`',' ','=',' ',
229 '\'','%','s','\'',0};
230
231 MSIRECORD *row, *rec;
232 LPCWSTR signature, guid;
233 BOOL sigpresent = TRUE;
234 BOOL isdir;
235 UINT type;
236 WCHAR path[MAX_PATH];
237 DWORD size = MAX_PATH;
238 LPWSTR ptr;
239 DWORD attr;
240
241 TRACE("%s\n", debugstr_w(sig->Name));
242
243 *appValue = NULL;
244
245 row = MSI_QueryGetRecord(package->db, query, sig->Name);
246 if (!row)
247 {
248 TRACE("failed to query CompLocator for %s\n", debugstr_w(sig->Name));
249 return ERROR_SUCCESS;
250 }
251
252 signature = MSI_RecordGetString(row, 1);
253 guid = MSI_RecordGetString(row, 2);
254 type = MSI_RecordGetInteger(row, 3);
255
256 rec = MSI_QueryGetRecord(package->db, sigquery, signature);
257 if (!rec)
258 sigpresent = FALSE;
259
260 *path = '\0';
261 MsiLocateComponentW(guid, path, &size);
262 if (!*path)
263 goto done;
264
265 attr = GetFileAttributesW(path);
266 if (attr == INVALID_FILE_ATTRIBUTES)
267 goto done;
268
269 isdir = (attr & FILE_ATTRIBUTE_DIRECTORY);
270
271 if (type != msidbLocatorTypeDirectory && sigpresent && !isdir)
272 {
273 *appValue = app_search_file(path, sig);
274 }
275 else if (!sigpresent && (type != msidbLocatorTypeDirectory || isdir))
276 {
277 if (type == msidbLocatorTypeFileName)
278 {
279 ptr = strrchrW(path, '\\');
280 *(ptr + 1) = '\0';
281 }
282 else
283 PathAddBackslashW(path);
284
285 *appValue = strdupW(path);
286 }
287 else if (sigpresent)
288 {
289 PathAddBackslashW(path);
290 lstrcatW(path, MSI_RecordGetString(rec, 2));
291
292 attr = GetFileAttributesW(path);
293 if (attr != INVALID_FILE_ATTRIBUTES &&
294 !(attr & FILE_ATTRIBUTE_DIRECTORY))
295 *appValue = strdupW(path);
296 }
297
298 done:
299 if (rec) msiobj_release(&rec->hdr);
300 msiobj_release(&row->hdr);
301 return ERROR_SUCCESS;
302 }
303
304 static void ACTION_ConvertRegValue(DWORD regType, const BYTE *value, DWORD sz,
305 LPWSTR *appValue)
306 {
307 static const WCHAR dwordFmt[] = { '#','%','d','\0' };
308 static const WCHAR binPre[] = { '#','x','\0' };
309 static const WCHAR binFmt[] = { '%','0','2','X','\0' };
310 LPWSTR ptr;
311 DWORD i;
312
313 switch (regType)
314 {
315 case REG_SZ:
316 if (*(LPCWSTR)value == '#')
317 {
318 /* escape leading pound with another */
319 *appValue = msi_alloc(sz + sizeof(WCHAR));
320 (*appValue)[0] = '#';
321 strcpyW(*appValue + 1, (LPCWSTR)value);
322 }
323 else
324 {
325 *appValue = msi_alloc(sz);
326 strcpyW(*appValue, (LPCWSTR)value);
327 }
328 break;
329 case REG_DWORD:
330 /* 7 chars for digits, 1 for NULL, 1 for #, and 1 for sign
331 * char if needed
332 */
333 *appValue = msi_alloc(10 * sizeof(WCHAR));
334 sprintfW(*appValue, dwordFmt, *(const DWORD *)value);
335 break;
336 case REG_EXPAND_SZ:
337 sz = ExpandEnvironmentStringsW((LPCWSTR)value, NULL, 0);
338 *appValue = msi_alloc(sz * sizeof(WCHAR));
339 ExpandEnvironmentStringsW((LPCWSTR)value, *appValue, sz);
340 break;
341 case REG_BINARY:
342 /* #x<nibbles>\0 */
343 *appValue = msi_alloc((sz * 2 + 3) * sizeof(WCHAR));
344 lstrcpyW(*appValue, binPre);
345 ptr = *appValue + lstrlenW(binPre);
346 for (i = 0; i < sz; i++, ptr += 2)
347 sprintfW(ptr, binFmt, value[i]);
348 break;
349 default:
350 WARN("unimplemented for values of type %d\n", regType);
351 *appValue = NULL;
352 }
353 }
354
355 static UINT ACTION_SearchDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
356 LPCWSTR path, int depth, LPWSTR *appValue);
357
358 static UINT ACTION_AppSearchReg(MSIPACKAGE *package, LPWSTR *appValue, MSISIGNATURE *sig)
359 {
360 static const WCHAR query[] = {
361 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
362 'R','e','g','L','o','c','a','t','o','r',' ','W','H','E','R','E',' ',
363 'S','i','g','n','a','t','u','r','e','_',' ','=',' ', '\'','%','s','\'',0};
364 const WCHAR *keyPath, *valueName;
365 WCHAR *deformatted = NULL, *ptr = NULL, *end;
366 int root, type;
367 HKEY rootKey, key = NULL;
368 DWORD sz = 0, regType;
369 LPBYTE value = NULL;
370 MSIRECORD *row;
371 UINT rc;
372
373 TRACE("%s\n", debugstr_w(sig->Name));
374
375 *appValue = NULL;
376
377 row = MSI_QueryGetRecord( package->db, query, sig->Name );
378 if (!row)
379 {
380 TRACE("failed to query RegLocator for %s\n", debugstr_w(sig->Name));
381 return ERROR_SUCCESS;
382 }
383
384 root = MSI_RecordGetInteger(row, 2);
385 keyPath = MSI_RecordGetString(row, 3);
386 valueName = MSI_RecordGetString(row, 4);
387 type = MSI_RecordGetInteger(row, 5);
388
389 deformat_string(package, keyPath, &deformatted);
390
391 switch (root)
392 {
393 case msidbRegistryRootClassesRoot:
394 rootKey = HKEY_CLASSES_ROOT;
395 break;
396 case msidbRegistryRootCurrentUser:
397 rootKey = HKEY_CURRENT_USER;
398 break;
399 case msidbRegistryRootLocalMachine:
400 rootKey = HKEY_LOCAL_MACHINE;
401 break;
402 case msidbRegistryRootUsers:
403 rootKey = HKEY_USERS;
404 break;
405 default:
406 WARN("Unknown root key %d\n", root);
407 goto end;
408 }
409
410 rc = RegOpenKeyW(rootKey, deformatted, &key);
411 if (rc)
412 {
413 TRACE("RegOpenKeyW returned %d\n", rc);
414 goto end;
415 }
416
417 msi_free(deformatted);
418 deformat_string(package, valueName, &deformatted);
419
420 rc = RegQueryValueExW(key, deformatted, NULL, NULL, NULL, &sz);
421 if (rc)
422 {
423 TRACE("RegQueryValueExW returned %d\n", rc);
424 goto end;
425 }
426 /* FIXME: sanity-check sz before allocating (is there an upper-limit
427 * on the value of a property?)
428 */
429 value = msi_alloc( sz );
430 rc = RegQueryValueExW(key, deformatted, NULL, &regType, value, &sz);
431 if (rc)
432 {
433 TRACE("RegQueryValueExW returned %d\n", rc);
434 goto end;
435 }
436
437 /* bail out if the registry key is empty */
438 if (sz == 0)
439 goto end;
440
441 /* expand if needed */
442 if (regType == REG_EXPAND_SZ)
443 {
444 sz = ExpandEnvironmentStringsW((LPCWSTR)value, NULL, 0);
445 if (sz)
446 {
447 LPWSTR buf = msi_alloc(sz * sizeof(WCHAR));
448 ExpandEnvironmentStringsW((LPCWSTR)value, buf, sz);
449 msi_free(value);
450 value = (LPBYTE)buf;
451 }
452 }
453
454 if ((regType == REG_SZ || regType == REG_EXPAND_SZ) &&
455 (ptr = strchrW((LPWSTR)value, '"')) && (end = strchrW(++ptr, '"')))
456 *end = '\0';
457 else
458 ptr = (LPWSTR)value;
459
460 switch (type & 0x0f)
461 {
462 case msidbLocatorTypeDirectory:
463 ACTION_SearchDirectory(package, sig, ptr, 0, appValue);
464 break;
465 case msidbLocatorTypeFileName:
466 *appValue = app_search_file(ptr, sig);
467 break;
468 case msidbLocatorTypeRawValue:
469 ACTION_ConvertRegValue(regType, value, sz, appValue);
470 break;
471 default:
472 FIXME("unimplemented for type %d (key path %s, value %s)\n",
473 type, debugstr_w(keyPath), debugstr_w(valueName));
474 }
475 end:
476 msi_free( value );
477 RegCloseKey( key );
478 msi_free( deformatted );
479
480 msiobj_release(&row->hdr);
481 return ERROR_SUCCESS;
482 }
483
484 static LPWSTR get_ini_field(LPWSTR buf, int field)
485 {
486 LPWSTR beg, end;
487 int i = 1;
488
489 if (field == 0)
490 return strdupW(buf);
491
492 beg = buf;
493 while ((end = strchrW(beg, ',')) && i < field)
494 {
495 beg = end + 1;
496 while (*beg && *beg == ' ')
497 beg++;
498
499 i++;
500 }
501
502 end = strchrW(beg, ',');
503 if (!end)
504 end = beg + lstrlenW(beg);
505
506 *end = '\0';
507 return strdupW(beg);
508 }
509
510 static UINT ACTION_AppSearchIni(MSIPACKAGE *package, LPWSTR *appValue,
511 MSISIGNATURE *sig)
512 {
513 static const WCHAR query[] = {
514 's','e','l','e','c','t',' ','*',' ',
515 'f','r','o','m',' ',
516 'I','n','i','L','o','c','a','t','o','r',' ',
517 'w','h','e','r','e',' ',
518 'S','i','g','n','a','t','u','r','e','_',' ','=',' ','\'','%','s','\'',0};
519 MSIRECORD *row;
520 LPWSTR fileName, section, key;
521 int field, type;
522 WCHAR buf[MAX_PATH];
523
524 TRACE("%s\n", debugstr_w(sig->Name));
525
526 *appValue = NULL;
527
528 row = MSI_QueryGetRecord( package->db, query, sig->Name );
529 if (!row)
530 {
531 TRACE("failed to query IniLocator for %s\n", debugstr_w(sig->Name));
532 return ERROR_SUCCESS;
533 }
534
535 fileName = msi_dup_record_field(row, 2);
536 section = msi_dup_record_field(row, 3);
537 key = msi_dup_record_field(row, 4);
538 field = MSI_RecordGetInteger(row, 5);
539 type = MSI_RecordGetInteger(row, 6);
540 if (field == MSI_NULL_INTEGER)
541 field = 0;
542 if (type == MSI_NULL_INTEGER)
543 type = 0;
544
545 GetPrivateProfileStringW(section, key, NULL, buf, MAX_PATH, fileName);
546 if (buf[0])
547 {
548 switch (type & 0x0f)
549 {
550 case msidbLocatorTypeDirectory:
551 ACTION_SearchDirectory(package, sig, buf, 0, appValue);
552 break;
553 case msidbLocatorTypeFileName:
554 *appValue = app_search_file(buf, sig);
555 break;
556 case msidbLocatorTypeRawValue:
557 *appValue = get_ini_field(buf, field);
558 break;
559 }
560 }
561
562 msi_free(fileName);
563 msi_free(section);
564 msi_free(key);
565
566 msiobj_release(&row->hdr);
567
568 return ERROR_SUCCESS;
569 }
570
571 /* Expands the value in src into a path without property names and only
572 * containing long path names into dst. Replaces at most len characters of dst,
573 * and always NULL-terminates dst if dst is not NULL and len >= 1.
574 * May modify src.
575 * Assumes src and dst are non-overlapping.
576 * FIXME: return code probably needed:
577 * - what does AppSearch return if the table values are invalid?
578 * - what if dst is too small?
579 */
580 static void ACTION_ExpandAnyPath(MSIPACKAGE *package, WCHAR *src, WCHAR *dst,
581 size_t len)
582 {
583 WCHAR *ptr, *deformatted;
584
585 if (!src || !dst || !len)
586 {
587 if (dst) *dst = '\0';
588 return;
589 }
590
591 dst[0] = '\0';
592
593 /* Ignore the short portion of the path */
594 if ((ptr = strchrW(src, '|')))
595 ptr++;
596 else
597 ptr = src;
598
599 deformat_string(package, ptr, &deformatted);
600 if (!deformatted || strlenW(deformatted) > len - 1)
601 {
602 msi_free(deformatted);
603 return;
604 }
605
606 lstrcpyW(dst, deformatted);
607 dst[lstrlenW(deformatted)] = '\0';
608 msi_free(deformatted);
609 }
610
611 static LANGID *parse_languages( const WCHAR *languages, DWORD *num_ids )
612 {
613 UINT i, count = 1;
614 WCHAR *str = strdupW( languages ), *p, *q;
615 LANGID *ret;
616
617 if (!str) return NULL;
618 for (p = q = str; (q = strchrW( q, ',' )); q++) count++;
619
620 if (!(ret = msi_alloc( count * sizeof(LANGID) )))
621 {
622 msi_free( str );
623 return NULL;
624 }
625 i = 0;
626 while (*p)
627 {
628 q = strchrW( p, ',' );
629 if (q) *q = 0;
630 ret[i] = atoiW( p );
631 if (!q) break;
632 p = q + 1;
633 i++;
634 }
635 msi_free( str );
636 *num_ids = count;
637 return ret;
638 }
639
640 static BOOL match_languages( const void *version, const WCHAR *languages )
641 {
642 struct lang
643 {
644 USHORT id;
645 USHORT codepage;
646 } *lang;
647 DWORD len, num_ids, i, j;
648 BOOL found = FALSE;
649 LANGID *ids;
650
651 if (!languages || !languages[0]) return TRUE;
652 if (!VerQueryValueW( version, szLangResource, (void **)&lang, &len )) return FALSE;
653 if (!(ids = parse_languages( languages, &num_ids ))) return FALSE;
654
655 for (i = 0; i < num_ids; i++)
656 {
657 found = FALSE;
658 for (j = 0; j < len / sizeof(struct lang); j++)
659 {
660 if (!ids[i] || ids[i] == lang[j].id) found = TRUE;
661 }
662 if (!found) goto done;
663 }
664
665 done:
666 msi_free( ids );
667 return found;
668 }
669
670 /* Sets *matches to whether the file (whose path is filePath) matches the
671 * versions set in sig.
672 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
673 * something else if an install-halting error occurs.
674 */
675 static UINT ACTION_FileVersionMatches(const MSISIGNATURE *sig, LPCWSTR filePath,
676 BOOL *matches)
677 {
678 UINT len;
679 void *version;
680 VS_FIXEDFILEINFO *info = NULL;
681 DWORD zero, size = GetFileVersionInfoSizeW( filePath, &zero );
682
683 *matches = FALSE;
684
685 if (!size) return ERROR_SUCCESS;
686 if (!(version = msi_alloc( size ))) return ERROR_OUTOFMEMORY;
687
688 if (GetFileVersionInfoW( filePath, 0, size, version ))
689 VerQueryValueW( version, szBackSlash, (void **)&info, &len );
690
691 if (info)
692 {
693 TRACE("comparing file version %d.%d.%d.%d:\n",
694 HIWORD(info->dwFileVersionMS),
695 LOWORD(info->dwFileVersionMS),
696 HIWORD(info->dwFileVersionLS),
697 LOWORD(info->dwFileVersionLS));
698 if (info->dwFileVersionMS < sig->MinVersionMS
699 || (info->dwFileVersionMS == sig->MinVersionMS &&
700 info->dwFileVersionLS < sig->MinVersionLS))
701 {
702 TRACE("less than minimum version %d.%d.%d.%d\n",
703 HIWORD(sig->MinVersionMS),
704 LOWORD(sig->MinVersionMS),
705 HIWORD(sig->MinVersionLS),
706 LOWORD(sig->MinVersionLS));
707 }
708 else if ((sig->MaxVersionMS || sig->MaxVersionLS) &&
709 (info->dwFileVersionMS > sig->MaxVersionMS ||
710 (info->dwFileVersionMS == sig->MaxVersionMS &&
711 info->dwFileVersionLS > sig->MaxVersionLS)))
712 {
713 TRACE("greater than maximum version %d.%d.%d.%d\n",
714 HIWORD(sig->MaxVersionMS),
715 LOWORD(sig->MaxVersionMS),
716 HIWORD(sig->MaxVersionLS),
717 LOWORD(sig->MaxVersionLS));
718 }
719 else if (!match_languages( version, sig->Languages ))
720 {
721 TRACE("languages %s not supported\n", debugstr_w( sig->Languages ));
722 }
723 else *matches = TRUE;
724 }
725 msi_free( version );
726 return ERROR_SUCCESS;
727 }
728
729 /* Sets *matches to whether the file in findData matches that in sig.
730 * fullFilePath is assumed to be the full path of the file specified in
731 * findData, which may be necessary to compare the version.
732 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
733 * something else if an install-halting error occurs.
734 */
735 static UINT ACTION_FileMatchesSig(const MSISIGNATURE *sig,
736 const WIN32_FIND_DATAW *findData, LPCWSTR fullFilePath, BOOL *matches)
737 {
738 UINT rc = ERROR_SUCCESS;
739
740 *matches = TRUE;
741 /* assumes the caller has already ensured the filenames match, so check
742 * the other fields..
743 */
744 if (sig->MinTime.dwLowDateTime || sig->MinTime.dwHighDateTime)
745 {
746 if (findData->ftCreationTime.dwHighDateTime <
747 sig->MinTime.dwHighDateTime ||
748 (findData->ftCreationTime.dwHighDateTime == sig->MinTime.dwHighDateTime
749 && findData->ftCreationTime.dwLowDateTime <
750 sig->MinTime.dwLowDateTime))
751 *matches = FALSE;
752 }
753 if (*matches && (sig->MaxTime.dwLowDateTime || sig->MaxTime.dwHighDateTime))
754 {
755 if (findData->ftCreationTime.dwHighDateTime >
756 sig->MaxTime.dwHighDateTime ||
757 (findData->ftCreationTime.dwHighDateTime == sig->MaxTime.dwHighDateTime
758 && findData->ftCreationTime.dwLowDateTime >
759 sig->MaxTime.dwLowDateTime))
760 *matches = FALSE;
761 }
762 if (*matches && sig->MinSize && findData->nFileSizeLow < sig->MinSize)
763 *matches = FALSE;
764 if (*matches && sig->MaxSize && findData->nFileSizeLow > sig->MaxSize)
765 *matches = FALSE;
766 if (*matches && (sig->MinVersionMS || sig->MinVersionLS ||
767 sig->MaxVersionMS || sig->MaxVersionLS))
768 rc = ACTION_FileVersionMatches(sig, fullFilePath, matches);
769 return rc;
770 }
771
772 /* Recursively searches the directory dir for files that match the signature
773 * sig, up to (depth + 1) levels deep. That is, if depth is 0, it searches dir
774 * (and only dir). If depth is 1, searches dir and its immediate
775 * subdirectories.
776 * Assumes sig->File is not NULL.
777 * Returns ERROR_SUCCESS on success (which may include non-critical errors),
778 * something else on failures which should halt the install.
779 */
780 static UINT ACTION_RecurseSearchDirectory(MSIPACKAGE *package, LPWSTR *appValue,
781 MSISIGNATURE *sig, LPCWSTR dir, int depth)
782 {
783 HANDLE hFind;
784 WIN32_FIND_DATAW findData;
785 UINT rc = ERROR_SUCCESS;
786 size_t dirLen = lstrlenW(dir), fileLen = lstrlenW(sig->File);
787 WCHAR subpath[MAX_PATH];
788 WCHAR *buf;
789 DWORD len;
790
791 static const WCHAR starDotStarW[] = { '*','.','*',0 };
792
793 TRACE("Searching directory %s for file %s, depth %d\n", debugstr_w(dir),
794 debugstr_w(sig->File), depth);
795
796 if (depth < 0)
797 return ERROR_SUCCESS;
798
799 *appValue = NULL;
800 /* We need the buffer in both paths below, so go ahead and allocate it
801 * here. Add two because we might need to add a backslash if the dir name
802 * isn't backslash-terminated.
803 */
804 len = dirLen + max(fileLen, strlenW(starDotStarW)) + 2;
805 buf = msi_alloc(len * sizeof(WCHAR));
806 if (!buf)
807 return ERROR_OUTOFMEMORY;
808
809 lstrcpyW(buf, dir);
810 PathAddBackslashW(buf);
811 lstrcatW(buf, sig->File);
812
813 hFind = FindFirstFileW(buf, &findData);
814 if (hFind != INVALID_HANDLE_VALUE)
815 {
816 if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
817 {
818 BOOL matches;
819
820 rc = ACTION_FileMatchesSig(sig, &findData, buf, &matches);
821 if (rc == ERROR_SUCCESS && matches)
822 {
823 TRACE("found file, returning %s\n", debugstr_w(buf));
824 *appValue = buf;
825 }
826 }
827 FindClose(hFind);
828 }
829
830 if (rc == ERROR_SUCCESS && !*appValue)
831 {
832 lstrcpyW(buf, dir);
833 PathAddBackslashW(buf);
834 lstrcatW(buf, starDotStarW);
835
836 hFind = FindFirstFileW(buf, &findData);
837 if (hFind != INVALID_HANDLE_VALUE)
838 {
839 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
840 strcmpW( findData.cFileName, szDot ) &&
841 strcmpW( findData.cFileName, szDotDot ))
842 {
843 lstrcpyW(subpath, dir);
844 PathAppendW(subpath, findData.cFileName);
845 rc = ACTION_RecurseSearchDirectory(package, appValue, sig,
846 subpath, depth - 1);
847 }
848
849 while (rc == ERROR_SUCCESS && !*appValue &&
850 FindNextFileW(hFind, &findData) != 0)
851 {
852 if (!strcmpW( findData.cFileName, szDot ) ||
853 !strcmpW( findData.cFileName, szDotDot ))
854 continue;
855
856 lstrcpyW(subpath, dir);
857 PathAppendW(subpath, findData.cFileName);
858 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
859 rc = ACTION_RecurseSearchDirectory(package, appValue,
860 sig, subpath, depth - 1);
861 }
862
863 FindClose(hFind);
864 }
865 }
866
867 if (*appValue != buf)
868 msi_free(buf);
869
870 return rc;
871 }
872
873 static UINT ACTION_CheckDirectory(MSIPACKAGE *package, LPCWSTR dir,
874 LPWSTR *appValue)
875 {
876 DWORD attr = GetFileAttributesW(dir);
877
878 if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY))
879 {
880 TRACE("directory exists, returning %s\n", debugstr_w(dir));
881 *appValue = strdupW(dir);
882 }
883
884 return ERROR_SUCCESS;
885 }
886
887 static BOOL ACTION_IsFullPath(LPCWSTR path)
888 {
889 WCHAR first = toupperW(path[0]);
890 BOOL ret;
891
892 if (first >= 'A' && first <= 'Z' && path[1] == ':')
893 ret = TRUE;
894 else if (path[0] == '\\' && path[1] == '\\')
895 ret = TRUE;
896 else
897 ret = FALSE;
898 return ret;
899 }
900
901 static UINT ACTION_SearchDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
902 LPCWSTR path, int depth, LPWSTR *appValue)
903 {
904 UINT rc;
905 DWORD attr;
906 LPWSTR val = NULL;
907
908 TRACE("%p, %p, %s, %d, %p\n", package, sig, debugstr_w(path), depth,
909 appValue);
910
911 if (ACTION_IsFullPath(path))
912 {
913 if (sig->File)
914 rc = ACTION_RecurseSearchDirectory(package, &val, sig, path, depth);
915 else
916 {
917 /* Recursively searching a directory makes no sense when the
918 * directory to search is the thing you're trying to find.
919 */
920 rc = ACTION_CheckDirectory(package, path, &val);
921 }
922 }
923 else
924 {
925 WCHAR pathWithDrive[MAX_PATH] = { 'C',':','\\',0 };
926 DWORD drives = GetLogicalDrives();
927 int i;
928
929 rc = ERROR_SUCCESS;
930 for (i = 0; rc == ERROR_SUCCESS && !val && i < 26; i++)
931 {
932 if (!(drives & (1 << i)))
933 continue;
934
935 pathWithDrive[0] = 'A' + i;
936 if (GetDriveTypeW(pathWithDrive) != DRIVE_FIXED)
937 continue;
938
939 lstrcpynW(pathWithDrive + 3, path,
940 sizeof(pathWithDrive) / sizeof(pathWithDrive[0]) - 3);
941
942 if (sig->File)
943 rc = ACTION_RecurseSearchDirectory(package, &val, sig,
944 pathWithDrive, depth);
945 else
946 rc = ACTION_CheckDirectory(package, pathWithDrive, &val);
947 }
948 }
949
950 attr = GetFileAttributesW(val);
951 if (attr != INVALID_FILE_ATTRIBUTES &&
952 (attr & FILE_ATTRIBUTE_DIRECTORY) &&
953 val && val[lstrlenW(val) - 1] != '\\')
954 {
955 val = msi_realloc(val, (lstrlenW(val) + 2) * sizeof(WCHAR));
956 if (!val)
957 rc = ERROR_OUTOFMEMORY;
958 else
959 PathAddBackslashW(val);
960 }
961
962 *appValue = val;
963
964 TRACE("returning %d\n", rc);
965 return rc;
966 }
967
968 static UINT ACTION_AppSearchSigName(MSIPACKAGE *package, LPCWSTR sigName,
969 MSISIGNATURE *sig, LPWSTR *appValue);
970
971 static UINT ACTION_AppSearchDr(MSIPACKAGE *package, LPWSTR *appValue, MSISIGNATURE *sig)
972 {
973 static const WCHAR query[] = {
974 's','e','l','e','c','t',' ','*',' ',
975 'f','r','o','m',' ',
976 'D','r','L','o','c','a','t','o','r',' ',
977 'w','h','e','r','e',' ',
978 'S','i','g','n','a','t','u','r','e','_',' ','=',' ', '\'','%','s','\'',0};
979 LPWSTR parent = NULL;
980 LPCWSTR parentName;
981 WCHAR path[MAX_PATH];
982 WCHAR expanded[MAX_PATH];
983 MSIRECORD *row;
984 int depth;
985 DWORD sz, attr;
986 UINT rc;
987
988 TRACE("%s\n", debugstr_w(sig->Name));
989
990 *appValue = NULL;
991
992 row = MSI_QueryGetRecord( package->db, query, sig->Name );
993 if (!row)
994 {
995 TRACE("failed to query DrLocator for %s\n", debugstr_w(sig->Name));
996 return ERROR_SUCCESS;
997 }
998
999 /* check whether parent is set */
1000 parentName = MSI_RecordGetString(row, 2);
1001 if (parentName)
1002 {
1003 MSISIGNATURE parentSig;
1004
1005 ACTION_AppSearchSigName(package, parentName, &parentSig, &parent);
1006 ACTION_FreeSignature(&parentSig);
1007 if (!parent)
1008 {
1009 msiobj_release(&row->hdr);
1010 return ERROR_SUCCESS;
1011 }
1012 }
1013
1014 sz = MAX_PATH;
1015 MSI_RecordGetStringW(row, 3, path, &sz);
1016
1017 if (MSI_RecordIsNull(row,4))
1018 depth = 0;
1019 else
1020 depth = MSI_RecordGetInteger(row,4);
1021
1022 if (sz)
1023 ACTION_ExpandAnyPath(package, path, expanded, MAX_PATH);
1024 else
1025 strcpyW(expanded, path);
1026
1027 if (parent)
1028 {
1029 attr = GetFileAttributesW(parent);
1030 if (attr != INVALID_FILE_ATTRIBUTES &&
1031 !(attr & FILE_ATTRIBUTE_DIRECTORY))
1032 {
1033 PathRemoveFileSpecW(parent);
1034 PathAddBackslashW(parent);
1035 }
1036
1037 strcpyW(path, parent);
1038 strcatW(path, expanded);
1039 }
1040 else if (sz)
1041 strcpyW(path, expanded);
1042
1043 PathAddBackslashW(path);
1044
1045 rc = ACTION_SearchDirectory(package, sig, path, depth, appValue);
1046
1047 msi_free(parent);
1048 msiobj_release(&row->hdr);
1049
1050 TRACE("returning %d\n", rc);
1051 return rc;
1052 }
1053
1054 static UINT ACTION_AppSearchSigName(MSIPACKAGE *package, LPCWSTR sigName,
1055 MSISIGNATURE *sig, LPWSTR *appValue)
1056 {
1057 UINT rc;
1058
1059 *appValue = NULL;
1060 rc = ACTION_AppSearchGetSignature(package, sig, sigName);
1061 if (rc == ERROR_SUCCESS)
1062 {
1063 rc = ACTION_AppSearchComponents(package, appValue, sig);
1064 if (rc == ERROR_SUCCESS && !*appValue)
1065 {
1066 rc = ACTION_AppSearchReg(package, appValue, sig);
1067 if (rc == ERROR_SUCCESS && !*appValue)
1068 {
1069 rc = ACTION_AppSearchIni(package, appValue, sig);
1070 if (rc == ERROR_SUCCESS && !*appValue)
1071 rc = ACTION_AppSearchDr(package, appValue, sig);
1072 }
1073 }
1074 }
1075 return rc;
1076 }
1077
1078 static UINT iterate_appsearch(MSIRECORD *row, LPVOID param)
1079 {
1080 MSIPACKAGE *package = param;
1081 LPCWSTR propName, sigName;
1082 LPWSTR value = NULL;
1083 MSISIGNATURE sig;
1084 MSIRECORD *uirow;
1085 UINT r;
1086
1087 /* get property and signature */
1088 propName = MSI_RecordGetString(row, 1);
1089 sigName = MSI_RecordGetString(row, 2);
1090
1091 TRACE("%s %s\n", debugstr_w(propName), debugstr_w(sigName));
1092
1093 r = ACTION_AppSearchSigName(package, sigName, &sig, &value);
1094 if (value)
1095 {
1096 r = msi_set_property( package->db, propName, value, -1 );
1097 if (r == ERROR_SUCCESS && !strcmpW( propName, szSourceDir ))
1098 msi_reset_folders( package, TRUE );
1099
1100 msi_free(value);
1101 }
1102 ACTION_FreeSignature(&sig);
1103
1104 uirow = MSI_CreateRecord( 2 );
1105 MSI_RecordSetStringW( uirow, 1, propName );
1106 MSI_RecordSetStringW( uirow, 2, sigName );
1107 msi_ui_actiondata( package, szAppSearch, uirow );
1108 msiobj_release( &uirow->hdr );
1109
1110 return r;
1111 }
1112
1113 UINT ACTION_AppSearch(MSIPACKAGE *package)
1114 {
1115 static const WCHAR query[] = {
1116 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1117 'A','p','p','S','e','a','r','c','h',0};
1118 MSIQUERY *view;
1119 UINT r;
1120
1121 if (msi_action_is_unique(package, szAppSearch))
1122 {
1123 TRACE("Skipping AppSearch action: already done in UI sequence\n");
1124 return ERROR_SUCCESS;
1125 }
1126 else
1127 msi_register_unique_action(package, szAppSearch);
1128
1129 r = MSI_OpenQuery( package->db, &view, query );
1130 if (r != ERROR_SUCCESS)
1131 return ERROR_SUCCESS;
1132
1133 r = MSI_IterateRecords( view, NULL, iterate_appsearch, package );
1134 msiobj_release( &view->hdr );
1135 return r;
1136 }
1137
1138 static UINT ITERATE_CCPSearch(MSIRECORD *row, LPVOID param)
1139 {
1140 MSIPACKAGE *package = param;
1141 LPCWSTR signature;
1142 LPWSTR value = NULL;
1143 MSISIGNATURE sig;
1144 UINT r = ERROR_SUCCESS;
1145
1146 static const WCHAR success[] = {'C','C','P','_','S','u','c','c','e','s','s',0};
1147
1148 signature = MSI_RecordGetString(row, 1);
1149
1150 TRACE("%s\n", debugstr_w(signature));
1151
1152 ACTION_AppSearchSigName(package, signature, &sig, &value);
1153 if (value)
1154 {
1155 TRACE("Found signature %s\n", debugstr_w(signature));
1156 msi_set_property( package->db, success, szOne, -1 );
1157 msi_free(value);
1158 r = ERROR_NO_MORE_ITEMS;
1159 }
1160
1161 ACTION_FreeSignature(&sig);
1162
1163 return r;
1164 }
1165
1166 UINT ACTION_CCPSearch(MSIPACKAGE *package)
1167 {
1168 static const WCHAR query[] = {
1169 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1170 'C','C','P','S','e','a','r','c','h',0};
1171 MSIQUERY *view;
1172 UINT r;
1173
1174 if (msi_action_is_unique(package, szCCPSearch))
1175 {
1176 TRACE("Skipping AppSearch action: already done in UI sequence\n");
1177 return ERROR_SUCCESS;
1178 }
1179 else
1180 msi_register_unique_action(package, szCCPSearch);
1181
1182 r = MSI_OpenQuery(package->db, &view, query);
1183 if (r != ERROR_SUCCESS)
1184 return ERROR_SUCCESS;
1185
1186 r = MSI_IterateRecords(view, NULL, ITERATE_CCPSearch, package);
1187 msiobj_release(&view->hdr);
1188 return r;
1189 }