public IReadOnlyList <RenamePair> GetRenamePairs(FileInfo file, string newName) { Ensure.NotNull(file, nameof(file)); Ensure.NotNullOrEmpty(newName, nameof(newName)); var pairs = new List <RenamePair>(); var soft = file.GetSoftDeleteFileFor(); if (soft.Exists) { var withNewName = soft.WithNewName(newName, new TempFileSettings(file.DirectoryName, file.Extension)); pairs.Add(new RenamePair(soft, withNewName)); } var allBackups = BackupFile.GetAllBackupsFor(file, this.Setting); foreach (var backup in allBackups) { var withNewName = backup.File.WithNewName(newName, this.Setting); pairs.Add(new RenamePair(backup.File, withNewName)); soft = backup.File.GetSoftDeleteFileFor(); if (soft.Exists) { withNewName = soft.WithNewName(newName, this.Setting); pairs.Add(new RenamePair(soft, withNewName)); } } return(pairs); }
/// <inheritdoc/> public bool CanRename(FileInfo file, string newName) { Ensure.NotNull(file, nameof(file)); Ensure.NotNullOrEmpty(newName, nameof(newName)); var soft = file.GetSoftDeleteFileFor(); if (soft.Exists) { if (!soft.CanRename(newName, this.Setting)) { return(false); } } var allBackups = BackupFile.GetAllBackupsFor(file, this.Setting); foreach (var backup in allBackups) { if (!backup.File.CanRename(newName, this.Setting)) { return(false); } soft = backup.File.GetSoftDeleteFileFor(); if (soft.Exists) { if (!soft.CanRename(newName, this.Setting)) { return(false); } } } return(true); }
/// <inheritdoc/> public bool CanRestore(FileInfo file) { var softDelete = file.GetSoftDeleteFileFor(); if (softDelete.Exists) { return(true); } var backups = BackupFile.GetAllBackupsFor(file, this.Setting); return(backups.Any()); }
/// <inheritdoc/> public void DeleteBackups(FileInfo file) { var soft = file.GetSoftDeleteFileFor(); soft?.Delete(); var allBackups = BackupFile.GetAllBackupsFor(file, this.Setting); foreach (var backup in allBackups) { backup.File?.Delete(); } }
/// <inheritdoc/> public virtual void AfterSave(LockedFile file) { if (file is null) { throw new ArgumentNullException(nameof(file)); } Ensure.ExtensionIsNotAnyOf(file.File, this.BackupExtensions, "file"); file.File.DeleteSoftDeleteFileFor(); var allBackups = BackupFile.GetAllBackupsFor(file.File, this.Setting); if (allBackups.Count == 0) { return; } foreach (var backup in allBackups) { backup.File.DeleteSoftDeleteFileFor(); } if (this.Setting.NumberOfBackups > 0) { // this is not efficient but the number of backups should be low while (allBackups.Count > this.Setting.NumberOfBackups) { var backupToPurge = allBackups.MinBy(x => x.TimeStamp); backupToPurge.File.HardDelete(); allBackups.Remove(backupToPurge); } } if (this.Setting.MaxAgeInDays > 0 && this.Setting.MaxAgeInDays < int.MaxValue) { // this is not efficient but the number of backups should be low while (true) { var backupToBurge = allBackups.MinBy(x => x.TimeStamp); var days = (DateTime.Now - backupToBurge.TimeStamp).Days; if (days < this.Setting.MaxAgeInDays) { break; } backupToBurge.File.HardDelete(); allBackups.Remove(backupToBurge); } } }
/// <inheritdoc/> public bool CanRestore(FileInfo file) { if (file is null) { throw new ArgumentNullException(nameof(file)); } var softDelete = file.SoftDeleteFile(); if (softDelete.Exists) { return(true); } var backups = BackupFile.GetAllBackupsFor(file, this.Setting); return(backups.Any()); }
/// <inheritdoc/> public bool CanRename(FileInfo file, string newName) { if (file is null) { throw new ArgumentNullException(nameof(file)); } if (string.IsNullOrEmpty(newName)) { throw new ArgumentNullException(nameof(newName)); } var soft = file.SoftDeleteFile(); if (soft.Exists) { if (!soft.CanRename(newName, this.Setting)) { return(false); } } var allBackups = BackupFile.GetAllBackupsFor(file, this.Setting); foreach (var backup in allBackups) { if (!backup.File.CanRename(newName, this.Setting)) { return(false); } soft = backup.File.SoftDeleteFile(); if (soft.Exists) { if (!soft.CanRename(newName, this.Setting)) { return(false); } } } return(true); }