Merge pull request #2040 from esdrubal/monoman
[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;
62                         int start = -1;
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])) {
69                                         len = 0;
70                                         start = i;
71                                 }
72                                 len += paths [i].Length + 1;
73                         }
74
75                         StringBuilder sb = new StringBuilder (len);
76                         if (start == -1) {
77                                 sb.Append (path1);
78                                 start = 0;
79                         }
80                         for (int i = start; i < paths.Length; ++i)
81                                 Combine (sb, paths [i]);
82                         return sb.ToString ();
83                 }
84
85                 private static void Combine (StringBuilder path, string part)
86                 {
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);
93                         }
94                         path.Append (part);
95                 }
96
97                 public static string GetDirectoryName (string path)
98                 {
99                         CheckPath (path);
100
101                         int lastDir = path.LastIndexOf (DirectorySeparatorChar);
102                         if (lastDir > 0)
103                                 return path.Substring (0, lastDir);
104                         if (lastDir == 0)
105                                 return "/";
106                         return "";
107                 }
108
109                 public static string GetFileName (string path)
110                 {
111                         if (path == null || path.Length == 0)
112                                 return path;
113
114                         int lastDir = path.LastIndexOf (DirectorySeparatorChar);
115                         if (lastDir >= 0)
116                                 return path.Substring (lastDir+1);
117
118                         return path;
119                 }
120
121                 public static string GetFullPath (string path)
122                 {
123                         path = _GetFullPath (path);
124                         return GetCanonicalPath (path);
125                 }
126
127                 private static string _GetFullPath (string path)
128                 {
129                         if (path == null)
130                                 throw new ArgumentNullException ("path");
131                         if (!IsPathRooted (path))
132                                 path = UnixDirectoryInfo.GetCurrentDirectory() + DirectorySeparatorChar + path;
133
134                         return path;
135                 }
136
137                 public static string GetCanonicalPath (string path)
138                 {
139                         string [] dirs;
140                         int lastIndex;
141                         GetPathComponents (path, out dirs, out lastIndex);
142                         string end = string.Join ("/", dirs, 0, lastIndex);
143                         return IsPathRooted (path) ? "/" + end : end;
144                 }
145
146                 private static void GetPathComponents (string path, 
147                         out string[] components, out int lastIndex)
148                 {
149                         string [] dirs = path.Split (DirectorySeparatorChar);
150                         int target = 0;
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;
155                                         else ++target;
156                                 }
157                                 else
158                                         dirs [target++] = dirs [i];
159                         }
160                         components = dirs;
161                         lastIndex = target;
162                 }
163
164                 public static string GetPathRoot (string path)
165                 {
166                         if (path == null)
167                                 return null;
168                         if (!IsPathRooted (path))
169                                 return "";
170                         return "/";
171                 }
172
173                 public static string GetCompleteRealPath (string path)
174                 {
175                         if (path == null)
176                                 throw new ArgumentNullException ("path");
177                         string [] dirs;
178                         int lastIndex;
179                         GetPathComponents (path, out dirs, out lastIndex);
180                         StringBuilder realPath = new StringBuilder ();
181                         if (dirs.Length > 0) {
182                                 string dir = IsPathRooted (path) ? "/" : "";
183                                 dir += dirs [0];
184                                 realPath.Append (GetRealPath (dir));
185                         }
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);
190                                 realPath.Append (p);
191                         }
192                         return realPath.ToString ();
193                 }
194
195                 public static string GetRealPath (string path)
196                 {
197                         do {
198                                 string name = ReadSymbolicLink (path);
199                                 if (name == null)
200                                         return path;
201                                 if (IsPathRooted (name))
202                                         path = name;
203                                 else {
204                                         path = GetDirectoryName (path) + DirectorySeparatorChar + name;
205                                         path = GetCanonicalPath (path);
206                                 }
207                         } while (true);
208                 }
209
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.
212                 internal static string ReadSymbolicLink (string path)
213                 {
214                         string target = TryReadLink (path);
215                         if (target == null) {
216                                 Native.Errno errno = Native.Stdlib.GetLastError ();
217                                 if (errno != Native.Errno.EINVAL)
218                                         UnixMarshal.ThrowExceptionForError (errno);
219                         }
220                         return target;
221                 }
222
223                 public static string TryReadLink (string path)
224                 {
225                         byte[] buf = new byte[256];
226                         do {
227                                 long r = Native.Syscall.readlink (path, buf);
228                                 if (r < 0)
229                                         return null;
230                                 else if (r == buf.Length)
231                                         buf = new byte[checked (buf.LongLength * 2)];
232                                 else
233                                         return UnixEncoding.Instance.GetString (buf, 0, checked ((int) r));
234                         } while (true);
235                 }
236
237                 public static string TryReadLinkAt (int dirfd, string path)
238                 {
239                         byte[] buf = new byte[256];
240                         do {
241                                 long r = Native.Syscall.readlinkat (dirfd, path, buf);
242                                 if (r < 0)
243                                         return null;
244                                 else if (r == buf.Length)
245                                         buf = new byte[checked (buf.LongLength * 2)];
246                                 else
247                                         return UnixEncoding.Instance.GetString (buf, 0, checked ((int) r));
248                         } while (true);
249                 }
250
251                 public static string ReadLink (string path)
252                 {
253                         string target = TryReadLink (path);
254                         if (target == null)
255                                 UnixMarshal.ThrowExceptionForLastError (); 
256                         return target;
257                 }
258
259                 public static string ReadLinkAt (int dirfd, string path)
260                 {
261                         string target = TryReadLinkAt (dirfd, path);
262                         if (target == null)
263                                 UnixMarshal.ThrowExceptionForLastError (); 
264                         return target;
265                 }
266
267                 public static bool IsPathRooted (string path)
268                 {
269                         if (path == null || path.Length == 0)
270                                 return false;
271                         return path [0] == DirectorySeparatorChar;
272                 }
273
274                 internal static void CheckPath (string path)
275                 {
276                         if (path == null)
277                                 throw new ArgumentNullException ();
278                         if (path.Length == 0)
279                                 throw new ArgumentException ("Path cannot contain a zero-length string", "path");
280                         if (path.IndexOfAny (_InvalidPathChars) != -1)
281                                 throw new ArgumentException ("Invalid characters in path.", "path");
282                 }
283         }
284 }
285
286 // vim: noexpandtab