From fec0e7768a4cc7282d6c0be52c91d369a01b9990 Mon Sep 17 00:00:00 2001 From: Philipp Crocoll Date: Mon, 30 Nov 2020 11:55:07 +0100 Subject: [PATCH] add more logging output to diagnose an issue describe by a user where merge conflicts seem to be detected even if they are not there --- src/Kp2aBusinessLogic/Io/CachingFileStorage.cs | 18 +++++++++++++----- .../database/SynchronizeCachedDatabase.cs | 6 +++++- src/Kp2aBusinessLogic/database/edit/SaveDB.cs | 16 ++++++++++------ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/Kp2aBusinessLogic/Io/CachingFileStorage.cs b/src/Kp2aBusinessLogic/Io/CachingFileStorage.cs index a3d29556..321d83bd 100644 --- a/src/Kp2aBusinessLogic/Io/CachingFileStorage.cs +++ b/src/Kp2aBusinessLogic/Io/CachingFileStorage.cs @@ -120,10 +120,14 @@ namespace keepass2android.Io public bool IsCached(IOConnectionInfo ioc) { - return File.Exists(CachedFilePath(ioc)) + bool result = File.Exists(CachedFilePath(ioc)) && File.Exists(VersionFilePath(ioc)) && File.Exists(BaseVersionFilePath(ioc)); - } + + Kp2aLog.Log(ioc.GetDisplayName() + " isCached = " + result); + + return result; + } public void Delete(IOConnectionInfo ioc) { @@ -593,11 +597,15 @@ namespace keepass2android.Io public string GetBaseVersionHash(IOConnectionInfo ioc) { - return File.ReadAllText(BaseVersionFilePath(ioc)); - } + string hash = File.ReadAllText(BaseVersionFilePath(ioc)); + Kp2aLog.Log(ioc.GetDisplayName() + " baseVersionHash = " + hash); + return hash; + } public string GetLocalVersionHash(IOConnectionInfo ioc) { - return File.ReadAllText(VersionFilePath(ioc)); + string hash = File.ReadAllText(VersionFilePath(ioc)); + Kp2aLog.Log(ioc.GetDisplayName() + " localVersionHash = " + hash); + return hash; } public bool HasLocalChanges(IOConnectionInfo ioc) { diff --git a/src/Kp2aBusinessLogic/database/SynchronizeCachedDatabase.cs b/src/Kp2aBusinessLogic/database/SynchronizeCachedDatabase.cs index e0f9a66c..5918c77f 100644 --- a/src/Kp2aBusinessLogic/database/SynchronizeCachedDatabase.cs +++ b/src/Kp2aBusinessLogic/database/SynchronizeCachedDatabase.cs @@ -43,17 +43,21 @@ namespace keepass2android try { remoteData = cachingFileStorage.GetRemoteDataAndHash(ioc, out hash); + Kp2aLog.Log("Checking for file change. Current hash = " + hash); } catch (FileNotFoundException) { StatusLogger.UpdateSubMessage(_app.GetResourceString(UiStringKey.RestoringRemoteFile)); cachingFileStorage.UpdateRemoteFile(ioc, _app.GetBooleanPreference(PreferenceKey.UseFileTransactions)); Finish(true, _app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully)); + Kp2aLog.Log("Checking for file change: file not found"); return; } //check if remote file was modified: - if (cachingFileStorage.GetBaseVersionHash(ioc) != hash) + var baseVersionHash = cachingFileStorage.GetBaseVersionHash(ioc); + Kp2aLog.Log("Checking for file change. baseVersionHash = " + baseVersionHash); + if (baseVersionHash != hash) { //remote file is modified if (cachingFileStorage.HasLocalChanges(ioc)) diff --git a/src/Kp2aBusinessLogic/database/edit/SaveDB.cs b/src/Kp2aBusinessLogic/database/edit/SaveDB.cs index 6bcbb11a..46fde8cb 100644 --- a/src/Kp2aBusinessLogic/database/edit/SaveDB.cs +++ b/src/Kp2aBusinessLogic/database/edit/SaveDB.cs @@ -116,14 +116,18 @@ namespace keepass2android return; } } - - if ( - (_streamForOrigFile != null) - || fileStorage.CheckForFileChangeFast(ioc, _db.LastFileVersion) //first try to use the fast change detection - || (FileHashChanged(ioc, _db.KpDatabase.HashOfFileOnDisk) == FileHashChange.Changed) //if that fails, hash the file and compare: - ) + + bool hasStreamForOrigFile = (_streamForOrigFile != null); + bool hasChangeFast = hasStreamForOrigFile || + fileStorage.CheckForFileChangeFast(ioc, _db.LastFileVersion); //first try to use the fast change detection; + bool hasHashChanged = hasChangeFast || + (FileHashChanged(ioc, _db.KpDatabase.HashOfFileOnDisk) == + FileHashChange.Changed); //if that fails, hash the file and compare: + + if (hasHashChanged) { + Kp2aLog.Log("Conflict. " + hasStreamForOrigFile + " " + hasChangeFast + " " + hasHashChanged); //ask user... _app.AskYesNoCancel(UiStringKey.TitleSyncQuestion, UiStringKey.MessageSyncQuestion,