private bool CopyFileToDestination(object context) { var fileCopyInfo = (FileCopyInfo)context; FabricFile.Copy(fileCopyInfo.Source, fileCopyInfo.Destination, true); return(true); }
public void Copy(string targetDirectory, bool overwrite) { var targetPath = Path.Combine(targetDirectory, this.FileName); if (!FabricFile.Exists(this.FilePath)) { throw new InvalidOperationException(string.Format("Checkpoint file not found at path {0}", this.FilePath)); } FabricFile.Copy(this.FilePath, targetPath, overwrite); }
internal static void CreateHardLinkOrCopyFile(string sourceFileName, string destinationFileName) { if (!FabricFile.Exists(sourceFileName)) { return; } // Since the source file won't be changed, we can simply create a hardlink if (!FabricFile.CreateHardLink(destinationFileName, sourceFileName)) { // Hardlink can fail, e.g. if the destination is on another drive or the drive isn't NTFS. // Fallback to a full file copy. FabricFile.Copy(sourceFileName, destinationFileName, overwrite: true); } }
public void FabricFile_CopyNegative() { try { var src = Environment.ExpandEnvironmentVariables(this.srcPath); LogHelper.Log("FabricFile.Copy from {0} to {1}", src, this.badPath); FabricFile.Copy(src, this.badPath, true); Assert.Fail("should never reach here"); } catch (Exception e) { LogHelper.Log("caught exception {0}", e); Assert.IsTrue(e is IOException); } }
public void Copy(string targetDirectory, bool overwrite) { var sourcePath = Path.Combine(this.directory, this.FileName); var targetPath = Path.Combine(targetDirectory, this.FileName); if (!FabricFile.Exists(sourcePath)) { throw new ArgumentException(string.Format("Checkpoint Manager file not found in directory {0}", this.directory), "this.directory"); } if (this.CurrentCheckpoint != null) { this.CurrentCheckpoint.Copy(targetDirectory, overwrite); } FabricFile.Copy(sourcePath, targetPath, overwrite); }
/// <summary> /// Copy File method called by the CopyItem delegate to dequeue a file copy item /// </summary> /// <param name="source"> /// Source file to be copied /// </param> /// <param name="destination"> /// Destination file /// </param> private void CopyFile(string source, string destination) { // Copy the file and decrement work items if (this.ShouldCopy(source) && this.AreFilesDifferent(source, destination)) { bool isCopied = false; Exception ex = null; int timeoutInSeconds = 1; for (int i = 1; i <= MaxRetryAttempts; i++) { try { FabricFile.Copy(source, destination, true); isCopied = true; break; } catch (Exception e) { if (ExceptionHandler.IsFatalException(e)) { throw; } ex = e; } timeoutInSeconds = 2 * timeoutInSeconds; timeoutInSeconds = (timeoutInSeconds >= 180) ? 180 : timeoutInSeconds; Thread.Sleep(timeoutInSeconds * 1000); } if (!isCopied) { this.traceSource.WriteError( ClassName, "File copy failed to copy {0}, to {1}, with message {2}, exception {3}\r\n", source, destination, ex.Message, ex); Interlocked.Increment(ref this.failedItems); } } }
private bool SaveToFile() { // Create a new temp file string tempFilePath = Utility.GetTempFileName(); try { // Open the temp file StreamWriter writer = null; try { Utility.PerformIOWithRetries( () => { FileStream fileStream = FabricFile.Open(tempFilePath, FileMode.Create, FileAccess.Write); #if !DotNetCoreClrLinux Helpers.SetIoPriorityHint(fileStream.SafeFileHandle, Kernel32Types.PRIORITY_HINT.IoPriorityHintVeryLow); #endif writer = new StreamWriter(fileStream); }); } catch (Exception e) { this.traceSource.WriteExceptionAsError( this.logSourceId, e, "Failed to open temp file {0} for persisting index map.", tempFilePath); return(false); } try { // Write the version information to the map file string versionString = string.Concat(VersionPrefix, this.fileFormatVersion.ToString(CultureInfo.InvariantCulture)); try { Utility.PerformIOWithRetries( () => { writer.WriteLine(versionString); }); } catch (Exception e) { this.traceSource.WriteExceptionAsError( this.logSourceId, e, "Failed to write version information to temp file {0} for persisting index map.", tempFilePath); return(false); } // Write the map records to the map file foreach (TItem item in this.dictionary.Keys) { string mapRecord = string.Concat( item.ToString(), ", ", this.dictionary[item].ToString(CultureInfo.InvariantCulture)); try { Utility.PerformIOWithRetries( () => { writer.WriteLine(mapRecord); }); } catch (Exception e) { this.traceSource.WriteExceptionAsError( this.logSourceId, e, "Failed to write record {0} to temp file {1} for persisting index map.", mapRecord, tempFilePath); return(false); } } } finally { writer.Dispose(); } // Copy the temp file as the new map file try { Utility.PerformIOWithRetries( () => { FabricFile.Copy(tempFilePath, this.fileFullPath, true); }); } catch (Exception e) { this.traceSource.WriteExceptionAsError( this.logSourceId, e, "Failed to copy file {0} to {1} for persisting index map", tempFilePath, this.fileFullPath); return(false); } this.traceSource.WriteInfo( this.logSourceId, "Index map file {0} created.", this.fileFullPath); } finally { try { Utility.PerformIOWithRetries( () => { FabricFile.Delete(tempFilePath); }); } catch (Exception e) { this.traceSource.WriteExceptionAsError( this.logSourceId, e, "Failed to delete temp file {0} which was created for persisting index map.", tempFilePath); } } return(true); }
private void CopyCallerHoldsReaderLock(string source, string destination, CopyFlag copyFlag, TimeoutHelper helper) { string destinationDirectory = FabricPath.GetDirectoryName(destination); if (!string.IsNullOrEmpty(destinationDirectory) && !FabricDirectory.Exists(destinationDirectory)) { FabricDirectory.CreateDirectory(destinationDirectory); } using (FileWriterLock writerLock = new FileWriterLock(destination)) { if (!writerLock.Acquire()) { throw new FabricTransientException(StringResources.Error_ImageStoreAcquireFileLockFailed, FabricErrorCode.ImageStoreAcquireFileLockFailed); } if (helper != null) { helper.ThrowIfExpired(); } if (FabricFile.Exists(source)) { // This is a file copy if (FabricFile.Exists(destination)) { FabricFile.Delete(destination, deleteReadonly: true); } int retryCount = 0; while (helper == null || !TimeoutHelper.HasExpired(helper)) { try { bool shouldOverwrite = (copyFlag != CopyFlag.AtomicCopySkipIfExists); FabricFile.Copy(source, destination, shouldOverwrite); break; } catch (UnauthorizedAccessException) { TraceSource.WriteInfo( TraceType, "Uploading {0} to {1} caused UnauthorizedAccessException. RetryCount: {2}.", source, destination, retryCount); if (retryCount++ > 3) { throw; } // This could happen when a file is marked for delete and we try to delete // it again or try to open the file. Retrying after sometime should fix the issue. Thread.Sleep(TimeSpan.FromSeconds(retryCount)); } if (helper != null) { helper.ThrowIfExpired(); } } } else { // This is a folder copy using (FolderCopy fc = new FolderCopy(copyFlag, null)) { fc.Copy(source, destination); } } } }
internal bool Update(Dictionary <string, ServicePackageTableRecord> servicePackageTable, EtwEventTimestamp latestDataTimestamp) { // Create a new temp file string tempFilePath = Utility.GetTempFileName(); string backupFilePath = string.Empty; try { // Open the temp file StreamWriter writer = null; try { Utility.PerformIOWithRetries( () => { FileStream file = FabricFile.Open(tempFilePath, FileMode.Create, FileAccess.Write); #if !DotNetCoreClrLinux Helpers.SetIoPriorityHint(file.SafeFileHandle, Kernel32Types.PRIORITY_HINT.IoPriorityHintVeryLow); #endif writer = new StreamWriter(file); }); } catch (Exception e) { Utility.TraceSource.WriteExceptionAsError( TraceType, e, "Failed to open temp file {0}.", tempFilePath); return(false); } try { // Write the version information to the backup file try { Utility.PerformIOWithRetries( () => { writer.WriteLine(BackupFileVersionString); }); } catch (Exception e) { Utility.TraceSource.WriteExceptionAsError( TraceType, e, "Failed to write version information to temp file {0}.", tempFilePath); return(false); } // Write the table records to the backup file foreach (string tableKey in servicePackageTable.Keys) { string tableRecord = string.Concat( servicePackageTable[tableKey].NodeName, ", ", servicePackageTable[tableKey].ApplicationInstanceId, ", ", servicePackageTable[tableKey].ApplicationRolloutVersion, ", ", servicePackageTable[tableKey].ServicePackageName, ", ", servicePackageTable[tableKey].ServiceRolloutVersion, ", ", servicePackageTable[tableKey].RunLayoutRoot); try { Utility.PerformIOWithRetries( () => { writer.WriteLine(tableRecord); }); } catch (Exception e) { Utility.TraceSource.WriteExceptionAsError( TraceType, e, "Failed to write record {0} to temp file {1}", tableRecord, tempFilePath); return(false); } } } finally { writer.Dispose(); } // Compute the name of the backup file long timstampBinary = latestDataTimestamp.Timestamp.ToBinary(); string fileName = string.Concat( BackupFilePrefix, timstampBinary.ToString("D20", CultureInfo.InvariantCulture), "_", latestDataTimestamp.Differentiator.ToString("D10", CultureInfo.InvariantCulture), ".", BackupFileExt); backupFilePath = Path.Combine(this.backupDirectory, fileName); // Copy the temp file as the new backup file try { Utility.PerformIOWithRetries( () => { FabricFile.Copy(tempFilePath, backupFilePath, true); }); } catch (Exception e) { Utility.TraceSource.WriteExceptionAsError( TraceType, e, "Failed to copy file {0} to {1}", tempFilePath, backupFilePath); return(false); } Utility.TraceSource.WriteInfo( TraceType, "Backup file {0} created. The backup file is valid up to timestamp {1} ({2}, {3}).", backupFilePath, latestDataTimestamp.Timestamp, latestDataTimestamp.Timestamp.Ticks, latestDataTimestamp.Differentiator); } finally { try { Utility.PerformIOWithRetries( () => { FabricFile.Delete(tempFilePath); }); } catch (Exception e) { Utility.TraceSource.WriteExceptionAsError( TraceType, e, "Failed to delete temp file {0}", tempFilePath); } } // Update the latest backup time this.LatestBackupTime = latestDataTimestamp; // Delete older backup files string backupFilePattern = string.Concat( BackupFilePrefix, "*.", BackupFileExt); string[] backupFiles = FabricDirectory.GetFiles( this.backupDirectory, backupFilePattern); foreach (string fileToDelete in backupFiles) { if (fileToDelete.Equals( backupFilePath, StringComparison.OrdinalIgnoreCase)) { // Don't delete the current backup file continue; } try { Utility.PerformIOWithRetries( () => { FabricFile.Delete(fileToDelete); }); } catch (Exception e) { // Deletion is on a best-effort basis. Log an error and // continue. Utility.TraceSource.WriteExceptionAsError( TraceType, e, "Failed to delete old backup file {0}", fileToDelete); } } return(true); }
public static void CopyFile(string source, string destination) { FabricFile.Copy(source, destination, true); }
private static bool SaveToFile() { // Create a new temp file string tempFilePath = Utility.GetTempFileName(); try { // Open the temp file StreamWriter writer = null; try { Utility.PerformIOWithRetries( () => { FileStream fileStream = FabricFile.Open(tempFilePath, FileMode.Create, FileAccess.Write); #if !DotNetCoreClrLinux Helpers.SetIoPriorityHint(fileStream.SafeFileHandle, Kernel32Types.PRIORITY_HINT.IoPriorityHintVeryLow); #endif writer = new StreamWriter(fileStream); }); } catch (Exception e) { Utility.TraceSource.WriteExceptionAsError( LogSourceId, e, "Failed to open temp file {0} for backing up application activation table.", tempFilePath); return(false); } try { // Write the version information to the backup file try { Utility.PerformIOWithRetries( () => { writer.WriteLine(VersionString); }); } catch (Exception e) { Utility.TraceSource.WriteExceptionAsError( LogSourceId, e, "Failed to write version information to temp file {0} for backing up application activation table.", tempFilePath); return(false); } // Write the activation records to the backup file foreach (string appType in applicationEtwTracesStartTime.Keys) { foreach (string appInstanceId in applicationEtwTracesStartTime[appType].Keys) { DateTime activationTime = applicationEtwTracesStartTime[appType][appInstanceId]; string activationRecord = string.Join( ",", appInstanceId, appType, activationTime.ToBinary().ToString(), activationTime.ToString()); try { Utility.PerformIOWithRetries( () => { writer.WriteLine(activationRecord); }); } catch (Exception e) { Utility.TraceSource.WriteExceptionAsError( LogSourceId, e, "Failed to write record {0} to temp file {1} for backing up application activation table.", activationRecord, tempFilePath); return(false); } } } } finally { writer.Dispose(); } // Copy the temp file as the new backup file try { Utility.PerformIOWithRetries( () => { FabricFile.Copy(tempFilePath, backupFileFullPath, true); }); } catch (Exception e) { Utility.TraceSource.WriteExceptionAsError( LogSourceId, e, "Failed to copy file {0} to {1} for backing up application activation table.", tempFilePath, backupFileFullPath); return(false); } Utility.TraceSource.WriteInfo( LogSourceId, "Application activation table backup file {0} created.", backupFileFullPath); } finally { try { Utility.PerformIOWithRetries( () => { FabricFile.Delete(tempFilePath); }); } catch (Exception e) { Utility.TraceSource.WriteExceptionAsError( LogSourceId, e, "Failed to delete temp file {0} which was created for backing up application activation table.", tempFilePath); } } return(true); }
private static void CopyFileWorker(object context) { CopyFileParameters copyParam = (CopyFileParameters)context; FabricFile.Copy(copyParam.Source, copyParam.Destination, copyParam.Overwrite); }
private static bool CopyFileToDestination(object context) { FileCopyInfo fileCopyInfo = (FileCopyInfo)context; FabricFile.Copy(fileCopyInfo.Source, fileCopyInfo.Destination, true); return true; }
protected override bool CopyFileToDestination(string source, string sourceRelative, int retryCount, out bool fileSkipped, out bool fileCompressed) { // No compression fileCompressed = false; // Compute the destination path string destination = Path.Combine(this.destinationPath, sourceRelative); // Figure out the name of the directory at the destination and in the // local map. var destinationDir = FabricPath.GetDirectoryName(destination); // If the directory at the destination doesn't exist, then create it if ((false == string.IsNullOrEmpty(destinationDir)) && (false == this.PerformDestinationOperation(DirectoryExistsAtDestination, destinationDir))) { try { Utility.PerformIOWithRetries( () => { PerformDestinationOperation( CreateDirectoryAtDestination, destinationDir); }, retryCount); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( this.LogSourceId, e, "Failed to create directory {0} for copying file {1}.", destinationDir, sourceRelative); fileSkipped = true; return false; } } // Determine whether we need to first copy the file to the staging folder string stagingFilePath = null; if (false == string.IsNullOrEmpty(this.stagingFolderPath)) { stagingFilePath = Path.Combine(this.stagingFolderPath, Path.GetFileName(sourceRelative)); } // Copy the file over to its destination DateTime sourceLastWriteTime = DateTime.MinValue; bool sourceFileNotFound = false; try { try { Utility.PerformIOWithRetries( () => { if (string.IsNullOrEmpty(stagingFilePath)) { this.perfHelper.ExternalOperationBegin( ExternalOperationTime.ExternalOperationType.FileShareCopy, 0); } try { // Copy the file if (false == string.IsNullOrEmpty(stagingFilePath)) { FabricFile.Copy(source, stagingFilePath, true); } else { FileCopyInfo copyInfo = new FileCopyInfo { Source = source, Destination = destination }; PerformDestinationOperation(CopyFileToDestination, copyInfo); } } catch (FileNotFoundException) { sourceFileNotFound = true; } if (string.IsNullOrEmpty(stagingFilePath)) { this.perfHelper.ExternalOperationEnd( ExternalOperationTime.ExternalOperationType.FileShareCopy, 0); this.perfHelper.FileUploaded(); } }, retryCount); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( this.LogSourceId, e, "File copy failed. Source: {0}, destination: {1}", source, destination); fileSkipped = true; return false; } if (sourceFileNotFound) { // The source file was not found. Maybe it // got deleted before we had a chance to copy // it. Handle the error and move on. this.TraceSource.WriteWarning( this.LogSourceId, "File {0} could not be uploaded to {1} because it was not found.", source, destination); fileSkipped = true; return true; } if (false == string.IsNullOrEmpty(stagingFilePath)) { try { Utility.PerformIOWithRetries( () => { this.perfHelper.ExternalOperationBegin( ExternalOperationTime.ExternalOperationType.FileShareCopy, 0); FileCopyInfo copyInfo = new FileCopyInfo { Source = stagingFilePath, Destination = destination }; PerformDestinationOperation(CopyFileToDestination, copyInfo); this.perfHelper.ExternalOperationEnd( ExternalOperationTime.ExternalOperationType.FileShareCopy, 0); this.perfHelper.FileUploaded(); }, retryCount); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( this.LogSourceId, e, "File copy failed. Source: {0}, destination: {1}", source, destination); fileSkipped = true; return false; } } } finally { if (false == string.IsNullOrEmpty(stagingFilePath)) { try { Utility.PerformIOWithRetries( () => { FabricFile.Delete(stagingFilePath); }, retryCount); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( this.LogSourceId, e, "Failed to delete file {0}", stagingFilePath); } } } fileSkipped = false; return true; }
private void TestIntersectingCopyAndReplace(bool isLongPath) { var folderPath = this.testPath; if (true == isLongPath) { folderPath = this.ExtendPath(folderPath); } var filePath = Path.Combine(folderPath, this.testFileName); Assert.IsTrue(!isLongPath || filePath.Length > 260, "file path must be greater than max path size."); var copyFilePath = Path.Combine(folderPath, this.testHardLinkedFileName); Assert.IsTrue(!isLongPath || copyFilePath.Length > 260, "hard linked file path must be greater than max path size."); var replaceFilePath = Path.Combine(folderPath, this.testReplaceFileName); Assert.IsTrue(!isLongPath || replaceFilePath.Length > 260, "replace file path must be greater than max path size."); var backupFilePath = Path.Combine(folderPath, this.testBackupFileName); Assert.IsTrue(!isLongPath || backupFilePath.Length > 260, "backup file path must be greater than max path size."); LogHelper.Log("FabricDirectory.Create {0}", folderPath); FabricDirectory.CreateDirectory(folderPath); LogHelper.Log("FabricFile.Create {0}", filePath); using (StreamWriter streamWriter = new StreamWriter(FabricFile.Create(filePath))) { LogHelper.Log("Write {0}", this.testString); streamWriter.WriteLine(this.testString); streamWriter.Flush(); } using (StreamWriter streamWriter = new StreamWriter(FabricFile.Create(replaceFilePath))) { LogHelper.Log("Write {0}", this.testNewString); streamWriter.WriteLine(this.testNewString); streamWriter.Flush(); } FabricFile.Copy(filePath, copyFilePath, false); using (StreamReader reader = new StreamReader(FabricFile.Open(filePath, FileMode.Open, FileAccess.Read))) { var content = reader.ReadLine(); Assert.AreEqual <string>(this.testString, content, "before replace current file must have the old content."); } using (StreamReader copyReader0 = new StreamReader(FabricFile.Open(copyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))) { var hardLinkContent0 = copyReader0.ReadLine(); Assert.AreEqual <string>(this.testString, hardLinkContent0, "after replace hard link file must have the old content."); FabricFile.Replace(replaceFilePath, filePath, backupFilePath, false); using (StreamReader fileReader = new StreamReader(FabricFile.Open(filePath, FileMode.Open, FileAccess.Read))) { var content = fileReader.ReadLine(); Assert.AreEqual <string>(this.testNewString, content, "after replace current file must have the new content."); } using (StreamReader copyReader1 = new StreamReader(FabricFile.Open(copyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))) { var content = copyReader1.ReadLine(); Assert.AreEqual <string>(this.testString, content, "after replace hard link file must have the old content."); } } }
private void CopyTest(bool copyFileExists, bool isLongPath, bool overWrite) { var folderPath = this.testPath; if (true == isLongPath) { folderPath = this.ExtendPath(folderPath); } var filePath = Path.Combine(folderPath, this.testFileName); Assert.IsTrue(!isLongPath || filePath.Length > 260, "file path must be greater than max path size."); var copyFilePath = Path.Combine(folderPath, this.testReplaceFileName); Assert.IsTrue(!isLongPath || copyFilePath.Length > 260, "replace file path must be greater than max path size."); LogHelper.Log("FabricDirectory.Create {0}", folderPath); FabricDirectory.CreateDirectory(folderPath); LogHelper.Log("FabricFile.Create {0}", filePath); using (StreamWriter streamWriter = new StreamWriter(FabricFile.Create(filePath))) { LogHelper.Log("Write {0}", this.testNewString); streamWriter.WriteLine(this.testNewString); streamWriter.Flush(); } if (copyFileExists) { using (StreamWriter streamWriter = new StreamWriter(FabricFile.Create(copyFilePath))) { LogHelper.Log("Write {0}", this.testString); streamWriter.WriteLine(this.testString); streamWriter.Flush(); } using (StreamReader copyReader = new StreamReader(FabricFile.Open(copyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))) { var content = copyReader.ReadLine(); Assert.AreEqual <string>(this.testString, content, "before copy state."); } } else { Assert.IsFalse(FabricFile.Exists(copyFilePath)); } try { FabricFile.Copy(filePath, copyFilePath, overWrite); Assert.IsTrue(overWrite); } catch (FabricException e) { Assert.IsFalse(overWrite); LogHelper.Log("Exception thrown as expected {0}", e.GetType().ToString()); } if (true == overWrite || false == copyFileExists) { using (StreamReader copyReader = new StreamReader(FabricFile.Open(copyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))) { var content = copyReader.ReadLine(); Assert.AreEqual <string>(this.testNewString, content, "after copy file must have the new content."); LogHelper.Log("Read {0} as expected", this.testNewString); } } }
private void ProcessPerfCounterFile(FileInfo perfCounterFile) { // Copy the file to the output folder. // NOTE: In the future we may (optionally?) convert this to a human- // readable CSV file before copying to the output folder. string destinationFilePath = Path.Combine( this.outputFolderPath, perfCounterFile.Name); try { Utility.PerformIOWithRetries( () => { FabricFile.Copy(perfCounterFile.FullName, destinationFilePath, true); }); } catch (Exception e) { this.traceSource.WriteExceptionAsError( this.logSourceId, e, "Failed to copy performance counter binary file. Source: {0}, Destination: {1}.", perfCounterFile.FullName, destinationFilePath); return; } this.traceSource.WriteInfo( this.logSourceId, "Performance counter binary copied successfully. Source: {0}, Destination: {1}.", perfCounterFile.FullName, destinationFilePath); // Move the file to the archives folder destinationFilePath = Path.Combine( this.perfCounterBinaryArchiveFolder, perfCounterFile.Name); try { Utility.PerformIOWithRetries( () => { FabricFile.Move(perfCounterFile.FullName, destinationFilePath); }); } catch (Exception e) { this.traceSource.WriteExceptionAsError( this.logSourceId, e, "Failed to move performance counter binary file to the archives folder. Source: {0}, Destination: {1}.", perfCounterFile.FullName, destinationFilePath); return; } this.traceSource.WriteInfo( this.logSourceId, "Performance counter binary successfully moved to the archives folder. Source: {0}, Destination: {1}.", perfCounterFile.FullName, destinationFilePath); }
private void CopyFileToDestinationBlobWorker(FileCopyInfo fileCopyInfo, CloudBlockBlob destinationBlob) { // First let's try to copy directly from the source location. This should work fine if the // source file is not in use. bool exceptionWithDirectCopy = false; try { CreateStreamAndUploadToBlob(fileCopyInfo.SourceFullPath, destinationBlob); this.TraceSource.WriteInfo( this.LogSourceId, "Uploaded file {0} to destination blob {1}", fileCopyInfo.SourceFullPath, destinationBlob.Name); } catch (Exception e) { if ((false == (e is IOException)) && (false == (e is FabricException))) { throw; } exceptionWithDirectCopy = true; } if (exceptionWithDirectCopy) { // We couldn't copy the file directly to blob. This can happen when the file is in use, // because we would be unable to open a handle to the file in that case. Therefore, // we make a copy of the file to a temp location and then open a handle to that temp // copy for uploading to blob. string tempDir = Utility.GetTempDirName(); string tempDest = Path.Combine(tempDir, fileCopyInfo.SourceFileName); FabricDirectory.CreateDirectory(tempDir); try { FabricFile.Copy(fileCopyInfo.SourceFullPath, tempDest, true); CreateStreamAndUploadToBlob(tempDest, destinationBlob); this.TraceSource.WriteInfo( this.LogSourceId, "Uploaded file {0} to destination blob {1}", fileCopyInfo.SourceFullPath, destinationBlob.Name); } finally { try { Utility.PerformIOWithRetries( () => { FabricDirectory.Delete(tempDir, true); }); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( this.LogSourceId, e, "Failed to delete temporary directory {0}.", tempDir); } } } }