ptwipedl.c

来自「PGP8.0源码 请认真阅读您的文件包然后写出其具体功能」· C语言 代码 · 共 335 行

C
335
字号
/*__________________________________________________________________________
 Copyright (C) 2002 PGP Corporation
 All rights reserved.
 
 $Id: PTwipedl.c,v 1.15 2002/08/06 20:10:33 dallen Exp $
__________________________________________________________________________*/
#include "precomp.h"
#include <process.h>
#include "PGPwd.h"               //defines and structs for VxD conversations
#include "PGPsdkDriver.h"
#include "PTdisk.h"

// Global Variables
HANDLE				stop_flag;
HANDLE				thread_handle;
DWORD				thread_id;
CRITICAL_SECTION	crit_sec; 
BOOL				bWDIsNT;
HANDLE				hDriver;
BOOL				bDriverOK;
HANDLE				hWorkThreadEvent;

BOOL DoUtilityDriverCall(PPGPWIPEDELETESTRUCT ppwds)
{
	DWORD dwLen;
	BOOL bresult;

	bresult=FALSE;

	if(hDriver!=0)
	{
		if(DeviceIoControl(hDriver, IOCTL_PGPUTIL_WIPEDELETE,
			ppwds, sizeof(PGPWIPEDELETESTRUCT),
			ppwds, sizeof(PGPWIPEDELETESTRUCT),
			&dwLen, NULL ))
		{
			bresult=TRUE;
		}
	}

	return bresult;
}

void DoFileWipe(PPGPWIPEDELETESTRUCT ppwds)
{
	FILELIST *fl;
	BOOL bSuccess;
	DWORD dwWipeFlags;
///	int szLen;

	dwWipeFlags=WIPE_DEFAULT|WIPE_DONOTWARN|WIPE_DONOTERROR;

	if(!g_bWipeProgress)
	{
		dwWipeFlags|=WIPE_DONOTPROGRESS;
	}
	
	if(!bWDIsNT)
		dwWipeFlags|=WIPE_DONOTDELETE;

	fl=NULL;
	PGPscAddToFileList(&fl,ppwds->usFileName,NULL,NULL);

	if(fl!=NULL)
	{
		// The null prevents us from locking up if PGPtray 
		// deletes files inside a message handler.
		bSuccess=PGPscWipeFileList(NULL,PGPsc,fl,dwWipeFlags,g_dwPasses);
	}
}

void __stdcall WipeDeleteCallbackFunction(void *keFileRecord)
{
	PGPWIPEDELETESTRUCT pwds;

	memset(&pwds,0x00,sizeof(PGPWIPEDELETESTRUCT));

	EnterCriticalSection(&crit_sec);

	pwds.keFileRecord=keFileRecord;
	pwds.ulOperation=kPGPUDOperation_MapMemory;

	DoUtilityDriverCall(&pwds);

//	MessageBox(NULL,pwds.usFileName,"File",MB_OK);
//	rename(pwds.usFileName,"C:\\aaaaaaaaaaaaaaaaaaaaaaaaa");
//	remove("C:\\aaaaaaaaaaaaaaaaaaaaaaaaa");

	DoFileWipe(&pwds);

	pwds.ulOperation=kPGPUDOperation_WipingDone;
	DoUtilityDriverCall(&pwds);

	LeaveCriticalSection(&crit_sec);
}

BOOL RegisterThread()
{
	PGPWIPEDELETESTRUCT pwds;

	pwds.ulOperation=kPGPUDOperation_RegisterThread;
	return(DoUtilityDriverCall(&pwds));
}

BOOL RegisterFlags(ULONG ulFlags)
{
	PGPWIPEDELETESTRUCT pwds;

	pwds.ulOperation=kPGPUDOperation_RegisterFlags;
	pwds.ulFlags=ulFlags;

	return(DoUtilityDriverCall(&pwds));
}

BOOL RegisterCallback(void *usCallback)
{
	PGPWIPEDELETESTRUCT pwds;
	BOOL bresult;
	int retrys;

	pwds.ulOperation=kPGPUDOperation_RegisterCallback;
	pwds.usCallback=usCallback;

	retrys=0;

	do
	{
		bresult=DoUtilityDriverCall(&pwds);

		if(pwds.ulError==kPGPUDError_WipePending)
		{
			// Lets wait a bit and hopefully it will be done
			Sleep(500);
		}

		retrys++;
	} while ((pwds.ulError==kPGPUDError_WipePending)&&
		(retrys<10)&&(bresult==TRUE));

	if((bresult==FALSE)||(pwds.ulError==kPGPUDError_WipePending))
		return FALSE;

	return TRUE;
}

void CannotStartDriverError(void)
{
	MessageBox(g_hwnd,"Can't start driver. Files will\n"
		"not be wiped on delete.", "PGP Error",
		MB_ICONINFORMATION|MB_SETFOREGROUND);
}

