Skip to content
Snippets Groups Projects
Commit 45aff91e authored by Rye Mutt's avatar Rye Mutt :bread:
Browse files

A few tweaks to make the cache more reliable

parent d857c3d4
No related branches found
No related tags found
No related merge requests found
...@@ -57,12 +57,17 @@ bool LLFileSystem::open() ...@@ -57,12 +57,17 @@ bool LLFileSystem::open()
{ {
case READ: mFile = LLFile::fopen(mFilePath, TEXT("rb")); break; case READ: mFile = LLFile::fopen(mFilePath, TEXT("rb")); break;
case WRITE: mFile = LLFile::fopen(mFilePath, TEXT("wb")); break; case WRITE: mFile = LLFile::fopen(mFilePath, TEXT("wb")); break;
case READ_WRITE: mFile = LLFile::fopen(mFilePath, TEXT("rb+")); break; case READ_WRITE: mFile = LLFile::fopen(mFilePath, TEXT("r+b")); break;
case APPEND: mFile = LLFile::fopen(mFilePath, TEXT("ab")); break; case APPEND: mFile = LLFile::fopen(mFilePath, TEXT("a+b")); break;
} }
if (mMode == READ_WRITE && !mFile) if (mMode == READ_WRITE && !mFile)
mFile = LLFile::fopen(mFilePath, TEXT("wb+")); mFile = LLFile::fopen(mFilePath, TEXT("wb+"));
if (mFile)
{
mPosition = ftell(mFile);
}
return mFile; return mFile;
} }
...@@ -85,45 +90,22 @@ BOOL LLFileSystem::read(U8* buffer, S32 bytes) ...@@ -85,45 +90,22 @@ BOOL LLFileSystem::read(U8* buffer, S32 bytes)
return FALSE; return FALSE;
} }
BOOL success = TRUE; BOOL success = FALSE;
fseek(mFile, mPosition, SEEK_SET); if (fseek(mFile, mPosition, SEEK_SET) == 0)
size_t bytes_read = fread((void*)buffer, 1, bytes, mFile);
if (bytes_read == bytes)
{
mBytesRead = bytes_read;
}
else
{ {
fseek(mFile, 0L, SEEK_END); mBytesRead = fread(buffer, 1, bytes, mFile);
long fsize = ftell(mFile);
if (mPosition < fsize) mPosition = ftell(mFile);
{
fseek(mFile, mPosition, SEEK_SET); // It probably would be correct to check for mBytesRead == bytes,
long rsize = fsize - mPosition; // but that will break avatar rezzing...
bytes_read = fread((void*)buffer, 1, rsize, mFile); if (mBytesRead)
if (bytes_read == rsize)
{
mBytesRead = bytes_read;
}
else
{
success = FALSE;
}
}
else
{ {
success = FALSE; success = TRUE;
} }
} }
if (success == FALSE)
{
mBytesRead = 0;
}
mPosition += mBytesRead;
// update the last access time for the file - this is required // update the last access time for the file - this is required
// even though we are reading and not writing because this is the // even though we are reading and not writing because this is the
// way the cache works - it relies on a valid "last accessed time" for // way the cache works - it relies on a valid "last accessed time" for
...@@ -160,41 +142,21 @@ BOOL LLFileSystem::write(const U8* buffer, S32 bytes) ...@@ -160,41 +142,21 @@ BOOL LLFileSystem::write(const U8* buffer, S32 bytes)
BOOL success = FALSE; BOOL success = FALSE;
if (mMode == READ_WRITE) if (mMode == READ_WRITE)
{ {
fseek(mFile, mPosition, SEEK_SET); if (fseek(mFile, mPosition, SEEK_SET) == 0)
size_t bytes_written = fwrite((const void*)buffer, 1, bytes, mFile);
if (bytes_written == bytes)
{ {
mPosition += bytes_written; size_t bytes_written = fwrite((const void*)buffer, 1, bytes, mFile);
success = (bytes_written == bytes);
success = TRUE;
}
}
else if (mMode == APPEND)
{
fseek(mFile, 0L, SEEK_END);
long fsize = ftell(mFile);
size_t bytes_written = fwrite((const void*)buffer, 1, bytes, mFile);
if (bytes_written == bytes)
{
mPosition = fsize + bytes_written;
success = TRUE;
} }
} }
else else
{ {
size_t bytes_written = fwrite((const void*)buffer, 1, bytes, mFile); size_t bytes_written = fwrite((const void*)buffer, 1, bytes, mFile);
success = (bytes_written == bytes);
if (bytes_written == bytes)
{
mPosition = bytes_written;
success = TRUE;
}
} }
// Always update position after any write operation in case file pointer position changed
mPosition = ftell(mFile);
return success; return success;
} }
...@@ -236,20 +198,12 @@ S32 LLFileSystem::tell() const ...@@ -236,20 +198,12 @@ S32 LLFileSystem::tell() const
S32 LLFileSystem::getSize() S32 LLFileSystem::getSize()
{ {
S32 file_size = 0; S32 file_size = 0;
if (mFile)
{ llstat stat;
fseek(mFile, 0L, SEEK_END); if (LLFile::stat(mFilePath, &stat) == 0)
file_size = ftell(mFile); {
fseek(mFile, mPosition, SEEK_SET); file_size = stat.st_size;
} }
else
{
llstat stat;
if (LLFile::stat(mFilePath, &stat) == 0)
{
file_size = stat.st_size;
}
}
return file_size; return file_size;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment