📄 gwcmdinfohash.cpp
字号:
////////////////////////////////////////////////////////////////////////////////
// File : gwcmdinfohash.cpp
//
// Copyright (C) 2002 PGP Corporation
//
// ABSTRACT
//
//
// Author: Satya S. Das
////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include "gwcmdinfohash.h"
#include <crtdbg.h>
#include "pgpdebug.h"
#include "util.h"
#include <process.h>
//////////////////////////////////////////////////////////////////
// Name:CCmdInfoHash::CCmdInfoHash
//
// Description:CCmdInfoHash constructor.
//
// In: none
//
// Out: none
//
// Comments:
///////////////////////////////////////////////////////////////////
CCmdInfoHash::CCmdInfoHash()
{
ZeroMemory(m_leArray, sizeof(m_leArray));
ZeroMemory(&m_leFilesToDelete, sizeof(LIST_ENTRY));
m_ulCount=0;
m_hMutex=NULL;
m_hDelThread=NULL;
m_hQuitDelThread=NULL;
m_bInited=FALSE;
}
//////////////////////////////////////////////////////////////////
// Name:CCmdInfoHash::Init()
//
// Description:Initializes the object if it has not been already
// inited. changes the m_bInited flag to true if
// successful.
// In: none
//
// Out: none
//
// Comments:
///////////////////////////////////////////////////////////////////
BOOL CCmdInfoHash::Init()
{
pgpAssert(0 == m_ulCount);
pgpAssert(NULL == m_hMutex);
pgpAssert(FALSE == m_bInited);
pgpAssert(NULL == m_hDelThread);
pgpAssert(NULL == m_hQuitDelThread);
if(TRUE == m_bInited)
return TRUE;
for (int iIndex=0;iIndex < MSGINFO_HASHTABLE_SIZE; iIndex++)
InitializeListHead(&m_leArray[iIndex]);
InitializeListHead(&m_leFilesToDelete);
m_hMutex=CreateMutex(NULL, FALSE, NULL);
pgpAssert(NULL != m_hMutex);
if(NULL == m_hMutex)
{
PgpGwTrace("CreateMutex failed with error %d\n", GetLastError());
return FALSE;
}
m_bInited=TRUE;
return TRUE;
}
//////////////////////////////////////////////////////////////////
// Name:CCmdInfoHash::Insert
//
// Description:This allows the client to insert a msginfo block
// into the hash.returns TRUE/FALSE depending on success
// or failure.
// In: MSGINFO pointer that is malloced should be passed to this.
//
// Out:
//
// Comments:
///////////////////////////////////////////////////////////////////
BOOL CCmdInfoHash::Insert(PMSGINFO pMsgInfo)
{
BOOL bRet=TRUE;
ULONG ulIndex=0;
//check parameters
pgpAssert(NULL != pMsgInfo);
pgpAssert(!IsBadReadPtr(pMsgInfo, sizeof(MSGINFO)));
if((NULL == pMsgInfo) || IsBadReadPtr(pMsgInfo, sizeof(MSGINFO)))
return FALSE;
//if not inited
if(FALSE == m_bInited)
{
bRet=Init();//initialze ourselves
if(FALSE == bRet)
return FALSE;
}
pgpAssert(NULL != pMsgInfo->ulKey);
ulIndex=(pMsgInfo->ulKey)%MSGINFO_HASHTABLE_SIZE;
pgpAssert(ulIndex < MSGINFO_HASHTABLE_SIZE);
pgpAssert(NULL != m_hMutex);
WaitForSingleObject(m_hMutex, INFINITE);
InsertHeadList(&m_leArray[ulIndex], (PLIST_ENTRY)pMsgInfo);
m_ulCount++;
ReleaseMutex(m_hMutex);
return TRUE;
}
//////////////////////////////////////////////////////////////////
// Name:CCmdInfoHash::MarkFileForDeletion
//
// Description:This allows the client to insert a msginfo block
// into the hash.returns TRUE/FALSE depending on success
// or failure.
// In: MSGINFO pointer that is malloced should be passed to this.
//
// Out:
//
// Comments:
///////////////////////////////////////////////////////////////////
BOOL CCmdInfoHash::MarkFileForDeletion(const char *pszFileName)
{
BOOL bRet=TRUE;
ULONG ulIndex=0;
TMPFILEINFO *ptfiDelInfo=NULL;
//check parameters
pgpAssert(NULL != pszFileName);
pgpAssert(!IsBadReadPtr(pszFileName, 1));
if((NULL == pszFileName) || IsBadReadPtr(pszFileName, 1))
return FALSE;
//if not inited
if(FALSE == m_bInited)
{
bRet=Init();//initialze ourselves
if(FALSE == bRet)
return FALSE;
}
//allocate the structure
ptfiDelInfo=(PTMPFILEINFO)calloc(sizeof(TMPFILEINFO), 1);
if(NULL == ptfiDelInfo)
return FALSE;
//put the file name in the structure
pgpAssert(NULL != ptfiDelInfo);
pgpAssert(NULL == ptfiDelInfo->szFilePath);
ptfiDelInfo->szFilePath=(char *)calloc(strlen(pszFileName)+1, 1);
pgpAssert(NULL != ptfiDelInfo->szFilePath);
if(NULL == ptfiDelInfo->szFilePath)
{
free(ptfiDelInfo);
ptfiDelInfo=NULL;
return FALSE;//allocation failed
}
strcpy(ptfiDelInfo->szFilePath, pszFileName);
//put the tick count in the insert time member
ptfiDelInfo->dwRequestTime=GetTickCount();
//put the structure in the list of files to be deleted at destruction
pgpAssert(NULL != m_hMutex);
WaitForSingleObject(m_hMutex, INFINITE);
InsertHeadList(&m_leFilesToDelete, (PLIST_ENTRY)ptfiDelInfo);
ReleaseMutex(m_hMutex);
//if the deletion thread has not started yet start it
if(NULL == m_hDelThread)
{
UINT uiThreadId=0;
//create a quit event for the deletion thread
pgpAssert(NULL == m_hQuitDelThread);
m_hQuitDelThread=CreateEvent(NULL, FALSE, FALSE, NULL);
pgpAssert(NULL != m_hQuitDelThread);
//create and start the temp file cleanup thread
//m_hDelThread=CreateThread(NULL, 0, &TempFileCleanupThreadProc,
// this, 0, &dwThreadId);
m_hDelThread=(HANDLE)_beginthreadex(NULL, 0, &TempFileCleanupThreadProc,
this, 0, &uiThreadId);
pgpAssert(NULL != m_hDelThread);
pgpAssert(0 != uiThreadId);
if(NULL == m_hDelThread)
{
PgpGwTrace("Temp file cleanup thread creation failed with error %d.\
Attachment files will not be cleaned !!\n", errno);
}
else
PgpGwTrace("Temp file cleanup thread spawned (id=%#x).\n", uiThreadId);
}
return TRUE;
}
//////////////////////////////////////////////////////////////////
// Name:CCmdInfoHash::Remove
//
// Description:This function accepts request to remove a certain
// MSGINFO blob from the hash based on the key (which
// currently is the HWND of the message window). So
// this searches the hash table for key and when found
// deletes it from the hash table. returns TRUE/FALSE.
//
// In: the key to the MSGINFO blob(=HWND)
//
// Out: none
//
// Comments:
///////////////////////////////////////////////////////////////////
BOOL CCmdInfoHash::Remove(ULONG ulKey)
{
PgpGwTrace("CCmdInfoHash::Remove ulKey=%#x\n", ulKey);
ULONG ulIndex=0;
PMSGINFO pMsgInfo=NULL;
PLIST_ENTRY pListEntry=NULL;
BOOL bRet=FALSE;
//if not inited
if(FALSE == m_bInited)
{
pgpAssert(FALSE);//should have been inited by now
return FALSE;//if not inited there cannot be anything to remove
}
ulIndex=ulKey%MSGINFO_HASHTABLE_SIZE;
pgpAssert(ulIndex < MSGINFO_HASHTABLE_SIZE);
pgpAssert(NULL != m_hMutex);
WaitForSingleObject(m_hMutex, INFINITE);
for (pListEntry=m_leArray[ulIndex].Flink;
pListEntry != &m_leArray[ulIndex];
pListEntry = pListEntry->Flink)
{
pMsgInfo = (PMSGINFO)pListEntry;
pgpAssert(NULL != pMsgInfo);
if (ulKey == pMsgInfo->ulKey)
{
//remove from the linked list
RemoveEntryList(pListEntry);
//free the entry itself
FreeMsgInfo((PMSGINFO)pListEntry);
//object chores
m_ulCount--;
bRet=TRUE;
break;
}
}
pgpAssert(NULL != m_hMutex);
ReleaseMutex(m_hMutex);
return bRet;
}
//////////////////////////////////////////////////////////////////
// Name:CCmdInfoHash::Get
//
// Description:Searches the hash table for a particular key. Pass
// the key and this will find the corresponding MSGINFO
// block of info that was inserted earlier into this
// hash table. It returns NULL pointer if there is no
// info.
//
// In: the key.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -