2
0

RemotePathDoubleQuoteTransformation.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Text;
  3. using Renci.SshNet.Common;
  4. namespace Renci.SshNet
  5. {
  6. /// <summary>
  7. /// Encloses a path in double quotes, and escapes any embedded double quote with a backslash.
  8. /// </summary>
  9. internal sealed class RemotePathDoubleQuoteTransformation : IRemotePathTransformation
  10. {
  11. /// <summary>
  12. /// Encloses a path in double quotes, and escapes any embedded double quote with a backslash.
  13. /// </summary>
  14. /// <param name="path">The path to transform.</param>
  15. /// <returns>
  16. /// The transformed path.
  17. /// </returns>
  18. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
  19. /// <example>
  20. /// <list type="table">
  21. /// <listheader>
  22. /// <term>Original</term>
  23. /// <term>Transformed</term>
  24. /// </listheader>
  25. /// <item>
  26. /// <term>/var/log/auth.log</term>
  27. /// <term>&quot;/var/log/auth.log&quot;</term>
  28. /// </item>
  29. /// <item>
  30. /// <term>/var/mp3/Guns N' Roses</term>
  31. /// <term>&quot;/var/mp3/Guns N' Roses&quot;</term>
  32. /// </item>
  33. /// <item>
  34. /// <term>/var/garbage!/temp</term>
  35. /// <term>&quot;/var/garbage!/temp&quot;</term>
  36. /// </item>
  37. /// <item>
  38. /// <term>/var/would be 'kewl'!/not?</term>
  39. /// <term>&quot;/var/would be 'kewl'!, not?&quot;</term>
  40. /// </item>
  41. /// <item>
  42. /// <term></term>
  43. /// <term>&quot;&quot;</term>
  44. /// </item>
  45. /// <item>
  46. /// <term>Hello &quot;World&quot;</term>
  47. /// <term>&quot;Hello \&quot;World&quot;</term>
  48. /// </item>
  49. /// </list>
  50. /// </example>
  51. public string Transform(string path)
  52. {
  53. ThrowHelper.ThrowIfNull(path);
  54. var transformed = new StringBuilder(path.Length);
  55. _ = transformed.Append('"');
  56. foreach (var c in path)
  57. {
  58. if (c == '"')
  59. {
  60. _ = transformed.Append('\\');
  61. }
  62. _ = transformed.Append(c);
  63. }
  64. _ = transformed.Append('"');
  65. return transformed.ToString();
  66. }
  67. }
  68. }