public void CreatesFirstConflictFile() { string fileName = "test1.txt"; string fullPath = Path.Combine(this.testFolder.FullName, fileName); var fileInfo = Factory.CreateFileInfo(fullPath); using (new FileInfo(fullPath).Create()) { } fileInfo = Factory.CreateFileInfo(fullPath); var conflictFile = Factory.CreateConflictFileInfo(fileInfo); Assert.That(conflictFile.Exists, Is.False); Assert.That(conflictFile.Directory.FullName, Is.EqualTo(fileInfo.Directory.FullName)); Assert.That(Path.GetExtension(conflictFile.FullName), Is.EqualTo(Path.GetExtension(fileInfo.FullName)), "The file extension must be kept the same as in the original file"); Assert.That(conflictFile.Name, Is.Not.EqualTo(fileInfo.Name)); }
protected byte[] DownloadChanges(IFileInfo target, IDocument remoteDocument, IMappedObject obj, IFileSystemInfoFactory fsFactory, ITransmissionManager transmissionManager, ILog logger) { // Download changes byte[] hash = null; var cacheFile = fsFactory.CreateDownloadCacheFileInfo(target); var transmission = transmissionManager.CreateTransmission(TransmissionType.DOWNLOAD_MODIFIED_FILE, target.FullName, cacheFile.FullName); hash = this.DownloadCacheFile(cacheFile, remoteDocument, transmission, fsFactory); obj.ChecksumAlgorithmName = "SHA-1"; try { var backupFile = fsFactory.CreateFileInfo(target.FullName + ".bak.sync"); Guid?uuid = target.Uuid; cacheFile.Replace(target, backupFile, true); try { target.Uuid = uuid; } catch (RestoreModificationDateException e) { logger.Debug("Failed to restore modification date of original file", e); } try { backupFile.Uuid = null; } catch (RestoreModificationDateException e) { logger.Debug("Failed to restore modification date of backup file", e); } byte[] checksumOfOldFile = null; using (var oldFileStream = backupFile.Open(FileMode.Open, FileAccess.Read, FileShare.None)) { checksumOfOldFile = SHA1Managed.Create().ComputeHash(oldFileStream); } if (!obj.LastChecksum.SequenceEqual(checksumOfOldFile)) { var conflictFile = fsFactory.CreateConflictFileInfo(target); backupFile.MoveTo(conflictFile.FullName); OperationsLogger.Info(string.Format("Updated local content of \"{0}\" with content of remote document {1} and created conflict file {2}", target.FullName, remoteDocument.Id, conflictFile.FullName)); } else { backupFile.Delete(); OperationsLogger.Info(string.Format("Updated local content of \"{0}\" with content of remote document {1}", target.FullName, remoteDocument.Id)); } } catch (Exception ex) { transmission.FailedException = ex; throw; } transmission.Status = TransmissionStatus.FINISHED; return(hash); }
protected static byte[] DownloadChanges(IFileInfo target, IDocument remoteDocument, IMappedObject obj, IFileSystemInfoFactory fsFactory, ActiveActivitiesManager transmissonManager, ILog logger) { // Download changes byte[] lastChecksum = obj.LastChecksum; byte[] hash = null; var cacheFile = fsFactory.CreateDownloadCacheFileInfo(target); var transmissionEvent = new FileTransmissionEvent(FileTransmissionType.DOWNLOAD_MODIFIED_FILE, target.FullName, cacheFile.FullName); transmissonManager.AddTransmission(transmissionEvent); try { using (SHA1 hashAlg = new SHA1Managed()) { using (var filestream = cacheFile.Open(FileMode.Create, FileAccess.Write, FileShare.None)) using (IFileDownloader download = ContentTaskUtils.CreateDownloader()) { download.DownloadFile(remoteDocument, filestream, transmissionEvent, hashAlg); obj.ChecksumAlgorithmName = "SHA-1"; hash = hashAlg.Hash; } } var backupFile = fsFactory.CreateFileInfo(target.FullName + ".bak.sync"); Guid?uuid = target.Uuid; cacheFile.Replace(target, backupFile, true); try { target.Uuid = uuid; } catch (RestoreModificationDateException e) { logger.Debug("Failed to restore modification date of original file", e); } try { backupFile.Uuid = null; } catch (RestoreModificationDateException e) { logger.Debug("Failed to restore modification date of backup file", e); } byte[] checksumOfOldFile = null; using (var oldFileStream = backupFile.Open(FileMode.Open, FileAccess.Read, FileShare.None)) { checksumOfOldFile = SHA1Managed.Create().ComputeHash(oldFileStream); } if (!lastChecksum.SequenceEqual(checksumOfOldFile)) { var conflictFile = fsFactory.CreateConflictFileInfo(target); backupFile.MoveTo(conflictFile.FullName); OperationsLogger.Info(string.Format("Updated local content of \"{0}\" with content of remote document {1} and created conflict file {2}", target.FullName, remoteDocument.Id, conflictFile.FullName)); } else { backupFile.Delete(); OperationsLogger.Info(string.Format("Updated local content of \"{0}\" with content of remote document {1}", target.FullName, remoteDocument.Id)); } } catch (Exception ex) { transmissionEvent.ReportProgress(new TransmissionProgressEventArgs { FailedException = ex }); throw; } transmissionEvent.ReportProgress(new TransmissionProgressEventArgs { Completed = true }); return(hash); }
protected byte[] DownloadChanges(IFileInfo target, IDocument remoteDocument, IMappedObject obj, IFileSystemInfoFactory fsFactory, ITransmissionManager transmissionManager, ILog logger) { // Download changes byte[] hash = null; var cacheFile = fsFactory.CreateDownloadCacheFileInfo(target); var transmission = transmissionManager.CreateTransmission(TransmissionType.DOWNLOAD_MODIFIED_FILE, target.FullName, cacheFile.FullName); hash = this.DownloadCacheFile(cacheFile, remoteDocument, transmission, fsFactory); obj.ChecksumAlgorithmName = "SHA-1"; try { var backupFile = fsFactory.CreateFileInfo(target.FullName + ".bak.sync"); Guid? uuid = target.Uuid; cacheFile.Replace(target, backupFile, true); try { target.Uuid = uuid; } catch (RestoreModificationDateException e) { logger.Debug("Failed to restore modification date of original file", e); } try { backupFile.Uuid = null; } catch (RestoreModificationDateException e) { logger.Debug("Failed to restore modification date of backup file", e); } byte[] checksumOfOldFile = null; using (var oldFileStream = backupFile.Open(FileMode.Open, FileAccess.Read, FileShare.None)) { checksumOfOldFile = SHA1Managed.Create().ComputeHash(oldFileStream); } if (!obj.LastChecksum.SequenceEqual(checksumOfOldFile)) { var conflictFile = fsFactory.CreateConflictFileInfo(target); backupFile.MoveTo(conflictFile.FullName); OperationsLogger.Info(string.Format("Updated local content of \"{0}\" with content of remote document {1} and created conflict file {2}", target.FullName, remoteDocument.Id, conflictFile.FullName)); } else { backupFile.Delete(); OperationsLogger.Info(string.Format("Updated local content of \"{0}\" with content of remote document {1}", target.FullName, remoteDocument.Id)); } } catch(Exception ex) { transmission.FailedException = ex; throw; } transmission.Status = TransmissionStatus.FINISHED; return hash; }