2 * Hyper Operating System Application Framework
5 * @brief %jp{FATボリューム用デバイスドライバ}
7 * Copyright (C) 2006-2007 by Project HOS
8 * http://sourceforge.jp/projects/hos/
13 #include "fatvol_local.h"
16 HANDLE FatVol_Open(C_DRVOBJ *pDrvObj, const char *pszPath, int iMode)
19 FATVOL_UINT uiCluster;
21 char szEntryName[8+1+3+1];
27 T_FATVOL_CLUSTERBUF *pClusterBuf;
28 unsigned char *pubBuf;
29 FATVOL_UINT uiDirCluster;
30 FATVOL_UINT uiDirEntryPos;
35 self = (C_FATVOL *)pDrvObj;
39 SysMtx_Lock(self->hMtx);
42 uiDirCluster = self->RootDirCluster;
44 if ( (pClusterBuf = FatVol_GetClusterBuf(self, uiDirCluster, 1)) == NULL )
46 SysMtx_Unlock(self->hMtx);
54 for ( iNameLen = 0; iNameLen < sizeof(szName) && *pszPath != '\0'; pszPath++, iNameLen++ )
57 if ( *pszPath == '/' )
62 szName[iNameLen] = *pszPath;
65 if ( szName[iNameLen] >= 'a' && szName[iNameLen] <= 'z' )
67 szName[iNameLen] -= ('a' - 'A');
70 szName[iNameLen] = '\0';
77 FatVol_RelClusterBuf(self, pClusterBuf, 0);
80 if ( iMode & FILE_OPEN_DIR )
82 if ( (hFile = FatVol_FileCreate(self, uiDirCluster, FATVOL_CLUSTER_ENDMARKER, 0, 0, iMode)) != HANDLE_NULL )
85 SysMtx_Unlock(self->hMtx);
91 SysMtx_Unlock(self->hMtx);
101 pubBuf = &pClusterBuf->pubBuf[uiDirEntryPos];
104 if ( pubBuf[0] == 0x00 )
111 for ( i = 0; i < 8; i++ )
113 if ( pubBuf[0+i] == ' ' || pubBuf[0+i] == '\0' )
117 szEntryName[iEntryNameLen++] = (char)pubBuf[0+i];
119 szEntryName[iEntryNameLen++] = '.';
120 for ( i = 0; i < 3; i++ )
122 if ( pubBuf[8+i] == ' ' || pubBuf[8+i] == '\0' )
126 szEntryName[iEntryNameLen++] = (char)pubBuf[8+i];
128 szEntryName[iEntryNameLen] = '\0';
129 if ( iEntryNameLen > 2 && szEntryName[iEntryNameLen-1] == '.' )
131 szEntryName[--iEntryNameLen] = '\0';
135 if ( strcmp(szEntryName, szName) == 0 )
143 if ( uiDirEntryPos >= self->BytesPerCluster )
145 FatVol_RelClusterBuf(self, pClusterBuf, 0);
146 uiDirCluster = FatVol_GetNextCluster(self, uiDirCluster);
147 if ( uiDirCluster == FATVOL_CLUSTER_ENDMARKER
148 || (pClusterBuf = FatVol_GetClusterBuf(self, uiDirCluster, 1)) == NULL )
150 SysMtx_Unlock(self->hMtx);
160 FatVol_RelClusterBuf(self, pClusterBuf, 0);
161 SysMtx_Unlock(self->hMtx);
166 uiCluster = pubBuf[26] + (pubBuf[27] << 8);
167 if ( self->iFatType == FATVOL_TYPE_FAT32 )
169 uiCluster += (pubBuf[20] << 16) + (pubBuf[21] << 24);
176 FileSize = pubBuf[28] + (pubBuf[29] << 8) + (pubBuf[30] << 16) + (pubBuf[31] << 24);
179 FatVol_RelClusterBuf(self, pClusterBuf, 0);
182 /* パス部分の解析が終わっていればここで抜ける */
183 if ( *pszPath == '\0' )
188 /* サブディレクトリでなければエラー */
189 if ( !(ubAttr & 0x10) )
191 SysMtx_Unlock(self->hMtx);
196 uiDirCluster = uiCluster;
198 if ( (pClusterBuf = FatVol_GetClusterBuf(self, uiDirCluster, 1)) == NULL )
200 SysMtx_Unlock(self->hMtx);
206 if ( iMode & FILE_OPEN_DIR )
208 SysMtx_Unlock(self->hMtx);
213 hFile = FatVol_FileCreate(self, uiCluster, uiDirCluster, uiDirEntryPos, FileSize, iMode);
214 if ( hFile != HANDLE_NULL )
220 SysMtx_Unlock(self->hMtx);