public void OpenFile() { var file = Factory.CreateFileInfo(Path.Combine(this.longPath, this.longName)); using (file.Open(FileMode.CreateNew)) { } using (file.Open(FileMode.Open)) { } using (file.Open(FileMode.Truncate)) { } }
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); }
public void FileInfoConstruction() { string fileName = "test1"; string fullPath = Path.Combine(this.testFolder.FullName, fileName); IFileInfo fileInfo = Factory.CreateFileInfo(fullPath); Assert.That(fileInfo, Is.Not.Null); }
private bool LoadCacheFile(IFileInfo target, IDocument remoteDocument, IFileSystemInfoFactory fsFactory) { if (this.TransmissionStorage == null) { return(false); } IFileTransmissionObject obj = this.TransmissionStorage.GetObjectByRemoteObjectId(remoteDocument.Id); if (obj == null) { return(false); } IFileInfo localFile = fsFactory.CreateFileInfo(obj.LocalPath); if (!localFile.Exists) { return(false); } if (obj.LastChangeToken != remoteDocument.ChangeToken || localFile.Length != obj.LastContentSize) { localFile.Delete(); return(false); } try { byte[] localHash; using (var f = localFile.Open(FileMode.Open, FileAccess.Read, FileShare.None)) { localHash = SHA1Managed.Create().ComputeHash(f); } if (!localHash.SequenceEqual(obj.LastChecksum)) { localFile.Delete(); return(false); } if (target.FullName != obj.LocalPath) { if (target.Exists) { Guid?uuid = target.Uuid; if (uuid != null) { localFile.Uuid = uuid; } target.Delete(); } localFile.MoveTo(target.FullName); target.Refresh(); } return(true); } catch (Exception) { localFile.Delete(); return(false); } }
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; }
private bool LoadCacheFile(IFileInfo target, IDocument remoteDocument, IFileSystemInfoFactory fsFactory) { if (this.TransmissionStorage == null) { return false; } IFileTransmissionObject obj = this.TransmissionStorage.GetObjectByRemoteObjectId(remoteDocument.Id); if (obj == null) { return false; } IFileInfo localFile = fsFactory.CreateFileInfo(obj.LocalPath); if (!localFile.Exists) { return false; } if (obj.LastChangeToken != remoteDocument.ChangeToken || localFile.Length != obj.LastContentSize) { localFile.Delete(); return false; } try { byte[] localHash; using (var f = localFile.Open(FileMode.Open, FileAccess.Read, FileShare.None)) { localHash = SHA1Managed.Create().ComputeHash(f); } if (!localHash.SequenceEqual(obj.LastChecksum)) { localFile.Delete(); return false; } if (target.FullName != obj.LocalPath) { if (target.Exists) { Guid? uuid = target.Uuid; if (uuid != null) { localFile.Uuid = uuid; } target.Delete(); } localFile.MoveTo(target.FullName); target.Refresh(); } return true; } catch (Exception) { localFile.Delete(); return false; } }