-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathFileTable.cpp
254 lines (221 loc) · 5.46 KB
/
FileTable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "FileTable.h"
#include <string.h>
#include <io.h>
#define FHSIZE 32
#define NFS3_FHSIZE 64
static CFileTable g_FileTable;
CFileTable::CFileTable()
{
m_pLastTable = m_pFirstTable = new FILE_TABLE;
memset(m_pFirstTable, 0, sizeof(FILE_TABLE));
m_nTableSize = 0;
m_pCacheList = NULL;
}
CFileTable::~CFileTable()
{
FILE_TABLE *pTable, *pTemp;
unsigned int i;
CACHE_LIST *pPrev;
pTable = m_pFirstTable;
while (pTable != NULL) //free file table
{
for (i = 0; i < TABLE_SIZE; i++)
{
delete[] pTable->pItems[i].handle;
delete[] pTable->pItems[i].path;
}
pTemp = pTable;
pTable = pTable->pNext;
delete pTemp;
}
while (m_pCacheList != NULL) //free cache
{
pPrev = m_pCacheList;
m_pCacheList = m_pCacheList->pNext;
delete pPrev;
}
}
unsigned long CFileTable::GetIDByPath(char *path)
{
unsigned char *handle;
handle = GetHandleByPath(path);
return *(unsigned long *)handle;
}
unsigned char *CFileTable::GetHandleByPath(char *path)
{
FILE_ITEM *pItem;
pItem = FindItemByPath(path);
if (pItem == NULL)
pItem = AddItem(path);
return pItem->handle;
}
char *CFileTable::GetPathByHandle(unsigned char *handle)
{
unsigned int id;
FILE_ITEM *pItem;
id = *(unsigned int *)handle;
pItem = GetItemByID(id);
return pItem == NULL? NULL : pItem->path;
}
FILE_ITEM *CFileTable::FindItemByPath(char *path)
{
CACHE_LIST *pCurr;
FILE_ITEM *pItem;
unsigned int i, j, nPathLen;
FILE_TABLE *pTable;
nPathLen = strlen(path);
pItem = NULL;
pCurr = m_pCacheList;
while (pCurr != NULL) //search in cache
{
if (nPathLen == pCurr->pItem->nPathLen) //comparing path length is faster than comparing path
if (strcmp(path, pCurr->pItem->path) == 0) //compare path
{
pItem = pCurr->pItem; //path matched
break;
}
pCurr = pCurr->pNext;
}
if (pItem == NULL) //not found in cache
{
pTable = m_pFirstTable;
for (i = 0; i < m_nTableSize; i += TABLE_SIZE) //search in file table
{
for (j = 0; j < TABLE_SIZE; j++)
{
if (i + j >= m_nTableSize) //all items in file table are compared
return NULL;
if (nPathLen == pTable->pItems[j].nPathLen) //comparing path length is faster than comparing path
if (strcmp(path, pTable->pItems[j].path) == 0) //compare path
{
pItem = pTable->pItems + j; //path matched
break;
}
}
if (pItem != NULL)
break;
pTable = pTable->pNext;
}
}
if (pItem != NULL)
PutItemInCache(pItem); //put the found item in cache
return pItem;
}
FILE_ITEM *CFileTable::AddItem(char *path)
{
FILE_ITEM item;
unsigned int nIndex;
item.path = new char[strlen(path) + 1];
strcpy(item.path, path); //path
item.nPathLen = strlen(item.path); //path length
item.handle = new unsigned char[NFS3_FHSIZE];
memset(item.handle, 0, NFS3_FHSIZE * sizeof(unsigned char));
*(unsigned int *)item.handle = m_nTableSize; //let its handle equal the index
item.bCached = false; //not in the cache
if (m_nTableSize > 0 && (m_nTableSize & (TABLE_SIZE - 1)) == 0)
{
m_pLastTable->pNext = new FILE_TABLE;
m_pLastTable = m_pLastTable->pNext;
memset(m_pLastTable, 0, sizeof(FILE_TABLE));
}
m_pLastTable->pItems[nIndex = m_nTableSize & (TABLE_SIZE - 1)] = item; //add the new item in the file table
++m_nTableSize;
return m_pLastTable->pItems + nIndex; //return the pointer to the new item
}
FILE_ITEM *CFileTable::GetItemByID(unsigned int nID)
{
FILE_TABLE *pTable;
unsigned int i;
if (nID >= m_nTableSize)
return NULL;
pTable = m_pFirstTable;
for (i = TABLE_SIZE; i <= nID; i += TABLE_SIZE)
pTable = pTable->pNext;
return pTable->pItems + nID + TABLE_SIZE - i;
}
void CFileTable::PutItemInCache(FILE_ITEM *pItem)
{
CACHE_LIST *pPrev, *pCurr;
int nCount;
pCurr = m_pCacheList;
if (pItem->bCached) //item is already in the cache
{
while (pCurr != NULL)
{
if (pItem == pCurr->pItem)
{
if (pCurr == m_pCacheList) //at the first
return;
else //move to the first
{
pPrev->pNext = pCurr->pNext;
pCurr->pNext = m_pCacheList;
m_pCacheList = pCurr;
return;
}
}
pPrev = pCurr;
pCurr = pCurr->pNext;
}
}
else
{
pItem->bCached = true;
for (nCount = 0; nCount < 9 && pCurr != NULL; nCount++) //seek to the end of the cache
{
pPrev = pCurr;
pCurr = pCurr->pNext;
}
if (nCount == 9 && pCurr != NULL) //there are 10 items in the cache
{
pPrev->pNext = NULL; //remove the last
pCurr->pItem->bCached = false;
}
else
pCurr = new CACHE_LIST;
pCurr->pItem = pItem;
pCurr->pNext = m_pCacheList;
m_pCacheList = pCurr; //insert to the first
}
}
bool FileExists(char *path)
{
int handle;
struct _finddata_t fileinfo;
handle = _findfirst(path, &fileinfo);
_findclose(handle);
return handle == -1? false : strcmp(fileinfo.name, strrchr(path, '\\') + 1) == 0; //filename must match case
}
unsigned long GetFileID(char *path)
{
return g_FileTable.GetIDByPath(path);
}
unsigned char *GetFileHandle(char *path)
{
return g_FileTable.GetHandleByPath(path);
}
char *GetFilePath(unsigned char *handle)
{
return g_FileTable.GetPathByHandle(handle);
}
bool RenameFile(char *pathFrom, char *pathTo)
{
FILE_ITEM *pItem;
pItem = g_FileTable.FindItemByPath(pathFrom);
if (pItem == NULL)
return false;
if (rename(pathFrom, pathTo) == 0) //success
{
delete[] pItem->path;
pItem->nPathLen = strlen(pathTo);
pItem->path = new char[pItem->nPathLen + 1];
strcpy(pItem->path, pathTo); //replace the path by new one
return true;
}
else
return false;
}
bool RemoveFile(char *path)
{
return remove(path) == 0;
}