public void AddDirectory(string path) { var fixedPath = FixPath(path, true); var separator = XFS.Separator(); lock (files) { if (FileExists(fixedPath) && (files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.ACCESS_TO_THE_PATH_IS_DENIED, fixedPath)); } if (forbiddenDirectories.Exists(directory => fixedPath.StartsWith(directory, StringComparison.OrdinalIgnoreCase))) { throw new DirectoryNotFoundException(string.Format("The parent directory of {0} cannot be written to.", fixedPath)); } var lastIndex = 0; bool isUnc = fixedPath.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase) || fixedPath.StartsWith(@"//", StringComparison.OrdinalIgnoreCase); if (isUnc) { //First, confirm they aren't trying to create '\\server\' lastIndex = fixedPath.IndexOf(separator, 2, StringComparison.OrdinalIgnoreCase); if (lastIndex < 0) { throw new ArgumentException(@"The UNC path should be of the form \\server\share.", "path"); } /* * Although CreateDirectory(@"\\server\share\") is not going to work in real code, we allow it here for the purposes of setting up test doubles. * See PR https://github.com/tathamoddie/System.IO.Abstractions/pull/90 for conversation */ } while ((lastIndex = fixedPath.IndexOf(separator, lastIndex + 1, StringComparison.OrdinalIgnoreCase)) > -1) { var segment = fixedPath.Substring(0, lastIndex + 1); if (!directory.Exists(segment)) { files[segment] = new MockDirectoryData(); } } var s = fixedPath.EndsWith(separator, StringComparison.OrdinalIgnoreCase) ? fixedPath : fixedPath + separator; files[s] = new MockDirectoryData(); } }
public void AddDirectory(string path) { var fixedPath = FixPath(path, true); var separator = XFS.Separator(); lock (_files) { if (FileExists(fixedPath) && (_files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("ACCESS_TO_THE_PATH_IS_DENIED") ?? throw new InvalidOperationException(), fixedPath)); } var lastIndex = 0; bool isUnc = fixedPath.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase) || fixedPath.StartsWith(@"//", StringComparison.OrdinalIgnoreCase); if (isUnc) { //First, confirm they aren't trying to create '\\server\' lastIndex = fixedPath.IndexOf(separator, 2, StringComparison.OrdinalIgnoreCase); if (lastIndex < 0) { throw new ArgumentException(@"The UNC path should be of the form \\server\share.", nameof(path)); } } while ((lastIndex = fixedPath.IndexOf(separator, lastIndex + 1, StringComparison.OrdinalIgnoreCase)) > -1) { var segment = fixedPath.Substring(0, lastIndex + 1); if (!_directory.Exists(segment)) { _files[segment] = new VirtualDirectoryData(); } } var s = fixedPath.EndsWith(separator, StringComparison.OrdinalIgnoreCase) ? fixedPath : fixedPath + separator; _files[s] = new VirtualDirectoryData(); } }
public void AddDirectory(string path) { var fixedPath = FixPath(path); var separator = XFS.Separator(); lock (files) { if (FileExists(path) && (files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, "Access to the path '{0}' is denied.", path)); } var lastIndex = 0; bool isUnc = path.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase) || path.StartsWith(@"//", StringComparison.OrdinalIgnoreCase); if (isUnc) { //First, confirm they aren't trying to create '\\server\' lastIndex = path.IndexOf(separator, 2); if (lastIndex < 0) { throw new ArgumentException(@"The UNC path should be of the form \\server\share.", "path"); } /* * Although CreateDirectory(@"\\server\share\") is not going to work in real code, we allow it here for the purposes of setting up test doubles. * See PR https://github.com/tathamoddie/System.IO.Abstractions/pull/90 for conversation */ } while ((lastIndex = path.IndexOf(separator, lastIndex + 1)) > -1) { var segment = path.Substring(0, lastIndex + 1); if (!directory.Exists(segment)) { files[segment] = new MockDirectoryData(); } } var s = path.EndsWith(separator, StringComparison.OrdinalIgnoreCase) ? path : path + separator; files[s] = new MockDirectoryData(); } }