use internal directory for offline caching. this reduces the likelihood of data loss. Users need to disable and then re-enable file caching to make sure all files are cached in the new directory. closes #83 https://github.com/PhilippC/keepass2android/issues/83

This commit is contained in:
Philipp Crocoll
2018-09-18 04:28:45 +02:00
parent 6e225808a9
commit cd189e01dc
4 changed files with 28 additions and 16 deletions

View File

@@ -255,7 +255,8 @@ namespace keepass2android.Io
if (ioc.IsLocalFile())
{
bool requiresPermission = !(ioc.Path.StartsWith(activity.Activity.FilesDir.CanonicalPath)
|| ioc.Path.StartsWith(IoUtil.GetInternalDirectory(activity.Activity).CanonicalPath));
|| ioc.Path.StartsWith(IoUtil.GetInternalDirectory(activity.Activity).CanonicalPath)
|| ioc.Path.StartsWith(IoUtil.GetInternalDirectory(activity.Activity).CanonicalPath));
var extDirectory = activity.Activity.GetExternalFilesDir(null);
if ((extDirectory != null) && (ioc.Path.StartsWith(extDirectory.CanonicalPath)))

View File

@@ -65,22 +65,28 @@ namespace keepass2android.Io
protected readonly OfflineSwitchableFileStorage _cachedStorage;
private readonly ICacheSupervisor _cacheSupervisor;
private readonly string _streamCacheDir;
private readonly string _legacyCacheDir;
private readonly string _cacheDir;
public CachingFileStorage(IFileStorage cachedStorage, string cacheDir, ICacheSupervisor cacheSupervisor)
public CachingFileStorage(IFileStorage cachedStorage, Context cacheDirContext, ICacheSupervisor cacheSupervisor)
{
_cachedStorage = new OfflineSwitchableFileStorage(cachedStorage);
_cacheSupervisor = cacheSupervisor;
_streamCacheDir = cacheDir + Java.IO.File.Separator + "OfflineCache" + Java.IO.File.Separator;
if (!Directory.Exists(_streamCacheDir))
Directory.CreateDirectory(_streamCacheDir);
}
_legacyCacheDir = cacheDirContext.CacheDir.Path + Java.IO.File.Separator + "OfflineCache" + Java.IO.File.Separator;
if (!Directory.Exists(_legacyCacheDir))
Directory.CreateDirectory(_legacyCacheDir);
_cacheDir = IoUtil.GetInternalDirectory(cacheDirContext).Path + Java.IO.File.Separator + "OfflineCache" + Java.IO.File.Separator;
if (!Directory.Exists(_cacheDir))
Directory.CreateDirectory(_cacheDir);
}
public void ClearCache()
{
IoUtil.DeleteDir(new Java.IO.File(_streamCacheDir), true);
}
IoUtil.DeleteDir(new Java.IO.File(_legacyCacheDir), true);
IoUtil.DeleteDir(new Java.IO.File(_cacheDir), true);
}
public IEnumerable<string> SupportedProtocols { get { return _cachedStorage.SupportedProtocols; } }
@@ -105,7 +111,11 @@ namespace keepass2android.Io
{
SHA256Managed sha256 = new SHA256Managed();
string iocAsHexString = MemUtil.ByteArrayToHexString(sha256.ComputeHash(Encoding.Unicode.GetBytes(ioc.Path.ToCharArray())))+".cache";
return _streamCacheDir + iocAsHexString;
if (File.Exists(_legacyCacheDir + iocAsHexString))
return _legacyCacheDir + iocAsHexString;
return _cacheDir + iocAsHexString;
}
public bool IsCached(IOConnectionInfo ioc)