/// <summary> /// Removes the destination for a file transfer. /// </summary> /// <param name="backend">Compiler backend that generated the transfer.</param> /// <param name="transfers">File transfers destination to remove.</param> private static void RemoveDestination(BackendCompiler backend, FileTransfer transfer) { if (File.Exists(transfer.Destination)) { backend.OnMessage(new CompilerMessageEventArgs(CompilerMessage.RemovingDestinationFile(transfer.Destination), transfer.LineNumber)); // try to ensure the file is not read-only FileAttributes attributes = File.GetAttributes(transfer.Destination); try { File.SetAttributes(transfer.Destination, attributes & ~FileAttributes.ReadOnly); } catch (ArgumentException ae) // thrown for unauthorized access errors { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.CannotTransferFile(transfer.Source, transfer.Destination), transfer.LineNumber), ae); } // try to delete the file try { File.Delete(transfer.Destination); } catch (IOException ioe) { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.CannotTransferFile(transfer.Source, transfer.Destination), transfer.LineNumber), ioe); } } else // no idea what just happened, bail { //throw; } }
/// <summary> /// Copy or move a single transfer. /// </summary> /// <param name="backend">Compiler backend that generated the transfers.</param> /// <param name="transfer">File transfers to execute.</param> public static void ExecuteTransfer(BackendCompiler backend, FileTransfer transfer) { if (transfer.Redundant) { // TODO: log that we tried to transfer a redundant file? return; } bool retry = false; do { try { if (transfer.Move) { backend.OnMessage(new CompilerMessageEventArgs(CompilerMessage.MoveFile(transfer.Source, transfer.Destination), transfer.LineNumber)); File.Move(transfer.Source, transfer.Destination); } else { backend.OnMessage(new CompilerMessageEventArgs(CompilerMessage.CopyFile(transfer.Source, transfer.Destination), transfer.LineNumber)); File.Copy(transfer.Source, transfer.Destination, true); } retry = false; } catch (FileNotFoundException e) { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.FileNotFound(transfer.Source), transfer.LineNumber), e); } catch (DirectoryNotFoundException dnfe) { // if we already retried, give up if (retry) { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.CannotTransferFile(transfer.Source, transfer.Destination), transfer.LineNumber), dnfe); } string directory = Path.GetDirectoryName(transfer.Destination); Directory.CreateDirectory(directory); retry = true; } catch (UnauthorizedAccessException uae) { // if we already retried, give up if (retry) { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.CannotTransferFile(transfer.Source, transfer.Destination), transfer.LineNumber), uae); } FileTransfer.RemoveDestination(backend, transfer); retry = true; } catch (IOException e) { // if we already retried, give up if (retry) { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.CannotTransferFile(transfer.Source, transfer.Destination), transfer.LineNumber), e); } FileTransfer.RemoveDestination(backend, transfer); retry = true; } } while (retry); }