// Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
// Copyright (C) 2002 Ximian, Inc. (http://www.ximian.com)
// Copyright (C) 2003 Ben Maurer
+// Copyright 2011 Xamarin Inc (http://www.xamarin.com).
//
// Author: Jim Richardson, develop@wtfo-guru.com
// Dan Lewis (dihlewis@yahoo.co.uk)
if (l >= 2 && DirectorySeparatorChar == '\\' && ret [l - 1] == VolumeSeparatorChar)
return ret + DirectorySeparatorChar;
+ else if (l == 1 && DirectorySeparatorChar == '\\' && path.Length >= 2 && path [nLast] == VolumeSeparatorChar)
+ return ret + VolumeSeparatorChar;
else {
//
// Important: do not use CanonicalizePath here, use
if ((path [1] != ':') || !Char.IsLetter (path [0]))
return path;
- string current = Directory.GetCurrentDirectory ();
+ string current = Directory.InsecureGetCurrentDirectory ();
// first, only the drive is specified
if (path.Length == 2) {
// then if the current directory is on the same drive
IsDsc (path [0]) &&
IsDsc (path [1])) {
if (path.Length == 2 || path.IndexOf (path [0], 2) < 0)
- throw new ArgumentException ("UNC pass should be of the form \\\\server\\share.");
+ throw new ArgumentException ("UNC paths should be of the form \\\\server\\share.");
if (path [0] != DirectorySeparatorChar)
path = path.Replace (AltDirectorySeparatorChar, DirectorySeparatorChar);
if (!IsPathRooted (path)) {
// avoid calling expensive CanonicalizePath when possible
- var start = 0;
- while ((start = path.IndexOf ('.', start)) != -1) {
- if (++start == path.Length || path [start] == DirectorySeparatorChar || path [start] == AltDirectorySeparatorChar)
- break;
+ if (!Environment.IsRunningOnWindows) {
+ var start = 0;
+ while ((start = path.IndexOf ('.', start)) != -1) {
+ if (++start == path.Length || path [start] == DirectorySeparatorChar || path [start] == AltDirectorySeparatorChar)
+ break;
+ }
+ canonicalize = start > 0;
}
- canonicalize = start > 0;
-
- path = Directory.GetCurrentDirectory () + DirectorySeparatorStr + path;
+
+ path = Directory.InsecureGetCurrentDirectory() + DirectorySeparatorStr + path;
} else if (DirectorySeparatorChar == '\\' &&
path.Length >= 2 &&
IsDsc (path [0]) &&
!IsDsc (path [1])) { // like `\abc\def'
- string current = Directory.GetCurrentDirectory ();
+ string current = Directory.InsecureGetCurrentDirectory();
if (current [1] == VolumeSeparatorChar)
path = current.Substring (0, 2) + path;
else
- path = current.Substring (0, current.IndexOf ('\\', current.IndexOf ("\\\\") + 1));
+ path = current.Substring (0, current.IndexOf ('\\', current.IndexOfOrdinalUnchecked ("\\\\") + 1));
}
}
string path;
Random rnd;
int num = 0;
+ int count = 0;
SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
f = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read,
8192, false, (FileOptions) 1);
}
- catch (SecurityException) {
- // avoid an endless loop
- throw;
- }
- catch (UnauthorizedAccessException) {
- // This can happen if we don't have write permission to /tmp
- throw;
- }
- catch (DirectoryNotFoundException) {
- // This happens when TMPDIR does not exist
- throw;
+ catch (IOException ex){
+ if (ex.hresult != MonoIO.FileAlreadyExistsHResult || count ++ > 65536)
+ throw;
}
- catch {
+ catch (UnauthorizedAccessException ex) {
+ if (count ++ > 65536)
+ throw new IOException (ex.Message, ex);
}
} while (f == null);
return String.Compare (subset, slast, path, slast, subset.Length - slast) == 0;
}
-#if NET_4_0 || MOONLIGHT
- public static string Combine (params string [] paths)
+#if NET_4_0
+ public
+#else
+ internal
+#endif
+ static string Combine (params string [] paths)
{
if (paths == null)
throw new ArgumentNullException ("paths");
- int l = 0;
- bool need_sep = false;
- foreach (var s in paths){
+ bool need_sep;
+ var ret = new StringBuilder ();
+ int pathsLen = paths.Length;
+ int slen;
+ foreach (var s in paths) {
+ need_sep = false;
if (s == null)
throw new ArgumentNullException ("One of the paths contains a null value", "paths");
if (s.IndexOfAny (InvalidPathChars) != -1)
throw new ArgumentException ("Illegal characters in path.");
- if (l == 0 && s.Length > 0){
- char p1end = s [s.Length - 1];
- if (p1end != DirectorySeparatorChar && p1end != AltDirectorySeparatorChar && p1end != VolumeSeparatorChar){
- need_sep = true;
- l += DirectorySeparatorStr.Length;
- }
- }
- }
- var ret = new StringBuilder (l);
- l = 0;
- foreach (var s in paths){
+
+ pathsLen--;
if (IsPathRooted (s))
- ret.Length = l = 0;
+ ret.Length = 0;
+
ret.Append (s);
- if (l == 0 && need_sep)
+ slen = s.Length;
+ if (slen > 0 && pathsLen > 0) {
+ char p1end = s [slen - 1];
+ if (p1end != DirectorySeparatorChar && p1end != AltDirectorySeparatorChar && p1end != VolumeSeparatorChar)
+ need_sep = true;
+ }
+
+ if (need_sep)
ret.Append (DirectorySeparatorStr);
- l = 1;
}
return ret.ToString ();
}
- public static string Combine (string path1, string path2, string path3)
+#if NET_4_0
+ public
+#else
+ internal
+#endif
+ static string Combine (string path1, string path2, string path3)
{
+ if (path1 == null)
+ throw new ArgumentNullException ("path1");
+
+ if (path2 == null)
+ throw new ArgumentNullException ("path2");
+
+ if (path3 == null)
+ throw new ArgumentNullException ("path3");
+
return Combine (new string [] { path1, path2, path3 });
}
- public static string Combine (string path1, string path2, string path3, string path4)
+#if NET_4_0
+ public
+#else
+ internal
+#endif
+ static string Combine (string path1, string path2, string path3, string path4)
{
+ if (path1 == null)
+ throw new ArgumentNullException ("path1");
+
+ if (path2 == null)
+ throw new ArgumentNullException ("path2");
+
+ if (path3 == null)
+ throw new ArgumentNullException ("path3");
+
+ if (path4 == null)
+ throw new ArgumentNullException ("path4");
+
return Combine (new string [] { path1, path2, path3, path4 });
}
-#endif
internal static void Validate (string path)
{
throw new ArgumentException (Locale.GetText ("Path is empty"));
if (path.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException (Locale.GetText ("Path contains invalid chars"));
-#if MOONLIGHT
- // On Moonlight (SL4+) there are some limitations in "Elevated Trust"
- if (SecurityManager.HasElevatedPermissions) {
+ if (Environment.IsRunningOnWindows) {
+ int idx = path.IndexOf (':');
+ if (idx >= 0 && idx != 1)
+ throw new ArgumentException (parameterName);
}
-#endif
}
}
}