appsearch.c
来自「一个类似windows」· C语言 代码 · 共 977 行 · 第 1/3 页
C
977 行
{
static const WCHAR starDotStarW[] = { '*','.','*',0 };
UINT rc = ERROR_SUCCESS;
size_t dirLen = lstrlenW(dir), fileLen = lstrlenW(sig->File);
WCHAR *buf;
TRACE("Searching directory %s for file %s, depth %d\n", debugstr_w(dir),
debugstr_w(sig->File), depth);
if (depth < 0)
return ERROR_INVALID_PARAMETER;
*appFound = FALSE;
/* We need the buffer in both paths below, so go ahead and allocate it
* here. Add two because we might need to add a backslash if the dir name
* isn't backslash-terminated.
*/
buf = msi_alloc( (dirLen + max(fileLen, lstrlenW(starDotStarW)) + 2) * sizeof(WCHAR));
if (buf)
{
/* a depth of 0 implies we should search dir, so go ahead and search */
HANDLE hFind;
WIN32_FIND_DATAW findData;
memcpy(buf, dir, dirLen * sizeof(WCHAR));
if (buf[dirLen - 1] != '\\')
buf[dirLen++ - 1] = '\\';
memcpy(buf + dirLen, sig->File, (fileLen + 1) * sizeof(WCHAR));
hFind = FindFirstFileW(buf, &findData);
if (hFind != INVALID_HANDLE_VALUE)
{
BOOL matches;
/* assuming Signature can't contain wildcards for the file name,
* so don't bother with FindNextFileW here.
*/
if (!(rc = ACTION_FileMatchesSig(sig, &findData, buf, &matches))
&& matches)
{
TRACE("found file, setting %s to %s\n",
debugstr_w(sig->Property), debugstr_w(buf));
rc = MSI_SetPropertyW(package, sig->Property, buf);
*appFound = TRUE;
}
FindClose(hFind);
}
if (rc == ERROR_SUCCESS && !*appFound && depth > 0)
{
HANDLE hFind;
WIN32_FIND_DATAW findData;
memcpy(buf, dir, dirLen * sizeof(WCHAR));
if (buf[dirLen - 1] != '\\')
buf[dirLen++ - 1] = '\\';
lstrcpyW(buf + dirLen, starDotStarW);
hFind = FindFirstFileW(buf, &findData);
if (hFind != INVALID_HANDLE_VALUE)
{
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
rc = ACTION_RecurseSearchDirectory(package, appFound,
sig, findData.cFileName, depth - 1);
while (rc == ERROR_SUCCESS && !*appFound &&
FindNextFileW(hFind, &findData) != 0)
{
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
rc = ACTION_RecurseSearchDirectory(package, appFound,
sig, findData.cFileName, depth - 1);
}
FindClose(hFind);
}
}
msi_free(buf);
}
else
rc = ERROR_OUTOFMEMORY;
return rc;
}
static UINT ACTION_CheckDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
LPCWSTR dir)
{
UINT rc = ERROR_SUCCESS;
if (GetFileAttributesW(dir) & FILE_ATTRIBUTE_DIRECTORY)
{
TRACE("directory exists, setting %s to %s\n",
debugstr_w(sig->Property), debugstr_w(dir));
rc = MSI_SetPropertyW(package, sig->Property, dir);
}
return rc;
}
static BOOL ACTION_IsFullPath(LPCWSTR path)
{
WCHAR first = toupperW(path[0]);
BOOL ret;
if (first >= 'A' && first <= 'Z' && path[1] == ':')
ret = TRUE;
else if (path[0] == '\\' && path[1] == '\\')
ret = TRUE;
else
ret = FALSE;
return ret;
}
static UINT ACTION_SearchDirectory(MSIPACKAGE *package, MSISIGNATURE *sig,
LPCWSTR expanded, int depth)
{
UINT rc;
BOOL found;
TRACE("%p, %p, %s, %d\n", package, sig, debugstr_w(expanded), depth);
if (ACTION_IsFullPath(expanded))
{
if (sig->File)
rc = ACTION_RecurseSearchDirectory(package, &found, sig,
expanded, depth);
else
{
/* Recursively searching a directory makes no sense when the
* directory to search is the thing you're trying to find.
*/
rc = ACTION_CheckDirectory(package, sig, expanded);
}
}
else
{
WCHAR pathWithDrive[MAX_PATH] = { 'C',':','\\',0 };
DWORD drives = GetLogicalDrives();
int i;
rc = ERROR_SUCCESS;
found = FALSE;
for (i = 0; rc == ERROR_SUCCESS && !found && i < 26; i++)
if (drives & (1 << drives))
{
pathWithDrive[0] = 'A' + i;
if (GetDriveTypeW(pathWithDrive) == DRIVE_FIXED)
{
lstrcpynW(pathWithDrive + 3, expanded,
sizeof(pathWithDrive) / sizeof(pathWithDrive[0]) - 3);
if (sig->File)
rc = ACTION_RecurseSearchDirectory(package, &found, sig,
pathWithDrive, depth);
else
rc = ACTION_CheckDirectory(package, sig, pathWithDrive);
}
}
}
TRACE("returning %d\n", rc);
return rc;
}
static UINT ACTION_AppSearchDr(MSIPACKAGE *package, MSISIGNATURE *sig)
{
MSIQUERY *view;
UINT rc;
static const WCHAR ExecSeqQuery[] = {
's','e','l','e','c','t',' ','*',' ',
'f','r','o','m',' ',
'D','r','L','o','c','a','t','o','r',' ',
'w','h','e','r','e',' ','S','i','g','n','a','t','u','r','e','_',' ','=',' ',
'\'','%','s','\'',0};
TRACE("(package %p, sig %p)\n", package, sig);
rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, sig->Name);
if (rc == ERROR_SUCCESS)
{
MSIRECORD *row = 0;
WCHAR buffer[MAX_PATH], expanded[MAX_PATH];
DWORD sz;
int depth;
rc = MSI_ViewExecute(view, 0);
if (rc != ERROR_SUCCESS)
{
TRACE("MSI_ViewExecute returned %d\n", rc);
goto end;
}
rc = MSI_ViewFetch(view,&row);
if (rc != ERROR_SUCCESS)
{
TRACE("MSI_ViewFetch returned %d\n", rc);
rc = ERROR_SUCCESS;
goto end;
}
/* check whether parent is set */
buffer[0] = 0;
sz=sizeof(buffer)/sizeof(buffer[0]);
rc = MSI_RecordGetStringW(row,2,buffer,&sz);
if (rc != ERROR_SUCCESS)
{
ERR("Error is %x\n",rc);
goto end;
}
else if (buffer[0])
{
FIXME(": searching parent (%s) unimplemented\n",
debugstr_w(buffer));
goto end;
}
/* no parent, now look for path */
buffer[0] = 0;
sz=sizeof(buffer)/sizeof(buffer[0]);
rc = MSI_RecordGetStringW(row,3,buffer,&sz);
if (rc != ERROR_SUCCESS)
{
ERR("Error is %x\n",rc);
goto end;
}
if (MSI_RecordIsNull(row,4))
depth = 0;
else
depth = MSI_RecordGetInteger(row,4);
ACTION_ExpandAnyPath(package, buffer, expanded,
sizeof(expanded) / sizeof(expanded[0]));
rc = ACTION_SearchDirectory(package, sig, expanded, depth);
end:
msiobj_release(&row->hdr);
MSI_ViewClose(view);
msiobj_release(&view->hdr);
}
else
{
TRACE("MSI_OpenQuery returned %d\n", rc);
rc = ERROR_SUCCESS;
}
TRACE("returning %d\n", rc);
return rc;
}
/* http://msdn.microsoft.com/library/en-us/msi/setup/appsearch_table.asp
* is the best reference for the AppSearch table and how it's used.
*/
UINT ACTION_AppSearch(MSIPACKAGE *package)
{
MSIQUERY *view;
UINT rc;
static const WCHAR ExecSeqQuery[] = {
's','e','l','e','c','t',' ','*',' ',
'f','r','o','m',' ',
'A','p','p','S','e','a','r','c','h',0};
rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery);
if (rc == ERROR_SUCCESS)
{
MSIRECORD *row = 0;
WCHAR propBuf[0x100], sigBuf[0x100];
DWORD sz;
MSISIGNATURE sig;
BOOL appFound = FALSE;
rc = MSI_ViewExecute(view, 0);
if (rc != ERROR_SUCCESS)
goto end;
while (!rc)
{
rc = MSI_ViewFetch(view,&row);
if (rc != ERROR_SUCCESS)
{
rc = ERROR_SUCCESS;
break;
}
/* get property and signature */
propBuf[0] = 0;
sz=sizeof(propBuf)/sizeof(propBuf[0]);
rc = MSI_RecordGetStringW(row,1,propBuf,&sz);
if (rc != ERROR_SUCCESS)
{
ERR("Error is %x\n",rc);
msiobj_release(&row->hdr);
break;
}
sigBuf[0] = 0;
sz=sizeof(sigBuf)/sizeof(sigBuf[0]);
rc = MSI_RecordGetStringW(row,2,sigBuf,&sz);
if (rc != ERROR_SUCCESS)
{
ERR("Error is %x\n",rc);
msiobj_release(&row->hdr);
break;
}
TRACE("Searching for Property %s, Signature_ %s\n",
debugstr_w(propBuf), debugstr_w(sigBuf));
/* This clears all the fields, so set Name and Property afterward */
rc = ACTION_AppSearchGetSignature(package, &sig, sigBuf);
sig.Name = sigBuf;
sig.Property = propBuf;
if (rc == ERROR_SUCCESS)
{
rc = ACTION_AppSearchComponents(package, &appFound, &sig);
if (rc == ERROR_SUCCESS && !appFound)
{
rc = ACTION_AppSearchReg(package, &appFound, &sig);
if (rc == ERROR_SUCCESS && !appFound)
{
rc = ACTION_AppSearchIni(package, &appFound, &sig);
if (rc == ERROR_SUCCESS && !appFound)
rc = ACTION_AppSearchDr(package, &sig);
}
}
}
msi_free( sig.File);
msi_free( sig.Languages);
msiobj_release(&row->hdr);
}
end:
MSI_ViewClose(view);
msiobj_release(&view->hdr);
}
else
rc = ERROR_SUCCESS;
return rc;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?