📄 fldrmgr.c
字号:
int ret;
UINT32 newId;
if (infoFileId == 0)
{
ret = FILEa_create(CMN_FILE_CATEGORY_MSG, NULL, &newId);
if (ret != 0)
{
CMN_LOG_I(("%s(%d): FILEa_create returned error\n",
__FILE__, __LINE__));
}
else
{
FILEa_close(CMN_FILE_CATEGORY_MSG, newId);
infoFileId = newId;
}
}
return indexTableEntryCreate( CMN_CLIENT_MMS, myTable, folder, 0,
fileId, infoFileId);
}
FmResult fldrmgrTestOpen(UINT32 fileId)
{
int ret;
ret = FILEa_open(CMN_FILE_CATEGORY_MSG, fileId, FM_READ_WRITE);
if (ret < 0)
{
CMN_LOG_I(("%s(%d): Couldn't open id=%ld\n",
__FILE__, __LINE__, fileId));
}
return ret == -1 ? FM_RESULT_FILE_ERROR : FM_RESULT_OK;
}
#endif
FmResult fldrmgrTerminate(CmnClientId client)
{
FmResult result;
result = indexTableSave( FM_OPER_TERMINATE, FM_SAVE_BOTH_FILES);
#ifdef CMN_INDEX_DYNAMIC
if (myTable != NULL && myTable->client == client)
{
G_FREE( client, myTable);
myTable = NULL;
}
#else
client = 0;
#endif
return result;
}
static FmResult getSize(CmnClientId client, UINT32 msgId, UINT32 *chunkSize,
CMN_BOOL isList)
{
UINT32 clientMsgId = 0;
INT32 size = 0;
*chunkSize = 0;
clientMsgId = fldrmgrGetId( client, msgId, isList);
if (clientMsgId == FM_INDEX_FILE)
{
return FM_RESULT_MSGID_NOT_FOUND;
}
size = FILEa_getSize( CMN_FILE_CATEGORY_MSG, clientMsgId);
if (size == FM_RETURN_ERROR)
{
CMN_LOG_I(("%s(%d): FILEa_getSize returned %d\n",
__FILE__, __LINE__, size));
return FM_RESULT_FILE_ERROR;
}
else
{
*chunkSize = (UINT32)size;
}
return FM_RESULT_OK;
}
static FmResult indexTableClear(IndexTable *table, char category)
{
FmResult result = FM_RESULT_ERROR;
memset( table, 0, sizeof(IndexTable));
table->indexFile.chkSum = chksumCalc32( table->indexFile.msg, SIZE_OF_MSGDATA);
table->indexFile.date = CMNa_currentTime();
result = indexTableWrite( table, category);
if (result != FM_RESULT_OK)
{
CMN_LOG_I(("%s(%d): Couldn't write %c file. "
"File system corrupt? Error=%d\n",
__FILE__, __LINE__, category, result));
}
else if ( FILEa_setSize( category, FM_INDEX_FILE,
(INT32)sizeof(IndexFile)) != (INT32)sizeof(IndexFile))
{
CMN_LOG_I(("%s(%d): Couldn't set size category=%d, size=%lu\n",
__FILE__, __LINE__, (int)category, sizeof(IndexFile)));
result = FM_RESULT_INDEX_FILE_ERROR;
}
return result;
}
static void indexTableCompress(IndexTable *table)
{
UINT32 lastFound = 0;
UINT32 i;
CMN_BOOL isCompressing = FALSE;
for ( i = 0; i < CMN_MAX_NO_OF_MESSAGES; ++i)
{
if (isCompressing && table->indexFile.msg[i].msgFile != 0)
{
memmove( &table->indexFile.msg[lastFound], &table->indexFile.msg[i],
sizeof(IndexInfo));
memset( &table->indexFile.msg[i], 0, sizeof(IndexInfo));
lastFound = i;
}
else if (table->indexFile.msg[i].msgFile == 0)
{
isCompressing = TRUE;
lastFound = i;
}
}
table->numOfMsg = 0;
for ( i = 0; i < CMN_MAX_NO_OF_MESSAGES &&
table->indexFile.msg[i].msgFile != 0; ++i)
{
++table->numOfMsg;
}
}
static FmResult indexTableConvert1( CmnClientId client, IndexTable *table)
{
CMN_BOOL isIndexFileOk = FALSE;
FmResult result = FM_RESULT_ERROR;
if (indexTableRead1( table, CMN_FILE_CATEGORY_MSG) != FM_RESULT_OK)
{
if (indexTableRead1( table, CMN_FILE_CATEGORY_MSG) != FM_RESULT_OK)
{
CMN_LOG_I(("%s(%d): Couldn't read neither index nor backup file "
"of old format. Result=%d\n", __FILE__, __LINE__, result));
return indexTableClear( table, CMN_FILE_CATEGORY_MSG);
}
}
isIndexFileOk = indexTableVerifyChecksum(table);
if ( !isIndexFileOk)
{
CMN_LOG_I(("%s(%d): Checksum of old format file is not correct.\n",
__FILE__, __LINE__, result));
return indexTableClear( table, CMN_FILE_CATEGORY_MSG);
}
indexTableCompress(table);
if (indexTableSort( client, table) != FM_RESULT_OK)
{
CMN_LOG_I(("%s(%d): Not enough memory to sort old index table.\n",
__FILE__, __LINE__));
return indexTableClear( table, CMN_FILE_CATEGORY_MSG);
}
table->indexFile.chkSum = chksumCalc32( table->indexFile.msg, SIZE_OF_MSGDATA);
table->indexFile.date = CMNa_currentTime();
result = indexTableWrite( table, CMN_FILE_CATEGORY_MSG);
if (result != FM_RESULT_OK)
{
CMN_LOG_I(("%s(%d): Couldn't write converted file. "
"File system corrupt? Result=%d\n", __FILE__, __LINE__, result));
}
else if ( FILEa_setSize( CMN_FILE_CATEGORY_MSG, FM_INDEX_FILE,
(INT32)sizeof(IndexFile)) != (INT32)sizeof(IndexFile))
{
CMN_LOG_I(("%s(%d): Couldn't set size size=%lu\n",
__FILE__, __LINE__, sizeof(IndexFile)));
result = FM_RESULT_INDEX_FILE_ERROR;
}
else
{
result = FM_RESULT_INDEX_FILE_CONVERTED;
}
return result;
}
static FmResult indexTableEntryCreate( CmnClientId client, IndexTable *table,
UINT16 folder, UINT8 isNotif, UINT32 msgFile, UINT32 infoFile)
{
int low;
int high;
int mid;
if (table->numOfMsg >= CMN_MAX_NO_OF_MESSAGES)
{
CMN_LOG_I(("%s(%d): Maximum number of allowed messages reached %d\n",
__FILE__, __LINE__, table->numOfMsg));
return FM_RESULT_INDEX_FILE_FULL;
}
low = 0;
mid = 0;
high = myTable->numOfMsg - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (msgFile < myTable->indexFile.msg[mid].msgFile)
{
high = mid - 1;
}
else if (msgFile > myTable->indexFile.msg[mid].msgFile)
{
low = mid + 1;
}
else if (myTable->indexFile.msg[mid].client == client)
{
CMN_LOG_I(("%s(%d): Trying to create a duplicate (%d)? Replacing instead!\n",
__FILE__, __LINE__, msgFile));
break;
}
else
{
CMN_LOG_I(("%s(%d): Trying to add same msg %d as client %d (%d)\n",
__FILE__, __LINE__, msgFile, myTable->indexFile.msg[mid].client,
client));
return FM_RESULT_ERROR;
}
}
if (high < 0)
{
mid = 0;
}
else if (low > high)
{
mid = (low + high) / 2 + 1;
}
if (mid < myTable->numOfMsg)
{
memmove( &table->indexFile.msg[mid + 1], &table->indexFile.msg[mid],
(UINT32)(myTable->numOfMsg - mid) * sizeof(IndexInfo));
memset( &table->indexFile.msg[mid], 0, sizeof(IndexInfo));
}
table->indexFile.msg[mid].client = client;
table->indexFile.msg[mid].folder = folder;
table->indexFile.msg[mid].msgFile = msgFile;
table->indexFile.msg[mid].infoFile = infoFile;
table->indexFile.msg[mid].date = CMNa_currentTime();
if (isNotif)
{
table->indexFile.msg[mid].flag |= CMN_ATTRIBUTE_NOTIF;
}
++table->numOfMsg;
return FM_RESULT_OK;
}
static void indexTableEntryRemove( IndexTable *table, int pos)
{
if (pos >= CMN_MAX_NO_OF_MESSAGES)
{
CMN_LOG_I(("%s(%d): Can't remove pos=%d(%d)\n",
__FILE__, __LINE__, pos, CMN_MAX_NO_OF_MESSAGES));
return;
}
if (pos < CMN_MAX_NO_OF_MESSAGES)
{
memset( &table->indexFile.msg[pos], 0, sizeof(IndexInfo));
}
if (pos < CMN_MAX_NO_OF_MESSAGES - 1)
{
memmove( &table->indexFile.msg[pos], &table->indexFile.msg[pos + 1],
(UINT32)(CMN_MAX_NO_OF_MESSAGES - pos - 1) * sizeof(IndexInfo));
memset( &table->indexFile.msg[CMN_MAX_NO_OF_MESSAGES - 1], 0,
sizeof(IndexInfo));
}
if (table->numOfMsg > 0)
{
--table->numOfMsg;
}
else
{
table->numOfMsg = 0;
}
}
static FmResult indexTableInit( CmnClientId client, IndexTable *table)
{
long indexFileSize = 0;
long backupFileSize = 0;
unsigned long indexFileDate = 0;
unsigned long backupFileDate = 0;
CMN_BOOL isIndexFileOk = FALSE;
CMN_BOOL isBackupFileOk = FALSE;
CMN_BOOL useBackupFile = FALSE;
FmResult result = FM_RESULT_ERROR;
memset( table, 0, sizeof(IndexTable));
indexFileSize = FILEa_getSize(CMN_FILE_CATEGORY_MSG, FM_INDEX_FILE);
backupFileSize = FILEa_getSize(CMN_FILE_CATEGORY_TMP, FM_INDEX_FILE);
if (indexFileSize == 0 && backupFileSize == 0)
{
return FM_RESULT_OK;
}
else if (indexFileSize < 0 || backupFileSize < 0)
{
CMN_LOG_I(("%s(%d): Can't get size of file 0. Error=%d, %d\n",
__FILE__, __LINE__, indexFileSize, backupFileSize));
return FM_RESULT_FILE_ERROR;
}
if (indexFileSize == FM_VERSION_1_INDEX_SIZE &&
backupFileSize == FM_VERSION_1_INDEX_SIZE)
{
CMN_LOG_I(("%s(%d): Converting old index file=%d, %d\n",
__FILE__, __LINE__, indexFileSize, backupFileSize));
return indexTableConvert1( client, table);
}
if (indexTableRead( table, CMN_FILE_CATEGORY_TMP) == FM_RESULT_OK)
{
isBackupFileOk = indexTableVerifyChecksum(table);
backupFileDate = table->indexFile.date;
if (table->numOfMsg < 0 || table->numOfMsg > CMN_MAX_NO_OF_MESSAGES)
{
CMN_LOG_I(("%s(%d): Backup index file contains corrupt count=%d\n",
__FILE__, __LINE__, table->numOfMsg));
isBackupFileOk = FALSE;
}
}
if (indexTableRead( table, CMN_FILE_CATEGORY_MSG) == FM_RESULT_OK)
{
isIndexFileOk = indexTableVerifyChecksum(table);
indexFileDate = table->indexFile.date;
if (table->numOfMsg < 0 || table->numOfMsg > CMN_MAX_NO_OF_MESSAGES)
{
CMN_LOG_I(("%s(%d): Index file contains corrupt count=%d\n",
__FILE__, __LINE__, table->numOfMsg));
isIndexFileOk = FALSE;
}
}
if (isIndexFileOk && isBackupFileOk)
{
if (indexFileDate >= backupFileDate)
{
result = FM_RESULT_OK;
}
else
{
useBackupFile = TRUE;
}
}
else if (isIndexFileOk)
{
result = FM_RESULT_OK;
}
else if (isBackupFileOk)
{
useBackupFile = TRUE;
}
else
{
CMN_LOG_I(("%s(%d): Both Index file and Backup file is corrupt.\n",
__FILE__, __LINE__));
memset( table, 0, sizeof(IndexTable));
result = FM_RESULT_OK;
}
if (useBackupFile)
{
result = indexTableRead( table, CMN_FILE_CATEGORY_TMP);
if (result != FM_RESULT_OK)
{
CMN_LOG_I(("%s(%d): Backup file corrupt at second read. Clear it.\n",
__FILE__, __LINE__));
result = indexTableClear( table, CMN_FILE_CATEGORY_TMP);
}
else if ( !indexTableVerifyChecksum(table) )
{
CMN_LOG_I(("%s(%d): Backup file chksum invalid at second read.\n",
__FILE__, __LINE__));
result = indexTableClear( table, CMN_FILE_CATEGORY_TMP);
}
if (result == FM_RESULT_OK)
{
CMN_LOG_I(("%s(%d): Replacing index file with backup copy.\n",
__FILE__, __LINE__));
result = indexTableWrite( table, CMN_FILE_CATEGORY_MSG);
if (result != FM_RESULT_OK)
{
CMN_LOG_I(("%s(%d): Couldn't restore backup/index file. "
"File system corrupt? Result=%d\n",
__FILE__, __LINE__, result));
result = FM_RESULT_INDEX_FILE_ERROR;
}
else if ( FILEa_setSize( CMN_FILE_CATEGORY_MSG, FM_INDEX_FILE,
(INT32)sizeof(IndexFile)) != (INT32)sizeof(IndexFile))
{
CMN_LOG_I(("%s(%d): Couldn't set size size=%lu\n",
__FILE__, __LINE__, sizeof(IndexFile)));
result = FM_RESULT_INDEX_FILE_ERROR;
}
}
if (result != FM_RESULT_OK)
{
CMN_LOG_I(("%s(%d): Couldn't read/write backup/index file. "
"File system corrupt? Result=%d\n",
__FILE__, __LINE__, result));
result = FM_RESULT_INDEX_FILE_ERROR;
}
}
return result;
}
static FmResult indexTableRead1( IndexTable *to, char category)
{
INT32 posChkSum = 0;
INT32 posMsgData = sizeof(UINT32);
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.chkSum,
posChkSum, sizeof(UINT32)) < (int)sizeof(UINT32))
{
CMN_LOG_I(("%s(%d): Couldn't read chksum category=%d.\n",
__FILE__, __LINE__, (int)category));
result = FM_RESULT_INDEX_FILE_ERROR;
}
else if ( FILEa_read( category, FM_INDEX_FILE, to->indexFile.msg, posMsgData,
(INT32)SIZE_OF_MSGDATA) < (INT32)SIZE_OF_MSGDATA)
{
CMN_LOG_I(("%s(%d): Couldn't read msgData category=%d, size=%lu\n",
__FILE__, __LINE__, (int)category, SIZE_OF_MSGDATA));
result = FM_RESULT_INDEX_FILE_ERROR;
}
if (isOpened)
{
FILEa_close( category, FM_INDEX_FILE);
isOpened = FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -