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