📄 udfsfile.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// This source code is licensed under Microsoft Shared Source License
// Version 1.0 for Windows CE.
// For a copy of the license visit http://go.microsoft.com/fwlink/?LinkId=3223.
//
//+-------------------------------------------------------------------------
//
//
// File: udfsfile.cpp
//
// Contents:
//
// Classes:
//
// Functions:
//
//--------------------------------------------------------------------------
#include "udfs.h"
//+-------------------------------------------------------------------------
//
// Member: CReadOnlyFileSystemDriver::ROFS_CreateFile
//
// Synopsis:
//
// Arguments: [hProc] --
// [pwsFileName] --
// [dwAccess] --
// [dwShareMode] --
// [pSecurityAttributes] --
// [dwCreate] --
// [dwFlagsAndAttributes] --
// [hTemplateFile] --
//
// Returns:
//
// Notes:
//
//--------------------------------------------------------------------------
HANDLE CReadOnlyFileSystemDriver::ROFS_CreateFile(
HANDLE hProc,
PCWSTR pwsFileName,
DWORD dwAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES pSecurityAttributes,
DWORD dwCreate,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile)
{
PDIRECTORY_ENTRY pDirEntry;
BOOL fRet;
PFILE_HANDLE_INFO pshr;
HANDLE hHandle = INVALID_HANDLE_VALUE;
WCHAR wChar;
int Count;
DEBUGMSG(ZONE_FUNCTION, (FS_MOUNT_POINT_NAME TEXT("UDFS!CreateFile Entered FileName=%s\r\n"), pwsFileName ? pwsFileName : L""));
if(dwAccess & ( GENERIC_WRITE | GENERIC_ALL))
{
// OK to ask for write but only in conjunction with other
SetLastError( ERROR_ACCESS_DENIED);
goto leave;
}
//
// Deny all opens for write-only access. We accept read-write
// access so old applications will continue to work. The access
// will be denied at the time of the write.
//
// If this is a create call where we will fail if the file already
// exists, or will have to alter the file's length or attributes,
// then we deny access to the file.
switch (dwCreate)
{
case CREATE_NEW:
case CREATE_ALWAYS:
case TRUNCATE_EXISTING:
SetLastError( ERROR_ACCESS_DENIED);
goto leave;
case OPEN_EXISTING:
case OPEN_ALWAYS:
case OPEN_FOR_LOADER:
break;
default:
SetLastError( ERROR_INVALID_PARAMETER);
goto leave;
}
__try {
Count = 0;
do {
wChar = pwsFileName[Count];
Count++;
} while (wChar != 0);
} __except(EXCEPTION_EXECUTE_HANDLER) {
SetLastError(ERROR_INVALID_PARAMETER);
goto leave;
}
EnterCriticalSection(&m_csListAccess);
fRet = FindFile(pwsFileName, &pDirEntry);
if (fRet == FALSE) {
goto error;
}
if (pDirEntry->fFlags & FILE_ATTRIBUTE_DIRECTORY) {
SetLastError(ERROR_ACCESS_DENIED);
goto error;
}
pshr = new FILE_HANDLE_INFO;
if (pshr == NULL) {
goto error;
}
memset(pshr, 0, sizeof(*pshr));
if ((dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED) != 0) {
//
// Asking for overlapped I/O
//
pshr->fOverlapped = TRUE;
}
//
// Add it to the handle list
//
if (m_pFileHandleList != NULL) {
m_pFileHandleList->pPrevious = pshr;
}
pshr->pNext = m_pFileHandleList;
m_pFileHandleList = pshr;
pshr->fFlags = pDirEntry->fFlags;
pshr->dwDiskLocation = pDirEntry->dwDiskLocation;
pshr->dwDiskSize = pDirEntry->dwDiskSize;
pshr->Time = pDirEntry->Time;
pshr->pVol = this;
pshr->State = StateClean;
pshr->dwByteOffset = pDirEntry->dwByteOffset;
hHandle = ::FSDMGR_CreateFileHandle(m_hVolume, hProc, (PFILE)pshr);
error:
LeaveCriticalSection(&m_csListAccess);
leave:
DEBUGMSG( ZONE_FUNCTION, (FS_MOUNT_POINT_NAME TEXT("UDFS!CreateFile Done hHandle = %x\r\n"), hHandle));
return hHandle;
}
//+-------------------------------------------------------------------------
//
// Member: CReadOnlyFileSystemDriver::ROFS_CloseFileHandle
//
// Synopsis:
//
// Arguments: [pfh] --
//
// Returns:
//
// Notes:
//
//--------------------------------------------------------------------------
BOOL CReadOnlyFileSystemDriver::ROFS_CloseFileHandle( PFILE_HANDLE_INFO pfh)
{
DEBUGMSG(ZONE_FUNCTION, (FS_MOUNT_POINT_NAME TEXT("UDFS!CloseFileHandle Entered Handle=%08X\r\n"), pfh));
EnterCriticalSection(&m_csListAccess);
if (pfh->pNext != NULL) {
pfh->pNext->pPrevious = pfh->pPrevious;
}
if (pfh->pPrevious == NULL) {
m_pFileHandleList = pfh->pNext;
} else {
pfh->pPrevious->pNext = pfh->pNext;
}
delete pfh;
LeaveCriticalSection(&m_csListAccess);
DEBUGMSG(ZONE_FUNCTION, (FS_MOUNT_POINT_NAME TEXT("UDFS!CloseFileHandle Done\r\n")));
return TRUE;
}
//+-------------------------------------------------------------------------
//
// Member: CReadOnlyFileSystemDriver::ROFS_GetFileAttributes
//
// Synopsis:
//
// Arguments: [pwsFileName] --
//
// Returns:
//
// Notes:
//
//--------------------------------------------------------------------------
DWORD CReadOnlyFileSystemDriver::ROFS_GetFileAttributes( PCWSTR pwsFileName)
{
BOOL fRet;
PDIRECTORY_ENTRY pDirEntry;
DWORD dwAttr;
DEBUGMSG(ZONE_FUNCTION, (FS_MOUNT_POINT_NAME TEXT("UDFS!GetFileAttributes Entered File=%s\r\n"), pwsFileName ? pwsFileName : L""));
fRet = FindFile(pwsFileName, &pDirEntry);
DEBUGMSG(ZONE_FUNCTION, (FS_MOUNT_POINT_NAME TEXT("UDFS!GetFileAttributes Done\r\n")));
if (fRet == FALSE) {
return 0xFFFFFFFF;
}
dwAttr = FILE_ATTRIBUTE_READONLY;
if (pDirEntry->fFlags & FILE_ATTRIBUTE_DIRECTORY) {
dwAttr |= FILE_ATTRIBUTE_DIRECTORY;
}
if (pDirEntry->fFlags & FILE_ATTRIBUTE_HIDDEN) {
dwAttr |= FILE_ATTRIBUTE_HIDDEN;
}
return dwAttr;
}
//+-------------------------------------------------------------------------
//
// Member: CReadOnlyFileSystemDriver::ROFS_ReadFile
//
// Synopsis:
//
// Arguments: [pfh] --
// [buffer] --
// [nBytesToRead] --
// [pNumBytesRead] --
// [pOverlapped] --
//
// Returns:
//
// Notes:
//
//--------------------------------------------------------------------------
BOOL CReadOnlyFileSystemDriver::ROFS_ReadFile( PFILE_HANDLE_INFO pfh, LPVOID buffer, DWORD nBytesToRead, DWORD * pNumBytesRead, OVERLAPPED * pOverlapped)
{
DWORD dwCurrentBytePosition = (DWORD)-1;
DWORD dwSector;
DWORD dwStartPosition;
DWORD dwBytesToRead;
BOOL fRet;
DWORD dwError;
LPDWORD pNumBytesRead2 = NULL;
DEBUGMSG(ZONE_FUNCTION, (FS_MOUNT_POINT_NAME TEXT("UDFS!ReadFile Entered Handle=%08X\r\n"), pfh));
dwError = ERROR_SUCCESS;
__try {
((PBYTE)buffer)[0] = 0;
((PBYTE)buffer)[nBytesToRead - 1] = 0;
if (pfh->fOverlapped == TRUE) {
if (pOverlapped != NULL) {
if (pOverlapped->OffsetHigh != 0)
{
dwError = ERROR_INVALID_PARAMETER;
}
dwCurrentBytePosition = pOverlapped->Offset;
pNumBytesRead2 = pNumBytesRead;
pNumBytesRead = &pOverlapped->InternalHigh;
} else {
dwError = ERROR_INVALID_PARAMETER;
}
} else {
if (pOverlapped) {
dwCurrentBytePosition = pOverlapped->Offset;
pOverlapped = NULL;
} else {
if (pNumBytesRead == NULL) {
dwError = ERROR_INVALID_PARAMETER;
}
}
}
if (pNumBytesRead) {
*pNumBytesRead = 0;
}
if (pNumBytesRead2) {
*pNumBytesRead2 = 0;
}
} __except(EXCEPTION_EXECUTE_HANDLER) {
dwError = ERROR_INVALID_PARAMETER;
}
if (dwError != ERROR_SUCCESS) {
SetLastError(dwError);
return FALSE;
}
if (dwCurrentBytePosition == (DWORD)-1) {
do {
dwCurrentBytePosition = pfh->dwFilePointer;
if (dwCurrentBytePosition == pfh->dwDiskSize) {
SetLastError(ERROR_HANDLE_EOF);
return TRUE;
}
DEBUGCHK(dwCurrentBytePosition < pfh->dwDiskSize);
if (dwCurrentBytePosition + nBytesToRead > pfh->dwDiskSize) {
dwBytesToRead = pfh->dwDiskSize - dwCurrentBytePosition;
} else {
dwBytesToRead = nBytesToRead;
}
if (dwBytesToRead > (65532*CD_SECTOR_SIZE)) {
dwBytesToRead = 65532*CD_SECTOR_SIZE;
}
} while ((DWORD)InterlockedTestExchange( (PLONG)&(pfh->dwFilePointer), dwCurrentBytePosition, (dwCurrentBytePosition + dwBytesToRead)) != dwCurrentBytePosition);
} else {
do {
if (dwCurrentBytePosition >= pfh->dwDiskSize) {
SetLastError(ERROR_HANDLE_EOF);
return TRUE;
}
DEBUGCHK(dwCurrentBytePosition < pfh->dwDiskSize);
if (dwCurrentBytePosition + nBytesToRead > pfh->dwDiskSize) {
dwBytesToRead = pfh->dwDiskSize - dwCurrentBytePosition;
} else {
dwBytesToRead = nBytesToRead;
}
if (dwBytesToRead > (65532*CD_SECTOR_SIZE)) {
dwBytesToRead = 65532*CD_SECTOR_SIZE;
}
} while ((DWORD)InterlockedTestExchange( (PLONG)&(pfh->dwFilePointer), dwCurrentBytePosition, (dwCurrentBytePosition + dwBytesToRead))
!= dwCurrentBytePosition);
}
nBytesToRead = dwBytesToRead;
//
// Find out where to read
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -