Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        /// <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());
        }
Exemplo n.º 4
0
        /// <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();
            }
        }
Exemplo n.º 5
0
        /// <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);
                }
            }
        }
Exemplo n.º 6
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());
        }
Exemplo n.º 7
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);
        }