📄 fldrmgr.c
字号:
}
return result;
}
static FmResult indexTableRead( IndexTable *to, char category)
{
CMN_BOOL isOpened = TRUE;
FmResult result = FM_RESULT_OK;
if (FILEa_open( category, FM_INDEX_FILE, FILE_OPTION_READ) < 0)
{
CMN_LOG_I(("%s(%d): Couldn't open index file category=%d\n",
__FILE__, __LINE__, (int)category));
isOpened = FALSE;
result = FM_RESULT_INDEX_FILE_ERROR;
}
else if (FILEa_read( category, FM_INDEX_FILE, &to->indexFile, 0,
sizeof(IndexFile)) < (int)sizeof(IndexFile))
{
CMN_LOG_I(("%s(%d): Couldn't read IndexFile category=%d.\n",
__FILE__, __LINE__, (int)category));
result = FM_RESULT_INDEX_FILE_ERROR;
}
if (isOpened)
{
FILEa_close( category, FM_INDEX_FILE);
isOpened = FALSE;
}
return result;
}
static FmResult indexTableSave( FmOperation oper, int save)
{
CMN_BOOL shouldWrite = FALSE;
FmResult result = FM_RESULT_OK;
FmResult resultbackup = FM_RESULT_OK;
if (myTable->isDirty == FALSE && myTable->isDirtyBackup == FALSE)
{
return result;
}
switch (oper)
{
case FM_OPER_ANY :
shouldWrite = TRUE;
break;
case FM_OPER_CREATE :
shouldWrite = CMN_INDEX_SAVE_CREATE;
break;
case FM_OPER_DELETE :
shouldWrite = CMN_INDEX_SAVE_DELETE;
break;
case FM_OPER_MOVE :
shouldWrite = CMN_INDEX_SAVE_MOVE;
break;
case FM_OPER_SET_DATE :
shouldWrite = CMN_INDEX_SAVE_SET_DATE;
break;
case FM_OPER_SET_METADATA :
shouldWrite = CMN_INDEX_SAVE_SET_FLAG;
break;
case FM_OPER_SET_FLAG :
shouldWrite = CMN_INDEX_SAVE_SET_READ;
break;
case FM_OPER_TERMINATE :
shouldWrite = TRUE;
break;
default :
CMN_LOG_I(("%s(%d): Illegal operation %d\n", __FILE__, __LINE__, oper));
shouldWrite = TRUE;
break;
}
if (shouldWrite == FALSE)
{
return result;
}
if (myTable->isDirty &&
(save == FM_SAVE_BOTH_FILES || save == FM_SAVE_INDEX_ONLY))
{
myTable->indexFile.date = CMNa_currentTime();
myTable->indexFile.chkSum = chksumCalc32( myTable->indexFile.msg,
SIZE_OF_MSGDATA);
result = indexTableWrite( myTable, CMN_FILE_CATEGORY_MSG);
if (result == FM_RESULT_OK)
{
myTable->isDirty = FALSE;
}
else
{
CMN_LOG_I(("%s(%d): SEVERE ERROR: Couldn't save IndexTable. "
"File system corrupt? Error=%d\n",
__FILE__, __LINE__, result));
return FM_RESULT_INDEX_FILE_ERROR;
}
}
if (myTable->isDirtyBackup &&
(save == FM_SAVE_BOTH_FILES || save == FM_SAVE_BACKUP_ONLY))
{
#if CMN_INDEX_ALWAYS_SAVE_BACKUP
{
#else
if (oper == FM_OPER_TERMINATE)
{
#endif
resultbackup = indexTableWrite( myTable, CMN_FILE_CATEGORY_TMP);
if (resultbackup == FM_RESULT_OK)
{
myTable->isDirtyBackup = FALSE;
}
else
{
CMN_LOG_I(("%s(%d): Couldn't save BackupTable %d\n",
__FILE__, __LINE__, resultbackup));
result = resultbackup;
}
}
}
return result;
}
static FmResult indexTableSort( CmnClientId client, IndexTable *table)
{
IndexTable *unsorted;
int i;
int j;
UINT32 maxId;
int maxPos;
unsorted = G_ALLOC( client, sizeof(IndexTable));
if (unsorted == NULL)
{
CMN_LOG_I(("%s(%d): Out of memory\n", __FILE__, __LINE__));
return FM_RESULT_MEMORY_ERROR;
}
memcpy( unsorted, table, sizeof(IndexTable));
j = unsorted->numOfMsg - 1;
while (j >= 0)
{
maxId = 0;
maxPos = 0;
for ( i = 0; i < unsorted->numOfMsg; ++i)
{
if (unsorted->indexFile.msg[i].msgFile > maxId)
{
maxId = unsorted->indexFile.msg[i].msgFile;
maxPos = i;
}
}
memcpy( &table->indexFile.msg[j], &unsorted->indexFile.msg[maxPos],
sizeof(IndexInfo));
indexTableEntryRemove( unsorted, maxPos);
--j;
}
G_FREE( client, unsorted);
return FM_RESULT_OK;
}
static FmResult indexTableValidate( IndexTable *table, const UINT32 files[], int nr)
{
int i;
int j;
CMN_BOOL isMissing = TRUE;
CMN_BOOL isMissingInfo = TRUE;
CMN_BOOL isRemoved = FALSE;
if (files == NULL || nr <= 1)
{
CMN_LOG_I(("%s(%d): No files (except file 0) on disk. Total %lu F files.\n",
__FILE__, __LINE__, nr));
memset( table, 0, sizeof(IndexTable));
return FM_RESULT_OK;
}
indexTableCompress(table);
i = 0;
while (i < table->numOfMsg)
{
if ( !FLDRMGR_FLAG_VALID(table->indexFile.msg[i].flag) )
{
indexTableEntryRemove( table, i);
isRemoved = TRUE;
}
else
{
for ( j = i + 1; j < table->numOfMsg; ++j)
{
if (table->indexFile.msg[i].msgFile == table->indexFile.msg[j].msgFile ||
table->indexFile.msg[i].infoFile == table->indexFile.msg[j].msgFile ||
table->indexFile.msg[i].msgFile == table->indexFile.msg[j].infoFile ||
table->indexFile.msg[i].infoFile == table->indexFile.msg[j].infoFile)
{
indexTableEntryRemove( table, j);
isRemoved = TRUE;
break;
}
}
}
if (isRemoved)
{
}
else
{
++i;
}
isRemoved = FALSE;
}
i = 0;
while (i < table->numOfMsg)
{
isMissing = TRUE;
isMissingInfo = TRUE;
for ( j = 0; j < nr; ++j)
{
if (table->indexFile.msg[i].msgFile == files[j])
{
isMissing = FALSE;
}
else if (table->indexFile.msg[i].infoFile == files[j])
{
isMissingInfo = FALSE;
}
if (isMissing == FALSE && isMissingInfo == FALSE)
{
break;
}
}
if (isMissing || isMissingInfo)
{
indexTableEntryRemove( table, i);
}
else
{
++i;
}
}
for ( j = 0; j < nr; ++j)
{
isMissing = TRUE;
for ( i = 0; isMissing && files[j] != 0 && i < table->numOfMsg; ++i)
{
if (table->indexFile.msg[i].msgFile == files[j] ||
table->indexFile.msg[i].infoFile == files[j])
{
isMissing = FALSE;
}
}
if (isMissing && files[j] != 0)
{
FILEa_delete( CMN_FILE_CATEGORY_MSG, files[j]);
}
}
return FM_RESULT_OK;
}
static CMN_BOOL indexTableVerifyChecksum( IndexTable *table)
{
UINT32 chksum;
CMN_BOOL result = TRUE;
chksum = chksumCalc32( table->indexFile.msg, SIZE_OF_MSGDATA);
if (chksum != table->indexFile.chkSum)
{
CMN_LOG_I(("%s(%d): Wrong check sum %lx != %lx\n",
__FILE__, __LINE__, chksum, table->indexFile.chkSum));
result = FALSE;
}
return result;
}
static FmResult indexTableWrite( IndexTable *from, char category)
{
INT32 written;
CMN_BOOL isOpened = TRUE;
FmResult result = FM_RESULT_OK;
from->indexFile.version = FM_VERSION_CURRENT;
if (FILEa_open( category, FM_INDEX_FILE, FILE_OPTION_WRITE) < 0)
{
CMN_LOG_I(("%s(%d): Couldn't open category=%d\n",
__FILE__, __LINE__, (int)category));
isOpened = FALSE;
result = FM_RESULT_INDEX_FILE_ERROR;
}
else if ( (written = FILEa_write( category, FM_INDEX_FILE, &from->indexFile, 0,
sizeof(IndexFile))) != (int)sizeof(IndexFile))
{
CMN_LOG_I(("%s(%d): Couldn't write IndexFile category=%d written=%ld\n",
__FILE__, __LINE__, (int)category, written));
if (written < 0)
{
result = FM_RESULT_INDEX_FILE_ERROR;
}
else
{
result = FM_RESULT_DISK_FULL;
}
}
if (isOpened)
{
if (result != FM_RESULT_OK)
{
CMN_LOG_I(("%s(%d): Index File not fully written, removing "
"remains of category=%d\n", __FILE__, __LINE__, (int)category));
if (FILEa_setSize( category, FM_INDEX_FILE, 0) != FM_RESULT_OK)
{
CMN_LOG_I(("%s(%d): Can't even set size on file 0.\n",
__FILE__, __LINE__));
}
}
FILEa_close( category, FM_INDEX_FILE);
isOpened = FALSE;
}
return result;
}
static CMN_BOOL initDisk( const UINT32 allFileIds[], int nrOfFiles)
{
int i = 0;
for (i = 0; i < nrOfFiles; i++)
{
FILEa_close( CMN_FILE_CATEGORY_MSG, allFileIds[i]);
}
return TRUE;
}
static FmResult setData(CmnClientId client, UINT32 msgId, unsigned char *data,
UINT32 dataSize, UINT32 startPos, CMN_BOOL isList)
{
UINT32 clientMsgId = 0;
long sizeWritten = 0;
INT32 writePosition = 0;
clientMsgId = fldrmgrGetId( client, msgId, isList);
if (clientMsgId == FM_INDEX_FILE)
{
return FM_RESULT_MSGID_NOT_FOUND;
}
if (startPos == FM_APPEND)
{
writePosition = FILEa_getSize(CMN_FILE_CATEGORY_MSG,clientMsgId);
if (writePosition == FM_RETURN_ERROR)
{
CMN_LOG_I(("%s(%d): FILEa_getSize returned %d\n",
__FILE__, __LINE__, writePosition));
return FM_RESULT_FILE_ERROR;
}
sizeWritten = FILEa_write(CMN_FILE_CATEGORY_MSG, clientMsgId, data,
writePosition, (long)dataSize);
}
else
{
sizeWritten = FILEa_write(CMN_FILE_CATEGORY_MSG, clientMsgId, data,
(long)startPos, (long)dataSize);
}
if (sizeWritten == FM_RETURN_ERROR)
{
CMN_LOG_I(("%s(%d): FILEa_write returned %d\n",
__FILE__, __LINE__, sizeWritten));
return FM_RESULT_FILE_ERROR;
}
else if (sizeWritten < (long)dataSize)
{
CMN_LOG_I(("%s(%d): FILEa_write returned %d\n",
__FILE__, __LINE__, sizeWritten));
return FM_RESULT_DISK_FULL;
}
return FM_RESULT_OK;
}
static FmResult getData(CmnClientId client, UINT32 msgId, UINT32 *bytesRead,
unsigned char *data, UINT32 startPos, UINT32 bytesToRead, CMN_BOOL isList)
{
UINT32 clientMsgId = 0;
INT32 returnSize = 0;
clientMsgId = fldrmgrGetId( client, msgId, isList);
if (clientMsgId == FM_INDEX_FILE)
{
return FM_RESULT_MSGID_NOT_FOUND;
}
returnSize = FILEa_read(CMN_FILE_CATEGORY_MSG, clientMsgId, data,
(INT32)startPos, (INT32)bytesToRead);
if (returnSize == FM_RETURN_ERROR)
{
CMN_LOG_I(("%s(%d): FILEa_read( %c, %lu, ptr, %lu, %lu) returned %d\n",
__FILE__, __LINE__, CMN_FILE_CATEGORY_MSG, clientMsgId,
startPos, bytesToRead, returnSize));
return FM_RESULT_FILE_ERROR;
}
else
{
*bytesRead = (UINT32)returnSize;
}
return FM_RESULT_OK;
}
static FmResult setSize(CmnClientId client, UINT32 msgId, CMN_BOOL isList,
UINT32 size)
{
UINT32 clientMsgId = 0UL;
long retSize = 0;
clientMsgId = fldrmgrGetId( client, msgId, isList);
if (clientMsgId == FM_INDEX_FILE)
{
CMN_LOG_I(("%s(%d): fldrmgrGetId failed\n", __FILE__, __LINE__));
return FM_RESULT_MSGID_NOT_FOUND;
}
retSize = FILEa_setSize(CMN_FILE_CATEGORY_MSG, clientMsgId, (long)size);
if (retSize == FM_RETURN_ERROR)
{
CMN_LOG_I(("%s(%d): FILEa_setSize failed\n", __FILE__, __LINE__));
return FM_RESULT_FILE_ERROR;
}
else if (retSize < (long)size)
{
return FM_RESULT_DISK_FULL;
}
return FM_RESULT_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -