* make-map.cs: Add a MphPrototypeFileGenerator, which looks for DllImport
[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 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[] InvalidPathChars = new char[]{'\0'};
43                 public static readonly char PathSeparator = ':';
44                 public static readonly char VolumeSeparatorChar = '/';
45
46                 public static string Combine (string path1, params string[] paths)
47                 {
48                         if (path1 == null)
49                                 throw new ArgumentNullException ("path1");
50                         if (paths == null)
51                                 throw new ArgumentNullException ("paths");
52                         if (path1.IndexOfAny (InvalidPathChars) != -1)
53                                 throw new ArgumentException ("Illegal characters in path", "path1");
54
55                         int len = path1.Length + 1;
56                         for (int i = 0; i < paths.Length; ++i) {
57                                 if (paths [i] == null)
58                                         throw new ArgumentNullException ("paths");
59                                 len += paths [i].Length + 1;
60                         }
61
62                         StringBuilder sb = new StringBuilder (len);
63                         sb.Append (path1);
64                         for (int i = 0; i < paths.Length; ++i)
65                                 Combine (sb, paths [i]);
66                         return sb.ToString ();
67                 }
68
69                 private static void Combine (StringBuilder path, string part)
70                 {
71                         if (part.IndexOfAny (InvalidPathChars) != -1)
72                                 throw new ArgumentException ("Illegal characters in path", "path1");
73                         char end = path [path.Length-1];
74                         if (end != DirectorySeparatorChar && 
75                                         end != AltDirectorySeparatorChar && 
76                                         end != VolumeSeparatorChar)
77                                 path.Append (DirectorySeparatorChar);
78                         path.Append (part);
79                 }
80
81                 public static string GetDirectoryName (string path)
82                 {
83                         CheckPath (path);
84
85                         int lastDir = path.LastIndexOf (DirectorySeparatorChar);
86                         if (lastDir > 0)
87                                 return path.Substring (0, lastDir);
88                         return "";
89                 }
90
91                 public static string GetFileName (string path)
92                 {
93                         if (path == null || path.Length == 0)
94                                 return path;
95
96                         int lastDir = path.LastIndexOf (DirectorySeparatorChar);
97                         if (lastDir >= 0)
98                                 return path.Substring (lastDir+1);
99
100                         return path;
101                 }
102
103                 public static string GetFullPath (string path)
104                 {
105                         path = _GetFullPath (path);
106                         return GetCanonicalPath (path);
107                 }
108
109                 private static string _GetFullPath (string path)
110                 {
111                         if (path == null)
112                                 throw new ArgumentNullException ("path");
113                         if (!IsPathRooted (path))
114                                 path = UnixDirectory.GetCurrentDirectory() + DirectorySeparatorChar + path;
115
116                         return path;
117                 }
118
119                 public static string GetCanonicalPath (string path)
120                 {
121                         string [] dirs;
122                         int lastIndex;
123                         GetPathComponents (path, out dirs, out lastIndex);
124                         string end = string.Join ("/", dirs, 0, lastIndex);
125                         return IsPathRooted (path) ? "/" + end : end;
126                 }
127
128                 private static void GetPathComponents (string path, 
129                         out string[] components, out int lastIndex)
130                 {
131                         string [] dirs = path.Split (DirectorySeparatorChar);
132                         int target = 0;
133                         for (int i = 0; i < dirs.Length; ++i) {
134                                 if (dirs [i] == "." || dirs [i] == string.Empty) continue;
135                                 else if (dirs [i] == "..") {
136                                         if (target != 0) --target;
137                                         else ++target;
138                                 }
139                                 else
140                                         dirs [target++] = dirs [i];
141                         }
142                         components = dirs;
143                         lastIndex = target;
144                 }
145
146                 public static string GetPathRoot (string path)
147                 {
148                         if (path == null)
149                                 return null;
150                         if (!IsPathRooted (path))
151                                 return "";
152                         return "/";
153                 }
154
155                 public static string GetCompleteRealPath (string path)
156                 {
157                         if (path == null)
158                                 throw new ArgumentNullException ("path");
159                         string [] dirs;
160                         int lastIndex;
161                         GetPathComponents (path, out dirs, out lastIndex);
162                         StringBuilder realPath = new StringBuilder ();
163                         if (dirs.Length > 0) {
164                                 string dir = IsPathRooted (path) ? "/" : "";
165                                 dir += dirs [0];
166                                 realPath.Append (GetRealPath (dir));
167                         }
168                         for (int i = 1; i < lastIndex; ++i) {
169                                 realPath.Append ("/").Append (dirs [i]);
170                                 string p = GetRealPath (realPath.ToString());
171                                 realPath.Remove (0, realPath.Length);
172                                 realPath.Append (p);
173                         }
174                         return realPath.ToString ();
175                 }
176
177                 public static string GetRealPath (string path)
178                 {
179                         do {
180                                 string name = ReadSymbolicLink (path);
181                                 if (name == null)
182                                         return path;
183                                 if (IsPathRooted (name))
184                                         path = name;
185                                 else {
186                                         path = GetDirectoryName (path) + DirectorySeparatorChar + name;
187                                         path = GetCanonicalPath (path);
188                                 }
189                         } while (true);
190                 }
191
192                 // Read the specified symbolic link.  If the file isn't a symbolic link,
193                 // return null; otherwise, return the contents of the symbolic link.
194                 //
195                 // readlink(2) is horribly evil, as there is no way to query how big the
196                 // symlink contents are.  Consequently, it's trial and error...
197                 internal static string ReadSymbolicLink (string path)
198                 {
199                         StringBuilder buf = new StringBuilder (256);
200                         do {
201                                 int r = Syscall.readlink (path, buf);
202                                 if (r < 0) {
203                                         Error e;
204                                         switch (e = Syscall.GetLastError()) {
205                                         case Error.EINVAL:
206                                                 // path isn't a symbolic link
207                                                 return null;
208                                         default:
209                                                 UnixMarshal.ThrowExceptionForError (e);
210                                                 break;
211                                         }
212                                 }
213                                 else if (r == buf.Capacity) {
214                                         buf.Capacity *= 2;
215                                 }
216                                 else
217                                         return buf.ToString (0, r);
218                         } while (true);
219                 }
220
221                 public static bool IsPathRooted (string path)
222                 {
223                         if (path == null || path.Length == 0)
224                                 return false;
225                         return path [0] == DirectorySeparatorChar;
226                 }
227
228                 internal static void CheckPath (string path)
229                 {
230                         if (path == null)
231                                 throw new ArgumentNullException ();
232                         if (path.IndexOfAny (UnixPath.InvalidPathChars) != -1)
233                                 throw new ArgumentException ("Invalid characters in path.");
234                 }
235         }
236 }
237
238 // vim: noexpandtab