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