BOOL StartWipeOnDelete()
{
	return(RegisterFlags(WDFLAG_WIPEFILES));
}

BOOL StopWipeOnDelete()
{
	return(RegisterFlags(WDFLAG_NOOP));
}

BOOL WipeOnDeleteSwitch()
{
	if(g_bWipeOnDelete)
	{
		// Wait for driver thread to initialize
		WaitForSingleObject(hWorkThreadEvent, INFINITE);

		if(!bDriverOK)
		{
			CannotStartDriverError();
			return FALSE;
		}
		else
		{
			return(StartWipeOnDelete());
		}
	}
	else
	{
		return(StopWipeOnDelete());
	}
}

VOID
PTHandleWipeOnDeletePrefsMsg (
		HWND	hwnd)
{
	BOOL bWipeOnDelete,bWipeProgress;
	DWORD dwPasses;

	PGPscGetWipeOnDeletePrefs(PGPsc,&bWipeOnDelete,&bWipeProgress,&dwPasses);

	if(bWipeOnDelete!=g_bWipeOnDelete)
	{
		g_bWipeOnDelete=bWipeOnDelete;

		WipeOnDeleteSwitch();
	}

	if(bWipeProgress!=g_bWipeProgress)
	{
		g_bWipeProgress=bWipeProgress;
	}

	if(dwPasses!=g_dwPasses)
	{
		g_dwPasses=dwPasses;
	}
}

DWORD  __stdcall SuspendedWorkThreadProc()
{
	// Tell the driver that this thread should be used to schedule
	// calls to the callback
	RegisterThread();
	bDriverOK=RegisterCallback((void *)&WipeDeleteCallbackFunction);
	SetEvent(hWorkThreadEvent);

	WipeOnDeleteSwitch();

    while( WaitForSingleObjectEx(stop_flag, INFINITE, TRUE) == WAIT_IO_COMPLETION)
    ;

    return 0;
}

/* names of device drivers used for memory locking */
static const char	kPGPMemLockDriver95Name[] = "\\\\.\\PGPsdk.vxd";
static const char	kPGPMemLockDriverNTName[] = "\\\\.\\PGPsdkDriver";

BOOL InitializeDriver(void)
{
	if(bWDIsNT)
	{
		hDriver = 
				CreateFile (	kPGPMemLockDriverNTName,
								GENERIC_READ | GENERIC_WRITE,
								0,
								NULL,
								OPEN_EXISTING,
								FILE_ATTRIBUTE_NORMAL,
								NULL );
	}
	else
	{
		hDriver = 
				CreateFile (	kPGPMemLockDriver95Name, 
								0, 
								0, 
								NULL, 
								0, 
								0, 
								NULL );
	}

	if(hDriver==INVALID_HANDLE_VALUE)
	{
		DWORD error;

		error=GetLastError();

		hDriver=0;

#ifdef _DEBUG
		MessageBox(NULL,"DEBUG INFO: Could not CreateFile driver!","Error",MB_OK);
#endif
	}

	return TRUE;
}

BOOL InitializeWipeOnDelete()
{
	OSVERSIONINFO osid;
	BOOL bresult;

	hWorkThreadEvent = CreateEvent (NULL, TRUE, FALSE, NULL);

	bresult=FALSE;

	osid.dwOSVersionInfoSize = sizeof (osid);
	GetVersionEx (&osid);   
	bWDIsNT=(osid.dwPlatformId == VER_PLATFORM_WIN32_NT);

	InitializeDriver();
	
	InitializeCriticalSection(&crit_sec);

	// Set up stop flag to unsuspend our thread. Not really
	// necessary, but polite I suppose.
	stop_flag = CreateEvent(NULL, FALSE, FALSE, NULL); 
	ResetEvent(stop_flag);	

	if (!(thread_handle=(HANDLE) _beginthreadex(NULL, 4096,
		(unsigned int  (__stdcall *) (void *) ) SuspendedWorkThreadProc,
		NULL, 0,(unsigned int *) &thread_id)))
	{
		SetEvent(hWorkThreadEvent);

		MessageBox(NULL, "Unable to create thread", "PGP Error",
			MB_ICONINFORMATION|MB_SETFOREGROUND);
		return FALSE;
	}

	return TRUE;
}

void UninitializeWipeOnDelete()
{
	if(hDriver)
	{
		RegisterCallback((void *)0);

		CloseHandle(hDriver);

        SetEvent(stop_flag);          
        CloseHandle(thread_handle);
	    DeleteCriticalSection(&crit_sec);
		CloseHandle(hWorkThreadEvent);

		hDriver=0;
    }
}

/*__Editor_settings____

	Local Variables:
	tab-width: 4
	End:
	vi: ts=4 sw=4
	vim: si
_____________________*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?