Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use File.OpenWrite when trying to overwrite a file #93744

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ILAssemblyGeneratingMethodDebugInfoProvider(string fileName, DebugInforma
{
_wrappedProvider = wrappedProvider;
_fileName = fileName;
_tw = new StreamWriter(File.OpenWrite(fileName));
_tw = new StreamWriter(new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More compact alternative:

Suggested change
_tw = new StreamWriter(new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None));
_tw = new StreamWriter(File.Create(fileName));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that it matters here but File.Create() uses FileAccess.ReadWrite instead of FileAccess.Write so it is slightly different.
I'll keep this as is to avoid restarting CI but let me know if you feel strongly.

}

public override MethodDebugInformation GetDebugInfo(MethodIL methodIL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public override bool Execute()
ConvertDictionaryToBlob(configProperties, blobBuilder);

Directory.CreateDirectory(Path.GetDirectoryName(OutputFile!)!);
using var stream = File.OpenWrite(OutputFile);
using var stream = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None);
blobBuilder.WriteContentTo(stream);

return !Log.HasLoggedErrors;
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/WasmAppBuilder/WasmAppBuilderBaseTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected virtual void UpdateRuntimeConfigJson()
AddToRuntimeConfig(wasmHostProperties: wasmHostProperties, runtimeArgsArray: runtimeArgsArray, perHostConfigs: perHostConfigs);

string dstPath = Path.Combine(AppDir!, Path.GetFileName(runtimeConfigPath));
using FileStream? fs = File.OpenWrite(dstPath);
using FileStream? fs = new FileStream(dstPath, FileMode.Create, FileAccess.Write, FileShare.None);
using var writer = new Utf8JsonWriter(fs, new JsonWriterOptions { Indented = true });
rootObject.WriteTo(writer);
_fileWrites.Add(dstPath);
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/WasmBuildTasks/GetChromeVersions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private async Task<Stream> GetDownloadFileStreamAsync(string filename, string ur
{
Log.LogMessage(MessageImportance.Low, $"Downloading {url} ...");
using Stream stream = await s_httpClient.GetStreamAsync(url).ConfigureAwait(false);
using FileStream fs = File.OpenWrite(filePath);
using FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
await stream.CopyToAsync(fs).ConfigureAwait(false);
}
catch (HttpRequestException hre)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public DgmlDependencyRecorder (LinkContext context, string? fileName = null)
Directory.CreateDirectory (context.OutputDirectory);
}

var depsFile = File.OpenWrite (fileName);
var depsFile = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
stream = depsFile;

writer = XmlWriter.Create (stream, settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public XmlDependencyRecorder (LinkContext context, string? fileName = null)
Directory.CreateDirectory (context.OutputDirectory);
}

var depsFile = File.OpenWrite (fileName);
var depsFile = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
stream = depsFile;

writer = XmlWriter.Create (stream, settings);
Expand Down
Loading