Elminate some copy/paste in FlotsamAssetCache.CheckFromFileCache() and use using() construct to ensure filestream is always closed

master-beforevarregion
Justin Clark-Casey (justincc) 2014-01-18 00:12:12 +00:00
parent 12bfce7b9f
commit 97fbb8ed45
1 changed files with 10 additions and 33 deletions

View File

@ -352,7 +352,6 @@ namespace OpenSim.Region.CoreModules.Asset
return false; return false;
} }
/// <summary> /// <summary>
/// Try to get an asset from the file cache. /// Try to get an asset from the file cache.
/// </summary> /// </summary>
@ -390,16 +389,17 @@ namespace OpenSim.Region.CoreModules.Asset
if (File.Exists(filename)) if (File.Exists(filename))
{ {
FileStream stream = null;
try try
{ {
stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read); using (FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
BinaryFormatter bformatter = new BinaryFormatter(); BinaryFormatter bformatter = new BinaryFormatter();
asset = (AssetBase)bformatter.Deserialize(stream); asset = (AssetBase)bformatter.Deserialize(stream);
m_DiskHits++; m_DiskHits++;
} }
}
catch (System.Runtime.Serialization.SerializationException e) catch (System.Runtime.Serialization.SerializationException e)
{ {
m_log.WarnFormat( m_log.WarnFormat(
@ -417,12 +417,6 @@ namespace OpenSim.Region.CoreModules.Asset
m_log.WarnFormat( m_log.WarnFormat(
"[FLOTSAM ASSET CACHE]: Failed to get file {0} for asset {1}. Exception {2} {3}", "[FLOTSAM ASSET CACHE]: Failed to get file {0} for asset {1}. Exception {2} {3}",
filename, id, e.Message, e.StackTrace); filename, id, e.Message, e.StackTrace);
}
finally
{
if (stream != null)
stream.Close();
} }
} }
@ -434,36 +428,19 @@ namespace OpenSim.Region.CoreModules.Asset
bool found = false; bool found = false;
string filename = GetFileName(id); string filename = GetFileName(id);
if (File.Exists(filename)) if (File.Exists(filename))
{ {
// actually check if we can open it, and so update expire
FileStream stream = null;
try try
{ {
stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read); using (FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
if (stream != null) if (stream != null)
{
found = true; found = true;
stream.Close();
} }
}
catch (System.Runtime.Serialization.SerializationException e)
{
found = false;
m_log.ErrorFormat(
"[FLOTSAM ASSET CACHE]: Failed to check file {0} for asset {1}. Exception {2} {3}",
filename, id, e.Message, e.StackTrace);
// If there was a problem deserializing the asset, the asset may
// either be corrupted OR was serialized under an old format
// {different version of AssetBase} -- we should attempt to
// delete it and re-cache
File.Delete(filename);
} }
catch (Exception e) catch (Exception e)
{ {
found = false;
m_log.ErrorFormat( m_log.ErrorFormat(
"[FLOTSAM ASSET CACHE]: Failed to check file {0} for asset {1}. Exception {2} {3}", "[FLOTSAM ASSET CACHE]: Failed to check file {0} for asset {1}. Exception {2} {3}",
filename, id, e.Message, e.StackTrace); filename, id, e.Message, e.StackTrace);