Beispiel #1
0
        /// <inheritdoc/>
        public virtual void AfterSave(LockedFile file)
        {
            Ensure.NotNull(file, 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 backupToBurge = allBackups.MinBy(x => x.TimeStamp);
                    backupToBurge.File.HardDelete();
                    allBackups.Remove(backupToBurge);
                }
            }

            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);
                }
            }
        }
Beispiel #2
0
        /// <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());
        }
Beispiel #3
0
        /// <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);
        }
Beispiel #4
0
        // ReSharper disable once UnusedMember.Global
        internal virtual void Restore(FileInfo file)
        {
            Ensure.NotNull(file, nameof(file));
            Ensure.ExtensionIsNotAnyOf(file, this.BackupExtensions, "file");

            var softDelete = file.WithAppendedExtension(FileHelper.SoftDeleteExtension);

            if (softDelete.Exists)
            {
                this.Restore(file, softDelete);
                return;
            }

            var backup = BackupFile.GetRestoreFileFor(file, this.Setting);

            if (backup != null)
            {
                this.Restore(file, backup);
            }
        }
Beispiel #5
0
        /// <inheritdoc />
        public IReadOnlyList <RenamePair> GetRenamePairs(FileInfo file, string newName)
        {
            if (file is null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (string.IsNullOrEmpty(newName))
            {
                throw new ArgumentNullException(nameof(newName));
            }

            var pairs = new List <RenamePair>();
            var soft  = file.SoftDeleteFile();

            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.SoftDeleteFile();
                if (soft.Exists)
                {
                    withNewName = soft.WithNewName(newName, this.Setting);
                    pairs.Add(new RenamePair(soft, withNewName));
                }
            }

            return(pairs);
        }