| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace Renci.SshNet.Common |
| | | 4 | | { |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents a POSIX path. |
| | | 7 | | /// </summary> |
| | | 8 | | internal sealed class PosixPath |
| | | 9 | | { |
| | 290 | 10 | | private PosixPath() |
| | 290 | 11 | | { |
| | 290 | 12 | | } |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Gets the directory of the path. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <value> |
| | | 18 | | /// The directory of the path. |
| | | 19 | | /// </value> |
| | 574 | 20 | | public string Directory { get; private set; } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the file part of the path. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <value> |
| | | 26 | | /// The file part of the path, or <see langword="null"/> if the path represents a directory. |
| | | 27 | | /// </value> |
| | 705 | 28 | | public string File { get; private set; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Create a <see cref="PosixPath"/> from the specified path. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="path">The path.</param> |
| | | 34 | | /// <returns> |
| | | 35 | | /// A <see cref="PosixPath"/> created from the specified path. |
| | | 36 | | /// </returns> |
| | | 37 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 38 | | /// <exception cref="ArgumentException"><paramref name="path"/> is empty ("").</exception> |
| | | 39 | | public static PosixPath CreateAbsoluteOrRelativeFilePath(string path) |
| | 293 | 40 | | { |
| | 293 | 41 | | if (path is null) |
| | 3 | 42 | | { |
| | 3 | 43 | | throw new ArgumentNullException(nameof(path)); |
| | | 44 | | } |
| | | 45 | | |
| | 290 | 46 | | var posixPath = new PosixPath(); |
| | | 47 | | |
| | 290 | 48 | | var pathEnd = path.LastIndexOf('/'); |
| | 290 | 49 | | if (pathEnd == -1) |
| | 52 | 50 | | { |
| | 52 | 51 | | if (path.Length == 0) |
| | 3 | 52 | | { |
| | 3 | 53 | | throw new ArgumentException("The path is a zero-length string.", nameof(path)); |
| | | 54 | | } |
| | | 55 | | |
| | 49 | 56 | | posixPath.Directory = "."; |
| | 49 | 57 | | posixPath.File = path; |
| | 49 | 58 | | } |
| | 238 | 59 | | else if (pathEnd == 0) |
| | 15 | 60 | | { |
| | 15 | 61 | | posixPath.Directory = "/"; |
| | 15 | 62 | | if (path.Length > 1) |
| | 12 | 63 | | { |
| | 12 | 64 | | posixPath.File = path.Substring(pathEnd + 1); |
| | 12 | 65 | | } |
| | 15 | 66 | | } |
| | | 67 | | else |
| | 223 | 68 | | { |
| | 223 | 69 | | posixPath.Directory = path.Substring(0, pathEnd); |
| | 223 | 70 | | if (pathEnd < path.Length - 1) |
| | 220 | 71 | | { |
| | 220 | 72 | | posixPath.File = path.Substring(pathEnd + 1); |
| | 220 | 73 | | } |
| | 223 | 74 | | } |
| | | 75 | | |
| | 287 | 76 | | return posixPath; |
| | 287 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Gets the file name part of a given POSIX path. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="path">The POSIX path to get the file name for.</param> |
| | | 83 | | /// <returns> |
| | | 84 | | /// The file name part of <paramref name="path"/>. |
| | | 85 | | /// </returns> |
| | | 86 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 87 | | /// <remarks> |
| | | 88 | | /// <para> |
| | | 89 | | /// If <paramref name="path"/> contains no forward slash, then <paramref name="path"/> |
| | | 90 | | /// is returned. |
| | | 91 | | /// </para> |
| | | 92 | | /// <para> |
| | | 93 | | /// If path has a trailing slash, <see cref="GetFileName(string)"/> return a zero-length string. |
| | | 94 | | /// </para> |
| | | 95 | | /// </remarks> |
| | | 96 | | public static string GetFileName(string path) |
| | 40 | 97 | | { |
| | 40 | 98 | | if (path is null) |
| | 3 | 99 | | { |
| | 3 | 100 | | throw new ArgumentNullException(nameof(path)); |
| | | 101 | | } |
| | | 102 | | |
| | 37 | 103 | | var pathEnd = path.LastIndexOf('/'); |
| | 37 | 104 | | if (pathEnd == -1) |
| | 10 | 105 | | { |
| | 10 | 106 | | return path; |
| | | 107 | | } |
| | | 108 | | |
| | 27 | 109 | | if (pathEnd == path.Length - 1) |
| | 6 | 110 | | { |
| | 6 | 111 | | return string.Empty; |
| | | 112 | | } |
| | | 113 | | |
| | 21 | 114 | | return path.Substring(pathEnd + 1); |
| | 37 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Gets the directory name part of a given POSIX path. |
| | | 119 | | /// </summary> |
| | | 120 | | /// <param name="path">The POSIX path to get the directory name for.</param> |
| | | 121 | | /// <returns> |
| | | 122 | | /// The directory part of the specified <paramref name="path"/>, or <c>.</c> if <paramref name="path"/> |
| | | 123 | | /// does not contain any directory information. |
| | | 124 | | /// </returns> |
| | | 125 | | /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception> |
| | | 126 | | public static string GetDirectoryName(string path) |
| | 39 | 127 | | { |
| | 39 | 128 | | if (path is null) |
| | 3 | 129 | | { |
| | 3 | 130 | | throw new ArgumentNullException(nameof(path)); |
| | | 131 | | } |
| | | 132 | | |
| | 36 | 133 | | var pathEnd = path.LastIndexOf('/'); |
| | 36 | 134 | | if (pathEnd == -1) |
| | 9 | 135 | | { |
| | 9 | 136 | | return "."; |
| | | 137 | | } |
| | | 138 | | |
| | 27 | 139 | | if (pathEnd == 0) |
| | 15 | 140 | | { |
| | 15 | 141 | | return "/"; |
| | | 142 | | } |
| | | 143 | | |
| | 12 | 144 | | if (pathEnd == path.Length - 1) |
| | 3 | 145 | | { |
| | 3 | 146 | | return path.Substring(0, pathEnd); |
| | | 147 | | } |
| | | 148 | | |
| | 9 | 149 | | return path.Substring(0, pathEnd); |
| | 36 | 150 | | } |
| | | 151 | | } |
| | | 152 | | } |