mndisk.c

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

C
285
字号
/*__________________________________________________________________________
 Copyright (C) 2002 PGP Corporation
 All rights reserved.
 
 $Id: MNdisk.c,v 1.9 2002/11/22 02:32:15 wjb Exp $
__________________________________________________________________________*/

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <shellapi.h>
#include <stdio.h>

#define PGPDISK_DYNLINK

#include "pgpPflPriv.h"
#include "pgpClientErrors.h"

#include "PGPdiskEngine.h"
#include "PGPdiskUI.h"

#include "MNdisk.h"

//	_________________
//
//	PGPdisk constants

static const char	*kPGPdiskAppName	= "PGPdisk.exe";

static const char	*kPGPdiskNotAvailText	= 
	"Your PGPdisk installation is not valid. Please re-install PGP and try "
	"again.";

//	________________________
//
//	PGPdisk static variables

static PGPBoolean	PGPdiskInitialized=FALSE;
static PGPBoolean	PGPdiskEngineInitialized=FALSE;
static PGPBoolean	PGPdiskUIInitialized=FALSE;

static PGPdiskContextRef	PGPdiskContext	= kInvalidPGPdiskContextRef;

//	_____________________
//
//	Function declarations

VOID
sPGPdiskShutdown();

//	_______________
//
//	Report an error

static 
VOID 
sReportError(const char *formatStr, PGPError error, HWND parent)
{
	pgpAssertStrValid(formatStr);

	if ((error != kPGPError_NoErr) && (error != kPGPError_UserAbort))
	{
		static char	pgpStrBuf[512];
		static char	errStrBuf[1024];

		PGPGetClientErrorString(error, 512, pgpStrBuf);
		sprintf(errStrBuf, formatStr, pgpStrBuf);

		MessageBox(parent, errStrBuf, "PGP Shell Extension", 
			MB_ICONERROR | MB_OK);
	}
}

//	______________________________
//
//	Perform PGPdisk initialization

static 
BOOL 
sPGPdiskStartup()
{
	PGPError	error	= kPGPError_NoErr;

	if (PGPdiskInitialized)
		return TRUE;

	error = PGPdiskEngineInitLibrary(0);

	if (IsntPGPError(error))
	{
		PGPdiskEngineInitialized = TRUE;
		error = PGPdiskUIInitLibrary();
	}

	if (IsntPGPError(error))
	{
		PGPdiskUIInitialized = TRUE;
		error = PGPdiskNewContext(kPGPdiskAPIVersion, &PGPdiskContext);
	}

	if (IsntPGPError(error))
	{
		PGPdiskInitialized = TRUE;
		return TRUE;
	}
	else
	{
		sPGPdiskShutdown();
		return FALSE;
	}
}

//	________________________
//
//	Perform PGPdisk shutdown

static 
VOID
sPGPdiskShutdown()
{
	if (PGPdiskContextRefIsValid(PGPdiskContext))
	{
		PGPdiskFreeContext(PGPdiskContext);
		PGPdiskContext = kInvalidPGPdiskContextRef;
	}

	if (PGPdiskUIInitialized)
	{
		PGPdiskUICleanupLibrary();
		PGPdiskUIInitialized = FALSE;
	}

	if (PGPdiskEngineInitialized)
	{
		PGPdiskEngineCleanupLibrary();
		PGPdiskEngineInitialized = FALSE;
	}

	PGPdiskInitialized = FALSE;
}

//	__________________________
//
//	Release PGPdisk resources

VOID 
MNdiskCleanup()
{
	sPGPdiskShutdown();
}

//	___________________________________
//
//	Determine if the path is a PGPdisk.

VOID
MNdiskIsPathPGPdisk(const char *path, BOOL *pIsPGPdisk, BOOL *pIsMounted)
{
	PGPUInt32	fileAttribs;

	pgpAssertStrValid(path);
	pgpAssertAddrValid(pIsPGPdisk, BOOL);
	pgpAssertAddrValid(pIsMounted, BOOL);

	*pIsPGPdisk = FALSE;
	*pIsMounted = FALSE;

	fileAttribs = GetFileAttributes(path);

	if ((strlen(path) <= 3) || (fileAttribs & FILE_ATTRIBUTE_DIRECTORY))
	{
		// we have a root here
		if (!sPGPdiskStartup())
			return;

		*pIsPGPdisk = *pIsMounted = PGPdiskIsRootMountedPGPdisk(path);
	}
	else if (_stricmp(path + strlen(path) - 4, kPGPdiskFileExtension) == 0)
	{
		// we have a PGPdisk file here
		if (!sPGPdiskStartup())
			return;

		*pIsPGPdisk = TRUE;
		*pIsMounted = PGPdiskIsPathMountedPGPdisk(path);
	}
}

//	________________________
//
//	Invoke app command line.

static 
VOID 
sInvokePGPdiskApp(const char *command, const char *path, HWND parent)
{
	static char	cmdLine[kPGPdiskMaxPathLength * 2];
	PGPUInt32	result;

	pgpAssertStrValid(command);

	strcpy(cmdLine, command);
	strcat(cmdLine, " ");

	if (path != NULL)
	{
		strcat(cmdLine, "\"");
		strcat(cmdLine, path);
		strcat(cmdLine, "\"");
	}

	result	= (PGPUInt32) ShellExecute(NULL, "open", kPGPdiskAppName, 
		cmdLine, NULL, SW_SHOWNORMAL);

	if (result < 32)
	{
		MessageBox(parent, kPGPdiskNotAvailText, "PGP Shell Extension", 
			MB_ICONERROR | MB_OK);
	}
}

//	________________________
//
//	Edit a PGPdisk
 
VOID 
MNdiskEditPGPdisk(const char *path, HWND parent)
{
	pgpAssertStrValid(path);

	if (!sPGPdiskStartup())
		return;

	// go to app for this one, should be in separate process
	sInvokePGPdiskApp("edit", path, parent);
}

//	________________________
//
//	Mount a PGPdisk
 
VOID 
MNdiskMountPGPdisk(const char *path, HWND parent)
{
	PGPError	error	= kPGPError_NoErr;
	pgpAssertStrValid(path);

	if (!sPGPdiskStartup())
		return;

	// In case anything has changed, dirty the keydb
	PGPdiskDirtyPGPKeyDB(PGPdiskContext);

	error = PGPdiskAskMountDisk(PGPdiskContext, path, parent);

	if (IsPGPError(error))
	{
		sReportError("PGPdisk Mount failed because: \"%s.\"", error, 
			parent);
	}
}

//	________________________
//
//	Unmount a PGPdisk

VOID 
MNdiskUnmountPGPdisk(const char *pathOrRoot, HWND parent)
{
	PGPError	error	= kPGPError_NoErr;
	pgpAssertStrValid(pathOrRoot);

	if (!sPGPdiskStartup())
		return;

	error = PGPdiskAskUnmountDisk(PGPdiskContext, pathOrRoot, parent, 
		kPGPdiskNoUnmountFlags);

	if (IsPGPError(error))
	{
		sReportError("PGPdisk Unmount failed because: \"%s.\"", error, 
			parent);
	}
}

⌨️ 快捷键说明

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