1
0
Fork 0

Fix a bug when database file size was 0

This commit is contained in:
Nukem 2015-04-05 00:19:38 -04:00
parent 4f70e1347f
commit 757ebe50ab
1 changed files with 53 additions and 18 deletions

View File

@ -61,40 +61,75 @@ void dbsave()
void dbload()
{
if(!FileExists(dbpath)) //no database to load
// If the file doesn't exist, there is no DB to load
if(!FileExists(dbpath))
return;
dprintf("loading database...");
dprintf("Loading database...");
DWORD ticks = GetTickCount();
WString wdbpath = StringUtils::Utf8ToUtf16(dbpath);
bool compress = !settingboolget("Engine", "DisableCompression");
LZ4_STATUS status = LZ4_decompress_fileW(wdbpath.c_str(), wdbpath.c_str());
if(status != LZ4_SUCCESS && status != LZ4_INVALID_ARCHIVE && compress)
// Multi-byte (UTF8) file path converted to UTF16
WString databasePathW = StringUtils::Utf8ToUtf16(dbpath);
// Decompress the file if compression was enabled
bool useCompression = !settingboolget("Engine", "DisableCompression");
LZ4_STATUS lzmaStatus = LZ4_INVALID_ARCHIVE;
{
dputs("\ninvalid database file!");
lzmaStatus = LZ4_decompress_fileW(databasePathW.c_str(), databasePathW.c_str());
// Check return code
if (useCompression && lzmaStatus != LZ4_SUCCESS && lzmaStatus != LZ4_INVALID_ARCHIVE)
{
dputs("\nInvalid database file!");
return;
}
}
// Open the file for reading by the JSON parser
FILE* jsonFile = nullptr;
long jsonFileSize = 0;
if(_wfopen_s(&jsonFile, databasePathW.c_str(), L"rb"))
{
dputs("\nFailed to open database file!");
return;
}
FILE* jsonFile = 0;
if(_wfopen_s(&jsonFile, wdbpath.c_str(), L"rb") != 0)
{
dputs("\nfailed to open database file!");
return;
}
JSON root = json_loadf(jsonFile, 0, 0);
// Get the current file size
fseek(jsonFile, 0, SEEK_END);
jsonFileSize = ftell(jsonFile);
fseek(jsonFile, 0, SEEK_SET);
// Verify that the file size is greater than 0.
// This corrects a bug when a file exists, but there is no data inside.
JSON root = nullptr;
if (jsonFileSize > 0)
root = json_loadf(jsonFile, 0, 0);
// Release the file handle and re-compress
fclose(jsonFile);
if(status != LZ4_INVALID_ARCHIVE && compress)
LZ4_compress_fileW(wdbpath.c_str(), wdbpath.c_str());
if(lzmaStatus != LZ4_INVALID_ARCHIVE && useCompression)
LZ4_compress_fileW(databasePathW.c_str(), databasePathW.c_str());
// Validate JSON load status
if(!root)
{
dputs("\ninvalid database file (JSON)!");
dputs("\nInvalid database file (JSON)!");
return;
}
// Finally load all structures
CommentCacheLoad(root);
LabelCacheLoad(root);
BookmarkCacheLoad(root);
FunctionCacheLoad(root);
LoopCacheLoad(root);
BpCacheLoad(root);
json_decref(root); //free root
// Free root
json_decref(root);
dprintf("%ums\n", GetTickCount() - ticks);
}