* UnixPath.cs: Add ReadLink() and TryReadLink() methods.
[mono.git] / mcs / class / Mono.Posix / Mono.Unix / UnixPath.cs
1 //
2 // Mono.Unix/UnixPath.cs
3 //
4 // Authors:
5 //   Jonathan Pryor (jonpryor@vt.edu)
6 //
7 // (C) 2004-2006 Jonathan Pryor
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System;
30 using System.Collections;
31 using System.Text;
32 using Mono.Unix;
33
34 namespace Mono.Unix {
35
36         public sealed class UnixPath
37         {
38                 private UnixPath () {}
39
40                 public static readonly char DirectorySeparatorChar = '/';
41                 public static readonly char AltDirectorySeparatorChar = '/';
42                 public static readonly char PathSeparator = ':';
43                 public static readonly char VolumeSeparatorChar = '/';
44
45                 private static readonly char[] _InvalidPathChars = new char[]{};
46
47                 public static char[] GetInvalidPathChars ()
48                 {
49                         return (char[]) _InvalidPathChars.Clone ();
50                 }
51
52                 public static string Combine (string path1, params string[] paths)
53                 {
54                         if (path1 == null)
55                                 throw new ArgumentNullException ("path1");
56                         if (paths == null)
57                                 throw new ArgumentNullException ("paths");
58                         if (path1.IndexOfAny (_InvalidPathChars) != -1)
59                                 throw new ArgumentException ("Illegal characters in path", "path1");
60
61                         int len = path1.Length + 1;
62                         for (int i = 0; i < paths.Length; ++i) {
63                                 if (paths [i] == null)
64                                         throw new ArgumentNullException ("paths");
65                                 len += paths [i].Length + 1;
66                         }
67
68                         StringBuilder sb = new StringBuilder (len);
69                         sb.Append (path1);
70                         for (int i = 0; i < paths.Length; ++i)
71                                 Combine (sb, paths [i]);
72                         return sb.ToString ();
73                 }
74
75                 private static void Combine (StringBuilder path, string part)
76                 {
77                         if (part.IndexOfAny (_InvalidPathChars) != -1)
78                                 throw new ArgumentException ("Illegal characters in path", "path1");
79                         char end = path [path.Length-1];
80                         if (end != DirectorySeparatorChar && 
81                                         end != AltDirectorySeparatorChar && 
82                                         end != VolumeSeparatorChar)
83                                 path.Append (DirectorySeparatorChar);
84                         path.Append (part);
85                 }
86
87                 public static string GetDirectoryName (string path)
88                 {
89                         CheckPath (path);
90
91                         int lastDir = path.LastIndexOf (DirectorySeparatorChar);
92                         if (lastDir > 0)
93                                 return path.Substring (0, lastDir);
94                         return "";
95                 }
96
97                 public static string GetFileName (string path)
98                 {
99                         if (path == null || path.Length == 0)
100                                 return path;
101
102                         int lastDir = path.LastIndexOf (DirectorySeparatorChar);
103                         if (lastDir >= 0)
104                                 return path.Substring (lastDir+1);
105
106                         return path;
107                 }
108
109                 public static string GetFullPath (string path)
110                 {
111                         path = _GetFullPath (path);
112                         return GetCanonicalPath (path);
113                 }
114
115                 private static string _GetFullPath (string path)
116                 {
117                         if (path == null)
118                                 throw new ArgumentNullException ("path");
119                         if (!IsPathRooted (path))
120                                 path = UnixDirectoryInfo.GetCurrentDirectory() + DirectorySeparatorChar + path;
121
122                         return path;
123                 }
124
125                 public static string GetCanonicalPath (string path)
126                 {
127                         string [] dirs;
128                         int lastIndex;
129                         GetPathComponents (path, out dirs, out lastIndex);
130                         string end = string.Join ("/", dirs, 0, lastIndex);
131                         return IsPathRooted (path) ? "/" + end : end;
132                 }
133
134                 private static void GetPathComponents (string path, 
135                         out string[] components, out int lastIndex)
136                 {
137                         string [] dirs = path.Split (DirectorySeparatorChar);
138                         int target = 0;
139                         for (int i = 0; i < dirs.Length; ++i) {
140                                 if (dirs [i] == "." || dirs [i] == string.Empty) continue;
141                                 else if (dirs [i] == "..") {
142                                         if (target != 0) --target;
143                                         else ++target;
144                                 }
145                                 else
146                                         dirs [target++] = dirs [i];
147                         }
148                         components = dirs;
149                         lastIndex = target;
150                 }
151
152                 public static string GetPathRoot (string path)
153                 {
154                         if (path == null)
155                                 return null;
156                         if (!IsPathRooted (path))
157                                 return "";
158                         return "/";
159                 }
160
161                 public static string GetCompleteRealPath (string path)
162                 {
163                         if (path == null)
164                                 throw new ArgumentNullException ("path");
165                         string [] dirs;
166                         int lastIndex;
167                         GetPathComponents (path, out dirs, out lastIndex);
168                         StringBuilder realPath = new StringBuilder ();
169                         if (dirs.Length > 0) {
170                                 string dir = IsPathRooted (path) ? "/" : "";
171                                 dir += dirs [0];
172                                 realPath.Append (GetRealPath (dir));
173                         }
174                         for (int i = 1; i < lastIndex; ++i) {
175                                 realPath.Append ("/").Append (dirs [i]);
176                                 string p = GetRealPath (realPath.ToString());
177                                 realPath.Remove (0, realPath.Length);
178                                 realPath.Append (p);
179                         }
180                         return realPath.ToString ();
181                 }
182
183                 public static string GetRealPath (string path)
184                 {
185                         do {
186                                 string name = ReadSymbolicLink (path);
187                                 if (name == null)
188                                         return path;
189                                 if (IsPathRooted (name))
190                                         path = name;
191                                 else {
192                                         path = GetDirectoryName (path) + DirectorySeparatorChar + name;
193                                         path = GetCanonicalPath (path);
194                                 }
195                         } while (true);
196                 }
197
198                 // Read the specified symbolic link.  If the file isn't a symbolic link,
199                 // return null; otherwise, return the contents of the symbolic link.
200                 //
201                 // readlink(2) is horribly evil, as there is no way to query how big the
202                 // symlink contents are.  Consequently, it's trial and error...
203                 internal static string ReadSymbolicLink (string path)
204                 {
205                         StringBuilder buf = new StringBuilder (256);
206                         do {
207                                 int r = Native.Syscall.readlink (path, buf);
208                                 if (r < 0) {
209                                         Native.Errno e;
210                                         switch (e = Native.Stdlib.GetLastError()) {
211                                         case Native.Errno.EINVAL:
212                                                 // path isn't a symbolic link
213                                                 return null;
214                                         default:
215                                                 UnixMarshal.ThrowExceptionForError (e);
216                                                 break;
217                                         }
218                                 }
219                                 else if (r == buf.Capacity) {
220                                         buf.Capacity *= 2;
221                                 }
222                                 else
223                                         return buf.ToString (0, r);
224                         } while (true);
225                 }
226
227                 // Read the specified symbolic link.  If the file isn't a symbolic link,
228                 // return null; otherwise, return the contents of the symbolic link.
229                 //
230                 // readlink(2) is horribly evil, as there is no way to query how big the
231                 // symlink contents are.  Consequently, it's trial and error...
232                 private static string ReadSymbolicLink (string path, out Native.Errno errno)
233                 {
234                         errno = (Native.Errno) 0;
235                         StringBuilder buf = new StringBuilder (256);
236                         do {
237                                 int r = Native.Syscall.readlink (path, buf);
238                                 if (r < 0) {
239                                         errno = Native.Stdlib.GetLastError ();
240                                         return null;
241                                 }
242                                 else if (r == buf.Capacity) {
243                                         buf.Capacity *= 2;
244                                 }
245                                 else
246                                         return buf.ToString (0, r);
247                         } while (true);
248                 }
249
250                 public static string TryReadLink (string path)
251                 {
252                         Native.Errno errno;
253                         return ReadSymbolicLink (path, out errno);
254                 }
255
256                 public static string ReadLink (string path)
257                 {
258                         Native.Errno errno;
259                         path = ReadSymbolicLink (path, out errno);
260                         if (errno != 0)
261                                 UnixMarshal.ThrowExceptionForError (errno);
262                         return path;
263                 }
264
265                 public static bool IsPathRooted (string path)
266                 {
267                         if (path == null || path.Length == 0)
268                                 return false;
269                         return path [0] == DirectorySeparatorChar;
270                 }
271
272                 internal static void CheckPath (string path)
273                 {
274                         if (path == null)
275                                 throw new ArgumentNullException ();
276                         if (path.Length == 0)
277                                 throw new ArgumentException ("Path cannot contain a zero-length string", "path");
278                         if (path.IndexOfAny (_InvalidPathChars) != -1)
279                                 throw new ArgumentException ("Invalid characters in path.", "path");
280                 }
281         }
282 }
283
284 // vim: noexpandtab