InternalConfigHost
using System.IO;
namespace System.Configuration.Internal
{
internal sealed class InternalConfigHost : IInternalConfigHost
{
private const FileAttributes InvalidAttributesForWrite = FileAttributes.ReadOnly | FileAttributes.Hidden;
bool IInternalConfigHost.SupportsChangeNotifications {
get {
return false;
}
}
bool IInternalConfigHost.SupportsRefresh {
get {
return false;
}
}
bool IInternalConfigHost.SupportsPath {
get {
return false;
}
}
bool IInternalConfigHost.SupportsLocation {
get {
return false;
}
}
bool IInternalConfigHost.IsRemote {
get {
return false;
}
}
void IInternalConfigHost.Init(IInternalConfigRoot configRoot, params object[] hostInitParams)
{
}
void IInternalConfigHost.InitForConfiguration(ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot configRoot, params object[] hostInitConfigurationParams)
{
configPath = null;
locationConfigPath = null;
}
bool IInternalConfigHost.IsConfigRecordRequired(string configPath)
{
return true;
}
bool IInternalConfigHost.IsInitDelayed(IInternalConfigRecord configRecord)
{
return false;
}
void IInternalConfigHost.RequireCompleteInit(IInternalConfigRecord configRecord)
{
}
public bool IsSecondaryRoot(string configPath)
{
return false;
}
string IInternalConfigHost.GetStreamName(string configPath)
{
throw ExceptionUtil.UnexpectedError("IInternalConfigHost.GetStreamName");
}
string IInternalConfigHost.GetStreamNameForConfigSource(string streamName, string configSource)
{
return StaticGetStreamNameForConfigSource(streamName, configSource);
}
object IInternalConfigHost.GetStreamVersion(string streamName)
{
return StaticGetStreamVersion(streamName);
}
Stream IInternalConfigHost.OpenStreamForRead(string streamName)
{
return StaticOpenStreamForRead(streamName);
}
Stream IInternalConfigHost.OpenStreamForRead(string streamName, bool assertPermissions)
{
return StaticOpenStreamForRead(streamName);
}
Stream IInternalConfigHost.OpenStreamForWrite(string streamName, string templateStreamName, ref object writeContext)
{
return StaticOpenStreamForWrite(streamName, templateStreamName, ref writeContext);
}
Stream IInternalConfigHost.OpenStreamForWrite(string streamName, string templateStreamName, ref object writeContext, bool assertPermissions)
{
return StaticOpenStreamForWrite(streamName, templateStreamName, ref writeContext);
}
void IInternalConfigHost.WriteCompleted(string streamName, bool success, object writeContext)
{
StaticWriteCompleted(streamName, success, writeContext);
}
void IInternalConfigHost.WriteCompleted(string streamName, bool success, object writeContext, bool assertPermissions)
{
StaticWriteCompleted(streamName, success, writeContext);
}
void IInternalConfigHost.DeleteStream(string streamName)
{
StaticDeleteStream(streamName);
}
bool IInternalConfigHost.IsFile(string streamName)
{
return StaticIsFile(streamName);
}
object IInternalConfigHost.StartMonitoringStreamForChanges(string streamName, StreamChangeCallback callback)
{
throw ExceptionUtil.UnexpectedError("IInternalConfigHost.StartMonitoringStreamForChanges");
}
void IInternalConfigHost.StopMonitoringStreamForChanges(string streamName, StreamChangeCallback callback)
{
throw ExceptionUtil.UnexpectedError("IInternalConfigHost.StopMonitoringStreamForChanges");
}
bool IInternalConfigHost.IsDefinitionAllowed(string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
{
return true;
}
void IInternalConfigHost.VerifyDefinitionAllowed(string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
{
}
bool IInternalConfigHost.IsAboveApplication(string configPath)
{
throw ExceptionUtil.UnexpectedError("IInternalConfigHost.IsAboveApplication");
}
string IInternalConfigHost.GetConfigPathFromLocationSubPath(string configPath, string locationSubPath)
{
throw ExceptionUtil.UnexpectedError("IInternalConfigHost.GetConfigPathFromLocationSubPath");
}
bool IInternalConfigHost.IsLocationApplicable(string configPath)
{
throw ExceptionUtil.UnexpectedError("IInternalConfigHost.IsLocationApplicable");
}
bool IInternalConfigHost.PrefetchAll(string configPath, string streamName)
{
return false;
}
bool IInternalConfigHost.PrefetchSection(string sectionGroupName, string sectionName)
{
return false;
}
object IInternalConfigHost.CreateDeprecatedConfigContext(string configPath)
{
throw ExceptionUtil.UnexpectedError("IInternalConfigHost.CreateDeprecatedConfigContext");
}
object IInternalConfigHost.CreateConfigurationContext(string configPath, string locationSubPath)
{
throw ExceptionUtil.UnexpectedError("IInternalConfigHost.CreateConfigurationContext");
}
string IInternalConfigHost.DecryptSection(string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
{
return ProtectedConfigurationSection.DecryptSection(encryptedXml, protectionProvider);
}
string IInternalConfigHost.EncryptSection(string clearTextXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
{
return ProtectedConfigurationSection.EncryptSection(clearTextXml, protectionProvider);
}
Type IInternalConfigHost.GetConfigType(string typeName, bool throwOnError)
{
return Type.GetType(typeName, throwOnError);
}
string IInternalConfigHost.GetConfigTypeName(Type t)
{
return t.AssemblyQualifiedName;
}
internal static string StaticGetStreamNameForConfigSource(string streamName, string configSource)
{
if (!Path.IsPathRooted(streamName))
throw ExceptionUtil.ParameterInvalid("streamName");
streamName = Path.GetFullPath(streamName);
string directoryOrRootName = UrlPath.GetDirectoryOrRootName(streamName);
string path = Path.Combine(directoryOrRootName, configSource);
path = Path.GetFullPath(path);
string directoryOrRootName2 = UrlPath.GetDirectoryOrRootName(path);
if (!UrlPath.IsEqualOrSubdirectory(directoryOrRootName, directoryOrRootName2))
throw new ArgumentException(string.Format(System.SR.Config_source_not_under_config_dir, configSource));
return path;
}
internal static FileVersion StaticGetStreamVersion(string streamName)
{
FileInfo fileInfo = new FileInfo(streamName);
if (!fileInfo.Exists)
return new FileVersion(false, 0, DateTime.MinValue, DateTime.MinValue);
return new FileVersion(true, fileInfo.Length, fileInfo.CreationTimeUtc, fileInfo.LastWriteTimeUtc);
}
internal static Stream StaticOpenStreamForRead(string streamName)
{
if (string.IsNullOrEmpty(streamName))
throw ExceptionUtil.UnexpectedError("InternalConfigHost::StaticOpenStreamForRead");
if (File.Exists(streamName))
return new FileStream(streamName, FileMode.Open, FileAccess.Read, FileShare.Read);
return null;
}
internal static Stream StaticOpenStreamForWrite(string streamName, string templateStreamName, ref object writeContext)
{
if (string.IsNullOrEmpty(streamName))
throw new ConfigurationErrorsException(System.SR.Config_no_stream_to_write);
string directoryName = Path.GetDirectoryName(streamName);
if (!Directory.Exists(directoryName))
Directory.CreateDirectory(directoryName);
WriteFileContext writeFileContext = null;
Stream result;
try {
writeFileContext = new WriteFileContext(streamName, templateStreamName);
if (File.Exists(streamName)) {
FileInfo fileInfo = new FileInfo(streamName);
FileAttributes attributes = fileInfo.Attributes;
if ((attributes & (FileAttributes.ReadOnly | FileAttributes.Hidden)) != 0)
throw new IOException(string.Format(System.SR.Config_invalid_attributes_for_write, streamName));
}
try {
result = new FileStream(writeFileContext.TempNewFilename, FileMode.Create, FileAccess.Write, FileShare.Read);
} catch (Exception inner) {
throw new ConfigurationErrorsException(string.Format(System.SR.Config_write_failed, streamName), inner);
}
} catch {
writeFileContext?.Complete(streamName, false);
throw;
}
writeContext = writeFileContext;
return result;
}
internal static void StaticWriteCompleted(string streamName, bool success, object writeContext)
{
((WriteFileContext)writeContext).Complete(streamName, success);
}
internal static void StaticDeleteStream(string streamName)
{
File.Delete(streamName);
}
internal static bool StaticIsFile(string streamName)
{
return Path.IsPathRooted(streamName);
}
public bool IsTrustedConfigPath(string configPath)
{
return true;
}
public bool IsFullTrustSectionWithoutAptcaAllowed(IInternalConfigRecord configRecord)
{
return true;
}
public IDisposable Impersonate()
{
return new DummyDisposable();
}
}
}