OSDN Git Service

55928abb61da0ca23f6f4ae56aa8467f3c642f34
[hos/hos-v4a.git] / aplfw / driver / volume / fat / fatvol_getclusterbuf.c
1 /** 
2  *  Hyper Operating System  Application Framework
3  *
4  * @file  fatvol.c
5  * @brief %jp{FATボリューム用デバイスドライバ}
6  *
7  * Copyright (C) 2006-2007 by Project HOS
8  * http://sourceforge.jp/projects/hos/
9  */
10
11
12 #include <string.h>
13 #include "fatvol_local.h"
14
15
16 static int FatVol_AlloctClusterBuf(C_FATVOL *self, FATVOL_UINT uiCluster, int iRead);
17
18
19 T_FATVOL_CLUSTERBUF *FatVol_GetClusterBuf(C_FATVOL *self, FATVOL_UINT uiCluster, int iRead)
20 {
21         int iIndex;
22         
23         /* バッファを割り当てる */
24         iIndex = FatVol_AlloctClusterBuf(self, uiCluster, iRead);
25         if ( iIndex < 0 )
26         {
27                 return NULL;
28         }
29         
30         /* 並び替える */
31         if ( iIndex != 0 )
32         {
33                 T_FATVOL_CLUSTERBUF Tmp;
34                 Tmp = self->pClusterBuf[iIndex];
35                 memmove(&self->pClusterBuf[1], &self->pClusterBuf[0], iIndex * sizeof(T_FATVOL_CLUSTERBUF));
36                 self->pClusterBuf[0] = Tmp;
37         }
38
39         return  &self->pClusterBuf[0];
40 }
41
42
43 int FatVol_AlloctClusterBuf(C_FATVOL *self, FATVOL_UINT uiCluster, int iRead)
44 {
45         int iIndex;
46         
47         /* 既にバッファに居ないか探索 */
48         for ( iIndex = 0; iIndex < self->iClusterBufNum; iIndex++ )
49         {
50                 /* 見つかれば既存バッファを返す */
51                 if ( self->pClusterBuf[iIndex].uiClusterNum == uiCluster )
52                 {
53                         return iIndex;
54                 }
55         }
56         
57         
58         /* 空のバッファを探す */
59         for ( iIndex = 0; iIndex < self->iClusterBufNum; iIndex++ )
60         {
61                 if ( self->pClusterBuf[iIndex].uiClusterNum == FATVOL_CLUSTER_ENDMARKER )
62                 {
63                         break;
64                 }
65         }
66         
67         /* 空きが無ければ空ける */
68         if ( iIndex >= self->iClusterBufNum )
69         {
70                 /* 末尾を空ける */
71                 iIndex = self->iClusterBufNum - 1;
72
73                 /* dirtyなら吐き出す */
74                 if ( self->pClusterBuf[iIndex].iDirty )
75                 {
76                         if ( FatVol_ClusterWrite(self, self->pClusterBuf[iIndex].uiClusterNum, self->pClusterBuf[iIndex].pubBuf) != FATVOL_ERR_OK )
77                         {
78                                 return -1;
79                         }
80                         self->pClusterBuf[iIndex].iDirty = 0;
81                 }
82                 self->pClusterBuf[iIndex].uiClusterNum = FATVOL_CLUSTER_ENDMARKER;
83         }
84         
85         /* バッファに読み込み */
86         if ( iRead )
87         {
88                 if ( FatVol_ClusterRead(self, uiCluster, self->pClusterBuf[iIndex].pubBuf) != FATVOL_ERR_OK )
89                 {
90                         return -1;
91                 }
92         }
93         self->pClusterBuf[iIndex].iDirty       = 0;
94         self->pClusterBuf[iIndex].uiClusterNum = uiCluster;
95         
96         return iIndex;  
97 }
98
99
100 /* end of file */