[MSI]
[reactos.git] / reactos / 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 if ((regType == REG_SZ || regType == REG_EXPAND_SZ) &&
442 (ptr = strchrW((LPWSTR)value, '"')) && (end = strchrW(++ptr, '"')))
443 *end = '\0';
444 else
445 ptr = (LPWSTR)value;
446
447 switch (type & 0x0f)
448 {
449 case msidbLocatorTypeDirectory:
450 ACTION_SearchDirectory(package, sig, ptr, 0, appValue);
451 break;
452 case msidbLocatorTypeFileName:
453 *appValue = app_search_file(ptr, sig);
454 break;
455 case msidbLocatorTypeRawValue:
456 ACTION_ConvertRegValue(regType, value, sz, appValue);
457 break;
458 default:
459 FIXME("unimplemented for type %d (key path %s, value %s)\n",
460 type, debugstr_w(keyPath), debugstr_w(valueName));
461 }
462 end:
463 msi_free( value );
464 RegCloseKey( key );
465 msi_free( deformatted );
466
467 msiobj_release(&row->hdr);
468 return ERROR_SUCCESS;
469 }
470
471 static LPWSTR get_ini_field(LPWSTR buf, int field)
472 {
473 LPWSTR beg, end;
474 int i = 1;
475
476 if (field == 0)
477 return strdupW(buf);
478
479 beg = buf;
480 while ((end = strchrW(beg, ',')) && i < field)
481 {
482 beg = end + 1;
483 while (*beg && *beg == ' ')
484 beg++;
485
486 i++;
487 }
488
489 end = strchrW(beg, ',');
490 if (!end)
491 end = beg + lstrlenW(beg);
492
493 *end = '\0';
494 return strdupW(beg);
495 }
496
497 static UINT ACTION_AppSearchIni(MSIPACKAGE *package, LPWSTR *appValue,
498 MSISIGNATURE *sig)
499 {
500 static const WCHAR query[] = {
501 's','e','l','e','c','t',' ','*',' ',
502 'f','r','o','m',' ',
503 'I','n','i','L','o','c','a','t','o','r',' ',
504 'w','h','e','r','e',' ',
505 'S','i','g','n','a','t','u','r','e','_',' ','=',' ','\'','%','s','\'',0};
506 MSIRECORD *row;
507 LPWSTR fileName, section, key;
508 int field, type;
509 WCHAR buf[MAX_PATH];
510
511 TRACE("%s\n", debugstr_w(sig->Name));
512
513 *appValue = NULL;
514
515 row = MSI_QueryGetRecord( package->db, query, sig->Name );
516 if (!row)
517 {
518 TRACE("failed to query IniLocator for %s\n", debugstr_w(sig->Name));
519 return ERROR_SUCCESS;
520 }
521
522 fileName = msi_dup_record_field(row, 2);
523 section = msi_dup_record_field(row, 3);
524 key = msi_dup_record_field(row, 4);
525 field = MSI_RecordGetInteger(row, 5);
526 type = MSI_RecordGetInteger(row, 6);
527 if (field == MSI_NULL_INTEGER)
528 field = 0;
529 if (type == MSI_NULL_INTEGER)
530 type = 0;
531
532 GetPrivateProfileStringW(section, key, NULL, buf, MAX_PATH, fileName);
533 if (buf[0])
534 {
535 switch (type & 0x0f)
536 {
537 case msidbLocatorTypeDirectory:
538 ACTION_SearchDirectory(package, sig, buf, 0, appValue);
539 break;
540 case msidbLocatorTypeFileName:
541 *appValue = app_search_file(buf, sig);
542 break;
543 case msidbLocatorTypeRawValue:
544 *appValue = get_ini_field(buf, field);
545 break;
546 }
547 }
548
549 msi_free(fileName);
550 msi_free(section);
551 msi_free(key);
552
553 msiobj_release(&row->hdr);
554
555 return ERROR_SUCCESS;
556 }
557
558 /* Expands the value in src into a path without property names and only
559 * containing long path names into dst. Replaces at most len characters of dst,
560 * and always NULL-terminates dst if dst is not NULL and len >= 1.
561 * May modify src.
562 * Assumes src and dst are non-overlapping.
563 * FIXME: return code probably needed:
564 * - what does AppSearch return if the table values are invalid?
565 * - what if dst is too small?
566 */
567 static void ACTION_ExpandAnyPath(MSIPACKAGE *package, WCHAR *src, WCHAR *dst,
568 size_t len)
569 {
570 WCHAR *ptr, *deformatted;
571
572 if (!src || !dst || !len)
573 {
574 if (dst) *dst = '\0';
575 return;
576 }
577
578 dst[0] = '\0';
579
580 /* Ignore the short portion of the path */
581 if ((ptr = strchrW(src, '|')))
582 ptr++;
583 else
584 ptr = src;
585
586 deformat_string(package, ptr, &deformatted);
587 if (!deformatted || strlenW(deformatted) > len - 1)
588 {
589 msi_free(deformatted);
590 return;
591 }
592
593 lstrcpyW(dst, deformatted);
594 dst[lstrlenW(deformatted)] = '\0';
595 msi_free(deformatted);
596 }
597
598 static LANGID *parse_languages( const WCHAR *languages, DWORD *num_ids )
599 {
600 UINT i, count = 1;
601 WCHAR *str = strdupW( languages ), *p, *q;
602 LANGID *ret;
603
604 if (!str) return NULL;
605 for (p = q = str; (q = strchrW( q, ',' )); q++) count++;
606
607 if (!(ret = msi_alloc( count * sizeof(LANGID) )))
608 {
609 msi_free( str );
610 return NULL;
611 }
612 i = 0;
613 while (*p)
614 {
615 q = strchrW( p, ',' );
616 if (q) *q = 0;
617 ret[i] = atoiW( p );
618 if (!q) break;
619 p = q + 1;
620 i++;
621 }
622 msi_free( str );
623 *num_ids = count;
624 return ret;
625 }
626
627 static BOOL match_languages( const void *version, const WCHAR *languages )
628 {
629 struct lang
630 {
631 USHORT id;
632 USHORT codepage;
633 } *lang;
634 DWORD len, num_ids, i, j;
635 BOOL found = FALSE;
636 LANGID *ids;
637
638 if (!languages || !languages[0]) return TRUE;
639 if (!VerQueryValueW( version, szLangResource, (void **)&lang, &len )) return FALSE;
640 if (!(ids = parse_languages( languages, &num_ids ))) return FALSE;
641
642 for (i = 0; i < num_ids; i++)
643 {
644 found = FALSE;
645 for (j = 0; j < len / sizeof(struct lang); j++)
646 {
647 if (!ids[i] || ids[i] == lang[j].id) found = TRUE;
648 }
649 if (!found) goto done;
650 }
651
652 done:
653 msi_free( ids );
654 return found;
655 }
656
657 /* Sets *matches to whether the file (whose path is filePath) matches the
658 * versions set in sig.
659 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
660 * something else if an install-halting error occurs.
661 */
662 static UINT ACTION_FileVersionMatches(const MSISIGNATURE *sig, LPCWSTR filePath,
663 BOOL *matches)
664 {
665 UINT len;
666 void *version;
667 VS_FIXEDFILEINFO *info = NULL;
668 DWORD zero, size = GetFileVersionInfoSizeW( filePath, &zero );
669
670 *matches = FALSE;
671
672 if (!size) return ERROR_SUCCESS;
673 if (!(version = msi_alloc( size ))) return ERROR_OUTOFMEMORY;
674
675 if (GetFileVersionInfoW( filePath, 0, size, version ))
676 VerQueryValueW( version, szBackSlash, (void **)&info, &len );
677
678 if (info)
679 {
680 TRACE("comparing file version %d.%d.%d.%d:\n",
681 HIWORD(info->dwFileVersionMS),
682 LOWORD(info->dwFileVersionMS),
683 HIWORD(info->dwFileVersionLS),
684 LOWORD(info->dwFileVersionLS));
685 if (info->dwFileVersionMS < sig->MinVersionMS
686 || (info->dwFileVersionMS == sig->MinVersionMS &&
687 info->dwFileVersionLS < sig->MinVersionLS))
688 {
689 TRACE("less than minimum version %d.%d.%d.%d\n",
690 HIWORD(sig->MinVersionMS),
691 LOWORD(sig->MinVersionMS),
692 HIWORD(sig->MinVersionLS),
693 LOWORD(sig->MinVersionLS));
694 }
695 else if ((sig->MaxVersionMS || sig->MaxVersionLS) &&
696 (info->dwFileVersionMS > sig->MaxVersionMS ||
697 (info->dwFileVersionMS == sig->MaxVersionMS &&
698 info->dwFileVersionLS > sig->MaxVersionLS)))
699 {
700 TRACE("greater than maximum version %d.%d.%d.%d\n",
701 HIWORD(sig->MaxVersionMS),
702 LOWORD(sig->MaxVersionMS),
703 HIWORD(sig->MaxVersionLS),
704 LOWORD(sig->MaxVersionLS));
705 }
706 else if (!match_languages( version, sig->Languages ))
707 {
708 TRACE("languages %s not supported\n", debugstr_w( sig->Languages ));
709 }
710 else *matches = TRUE;
711 }
712 msi_free( version );
713 return ERROR_SUCCESS;
714 }
715
716 /* Sets *matches to whether the file in findData matches that in sig.
717 * fullFilePath is assumed to be the full path of the file specified in
718 * findData, which may be necessary to compare the version.
719 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
720 * something else if an install-halting error occurs.
721 */
722 static UINT ACTION_FileMatchesSig(const MSISIGNATURE *sig,
723 const WIN32_FIND_DATAW *findData, LPCWSTR fullFilePath, BOOL *matches)
724 {
725 UINT rc = ERROR_SUCCESS;
726
727 *matches = TRUE;
728 /* assumes the caller has already ensured the filenames match, so check
729 * the other fields..
730 */
731 if (sig->MinTime.dwLowDateTime || sig->MinTime.dwHighDateTime)
732 {
733 if (findData->ftCreationTime.dwHighDateTime <
734 sig->MinTime.dwHighDateTime ||
735 (findData->ftCreationTime.dwHighDateTime == sig->MinTime.dwHighDateTime
736 && findData->ftCreationTime.dwLowDateTime <
737 sig->MinTime.dwLowDateTime))
738 *matches = FALSE;
739 }
740 if (*matches && (sig->MaxTime.dwLowDateTime || sig->MaxTime.dwHighDateTime))
741 {
742 if (findData->ftCreationTime.dwHighDateTime >
743 sig->MaxTime.dwHighDateTime ||
744 (findData->ftCreationTime.dwHighDateTime == sig->MaxTime.dwHighDateTime
745 && findData->ftCreationTime.dwLowDateTime >
746 sig->MaxTime.dwLowDateTime))
747 *matches = FALSE;
748 }
749 if (*matches && sig->MinSize && findData->nFileSizeLow < sig->MinSize)
750 *matches = FALSE;
751 if (*matches && sig->MaxSize && findData->nFileSizeLow > sig->MaxSize)
752 *matches = FALSE;
753 if (*matches && (sig->MinVersionMS || sig->MinVersionLS ||
754 sig->MaxVersionMS || sig->MaxVersionLS))
755 rc = ACTION_FileVersionMatches(sig, fullFilePath, matches);
756 return rc;
757 }
758
759 /* Recursively searches the directory dir for files that match the signature
760 * sig, up to (depth + 1) levels deep. That is, if depth is 0, it searches dir
761 * (and only dir). If depth is 1, searches dir and its immediate
762 * subdirectories.
763 * Assumes sig->File is not NULL.
764 * Returns ERROR_SUCCESS on success (which may include non-critical errors),
765 * something else on failures which should halt the install.
766 */
767 static UINT ACTION_RecurseSearchDirectory(MSIPACKAGE *package, LPWSTR *appValue,
768 MSISIGNATURE *sig, LPCWSTR dir, int depth)
769 {
770 HANDLE hFind;
771 WIN32_FIND_DATAW findData;
772 UINT rc = ERROR_SUCCESS;
773 size_t dirLen = lstrlenW(dir), fileLen = lstrlenW(sig->File);
774 WCHAR subpath[MAX_PATH];
775 WCHAR *buf;
776 DWORD len;
777
778 static const WCHAR starDotStarW[] = { '*','.','*',0 };
779
780 TRACE("Searching directory %s for file %s, depth %d\n", debugstr_w(dir),
781 debugstr_w(sig->File), depth);
782
783 if (depth < 0)
784 return ERROR_SUCCESS;
785
786 *appValue = NULL;
787 /* We need the buffer in both paths below, so go ahead and allocate it
788 * here. Add two because we might need to add a backslash if the dir name
789 * isn't backslash-terminated.
790 */
791 len = dirLen + max(fileLen, strlenW(starDotStarW)) + 2;
792 buf = msi_alloc(len * sizeof(WCHAR));
793 if (!buf)
794 return ERROR_OUTOFMEMORY;
795
796 lstrcpyW(buf, dir);
797 PathAddBackslashW(buf);
798 lstrcatW(buf, sig->File);
799
800 hFind = FindFirstFileW(buf, &findData);
801 if (hFind != INVALID_HANDLE_VALUE)
802 {
803 if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
804 {
805 BOOL matches;
806
807 rc = ACTION_FileMatchesSig(sig, &findData, buf, &matches);
808 if (rc == ERROR_SUCCESS && matches)
809 {
810 TRACE("found file, returning %s\n", debugstr_w(buf));
811 *appValue = buf;
812 }
813 }
814 FindClose(hFind);
815 }
816
817 if (rc == ERROR_SUCCESS && !*appValue)
818 {
819 lstrcpyW(buf, dir);
820 PathAddBackslashW(buf);
821 lstrcatW(buf, starDotStarW);
822
823 hFind = FindFirstFileW(buf, &findData);
824 if (hFind != INVALID_HANDLE_VALUE)
825 {
826 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
827 strcmpW( findData.cFileName, szDot ) &&
828 strcmpW( findData.cFileName, szDotDot ))
829 {
830 lstrcpyW(subpath, dir);
831 PathAppendW(subpath, findData.cFileName);
832 rc = ACTION_RecurseSearchDirectory(package, appValue, sig,
833 subpath, depth - 1);
834 }
835
836 while (rc == ERROR_SUCCESS && !*appValue &&
837 FindNextFileW(hFind, &findData) != 0)
838 {
839 if (!strcmpW( findData.cFileName, szDot ) ||
840 !strcmpW( findData.cFileName, szDotDot ))
841 continue;
842
843 lstrcpyW(subpath, dir);
844 PathAppendW(subpath, findData.cFileName);
845 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
846 rc = ACTION_RecurseSearchDirectory(package, appValue,
847 sig, subpath, depth - 1);
848 }
849
850 FindClose(hFind);
851 }
852 }
853
854 if (*appValue != buf)
855 msi_free(buf);
856
857 return rc;
858 }
859
860 static UINT ACTION_CheckDirectory(MSIPACKAGE *package, LPCWSTR dir,
861 LPWSTR *appValue)
862 {
863 DWORD attr = GetFileAttributesW(dir);
864
865 if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY))
866 {
867 TRACE("directory exists, returning %s\n", debugstr_w(dir));
868 *appValue = strdupW(dir);
869 }
870
871 return ERROR_SUCCESS;
872 }
873
874 static BOOL ACTION_IsFullPath(LPCWSTR path)
875 {
876 WCHAR first = toupperW(path[0]);
877 BOOL ret;
878
879 if (first >= 'A' && first <= 'Z' && path[1] == ':')
880 ret = TRUE;
881 else if (path[0] == '\\' && path[1] == '\\')
882 ret = TRUE;
883 else
884 ret = FALSE;
885 return ret;
886 }
887
888 static UINT ACTION_SearchDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
889 LPCWSTR path, int depth, LPWSTR *appValue)
890 {
891 UINT rc;
892 DWORD attr;
893 LPWSTR val = NULL;
894
895 TRACE("%p, %p, %s, %d, %p\n", package, sig, debugstr_w(path), depth,
896 appValue);
897
898 if (ACTION_IsFullPath(path))
899 {
900 if (sig->File)
901 rc = ACTION_RecurseSearchDirectory(package, &val, sig, path, depth);
902 else
903 {
904 /* Recursively searching a directory makes no sense when the
905 * directory to search is the thing you're trying to find.
906 */
907 rc = ACTION_CheckDirectory(package, path, &val);
908 }
909 }
910 else
911 {
912 WCHAR pathWithDrive[MAX_PATH] = { 'C',':','\\',0 };
913 DWORD drives = GetLogicalDrives();
914 int i;
915
916 rc = ERROR_SUCCESS;
917 for (i = 0; rc == ERROR_SUCCESS && !val && i < 26; i++)
918 {
919 if (!(drives & (1 << i)))
920 continue;
921
922 pathWithDrive[0] = 'A' + i;
923 if (GetDriveTypeW(pathWithDrive) != DRIVE_FIXED)
924 continue;
925
926 lstrcpynW(pathWithDrive + 3, path,
927 sizeof(pathWithDrive) / sizeof(pathWithDrive[0]) - 3);
928
929 if (sig->File)
930 rc = ACTION_RecurseSearchDirectory(package, &val, sig,
931 pathWithDrive, depth);
932 else
933 rc = ACTION_CheckDirectory(package, pathWithDrive, &val);
934 }
935 }
936
937 attr = GetFileAttributesW(val);
938 if (attr != INVALID_FILE_ATTRIBUTES &&
939 (attr & FILE_ATTRIBUTE_DIRECTORY) &&
940 val && val[lstrlenW(val) - 1] != '\\')
941 {
942 val = msi_realloc(val, (lstrlenW(val) + 2) * sizeof(WCHAR));
943 if (!val)
944 rc = ERROR_OUTOFMEMORY;
945 else
946 PathAddBackslashW(val);
947 }
948
949 *appValue = val;
950
951 TRACE("returning %d\n", rc);
952 return rc;
953 }
954
955 static UINT ACTION_AppSearchSigName(MSIPACKAGE *package, LPCWSTR sigName,
956 MSISIGNATURE *sig, LPWSTR *appValue);
957
958 static UINT ACTION_AppSearchDr(MSIPACKAGE *package, LPWSTR *appValue, MSISIGNATURE *sig)
959 {
960 static const WCHAR query[] = {
961 's','e','l','e','c','t',' ','*',' ',
962 'f','r','o','m',' ',
963 'D','r','L','o','c','a','t','o','r',' ',
964 'w','h','e','r','e',' ',
965 'S','i','g','n','a','t','u','r','e','_',' ','=',' ', '\'','%','s','\'',0};
966 LPWSTR parent = NULL;
967 LPCWSTR parentName;
968 WCHAR path[MAX_PATH];
969 WCHAR expanded[MAX_PATH];
970 MSIRECORD *row;
971 int depth;
972 DWORD sz, attr;
973 UINT rc;
974
975 TRACE("%s\n", debugstr_w(sig->Name));
976
977 *appValue = NULL;
978
979 row = MSI_QueryGetRecord( package->db, query, sig->Name );
980 if (!row)
981 {
982 TRACE("failed to query DrLocator for %s\n", debugstr_w(sig->Name));
983 return ERROR_SUCCESS;
984 }
985
986 /* check whether parent is set */
987 parentName = MSI_RecordGetString(row, 2);
988 if (parentName)
989 {
990 MSISIGNATURE parentSig;
991
992 ACTION_AppSearchSigName(package, parentName, &parentSig, &parent);
993 ACTION_FreeSignature(&parentSig);
994 if (!parent)
995 {
996 msiobj_release(&row->hdr);
997 return ERROR_SUCCESS;
998 }
999 }
1000
1001 sz = MAX_PATH;
1002 MSI_RecordGetStringW(row, 3, path, &sz);
1003
1004 if (MSI_RecordIsNull(row,4))
1005 depth = 0;
1006 else
1007 depth = MSI_RecordGetInteger(row,4);
1008
1009 if (sz)
1010 ACTION_ExpandAnyPath(package, path, expanded, MAX_PATH);
1011 else
1012 strcpyW(expanded, path);
1013
1014 if (parent)
1015 {
1016 attr = GetFileAttributesW(parent);
1017 if (attr != INVALID_FILE_ATTRIBUTES &&
1018 !(attr & FILE_ATTRIBUTE_DIRECTORY))
1019 {
1020 PathRemoveFileSpecW(parent);
1021 PathAddBackslashW(parent);
1022 }
1023
1024 strcpyW(path, parent);
1025 strcatW(path, expanded);
1026 }
1027 else if (sz)
1028 strcpyW(path, expanded);
1029
1030 PathAddBackslashW(path);
1031
1032 rc = ACTION_SearchDirectory(package, sig, path, depth, appValue);
1033
1034 msi_free(parent);
1035 msiobj_release(&row->hdr);
1036
1037 TRACE("returning %d\n", rc);
1038 return rc;
1039 }
1040
1041 static UINT ACTION_AppSearchSigName(MSIPACKAGE *package, LPCWSTR sigName,
1042 MSISIGNATURE *sig, LPWSTR *appValue)
1043 {
1044 UINT rc;
1045
1046 *appValue = NULL;
1047 rc = ACTION_AppSearchGetSignature(package, sig, sigName);
1048 if (rc == ERROR_SUCCESS)
1049 {
1050 rc = ACTION_AppSearchComponents(package, appValue, sig);
1051 if (rc == ERROR_SUCCESS && !*appValue)
1052 {
1053 rc = ACTION_AppSearchReg(package, appValue, sig);
1054 if (rc == ERROR_SUCCESS && !*appValue)
1055 {
1056 rc = ACTION_AppSearchIni(package, appValue, sig);
1057 if (rc == ERROR_SUCCESS && !*appValue)
1058 rc = ACTION_AppSearchDr(package, appValue, sig);
1059 }
1060 }
1061 }
1062 return rc;
1063 }
1064
1065 static UINT iterate_appsearch(MSIRECORD *row, LPVOID param)
1066 {
1067 MSIPACKAGE *package = param;
1068 LPCWSTR propName, sigName;
1069 LPWSTR value = NULL;
1070 MSISIGNATURE sig;
1071 MSIRECORD *uirow;
1072 UINT r;
1073
1074 /* get property and signature */
1075 propName = MSI_RecordGetString(row, 1);
1076 sigName = MSI_RecordGetString(row, 2);
1077
1078 TRACE("%s %s\n", debugstr_w(propName), debugstr_w(sigName));
1079
1080 r = ACTION_AppSearchSigName(package, sigName, &sig, &value);
1081 if (value)
1082 {
1083 r = msi_set_property( package->db, propName, value, -1 );
1084 if (r == ERROR_SUCCESS && !strcmpW( propName, szSourceDir ))
1085 msi_reset_folders( package, TRUE );
1086
1087 msi_free(value);
1088 }
1089 ACTION_FreeSignature(&sig);
1090
1091 uirow = MSI_CreateRecord( 2 );
1092 MSI_RecordSetStringW( uirow, 1, propName );
1093 MSI_RecordSetStringW( uirow, 2, sigName );
1094 msi_ui_actiondata( package, szAppSearch, uirow );
1095 msiobj_release( &uirow->hdr );
1096
1097 return r;
1098 }
1099
1100 UINT ACTION_AppSearch(MSIPACKAGE *package)
1101 {
1102 static const WCHAR query[] = {
1103 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1104 'A','p','p','S','e','a','r','c','h',0};
1105 MSIQUERY *view;
1106 UINT r;
1107
1108 if (msi_action_is_unique(package, szAppSearch))
1109 {
1110 TRACE("Skipping AppSearch action: already done in UI sequence\n");
1111 return ERROR_SUCCESS;
1112 }
1113 else
1114 msi_register_unique_action(package, szAppSearch);
1115
1116 r = MSI_OpenQuery( package->db, &view, query );
1117 if (r != ERROR_SUCCESS)
1118 return ERROR_SUCCESS;
1119
1120 r = MSI_IterateRecords( view, NULL, iterate_appsearch, package );
1121 msiobj_release( &view->hdr );
1122 return r;
1123 }
1124
1125 static UINT ITERATE_CCPSearch(MSIRECORD *row, LPVOID param)
1126 {
1127 MSIPACKAGE *package = param;
1128 LPCWSTR signature;
1129 LPWSTR value = NULL;
1130 MSISIGNATURE sig;
1131 UINT r = ERROR_SUCCESS;
1132
1133 static const WCHAR success[] = {'C','C','P','_','S','u','c','c','e','s','s',0};
1134
1135 signature = MSI_RecordGetString(row, 1);
1136
1137 TRACE("%s\n", debugstr_w(signature));
1138
1139 ACTION_AppSearchSigName(package, signature, &sig, &value);
1140 if (value)
1141 {
1142 TRACE("Found signature %s\n", debugstr_w(signature));
1143 msi_set_property( package->db, success, szOne, -1 );
1144 msi_free(value);
1145 r = ERROR_NO_MORE_ITEMS;
1146 }
1147
1148 ACTION_FreeSignature(&sig);
1149
1150 return r;
1151 }
1152
1153 UINT ACTION_CCPSearch(MSIPACKAGE *package)
1154 {
1155 static const WCHAR query[] = {
1156 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1157 'C','C','P','S','e','a','r','c','h',0};
1158 MSIQUERY *view;
1159 UINT r;
1160
1161 if (msi_action_is_unique(package, szCCPSearch))
1162 {
1163 TRACE("Skipping AppSearch action: already done in UI sequence\n");
1164 return ERROR_SUCCESS;
1165 }
1166 else
1167 msi_register_unique_action(package, szCCPSearch);
1168
1169 r = MSI_OpenQuery(package->db, &view, query);
1170 if (r != ERROR_SUCCESS)
1171 return ERROR_SUCCESS;
1172
1173 r = MSI_IterateRecords(view, NULL, ITERATE_CCPSearch, package);
1174 msiobj_release(&view->hdr);
1175 return r;
1176 }