internal static bool InternalExists(String path) { Contract.Requires(path != null); String tempPath = Path.AddLongPathPrefix(path); return(File.InternalExists(tempPath)); }
public override void CreateDirectory(string fullPath) { if (PathInternal.IsDirectoryTooLong(fullPath)) { throw new PathTooLongException(SR.IO_PathTooLong); } // We can save a bunch of work if the directory we want to create already exists. This also // saves us in the case where sub paths are inaccessible (due to ERROR_ACCESS_DENIED) but the // final path is accessable and the directory already exists. For example, consider trying // to create c:\Foo\Bar\Baz, where everything already exists but ACLS prevent access to c:\Foo // and c:\Foo\Bar. In that case, this code will think it needs to create c:\Foo, and c:\Foo\Bar // and fail to due so, causing an exception to be thrown. This is not what we want. if (DirectoryExists(fullPath)) { return; } List <string> stackDir = new List <string>(); // Attempt to figure out which directories don't exist, and only // create the ones we need. Note that InternalExists may fail due // to Win32 ACL's preventing us from seeing a directory, and this // isn't threadsafe. bool somepathexists = false; int length = fullPath.Length; // We need to trim the trailing slash or the code will try to create 2 directories of the same name. if (length >= 2 && PathHelpers.EndsInDirectorySeparator(fullPath)) { length--; } int lengthRoot = PathInternal.GetRootLength(fullPath); if (length > lengthRoot) { // Special case root (fullpath = X:\\) int i = length - 1; while (i >= lengthRoot && !somepathexists) { String dir = fullPath.Substring(0, i + 1); if (!DirectoryExists(dir)) // Create only the ones missing { stackDir.Add(dir); } else { somepathexists = true; } while (i > lengthRoot && !PathInternal.IsDirectorySeparator(fullPath[i])) { i--; } i--; } } int count = stackDir.Count; // If we were passed a DirectorySecurity, convert it to a security // descriptor and set it in he call to CreateDirectory. Interop.mincore.SECURITY_ATTRIBUTES secAttrs = default(Interop.mincore.SECURITY_ATTRIBUTES); bool r = true; int firstError = 0; String errorString = fullPath; // If all the security checks succeeded create all the directories while (stackDir.Count > 0) { String name = stackDir[stackDir.Count - 1]; stackDir.RemoveAt(stackDir.Count - 1); r = Interop.mincore.CreateDirectory(name, ref secAttrs); if (!r && (firstError == 0)) { int currentError = Marshal.GetLastWin32Error(); // While we tried to avoid creating directories that don't // exist above, there are at least two cases that will // cause us to see ERROR_ALREADY_EXISTS here. InternalExists // can fail because we didn't have permission to the // directory. Secondly, another thread or process could // create the directory between the time we check and the // time we try using the directory. Thirdly, it could // fail because the target does exist, but is a file. if (currentError != Interop.mincore.Errors.ERROR_ALREADY_EXISTS) { firstError = currentError; } else { // If there's a file in this directory's place, or if we have ERROR_ACCESS_DENIED when checking if the directory already exists throw. if (File.InternalExists(name) || (!DirectoryExists(name, out currentError) && currentError == Interop.mincore.Errors.ERROR_ACCESS_DENIED)) { firstError = currentError; errorString = name; } } } } // We need this check to mask OS differences // Handle CreateDirectory("X:\\") when X: doesn't exist. Similarly for n/w paths. if ((count == 0) && !somepathexists) { String root = Directory.InternalGetDirectoryRoot(fullPath); if (!DirectoryExists(root)) { throw Win32Marshal.GetExceptionForWin32Error(Interop.mincore.Errors.ERROR_PATH_NOT_FOUND, root); } return; } // Only throw an exception if creating the exact directory we // wanted failed to work correctly. if (!r && (firstError != 0)) { throw Win32Marshal.GetExceptionForWin32Error(firstError, errorString); } }
internal static bool InternalExists(string path) { return(File.InternalExists(Path.AddLongPathPrefix(path))); }
internal static unsafe void InternalCreateDirectory(string fullPath, string path, object dirSecurityObj) { DirectorySecurity security = (DirectorySecurity)dirSecurityObj; int length = fullPath.Length; if ((length >= 2) && Path.IsDirectorySeparator(fullPath[length - 1])) { length--; } int rootLength = Path.GetRootLength(fullPath); if ((length == 2) && Path.IsDirectorySeparator(fullPath[1])) { throw new IOException(Environment.GetResourceString("IO.IO_CannotCreateDirectory", new object[] { path })); } List <string> list = new List <string>(); bool flag = false; if (length > rootLength) { for (int i = length - 1; (i >= rootLength) && !flag; i--) { string str = fullPath.Substring(0, i + 1); if (!InternalExists(str)) { list.Add(str); } else { flag = true; } while (((i > rootLength) && (fullPath[i] != Path.DirectorySeparatorChar)) && (fullPath[i] != Path.AltDirectorySeparatorChar)) { i--; } } } int count = list.Count; if (list.Count != 0) { string[] array = new string[list.Count]; list.CopyTo(array, 0); for (int j = 0; j < array.Length; j++) { string[] strArray2; IntPtr ptr; (strArray2 = array)[(int)(ptr = (IntPtr)j)] = strArray2[(int)ptr] + @"\."; } AccessControlActions control = (security == null) ? AccessControlActions.None : AccessControlActions.Change; new FileIOPermission(FileIOPermissionAccess.Write, control, array, false, false).Demand(); } Win32Native.SECURITY_ATTRIBUTES structure = null; if (security != null) { structure = new Win32Native.SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf(structure) }; byte[] securityDescriptorBinaryForm = security.GetSecurityDescriptorBinaryForm(); byte * pDest = stackalloc byte[(IntPtr)securityDescriptorBinaryForm.Length]; Buffer.memcpy(securityDescriptorBinaryForm, 0, pDest, 0, securityDescriptorBinaryForm.Length); structure.pSecurityDescriptor = pDest; } bool flag2 = true; int errorCode = 0; string maybeFullPath = path; while (list.Count > 0) { string str3 = list[list.Count - 1]; list.RemoveAt(list.Count - 1); if (str3.Length >= 0xf8) { throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong")); } flag2 = Win32Native.CreateDirectory(str3, structure); if (!flag2 && (errorCode == 0)) { int lastError = Marshal.GetLastWin32Error(); if (lastError != 0xb7) { errorCode = lastError; } else if (File.InternalExists(str3) || (!InternalExists(str3, out lastError) && (lastError == 5))) { errorCode = lastError; try { new FileIOPermission(FileIOPermissionAccess.PathDiscovery, GetDemandDir(str3, true)).Demand(); maybeFullPath = str3; continue; } catch (SecurityException) { continue; } } } } if ((count == 0) && !flag) { if (!InternalExists(InternalGetDirectoryRoot(fullPath))) { __Error.WinIOError(3, InternalGetDirectoryRoot(path)); } } else if (!flag2 && (errorCode != 0)) { __Error.WinIOError(errorCode, maybeFullPath); } }