private void UpdateDestinationIndexWithDeletedPaths(IDictionary <string, BackupFileInfo> sourceIndex, IDictionary <string, BackupFileInfo> destinationIndex, string hostName) { foreach (var destinationFileInfo in destinationIndex.Values) { BackupFileInfo sourceFileInfo = null; if (sourceIndex.TryGetValue(destinationFileInfo.FileId, out sourceFileInfo) == true) { // If the destination exists in source, verify all paths exist foreach (var destinationPath in destinationFileInfo.Paths.Where(p => string.Equals(p.Host, hostName, StringComparison.InvariantCultureIgnoreCase))) { if (sourceFileInfo.Paths.Any(sourcePath => string.Equals(sourcePath.Host, destinationPath.Host, StringComparison.InvariantCultureIgnoreCase) && string.Equals(sourcePath.RootPath, destinationPath.RootPath, StringComparison.InvariantCultureIgnoreCase) && string.Equals(sourcePath.RelativePath, destinationPath.RelativePath)) == false) { destinationPath.DeletedDate = DateTime.UtcNow; } } } else { // Mark as deleted if the file is not found foreach (var destinationPath in destinationFileInfo.Paths.Where(p => string.Equals(p.Host, hostName, StringComparison.InvariantCultureIgnoreCase))) { destinationPath.DeletedDate = DateTime.UtcNow; } } } }
public void RestoreBackup(BackupFileInfo backupFileInfo) { if (!File.Exists(backupFileInfo.FullName)) { throw new Exception("Backup file with name: " + backupFileInfo.Name + " does not exist!"); } string pathToDatabaseFile = GetFullPathToDatabaseFile(); if (File.Exists(pathToDatabaseFile)) { File.Delete(pathToDatabaseFile); } File.Copy(backupFileInfo.FullName, pathToDatabaseFile); File.Delete(backupFileInfo.FullName); }
public IDictionary <string, BackupFileInfo> GetFileIndex() { IDictionary <string, BackupFileInfo> result = new Dictionary <string, BackupFileInfo>(); foreach (var directoryInfo in _directoriesInfo) { string host = Environment.MachineName; string rootPath = directoryInfo.FullName; int rootLength = rootPath.Length + 1; foreach (var file in directoryInfo.GetFiles("*", SearchOption.AllDirectories)) { string relativePath = file.FullName.Substring(rootLength); PathInfo filePathInfo = new PathInfo { Host = host, RootPath = rootPath, RelativePath = relativePath, AddedDateTime = DateTime.UtcNow, DeletedDate = null }; BackupFileInfo backupFileInfo = new BackupFileInfo(); using (var fileStream = file.OpenRead()) { backupFileInfo.FileId = _fileHasher.CalculateHash(fileStream); } if (result.ContainsKey(backupFileInfo.FileId)) { backupFileInfo = result[backupFileInfo.FileId]; } else { backupFileInfo.Paths = new List <PathInfo>(); result[backupFileInfo.FileId] = backupFileInfo; } backupFileInfo.Paths.Add(filePathInfo); } } return(result); }
public void Page_Load() { string skin = _Request.Get("skin"); string file = FilePath; string version = _Request.Get("history"); if (skin != null && file != null) { if (skin.IndexOf("..") >= 0 || skin.IndexOf('/') >= 0 || skin.IndexOf('\\') >= 0 || file.IndexOf("..") >= 0 || (StringUtil.EqualsIgnoreCase(file, "\\_skin.config") == false && false == ( file.EndsWith(".ascx", StringComparison.OrdinalIgnoreCase) || file.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase) || file.EndsWith(".html", StringComparison.OrdinalIgnoreCase) || file.EndsWith(".js", StringComparison.OrdinalIgnoreCase) || file.EndsWith(".css", StringComparison.OrdinalIgnoreCase) ) )) { ShowError("关键参数不在正常范围内"); } string path = IOUtil.JoinPath(Globals.GetPath(SystemDirecotry.Skins), skin, file); if (File.Exists(path)) { string skinRoot = IOUtil.JoinPath(Globals.GetPath(SystemDirecotry.Skins), skin); string historyDir = GetBackupFileDir(skin, file); m_BackupFileList = GetBackupFileInfos(historyDir); if (_Request.IsClick("save")) { FileInfo fileInfo = new FileInfo(path); if (fileInfo.IsReadOnly) { fileInfo.IsReadOnly = false; } if (Directory.Exists(historyDir) == false) { Directory.CreateDirectory(historyDir); } if (m_BackupFileList.Length == 0) { File.Copy(path, IOUtil.JoinPath(historyDir, "1.config")); } else { File.Copy(path, IOUtil.JoinPath(historyDir, (m_BackupFileList[0].Version + 1) + ".config")); } m_FileContent = _Request.Get("FileContent", Method.Post, m_FileContent, false); File.WriteAllText(path, m_FileContent, System.Text.Encoding.UTF8); m_FileContent = StringUtil.HtmlEncode(m_FileContent); BbsRouter.JumpToCurrentUrl("skin=" + skin + "&file=" + FilePath); } m_FileContent = File.ReadAllText(path, System.Text.Encoding.Default); m_FileContent = StringUtil.HtmlEncode(m_FileContent); if (version != null) { if (version.IndexOf(',') < 0) { int ver2 = int.Parse(version); BackupFileInfo file2 = null; foreach (BackupFileInfo backupFile in m_BackupFileList) { if (backupFile.Version == ver2) { file2 = backupFile; break; } } m_FileContent2 = File.ReadAllText(file2.FullName, System.Text.Encoding.Default); m_FileContent2 = StringUtil.HtmlEncode(m_FileContent2); } else { string[] vers = version.Split(','); int ver1 = int.Parse(vers[0]); int ver2 = int.Parse(vers[1]); BackupFileInfo file1 = null; BackupFileInfo file2 = null; foreach (BackupFileInfo backupFile in m_BackupFileList) { if (backupFile.Version == ver1) { file1 = backupFile; } else if (backupFile.Version == ver2) { file2 = backupFile; } if (file1 != null && file2 != null) { break; } } m_FileContent = File.ReadAllText(file1.FullName, System.Text.Encoding.Default); m_FileContent = StringUtil.HtmlEncode(m_FileContent); m_FileContent2 = File.ReadAllText(file2.FullName, System.Text.Encoding.Default); m_FileContent2 = StringUtil.HtmlEncode(m_FileContent2); } } } } }
public override Missing Handle(FileReplaceArguments arguments) { FileEntry sourceFile = ResolveExistingFile(arguments.SourcePath); AssertNoDuplicatePaths(arguments); AssertAllOnSameDrive(arguments); FileEntry destinationFile = ResolveExistingFile(arguments.DestinationPath); bool destinationHasOnlyArchiveAttribute = destinationFile.Attributes == FileAttributes.Archive; NotifySourceFileChanged(sourceFile, destinationFile); NotifyDirectoriesChanged(false, destinationFile.Parent, sourceFile.Parent); BackupFileInfo backupFileInfo = BackupFileInfo.FromPath(arguments.BackupDestinationPath, Container); if (backupFileInfo != null && !backupFileInfo.Exists) { string destinationFileName = destinationFile.Name; DirectoryEntry destinationDirectory = destinationFile.Parent; MoveSingleFile(destinationFile, backupFileInfo.Directory, backupFileInfo.FileName, destinationFile.PathFormatter, false); if (destinationDirectory != backupFileInfo.Directory) { NotifyDirectoriesChanged(false, destinationDirectory); } FileEntry backupFile = backupFileInfo.GetExistingFile(); TransferSourceContentsToDestinationFile(sourceFile, destinationDirectory, destinationFileName, backupFile); } else { var spaceTracker = new VolumeSpaceTracker(sourceFile.Parent.Root); if (backupFileInfo == null) { FileEntry tempBackupFile = CreateTempBackupFile(destinationFile); NotifyDirectoriesChanged(false, tempBackupFile.Parent); backupFileInfo = BackupFileInfo.FromTempFile(tempBackupFile, Container); } FileEntry backupFile = backupFileInfo.GetExistingFile(); if (backupFile.Parent != destinationFile.Parent && backupFile.Parent != sourceFile.Parent) { NotifyDirectoriesChanged(false, backupFile.Parent); } TransferDestinationContentsToExistingBackupFile(destinationFile, backupFile, spaceTracker); destinationFile.Parent.DeleteFile(destinationFile.Name, true); Container.ChangeTracker.NotifyContentsAccessed(backupFile.PathFormatter, FileAccessKinds.All); NotifyDirectoriesChanged(backupFileInfo.IsTempFile, backupFile.Parent, destinationFile.Parent); TransferSourceContentsToDestinationFile(sourceFile, destinationFile.Parent, destinationFile.Name, destinationFile); spaceTracker.ApplyVolumeSpaceChange(); } backupFileInfo.Complete(); if (!destinationHasOnlyArchiveAttribute) { Container.ChangeTracker.NotifyContentsAccessed(backupFileInfo.Path.Formatter, FileAccessKinds.Attributes); Container.ChangeTracker.NotifyContentsAccessed(arguments.DestinationPath.Formatter, FileAccessKinds.Attributes); } return(Missing.Value); }