Skip to content

Commit

Permalink
#5 Fix removal of original file by cleanup
Browse files Browse the repository at this point in the history
- fixed search pattern for backup-files
- added additional check for original database path -> will
  not be deleted
  • Loading branch information
marvinweber committed Sep 4, 2019
1 parent bac0ca0 commit 68f1f27
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions KPSimpleBackup/KPSimpleBackup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void BackupAction(PwDatabase database)
String dateTimeFormat = this.m_config.DateFormat;
string time = DateTime.Now.ToString(dateTimeFormat);

// Save backup database for each specified path
// Save backup database for each specified path and perform cleanup
foreach (String backupFolderPath in paths)
{
string dbBackupFileName = this.GetBackupFileName(database);
Expand All @@ -127,7 +127,7 @@ private void BackupAction(PwDatabase database)
database.SaveAs(connection, false, null);

// Cleanup
this.Cleanup(backupFolderPath, dbBackupFileName);
this.Cleanup(backupFolderPath, dbBackupFileName, database.IOConnectionInfo.Path);
}
}

Expand All @@ -146,20 +146,26 @@ private String GetBackupFileName(PwDatabase database)
return backupFileName;
}

private void Cleanup(String path, String fileNamePrefix)
private void Cleanup(String path, String fileNamePrefix, String originalDatabasePath)
{
int filesToKeepAmount = (int)this.m_config.FileAmountToKeep;
// read from settings wether to use recycle bin or delete files permanently
int filesToKeepAmount = (int) this.m_config.FileAmountToKeep;
// read from settings whether to use recycle bin or delete files permanently
var recycleOption = this.m_config.UseRecycleBinDeletedBackups ? RecycleOption.SendToRecycleBin : RecycleOption.DeletePermanently;

String searchPattern = fileNamePrefix + "*" + ".kdbx";
String searchPattern = fileNamePrefix + "_*.kdbx";
String[] fileList = Directory.GetFiles(path, searchPattern).OrderBy(f => f).Reverse().ToArray();

// if more backup files available than needed loop through the unnecessary ones and remove them
if (fileList.Count() > filesToKeepAmount)
{
for (int i = filesToKeepAmount; i < fileList.Count(); i++)
{
// never delete original file -> always skip it (in case it made it in the filelist)
if (fileList[i].Equals(originalDatabasePath))
{
continue;
}

FileSystem.DeleteFile(fileList[i], UIOption.OnlyErrorDialogs, recycleOption);
}
}
Expand Down

0 comments on commit 68f1f27

Please sign in to comment.