private static void DeleteOldBackups( [NotNull] DirectoryInfo directory )
{
if ( directory == null )
throw new ArgumentNullException( "directory" );
var backupList = directory.GetFiles( "*.fcm" ).OrderBy( fi => -fi.CreationTimeUtc.Ticks ).ToList();
int maxFileCount = ConfigKey.MaxBackups.GetInt();
if ( maxFileCount > 0 ) {
while ( backupList.Count > maxFileCount ) {
FileInfo info = backupList[backupList.Count - 1];
backupList.RemoveAt( backupList.Count - 1 );
try {
File.Delete( info.FullName );
} catch ( Exception ex ) {
Logger.Log( LogType.Error,
"Map.SaveBackup: Error occurred while trying delete old backup \"{0}\": {1}",
info.FullName, ex );
break;
}
Logger.Log( LogType.SystemActivity,
"Map.SaveBackup: Deleted old backup \"{0}\"", info.Name );
}
}
int maxFileSize = ConfigKey.MaxBackupSize.GetInt();
if ( maxFileSize > 0 ) {
while ( true ) {
FileInfo[] fis = directory.GetFiles();
long size = fis.Sum( fi => fi.Length );
if ( size / 1024 / 1024 > maxFileSize ) {
FileInfo info = backupList[backupList.Count - 1];
backupList.RemoveAt( backupList.Count - 1 );
try {
File.Delete( info.FullName );
} catch ( Exception ex ) {
Logger.Log( LogType.Error,
"Map.SaveBackup: Error occurred while trying delete old backup \"{0}\": {1}",
info.Name, ex );
break;
}
Logger.Log( LogType.SystemActivity,
"Map.SaveBackup: Deleted old backup \"{0}\"", info.Name );
} else {
break;
}
}
}
}