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
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38 typedef struct tagMSISIGNATURE
40 LPCWSTR Name; /* NOT owned by this structure */
53 void msi_parse_version_string(LPCWSTR verStr, PDWORD ms, PDWORD ls)
56 int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
59 ptr = strchrW(verStr, '.');
63 ptr = strchrW(ptr + 1, '.');
68 ptr = strchrW(ptr + 1, '.');
72 /* FIXME: byte-order dependent? */
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',' ','*',' ',
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',' ','=',' ',
92 LPWSTR minVersion, maxVersion, p;
96 TRACE("package %p, sig %p\n", package, sig);
98 memset(sig, 0, sizeof(*sig));
100 row = MSI_QueryGetRecord( package->db, query, name );
103 TRACE("failed to query signature for %s\n", debugstr_w(name));
104 return ERROR_SUCCESS;
108 sig->File = msi_dup_record_field(row,2);
109 if ((p = strchrW(sig->File, '|')))
112 memmove(sig->File, p, (strlenW(p) + 1) * sizeof(WCHAR));
115 minVersion = msi_dup_record_field(row,3);
118 msi_parse_version_string( minVersion, &sig->MinVersionMS, &sig->MinVersionLS );
119 msi_free( minVersion );
121 maxVersion = msi_dup_record_field(row,4);
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)
130 sig->MaxSize = MSI_RecordGetInteger(row,6);
131 if (sig->MaxSize == MSI_NULL_INTEGER)
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)
161 msi_free(sig->Languages);
164 static LPWSTR app_search_file(LPWSTR path, MSISIGNATURE *sig)
166 VS_FIXEDFILEINFO *info;
167 DWORD attr, handle, size;
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);
184 attr = GetFileAttributesW(path);
185 if (attr == INVALID_FILE_ATTRIBUTES ||
186 (attr & FILE_ATTRIBUTE_DIRECTORY))
189 size = GetFileVersionInfoSizeW(path, &handle);
191 return strdupW(path);
193 buffer = msi_alloc(size);
197 if (!GetFileVersionInfoW(path, 0, size, buffer))
200 if (!VerQueryValueW(buffer, szBackSlash, (LPVOID)&info, &size) || !info)
203 if (sig->MinVersionLS || sig->MinVersionMS)
205 if (info->dwFileVersionMS < sig->MinVersionMS)
208 if (info->dwFileVersionMS == sig->MinVersionMS &&
209 info->dwFileVersionLS < sig->MinVersionLS)
213 if (sig->MaxVersionLS || sig->MaxVersionMS)
215 if (info->dwFileVersionMS > sig->MaxVersionMS)
218 if (info->dwFileVersionMS == sig->MaxVersionMS &&
219 info->dwFileVersionLS > sig->MaxVersionLS)
230 static UINT ACTION_AppSearchComponents(MSIPACKAGE *package, LPWSTR *appValue, MSISIGNATURE *sig)
232 static const WCHAR query[] = {
233 'S','E','L','E','C','T',' ','*',' ',
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;
249 WCHAR path[MAX_PATH];
250 DWORD size = MAX_PATH;
254 TRACE("%s\n", debugstr_w(sig->Name));
258 row = MSI_QueryGetRecord(package->db, query, sig->Name);
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);
274 MsiLocateComponentW(guid, path, &size);
278 attr = GetFileAttributesW(path);
279 if (attr == INVALID_FILE_ATTRIBUTES)
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, '\\');
296 PathAddBackslashW(path);
298 *appValue = strdupW(path);
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);
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,
320 static const WCHAR dwordFmt[] = { '#','%','d','\0' };
321 static const WCHAR binPre[] = { '#','x','\0' };
322 static const WCHAR binFmt[] = { '%','0','2','X','\0' };
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);
338 *appValue = msi_alloc(sz);
339 strcpyW(*appValue, (LPCWSTR)value);
343 /* 7 chars for digits, 1 for NULL, 1 for #, and 1 for sign
346 *appValue = msi_alloc(10 * sizeof(WCHAR));
347 sprintfW(*appValue, dwordFmt, *(const DWORD *)value);
350 sz = ExpandEnvironmentStringsW((LPCWSTR)value, NULL, 0);
351 *appValue = msi_alloc(sz * sizeof(WCHAR));
352 ExpandEnvironmentStringsW((LPCWSTR)value, *appValue, sz);
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]);
363 WARN("unimplemented for values of type %d\n", regType);
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;
380 HKEY rootKey, key = NULL;
381 DWORD sz = 0, regType;
386 TRACE("%s\n", debugstr_w(sig->Name));
390 row = MSI_QueryGetRecord( package->db, query, sig->Name );
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);
406 case msidbRegistryRootClassesRoot:
407 rootKey = HKEY_CLASSES_ROOT;
409 case msidbRegistryRootCurrentUser:
410 rootKey = HKEY_CURRENT_USER;
412 case msidbRegistryRootLocalMachine:
413 rootKey = HKEY_LOCAL_MACHINE;
415 case msidbRegistryRootUsers:
416 rootKey = HKEY_USERS;
419 WARN("Unknown root key %d\n", root);
423 rc = RegOpenKeyW(rootKey, deformatted, &key);
426 TRACE("RegOpenKeyW returned %d\n", rc);
430 msi_free(deformatted);
431 deformat_string(package, valueName, &deformatted);
433 rc = RegQueryValueExW(key, deformatted, NULL, NULL, NULL, &sz);
436 TRACE("RegQueryValueExW returned %d\n", rc);
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, ®Type, value, &sz);
446 TRACE("RegQueryValueExW returned %d\n", rc);
450 /* bail out if the registry key is empty */
454 if ((regType == REG_SZ || regType == REG_EXPAND_SZ) &&
455 (ptr = strchrW((LPWSTR)value, '"')) && (end = strchrW(++ptr, '"')))
462 case msidbLocatorTypeDirectory:
463 ACTION_SearchDirectory(package, sig, ptr, 0, appValue);
465 case msidbLocatorTypeFileName:
466 *appValue = app_search_file(ptr, sig);
468 case msidbLocatorTypeRawValue:
469 ACTION_ConvertRegValue(regType, value, sz, appValue);
472 FIXME("unimplemented for type %d (key path %s, value %s)\n",
473 type, debugstr_w(keyPath), debugstr_w(valueName));
478 msi_free( deformatted );
480 msiobj_release(&row->hdr);
481 return ERROR_SUCCESS;
484 static LPWSTR get_ini_field(LPWSTR buf, int field)
493 while ((end = strchrW(beg, ',')) && i < field)
496 while (*beg && *beg == ' ')
502 end = strchrW(beg, ',');
504 end = beg + lstrlenW(beg);
510 static UINT ACTION_AppSearchIni(MSIPACKAGE *package, LPWSTR *appValue,
513 static const WCHAR query[] = {
514 's','e','l','e','c','t',' ','*',' ',
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};
520 LPWSTR fileName, section, key;
524 TRACE("%s\n", debugstr_w(sig->Name));
528 row = MSI_QueryGetRecord( package->db, query, sig->Name );
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)
542 if (type == MSI_NULL_INTEGER)
545 GetPrivateProfileStringW(section, key, NULL, buf, MAX_PATH, fileName);
550 case msidbLocatorTypeDirectory:
551 ACTION_SearchDirectory(package, sig, buf, 0, appValue);
553 case msidbLocatorTypeFileName:
554 *appValue = app_search_file(buf, sig);
556 case msidbLocatorTypeRawValue:
557 *appValue = get_ini_field(buf, field);
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.
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,
583 WCHAR *ptr, *deformatted;
585 if (!src || !dst || !len)
587 if (dst) *dst = '\0';
593 /* Ignore the short portion of the path */
594 if ((ptr = strchrW(src, '|')))
599 deformat_string(package, ptr, &deformatted);
600 if (!deformatted || strlenW(deformatted) > len - 1)
602 msi_free(deformatted);
606 lstrcpyW(dst, deformatted);
607 dst[lstrlenW(deformatted)] = '\0';
608 msi_free(deformatted);
611 static LANGID *parse_languages( const WCHAR *languages, DWORD *num_ids )
614 WCHAR *str = strdupW( languages ), *p, *q;
617 if (!str) return NULL;
618 for (p = q = str; (q = strchrW( q, ',' )); q++) count++;
620 if (!(ret = msi_alloc( count * sizeof(LANGID) )))
628 q = strchrW( p, ',' );
640 static BOOL match_languages( const void *version, const WCHAR *languages )
647 DWORD len, num_ids, i, j;
651 if (!languages || !languages[0]) return TRUE;
652 if (!VerQueryValueW( version, szLangResource, (void **)&lang, &len )) return FALSE;
653 if (!(ids = parse_languages( languages, &num_ids ))) return FALSE;
655 for (i = 0; i < num_ids; i++)
658 for (j = 0; j < len / sizeof(struct lang); j++)
660 if (!ids[i] || ids[i] == lang[j].id) found = TRUE;
662 if (!found) goto done;
670 /* Sets *matches to whether the file (whose path is filePath) matches the
671 * versions set in sig.
672 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
673 * something else if an install-halting error occurs.
675 static UINT ACTION_FileVersionMatches(const MSISIGNATURE *sig, LPCWSTR filePath,
680 VS_FIXEDFILEINFO *info = NULL;
681 DWORD zero, size = GetFileVersionInfoSizeW( filePath, &zero );
685 if (!size) return ERROR_SUCCESS;
686 if (!(version = msi_alloc( size ))) return ERROR_OUTOFMEMORY;
688 if (GetFileVersionInfoW( filePath, 0, size, version ))
689 VerQueryValueW( version, szBackSlash, (void **)&info, &len );
693 TRACE("comparing file version %d.%d.%d.%d:\n",
694 HIWORD(info->dwFileVersionMS),
695 LOWORD(info->dwFileVersionMS),
696 HIWORD(info->dwFileVersionLS),
697 LOWORD(info->dwFileVersionLS));
698 if (info->dwFileVersionMS < sig->MinVersionMS
699 || (info->dwFileVersionMS == sig->MinVersionMS &&
700 info->dwFileVersionLS < sig->MinVersionLS))
702 TRACE("less than minimum version %d.%d.%d.%d\n",
703 HIWORD(sig->MinVersionMS),
704 LOWORD(sig->MinVersionMS),
705 HIWORD(sig->MinVersionLS),
706 LOWORD(sig->MinVersionLS));
708 else if ((sig->MaxVersionMS || sig->MaxVersionLS) &&
709 (info->dwFileVersionMS > sig->MaxVersionMS ||
710 (info->dwFileVersionMS == sig->MaxVersionMS &&
711 info->dwFileVersionLS > sig->MaxVersionLS)))
713 TRACE("greater than maximum version %d.%d.%d.%d\n",
714 HIWORD(sig->MaxVersionMS),
715 LOWORD(sig->MaxVersionMS),
716 HIWORD(sig->MaxVersionLS),
717 LOWORD(sig->MaxVersionLS));
719 else if (!match_languages( version, sig->Languages ))
721 TRACE("languages %s not supported\n", debugstr_w( sig->Languages ));
723 else *matches = TRUE;
726 return ERROR_SUCCESS;
729 /* Sets *matches to whether the file in findData matches that in sig.
730 * fullFilePath is assumed to be the full path of the file specified in
731 * findData, which may be necessary to compare the version.
732 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
733 * something else if an install-halting error occurs.
735 static UINT ACTION_FileMatchesSig(const MSISIGNATURE *sig,
736 const WIN32_FIND_DATAW *findData, LPCWSTR fullFilePath, BOOL *matches)
738 UINT rc = ERROR_SUCCESS;
741 /* assumes the caller has already ensured the filenames match, so check
744 if (sig->MinTime.dwLowDateTime || sig->MinTime.dwHighDateTime)
746 if (findData->ftCreationTime.dwHighDateTime <
747 sig->MinTime.dwHighDateTime ||
748 (findData->ftCreationTime.dwHighDateTime == sig->MinTime.dwHighDateTime
749 && findData->ftCreationTime.dwLowDateTime <
750 sig->MinTime.dwLowDateTime))
753 if (*matches && (sig->MaxTime.dwLowDateTime || sig->MaxTime.dwHighDateTime))
755 if (findData->ftCreationTime.dwHighDateTime >
756 sig->MaxTime.dwHighDateTime ||
757 (findData->ftCreationTime.dwHighDateTime == sig->MaxTime.dwHighDateTime
758 && findData->ftCreationTime.dwLowDateTime >
759 sig->MaxTime.dwLowDateTime))
762 if (*matches && sig->MinSize && findData->nFileSizeLow < sig->MinSize)
764 if (*matches && sig->MaxSize && findData->nFileSizeLow > sig->MaxSize)
766 if (*matches && (sig->MinVersionMS || sig->MinVersionLS ||
767 sig->MaxVersionMS || sig->MaxVersionLS))
768 rc = ACTION_FileVersionMatches(sig, fullFilePath, matches);
772 /* Recursively searches the directory dir for files that match the signature
773 * sig, up to (depth + 1) levels deep. That is, if depth is 0, it searches dir
774 * (and only dir). If depth is 1, searches dir and its immediate
776 * Assumes sig->File is not NULL.
777 * Returns ERROR_SUCCESS on success (which may include non-critical errors),
778 * something else on failures which should halt the install.
780 static UINT ACTION_RecurseSearchDirectory(MSIPACKAGE *package, LPWSTR *appValue,
781 MSISIGNATURE *sig, LPCWSTR dir, int depth)
784 WIN32_FIND_DATAW findData;
785 UINT rc = ERROR_SUCCESS;
786 size_t dirLen = lstrlenW(dir), fileLen = lstrlenW(sig->File);
787 WCHAR subpath[MAX_PATH];
791 static const WCHAR starDotStarW[] = { '*','.','*',0 };
793 TRACE("Searching directory %s for file %s, depth %d\n", debugstr_w(dir),
794 debugstr_w(sig->File), depth);
797 return ERROR_SUCCESS;
800 /* We need the buffer in both paths below, so go ahead and allocate it
801 * here. Add two because we might need to add a backslash if the dir name
802 * isn't backslash-terminated.
804 len = dirLen + max(fileLen, strlenW(starDotStarW)) + 2;
805 buf = msi_alloc(len * sizeof(WCHAR));
807 return ERROR_OUTOFMEMORY;
810 PathAddBackslashW(buf);
811 lstrcatW(buf, sig->File);
813 hFind = FindFirstFileW(buf, &findData);
814 if (hFind != INVALID_HANDLE_VALUE)
816 if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
820 rc = ACTION_FileMatchesSig(sig, &findData, buf, &matches);
821 if (rc == ERROR_SUCCESS && matches)
823 TRACE("found file, returning %s\n", debugstr_w(buf));
830 if (rc == ERROR_SUCCESS && !*appValue)
833 PathAddBackslashW(buf);
834 lstrcatW(buf, starDotStarW);
836 hFind = FindFirstFileW(buf, &findData);
837 if (hFind != INVALID_HANDLE_VALUE)
839 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
840 strcmpW( findData.cFileName, szDot ) &&
841 strcmpW( findData.cFileName, szDotDot ))
843 lstrcpyW(subpath, dir);
844 PathAppendW(subpath, findData.cFileName);
845 rc = ACTION_RecurseSearchDirectory(package, appValue, sig,
849 while (rc == ERROR_SUCCESS && !*appValue &&
850 FindNextFileW(hFind, &findData) != 0)
852 if (!strcmpW( findData.cFileName, szDot ) ||
853 !strcmpW( findData.cFileName, szDotDot ))
856 lstrcpyW(subpath, dir);
857 PathAppendW(subpath, findData.cFileName);
858 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
859 rc = ACTION_RecurseSearchDirectory(package, appValue,
860 sig, subpath, depth - 1);
867 if (*appValue != buf)
873 static UINT ACTION_CheckDirectory(MSIPACKAGE *package, LPCWSTR dir,
876 DWORD attr = GetFileAttributesW(dir);
878 if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY))
880 TRACE("directory exists, returning %s\n", debugstr_w(dir));
881 *appValue = strdupW(dir);
884 return ERROR_SUCCESS;
887 static BOOL ACTION_IsFullPath(LPCWSTR path)
889 WCHAR first = toupperW(path[0]);
892 if (first >= 'A' && first <= 'Z' && path[1] == ':')
894 else if (path[0] == '\\' && path[1] == '\\')
901 static UINT ACTION_SearchDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
902 LPCWSTR path, int depth, LPWSTR *appValue)
908 TRACE("%p, %p, %s, %d, %p\n", package, sig, debugstr_w(path), depth,
911 if (ACTION_IsFullPath(path))
914 rc = ACTION_RecurseSearchDirectory(package, &val, sig, path, depth);
917 /* Recursively searching a directory makes no sense when the
918 * directory to search is the thing you're trying to find.
920 rc = ACTION_CheckDirectory(package, path, &val);
925 WCHAR pathWithDrive[MAX_PATH] = { 'C',':','\\',0 };
926 DWORD drives = GetLogicalDrives();
930 for (i = 0; rc == ERROR_SUCCESS && !val && i < 26; i++)
932 if (!(drives & (1 << i)))
935 pathWithDrive[0] = 'A' + i;
936 if (GetDriveTypeW(pathWithDrive) != DRIVE_FIXED)
939 lstrcpynW(pathWithDrive + 3, path,
940 sizeof(pathWithDrive) / sizeof(pathWithDrive[0]) - 3);
943 rc = ACTION_RecurseSearchDirectory(package, &val, sig,
944 pathWithDrive, depth);
946 rc = ACTION_CheckDirectory(package, pathWithDrive, &val);
950 attr = GetFileAttributesW(val);
951 if (attr != INVALID_FILE_ATTRIBUTES &&
952 (attr & FILE_ATTRIBUTE_DIRECTORY) &&
953 val && val[lstrlenW(val) - 1] != '\\')
955 val = msi_realloc(val, (lstrlenW(val) + 2) * sizeof(WCHAR));
957 rc = ERROR_OUTOFMEMORY;
959 PathAddBackslashW(val);
964 TRACE("returning %d\n", rc);
968 static UINT ACTION_AppSearchSigName(MSIPACKAGE *package, LPCWSTR sigName,
969 MSISIGNATURE *sig, LPWSTR *appValue);
971 static UINT ACTION_AppSearchDr(MSIPACKAGE *package, LPWSTR *appValue, MSISIGNATURE *sig)
973 static const WCHAR query[] = {
974 's','e','l','e','c','t',' ','*',' ',
976 'D','r','L','o','c','a','t','o','r',' ',
977 'w','h','e','r','e',' ',
978 'S','i','g','n','a','t','u','r','e','_',' ','=',' ', '\'','%','s','\'',0};
979 LPWSTR parent = NULL;
981 WCHAR path[MAX_PATH];
982 WCHAR expanded[MAX_PATH];
988 TRACE("%s\n", debugstr_w(sig->Name));
992 row = MSI_QueryGetRecord( package->db, query, sig->Name );
995 TRACE("failed to query DrLocator for %s\n", debugstr_w(sig->Name));
996 return ERROR_SUCCESS;
999 /* check whether parent is set */
1000 parentName = MSI_RecordGetString(row, 2);
1003 MSISIGNATURE parentSig;
1005 ACTION_AppSearchSigName(package, parentName, &parentSig, &parent);
1006 ACTION_FreeSignature(&parentSig);
1009 msiobj_release(&row->hdr);
1010 return ERROR_SUCCESS;
1015 MSI_RecordGetStringW(row, 3, path, &sz);
1017 if (MSI_RecordIsNull(row,4))
1020 depth = MSI_RecordGetInteger(row,4);
1023 ACTION_ExpandAnyPath(package, path, expanded, MAX_PATH);
1025 strcpyW(expanded, path);
1029 attr = GetFileAttributesW(parent);
1030 if (attr != INVALID_FILE_ATTRIBUTES &&
1031 !(attr & FILE_ATTRIBUTE_DIRECTORY))
1033 PathRemoveFileSpecW(parent);
1034 PathAddBackslashW(parent);
1037 strcpyW(path, parent);
1038 strcatW(path, expanded);
1041 strcpyW(path, expanded);
1043 PathAddBackslashW(path);
1045 rc = ACTION_SearchDirectory(package, sig, path, depth, appValue);
1048 msiobj_release(&row->hdr);
1050 TRACE("returning %d\n", rc);
1054 static UINT ACTION_AppSearchSigName(MSIPACKAGE *package, LPCWSTR sigName,
1055 MSISIGNATURE *sig, LPWSTR *appValue)
1060 rc = ACTION_AppSearchGetSignature(package, sig, sigName);
1061 if (rc == ERROR_SUCCESS)
1063 rc = ACTION_AppSearchComponents(package, appValue, sig);
1064 if (rc == ERROR_SUCCESS && !*appValue)
1066 rc = ACTION_AppSearchReg(package, appValue, sig);
1067 if (rc == ERROR_SUCCESS && !*appValue)
1069 rc = ACTION_AppSearchIni(package, appValue, sig);
1070 if (rc == ERROR_SUCCESS && !*appValue)
1071 rc = ACTION_AppSearchDr(package, appValue, sig);
1078 static UINT iterate_appsearch(MSIRECORD *row, LPVOID param)
1080 MSIPACKAGE *package = param;
1081 LPCWSTR propName, sigName;
1082 LPWSTR value = NULL;
1087 /* get property and signature */
1088 propName = MSI_RecordGetString(row, 1);
1089 sigName = MSI_RecordGetString(row, 2);
1091 TRACE("%s %s\n", debugstr_w(propName), debugstr_w(sigName));
1093 r = ACTION_AppSearchSigName(package, sigName, &sig, &value);
1096 r = msi_set_property( package->db, propName, value );
1097 if (r == ERROR_SUCCESS && !strcmpW( propName, szSourceDir ))
1098 msi_reset_folders( package, TRUE );
1102 ACTION_FreeSignature(&sig);
1104 uirow = MSI_CreateRecord( 2 );
1105 MSI_RecordSetStringW( uirow, 1, propName );
1106 MSI_RecordSetStringW( uirow, 2, sigName );
1107 msi_ui_actiondata( package, szAppSearch, uirow );
1108 msiobj_release( &uirow->hdr );
1113 UINT ACTION_AppSearch(MSIPACKAGE *package)
1115 static const WCHAR query[] = {
1116 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1117 'A','p','p','S','e','a','r','c','h',0};
1121 if (msi_action_is_unique(package, szAppSearch))
1123 TRACE("Skipping AppSearch action: already done in UI sequence\n");
1124 return ERROR_SUCCESS;
1127 msi_register_unique_action(package, szAppSearch);
1129 r = MSI_OpenQuery( package->db, &view, query );
1130 if (r != ERROR_SUCCESS)
1131 return ERROR_SUCCESS;
1133 r = MSI_IterateRecords( view, NULL, iterate_appsearch, package );
1134 msiobj_release( &view->hdr );
1138 static UINT ITERATE_CCPSearch(MSIRECORD *row, LPVOID param)
1140 MSIPACKAGE *package = param;
1142 LPWSTR value = NULL;
1144 UINT r = ERROR_SUCCESS;
1146 static const WCHAR success[] = {'C','C','P','_','S','u','c','c','e','s','s',0};
1148 signature = MSI_RecordGetString(row, 1);
1150 TRACE("%s\n", debugstr_w(signature));
1152 ACTION_AppSearchSigName(package, signature, &sig, &value);
1155 TRACE("Found signature %s\n", debugstr_w(signature));
1156 msi_set_property(package->db, success, szOne);
1158 r = ERROR_NO_MORE_ITEMS;
1161 ACTION_FreeSignature(&sig);
1166 UINT ACTION_CCPSearch(MSIPACKAGE *package)
1168 static const WCHAR query[] = {
1169 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1170 'C','C','P','S','e','a','r','c','h',0};
1174 if (msi_action_is_unique(package, szCCPSearch))
1176 TRACE("Skipping AppSearch action: already done in UI sequence\n");
1177 return ERROR_SUCCESS;
1180 msi_register_unique_action(package, szCCPSearch);
1182 r = MSI_OpenQuery(package->db, &view, query);
1183 if (r != ERROR_SUCCESS)
1184 return ERROR_SUCCESS;
1186 r = MSI_IterateRecords(view, NULL, ITERATE_CCPSearch, package);
1187 msiobj_release(&view->hdr);