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