Switch to C I/O API for asset cache

master
Ansariel 2021-03-10 22:19:13 +01:00
parent e6c34f01d2
commit 2c57d7164e
1 changed files with 123 additions and 46 deletions

View File

@ -62,12 +62,20 @@ bool LLFileSystem::getExists(const LLUUID& file_id, const LLAssetType::EType fil
const std::string extra_info = "";
const std::string filename = LLDiskCache::getInstance()->metaDataToFilepath(id_str, file_type, extra_info);
llifstream file(filename, std::ios::binary);
if (file.is_open())
// <FS:Ansariel> IO-streams replacement
//llifstream file(filename, std::ios::binary);
//if (file.is_open())
//{
// file.seekg(0, std::ios::end);
// return file.tellg() > 0;
//}
llstat file_stat;
if (LLFile::stat(filename, &file_stat) == 0)
{
file.seekg(0, std::ios::end);
return file.tellg() > 0;
return S_ISREG(file_stat.st_mode) && file_stat.st_size > 0;
}
// </FS:Ansariel>
return false;
}
@ -130,12 +138,19 @@ S32 LLFileSystem::getFileSize(const LLUUID& file_id, const LLAssetType::EType fi
const std::string filename = LLDiskCache::getInstance()->metaDataToFilepath(id_str, file_type, extra_info);
S32 file_size = 0;
llifstream file(filename, std::ios::binary);
if (file.is_open())
// <FS:Ansariel> IO-streams replacement
//llifstream file(filename, std::ios::binary);
//if (file.is_open())
//{
// file.seekg(0, std::ios::end);
// file_size = file.tellg();
//}
llstat file_stat;
if (LLFile::stat(filename, &file_stat) == 0)
{
file.seekg(0, std::ios::end);
file_size = file.tellg();
file_size = file_stat.st_size;
}
// </FS:Ansariel>
return file_size;
}
@ -149,23 +164,37 @@ BOOL LLFileSystem::read(U8* buffer, S32 bytes)
const std::string extra_info = "";
const std::string filename = LLDiskCache::getInstance()->metaDataToFilepath(id, mFileType, extra_info);
llifstream file(filename, std::ios::binary);
if (file.is_open())
// <FS:Ansariel> IO-streams replacement
//llifstream file(filename, std::ios::binary);
//if (file.is_open())
//{
// file.seekg(mPosition, std::ios::beg);
// file.read((char*)buffer, bytes);
// if (file)
// {
// mBytesRead = bytes;
// }
// else
// {
// mBytesRead = file.gcount();
// }
// file.close();
// mPosition += mBytesRead;
// if (!mBytesRead)
// {
// success = FALSE;
// }
//}
LLFILE* file = LLFile::fopen(filename, "rb");
if (file)
{
file.seekg(mPosition, std::ios::beg);
file.read((char*)buffer, bytes);
if (file)
{
mBytesRead = bytes;
}
else
{
mBytesRead = file.gcount();
}
file.close();
fseek(file, mPosition, SEEK_SET);
mBytesRead = fread(buffer, 1, bytes, file);
fclose(file);
mPosition += mBytesRead;
if (!mBytesRead)
@ -173,6 +202,7 @@ BOOL LLFileSystem::read(U8* buffer, S32 bytes)
success = FALSE;
}
}
// </FS:Ansariel>
// update the last access time for the file - this is required
// even though we are reading and not writing because this is the
@ -202,55 +232,102 @@ BOOL LLFileSystem::write(const U8* buffer, S32 bytes)
BOOL success = FALSE;
// <FS:Ansariel> IO-streams replacement
//if (mMode == APPEND)
//{
// llofstream ofs(filename, std::ios::app | std::ios::binary);
// if (ofs)
// {
// ofs.write((const char*)buffer, bytes);
// mPosition = ofs.tellp(); // <FS:Ansariel> Fix asset caching
// success = TRUE;
// }
//}
//// <FS:Ansariel> Fix asset caching
//else if (mMode == READ_WRITE)
//{
// // Don't truncate if file already exists
// llofstream ofs(filename, std::ios::in | std::ios::binary);
// if (ofs)
// {
// ofs.seekp(mPosition, std::ios::beg);
// ofs.write((const char*)buffer, bytes);
// mPosition += bytes;
// success = TRUE;
// }
// else
// {
// // File doesn't exist - open in write mode
// ofs.open(filename, std::ios::binary);
// if (ofs.is_open())
// {
// ofs.write((const char*)buffer, bytes);
// mPosition += bytes;
// success = TRUE;
// }
// }
//}
//// </FS:Ansariel>
//else
//{
// llofstream ofs(filename, std::ios::binary);
// if (ofs)
// {
// ofs.write((const char*)buffer, bytes);
// mPosition += bytes;
// success = TRUE;
// }
//}
if (mMode == APPEND)
{
llofstream ofs(filename, std::ios::app | std::ios::binary);
LLFILE* ofs = LLFile::fopen(filename, "a+b");
if (ofs)
{
ofs.write((const char*)buffer, bytes);
mPosition = ofs.tellp(); // <FS:Ansariel> Fix asset caching
fwrite(buffer, 1, bytes, ofs);
mPosition = ftell(ofs);
fclose(ofs);
success = TRUE;
}
}
// <FS:Ansariel> Fix asset caching
else if (mMode == READ_WRITE)
{
// Don't truncate if file already exists
llofstream ofs(filename, std::ios::in | std::ios::binary);
LLFILE* ofs = LLFile::fopen(filename, "r+b");
if (ofs)
{
ofs.seekp(mPosition, std::ios::beg);
ofs.write((const char*)buffer, bytes);
mPosition += bytes;
fseek(ofs, mPosition, SEEK_SET);
fwrite(buffer, 1, bytes, ofs);
mPosition = ftell(ofs);
fclose(ofs);
success = TRUE;
}
else
{
// File doesn't exist - open in write mode
ofs.open(filename, std::ios::binary);
if (ofs.is_open())
ofs = LLFile::fopen(filename, "wb");
if (ofs)
{
ofs.write((const char*)buffer, bytes);
mPosition += bytes;
fwrite(buffer, 1, bytes, ofs);
mPosition = ftell(ofs);
fclose(ofs);
success = TRUE;
}
}
}
// </FS:Ansariel>
else
{
llofstream ofs(filename, std::ios::binary);
LLFILE* ofs = LLFile::fopen(filename, "wb");
if (ofs)
{
ofs.write((const char*)buffer, bytes);
mPosition += bytes;
fwrite(buffer, 1, bytes, ofs);
mPosition = ftell(ofs);
fclose(ofs);
success = TRUE;
}
}
// </FS:Ansariel>
return success;
}