| | | 1 | | using System; |
| | | 2 | | using System.Text; |
| | | 3 | | |
| | | 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>"/var/log/auth.log"</term> |
| | | 28 | | /// </item> |
| | | 29 | | /// <item> |
| | | 30 | | /// <term>/var/mp3/Guns N' Roses</term> |
| | | 31 | | /// <term>"/var/mp3/Guns N' Roses"</term> |
| | | 32 | | /// </item> |
| | | 33 | | /// <item> |
| | | 34 | | /// <term>/var/garbage!/temp</term> |
| | | 35 | | /// <term>"/var/garbage!/temp"</term> |
| | | 36 | | /// </item> |
| | | 37 | | /// <item> |
| | | 38 | | /// <term>/var/would be 'kewl'!/not?</term> |
| | | 39 | | /// <term>"/var/would be 'kewl'!, not?"</term> |
| | | 40 | | /// </item> |
| | | 41 | | /// <item> |
| | | 42 | | /// <term></term> |
| | | 43 | | /// <term>""</term> |
| | | 44 | | /// </item> |
| | | 45 | | /// <item> |
| | | 46 | | /// <term>Hello "World"</term> |
| | | 47 | | /// <term>"Hello \"World"</term> |
| | | 48 | | /// </item> |
| | | 49 | | /// </list> |
| | | 50 | | /// </example> |
| | | 51 | | public string Transform(string path) |
| | 679 | 52 | | { |
| | 679 | 53 | | if (path is null) |
| | 3 | 54 | | { |
| | 3 | 55 | | throw new ArgumentNullException(nameof(path)); |
| | | 56 | | } |
| | | 57 | | |
| | 676 | 58 | | var transformed = new StringBuilder(path.Length); |
| | | 59 | | |
| | 676 | 60 | | _ = transformed.Append('"'); |
| | | 61 | | |
| | 14706 | 62 | | foreach (var c in path) |
| | 6339 | 63 | | { |
| | 6339 | 64 | | if (c == '"') |
| | 21 | 65 | | { |
| | 21 | 66 | | _ = transformed.Append('\\'); |
| | 21 | 67 | | } |
| | | 68 | | |
| | 6339 | 69 | | _ = transformed.Append(c); |
| | 6339 | 70 | | } |
| | | 71 | | |
| | 676 | 72 | | _ = transformed.Append('"'); |
| | | 73 | | |
| | 676 | 74 | | return transformed.ToString(); |
| | 676 | 75 | | } |
| | | 76 | | } |
| | | 77 | | } |