SessionLogReader.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Xml;
  5. using System.Text;
  6. namespace WinSCP
  7. {
  8. internal class SessionLogReader : CustomLogReader
  9. {
  10. public SessionLogReader(Session session) :
  11. base(session)
  12. {
  13. _position = 0;
  14. }
  15. public override void Dispose()
  16. {
  17. using (Session.Logger.CreateCallstack())
  18. {
  19. Cleanup();
  20. }
  21. base.Dispose();
  22. }
  23. private void Cleanup()
  24. {
  25. if (_stream != null)
  26. {
  27. Session.Logger.WriteLine("Closing log");
  28. _stream.Dispose();
  29. _stream = null;
  30. }
  31. ((IDisposable)_reader).Dispose();
  32. _reader = null;
  33. }
  34. public override bool Read(LogReadFlags flags)
  35. {
  36. using (Session.Logger.CreateCallstack())
  37. {
  38. bool result;
  39. bool retry;
  40. do
  41. {
  42. result = DoRead();
  43. retry = false;
  44. if (result &&
  45. IsNonEmptyElement("failure"))
  46. {
  47. SessionRemoteException e = SessionRemoteException.ReadFailure(this);
  48. Session.RaiseFailed(e);
  49. if ((flags & LogReadFlags.ThrowFailures) == 0)
  50. {
  51. retry = true;
  52. }
  53. else
  54. {
  55. throw e;
  56. }
  57. }
  58. }
  59. while (retry);
  60. return result;
  61. }
  62. }
  63. private bool DoRead()
  64. {
  65. int interval = 125;
  66. bool result;
  67. do
  68. {
  69. if (_reader == null)
  70. {
  71. OpenLog();
  72. }
  73. Debug.Assert(_reader != null);
  74. try
  75. {
  76. result = _reader.Read();
  77. if (result)
  78. {
  79. ++_position;
  80. Session.Logger.WriteLine("Read node {0}: {1} {2}{3}{4}",
  81. _position, _reader.NodeType, _reader.Name,
  82. (_reader.HasValue && !string.IsNullOrEmpty(_reader.Name) && !string.IsNullOrEmpty(_reader.Value) ? "=" : string.Empty),
  83. _reader.Value);
  84. Session.GotOutput();
  85. }
  86. else
  87. {
  88. Session.Logger.WriteLine("Cannot read");
  89. if (!_closed)
  90. {
  91. // this should not happen as when the log is not closed,
  92. // we should get XmlException on reaching the end
  93. _closed = true;
  94. Cleanup();
  95. }
  96. Session.CheckForTimeout();
  97. }
  98. }
  99. catch (XmlException e)
  100. {
  101. Cleanup();
  102. if (!_closed)
  103. {
  104. // If log was not closed, it is likely the XML is not well-formed
  105. // (at least top-level <session/> tag is not closed),
  106. // so we swallow the parsing errors here.
  107. Session.Logger.WriteLine("Error parsing session log file, but it is not closed yet, will retry");
  108. result = false;
  109. }
  110. else
  111. {
  112. // check if the the root cause was session abort
  113. Session.CheckForTimeout();
  114. LogContents();
  115. throw new SessionLocalException(Session, "Error parsing session log file", e);
  116. }
  117. }
  118. if (!result && !_closed)
  119. {
  120. Session.Logger.WriteLine("Waiting for log update and dispatching events for {0}", interval);
  121. Session.DispatchEvents(interval);
  122. if (interval < 500)
  123. {
  124. interval *= 2;
  125. }
  126. }
  127. }
  128. while (!result && !_closed);
  129. if (result)
  130. {
  131. LogContents();
  132. }
  133. return result;
  134. }
  135. private void LogContents()
  136. {
  137. if (Session.Logger.Logging)
  138. {
  139. try
  140. {
  141. // alterative to File.ReadAllText with write-sharing
  142. // (note that the StreamReader disposes the Stream)
  143. using (StreamReader reader = new StreamReader(OpenLogFileWithWriteSharing(), Encoding.UTF8))
  144. {
  145. string contents = reader.ReadToEnd();
  146. if ((_logged == null) || (_logged != contents))
  147. {
  148. Session.Logger.WriteLine("Log contents:\n{0}", contents);
  149. _logged = contents;
  150. }
  151. else
  152. {
  153. Session.Logger.WriteLine("Log contents has not changed");
  154. }
  155. }
  156. }
  157. catch (Exception e)
  158. {
  159. Session.Logger.WriteLine("Error logging log contents [{0}]", e.Message);
  160. }
  161. }
  162. }
  163. private void OpenLog()
  164. {
  165. Session.Logger.WriteLine("Opening log");
  166. if (_closed)
  167. {
  168. throw new InvalidOperationException("Log was closed already");
  169. }
  170. try
  171. {
  172. // First try to open file without write sharing.
  173. // This fails, if WinSCP is still writing to the log file.
  174. // This is done only as a way to detect that log file is not complete yet.
  175. _stream = File.Open(Session.XmlLogPath, FileMode.Open, FileAccess.Read, FileShare.Read);
  176. _closed = true;
  177. Session.Logger.WriteLine("Log opened without write sharing");
  178. LogContents();
  179. }
  180. catch (IOException)
  181. {
  182. // If log file is still being written to, open it with write sharing
  183. _stream = OpenLogFileWithWriteSharing();
  184. _closed = false;
  185. Session.Logger.WriteLine("Log opened with write sharing");
  186. }
  187. _reader = XmlReader.Create(_stream);
  188. int skip = _position;
  189. Session.Logger.WriteLine("Skipping {0} nodes", skip);
  190. while (skip > 0)
  191. {
  192. if (!_reader.Read())
  193. {
  194. throw new SessionLocalException(Session, "Read less nodes than in previous log parsing");
  195. }
  196. --skip;
  197. }
  198. }
  199. private FileStream OpenLogFileWithWriteSharing()
  200. {
  201. return File.Open(Session.XmlLogPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  202. }
  203. internal override XmlReader Reader
  204. {
  205. get
  206. {
  207. if (_reader == null)
  208. {
  209. throw new SessionLocalException(Session, "Reading has not commenced yet");
  210. }
  211. return _reader;
  212. }
  213. }
  214. private int _position;
  215. private XmlReader _reader;
  216. private FileStream _stream;
  217. private bool _closed;
  218. private string _logged;
  219. }
  220. }