public void CreateDownloadCacheFileInfo([Values(true, false)] bool extendedAttributesAvailable) { Guid? uuid = extendedAttributesAvailable ? (Guid?)Guid.NewGuid() : null; string fileName = "file"; IFileInfo file = Mock.Of <IFileInfo>( f => f.Uuid == uuid && f.Exists == true && f.FullName == Path.Combine(Path.GetTempPath(), Path.GetRandomFileName(), fileName) && f.Name == fileName); var cacheFile = Factory.CreateDownloadCacheFileInfo(file); if (extendedAttributesAvailable) { // Ensure that the path does not maps to temp path to avoid problems with extended attribute support Assert.That(cacheFile.FullName.Contains(Path.GetTempPath()), Is.False); Assert.That(cacheFile.Name, Is.EqualTo(uuid.ToString() + ".sync")); } else { Assert.That(cacheFile.FullName, Is.EqualTo(file.FullName + ".sync")); Assert.That(cacheFile.Name, Is.EqualTo(file.Name + ".sync")); } }
public void CreateDownloadCacheIfExtendedAttributesAreAvailable() { Guid uuid = Guid.NewGuid(); var file = Mock.Of <IFileInfo>( f => f.Uuid == uuid && f.Exists == true); var cacheFile = Factory.CreateDownloadCacheFileInfo(file); Assert.That(cacheFile.Name, Is.EqualTo(uuid.ToString() + ".sync")); // Ensure that the path does not maps to temp path to avoid problems with extended attribute support Assert.That(cacheFile.FullName.Contains(Path.GetTempPath()), Is.False); }
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; }