Imported Upstream version 5.0.0.42

Former-commit-id: fd56571888259555122d8a0f58c68838229cea2b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2017-04-10 11:41:01 +00:00
parent 1190d13a04
commit 6bdd276d05
19939 changed files with 3099680 additions and 93811 deletions

View File

@@ -48,9 +48,16 @@ using System.Security.AccessControl;
namespace System.IO
{
[ComVisible (true)]
public static class Directory
public static partial class Directory
{
public static string GetDirectoryRoot (string path)
{
Path.Validate (path);
SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
// FIXME nice hack but that does not work under windows
return new String(Path.DirectorySeparatorChar, 1);
}
public static DirectoryInfo CreateDirectory (string path)
{
@@ -251,77 +258,6 @@ namespace System.IO
return result;
}
public static string [] GetDirectories (string path)
{
return GetDirectories (path, "*");
}
public static string [] GetDirectories (string path, string searchPattern)
{
return GetFileSystemEntries (path, searchPattern, FileAttributes.Directory, FileAttributes.Directory);
}
public static string [] GetDirectories (string path, string searchPattern, SearchOption searchOption)
{
if (searchOption == SearchOption.TopDirectoryOnly)
return GetDirectories (path, searchPattern);
var all = new List<string> ();
GetDirectoriesRecurse (path, searchPattern, all);
return all.ToArray ();
}
static void GetDirectoriesRecurse (string path, string searchPattern, List<string> all)
{
all.AddRange (GetDirectories (path, searchPattern));
foreach (string dir in GetDirectories (path))
GetDirectoriesRecurse (dir, searchPattern, all);
}
public static string GetDirectoryRoot (string path)
{
Path.Validate (path);
SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
// FIXME nice hack but that does not work under windows
return new String(Path.DirectorySeparatorChar,1);
}
public static string [] GetFiles (string path)
{
return GetFiles (path, "*");
}
public static string [] GetFiles (string path, string searchPattern)
{
return GetFileSystemEntries (path, searchPattern, FileAttributes.Directory, 0);
}
public static string[] GetFiles (string path, string searchPattern, SearchOption searchOption)
{
if (searchOption == SearchOption.TopDirectoryOnly)
return GetFiles (path, searchPattern);
var all = new List<string> ();
GetFilesRecurse (path, searchPattern, all);
return all.ToArray ();
}
static void GetFilesRecurse (string path, string searchPattern, List<string> all)
{
all.AddRange (GetFiles (path, searchPattern));
foreach (string dir in GetDirectories (path))
GetFilesRecurse (dir, searchPattern, all);
}
public static string [] GetFileSystemEntries (string path)
{
return GetFileSystemEntries (path, "*");
}
public static string [] GetFileSystemEntries (string path, string searchPattern)
{
return GetFileSystemEntries (path, searchPattern, 0, 0);
}
public static string[] GetLogicalDrives ()
{
return Environment.GetLogicalDrives ();
@@ -443,195 +379,6 @@ namespace System.IO
SetLastWriteTime (path, lastWriteTimeUtc.ToLocalTime ());
}
// private
// Does the common validation, searchPattern has already been checked for not-null
static string ValidateDirectoryListing (string path, string searchPattern, out bool stop)
{
Path.Validate (path);
string wild = Path.Combine (path, searchPattern);
string wildpath = Path.GetDirectoryName (wild);
if (wildpath.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Pattern contains invalid characters", "pattern");
MonoIOError error;
if (!MonoIO.ExistsDirectory (wildpath, out error)) {
if (error == MonoIOError.ERROR_SUCCESS) {
MonoIOError file_error;
if (MonoIO.ExistsFile (wildpath, out file_error))
throw new IOException ("The directory name is invalid.");
}
if (error != MonoIOError.ERROR_PATH_NOT_FOUND)
throw MonoIO.GetException (wildpath, error);
if (wildpath.IndexOfAny (SearchPattern.WildcardChars) == -1)
throw new DirectoryNotFoundException ("Directory '" + wildpath + "' not found.");
if (path.IndexOfAny (SearchPattern.WildcardChars) == -1)
throw new ArgumentException ("Pattern is invalid", "searchPattern");
throw new ArgumentException ("Path is invalid", "path");
}
stop = false;
return wild;
}
private static string [] GetFileSystemEntries (string path, string searchPattern, FileAttributes mask, FileAttributes attrs)
{
if (searchPattern == null)
throw new ArgumentNullException ("searchPattern");
if (searchPattern.Length == 0)
return new string [] {};
bool stop;
string path_with_pattern = ValidateDirectoryListing (path, searchPattern, out stop);
if (stop)
return new string [] { path_with_pattern };
MonoIOError error;
string [] result = MonoIO.GetFileSystemEntries (path, path_with_pattern, (int) attrs, (int) mask, out error);
if (error != 0)
throw MonoIO.GetException (Path.GetDirectoryName (Path.Combine (path, searchPattern)), error);
return result;
}
public static string[] GetFileSystemEntries (string path, string searchPattern, SearchOption searchOption)
{
// Take the simple way home:
return new List<string> (EnumerateFileSystemEntries (path, searchPattern, searchOption)).ToArray ();
}
static void EnumerateCheck (string path, string searchPattern, SearchOption searchOption)
{
if (searchPattern == null)
throw new ArgumentNullException ("searchPattern");
if (searchPattern.Length == 0)
return;
if (searchOption != SearchOption.TopDirectoryOnly && searchOption != SearchOption.AllDirectories)
throw new ArgumentOutOfRangeException ("searchoption");
Path.Validate (path);
SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
}
internal static IEnumerable<string> EnumerateKind (string path, string searchPattern, SearchOption searchOption, FileAttributes kind)
{
if (searchPattern.Length == 0)
yield break;
bool stop;
string path_with_pattern = ValidateDirectoryListing (path, searchPattern, out stop);
if (stop){
yield return path_with_pattern;
yield break;
}
IntPtr handle;
MonoIOError error;
FileAttributes rattr;
string s = MonoIO.FindFirst (path, path_with_pattern, out rattr, out error, out handle);
try {
while (s != null) {
// Convert any file specific flag to FileAttributes.Normal which is used as include files flag
if (((rattr & FileAttributes.Directory) == 0) && rattr != 0)
rattr |= FileAttributes.Normal;
if ((rattr & kind) != 0)
yield return s;
s = MonoIO.FindNext (handle, out rattr, out error);
}
if (error != 0)
throw MonoIO.GetException (Path.GetDirectoryName (Path.Combine (path, searchPattern)), (MonoIOError) error);
} finally {
if (handle != IntPtr.Zero)
MonoIO.FindClose (handle);
}
if (searchOption == SearchOption.AllDirectories) {
s = MonoIO.FindFirst (path, Path.Combine (path, "*"), out rattr, out error, out handle);
try {
while (s != null) {
if ((rattr & FileAttributes.Directory) != 0 && (rattr & FileAttributes.ReparsePoint) == 0)
foreach (string child in EnumerateKind (s, searchPattern, searchOption, kind))
yield return child;
s = MonoIO.FindNext (handle, out rattr, out error);
}
if (error != 0)
throw MonoIO.GetException (path, (MonoIOError) error);
} finally {
if (handle != IntPtr.Zero)
MonoIO.FindClose (handle);
}
}
}
public static IEnumerable<string> EnumerateDirectories (string path, string searchPattern, SearchOption searchOption)
{
EnumerateCheck (path, searchPattern, searchOption);
return EnumerateKind (path, searchPattern, searchOption, FileAttributes.Directory);
}
public static IEnumerable<string> EnumerateDirectories (string path, string searchPattern)
{
EnumerateCheck (path, searchPattern, SearchOption.TopDirectoryOnly);
return EnumerateKind (path, searchPattern, SearchOption.TopDirectoryOnly, FileAttributes.Directory);
}
public static IEnumerable<string> EnumerateDirectories (string path)
{
Path.Validate (path); // no need for EnumerateCheck since we supply valid arguments
SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
return EnumerateKind (path, "*", SearchOption.TopDirectoryOnly, FileAttributes.Directory);
}
public static IEnumerable<string> EnumerateFiles (string path, string searchPattern, SearchOption searchOption)
{
EnumerateCheck (path, searchPattern, searchOption);
return EnumerateKind (path, searchPattern, searchOption, FileAttributes.Normal);
}
public static IEnumerable<string> EnumerateFiles (string path, string searchPattern)
{
EnumerateCheck (path, searchPattern, SearchOption.TopDirectoryOnly);
return EnumerateKind (path, searchPattern, SearchOption.TopDirectoryOnly, FileAttributes.Normal);
}
public static IEnumerable<string> EnumerateFiles (string path)
{
Path.Validate (path); // no need for EnumerateCheck since we supply valid arguments
SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
return EnumerateKind (path, "*", SearchOption.TopDirectoryOnly, FileAttributes.Normal);
}
public static IEnumerable<string> EnumerateFileSystemEntries (string path, string searchPattern, SearchOption searchOption)
{
EnumerateCheck (path, searchPattern, searchOption);
return EnumerateKind (path, searchPattern, searchOption, FileAttributes.Normal | FileAttributes.Directory);
}
public static IEnumerable<string> EnumerateFileSystemEntries (string path, string searchPattern)
{
EnumerateCheck (path, searchPattern, SearchOption.TopDirectoryOnly);
return EnumerateKind (path, searchPattern, SearchOption.TopDirectoryOnly, FileAttributes.Normal | FileAttributes.Directory);
}
public static IEnumerable<string> EnumerateFileSystemEntries (string path)
{
Path.Validate (path); // no need for EnumerateCheck since we supply valid arguments
SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
return EnumerateKind (path, "*", SearchOption.TopDirectoryOnly, FileAttributes.Normal | FileAttributes.Directory);
}
public static DirectorySecurity GetAccessControl (string path, AccessControlSections includeSections)
{
return new DirectorySecurity (path, includeSections);

View File

@@ -50,7 +50,7 @@ namespace System.IO
// The above is an internal value used by Path.GetTempFile to
// get a file with 600 permissions, regardless of the umask
// settings. If a value "1" must be introduced here, update
// both metadata/file-io.c and Path.GetTempFile
// both metadata/w32file.c and Path.GetTempFile
//
}
}

View File

@@ -188,6 +188,8 @@ namespace System.IO
throw new ArgumentException ("Name has invalid chars");
}
path = Path.InsecureGetFullPath (path);
if (Directory.Exists (path)) {
// don't leak the path information for isolated storage
string msg = Locale.GetText ("Access to the path '{0}' is denied.");
@@ -211,11 +213,7 @@ namespace System.IO
SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
string dname;
if (Path.DirectorySeparatorChar != '/' && path.IndexOf ('/') >= 0)
dname = Path.GetDirectoryName (Path.GetFullPath (path));
else
dname = Path.GetDirectoryName (path);
string dname = Path.GetDirectoryName (path);
if (dname.Length > 0) {
string fp = Path.GetFullPath (dname);
if (!Directory.Exists (fp)) {

View File

@@ -237,7 +237,16 @@ namespace System.IO
[MethodImplAttribute (MethodImplOptions.InternalCall)]
public extern static int FindClose (IntPtr handle);
[MethodImplAttribute (MethodImplOptions.InternalCall)]
public extern static IntPtr FindFirstFile (string path_with_pattern, out string fileName, out int fileAttr, out int error);
[MethodImplAttribute (MethodImplOptions.InternalCall)]
public extern static bool FindNextFile (IntPtr hnd, out string fileName, out int fileAttr, out int error);
[MethodImplAttribute (MethodImplOptions.InternalCall)]
public extern static bool FindCloseFile (IntPtr hnd);
public static bool Exists (string path, out MonoIOError error)
{
FileAttributes attrs = GetFileAttributes (path,

View File

@@ -284,7 +284,7 @@ namespace System.IO {
SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
#if !MOBILE
#if MONO_FEATURE_CAS
if (SecurityManager.SecurityEnabled) {
new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fullpath).Demand ();
}
@@ -297,7 +297,7 @@ namespace System.IO {
return InsecureGetFullPath (path);
}
#if !MOBILE
#if WIN_PLATFORM
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa364963%28v=vs.85%29.aspx
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern int GetFullPathName(string path, int numBufferChars, StringBuilder buffer, ref IntPtr lpFilePartOrNull);
@@ -318,11 +318,13 @@ namespace System.IO {
buffer = new StringBuilder(length);
GetFullPathName(path, length, buffer, ref ptr);
}
return buffer.ToString();
}
internal static string WindowsDriveAdjustment (string path)
{
// three special cases to consider when a drive is specified
if (path.Length < 2) {
if (path.Length == 1 && (path[0] == '\\' || path[0] == '/'))
@@ -365,7 +367,7 @@ namespace System.IO {
string msg = Locale.GetText ("The specified path is not of a legal form (empty).");
throw new ArgumentException (msg);
}
#if !MOBILE
#if WIN_PLATFORM
// adjust for drives, i.e. a special case for windows
if (Environment.IsRunningOnWindows)
path = WindowsDriveAdjustment (path);
@@ -732,6 +734,7 @@ namespace System.IO {
else {
string ret = String.Join (DirectorySeparatorStr, dirs, 0, target);
if (Environment.IsRunningOnWindows) {
#if WIN_PLATFORM
// append leading '\' of the UNC path that was lost in STEP 3.
if (isUnc)
ret = Path.DirectorySeparatorStr + ret;
@@ -757,6 +760,7 @@ namespace System.IO {
else
return current + ret;
}
#endif
} else {
if (root != "" && ret.Length > 0 && ret [0] != '/')
ret = root + ret;
@@ -876,11 +880,13 @@ namespace System.IO {
throw new ArgumentException (Locale.GetText ("Path is empty"));
if (path.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException (Locale.GetText ("Path contains invalid chars"));
#if WIN_PLATFORM
if (Environment.IsRunningOnWindows) {
int idx = path.IndexOf (':');
if (idx >= 0 && idx != 1)
throw new ArgumentException (parameterName);
}
#endif
}
internal static string DirectorySeparatorCharAsString {
@@ -890,5 +896,61 @@ namespace System.IO {
}
internal const int MAX_PATH = 260; // From WinDef.h
#region Copied from referencesource
// this was copied from corefx since it's not available in referencesource
internal static readonly char[] trimEndCharsWindows = { (char)0x9, (char)0xA, (char)0xB, (char)0xC, (char)0xD, (char)0x20, (char)0x85, (char)0xA0 };
internal static readonly char[] trimEndCharsUnix = { };
internal static char[] TrimEndChars => Environment.IsRunningOnWindows ? trimEndCharsWindows : trimEndCharsUnix;
// ".." can only be used if it is specified as a part of a valid File/Directory name. We disallow
// the user being able to use it to move up directories. Here are some examples eg
// Valid: a..b abc..d
// Invalid: ..ab ab.. .. abc..d\abc..
//
internal static void CheckSearchPattern(String searchPattern)
{
int index;
while ((index = searchPattern.IndexOf("..", StringComparison.Ordinal)) != -1) {
if (index + 2 == searchPattern.Length) // Terminal ".." . Files names cannot end in ".."
throw new ArgumentException(Environment.GetResourceString("Arg_InvalidSearchPattern"));
if ((searchPattern[index+2] == DirectorySeparatorChar)
|| (searchPattern[index+2] == AltDirectorySeparatorChar))
throw new ArgumentException(Environment.GetResourceString("Arg_InvalidSearchPattern"));
searchPattern = searchPattern.Substring(index + 2);
}
}
internal static void CheckInvalidPathChars(string path, bool checkAdditional = false)
{
if (path == null)
throw new ArgumentNullException("path");
if (PathInternal.HasIllegalCharacters(path, checkAdditional))
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPathChars"));
}
internal static String InternalCombine(String path1, String path2) {
if (path1==null || path2==null)
throw new ArgumentNullException((path1==null) ? "path1" : "path2");
CheckInvalidPathChars(path1);
CheckInvalidPathChars(path2);
if (path2.Length == 0)
throw new ArgumentException(Environment.GetResourceString("Argument_PathEmpty"), "path2");
if (IsPathRooted(path2))
throw new ArgumentException(Environment.GetResourceString("Arg_Path2IsRooted"), "path2");
int i = path1.Length;
if (i == 0) return path2;
char ch = path1[i - 1];
if (ch != DirectorySeparatorChar && ch != AltDirectorySeparatorChar && ch != VolumeSeparatorChar)
return path1 + DirectorySeparatorCharAsString + path2;
return path1 + path2;
}
#endregion
}
}