// Parameters: // assertPermissions - If true, then we'll assert all required permissions. Used by ClientSettingsConfigurationHost. // to allow low-trust apps to use ClientSettingsStore. static internal void StaticWriteCompleted(string streamName, bool success, object writeContext, bool assertPermissions) { WriteFileContext writeFileContext = (WriteFileContext)writeContext; bool revertAssert = false; if (assertPermissions) { // If asked to assert permissions, we will assert allAccess on the streamName, the temporary file // created by WriteContext, and also the directory itself. The last one is needed because // WriteFileContext will call TempFileCollection.Dispose, which will remove a .tmp file it created. string dir = Path.GetDirectoryName(streamName); string[] filePaths = new string[] { streamName, writeFileContext.TempNewFilename, dir }; FileIOPermission fileIOPerm = new FileIOPermission(FileIOPermissionAccess.AllAccess, AccessControlActions.View | AccessControlActions.Change, filePaths); fileIOPerm.Assert(); revertAssert = true; } try { writeFileContext.Complete(streamName, success); } finally { if (revertAssert) { CodeAccessPermission.RevertAssert(); } } }
// This method doesn't really open the streamName for write. Instead, using WriteFileContext // it opens a stream on a temporary file created in the same directory as streamName. internal static Stream StaticOpenStreamForWrite(string streamName, string templateStreamName, ref object writeContext) { if (string.IsNullOrEmpty(streamName)) { throw new ConfigurationErrorsException(SR.Config_no_stream_to_write); } // Create directory if it does not exist. // Ignore errors, allow any failure to come when trying to open the file. string dir = Path.GetDirectoryName(streamName); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } Stream stream; WriteFileContext writeFileContext = null; try { writeFileContext = new WriteFileContext(streamName, templateStreamName); if (File.Exists(streamName)) { FileInfo fi = new FileInfo(streamName); FileAttributes attrs = fi.Attributes; if ((attrs & InvalidAttributesForWrite) != 0) { throw new IOException(SR.Format(SR.Config_invalid_attributes_for_write, streamName)); } } try { stream = new FileStream(writeFileContext.TempNewFilename, FileMode.Create, FileAccess.Write, FileShare.Read); } catch (Exception e) { // Wrap all exceptions so that we provide a meaningful filename - otherwise the end user // will just see the temporary file name, which is meaningless. throw new ConfigurationErrorsException(SR.Format(SR.Config_write_failed, streamName), e); } } catch { writeFileContext?.Complete(streamName, false); throw; } writeContext = writeFileContext; return(stream); }
internal static void StaticWriteCompleted(string streamName, bool success, object writeContext, bool assertPermissions) { WriteFileContext context = (WriteFileContext)writeContext; bool flag = false; if (assertPermissions) { string directoryName = Path.GetDirectoryName(streamName); string[] pathList = new string[] { streamName, context.TempNewFilename, directoryName }; new FileIOPermission(FileIOPermissionAccess.AllAccess, AccessControlActions.Change | AccessControlActions.View, pathList).Assert(); flag = true; } try { context.Complete(streamName, success); } finally { if (flag) { CodeAccessPermission.RevertAssert(); } } }
// This method doesn't really open the streamName for write. Instead, using WriteFileContext // it opens a stream on a temporary file created in the same directory as streamName. // // Parameters: // assertPermissions - If true, then we'll assert all required permissions. Used by ClientSettingsConfigurationHost. // to allow low-trust apps to use ClientSettingsStore. static internal Stream StaticOpenStreamForWrite(string streamName, string templateStreamName, ref object writeContext, bool assertPermissions) { bool revertAssert = false; if (string.IsNullOrEmpty(streamName)) { throw new ConfigurationErrorsException(SR.GetString(SR.Config_no_stream_to_write)); } // Create directory if it does not exist. // Ignore errors, allow any failure to come when trying to open the file. string dir = Path.GetDirectoryName(streamName); try { if (!Directory.Exists(dir)) { // if (assertPermissions) { new FileIOPermission(PermissionState.Unrestricted).Assert(); revertAssert = true; } Directory.CreateDirectory(dir); } } catch { } finally { if (revertAssert) { CodeAccessPermission.RevertAssert(); } } Stream stream; WriteFileContext writeFileContext = null; revertAssert = false; if (assertPermissions) { // If we're asked to assert permission, we will assert allAccess on the directory (instead of just the file). // We need to assert for the whole directory because WriteFileContext will call TempFileCollection.AddExtension, // which will generate a temporary file and make a AllAccess Demand on that file. // Since we don't know the name of the temporary file right now, we need to assert for the whole dir. new FileIOPermission(FileIOPermissionAccess.AllAccess, dir).Assert(); revertAssert = true; } try { writeFileContext = new WriteFileContext(streamName, templateStreamName); if (File.Exists(streamName)) { FileInfo fi = new FileInfo(streamName); FileAttributes attrs = fi.Attributes; if ((int)(attrs & InvalidAttributesForWrite) != 0) { throw new IOException(SR.GetString(SR.Config_invalid_attributes_for_write, streamName)); } } try { stream = new FileStream(writeFileContext.TempNewFilename, FileMode.Create, FileAccess.Write, FileShare.Read); } // Wrap all exceptions so that we provide a meaningful filename - otherwise the end user // will just see the temporary file name, which is meaningless. catch (Exception e) { throw new ConfigurationErrorsException(SR.GetString(SR.Config_write_failed, streamName), e); } } catch { if (writeFileContext != null) { writeFileContext.Complete(streamName, false); } throw; } finally { if (revertAssert) { CodeAccessPermission.RevertAssert(); } } writeContext = writeFileContext; return stream; }
// This method doesn't really open the streamName for write. Instead, using WriteFileContext // it opens a stream on a temporary file created in the same directory as streamName. // // Parameters: // assertPermissions - If true, then we'll assert all required permissions. Used by ClientSettingsConfigurationHost. // to allow low-trust apps to use ClientSettingsStore. static internal Stream StaticOpenStreamForWrite(string streamName, string templateStreamName, ref object writeContext, bool assertPermissions) { bool revertAssert = false; if (string.IsNullOrEmpty(streamName)) { throw new ConfigurationErrorsException(SR.GetString(SR.Config_no_stream_to_write)); } // Create directory if it does not exist. // Ignore errors, allow any failure to come when trying to open the file. string dir = Path.GetDirectoryName(streamName); try { if (!Directory.Exists(dir)) { // if (assertPermissions) { new FileIOPermission(PermissionState.Unrestricted).Assert(); revertAssert = true; } Directory.CreateDirectory(dir); } } catch { } finally { if (revertAssert) { CodeAccessPermission.RevertAssert(); } } Stream stream; WriteFileContext writeFileContext = null; revertAssert = false; if (assertPermissions) { // If we're asked to assert permission, we will assert allAccess on the directory (instead of just the file). // We need to assert for the whole directory because WriteFileContext will call TempFileCollection.AddExtension, // which will generate a temporary file and make a AllAccess Demand on that file. // Since we don't know the name of the temporary file right now, we need to assert for the whole dir. new FileIOPermission(FileIOPermissionAccess.AllAccess, dir).Assert(); revertAssert = true; } try { writeFileContext = new WriteFileContext(streamName, templateStreamName); if (File.Exists(streamName)) { FileInfo fi = new FileInfo(streamName); FileAttributes attrs = fi.Attributes; if ((int)(attrs & InvalidAttributesForWrite) != 0) { throw new IOException(SR.GetString(SR.Config_invalid_attributes_for_write, streamName)); } } try { stream = new FileStream(writeFileContext.TempNewFilename, FileMode.Create, FileAccess.Write, FileShare.Read); } // Wrap all exceptions so that we provide a meaningful filename - otherwise the end user // will just see the temporary file name, which is meaningless. catch (Exception e) { throw new ConfigurationErrorsException(SR.GetString(SR.Config_write_failed, streamName), e); } } catch { if (writeFileContext != null) { writeFileContext.Complete(streamName, false); } throw; } finally { if (revertAssert) { CodeAccessPermission.RevertAssert(); } } writeContext = writeFileContext; return(stream); }
internal static Stream StaticOpenStreamForWrite(string streamName, string templateStreamName, ref object writeContext, bool assertPermissions) { Stream stream; bool flag = false; if (string.IsNullOrEmpty(streamName)) { throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_no_stream_to_write")); } string directoryName = Path.GetDirectoryName(streamName); try { if (!Directory.Exists(directoryName)) { if (assertPermissions) { new FileIOPermission(PermissionState.Unrestricted).Assert(); flag = true; } Directory.CreateDirectory(directoryName); } } catch { } finally { if (flag) { CodeAccessPermission.RevertAssert(); } } WriteFileContext context = null; flag = false; if (assertPermissions) { new FileIOPermission(FileIOPermissionAccess.AllAccess, directoryName).Assert(); flag = true; } try { context = new WriteFileContext(streamName, templateStreamName); if (File.Exists(streamName)) { FileInfo info = new FileInfo(streamName); if ((info.Attributes & (FileAttributes.Hidden | FileAttributes.ReadOnly)) != 0) { throw new IOException(System.Configuration.SR.GetString("Config_invalid_attributes_for_write", new object[] { streamName })); } } try { stream = new FileStream(context.TempNewFilename, FileMode.Create, FileAccess.Write, FileShare.Read); } catch (Exception exception) { throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_write_failed", new object[] { streamName }), exception); } } catch { if (context != null) { context.Complete(streamName, false); } throw; } finally { if (flag) { CodeAccessPermission.RevertAssert(); } } writeContext = context; return stream; }
// This method doesn't really open the streamName for write. Instead, using WriteFileContext // it opens a stream on a temporary file created in the same directory as streamName. internal static Stream StaticOpenStreamForWrite(string streamName, string templateStreamName, ref object writeContext) { if (string.IsNullOrEmpty(streamName)) throw new ConfigurationErrorsException(SR.Config_no_stream_to_write); // Create directory if it does not exist. // Ignore errors, allow any failure to come when trying to open the file. string dir = Path.GetDirectoryName(streamName); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); Stream stream; WriteFileContext writeFileContext = null; try { writeFileContext = new WriteFileContext(streamName, templateStreamName); if (File.Exists(streamName)) { FileInfo fi = new FileInfo(streamName); FileAttributes attrs = fi.Attributes; if ((attrs & InvalidAttributesForWrite) != 0) { throw new IOException(string.Format(SR.Config_invalid_attributes_for_write, streamName)); } } try { stream = new FileStream(writeFileContext.TempNewFilename, FileMode.Create, FileAccess.Write, FileShare.Read); } catch (Exception e) { // Wrap all exceptions so that we provide a meaningful filename - otherwise the end user // will just see the temporary file name, which is meaningless. throw new ConfigurationErrorsException(string.Format(SR.Config_write_failed, streamName), e); } } catch { writeFileContext?.Complete(streamName, false); throw; } writeContext = writeFileContext; return stream; }
internal static Stream StaticOpenStreamForWrite(string streamName, string templateStreamName, ref object writeContext, bool assertPermissions) { Stream stream; bool flag = false; if (string.IsNullOrEmpty(streamName)) { throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_no_stream_to_write")); } string directoryName = Path.GetDirectoryName(streamName); try { if (!Directory.Exists(directoryName)) { if (assertPermissions) { new FileIOPermission(PermissionState.Unrestricted).Assert(); flag = true; } Directory.CreateDirectory(directoryName); } } catch { } finally { if (flag) { CodeAccessPermission.RevertAssert(); } } WriteFileContext context = null; flag = false; if (assertPermissions) { new FileIOPermission(FileIOPermissionAccess.AllAccess, directoryName).Assert(); flag = true; } try { context = new WriteFileContext(streamName, templateStreamName); if (File.Exists(streamName)) { FileInfo info = new FileInfo(streamName); if ((info.Attributes & (FileAttributes.Hidden | FileAttributes.ReadOnly)) != 0) { throw new IOException(System.Configuration.SR.GetString("Config_invalid_attributes_for_write", new object[] { streamName })); } } try { stream = new FileStream(context.TempNewFilename, FileMode.Create, FileAccess.Write, FileShare.Read); } catch (Exception exception) { throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_write_failed", new object[] { streamName }), exception); } } catch { if (context != null) { context.Complete(streamName, false); } throw; } finally { if (flag) { CodeAccessPermission.RevertAssert(); } } writeContext = context; return(stream); }