2 // Mono.Unix/UnixPath.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
7 // (C) 2004-2006 Jonathan Pryor
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
36 public sealed class UnixPath
38 private UnixPath () {}
40 public static readonly char DirectorySeparatorChar = '/';
41 public static readonly char AltDirectorySeparatorChar = '/';
42 public static readonly char PathSeparator = ':';
43 public static readonly char VolumeSeparatorChar = '/';
45 private static readonly char[] _InvalidPathChars = new char[]{};
47 public static char[] GetInvalidPathChars ()
49 return (char[]) _InvalidPathChars.Clone ();
52 public static string Combine (string path1, params string[] paths)
55 throw new ArgumentNullException ("path1");
57 throw new ArgumentNullException ("paths");
58 if (path1.IndexOfAny (_InvalidPathChars) != -1)
59 throw new ArgumentException ("Illegal characters in path", "path1");
61 int len = path1.Length;
63 for (int i = 0; i < paths.Length; ++i) {
64 if (paths [i] == null)
65 throw new ArgumentNullException ("paths[" + i + "]");
66 if (paths [i].IndexOfAny (_InvalidPathChars) != -1)
67 throw new ArgumentException ("Illegal characters in path", "paths[" + i + "]");
68 if (IsPathRooted (paths [i])) {
72 len += paths [i].Length + 1;
75 StringBuilder sb = new StringBuilder (len);
80 for (int i = start; i < paths.Length; ++i)
81 Combine (sb, paths [i]);
82 return sb.ToString ();
85 private static void Combine (StringBuilder path, string part)
87 if (path.Length > 0 && part.Length > 0) {
88 char end = path [path.Length-1];
89 if (end != DirectorySeparatorChar &&
90 end != AltDirectorySeparatorChar &&
91 end != VolumeSeparatorChar)
92 path.Append (DirectorySeparatorChar);
97 public static string GetDirectoryName (string path)
101 int lastDir = path.LastIndexOf (DirectorySeparatorChar);
103 return path.Substring (0, lastDir);
109 public static string GetFileName (string path)
111 if (path == null || path.Length == 0)
114 int lastDir = path.LastIndexOf (DirectorySeparatorChar);
116 return path.Substring (lastDir+1);
121 public static string GetFullPath (string path)
123 path = _GetFullPath (path);
124 return GetCanonicalPath (path);
127 private static string _GetFullPath (string path)
130 throw new ArgumentNullException ("path");
131 if (!IsPathRooted (path))
132 path = UnixDirectoryInfo.GetCurrentDirectory() + DirectorySeparatorChar + path;
137 public static string GetCanonicalPath (string path)
141 GetPathComponents (path, out dirs, out lastIndex);
142 string end = string.Join ("/", dirs, 0, lastIndex);
143 return IsPathRooted (path) ? "/" + end : end;
146 private static void GetPathComponents (string path,
147 out string[] components, out int lastIndex)
149 string [] dirs = path.Split (DirectorySeparatorChar);
151 for (int i = 0; i < dirs.Length; ++i) {
152 if (dirs [i] == "." || dirs [i] == string.Empty) continue;
153 else if (dirs [i] == "..") {
154 if (target != 0) --target;
158 dirs [target++] = dirs [i];
164 public static string GetPathRoot (string path)
168 if (!IsPathRooted (path))
173 public static string GetCompleteRealPath (string path)
176 throw new ArgumentNullException ("path");
179 GetPathComponents (path, out dirs, out lastIndex);
180 StringBuilder realPath = new StringBuilder ();
181 if (dirs.Length > 0) {
182 string dir = IsPathRooted (path) ? "/" : "";
184 realPath.Append (GetRealPath (dir));
186 for (int i = 1; i < lastIndex; ++i) {
187 realPath.Append ("/").Append (dirs [i]);
188 string p = GetRealPath (realPath.ToString());
189 realPath.Remove (0, realPath.Length);
192 return realPath.ToString ();
195 public static string GetRealPath (string path)
198 string name = ReadSymbolicLink (path);
201 if (IsPathRooted (name))
204 path = GetDirectoryName (path) + DirectorySeparatorChar + name;
205 path = GetCanonicalPath (path);
210 // Read the specified symbolic link. If the file isn't a symbolic link,
211 // return null; otherwise, return the contents of the symbolic link.
213 // readlink(2) is horribly evil, as there is no way to query how big the
214 // symlink contents are. Consequently, it's trial and error...
215 internal static string ReadSymbolicLink (string path)
217 StringBuilder buf = new StringBuilder (256);
219 int r = Native.Syscall.readlink (path, buf);
222 switch (e = Native.Stdlib.GetLastError()) {
223 case Native.Errno.EINVAL:
224 // path isn't a symbolic link
227 UnixMarshal.ThrowExceptionForError (e);
231 else if (r == buf.Capacity) {
235 return buf.ToString (0, r);
239 // Read the specified symbolic link. If the file isn't a symbolic link,
240 // return null; otherwise, return the contents of the symbolic link.
242 // readlink(2) is horribly evil, as there is no way to query how big the
243 // symlink contents are. Consequently, it's trial and error...
244 private static string ReadSymbolicLink (string path, out Native.Errno errno)
246 errno = (Native.Errno) 0;
247 StringBuilder buf = new StringBuilder (256);
249 int r = Native.Syscall.readlink (path, buf);
251 errno = Native.Stdlib.GetLastError ();
254 else if (r == buf.Capacity) {
258 return buf.ToString (0, r);
262 public static string TryReadLink (string path)
265 return ReadSymbolicLink (path, out errno);
268 public static string ReadLink (string path)
271 path = ReadSymbolicLink (path, out errno);
273 UnixMarshal.ThrowExceptionForError (errno);
277 public static bool IsPathRooted (string path)
279 if (path == null || path.Length == 0)
281 return path [0] == DirectorySeparatorChar;
284 internal static void CheckPath (string path)
287 throw new ArgumentNullException ();
288 if (path.Length == 0)
289 throw new ArgumentException ("Path cannot contain a zero-length string", "path");
290 if (path.IndexOfAny (_InvalidPathChars) != -1)
291 throw new ArgumentException ("Invalid characters in path.", "path");