Sync to Wine-20050725:
[reactos.git] / reactos / lib / 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "wine/unicode.h"
32 #include "wine/debug.h"
33 #include "msipriv.h"
34 #include "action.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
37
38 typedef struct tagMSISIGNATURE
39 {
40 LPWSTR Name; /* NOT owned by this structure */
41 LPWSTR Property; /* NOT owned by this structure */
42 LPWSTR File;
43 DWORD MinVersionMS;
44 DWORD MinVersionLS;
45 DWORD MaxVersionMS;
46 DWORD MaxVersionLS;
47 DWORD MinSize;
48 DWORD MaxSize;
49 FILETIME MinTime;
50 FILETIME MaxTime;
51 LPWSTR Languages;
52 }MSISIGNATURE;
53
54 static void ACTION_VerStrToInteger(LPCWSTR verStr, PDWORD ms, PDWORD ls)
55 {
56 const WCHAR *ptr;
57 int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
58
59 x1 = atoiW(verStr);
60 ptr = strchrW(verStr, '.');
61 if (ptr)
62 {
63 x2 = atoiW(ptr + 1);
64 ptr = strchrW(ptr + 1, '.');
65 }
66 if (ptr)
67 {
68 x3 = atoiW(ptr + 1);
69 ptr = strchrW(ptr + 1, '.');
70 }
71 if (ptr)
72 x4 = atoiW(ptr + 1);
73 /* FIXME: byte-order dependent? */
74 *ms = x1 << 16 | x2;
75 *ls = x3 << 16 | x4;
76 }
77
78 /* Fills in sig with the the values from the Signature table, where name is the
79 * signature to find. Upon return, sig->File will be NULL if the record is not
80 * found, and not NULL if it is found.
81 * Warning: clears all fields in sig!
82 * Returns ERROR_SUCCESS upon success (where not finding the record counts as
83 * success), something else on error.
84 */
85 static UINT ACTION_AppSearchGetSignature(MSIPACKAGE *package, MSISIGNATURE *sig,
86 LPCWSTR name)
87 {
88 MSIQUERY *view;
89 UINT rc;
90 static const WCHAR ExecSeqQuery[] = {
91 's','e','l','e','c','t',' ','*',' ',
92 'f','r','o','m',' ',
93 'S','i','g','n','a','t','u','r','e',' ',
94 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e',' ','=',' ',
95 '\'','%','s','\'',0};
96
97 TRACE("(package %p, sig %p)\n", package, sig);
98 memset(sig, 0, sizeof(*sig));
99 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, name);
100 if (rc == ERROR_SUCCESS)
101 {
102 MSIRECORD *row = 0;
103 DWORD time;
104 WCHAR *minVersion, *maxVersion;
105
106 rc = MSI_ViewExecute(view, 0);
107 if (rc != ERROR_SUCCESS)
108 {
109 TRACE("MSI_ViewExecute returned %d\n", rc);
110 goto end;
111 }
112 rc = MSI_ViewFetch(view,&row);
113 if (rc != ERROR_SUCCESS)
114 {
115 TRACE("MSI_ViewFetch returned %d\n", rc);
116 rc = ERROR_SUCCESS;
117 goto end;
118 }
119
120 /* get properties */
121 sig->File = load_dynamic_stringW(row,2);
122 minVersion = load_dynamic_stringW(row,3);
123 if (minVersion)
124 {
125 ACTION_VerStrToInteger(minVersion, &sig->MinVersionMS,
126 &sig->MinVersionLS);
127 HeapFree(GetProcessHeap(), 0, minVersion);
128 }
129 maxVersion = load_dynamic_stringW(row,4);
130 if (maxVersion)
131 {
132 ACTION_VerStrToInteger(maxVersion, &sig->MaxVersionMS,
133 &sig->MaxVersionLS);
134 HeapFree(GetProcessHeap(), 0, maxVersion);
135 }
136 sig->MinSize = MSI_RecordGetInteger(row,5);
137 if (sig->MinSize == MSI_NULL_INTEGER)
138 sig->MinSize = 0;
139 sig->MaxSize = MSI_RecordGetInteger(row,6);
140 if (sig->MaxSize == MSI_NULL_INTEGER)
141 sig->MaxSize = 0;
142 sig->Languages = load_dynamic_stringW(row,9);
143 time = MSI_RecordGetInteger(row,7);
144 if (time != MSI_NULL_INTEGER)
145 DosDateTimeToFileTime(HIWORD(time), LOWORD(time), &sig->MinTime);
146 time = MSI_RecordGetInteger(row,8);
147 if (time != MSI_NULL_INTEGER)
148 DosDateTimeToFileTime(HIWORD(time), LOWORD(time), &sig->MaxTime);
149 TRACE("Found file name %s for Signature_ %s;\n",
150 debugstr_w(sig->File), debugstr_w(name));
151 TRACE("MinVersion is %d.%d.%d.%d\n", HIWORD(sig->MinVersionMS),
152 LOWORD(sig->MinVersionMS), HIWORD(sig->MinVersionLS),
153 LOWORD(sig->MinVersionLS));
154 TRACE("MaxVersion is %d.%d.%d.%d\n", HIWORD(sig->MaxVersionMS),
155 LOWORD(sig->MaxVersionMS), HIWORD(sig->MaxVersionLS),
156 LOWORD(sig->MaxVersionLS));
157 TRACE("MinSize is %ld, MaxSize is %ld;\n", sig->MinSize, sig->MaxSize);
158 TRACE("Languages is %s\n", debugstr_w(sig->Languages));
159
160 end:
161 msiobj_release(&row->hdr);
162 MSI_ViewClose(view);
163 msiobj_release(&view->hdr);
164 }
165 else
166 {
167 TRACE("MSI_OpenQuery returned %d\n", rc);
168 rc = ERROR_SUCCESS;
169 }
170
171 TRACE("returning %d\n", rc);
172 return rc;
173 }
174
175 static UINT ACTION_AppSearchComponents(MSIPACKAGE *package, BOOL *appFound,
176 MSISIGNATURE *sig)
177 {
178 MSIQUERY *view;
179 UINT rc;
180 static const WCHAR ExecSeqQuery[] = {
181 's','e','l','e','c','t',' ','*',' ',
182 'f','r','o','m',' ',
183 'C','o','m','p','L','o','c','a','t','o','r',' ',
184 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e','_',' ','=',' ',
185 '\'','%','s','\'',0};
186
187 TRACE("(package %p, appFound %p, sig %p)\n", package, appFound, sig);
188 *appFound = FALSE;
189 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, sig->Name);
190 if (rc == ERROR_SUCCESS)
191 {
192 MSIRECORD *row = 0;
193 WCHAR guid[50];
194 DWORD sz;
195
196 rc = MSI_ViewExecute(view, 0);
197 if (rc != ERROR_SUCCESS)
198 {
199 TRACE("MSI_ViewExecute returned %d\n", rc);
200 goto end;
201 }
202 rc = MSI_ViewFetch(view,&row);
203 if (rc != ERROR_SUCCESS)
204 {
205 TRACE("MSI_ViewFetch returned %d\n", rc);
206 rc = ERROR_SUCCESS;
207 goto end;
208 }
209
210 /* get GUID */
211 guid[0] = 0;
212 sz=sizeof(guid)/sizeof(guid[0]);
213 rc = MSI_RecordGetStringW(row,2,guid,&sz);
214 if (rc != ERROR_SUCCESS)
215 {
216 ERR("Error is %x\n",rc);
217 goto end;
218 }
219 FIXME("AppSearch unimplemented for CompLocator table (GUID %s)\n",
220 debugstr_w(guid));
221
222 end:
223 msiobj_release(&row->hdr);
224 MSI_ViewClose(view);
225 msiobj_release(&view->hdr);
226 }
227 else
228 {
229 TRACE("MSI_OpenQuery returned %d\n", rc);
230 rc = ERROR_SUCCESS;
231 }
232
233 TRACE("returning %d\n", rc);
234 return rc;
235 }
236
237 static UINT ACTION_AppSearchReg(MSIPACKAGE *package, BOOL *appFound,
238 MSISIGNATURE *sig)
239 {
240 MSIQUERY *view;
241 UINT rc;
242 static const WCHAR ExecSeqQuery[] = {
243 's','e','l','e','c','t',' ','*',' ',
244 'f','r','o','m',' ',
245 'R','e','g','L','o','c','a','t','o','r',' ',
246 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e','_',' ','=',' ',
247 '\'','%','s','\'',0};
248 static const WCHAR dwordFmt[] = { '#','%','d','\0' };
249 static const WCHAR expandSzFmt[] = { '#','%','%','%','s','\0' };
250 static const WCHAR binFmt[] = { '#','x','%','x','\0' };
251
252 TRACE("(package %p, appFound %p, sig %p)\n", package, appFound, sig);
253 *appFound = FALSE;
254 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, sig->Name);
255 if (rc == ERROR_SUCCESS)
256 {
257 MSIRECORD *row = 0;
258 LPWSTR keyPath = NULL, valueName = NULL, propertyValue = NULL;
259 int root, type;
260 HKEY rootKey, key = NULL;
261 DWORD sz = 0, regType, i;
262 LPBYTE value = NULL;
263
264 rc = MSI_ViewExecute(view, 0);
265 if (rc != ERROR_SUCCESS)
266 {
267 TRACE("MSI_ViewExecute returned %d\n", rc);
268 goto end;
269 }
270 rc = MSI_ViewFetch(view,&row);
271 if (rc != ERROR_SUCCESS)
272 {
273 TRACE("MSI_ViewFetch returned %d\n", rc);
274 rc = ERROR_SUCCESS;
275 goto end;
276 }
277
278 root = MSI_RecordGetInteger(row,2);
279 keyPath = load_dynamic_stringW(row,3);
280 /* FIXME: keyPath needs to be expanded for properties */
281 valueName = load_dynamic_stringW(row,4);
282 /* FIXME: valueName probably does too */
283 type = MSI_RecordGetInteger(row,5);
284
285 if ((type & 0x0f) != msidbLocatorTypeRawValue)
286 {
287 FIXME("AppSearch unimplemented for type %d (key path %s, value %s)\n",
288 type, debugstr_w(keyPath), debugstr_w(valueName));
289 goto end;
290 }
291
292 switch (root)
293 {
294 case msidbRegistryRootClassesRoot:
295 rootKey = HKEY_CLASSES_ROOT;
296 break;
297 case msidbRegistryRootCurrentUser:
298 rootKey = HKEY_CURRENT_USER;
299 break;
300 case msidbRegistryRootLocalMachine:
301 rootKey = HKEY_LOCAL_MACHINE;
302 break;
303 case msidbRegistryRootUsers:
304 rootKey = HKEY_USERS;
305 break;
306 default:
307 WARN("Unknown root key %d\n", root);
308 goto end;
309 }
310
311 rc = RegCreateKeyW(rootKey, keyPath, &key);
312 if (rc)
313 {
314 TRACE("RegCreateKeyW returned %d\n", rc);
315 rc = ERROR_SUCCESS;
316 goto end;
317 }
318 rc = RegQueryValueExW(key, valueName, NULL, NULL, NULL, &sz);
319 if (rc)
320 {
321 TRACE("RegQueryValueExW returned %d\n", rc);
322 rc = ERROR_SUCCESS;
323 goto end;
324 }
325 /* FIXME: sanity-check sz before allocating (is there an upper-limit
326 * on the value of a property?)
327 */
328 value = HeapAlloc(GetProcessHeap(), 0, sz);
329 rc = RegQueryValueExW(key, valueName, NULL, &regType, value, &sz);
330 if (rc)
331 {
332 TRACE("RegQueryValueExW returned %d\n", rc);
333 rc = ERROR_SUCCESS;
334 goto end;
335 }
336
337 /* bail out if the registry key is empty */
338 if (sz == 0)
339 {
340 rc = ERROR_SUCCESS;
341 goto end;
342 }
343
344 switch (regType)
345 {
346 case REG_SZ:
347 if (*(LPWSTR)value == '#')
348 {
349 /* escape leading pound with another */
350 propertyValue = HeapAlloc(GetProcessHeap(), 0,
351 sz + sizeof(WCHAR));
352 propertyValue[0] = '#';
353 strcpyW(propertyValue + 1, (LPWSTR)value);
354 }
355 else
356 {
357 propertyValue = HeapAlloc(GetProcessHeap(), 0, sz);
358 strcpyW(propertyValue, (LPWSTR)value);
359 }
360 break;
361 case REG_DWORD:
362 /* 7 chars for digits, 1 for NULL, 1 for #, and 1 for sign
363 * char if needed
364 */
365 propertyValue = HeapAlloc(GetProcessHeap(), 0,
366 10 * sizeof(WCHAR));
367 sprintfW(propertyValue, dwordFmt, *(DWORD *)value);
368 break;
369 case REG_EXPAND_SZ:
370 /* space for extra #% characters in front */
371 propertyValue = HeapAlloc(GetProcessHeap(), 0,
372 sz + 2 * sizeof(WCHAR));
373 sprintfW(propertyValue, expandSzFmt, (LPWSTR)value);
374 break;
375 case REG_BINARY:
376 /* 3 == length of "#x<nibble>" */
377 propertyValue = HeapAlloc(GetProcessHeap(), 0,
378 (sz * 3 + 1) * sizeof(WCHAR));
379 for (i = 0; i < sz; i++)
380 sprintfW(propertyValue + i * 3, binFmt, value[i]);
381 break;
382 default:
383 WARN("unimplemented for values of type %ld\n", regType);
384 goto end;
385 }
386
387 TRACE("found registry value, setting %s to %s\n",
388 debugstr_w(sig->Property), debugstr_w(propertyValue));
389 rc = MSI_SetPropertyW(package, sig->Property, propertyValue);
390 *appFound = TRUE;
391
392 end:
393 HeapFree(GetProcessHeap(), 0, propertyValue);
394 HeapFree(GetProcessHeap(), 0, value);
395 RegCloseKey(key);
396
397 HeapFree(GetProcessHeap(), 0, keyPath);
398 HeapFree(GetProcessHeap(), 0, valueName);
399
400 msiobj_release(&row->hdr);
401 MSI_ViewClose(view);
402 msiobj_release(&view->hdr);
403 }
404 else
405 {
406 TRACE("MSI_OpenQuery returned %d\n", rc);
407 rc = ERROR_SUCCESS;
408 }
409
410 TRACE("returning %d\n", rc);
411 return rc;
412 }
413
414 static UINT ACTION_AppSearchIni(MSIPACKAGE *package, BOOL *appFound,
415 MSISIGNATURE *sig)
416 {
417 MSIQUERY *view;
418 UINT rc;
419 static const WCHAR ExecSeqQuery[] = {
420 's','e','l','e','c','t',' ','*',' ',
421 'f','r','o','m',' ',
422 'I','n','i','L','o','c','a','t','o','r',' ',
423 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e','_',' ','=',' ',
424 '\'','%','s','\'',0};
425
426 TRACE("(package %p, appFound %p, sig %p)\n", package, appFound, sig);
427 *appFound = FALSE;
428 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, sig->Name);
429 if (rc == ERROR_SUCCESS)
430 {
431 MSIRECORD *row = 0;
432 LPWSTR fileName;
433
434 rc = MSI_ViewExecute(view, 0);
435 if (rc != ERROR_SUCCESS)
436 {
437 TRACE("MSI_ViewExecute returned %d\n", rc);
438 goto end;
439 }
440 rc = MSI_ViewFetch(view,&row);
441 if (rc != ERROR_SUCCESS)
442 {
443 TRACE("MSI_ViewFetch returned %d\n", rc);
444 rc = ERROR_SUCCESS;
445 goto end;
446 }
447
448 /* get file name */
449 fileName = load_dynamic_stringW(row,2);
450 FIXME("AppSearch unimplemented for IniLocator (ini file name %s)\n",
451 debugstr_w(fileName));
452 HeapFree(GetProcessHeap(), 0, fileName);
453
454 end:
455 msiobj_release(&row->hdr);
456 MSI_ViewClose(view);
457 msiobj_release(&view->hdr);
458 }
459 else
460 {
461 TRACE("MSI_OpenQuery returned %d\n", rc);
462 rc = ERROR_SUCCESS;
463 }
464
465
466 TRACE("returning %d\n", rc);
467 return rc;
468 }
469
470 /* Expands the value in src into a path without property names and only
471 * containing long path names into dst. Replaces at most len characters of dst,
472 * and always NULL-terminates dst if dst is not NULL and len >= 1.
473 * May modify src.
474 * Assumes src and dst are non-overlapping.
475 * FIXME: return code probably needed:
476 * - what does AppSearch return if the table values are invalid?
477 * - what if dst is too small?
478 */
479 static void ACTION_ExpandAnyPath(MSIPACKAGE *package, WCHAR *src, WCHAR *dst,
480 size_t len)
481 {
482 WCHAR *ptr;
483 size_t copied = 0;
484
485 if (!src || !dst || !len)
486 return;
487
488 /* Ignore the short portion of the path, don't think we can use it anyway */
489 if ((ptr = strchrW(src, '|')))
490 ptr++;
491 else
492 ptr = src;
493 while (*ptr && copied < len - 1)
494 {
495 WCHAR *prop = strchrW(ptr, '[');
496
497 if (prop)
498 {
499 WCHAR *propEnd = strchrW(prop + 1, ']');
500
501 if (!propEnd)
502 {
503 WARN("Unterminated property name in AnyPath: %s\n",
504 debugstr_w(prop));
505 break;
506 }
507 else
508 {
509 DWORD propLen;
510
511 *propEnd = 0;
512 propLen = len - copied - 1;
513 MSI_GetPropertyW(package, prop + 1, dst + copied, &propLen);
514 ptr = propEnd + 1;
515 copied += propLen;
516 }
517 }
518 else
519 {
520 size_t toCopy = min(strlenW(ptr) + 1, len - copied - 1);
521
522 memcpy(dst + copied, ptr, toCopy * sizeof(WCHAR));
523 ptr += toCopy;
524 copied += toCopy;
525 }
526 }
527 *(dst + copied) = '\0';
528 }
529
530 /* Sets *matches to whether the file (whose path is filePath) matches the
531 * versions set in sig.
532 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
533 * something else if an install-halting error occurs.
534 */
535 static UINT ACTION_FileVersionMatches(MSISIGNATURE *sig, LPCWSTR filePath,
536 BOOL *matches)
537 {
538 UINT rc = ERROR_SUCCESS;
539
540 *matches = FALSE;
541 if (sig->Languages)
542 {
543 FIXME(": need to check version for languages %s\n",
544 debugstr_w(sig->Languages));
545 }
546 else
547 {
548 DWORD zero, size = GetFileVersionInfoSizeW((LPWSTR)filePath, &zero);
549
550 if (size)
551 {
552 LPVOID buf = HeapAlloc(GetProcessHeap(), 0, size);
553
554 if (buf)
555 {
556 static const WCHAR rootW[] = { '\\',0 };
557 UINT versionLen;
558 LPVOID subBlock = NULL;
559
560 if (GetFileVersionInfoW((LPWSTR)filePath, 0, size, buf))
561 VerQueryValueW(buf, (LPWSTR)rootW, &subBlock, &versionLen);
562 if (subBlock)
563 {
564 VS_FIXEDFILEINFO *info =
565 (VS_FIXEDFILEINFO *)subBlock;
566
567 TRACE("Comparing file version %d.%d.%d.%d:\n",
568 HIWORD(info->dwFileVersionMS),
569 LOWORD(info->dwFileVersionMS),
570 HIWORD(info->dwFileVersionLS),
571 LOWORD(info->dwFileVersionLS));
572 if (info->dwFileVersionMS < sig->MinVersionMS
573 || (info->dwFileVersionMS == sig->MinVersionMS &&
574 info->dwFileVersionLS < sig->MinVersionLS))
575 {
576 TRACE("Less than minimum version %d.%d.%d.%d\n",
577 HIWORD(sig->MinVersionMS),
578 LOWORD(sig->MinVersionMS),
579 HIWORD(sig->MinVersionLS),
580 LOWORD(sig->MinVersionLS));
581 }
582 else if (info->dwFileVersionMS < sig->MinVersionMS
583 || (info->dwFileVersionMS == sig->MinVersionMS &&
584 info->dwFileVersionLS < sig->MinVersionLS))
585 {
586 TRACE("Greater than minimum version %d.%d.%d.%d\n",
587 HIWORD(sig->MaxVersionMS),
588 LOWORD(sig->MaxVersionMS),
589 HIWORD(sig->MaxVersionLS),
590 LOWORD(sig->MaxVersionLS));
591 }
592 else
593 *matches = TRUE;
594 }
595 HeapFree(GetProcessHeap(), 0, buf);
596 }
597 else
598 rc = ERROR_OUTOFMEMORY;
599 }
600 }
601 return rc;
602 }
603
604 /* Sets *matches to whether the file in findData matches that in sig.
605 * fullFilePath is assumed to be the full path of the file specified in
606 * findData, which may be necessary to compare the version.
607 * Return ERROR_SUCCESS in case of success (whether or not the file matches),
608 * something else if an install-halting error occurs.
609 */
610 static UINT ACTION_FileMatchesSig(MSISIGNATURE *sig,
611 LPWIN32_FIND_DATAW findData, LPCWSTR fullFilePath, BOOL *matches)
612 {
613 UINT rc = ERROR_SUCCESS;
614
615 *matches = TRUE;
616 /* assumes the caller has already ensured the filenames match, so check
617 * the other fields..
618 */
619 if (sig->MinTime.dwLowDateTime || sig->MinTime.dwHighDateTime)
620 {
621 if (findData->ftCreationTime.dwHighDateTime <
622 sig->MinTime.dwHighDateTime ||
623 (findData->ftCreationTime.dwHighDateTime == sig->MinTime.dwHighDateTime
624 && findData->ftCreationTime.dwLowDateTime <
625 sig->MinTime.dwLowDateTime))
626 *matches = FALSE;
627 }
628 if (*matches && (sig->MaxTime.dwLowDateTime || sig->MaxTime.dwHighDateTime))
629 {
630 if (findData->ftCreationTime.dwHighDateTime >
631 sig->MaxTime.dwHighDateTime ||
632 (findData->ftCreationTime.dwHighDateTime == sig->MaxTime.dwHighDateTime
633 && findData->ftCreationTime.dwLowDateTime >
634 sig->MaxTime.dwLowDateTime))
635 *matches = FALSE;
636 }
637 if (*matches && sig->MinSize && findData->nFileSizeLow < sig->MinSize)
638 *matches = FALSE;
639 if (*matches && sig->MaxSize && findData->nFileSizeLow > sig->MaxSize)
640 *matches = FALSE;
641 if (*matches && (sig->MinVersionMS || sig->MinVersionLS ||
642 sig->MaxVersionMS || sig->MaxVersionLS))
643 rc = ACTION_FileVersionMatches(sig, fullFilePath, matches);
644 return rc;
645 }
646
647 /* Recursively searches the directory dir for files that match the signature
648 * sig, up to (depth + 1) levels deep. That is, if depth is 0, it searches dir
649 * (and only dir). If depth is 1, searches dir and its immediate
650 * subdirectories.
651 * Assumes sig->File is not NULL.
652 * Returns ERROR_SUCCESS on success (which may include non-critical errors),
653 * something else on failures which should halt the install.
654 */
655 static UINT ACTION_RecurseSearchDirectory(MSIPACKAGE *package, BOOL *appFound,
656 MSISIGNATURE *sig, LPCWSTR dir, int depth)
657 {
658 static const WCHAR starDotStarW[] = { '*','.','*',0 };
659 UINT rc = ERROR_SUCCESS;
660 size_t dirLen = lstrlenW(dir), fileLen = lstrlenW(sig->File);
661 WCHAR *buf;
662
663 TRACE("Searching directory %s for file %s, depth %d\n", debugstr_w(dir),
664 debugstr_w(sig->File), depth);
665
666 if (depth < 0)
667 return ERROR_INVALID_PARAMETER;
668
669 *appFound = FALSE;
670 /* We need the buffer in both paths below, so go ahead and allocate it
671 * here. Add two because we might need to add a backslash if the dir name
672 * isn't backslash-terminated.
673 */
674 buf = HeapAlloc(GetProcessHeap(), 0,
675 (dirLen + max(fileLen, lstrlenW(starDotStarW)) + 2) * sizeof(WCHAR));
676 if (buf)
677 {
678 /* a depth of 0 implies we should search dir, so go ahead and search */
679 HANDLE hFind;
680 WIN32_FIND_DATAW findData;
681
682 memcpy(buf, dir, dirLen * sizeof(WCHAR));
683 if (buf[dirLen - 1] != '\\')
684 buf[dirLen++ - 1] = '\\';
685 memcpy(buf + dirLen, sig->File, (fileLen + 1) * sizeof(WCHAR));
686 hFind = FindFirstFileW(buf, &findData);
687 if (hFind != INVALID_HANDLE_VALUE)
688 {
689 BOOL matches;
690
691 /* assuming Signature can't contain wildcards for the file name,
692 * so don't bother with FindNextFileW here.
693 */
694 if (!(rc = ACTION_FileMatchesSig(sig, &findData, buf, &matches))
695 && matches)
696 {
697 TRACE("found file, setting %s to %s\n",
698 debugstr_w(sig->Property), debugstr_w(buf));
699 rc = MSI_SetPropertyW(package, sig->Property, buf);
700 *appFound = TRUE;
701 }
702 FindClose(hFind);
703 }
704 if (rc == ERROR_SUCCESS && !*appFound && depth > 0)
705 {
706 HANDLE hFind;
707 WIN32_FIND_DATAW findData;
708
709 memcpy(buf, dir, dirLen * sizeof(WCHAR));
710 if (buf[dirLen - 1] != '\\')
711 buf[dirLen++ - 1] = '\\';
712 lstrcpyW(buf + dirLen, starDotStarW);
713 hFind = FindFirstFileW(buf, &findData);
714 if (hFind != INVALID_HANDLE_VALUE)
715 {
716 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
717 rc = ACTION_RecurseSearchDirectory(package, appFound,
718 sig, findData.cFileName, depth - 1);
719 while (rc == ERROR_SUCCESS && !*appFound &&
720 FindNextFileW(hFind, &findData) != 0)
721 {
722 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
723 rc = ACTION_RecurseSearchDirectory(package, appFound,
724 sig, findData.cFileName, depth - 1);
725 }
726 FindClose(hFind);
727 }
728 }
729 HeapFree(GetProcessHeap(), 0, buf);
730 }
731 else
732 rc = ERROR_OUTOFMEMORY;
733
734 return rc;
735 }
736
737 static UINT ACTION_CheckDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
738 LPCWSTR dir)
739 {
740 UINT rc = ERROR_SUCCESS;
741
742 if (GetFileAttributesW(dir) & FILE_ATTRIBUTE_DIRECTORY)
743 {
744 TRACE("directory exists, setting %s to %s\n",
745 debugstr_w(sig->Property), debugstr_w(dir));
746 rc = MSI_SetPropertyW(package, sig->Property, dir);
747 }
748 return rc;
749 }
750
751 static BOOL ACTION_IsFullPath(LPCWSTR path)
752 {
753 WCHAR first = toupperW(path[0]);
754 BOOL ret;
755
756 if (first >= 'A' && first <= 'Z' && path[1] == ':')
757 ret = TRUE;
758 else if (path[0] == '\\' && path[1] == '\\')
759 ret = TRUE;
760 else
761 ret = FALSE;
762 return ret;
763 }
764
765 static UINT ACTION_SearchDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
766 LPCWSTR expanded, int depth)
767 {
768 UINT rc;
769 BOOL found;
770
771 TRACE("%p, %p, %s, %d\n", package, sig, debugstr_w(expanded), depth);
772 if (ACTION_IsFullPath(expanded))
773 {
774 if (sig->File)
775 rc = ACTION_RecurseSearchDirectory(package, &found, sig,
776 expanded, depth);
777 else
778 {
779 /* Recursively searching a directory makes no sense when the
780 * directory to search is the thing you're trying to find.
781 */
782 rc = ACTION_CheckDirectory(package, sig, expanded);
783 }
784 }
785 else
786 {
787 WCHAR pathWithDrive[MAX_PATH] = { 'C',':','\\',0 };
788 DWORD drives = GetLogicalDrives();
789 int i;
790
791 rc = ERROR_SUCCESS;
792 found = FALSE;
793 for (i = 0; rc == ERROR_SUCCESS && !found && i < 26; i++)
794 if (drives & (1 << drives))
795 {
796 pathWithDrive[0] = 'A' + i;
797 if (GetDriveTypeW(pathWithDrive) == DRIVE_FIXED)
798 {
799 lstrcpynW(pathWithDrive + 3, expanded,
800 sizeof(pathWithDrive) / sizeof(pathWithDrive[0]) - 3);
801 if (sig->File)
802 rc = ACTION_RecurseSearchDirectory(package, &found, sig,
803 pathWithDrive, depth);
804 else
805 rc = ACTION_CheckDirectory(package, sig, pathWithDrive);
806 }
807 }
808 }
809 TRACE("returning %d\n", rc);
810 return rc;
811 }
812
813 static UINT ACTION_AppSearchDr(MSIPACKAGE *package, MSISIGNATURE *sig)
814 {
815 MSIQUERY *view;
816 UINT rc;
817 static const WCHAR ExecSeqQuery[] = {
818 's','e','l','e','c','t',' ','*',' ',
819 'f','r','o','m',' ',
820 'D','r','L','o','c','a','t','o','r',' ',
821 'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e','_',' ','=',' ',
822 '\'','%','s','\'',0};
823
824 TRACE("(package %p, sig %p)\n", package, sig);
825 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, sig->Name);
826 if (rc == ERROR_SUCCESS)
827 {
828 MSIRECORD *row = 0;
829 WCHAR buffer[MAX_PATH], expanded[MAX_PATH];
830 DWORD sz;
831 int depth;
832
833 rc = MSI_ViewExecute(view, 0);
834 if (rc != ERROR_SUCCESS)
835 {
836 TRACE("MSI_ViewExecute returned %d\n", rc);
837 goto end;
838 }
839 rc = MSI_ViewFetch(view,&row);
840 if (rc != ERROR_SUCCESS)
841 {
842 TRACE("MSI_ViewFetch returned %d\n", rc);
843 rc = ERROR_SUCCESS;
844 goto end;
845 }
846
847 /* check whether parent is set */
848 buffer[0] = 0;
849 sz=sizeof(buffer)/sizeof(buffer[0]);
850 rc = MSI_RecordGetStringW(row,2,buffer,&sz);
851 if (rc != ERROR_SUCCESS)
852 {
853 ERR("Error is %x\n",rc);
854 goto end;
855 }
856 else if (buffer[0])
857 {
858 FIXME(": searching parent (%s) unimplemented\n",
859 debugstr_w(buffer));
860 goto end;
861 }
862 /* no parent, now look for path */
863 buffer[0] = 0;
864 sz=sizeof(buffer)/sizeof(buffer[0]);
865 rc = MSI_RecordGetStringW(row,3,buffer,&sz);
866 if (rc != ERROR_SUCCESS)
867 {
868 ERR("Error is %x\n",rc);
869 goto end;
870 }
871 if (MSI_RecordIsNull(row,4))
872 depth = 0;
873 else
874 depth = MSI_RecordGetInteger(row,4);
875 ACTION_ExpandAnyPath(package, buffer, expanded,
876 sizeof(expanded) / sizeof(expanded[0]));
877 rc = ACTION_SearchDirectory(package, sig, expanded, depth);
878
879 end:
880 msiobj_release(&row->hdr);
881 MSI_ViewClose(view);
882 msiobj_release(&view->hdr);
883 }
884 else
885 {
886 TRACE("MSI_OpenQuery returned %d\n", rc);
887 rc = ERROR_SUCCESS;
888 }
889
890
891 TRACE("returning %d\n", rc);
892 return rc;
893 }
894
895 /* http://msdn.microsoft.com/library/en-us/msi/setup/appsearch_table.asp
896 * is the best reference for the AppSearch table and how it's used.
897 */
898 UINT ACTION_AppSearch(MSIPACKAGE *package)
899 {
900 MSIQUERY *view;
901 UINT rc;
902 static const WCHAR ExecSeqQuery[] = {
903 's','e','l','e','c','t',' ','*',' ',
904 'f','r','o','m',' ',
905 'A','p','p','S','e','a','r','c','h',0};
906
907 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery);
908 if (rc == ERROR_SUCCESS)
909 {
910 MSIRECORD *row = 0;
911 WCHAR propBuf[0x100], sigBuf[0x100];
912 DWORD sz;
913 MSISIGNATURE sig;
914 BOOL appFound = FALSE;
915
916 rc = MSI_ViewExecute(view, 0);
917 if (rc != ERROR_SUCCESS)
918 goto end;
919
920 while (!rc)
921 {
922 rc = MSI_ViewFetch(view,&row);
923 if (rc != ERROR_SUCCESS)
924 {
925 rc = ERROR_SUCCESS;
926 break;
927 }
928
929 /* get property and signature */
930 propBuf[0] = 0;
931 sz=sizeof(propBuf)/sizeof(propBuf[0]);
932 rc = MSI_RecordGetStringW(row,1,propBuf,&sz);
933 if (rc != ERROR_SUCCESS)
934 {
935 ERR("Error is %x\n",rc);
936 msiobj_release(&row->hdr);
937 break;
938 }
939 sigBuf[0] = 0;
940 sz=sizeof(sigBuf)/sizeof(sigBuf[0]);
941 rc = MSI_RecordGetStringW(row,2,sigBuf,&sz);
942 if (rc != ERROR_SUCCESS)
943 {
944 ERR("Error is %x\n",rc);
945 msiobj_release(&row->hdr);
946 break;
947 }
948 TRACE("Searching for Property %s, Signature_ %s\n",
949 debugstr_w(propBuf), debugstr_w(sigBuf));
950 /* This clears all the fields, so set Name and Property afterward */
951 rc = ACTION_AppSearchGetSignature(package, &sig, sigBuf);
952 sig.Name = sigBuf;
953 sig.Property = propBuf;
954 if (rc == ERROR_SUCCESS)
955 {
956 rc = ACTION_AppSearchComponents(package, &appFound, &sig);
957 if (rc == ERROR_SUCCESS && !appFound)
958 {
959 rc = ACTION_AppSearchReg(package, &appFound, &sig);
960 if (rc == ERROR_SUCCESS && !appFound)
961 {
962 rc = ACTION_AppSearchIni(package, &appFound, &sig);
963 if (rc == ERROR_SUCCESS && !appFound)
964 rc = ACTION_AppSearchDr(package, &sig);
965 }
966 }
967 }
968 HeapFree(GetProcessHeap(), 0, sig.File);
969 HeapFree(GetProcessHeap(), 0, sig.Languages);
970 msiobj_release(&row->hdr);
971 }
972
973 end:
974 MSI_ViewClose(view);
975 msiobj_release(&view->hdr);
976 }
977 else
978 rc = ERROR_SUCCESS;
979
980 return rc;
981 }