Skip to content

Commit

Permalink
refactor Backup class
Browse files Browse the repository at this point in the history
  • Loading branch information
clown committed Apr 3, 2024
1 parent 58b0e06 commit f7e4637
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions Applications/Editor/Main/Sources/Models/Backup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Backup(SettingFolder settings)
{
_settings = settings;
var v = _settings.Value;
Logger.Debug($"Enable:{v.BackupEnabled}, Delete:{v.BackupAutoDelete}");
Logger.Debug($"Enable:{v.BackupEnabled}, Delete:{v.BackupAutoDelete}, Gen:{v.BackupDays}");

var src = _settings.Value.Backup;
var cvt = GetRootDirectory();
Expand Down Expand Up @@ -134,8 +134,8 @@ public void Invoke(Entity src)
var dest = Io.Combine(GetRootDirectory(), DateTime.Today.ToString("yyyyMMdd"), src.Name);
if (Io.Exists(dest)) return;

Logger.Debug($"[Backup] {dest.Quote()}");
Io.Copy(src.FullName, dest, false);
Logger.Debug($"[Backup] {dest.Quote()}");
}
catch (Exception err) { throw new BackupException(err); }
}
Expand All @@ -158,11 +158,15 @@ public void Cleanup()
{
try
{
if (!_settings.Value.BackupEnabled || !_settings.Value.BackupAutoDelete) return;
if (!_settings.Value.BackupEnabled || !_settings.Value.BackupAutoDelete)
{
Logger.Debug("[Clean] Skip");
return;
}

var src = Io.GetDirectories(GetRootDirectory()).ToList();
var cvt = src.Where(IsClean).ToList();
if (src.Count != cvt.Count) Logger.Warn($"Non-targeted folders detected ({cvt.Count}/{src.Count})");
var cvt = src.Where(IsBackupFolder).ToList();
if (src.Count != cvt.Count) Logger.Warn($"Folders:{cvt.Count}/{src.Count}");

var n = cvt.Count - _settings.Value.BackupDays;
if (n <= 0) return;
Expand All @@ -182,14 +186,14 @@ public void Cleanup()

/* --------------------------------------------------------------------- */
///
/// IsClean
/// IsBackupFolder
///
/// <summary>
/// Determines if the specified path is a target folder for cleaning up.
/// </summary>
///
/* --------------------------------------------------------------------- */
private static bool IsClean(string src)
private static bool IsBackupFolder(string src)
{
if (!src.HasValue()) return false;
if (!Io.IsDirectory(src)) return false;
Expand Down

0 comments on commit f7e4637

Please sign in to comment.