async Task TryMoveOldCopyToHistory(string backupDir, string sourceFile, byte[] destContent) { if (_historyProvider == null) { return; } var historyDirName = _historyProvider.ConvertFileNameToHistoryDirectoryName(sourceFile); var historyDirPath = _destination.CombinePath(backupDir, historyDirName); if (await _destination.IsDirectoryExists(historyDirPath)) { await TryCleanupHistoryDirectory(historyDirPath); } else { await _destination.CreateDirectory(historyDirPath); } var nameInHistory = _historyProvider.ConvertFileNameToVersionedFileName(sourceFile); var pathInHistory = _destination.CombinePath(historyDirPath, nameInHistory); _logger?.LogDebug($"TryMoveOldCopyToHistory('{backupDir}', '{sourceFile}'): create file in history: '{pathInHistory}'"); await _destination.CreateFile(pathInHistory, destContent); _changeValidator?.OnFileChanged(pathInHistory); }
public async void FilesInDirectoryBackedUpWithHistory() { var sourceDir = "source"; var backupDir = "backup"; var fileToBackup = "file"; var firstContent = new byte[0]; var secondContent = new byte[] { 42 }; // Ordinary case await _fs.CreateDirectory(sourceDir); await _fs.CreateFile(_fs.CombinePath(sourceDir, fileToBackup), firstContent); await _backup.Dump(sourceDir, backupDir); // Modify file await _fs.DeleteFile(fileToBackup); await _fs.CreateFile(fileToBackup, secondContent); await _backup.Dump(sourceDir, backupDir); // Latest file must exists Assert.True(await _fs.IsFileExists(_fs.CombinePath(backupDir, sourceDir, fileToBackup))); Assert.Equal(secondContent, await _fs.ReadAllBytes(fileToBackup)); // First file must exists in history var historyDirName = _history.ConvertFileNameToHistoryDirectoryName(fileToBackup); var historyDirPath = _fs.CombinePath(backupDir, sourceDir, historyDirName); Assert.True(await _fs.IsDirectoryExists(historyDirPath)); var filesInside = (await _fs.GetFiles(historyDirPath)).ToList(); Assert.True(filesInside.Count == 1); var fileName = filesInside[0]; var contentInHistory = await _fs.ReadAllBytes(_fs.CombinePath(backupDir, sourceDir, historyDirName, fileName)); Assert.Equal(firstContent, contentInHistory); }