private static TypeInfoDataBase LoadFromFileHelper(Collection <PSSnapInTypeAndFormatErrors> files, MshExpressionFactory expressionFactory, AuthorizationManager authorizationManager, PSHost host, bool preValidated, out List <XmlLoaderLoggerEntry> logEntries, out bool success) { success = true; logEntries = new List <XmlLoaderLoggerEntry>(); TypeInfoDataBase db = new TypeInfoDataBase(); AddPreLoadInstrinsics(db); foreach (PSSnapInTypeAndFormatErrors errors in files) { if (errors.FormatData != null) { using (TypeInfoDataBaseLoader loader = new TypeInfoDataBaseLoader()) { if (!loader.LoadFormattingData(errors.FormatData, db, expressionFactory)) { success = false; } foreach (XmlLoaderLoggerEntry entry in loader.LogEntries) { if (entry.entryType == XmlLoaderLoggerEntry.EntryType.Error) { string item = StringUtil.Format(FormatAndOutXmlLoadingStrings.MshSnapinQualifiedError, errors.PSSnapinName, entry.message); errors.Errors.Add(item); } } logEntries.AddRange(loader.LogEntries); continue; } } XmlFileLoadInfo info = new XmlFileLoadInfo(Path.GetPathRoot(errors.FullPath), errors.FullPath, errors.Errors, errors.PSSnapinName); using (TypeInfoDataBaseLoader loader2 = new TypeInfoDataBaseLoader()) { if (!loader2.LoadXmlFile(info, db, expressionFactory, authorizationManager, host, preValidated)) { success = false; } foreach (XmlLoaderLoggerEntry entry2 in loader2.LogEntries) { if (entry2.entryType == XmlLoaderLoggerEntry.EntryType.Error) { string str2 = StringUtil.Format(FormatAndOutXmlLoadingStrings.MshSnapinQualifiedError, info.psSnapinName, entry2.message); info.errors.Add(str2); if (entry2.failToLoadFile) { errors.FailToLoadFile = true; } } } logEntries.AddRange(loader2.LogEntries); } } AddPostLoadInstrinsics(db); return(db); }
internal bool LoadXmlFile(XmlFileLoadInfo info, TypeInfoDataBase db, MshExpressionFactory expressionFactory, AuthorizationManager authorizationManager, PSHost host, bool preValidated) { if (info == null) { throw PSTraceSource.NewArgumentNullException("info"); } if (info.filePath == null) { throw PSTraceSource.NewArgumentNullException("info.filePath"); } if (db == null) { throw PSTraceSource.NewArgumentNullException("db"); } if (expressionFactory == null) { throw PSTraceSource.NewArgumentNullException("expressionFactory"); } base.displayResourceManagerCache = db.displayResourceManagerCache; base.expressionFactory = expressionFactory; base.SetDatabaseLoadingInfo(info); base.ReportTrace("loading file started"); XmlDocument doc = null; bool isFullyTrusted = false; doc = base.LoadXmlDocumentFromFileLoadingInfo(authorizationManager, host, out isFullyTrusted); if (SystemPolicy.GetSystemLockdownPolicy() == SystemEnforcementMode.Enforce) { base.SetLoadingInfoIsFullyTrusted(isFullyTrusted); } if (doc == null) { return false; } bool suppressValidation = this.suppressValidation; try { this.suppressValidation = preValidated; try { this.LoadData(doc, db); } catch (TooManyErrorsException) { return false; } catch (Exception exception) { base.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.ErrorInFile, base.FilePath, exception.Message)); throw; } if (base.HasErrors) { return false; } } finally { this.suppressValidation = suppressValidation; } base.ReportTrace("file loaded with no errors"); return true; }
/// <summary> /// it loads a database from file(s). /// </summary> /// <param name="files">*.formal.xml files to be loaded</param> /// <param name="expressionFactory">expression factory to validate script blocks</param> /// <param name="authorizationManager"> /// Authorization manager to perform signature checks before reading ps1xml files (or null of no checks are needed) /// </param> /// <param name="host"> /// Host passed to <paramref name="authorizationManager"/>. Can be null if no interactive questions should be asked. /// </param> /// <param name="preValidated"> /// True if the format data has been pre-validated (build time, manual testing, etc) so that validation can be /// skipped at runtime. /// </param> /// <param name="logEntries">list of logger entries (errors, etc.) to return to the caller</param> /// <param name="success"> true if no error occurred</param> /// <returns>a database instance loaded from file(s)</returns> private static TypeInfoDataBase LoadFromFileHelper( Collection <PSSnapInTypeAndFormatErrors> files, PSPropertyExpressionFactory expressionFactory, AuthorizationManager authorizationManager, PSHost host, bool preValidated, out List <XmlLoaderLoggerEntry> logEntries, out bool success) { success = true; // Holds the aggregated log entries for all files... logEntries = new List <XmlLoaderLoggerEntry>(); // fresh instance of the database TypeInfoDataBase db = new TypeInfoDataBase(); // prepopulate the database with any necessary overriding data AddPreLoadIntrinsics(db); var etwEnabled = RunspaceEventSource.Log.IsEnabled(); // load the XML document into a copy of the // in memory database foreach (PSSnapInTypeAndFormatErrors file in files) { // Loads formatting data from ExtendedTypeDefinition instance if (file.FormatData != null) { LoadFormatDataHelper(file.FormatData, expressionFactory, logEntries, ref success, file, db, isBuiltInFormatData: false, isForHelp: false); continue; } if (etwEnabled) { RunspaceEventSource.Log.ProcessFormatFileStart(file.FullPath); } if (!ProcessBuiltin(file, db, expressionFactory, logEntries, ref success)) { // Loads formatting data from formatting data XML file XmlFileLoadInfo info = new XmlFileLoadInfo(Path.GetPathRoot(file.FullPath), file.FullPath, file.Errors, file.PSSnapinName); using (TypeInfoDataBaseLoader loader = new TypeInfoDataBaseLoader()) { if (!loader.LoadXmlFile(info, db, expressionFactory, authorizationManager, host, preValidated)) { success = false; } foreach (XmlLoaderLoggerEntry entry in loader.LogEntries) { // filter in only errors from the current file... if (entry.entryType == XmlLoaderLoggerEntry.EntryType.Error) { string mshsnapinMessage = StringUtil.Format(FormatAndOutXmlLoadingStrings.MshSnapinQualifiedError, info.psSnapinName, entry.message); info.errors.Add(mshsnapinMessage); if (entry.failToLoadFile) { file.FailToLoadFile = true; } } } // now aggregate the entries... logEntries.AddRange(loader.LogEntries); } } if (etwEnabled) { RunspaceEventSource.Log.ProcessFormatFileStop(file.FullPath); } } // add any sensible defaults to the database AddPostLoadIntrinsics(db); return(db); }
/// <summary> /// entry point for the loader algorithm /// </summary> /// <param name="info">information needed to load the file</param> /// <param name="db">database instance to load the file into</param> /// <param name="expressionFactory">expression factory to validate script blocks</param> /// <param name="authorizationManager"> /// Authorization manager to perform signature checks before reading ps1xml files (or null of no checks are needed) /// </param> /// <param name="host"> /// Host passed to <paramref name="authorizationManager"/>. Can be null if no interactive questions should be asked. /// </param> /// <param name="preValidated"> /// True if the format data has been pre-validated (build time, manual testing, etc) so that validation can be /// skipped at runtime. /// </param> /// <returns>true if successful</returns> internal bool LoadXmlFile( XmlFileLoadInfo info, TypeInfoDataBase db, MshExpressionFactory expressionFactory, AuthorizationManager authorizationManager, PSHost host, bool preValidated) { if (info == null) throw PSTraceSource.NewArgumentNullException("info"); if (info.filePath == null) throw PSTraceSource.NewArgumentNullException("info.filePath"); if (db == null) throw PSTraceSource.NewArgumentNullException("db"); if (expressionFactory == null) throw PSTraceSource.NewArgumentNullException("expressionFactory"); if (SecuritySupport.IsProductBinary(info.filePath)) { this.SetLoadingInfoIsProductCode(true); } this.displayResourceManagerCache = db.displayResourceManagerCache; this.expressionFactory = expressionFactory; this.SetDatabaseLoadingInfo(info); this.ReportTrace("loading file started"); // load file into XML document XmlDocument newDocument = null; bool isFullyTrusted = false; newDocument = LoadXmlDocumentFromFileLoadingInfo(authorizationManager, host, out isFullyTrusted); // If we're not in a locked-down environment, types and formatting are allowed based just on the authorization // manager. If we are in a locked-down environment, additionally check the system policy. if (SystemPolicy.GetSystemLockdownPolicy() == SystemEnforcementMode.Enforce) { SetLoadingInfoIsFullyTrusted(isFullyTrusted); } if (newDocument == null) { return false; } // load the XML document into a copy of the // in memory database bool previousSuppressValidation = _suppressValidation; try { _suppressValidation = preValidated; try { this.LoadData(newDocument, db); } catch (TooManyErrorsException) { // already logged an error before throwing return false; } catch (Exception e) // will rethrow { //Error in file {0}: {1} this.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.ErrorInFile, FilePath, e.Message)); throw; } if (this.HasErrors) { return false; } } finally { _suppressValidation = previousSuppressValidation; } this.ReportTrace("file loaded with no errors"); return true; }
protected void SetDatabaseLoadingInfo(XmlFileLoadInfo info) { _loadingInfo.fileDirectory = info.fileDirectory; _loadingInfo.filePath = info.filePath; }
private static TypeInfoDataBase LoadFromFileHelper(Collection<PSSnapInTypeAndFormatErrors> files, MshExpressionFactory expressionFactory, AuthorizationManager authorizationManager, PSHost host, bool preValidated, out List<XmlLoaderLoggerEntry> logEntries, out bool success) { success = true; logEntries = new List<XmlLoaderLoggerEntry>(); TypeInfoDataBase db = new TypeInfoDataBase(); AddPreLoadInstrinsics(db); foreach (PSSnapInTypeAndFormatErrors errors in files) { if (errors.FormatData != null) { using (TypeInfoDataBaseLoader loader = new TypeInfoDataBaseLoader()) { if (!loader.LoadFormattingData(errors.FormatData, db, expressionFactory)) { success = false; } foreach (XmlLoaderLoggerEntry entry in loader.LogEntries) { if (entry.entryType == XmlLoaderLoggerEntry.EntryType.Error) { string item = StringUtil.Format(FormatAndOutXmlLoadingStrings.MshSnapinQualifiedError, errors.PSSnapinName, entry.message); errors.Errors.Add(item); } } logEntries.AddRange(loader.LogEntries); continue; } } XmlFileLoadInfo info = new XmlFileLoadInfo(Path.GetPathRoot(errors.FullPath), errors.FullPath, errors.Errors, errors.PSSnapinName); using (TypeInfoDataBaseLoader loader2 = new TypeInfoDataBaseLoader()) { if (!loader2.LoadXmlFile(info, db, expressionFactory, authorizationManager, host, preValidated)) { success = false; } foreach (XmlLoaderLoggerEntry entry2 in loader2.LogEntries) { if (entry2.entryType == XmlLoaderLoggerEntry.EntryType.Error) { string str2 = StringUtil.Format(FormatAndOutXmlLoadingStrings.MshSnapinQualifiedError, info.psSnapinName, entry2.message); info.errors.Add(str2); if (entry2.failToLoadFile) { errors.FailToLoadFile = true; } } } logEntries.AddRange(loader2.LogEntries); } } AddPostLoadInstrinsics(db); return db; }
/// <summary> /// it loads a database from file(s). /// </summary> /// <param name="files">*.formal.xml files to be loaded</param> /// <param name="expressionFactory">expression factory to validate script blocks</param> /// <param name="authorizationManager"> /// Authorization manager to perform signature checks before reading ps1xml files (or null of no checks are needed) /// </param> /// <param name="host"> /// Host passed to <paramref name="authorizationManager"/>. Can be null if no interactive questions should be asked. /// </param> /// <param name="preValidated"> /// True if the format data has been pre-validated (build time, manual testing, etc) so that validation can be /// skipped at runtime. /// </param> /// <param name="logEntries">list of logger entries (errors, etc.) to return to the caller</param> /// <param name="success"> true if no error occurred</param> /// <returns>a database instance loaded from file(s)</returns> private static TypeInfoDataBase LoadFromFileHelper( Collection<PSSnapInTypeAndFormatErrors> files, MshExpressionFactory expressionFactory, AuthorizationManager authorizationManager, PSHost host, bool preValidated, out List<XmlLoaderLoggerEntry> logEntries, out bool success) { success = true; // Holds the aggregated log entries for all files... logEntries = new List<XmlLoaderLoggerEntry>(); // fresh instance of the database TypeInfoDataBase db = new TypeInfoDataBase(); // prepopulate the database with any necessary overriding data AddPreLoadInstrinsics(db); var etwEnabled = RunspaceEventSource.Log.IsEnabled(); // load the XML document into a copy of the // in memory database foreach (PSSnapInTypeAndFormatErrors file in files) { // Loads formatting data from ExtendedTypeDefinition instance if (file.FormatData != null) { LoadFormatDataHelper(file.FormatData, expressionFactory, logEntries, ref success, file, db, isBuiltInFormatData: false, isForHelp: false); continue; } if (etwEnabled) RunspaceEventSource.Log.ProcessFormatFileStart(file.FullPath); if (!ProcessBuiltin(file, db, expressionFactory, logEntries, ref success)) { // Loads formatting data from formatting data XML file XmlFileLoadInfo info = new XmlFileLoadInfo(Path.GetPathRoot(file.FullPath), file.FullPath, file.Errors, file.PSSnapinName); using (TypeInfoDataBaseLoader loader = new TypeInfoDataBaseLoader()) { if (!loader.LoadXmlFile(info, db, expressionFactory, authorizationManager, host, preValidated)) success = false; foreach (XmlLoaderLoggerEntry entry in loader.LogEntries) { // filter in only errors from the current file... if (entry.entryType == XmlLoaderLoggerEntry.EntryType.Error) { string mshsnapinMessage = StringUtil.Format(FormatAndOutXmlLoadingStrings.MshSnapinQualifiedError, info.psSnapinName, entry.message); info.errors.Add(mshsnapinMessage); if (entry.failToLoadFile) { file.FailToLoadFile = true; } } } // now aggregate the entries... logEntries.AddRange(loader.LogEntries); } } if (etwEnabled) RunspaceEventSource.Log.ProcessFormatFileStop(file.FullPath); } // add any sensible defaults to the database AddPostLoadInstrinsics(db); return db; }