/*
	WinSTon

	Create Disc Image functions(only works on NT - Damn)
*/

#include "..\includes\winston.h"
//#include <winioctl.h>
#include "..\includes\debug.h"

//-----------------------------------------------------------------------
/*
	Lock disc volume
*/
BOOL CreateDiscImage_LockVolume(HANDLE hDisk)
{
	DWORD ReturnedByteCount;

	return(DeviceIoControl(hDisk,FSCTL_LOCK_VOLUME,NULL,0,NULL,0,&ReturnedByteCount,NULL));
}

//-----------------------------------------------------------------------
/*
	UnLock disc volume
*/
BOOL CreateDiscImage_UnLockVolume(HANDLE hDisk)
{
	DWORD ReturnedByteCount;

	return(DeviceIoControl(hDisk,FSCTL_UNLOCK_VOLUME,NULL,0,NULL,0,&ReturnedByteCount,NULL));
}

//-----------------------------------------------------------------------
/*
	Read PC floppy disc into image file
*/
void CreateDiscImage_ReadImage(char *pszDriveName)
{
    char szDrive[MAX_PATH];
	HANDLE hDrive;

	// Build PC floppy image name
	sprintf(szDrive,"\\\\.\\%s",pszDriveName);
	hDrive = CreateFile(szDrive,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
	if (hDrive!=INVALID_HANDLE_VALUE) {
        if (CreateDiscImage_LockVolume(hDrive)) {
			Debug_File("all ok\n");
			CreateDiscImage_UnLockVolume(hDrive);
		}
		else {
            Debug_File("MFMT:Locking volume %s %s failed %d\n", szDrive,pszDriveName, GetLastError());
		}
	}
	else {
		Debug_File("MFMT: Open %s %s failed %d\n", szDrive,pszDriveName, GetLastError());
	}
}
