Skip to content

Commit ea705d0

Browse files
committed
Added Merge function
1 parent 02702e1 commit ea705d0

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

PassXYZLib/PxDatabase.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,34 @@ private void EnsureRecycleBin(ref PwGroup pgRecycleBin)
531531
else { Debug.Assert(pgRecycleBin.Uuid.Equals(this.RecycleBinUuid)); }
532532
}
533533

534+
/// <summary>
535+
/// Remove RecycleBin before merge. RecycleBin should be kept locally and should not be merged.
536+
/// </summary>
537+
/// <param name="pwDb"></param>
538+
/// <returns></returns>
539+
private bool DeleteRecycleBin(PwDatabase pwDb)
540+
{
541+
if (pwDb == null) { return false; }
542+
543+
PwGroup pgRecycleBin = pwDb.RootGroup.FindGroup(pwDb.RecycleBinUuid, true);
544+
545+
if (pgRecycleBin != null)
546+
{
547+
pwDb.RootGroup.Groups.Remove(pgRecycleBin);
548+
pgRecycleBin.DeleteAllObjects(pwDb);
549+
PwDeletedObject pdo = new PwDeletedObject(pgRecycleBin.Uuid, DateTime.UtcNow);
550+
pwDb.DeletedObjects.Add(pdo);
551+
Debug.WriteLine("DeleteRecycleBin successfully.");
552+
return true;
553+
}
554+
else
555+
{
556+
Debug.WriteLine("DeleteRecycleBin failure.");
557+
return false;
558+
}
559+
}
560+
561+
534562
/// <summary>
535563
/// Find an entry or a group.
536564
/// </summary>
@@ -966,6 +994,58 @@ orderby e.LastModificationTime descending
966994
return resultsList;
967995
}
968996

997+
public bool Merge(string path, PwMergeMethod mm)
998+
{
999+
var pwImp = new PwDatabase();
1000+
var ioInfo = IOConnectionInfo.FromPath(path);
1001+
1002+
var compositeKey = MasterKey;
1003+
1004+
KPCLibLogger swLogger = new KPCLibLogger();
1005+
try
1006+
{
1007+
swLogger.StartLogging("Merge: Opening database ...", true);
1008+
pwImp.Open(ioInfo, compositeKey, swLogger);
1009+
swLogger.EndLogging();
1010+
}
1011+
catch (Exception e)
1012+
{
1013+
Debug.WriteLine($"$Failed to open database: {e.Message}.");
1014+
return false;
1015+
}
1016+
1017+
// We only merge, if these are the same database with different versions.
1018+
if (RootGroup.EqualsGroup(pwImp.RootGroup, (PwCompareOptions.IgnoreLastBackup |
1019+
PwCompareOptions.IgnoreHistory | PwCompareOptions.IgnoreParentGroup |
1020+
PwCompareOptions.IgnoreTimes | PwCompareOptions.PropertiesOnly), MemProtCmpMode.None))
1021+
{
1022+
Debug.WriteLine($"Merge: Root group are the same. Merge method is {mm}.");
1023+
}
1024+
else
1025+
{
1026+
Debug.WriteLine($"Merge: Root group are different DBase={RootGroup}, pwImp={pwImp.RootGroup}.");
1027+
pwImp.Close();
1028+
return false;
1029+
}
1030+
1031+
try
1032+
{
1033+
// Need to remove RecycleBin first before merge.
1034+
DeleteRecycleBin(pwImp);
1035+
1036+
MergeIn(pwImp, mm, swLogger);
1037+
DescriptionChanged = DateTime.UtcNow;
1038+
Save(swLogger);
1039+
pwImp.Close();
1040+
}
1041+
catch (Exception exMerge)
1042+
{
1043+
Debug.WriteLine($"Merge failed {exMerge}");
1044+
return false;
1045+
}
1046+
return true;
1047+
}
1048+
9691049
// The end of PxDatabase
9701050
}
9711051

PassXYZLib/User.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,28 @@ public static string TmpFilePath
8787
{
8888
get
8989
{
90-
return System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "tmp");
90+
string tmpPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "tmp");
91+
if (!Directory.Exists(tmpPath))
92+
{
93+
Directory.CreateDirectory(tmpPath);
94+
}
95+
return tmpPath;
96+
}
97+
}
98+
99+
/// <summary>
100+
/// The backup file path.
101+
/// </summary>
102+
public static string BakFilePath
103+
{
104+
get
105+
{
106+
string bakPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "bak");
107+
if (!Directory.Exists(bakPath))
108+
{
109+
Directory.CreateDirectory(bakPath);
110+
}
111+
return bakPath;
91112
}
92113
}
93114

@@ -138,7 +159,7 @@ public class User
138159
/// This is because it is difficult to manage data file in mobile devices. The actual data file is encoded
139160
/// using base58 encoding with information such as key file or device lock enabled.
140161
/// </summary>
141-
virtual public string Username
162+
public virtual string Username
142163
{
143164
get => _username;
144165
set

0 commit comments

Comments
 (0)