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/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
37 typedef struct tagMSISIGNATURE
39 LPCWSTR Name
; /* NOT owned by this structure */
52 void msi_parse_version_string(LPCWSTR verStr
, PDWORD ms
, PDWORD ls
)
55 int x1
= 0, x2
= 0, x3
= 0, x4
= 0;
57 x1
= wcstol(verStr
, NULL
, 10);
58 ptr
= wcschr(verStr
, '.');
61 x2
= wcstol(ptr
+ 1, NULL
, 10);
62 ptr
= wcschr(ptr
+ 1, '.');
66 x3
= wcstol(ptr
+ 1, NULL
, 10);
67 ptr
= wcschr(ptr
+ 1, '.');
70 x4
= wcstol(ptr
+ 1, NULL
, 10);
71 /* FIXME: byte-order dependent? */
73 if (ls
) *ls
= x3
<< 16 | x4
;
76 /* Fills in sig with the values from the Signature table, where name is the
77 * signature to find. Upon return, sig->File will be NULL if the record is not
78 * found, and not NULL if it is found.
79 * Warning: clears all fields in sig!
80 * Returns ERROR_SUCCESS upon success (where not finding the record counts as
81 * success), something else on error.
83 static UINT
get_signature( MSIPACKAGE
*package
, MSISIGNATURE
*sig
, const WCHAR
*name
)
85 static const WCHAR query
[] = {
86 's','e','l','e','c','t',' ','*',' ',
88 'S','i','g','n','a','t','u','r','e',' ',
89 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e',' ','=',' ',
91 LPWSTR minVersion
, maxVersion
, p
;
95 TRACE("package %p, sig %p\n", package
, sig
);
97 memset(sig
, 0, sizeof(*sig
));
99 row
= MSI_QueryGetRecord( package
->db
, query
, name
);
102 TRACE("failed to query signature for %s\n", debugstr_w(name
));
103 return ERROR_SUCCESS
;
107 sig
->File
= msi_dup_record_field(row
,2);
108 if ((p
= wcschr(sig
->File
, '|')))
111 memmove(sig
->File
, p
, (lstrlenW(p
) + 1) * sizeof(WCHAR
));
114 minVersion
= msi_dup_record_field(row
,3);
117 msi_parse_version_string( minVersion
, &sig
->MinVersionMS
, &sig
->MinVersionLS
);
118 msi_free( minVersion
);
120 maxVersion
= msi_dup_record_field(row
,4);
123 msi_parse_version_string( maxVersion
, &sig
->MaxVersionMS
, &sig
->MaxVersionLS
);
124 msi_free( maxVersion
);
126 sig
->MinSize
= MSI_RecordGetInteger(row
,5);
127 if (sig
->MinSize
== MSI_NULL_INTEGER
)
129 sig
->MaxSize
= MSI_RecordGetInteger(row
,6);
130 if (sig
->MaxSize
== MSI_NULL_INTEGER
)
132 sig
->Languages
= msi_dup_record_field(row
,9);
133 time
= MSI_RecordGetInteger(row
,7);
134 if (time
!= MSI_NULL_INTEGER
)
135 DosDateTimeToFileTime(HIWORD(time
), LOWORD(time
), &sig
->MinTime
);
136 time
= MSI_RecordGetInteger(row
,8);
137 if (time
!= MSI_NULL_INTEGER
)
138 DosDateTimeToFileTime(HIWORD(time
), LOWORD(time
), &sig
->MaxTime
);
140 TRACE("Found file name %s for Signature_ %s;\n",
141 debugstr_w(sig
->File
), debugstr_w(name
));
142 TRACE("MinVersion is %d.%d.%d.%d\n", HIWORD(sig
->MinVersionMS
),
143 LOWORD(sig
->MinVersionMS
), HIWORD(sig
->MinVersionLS
),
144 LOWORD(sig
->MinVersionLS
));
145 TRACE("MaxVersion is %d.%d.%d.%d\n", HIWORD(sig
->MaxVersionMS
),
146 LOWORD(sig
->MaxVersionMS
), HIWORD(sig
->MaxVersionLS
),
147 LOWORD(sig
->MaxVersionLS
));
148 TRACE("MinSize is %d, MaxSize is %d;\n", sig
->MinSize
, sig
->MaxSize
);
149 TRACE("Languages is %s\n", debugstr_w(sig
->Languages
));
151 msiobj_release( &row
->hdr
);
153 return ERROR_SUCCESS
;
156 /* Frees any memory allocated in sig */
157 static void free_signature( MSISIGNATURE
*sig
)
160 msi_free(sig
->Languages
);
163 static WCHAR
*search_file( MSIPACKAGE
*package
, WCHAR
*path
, MSISIGNATURE
*sig
)
165 VS_FIXEDFILEINFO
*info
;
172 PathRemoveFileSpecW(path
);
173 PathAddBackslashW(path
);
175 attr
= msi_get_file_attributes( package
, path
);
176 if (attr
!= INVALID_FILE_ATTRIBUTES
&& (attr
& FILE_ATTRIBUTE_DIRECTORY
))
177 return strdupW(path
);
182 attr
= msi_get_file_attributes( package
, path
);
183 if (attr
== INVALID_FILE_ATTRIBUTES
|| (attr
& FILE_ATTRIBUTE_DIRECTORY
))
186 size
= msi_get_file_version_info( package
, path
, 0, NULL
);
188 return strdupW(path
);
190 buffer
= msi_alloc(size
);
194 size
= msi_get_file_version_info( package
, path
, size
, buffer
);
198 if (!VerQueryValueW(buffer
, szBackSlash
, (LPVOID
)&info
, &size
) || !info
)
201 if (sig
->MinVersionLS
|| sig
->MinVersionMS
)
203 if (info
->dwFileVersionMS
< sig
->MinVersionMS
)
206 if (info
->dwFileVersionMS
== sig
->MinVersionMS
&&
207 info
->dwFileVersionLS
< sig
->MinVersionLS
)
211 if (sig
->MaxVersionLS
|| sig
->MaxVersionMS
)
213 if (info
->dwFileVersionMS
> sig
->MaxVersionMS
)
216 if (info
->dwFileVersionMS
== sig
->MaxVersionMS
&&
217 info
->dwFileVersionLS
> sig
->MaxVersionLS
)
228 static UINT
search_components( MSIPACKAGE
*package
, WCHAR
**appValue
, MSISIGNATURE
*sig
)
230 static const WCHAR query
[] = {
231 'S','E','L','E','C','T',' ','*',' ',
233 '`','C','o','m','p','L','o','c','a','t','o','r','`',' ',
234 'W','H','E','R','E',' ','`','S','i','g','n','a','t','u','r','e','_','`',' ','=',' ',
235 '\'','%','s','\'',0};
236 static const WCHAR sigquery
[] = {
237 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
238 '`','S','i','g','n','a','t','u','r','e','`',' ',
239 'W','H','E','R','E',' ','`','S','i','g','n','a','t','u','r','e','`',' ','=',' ',
240 '\'','%','s','\'',0};
241 MSIRECORD
*row
, *rec
;
242 LPCWSTR signature
, guid
;
243 BOOL sigpresent
= TRUE
;
246 WCHAR path
[MAX_PATH
];
247 DWORD size
= MAX_PATH
;
251 TRACE("%s\n", debugstr_w(sig
->Name
));
255 row
= MSI_QueryGetRecord(package
->db
, query
, sig
->Name
);
258 TRACE("failed to query CompLocator for %s\n", debugstr_w(sig
->Name
));
259 return ERROR_SUCCESS
;
262 signature
= MSI_RecordGetString(row
, 1);
263 guid
= MSI_RecordGetString(row
, 2);
264 type
= MSI_RecordGetInteger(row
, 3);
266 rec
= MSI_QueryGetRecord(package
->db
, sigquery
, signature
);
271 MsiLocateComponentW(guid
, path
, &size
);
275 attr
= msi_get_file_attributes( package
, path
);
276 if (attr
== INVALID_FILE_ATTRIBUTES
)
279 isdir
= (attr
& FILE_ATTRIBUTE_DIRECTORY
);
281 if (type
!= msidbLocatorTypeDirectory
&& sigpresent
&& !isdir
)
283 *appValue
= search_file( package
, path
, sig
);
285 else if (!sigpresent
&& (type
!= msidbLocatorTypeDirectory
|| isdir
))
287 if (type
== msidbLocatorTypeFileName
)
289 ptr
= wcsrchr(path
, '\\');
293 PathAddBackslashW(path
);
295 *appValue
= strdupW(path
);
299 PathAddBackslashW(path
);
300 lstrcatW(path
, MSI_RecordGetString(rec
, 2));
302 attr
= msi_get_file_attributes( package
, path
);
303 if (attr
!= INVALID_FILE_ATTRIBUTES
&& !(attr
& FILE_ATTRIBUTE_DIRECTORY
))
304 *appValue
= strdupW(path
);
308 if (rec
) msiobj_release(&rec
->hdr
);
309 msiobj_release(&row
->hdr
);
310 return ERROR_SUCCESS
;
313 static void convert_reg_value( DWORD regType
, const BYTE
*value
, DWORD sz
, WCHAR
**appValue
)
315 static const WCHAR dwordFmt
[] = { '#','%','d','\0' };
316 static const WCHAR binPre
[] = { '#','x','\0' };
317 static const WCHAR binFmt
[] = { '%','0','2','X','\0' };
324 if (*(LPCWSTR
)value
== '#')
326 /* escape leading pound with another */
327 *appValue
= msi_alloc(sz
+ sizeof(WCHAR
));
328 (*appValue
)[0] = '#';
329 lstrcpyW(*appValue
+ 1, (LPCWSTR
)value
);
333 *appValue
= msi_alloc(sz
);
334 lstrcpyW(*appValue
, (LPCWSTR
)value
);
338 /* 7 chars for digits, 1 for NULL, 1 for #, and 1 for sign
341 *appValue
= msi_alloc(10 * sizeof(WCHAR
));
342 swprintf(*appValue
, 10, dwordFmt
, *(const DWORD
*)value
);
345 sz
= ExpandEnvironmentStringsW((LPCWSTR
)value
, NULL
, 0);
346 *appValue
= msi_alloc(sz
* sizeof(WCHAR
));
347 ExpandEnvironmentStringsW((LPCWSTR
)value
, *appValue
, sz
);
351 *appValue
= msi_alloc((sz
* 2 + 3) * sizeof(WCHAR
));
352 lstrcpyW(*appValue
, binPre
);
353 ptr
= *appValue
+ lstrlenW(binPre
);
354 for (i
= 0; i
< sz
; i
++, ptr
+= 2)
355 swprintf(ptr
, 3, binFmt
, value
[i
]);
358 WARN("unimplemented for values of type %d\n", regType
);
363 static UINT
search_directory( MSIPACKAGE
*, MSISIGNATURE
*, const WCHAR
*, int, WCHAR
** );
365 static UINT
search_reg( MSIPACKAGE
*package
, WCHAR
**appValue
, MSISIGNATURE
*sig
)
367 static const WCHAR query
[] = {
368 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
369 'R','e','g','L','o','c','a','t','o','r',' ','W','H','E','R','E',' ',
370 'S','i','g','n','a','t','u','r','e','_',' ','=',' ', '\'','%','s','\'',0};
371 const WCHAR
*keyPath
, *valueName
;
372 WCHAR
*deformatted
= NULL
, *ptr
= NULL
, *end
;
374 REGSAM access
= KEY_READ
;
375 HKEY rootKey
, key
= NULL
;
376 DWORD sz
= 0, regType
;
381 TRACE("%s\n", debugstr_w(sig
->Name
));
385 row
= MSI_QueryGetRecord( package
->db
, query
, sig
->Name
);
388 TRACE("failed to query RegLocator for %s\n", debugstr_w(sig
->Name
));
389 return ERROR_SUCCESS
;
392 root
= MSI_RecordGetInteger(row
, 2);
393 keyPath
= MSI_RecordGetString(row
, 3);
394 valueName
= MSI_RecordGetString(row
, 4);
395 type
= MSI_RecordGetInteger(row
, 5);
397 deformat_string(package
, keyPath
, &deformatted
);
401 case msidbRegistryRootClassesRoot
:
402 rootKey
= HKEY_CLASSES_ROOT
;
404 case msidbRegistryRootCurrentUser
:
405 rootKey
= HKEY_CURRENT_USER
;
407 case msidbRegistryRootLocalMachine
:
408 rootKey
= HKEY_LOCAL_MACHINE
;
409 if (type
& msidbLocatorType64bit
) access
|= KEY_WOW64_64KEY
;
410 else access
|= KEY_WOW64_32KEY
;
412 case msidbRegistryRootUsers
:
413 rootKey
= HKEY_USERS
;
416 WARN("Unknown root key %d\n", root
);
420 rc
= RegOpenKeyExW( rootKey
, deformatted
, 0, access
, &key
);
423 TRACE("RegOpenKeyExW returned %d\n", rc
);
427 msi_free(deformatted
);
428 deformat_string(package
, valueName
, &deformatted
);
430 rc
= RegQueryValueExW(key
, deformatted
, NULL
, NULL
, NULL
, &sz
);
433 TRACE("RegQueryValueExW returned %d\n", rc
);
436 /* FIXME: sanity-check sz before allocating (is there an upper-limit
437 * on the value of a property?)
439 value
= msi_alloc( sz
);
440 rc
= RegQueryValueExW(key
, deformatted
, NULL
, ®Type
, value
, &sz
);
443 TRACE("RegQueryValueExW returned %d\n", rc
);
447 /* bail out if the registry key is empty */
451 /* expand if needed */
452 if (regType
== REG_EXPAND_SZ
)
454 sz
= ExpandEnvironmentStringsW((LPCWSTR
)value
, NULL
, 0);
457 LPWSTR buf
= msi_alloc(sz
* sizeof(WCHAR
));
458 ExpandEnvironmentStringsW((LPCWSTR
)value
, buf
, sz
);
464 if ((regType
== REG_SZ
|| regType
== REG_EXPAND_SZ
) &&
465 (ptr
= wcschr((LPWSTR
)value
, '"')) && (end
= wcschr(++ptr
, '"')))
472 case msidbLocatorTypeDirectory
:
473 search_directory( package
, sig
, ptr
, 0, appValue
);
475 case msidbLocatorTypeFileName
:
476 *appValue
= search_file( package
, ptr
, sig
);
478 case msidbLocatorTypeRawValue
:
479 convert_reg_value( regType
, value
, sz
, appValue
);
482 FIXME("unimplemented for type %d (key path %s, value %s)\n",
483 type
, debugstr_w(keyPath
), debugstr_w(valueName
));
488 msi_free( deformatted
);
490 msiobj_release(&row
->hdr
);
491 return ERROR_SUCCESS
;
494 static LPWSTR
get_ini_field(LPWSTR buf
, int field
)
503 while ((end
= wcschr(beg
, ',')) && i
< field
)
512 end
= wcschr(beg
, ',');
514 end
= beg
+ lstrlenW(beg
);
520 static UINT
search_ini( MSIPACKAGE
*package
, WCHAR
**appValue
, MSISIGNATURE
*sig
)
522 static const WCHAR query
[] = {
523 's','e','l','e','c','t',' ','*',' ',
525 'I','n','i','L','o','c','a','t','o','r',' ',
526 'w','h','e','r','e',' ',
527 'S','i','g','n','a','t','u','r','e','_',' ','=',' ','\'','%','s','\'',0};
529 LPWSTR fileName
, section
, key
;
533 TRACE("%s\n", debugstr_w(sig
->Name
));
537 row
= MSI_QueryGetRecord( package
->db
, query
, sig
->Name
);
540 TRACE("failed to query IniLocator for %s\n", debugstr_w(sig
->Name
));
541 return ERROR_SUCCESS
;
544 fileName
= msi_dup_record_field(row
, 2);
545 section
= msi_dup_record_field(row
, 3);
546 key
= msi_dup_record_field(row
, 4);
547 field
= MSI_RecordGetInteger(row
, 5);
548 type
= MSI_RecordGetInteger(row
, 6);
549 if (field
== MSI_NULL_INTEGER
)
551 if (type
== MSI_NULL_INTEGER
)
554 GetPrivateProfileStringW(section
, key
, NULL
, buf
, MAX_PATH
, fileName
);
559 case msidbLocatorTypeDirectory
:
560 search_directory( package
, sig
, buf
, 0, appValue
);
562 case msidbLocatorTypeFileName
:
563 *appValue
= search_file( package
, buf
, sig
);
565 case msidbLocatorTypeRawValue
:
566 *appValue
= get_ini_field(buf
, field
);
575 msiobj_release(&row
->hdr
);
577 return ERROR_SUCCESS
;
580 /* Expands the value in src into a path without property names and only
581 * containing long path names into dst. Replaces at most len characters of dst,
582 * and always NULL-terminates dst if dst is not NULL and len >= 1.
584 * Assumes src and dst are non-overlapping.
585 * FIXME: return code probably needed:
586 * - what does AppSearch return if the table values are invalid?
587 * - what if dst is too small?
589 static void expand_any_path( MSIPACKAGE
*package
, WCHAR
*src
, WCHAR
*dst
, size_t len
)
591 WCHAR
*ptr
, *deformatted
;
593 if (!src
|| !dst
|| !len
)
595 if (dst
) *dst
= '\0';
601 /* Ignore the short portion of the path */
602 if ((ptr
= wcschr(src
, '|')))
607 deformat_string(package
, ptr
, &deformatted
);
608 if (!deformatted
|| lstrlenW(deformatted
) > len
- 1)
610 msi_free(deformatted
);
614 lstrcpyW(dst
, deformatted
);
615 dst
[lstrlenW(deformatted
)] = '\0';
616 msi_free(deformatted
);
619 static LANGID
*parse_languages( const WCHAR
*languages
, DWORD
*num_ids
)
622 WCHAR
*str
= strdupW( languages
), *p
, *q
;
625 if (!str
) return NULL
;
626 for (p
= q
= str
; (q
= wcschr( q
, ',' )); q
++) count
++;
628 if (!(ret
= msi_alloc( count
* sizeof(LANGID
) )))
636 q
= wcschr( p
, ',' );
638 ret
[i
] = wcstol( p
, NULL
, 10 );
648 static BOOL
match_languages( const void *version
, const WCHAR
*languages
)
655 DWORD len
, num_ids
, i
, j
;
659 if (!languages
|| !languages
[0]) return TRUE
;
660 if (!VerQueryValueW( version
, szLangResource
, (void **)&lang
, &len
)) return FALSE
;
661 if (!(ids
= parse_languages( languages
, &num_ids
))) return FALSE
;
663 for (i
= 0; i
< num_ids
; i
++)
666 for (j
= 0; j
< len
/ sizeof(struct lang
); j
++)
668 if (!ids
[i
] || ids
[i
] == lang
[j
].id
) found
= TRUE
;
670 if (!found
) goto done
;
678 /* Sets *matches to whether the file (whose path is filePath) matches the
679 * versions set in sig.
680 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
681 * something else if an install-halting error occurs.
683 static UINT
file_version_matches( MSIPACKAGE
*package
, const MSISIGNATURE
*sig
, const WCHAR
*filePath
,
688 VS_FIXEDFILEINFO
*info
= NULL
;
689 DWORD size
= msi_get_file_version_info( package
, filePath
, 0, NULL
);
693 if (!size
) return ERROR_SUCCESS
;
694 if (!(version
= msi_alloc( size
))) return ERROR_OUTOFMEMORY
;
696 if (msi_get_file_version_info( package
, filePath
, size
, version
))
697 VerQueryValueW( version
, szBackSlash
, (void **)&info
, &len
);
701 TRACE("comparing file version %d.%d.%d.%d:\n",
702 HIWORD(info
->dwFileVersionMS
),
703 LOWORD(info
->dwFileVersionMS
),
704 HIWORD(info
->dwFileVersionLS
),
705 LOWORD(info
->dwFileVersionLS
));
706 if (info
->dwFileVersionMS
< sig
->MinVersionMS
707 || (info
->dwFileVersionMS
== sig
->MinVersionMS
&&
708 info
->dwFileVersionLS
< sig
->MinVersionLS
))
710 TRACE("less than minimum version %d.%d.%d.%d\n",
711 HIWORD(sig
->MinVersionMS
),
712 LOWORD(sig
->MinVersionMS
),
713 HIWORD(sig
->MinVersionLS
),
714 LOWORD(sig
->MinVersionLS
));
716 else if ((sig
->MaxVersionMS
|| sig
->MaxVersionLS
) &&
717 (info
->dwFileVersionMS
> sig
->MaxVersionMS
||
718 (info
->dwFileVersionMS
== sig
->MaxVersionMS
&&
719 info
->dwFileVersionLS
> sig
->MaxVersionLS
)))
721 TRACE("greater than maximum version %d.%d.%d.%d\n",
722 HIWORD(sig
->MaxVersionMS
),
723 LOWORD(sig
->MaxVersionMS
),
724 HIWORD(sig
->MaxVersionLS
),
725 LOWORD(sig
->MaxVersionLS
));
727 else if (!match_languages( version
, sig
->Languages
))
729 TRACE("languages %s not supported\n", debugstr_w( sig
->Languages
));
731 else *matches
= TRUE
;
734 return ERROR_SUCCESS
;
737 /* Sets *matches to whether the file in findData matches that in sig.
738 * fullFilePath is assumed to be the full path of the file specified in
739 * findData, which may be necessary to compare the version.
740 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
741 * something else if an install-halting error occurs.
743 static UINT
file_matches_sig( MSIPACKAGE
*package
, const MSISIGNATURE
*sig
, const WIN32_FIND_DATAW
*findData
,
744 const WCHAR
*fullFilePath
, BOOL
*matches
)
746 UINT rc
= ERROR_SUCCESS
;
749 /* assumes the caller has already ensured the filenames match, so check
752 if (sig
->MinTime
.dwLowDateTime
|| sig
->MinTime
.dwHighDateTime
)
754 if (findData
->ftCreationTime
.dwHighDateTime
<
755 sig
->MinTime
.dwHighDateTime
||
756 (findData
->ftCreationTime
.dwHighDateTime
== sig
->MinTime
.dwHighDateTime
757 && findData
->ftCreationTime
.dwLowDateTime
<
758 sig
->MinTime
.dwLowDateTime
))
761 if (*matches
&& (sig
->MaxTime
.dwLowDateTime
|| sig
->MaxTime
.dwHighDateTime
))
763 if (findData
->ftCreationTime
.dwHighDateTime
>
764 sig
->MaxTime
.dwHighDateTime
||
765 (findData
->ftCreationTime
.dwHighDateTime
== sig
->MaxTime
.dwHighDateTime
766 && findData
->ftCreationTime
.dwLowDateTime
>
767 sig
->MaxTime
.dwLowDateTime
))
770 if (*matches
&& sig
->MinSize
&& findData
->nFileSizeLow
< sig
->MinSize
)
772 if (*matches
&& sig
->MaxSize
&& findData
->nFileSizeLow
> sig
->MaxSize
)
774 if (*matches
&& (sig
->MinVersionMS
|| sig
->MinVersionLS
||
775 sig
->MaxVersionMS
|| sig
->MaxVersionLS
))
776 rc
= file_version_matches( package
, sig
, fullFilePath
, matches
);
780 /* Recursively searches the directory dir for files that match the signature
781 * sig, up to (depth + 1) levels deep. That is, if depth is 0, it searches dir
782 * (and only dir). If depth is 1, searches dir and its immediate
784 * Assumes sig->File is not NULL.
785 * Returns ERROR_SUCCESS on success (which may include non-critical errors),
786 * something else on failures which should halt the install.
788 static UINT
recurse_search_directory( MSIPACKAGE
*package
, WCHAR
**appValue
, MSISIGNATURE
*sig
, const WCHAR
*dir
,
791 static const WCHAR starDotStarW
[] = { '*','.','*',0 };
793 WIN32_FIND_DATAW findData
;
794 UINT rc
= ERROR_SUCCESS
;
795 size_t dirLen
= lstrlenW(dir
), fileLen
= lstrlenW(sig
->File
);
796 WCHAR subpath
[MAX_PATH
];
800 TRACE("Searching directory %s for file %s, depth %d\n", debugstr_w(dir
), debugstr_w(sig
->File
), depth
);
803 return ERROR_SUCCESS
;
806 /* We need the buffer in both paths below, so go ahead and allocate it
807 * here. Add two because we might need to add a backslash if the dir name
808 * isn't backslash-terminated.
810 len
= dirLen
+ max(fileLen
, lstrlenW(starDotStarW
)) + 2;
811 buf
= msi_alloc(len
* sizeof(WCHAR
));
813 return ERROR_OUTOFMEMORY
;
816 PathAddBackslashW(buf
);
817 lstrcatW(buf
, sig
->File
);
819 hFind
= msi_find_first_file( package
, buf
, &findData
);
820 if (hFind
!= INVALID_HANDLE_VALUE
)
822 if (!(findData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
826 rc
= file_matches_sig( package
, sig
, &findData
, buf
, &matches
);
827 if (rc
== ERROR_SUCCESS
&& matches
)
829 TRACE("found file, returning %s\n", debugstr_w(buf
));
836 if (rc
== ERROR_SUCCESS
&& !*appValue
)
839 PathAddBackslashW(buf
);
840 lstrcatW(buf
, starDotStarW
);
842 hFind
= msi_find_first_file( package
, buf
, &findData
);
843 if (hFind
!= INVALID_HANDLE_VALUE
)
845 if (findData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
&&
846 wcscmp( findData
.cFileName
, szDot
) &&
847 wcscmp( findData
.cFileName
, szDotDot
))
849 lstrcpyW(subpath
, dir
);
850 PathAppendW(subpath
, findData
.cFileName
);
851 rc
= recurse_search_directory( package
, appValue
, sig
, subpath
, depth
- 1 );
854 while (rc
== ERROR_SUCCESS
&& !*appValue
&& msi_find_next_file( package
, hFind
, &findData
))
856 if (!wcscmp( findData
.cFileName
, szDot
) ||
857 !wcscmp( findData
.cFileName
, szDotDot
))
860 lstrcpyW(subpath
, dir
);
861 PathAppendW(subpath
, findData
.cFileName
);
862 if (findData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
863 rc
= recurse_search_directory( package
, appValue
, sig
, subpath
, depth
- 1 );
870 if (*appValue
!= buf
)
876 static UINT
check_directory( MSIPACKAGE
*package
, const WCHAR
*dir
, WCHAR
**appValue
)
878 DWORD attr
= msi_get_file_attributes( package
, dir
);
880 if (attr
!= INVALID_FILE_ATTRIBUTES
&& (attr
& FILE_ATTRIBUTE_DIRECTORY
))
882 TRACE("directory exists, returning %s\n", debugstr_w(dir
));
883 *appValue
= strdupW(dir
);
886 return ERROR_SUCCESS
;
889 static BOOL
is_full_path( const WCHAR
*path
)
891 WCHAR first
= towupper(path
[0]);
894 if (first
>= 'A' && first
<= 'Z' && path
[1] == ':')
896 else if (path
[0] == '\\' && path
[1] == '\\')
903 static UINT
search_directory( MSIPACKAGE
*package
, MSISIGNATURE
*sig
, const WCHAR
*path
, int depth
, WCHAR
**appValue
)
909 TRACE("%p, %p, %s, %d, %p\n", package
, sig
, debugstr_w(path
), depth
, appValue
);
911 if (is_full_path( path
))
914 rc
= recurse_search_directory( 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
= check_directory( 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
, ARRAY_SIZE(pathWithDrive
) - 3);
942 rc
= recurse_search_directory( package
, &val
, sig
, pathWithDrive
, depth
);
944 rc
= check_directory( package
, pathWithDrive
, &val
);
948 attr
= msi_get_file_attributes( package
, val
);
949 if (attr
!= INVALID_FILE_ATTRIBUTES
&& (attr
& FILE_ATTRIBUTE_DIRECTORY
) &&
950 val
&& val
[lstrlenW(val
) - 1] != '\\')
952 val
= msi_realloc(val
, (lstrlenW(val
) + 2) * sizeof(WCHAR
));
954 rc
= ERROR_OUTOFMEMORY
;
956 PathAddBackslashW(val
);
961 TRACE("returning %d\n", rc
);
965 static UINT
search_sig_name( MSIPACKAGE
*, const WCHAR
*, MSISIGNATURE
*, WCHAR
** );
967 static UINT
search_dr( MSIPACKAGE
*package
, WCHAR
**appValue
, MSISIGNATURE
*sig
)
969 static const WCHAR query
[] = {
970 's','e','l','e','c','t',' ','*',' ',
972 'D','r','L','o','c','a','t','o','r',' ',
973 'w','h','e','r','e',' ',
974 'S','i','g','n','a','t','u','r','e','_',' ','=',' ', '\'','%','s','\'',0};
975 LPWSTR parent
= NULL
;
977 WCHAR path
[MAX_PATH
];
978 WCHAR expanded
[MAX_PATH
];
984 TRACE("%s\n", debugstr_w(sig
->Name
));
988 row
= MSI_QueryGetRecord( package
->db
, query
, sig
->Name
);
991 TRACE("failed to query DrLocator for %s\n", debugstr_w(sig
->Name
));
992 return ERROR_SUCCESS
;
995 /* check whether parent is set */
996 parentName
= MSI_RecordGetString(row
, 2);
999 MSISIGNATURE parentSig
;
1001 search_sig_name( package
, parentName
, &parentSig
, &parent
);
1002 free_signature( &parentSig
);
1005 msiobj_release(&row
->hdr
);
1006 return ERROR_SUCCESS
;
1011 MSI_RecordGetStringW(row
, 3, path
, &sz
);
1013 if (MSI_RecordIsNull(row
,4))
1016 depth
= MSI_RecordGetInteger(row
,4);
1019 expand_any_path( package
, path
, expanded
, MAX_PATH
);
1021 lstrcpyW(expanded
, path
);
1025 attr
= msi_get_file_attributes( package
, parent
);
1026 if (attr
!= INVALID_FILE_ATTRIBUTES
&&
1027 !(attr
& FILE_ATTRIBUTE_DIRECTORY
))
1029 PathRemoveFileSpecW(parent
);
1030 PathAddBackslashW(parent
);
1033 lstrcpyW(path
, parent
);
1034 lstrcatW(path
, expanded
);
1036 else if (sz
) lstrcpyW(path
, expanded
);
1038 PathAddBackslashW(path
);
1040 rc
= search_directory( package
, sig
, path
, depth
, appValue
);
1043 msiobj_release(&row
->hdr
);
1044 TRACE("returning %d\n", rc
);
1048 static UINT
search_sig_name( MSIPACKAGE
*package
, const WCHAR
*sigName
, MSISIGNATURE
*sig
, WCHAR
**appValue
)
1053 rc
= get_signature( package
, sig
, sigName
);
1054 if (rc
== ERROR_SUCCESS
)
1056 rc
= search_components( package
, appValue
, sig
);
1057 if (rc
== ERROR_SUCCESS
&& !*appValue
)
1059 rc
= search_reg( package
, appValue
, sig
);
1060 if (rc
== ERROR_SUCCESS
&& !*appValue
)
1062 rc
= search_ini( package
, appValue
, sig
);
1063 if (rc
== ERROR_SUCCESS
&& !*appValue
)
1064 rc
= search_dr( package
, appValue
, sig
);
1071 static UINT
ITERATE_AppSearch(MSIRECORD
*row
, LPVOID param
)
1073 MSIPACKAGE
*package
= param
;
1074 LPCWSTR propName
, sigName
;
1075 LPWSTR value
= NULL
;
1080 /* get property and signature */
1081 propName
= MSI_RecordGetString(row
, 1);
1082 sigName
= MSI_RecordGetString(row
, 2);
1084 TRACE("%s %s\n", debugstr_w(propName
), debugstr_w(sigName
));
1086 r
= search_sig_name( package
, sigName
, &sig
, &value
);
1089 r
= msi_set_property( package
->db
, propName
, value
, -1 );
1090 if (r
== ERROR_SUCCESS
&& !wcscmp( propName
, szSourceDir
))
1091 msi_reset_source_folders( package
);
1095 free_signature( &sig
);
1097 uirow
= MSI_CreateRecord( 2 );
1098 MSI_RecordSetStringW( uirow
, 1, propName
);
1099 MSI_RecordSetStringW( uirow
, 2, sigName
);
1100 MSI_ProcessMessage(package
, INSTALLMESSAGE_ACTIONDATA
, uirow
);
1101 msiobj_release( &uirow
->hdr
);
1106 UINT
ACTION_AppSearch(MSIPACKAGE
*package
)
1108 static const WCHAR query
[] = {
1109 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1110 'A','p','p','S','e','a','r','c','h',0};
1114 if (msi_action_is_unique(package
, szAppSearch
))
1116 TRACE("Skipping AppSearch action: already done in UI sequence\n");
1117 return ERROR_SUCCESS
;
1120 msi_register_unique_action(package
, szAppSearch
);
1122 r
= MSI_OpenQuery( package
->db
, &view
, query
);
1123 if (r
!= ERROR_SUCCESS
)
1124 return ERROR_SUCCESS
;
1126 r
= MSI_IterateRecords( view
, NULL
, ITERATE_AppSearch
, package
);
1127 msiobj_release( &view
->hdr
);
1131 static UINT
ITERATE_CCPSearch(MSIRECORD
*row
, LPVOID param
)
1133 MSIPACKAGE
*package
= param
;
1135 LPWSTR value
= NULL
;
1137 UINT r
= ERROR_SUCCESS
;
1139 static const WCHAR success
[] = {'C','C','P','_','S','u','c','c','e','s','s',0};
1141 signature
= MSI_RecordGetString(row
, 1);
1143 TRACE("%s\n", debugstr_w(signature
));
1145 search_sig_name( package
, signature
, &sig
, &value
);
1148 TRACE("Found signature %s\n", debugstr_w(signature
));
1149 msi_set_property( package
->db
, success
, szOne
, -1 );
1151 r
= ERROR_NO_MORE_ITEMS
;
1154 free_signature(&sig
);
1158 UINT
ACTION_CCPSearch(MSIPACKAGE
*package
)
1160 static const WCHAR query
[] = {
1161 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1162 'C','C','P','S','e','a','r','c','h',0};
1166 if (msi_action_is_unique(package
, szCCPSearch
))
1168 TRACE("Skipping AppSearch action: already done in UI sequence\n");
1169 return ERROR_SUCCESS
;
1172 msi_register_unique_action(package
, szCCPSearch
);
1174 r
= MSI_OpenQuery(package
->db
, &view
, query
);
1175 if (r
!= ERROR_SUCCESS
)
1176 return ERROR_SUCCESS
;
1178 r
= MSI_IterateRecords(view
, NULL
, ITERATE_CCPSearch
, package
);
1179 msiobj_release(&view
->hdr
);