services: Move service cleanup code to separate function.
[wine/multimedia.git] / dlls / msi / appsearch.c
blob0010fd895d3ea96e47b0d6afcecb0363961e177c
1 /*
2 * Implementation of the AppSearch action of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Juan Lang
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winreg.h"
27 #include "msi.h"
28 #include "msiquery.h"
29 #include "msidefs.h"
30 #include "winver.h"
31 #include "shlwapi.h"
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
34 #include "msipriv.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38 typedef struct tagMSISIGNATURE
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;
53 void msi_parse_version_string(LPCWSTR verStr, PDWORD ms, PDWORD ls)
55 const WCHAR *ptr;
56 int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
58 x1 = atoiW(verStr);
59 ptr = strchrW(verStr, '.');
60 if (ptr)
62 x2 = atoiW(ptr + 1);
63 ptr = strchrW(ptr + 1, '.');
65 if (ptr)
67 x3 = atoiW(ptr + 1);
68 ptr = strchrW(ptr + 1, '.');
70 if (ptr)
71 x4 = atoiW(ptr + 1);
72 /* FIXME: byte-order dependent? */
73 *ms = x1 << 16 | x2;
74 if (ls) *ls = x3 << 16 | x4;
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.
84 static UINT ACTION_AppSearchGetSignature(MSIPACKAGE *package, MSISIGNATURE *sig, LPCWSTR name)
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, p;
93 MSIRECORD *row;
94 DWORD time;
96 TRACE("package %p, sig %p\n", package, sig);
98 memset(sig, 0, sizeof(*sig));
99 sig->Name = name;
100 row = MSI_QueryGetRecord( package->db, query, name );
101 if (!row)
103 TRACE("failed to query signature for %s\n", debugstr_w(name));
104 return ERROR_SUCCESS;
107 /* get properties */
108 sig->File = msi_dup_record_field(row,2);
109 if ((p = strchrW(sig->File, '|')))
111 p++;
112 memmove(sig->File, p, (strlenW(p) + 1) * sizeof(WCHAR));
115 minVersion = msi_dup_record_field(row,3);
116 if (minVersion)
118 msi_parse_version_string( minVersion, &sig->MinVersionMS, &sig->MinVersionLS );
119 msi_free( minVersion );
121 maxVersion = msi_dup_record_field(row,4);
122 if (maxVersion)
124 msi_parse_version_string( maxVersion, &sig->MaxVersionMS, &sig->MaxVersionLS );
125 msi_free( maxVersion );
127 sig->MinSize = MSI_RecordGetInteger(row,5);
128 if (sig->MinSize == MSI_NULL_INTEGER)
129 sig->MinSize = 0;
130 sig->MaxSize = MSI_RecordGetInteger(row,6);
131 if (sig->MaxSize == MSI_NULL_INTEGER)
132 sig->MaxSize = 0;
133 sig->Languages = msi_dup_record_field(row,9);
134 time = MSI_RecordGetInteger(row,7);
135 if (time != MSI_NULL_INTEGER)
136 DosDateTimeToFileTime(HIWORD(time), LOWORD(time), &sig->MinTime);
137 time = MSI_RecordGetInteger(row,8);
138 if (time != MSI_NULL_INTEGER)
139 DosDateTimeToFileTime(HIWORD(time), LOWORD(time), &sig->MaxTime);
141 TRACE("Found file name %s for Signature_ %s;\n",
142 debugstr_w(sig->File), debugstr_w(name));
143 TRACE("MinVersion is %d.%d.%d.%d\n", HIWORD(sig->MinVersionMS),
144 LOWORD(sig->MinVersionMS), HIWORD(sig->MinVersionLS),
145 LOWORD(sig->MinVersionLS));
146 TRACE("MaxVersion is %d.%d.%d.%d\n", HIWORD(sig->MaxVersionMS),
147 LOWORD(sig->MaxVersionMS), HIWORD(sig->MaxVersionLS),
148 LOWORD(sig->MaxVersionLS));
149 TRACE("MinSize is %d, MaxSize is %d;\n", sig->MinSize, sig->MaxSize);
150 TRACE("Languages is %s\n", debugstr_w(sig->Languages));
152 msiobj_release( &row->hdr );
154 return ERROR_SUCCESS;
157 /* Frees any memory allocated in sig */
158 static void ACTION_FreeSignature(MSISIGNATURE *sig)
160 msi_free(sig->File);
161 msi_free(sig->Languages);
164 static LPWSTR app_search_file(LPWSTR path, MSISIGNATURE *sig)
166 VS_FIXEDFILEINFO *info;
167 DWORD attr, handle, size;
168 LPWSTR val = NULL;
169 LPBYTE buffer;
171 if (!sig->File)
173 PathRemoveFileSpecW(path);
174 PathAddBackslashW(path);
176 attr = GetFileAttributesW(path);
177 if (attr != INVALID_FILE_ATTRIBUTES &&
178 (attr & FILE_ATTRIBUTE_DIRECTORY))
179 return strdupW(path);
181 return NULL;
184 attr = GetFileAttributesW(path);
185 if (attr == INVALID_FILE_ATTRIBUTES ||
186 (attr & FILE_ATTRIBUTE_DIRECTORY))
187 return NULL;
189 size = GetFileVersionInfoSizeW(path, &handle);
190 if (!size)
191 return strdupW(path);
193 buffer = msi_alloc(size);
194 if (!buffer)
195 return NULL;
197 if (!GetFileVersionInfoW(path, 0, size, buffer))
198 goto done;
200 if (!VerQueryValueW(buffer, szBackSlash, (LPVOID)&info, &size) || !info)
201 goto done;
203 if (sig->MinVersionLS || sig->MinVersionMS)
205 if (info->dwFileVersionMS < sig->MinVersionMS)
206 goto done;
208 if (info->dwFileVersionMS == sig->MinVersionMS &&
209 info->dwFileVersionLS < sig->MinVersionLS)
210 goto done;
213 if (sig->MaxVersionLS || sig->MaxVersionMS)
215 if (info->dwFileVersionMS > sig->MaxVersionMS)
216 goto done;
218 if (info->dwFileVersionMS == sig->MaxVersionMS &&
219 info->dwFileVersionLS > sig->MaxVersionLS)
220 goto done;
223 val = strdupW(path);
225 done:
226 msi_free(buffer);
227 return val;
230 static UINT ACTION_AppSearchComponents(MSIPACKAGE *package, LPWSTR *appValue, MSISIGNATURE *sig)
232 static const WCHAR query[] = {
233 'S','E','L','E','C','T',' ','*',' ',
234 'F','R','O','M',' ',
235 '`','C','o','m','p','L','o','c','a','t','o','r','`',' ',
236 'W','H','E','R','E',' ','`','S','i','g','n','a','t','u','r','e','_','`',' ','=',' ',
237 '\'','%','s','\'',0};
238 static const WCHAR sigquery[] = {
239 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
240 '`','S','i','g','n','a','t','u','r','e','`',' ',
241 'W','H','E','R','E',' ','`','S','i','g','n','a','t','u','r','e','`',' ','=',' ',
242 '\'','%','s','\'',0};
244 MSIRECORD *row, *rec;
245 LPCWSTR signature, guid;
246 BOOL sigpresent = TRUE;
247 BOOL isdir;
248 UINT type;
249 WCHAR path[MAX_PATH];
250 DWORD size = MAX_PATH;
251 LPWSTR ptr;
252 DWORD attr;
254 TRACE("%s\n", debugstr_w(sig->Name));
256 *appValue = NULL;
258 row = MSI_QueryGetRecord(package->db, query, sig->Name);
259 if (!row)
261 TRACE("failed to query CompLocator for %s\n", debugstr_w(sig->Name));
262 return ERROR_SUCCESS;
265 signature = MSI_RecordGetString(row, 1);
266 guid = MSI_RecordGetString(row, 2);
267 type = MSI_RecordGetInteger(row, 3);
269 rec = MSI_QueryGetRecord(package->db, sigquery, signature);
270 if (!rec)
271 sigpresent = FALSE;
273 *path = '\0';
274 MsiLocateComponentW(guid, path, &size);
275 if (!*path)
276 goto done;
278 attr = GetFileAttributesW(path);
279 if (attr == INVALID_FILE_ATTRIBUTES)
280 goto done;
282 isdir = (attr & FILE_ATTRIBUTE_DIRECTORY);
284 if (type != msidbLocatorTypeDirectory && sigpresent && !isdir)
286 *appValue = app_search_file(path, sig);
288 else if (!sigpresent && (type != msidbLocatorTypeDirectory || isdir))
290 if (type == msidbLocatorTypeFileName)
292 ptr = strrchrW(path, '\\');
293 *(ptr + 1) = '\0';
295 else
296 PathAddBackslashW(path);
298 *appValue = strdupW(path);
300 else if (sigpresent)
302 PathAddBackslashW(path);
303 lstrcatW(path, MSI_RecordGetString(rec, 2));
305 attr = GetFileAttributesW(path);
306 if (attr != INVALID_FILE_ATTRIBUTES &&
307 !(attr & FILE_ATTRIBUTE_DIRECTORY))
308 *appValue = strdupW(path);
311 done:
312 if (rec) msiobj_release(&rec->hdr);
313 msiobj_release(&row->hdr);
314 return ERROR_SUCCESS;
317 static void ACTION_ConvertRegValue(DWORD regType, const BYTE *value, DWORD sz,
318 LPWSTR *appValue)
320 static const WCHAR dwordFmt[] = { '#','%','d','\0' };
321 static const WCHAR binPre[] = { '#','x','\0' };
322 static const WCHAR binFmt[] = { '%','0','2','X','\0' };
323 LPWSTR ptr;
324 DWORD i;
326 switch (regType)
328 case REG_SZ:
329 if (*(LPCWSTR)value == '#')
331 /* escape leading pound with another */
332 *appValue = msi_alloc(sz + sizeof(WCHAR));
333 (*appValue)[0] = '#';
334 strcpyW(*appValue + 1, (LPCWSTR)value);
336 else
338 *appValue = msi_alloc(sz);
339 strcpyW(*appValue, (LPCWSTR)value);
341 break;
342 case REG_DWORD:
343 /* 7 chars for digits, 1 for NULL, 1 for #, and 1 for sign
344 * char if needed
346 *appValue = msi_alloc(10 * sizeof(WCHAR));
347 sprintfW(*appValue, dwordFmt, *(const DWORD *)value);
348 break;
349 case REG_EXPAND_SZ:
350 sz = ExpandEnvironmentStringsW((LPCWSTR)value, NULL, 0);
351 *appValue = msi_alloc(sz * sizeof(WCHAR));
352 ExpandEnvironmentStringsW((LPCWSTR)value, *appValue, sz);
353 break;
354 case REG_BINARY:
355 /* #x<nibbles>\0 */
356 *appValue = msi_alloc((sz * 2 + 3) * sizeof(WCHAR));
357 lstrcpyW(*appValue, binPre);
358 ptr = *appValue + lstrlenW(binPre);
359 for (i = 0; i < sz; i++, ptr += 2)
360 sprintfW(ptr, binFmt, value[i]);
361 break;
362 default:
363 WARN("unimplemented for values of type %d\n", regType);
364 *appValue = NULL;
368 static UINT ACTION_SearchDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
369 LPCWSTR path, int depth, LPWSTR *appValue);
371 static UINT ACTION_AppSearchReg(MSIPACKAGE *package, LPWSTR *appValue, MSISIGNATURE *sig)
373 static const WCHAR query[] = {
374 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
375 'R','e','g','L','o','c','a','t','o','r',' ','W','H','E','R','E',' ',
376 'S','i','g','n','a','t','u','r','e','_',' ','=',' ', '\'','%','s','\'',0};
377 const WCHAR *keyPath, *valueName;
378 WCHAR *deformatted = NULL, *ptr = NULL, *end;
379 int root, type;
380 HKEY rootKey, key = NULL;
381 DWORD sz = 0, regType;
382 LPBYTE value = NULL;
383 MSIRECORD *row;
384 UINT rc;
386 TRACE("%s\n", debugstr_w(sig->Name));
388 *appValue = NULL;
390 row = MSI_QueryGetRecord( package->db, query, sig->Name );
391 if (!row)
393 TRACE("failed to query RegLocator for %s\n", debugstr_w(sig->Name));
394 return ERROR_SUCCESS;
397 root = MSI_RecordGetInteger(row, 2);
398 keyPath = MSI_RecordGetString(row, 3);
399 valueName = MSI_RecordGetString(row, 4);
400 type = MSI_RecordGetInteger(row, 5);
402 deformat_string(package, keyPath, &deformatted);
404 switch (root)
406 case msidbRegistryRootClassesRoot:
407 rootKey = HKEY_CLASSES_ROOT;
408 break;
409 case msidbRegistryRootCurrentUser:
410 rootKey = HKEY_CURRENT_USER;
411 break;
412 case msidbRegistryRootLocalMachine:
413 rootKey = HKEY_LOCAL_MACHINE;
414 break;
415 case msidbRegistryRootUsers:
416 rootKey = HKEY_USERS;
417 break;
418 default:
419 WARN("Unknown root key %d\n", root);
420 goto end;
423 rc = RegOpenKeyW(rootKey, deformatted, &key);
424 if (rc)
426 TRACE("RegOpenKeyW returned %d\n", rc);
427 goto end;
430 msi_free(deformatted);
431 deformat_string(package, valueName, &deformatted);
433 rc = RegQueryValueExW(key, deformatted, NULL, NULL, NULL, &sz);
434 if (rc)
436 TRACE("RegQueryValueExW returned %d\n", rc);
437 goto end;
439 /* FIXME: sanity-check sz before allocating (is there an upper-limit
440 * on the value of a property?)
442 value = msi_alloc( sz );
443 rc = RegQueryValueExW(key, deformatted, NULL, &regType, value, &sz);
444 if (rc)
446 TRACE("RegQueryValueExW returned %d\n", rc);
447 goto end;
450 /* bail out if the registry key is empty */
451 if (sz == 0)
452 goto end;
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;
460 switch (type & 0x0f)
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));
475 end:
476 msi_free( value );
477 RegCloseKey( key );
478 msi_free( deformatted );
480 msiobj_release(&row->hdr);
481 return ERROR_SUCCESS;
484 static LPWSTR get_ini_field(LPWSTR buf, int field)
486 LPWSTR beg, end;
487 int i = 1;
489 if (field == 0)
490 return strdupW(buf);
492 beg = buf;
493 while ((end = strchrW(beg, ',')) && i < field)
495 beg = end + 1;
496 while (*beg && *beg == ' ')
497 beg++;
499 i++;
502 end = strchrW(beg, ',');
503 if (!end)
504 end = beg + lstrlenW(beg);
506 *end = '\0';
507 return strdupW(beg);
510 static UINT ACTION_AppSearchIni(MSIPACKAGE *package, LPWSTR *appValue,
511 MSISIGNATURE *sig)
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];
524 TRACE("%s\n", debugstr_w(sig->Name));
526 *appValue = NULL;
528 row = MSI_QueryGetRecord( package->db, query, sig->Name );
529 if (!row)
531 TRACE("failed to query IniLocator for %s\n", debugstr_w(sig->Name));
532 return ERROR_SUCCESS;
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;
545 GetPrivateProfileStringW(section, key, NULL, buf, MAX_PATH, fileName);
546 if (buf[0])
548 switch (type & 0x0f)
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;
562 msi_free(fileName);
563 msi_free(section);
564 msi_free(key);
566 msiobj_release(&row->hdr);
568 return ERROR_SUCCESS;
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?
580 static void ACTION_ExpandAnyPath(MSIPACKAGE *package, WCHAR *src, WCHAR *dst,
581 size_t len)
583 WCHAR *ptr, *deformatted;
585 if (!src || !dst || !len)
587 if (dst) *dst = '\0';
588 return;
591 dst[0] = '\0';
593 /* Ignore the short portion of the path */
594 if ((ptr = strchrW(src, '|')))
595 ptr++;
596 else
597 ptr = src;
599 deformat_string(package, ptr, &deformatted);
600 if (!deformatted || strlenW(deformatted) > len - 1)
602 msi_free(deformatted);
603 return;
606 lstrcpyW(dst, deformatted);
607 dst[lstrlenW(deformatted)] = '\0';
608 msi_free(deformatted);
611 /* Sets *matches to whether the file (whose path is filePath) matches the
612 * versions set in sig.
613 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
614 * something else if an install-halting error occurs.
616 static UINT ACTION_FileVersionMatches(const MSISIGNATURE *sig, LPCWSTR filePath,
617 BOOL *matches)
619 UINT rc = ERROR_SUCCESS;
621 *matches = FALSE;
622 if (sig->Languages)
624 FIXME(": need to check version for languages %s\n",
625 debugstr_w(sig->Languages));
627 else
629 DWORD zero, size = GetFileVersionInfoSizeW(filePath, &zero);
631 if (size)
633 LPVOID buf = msi_alloc( size);
635 if (buf)
637 UINT versionLen;
638 LPVOID subBlock = NULL;
640 if (GetFileVersionInfoW(filePath, 0, size, buf))
641 VerQueryValueW(buf, szBackSlash, &subBlock, &versionLen);
642 if (subBlock)
644 VS_FIXEDFILEINFO *info = subBlock;
646 TRACE("Comparing file version %d.%d.%d.%d:\n",
647 HIWORD(info->dwFileVersionMS),
648 LOWORD(info->dwFileVersionMS),
649 HIWORD(info->dwFileVersionLS),
650 LOWORD(info->dwFileVersionLS));
651 if (info->dwFileVersionMS < sig->MinVersionMS
652 || (info->dwFileVersionMS == sig->MinVersionMS &&
653 info->dwFileVersionLS < sig->MinVersionLS))
655 TRACE("Less than minimum version %d.%d.%d.%d\n",
656 HIWORD(sig->MinVersionMS),
657 LOWORD(sig->MinVersionMS),
658 HIWORD(sig->MinVersionLS),
659 LOWORD(sig->MinVersionLS));
661 else if ((sig->MaxVersionMS || sig->MaxVersionLS) &&
662 (info->dwFileVersionMS > sig->MaxVersionMS ||
663 (info->dwFileVersionMS == sig->MaxVersionMS &&
664 info->dwFileVersionLS > sig->MaxVersionLS)))
666 TRACE("Greater than maximum version %d.%d.%d.%d\n",
667 HIWORD(sig->MaxVersionMS),
668 LOWORD(sig->MaxVersionMS),
669 HIWORD(sig->MaxVersionLS),
670 LOWORD(sig->MaxVersionLS));
672 else
673 *matches = TRUE;
675 msi_free( buf);
677 else
678 rc = ERROR_OUTOFMEMORY;
681 return rc;
684 /* Sets *matches to whether the file in findData matches that in sig.
685 * fullFilePath is assumed to be the full path of the file specified in
686 * findData, which may be necessary to compare the version.
687 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
688 * something else if an install-halting error occurs.
690 static UINT ACTION_FileMatchesSig(const MSISIGNATURE *sig,
691 const WIN32_FIND_DATAW *findData, LPCWSTR fullFilePath, BOOL *matches)
693 UINT rc = ERROR_SUCCESS;
695 *matches = TRUE;
696 /* assumes the caller has already ensured the filenames match, so check
697 * the other fields..
699 if (sig->MinTime.dwLowDateTime || sig->MinTime.dwHighDateTime)
701 if (findData->ftCreationTime.dwHighDateTime <
702 sig->MinTime.dwHighDateTime ||
703 (findData->ftCreationTime.dwHighDateTime == sig->MinTime.dwHighDateTime
704 && findData->ftCreationTime.dwLowDateTime <
705 sig->MinTime.dwLowDateTime))
706 *matches = FALSE;
708 if (*matches && (sig->MaxTime.dwLowDateTime || sig->MaxTime.dwHighDateTime))
710 if (findData->ftCreationTime.dwHighDateTime >
711 sig->MaxTime.dwHighDateTime ||
712 (findData->ftCreationTime.dwHighDateTime == sig->MaxTime.dwHighDateTime
713 && findData->ftCreationTime.dwLowDateTime >
714 sig->MaxTime.dwLowDateTime))
715 *matches = FALSE;
717 if (*matches && sig->MinSize && findData->nFileSizeLow < sig->MinSize)
718 *matches = FALSE;
719 if (*matches && sig->MaxSize && findData->nFileSizeLow > sig->MaxSize)
720 *matches = FALSE;
721 if (*matches && (sig->MinVersionMS || sig->MinVersionLS ||
722 sig->MaxVersionMS || sig->MaxVersionLS))
723 rc = ACTION_FileVersionMatches(sig, fullFilePath, matches);
724 return rc;
727 /* Recursively searches the directory dir for files that match the signature
728 * sig, up to (depth + 1) levels deep. That is, if depth is 0, it searches dir
729 * (and only dir). If depth is 1, searches dir and its immediate
730 * subdirectories.
731 * Assumes sig->File is not NULL.
732 * Returns ERROR_SUCCESS on success (which may include non-critical errors),
733 * something else on failures which should halt the install.
735 static UINT ACTION_RecurseSearchDirectory(MSIPACKAGE *package, LPWSTR *appValue,
736 MSISIGNATURE *sig, LPCWSTR dir, int depth)
738 HANDLE hFind;
739 WIN32_FIND_DATAW findData;
740 UINT rc = ERROR_SUCCESS;
741 size_t dirLen = lstrlenW(dir), fileLen = lstrlenW(sig->File);
742 WCHAR subpath[MAX_PATH];
743 WCHAR *buf;
744 DWORD len;
746 static const WCHAR starDotStarW[] = { '*','.','*',0 };
748 TRACE("Searching directory %s for file %s, depth %d\n", debugstr_w(dir),
749 debugstr_w(sig->File), depth);
751 if (depth < 0)
752 return ERROR_SUCCESS;
754 *appValue = NULL;
755 /* We need the buffer in both paths below, so go ahead and allocate it
756 * here. Add two because we might need to add a backslash if the dir name
757 * isn't backslash-terminated.
759 len = dirLen + max(fileLen, strlenW(starDotStarW)) + 2;
760 buf = msi_alloc(len * sizeof(WCHAR));
761 if (!buf)
762 return ERROR_OUTOFMEMORY;
764 lstrcpyW(buf, dir);
765 PathAddBackslashW(buf);
766 lstrcatW(buf, sig->File);
768 hFind = FindFirstFileW(buf, &findData);
769 if (hFind != INVALID_HANDLE_VALUE)
771 if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
773 BOOL matches;
775 rc = ACTION_FileMatchesSig(sig, &findData, buf, &matches);
776 if (rc == ERROR_SUCCESS && matches)
778 TRACE("found file, returning %s\n", debugstr_w(buf));
779 *appValue = buf;
782 FindClose(hFind);
785 if (rc == ERROR_SUCCESS && !*appValue)
787 lstrcpyW(buf, dir);
788 PathAddBackslashW(buf);
789 lstrcatW(buf, starDotStarW);
791 hFind = FindFirstFileW(buf, &findData);
792 if (hFind != INVALID_HANDLE_VALUE)
794 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
795 strcmpW( findData.cFileName, szDot ) &&
796 strcmpW( findData.cFileName, szDotDot ))
798 lstrcpyW(subpath, dir);
799 PathAppendW(subpath, findData.cFileName);
800 rc = ACTION_RecurseSearchDirectory(package, appValue, sig,
801 subpath, depth - 1);
804 while (rc == ERROR_SUCCESS && !*appValue &&
805 FindNextFileW(hFind, &findData) != 0)
807 if (!strcmpW( findData.cFileName, szDot ) ||
808 !strcmpW( findData.cFileName, szDotDot ))
809 continue;
811 lstrcpyW(subpath, dir);
812 PathAppendW(subpath, findData.cFileName);
813 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
814 rc = ACTION_RecurseSearchDirectory(package, appValue,
815 sig, subpath, depth - 1);
818 FindClose(hFind);
822 if (*appValue != buf)
823 msi_free(buf);
825 return rc;
828 static UINT ACTION_CheckDirectory(MSIPACKAGE *package, LPCWSTR dir,
829 LPWSTR *appValue)
831 DWORD attr = GetFileAttributesW(dir);
833 if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY))
835 TRACE("directory exists, returning %s\n", debugstr_w(dir));
836 *appValue = strdupW(dir);
839 return ERROR_SUCCESS;
842 static BOOL ACTION_IsFullPath(LPCWSTR path)
844 WCHAR first = toupperW(path[0]);
845 BOOL ret;
847 if (first >= 'A' && first <= 'Z' && path[1] == ':')
848 ret = TRUE;
849 else if (path[0] == '\\' && path[1] == '\\')
850 ret = TRUE;
851 else
852 ret = FALSE;
853 return ret;
856 static UINT ACTION_SearchDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
857 LPCWSTR path, int depth, LPWSTR *appValue)
859 UINT rc;
860 DWORD attr;
861 LPWSTR val = NULL;
863 TRACE("%p, %p, %s, %d, %p\n", package, sig, debugstr_w(path), depth,
864 appValue);
866 if (ACTION_IsFullPath(path))
868 if (sig->File)
869 rc = ACTION_RecurseSearchDirectory(package, &val, sig, path, depth);
870 else
872 /* Recursively searching a directory makes no sense when the
873 * directory to search is the thing you're trying to find.
875 rc = ACTION_CheckDirectory(package, path, &val);
878 else
880 WCHAR pathWithDrive[MAX_PATH] = { 'C',':','\\',0 };
881 DWORD drives = GetLogicalDrives();
882 int i;
884 rc = ERROR_SUCCESS;
885 for (i = 0; rc == ERROR_SUCCESS && !val && i < 26; i++)
887 if (!(drives & (1 << i)))
888 continue;
890 pathWithDrive[0] = 'A' + i;
891 if (GetDriveTypeW(pathWithDrive) != DRIVE_FIXED)
892 continue;
894 lstrcpynW(pathWithDrive + 3, path,
895 sizeof(pathWithDrive) / sizeof(pathWithDrive[0]) - 3);
897 if (sig->File)
898 rc = ACTION_RecurseSearchDirectory(package, &val, sig,
899 pathWithDrive, depth);
900 else
901 rc = ACTION_CheckDirectory(package, pathWithDrive, &val);
905 attr = GetFileAttributesW(val);
906 if (attr != INVALID_FILE_ATTRIBUTES &&
907 (attr & FILE_ATTRIBUTE_DIRECTORY) &&
908 val && val[lstrlenW(val) - 1] != '\\')
910 val = msi_realloc(val, (lstrlenW(val) + 2) * sizeof(WCHAR));
911 if (!val)
912 rc = ERROR_OUTOFMEMORY;
913 else
914 PathAddBackslashW(val);
917 *appValue = val;
919 TRACE("returning %d\n", rc);
920 return rc;
923 static UINT ACTION_AppSearchSigName(MSIPACKAGE *package, LPCWSTR sigName,
924 MSISIGNATURE *sig, LPWSTR *appValue);
926 static UINT ACTION_AppSearchDr(MSIPACKAGE *package, LPWSTR *appValue, MSISIGNATURE *sig)
928 static const WCHAR query[] = {
929 's','e','l','e','c','t',' ','*',' ',
930 'f','r','o','m',' ',
931 'D','r','L','o','c','a','t','o','r',' ',
932 'w','h','e','r','e',' ',
933 'S','i','g','n','a','t','u','r','e','_',' ','=',' ', '\'','%','s','\'',0};
934 LPWSTR parent = NULL;
935 LPCWSTR parentName;
936 WCHAR path[MAX_PATH];
937 WCHAR expanded[MAX_PATH];
938 MSIRECORD *row;
939 int depth;
940 DWORD sz, attr;
941 UINT rc;
943 TRACE("%s\n", debugstr_w(sig->Name));
945 *appValue = NULL;
947 row = MSI_QueryGetRecord( package->db, query, sig->Name );
948 if (!row)
950 TRACE("failed to query DrLocator for %s\n", debugstr_w(sig->Name));
951 return ERROR_SUCCESS;
954 /* check whether parent is set */
955 parentName = MSI_RecordGetString(row, 2);
956 if (parentName)
958 MSISIGNATURE parentSig;
960 ACTION_AppSearchSigName(package, parentName, &parentSig, &parent);
961 ACTION_FreeSignature(&parentSig);
962 if (!parent)
964 msiobj_release(&row->hdr);
965 return ERROR_SUCCESS;
969 sz = MAX_PATH;
970 MSI_RecordGetStringW(row, 3, path, &sz);
972 if (MSI_RecordIsNull(row,4))
973 depth = 0;
974 else
975 depth = MSI_RecordGetInteger(row,4);
977 if (sz)
978 ACTION_ExpandAnyPath(package, path, expanded, MAX_PATH);
979 else
980 strcpyW(expanded, path);
982 if (parent)
984 attr = GetFileAttributesW(parent);
985 if (attr != INVALID_FILE_ATTRIBUTES &&
986 !(attr & FILE_ATTRIBUTE_DIRECTORY))
988 PathRemoveFileSpecW(parent);
989 PathAddBackslashW(parent);
992 strcpyW(path, parent);
993 strcatW(path, expanded);
995 else if (sz)
996 strcpyW(path, expanded);
998 PathAddBackslashW(path);
1000 rc = ACTION_SearchDirectory(package, sig, path, depth, appValue);
1002 msi_free(parent);
1003 msiobj_release(&row->hdr);
1005 TRACE("returning %d\n", rc);
1006 return rc;
1009 static UINT ACTION_AppSearchSigName(MSIPACKAGE *package, LPCWSTR sigName,
1010 MSISIGNATURE *sig, LPWSTR *appValue)
1012 UINT rc;
1014 *appValue = NULL;
1015 rc = ACTION_AppSearchGetSignature(package, sig, sigName);
1016 if (rc == ERROR_SUCCESS)
1018 rc = ACTION_AppSearchComponents(package, appValue, sig);
1019 if (rc == ERROR_SUCCESS && !*appValue)
1021 rc = ACTION_AppSearchReg(package, appValue, sig);
1022 if (rc == ERROR_SUCCESS && !*appValue)
1024 rc = ACTION_AppSearchIni(package, appValue, sig);
1025 if (rc == ERROR_SUCCESS && !*appValue)
1026 rc = ACTION_AppSearchDr(package, appValue, sig);
1030 return rc;
1033 static UINT iterate_appsearch(MSIRECORD *row, LPVOID param)
1035 MSIPACKAGE *package = param;
1036 LPCWSTR propName, sigName;
1037 LPWSTR value = NULL;
1038 MSISIGNATURE sig;
1039 MSIRECORD *uirow;
1040 UINT r;
1042 /* get property and signature */
1043 propName = MSI_RecordGetString(row, 1);
1044 sigName = MSI_RecordGetString(row, 2);
1046 TRACE("%s %s\n", debugstr_w(propName), debugstr_w(sigName));
1048 r = ACTION_AppSearchSigName(package, sigName, &sig, &value);
1049 if (value)
1051 r = msi_set_property( package->db, propName, value );
1052 if (r == ERROR_SUCCESS && !strcmpW( propName, szSourceDir ))
1053 msi_reset_folders( package, TRUE );
1055 msi_free(value);
1057 ACTION_FreeSignature(&sig);
1059 uirow = MSI_CreateRecord( 2 );
1060 MSI_RecordSetStringW( uirow, 1, propName );
1061 MSI_RecordSetStringW( uirow, 2, sigName );
1062 msi_ui_actiondata( package, szAppSearch, uirow );
1063 msiobj_release( &uirow->hdr );
1065 return r;
1068 UINT ACTION_AppSearch(MSIPACKAGE *package)
1070 static const WCHAR query[] = {
1071 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1072 'A','p','p','S','e','a','r','c','h',0};
1073 MSIQUERY *view;
1074 UINT r;
1076 if (msi_action_is_unique(package, szAppSearch))
1078 TRACE("Skipping AppSearch action: already done in UI sequence\n");
1079 return ERROR_SUCCESS;
1081 else
1082 msi_register_unique_action(package, szAppSearch);
1084 r = MSI_OpenQuery( package->db, &view, query );
1085 if (r != ERROR_SUCCESS)
1086 return ERROR_SUCCESS;
1088 r = MSI_IterateRecords( view, NULL, iterate_appsearch, package );
1089 msiobj_release( &view->hdr );
1090 return r;
1093 static UINT ITERATE_CCPSearch(MSIRECORD *row, LPVOID param)
1095 MSIPACKAGE *package = param;
1096 LPCWSTR signature;
1097 LPWSTR value = NULL;
1098 MSISIGNATURE sig;
1099 UINT r = ERROR_SUCCESS;
1101 static const WCHAR success[] = {'C','C','P','_','S','u','c','c','e','s','s',0};
1103 signature = MSI_RecordGetString(row, 1);
1105 TRACE("%s\n", debugstr_w(signature));
1107 ACTION_AppSearchSigName(package, signature, &sig, &value);
1108 if (value)
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;
1116 ACTION_FreeSignature(&sig);
1118 return r;
1121 UINT ACTION_CCPSearch(MSIPACKAGE *package)
1123 static const WCHAR query[] = {
1124 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1125 'C','C','P','S','e','a','r','c','h',0};
1126 MSIQUERY *view;
1127 UINT r;
1129 if (msi_action_is_unique(package, szCCPSearch))
1131 TRACE("Skipping AppSearch action: already done in UI sequence\n");
1132 return ERROR_SUCCESS;
1134 else
1135 msi_register_unique_action(package, szCCPSearch);
1137 r = MSI_OpenQuery(package->db, &view, query);
1138 if (r != ERROR_SUCCESS)
1139 return ERROR_SUCCESS;
1141 r = MSI_IterateRecords(view, NULL, ITERATE_CCPSearch, package);
1142 msiobj_release(&view->hdr);
1143 return r;