📄 file.c
字号:
#ifdef TFAT
if(pstm->s_pvol->v_fTfat && pstm->s_pstmParent)
EnterCriticalSection(&pstm->s_pstmParent->s_cs);
#endif
EnterCriticalSection(&pstm->s_cs);
__try {
*lpNumBytesRead = 0;
if (!(pfh->fh_mode & FH_MODE_READ) || (pfh->fh_flags & (FHF_VOLUME | FHF_UNMOUNTED)))
dwError = ERROR_ACCESS_DENIED;
else {
if (!lpdwLowOffset) {
// authorize access
if (FSDMGR_TestFileLockEx(AcquireFileLockState, ReleaseFileLockState, (DWORD)pfh, TRUE, nBytesToRead, pfh->fh_pos, 0)) {
dwError = ReadStreamData(pstm, pfh->fh_pos, buffer, nBytesToRead, lpNumBytesRead);
pfh->fh_pos += *lpNumBytesRead;
}
else {
dwError = ERROR_ACCESS_DENIED;
}
}
else {
// authorize access
if (FSDMGR_TestFileLockEx(AcquireFileLockState, ReleaseFileLockState, (DWORD)pfh, TRUE, nBytesToRead, *lpdwLowOffset, 0)) {
dwError = ReadStreamData(pstm, *lpdwLowOffset, buffer, nBytesToRead, lpNumBytesRead);
}
else {
dwError = ERROR_ACCESS_DENIED;
}
}
}
} __except (EXCEPTION_EXECUTE_HANDLER) {
dwError = ERROR_INVALID_PARAMETER;
}
LeaveCriticalSection(&pstm->s_cs);
#ifdef TFAT
if(pstm->s_pvol->v_fTfat && pstm->s_pstmParent)
LeaveCriticalSection(&pstm->s_pstmParent->s_cs);
#endif
return dwError;
}
DWORD FATFSWriteFile(
PFHANDLE pfh,
LPCVOID buffer,
DWORD nBytesToWrite,
LPDWORD lpNumBytesWritten,
LPOVERLAPPED lpOverlapped,
LPDWORD lpdwLowOffset,
LPDWORD lpdwHighOffset)
{
PDSTREAM pstm;
DWORD dwError = ERROR_GEN_FAILURE;
pstm = pfh->fh_pstm;
ASSERT(pstm);
#ifdef TFAT
if(pstm->s_pstmParent)
EnterCriticalSection(&pstm->s_pstmParent->s_cs);
#endif
EnterCriticalSection(&pstm->s_cs);
__try {
*lpNumBytesWritten = 0;
if (!(pfh->fh_mode & FH_MODE_WRITE) || (pfh->fh_flags & (FHF_VOLUME | FHF_UNMOUNTED)))
dwError = ERROR_ACCESS_DENIED;
else {
if (!lpdwLowOffset) {
if (FSDMGR_TestFileLockEx(AcquireFileLockState, ReleaseFileLockState, (DWORD)pfh, FALSE, nBytesToWrite, pfh->fh_pos, 0)) {
dwError = WriteStreamData(pstm, pfh->fh_pos, buffer, nBytesToWrite, lpNumBytesWritten, TRUE);
pfh->fh_pos += *lpNumBytesWritten;
}
else {
dwError = ERROR_ACCESS_DENIED;
}
}
else {
if (FSDMGR_TestFileLockEx(AcquireFileLockState, ReleaseFileLockState, (DWORD)pfh, TRUE, nBytesToWrite, *lpdwLowOffset, 0)) {
dwError = WriteStreamData(pstm, *lpdwLowOffset, buffer, nBytesToWrite, lpNumBytesWritten, TRUE);
}
else {
dwError = ERROR_ACCESS_DENIED;
}
}
// TEST_BREAK
PWR_BREAK_NOTIFY(11);
}
} __except (EXCEPTION_EXECUTE_HANDLER) {
dwError = ERROR_INVALID_PARAMETER;
}
LeaveCriticalSection(&pstm->s_cs);
#ifdef TFAT
if(pstm->s_pstmParent)
LeaveCriticalSection(&pstm->s_pstmParent->s_cs);
#endif
return dwError;
}
#if NUM_FILE_APIS == 13
BOOL FAT_ReadFilePagein(
PFHANDLE pfh,
LPVOID buffer,
DWORD nBytesToRead,
LPDWORD lpNumBytesRead,
LPOVERLAPPED lpOverlapped)
{
#ifdef DEMAND_PAGING
PDSTREAM pstm;
DWORD dwError;
pstm = pfh->fh_pstm;
ASSERT(pstm);
if (buffer == NULL && nBytesToRead == 0) {
if (pstm->s_pvol->v_pdsk->d_diActive.di_flags & DISK_INFO_FLAG_PAGEABLE) {
pstm->s_flags |= STF_DEMANDPAGED;
return TRUE; // yes, Pagein requests are supported
}
DEBUGMSG(TRUE,(DBGTEXT("FATFS!FAT_ReadFilePagein: driver declines to demand-page '%.11hs'\r\n"), pstm->s_achOEM));
SetLastError(ERROR_NOT_SUPPORTED);
return FALSE; // but this little piggy cried all the way home
}
DEBUGMSG(ZONE_APIS,(DBGTEXT("FATFS!FAT_ReadFilePagein(0x%x,0x%x bytes,position 0x%x)\r\n"), pfh, nBytesToRead, pfh->fh_pos));
ASSERT(pstm->s_flags & STF_DEMANDPAGED);
if (dwError = FATFSReadFile(pfh, buffer, nBytesToRead, lpNumBytesRead, lpOverlapped, NULL, NULL))
SetLastError(dwError);
DEBUGMSG(ZONE_APIS || ZONE_ERRORS && dwError,
(DBGTEXT("FATFS!FAT_ReadFilePagein(0x%x) returned 0x%x (%d, 0x%x bytes, pos 0x%x, size 0x%x)\r\n"), pfh, dwError == ERROR_SUCCESS, dwError, *lpNumBytesRead, pfh->fh_pos, pstm->s_size));
return dwError == ERROR_SUCCESS;
#else
DEBUGMSG(TRUE,(DBGTEXT("FATFS!FAT_ReadFilePagein(0x%x,0x%x bytes,position 0x%x) is hard-coded to FAIL!\r\n"), pfh, nBytesToRead, pfh->fh_pos));
SetLastError(ERROR_NOT_SUPPORTED);
return FALSE;
#endif
}
#endif
BOOL FAT_ReadFile(
PFHANDLE pfh,
LPVOID buffer,
DWORD nBytesToRead,
LPDWORD lpNumBytesRead,
LPOVERLAPPED lpOverlapped)
{
PDSTREAM pstm;
DWORD dwError;
pstm = pfh->fh_pstm;
ASSERT(pstm);
DEBUGMSG(ZONE_APIS,(DBGTEXT("FATFS!FAT_ReadFile(0x%x,0x%x bytes,position 0x%x)\r\n"), pfh, nBytesToRead, pfh->fh_pos));
if (!FATEnter(NULL, LOGID_NONE)) {
DEBUGMSG(ZONE_APIS || ZONE_ERRORS,(DBGTEXT("FATFS!FAT_ReadFile bailing...\r\n")));
return FALSE;
}
if (buffer)
LockPages(buffer,nBytesToRead,0,LOCKFLAG_WRITE);
if (dwError = FATFSReadFile(pfh, buffer, nBytesToRead, lpNumBytesRead, lpOverlapped, NULL, NULL))
SetLastError(dwError);
if (buffer)
UnlockPages(buffer,nBytesToRead);
FATExit(pstm->s_pvol, LOGID_NONE);
DEBUGMSG(ZONE_APIS || ZONE_ERRORS && dwError,
(DBGTEXT("FATFS!FAT_ReadFile(0x%x) returned 0x%x (%d, 0x%x bytes, pos 0x%x, size 0x%x)\r\n"), pfh, dwError == ERROR_SUCCESS, dwError, *lpNumBytesRead, pfh->fh_pos, pstm->s_size));
return dwError == ERROR_SUCCESS;
}
#if NUM_FILE_APIS > 13
BOOL FAT_ReadFileWithSeek(
PFHANDLE pfh,
LPVOID buffer,
DWORD nBytesToRead,
LPDWORD lpNumBytesRead,
LPOVERLAPPED lpOverlapped,
DWORD dwLowOffset,
DWORD dwHighOffset)
{
#ifdef DEMAND_PAGING
PDSTREAM pstm;
DWORD dwError=ERROR_SUCCESS;
pstm = pfh->fh_pstm;
ASSERT(pstm);
DEBUGMSG(ZONE_APIS,(DBGTEXT("FATFS!FAT_ReadFileWithSeek(0x%x,0x%x bytes,position 0x%x)\r\n"), pfh, nBytesToRead, dwLowOffset));
if (buffer == NULL && nBytesToRead == 0) {
if (pstm->s_pvol->v_pdsk->d_diActive.di_flags & DISK_INFO_FLAG_PAGEABLE) {
pstm->s_flags |= STF_DEMANDPAGED;
return TRUE; // yes, Pagein requests are supported
}
DEBUGMSG(TRUE,(DBGTEXT("FATFS!FAT_ReadFileWithSeek: driver declines to demand-page '%.11hs'\r\n"), pstm->s_achOEM));
SetLastError(ERROR_NOT_SUPPORTED);
return FALSE; // but this little piggy cried all the way home
}
if (buffer)
LockPages(buffer,nBytesToRead,0,LOCKFLAG_WRITE);
if (!FATEnter(NULL, LOGID_NONE))
return FALSE;
dwError = FATFSReadFile(pfh, buffer, nBytesToRead, lpNumBytesRead, lpOverlapped, &dwLowOffset, &dwHighOffset);
FATExit(pstm->s_pvol, LOGID_NONE);
if (dwError) {
SetLastError(dwError);
}
if (buffer)
UnlockPages(buffer,nBytesToRead);
DEBUGMSG(ZONE_APIS || ZONE_ERRORS && dwError,
(DBGTEXT("FATFS!FAT_ReadFileWithSeek(0x%x) returned 0x%x (%d, 0x%x bytes, size 0x%x)\r\n"), pfh, dwError == ERROR_SUCCESS, dwError, *lpNumBytesRead, pstm->s_size));
return dwError == ERROR_SUCCESS;
#else
DEBUGMSG(TRUE,(DBGTEXT("FATFS!FAT_ReadFileWithSeek(0x%x,0x%x bytes,position 0x%x) is hard-coded to FAIL!\r\n"), pfh, nBytesToRead, pfh->fh_pos));
SetLastError(ERROR_NOT_SUPPORTED);
return FALSE;
#endif
}
BOOL FAT_ReadFileScatter(
PFHANDLE pfh,
FILE_SEGMENT_ELEMENT aSegmentArray[],
DWORD nNumberOfBytesToRead,
LPDWORD lpReserved,
LPOVERLAPPED lpOverlapped)
{
PDSTREAM pstm;
DWORD dwError = ERROR_SUCCESS;
SYSTEM_INFO SystemInfo;
DWORD dwNumPages;
DWORD iPage;
// Interpret lpReserved as an array of 64-bit offsets if provided.
FILE_SEGMENT_ELEMENT* aOffsetArray = (FILE_SEGMENT_ELEMENT*)lpReserved;
pstm = pfh->fh_pstm;
ASSERT(pstm);
DEBUGMSG(ZONE_APIS,(DBGTEXT("FATFS!FAT_ReadFileScatter(0x%x,0x%x bytes)\r\n"), pfh, nNumberOfBytesToRead));
GetSystemInfo (&SystemInfo);
if (!nNumberOfBytesToRead || nNumberOfBytesToRead % SystemInfo.dwPageSize) {
SetLastError (ERROR_INVALID_PARAMETER);
return FALSE;
}
dwNumPages = nNumberOfBytesToRead / SystemInfo.dwPageSize;
if (!FATEnter(NULL, LOGID_NONE)) {
DEBUGMSG(ZONE_APIS || ZONE_ERRORS,(DBGTEXT("FATFS!FAT_ReadFile bailing...\r\n")));
return FALSE;
}
#ifdef TFAT
if(pstm->s_pvol->v_fTfat && pstm->s_pstmParent)
EnterCriticalSection(&pstm->s_pstmParent->s_cs);
#endif
EnterCriticalSection(&pstm->s_cs);
__try {
if (!(pfh->fh_mode & FH_MODE_READ) || (pfh->fh_flags & (FHF_VOLUME | FHF_UNMOUNTED)))
dwError = ERROR_ACCESS_DENIED;
else {
for (iPage = 0; iPage < dwNumPages; iPage++) {
DWORD pos = aOffsetArray ? (DWORD)aOffsetArray[iPage].Alignment : pfh->fh_pos;
dwError = ReadStreamData(pstm, pos, aSegmentArray[iPage].Buffer, SystemInfo.dwPageSize, NULL);
if (dwError)
break;
if (!aOffsetArray)
pfh->fh_pos += SystemInfo.dwPageSize;
}
}
} __except (EXCEPTION_EXECUTE_HANDLER) {
dwError = ERROR_INVALID_PARAMETER;
}
LeaveCriticalSection(&pstm->s_cs);
#ifdef TFAT
if(pstm->s_pvol->v_fTfat && pstm->s_pstmParent)
LeaveCriticalSection(&pstm->s_pstmParent->s_cs);
#endif
FATExit(pstm->s_pvol, LOGID_NONE);
if (dwError)
SetLastError (dwError);
return dwError == ERROR_SUCCESS;
}
#endif
BOOL FAT_WriteFile(
PFHANDLE pfh,
LPCVOID buffer,
DWORD nBytesToWrite,
LPDWORD lpNumBytesWritten,
LPOVERLAPPED lpOverlapped)
{
PDSTREAM pstm;
DWORD dwError;
pstm = pfh->fh_pstm;
ASSERT(pstm);
DEBUGMSG(ZONE_APIS,(DBGTEXT("FATFS!FAT_WriteFile(0x%x,0x%x bytes,position 0x%x)\r\n"), pfh, nBytesToWrite, pfh->fh_pos));
if (!FATEnter(pfh->fh_pstm->s_pvol, LOGID_WRITEFILE))
return FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -