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

allow FileStream to open any types of files from path #54676

Merged
merged 3 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -23,7 +23,7 @@ protected override async Task<StreamPair> CreateConnectedStreamsAsync()
string pipePath = Path.GetFullPath($@"\\.\pipe\{pipeName}");

var server = new NamedPipeServerStream(pipeName, PipeDirection.In);
var clienStream = new FileStream(File.OpenHandle(pipePath, FileMode.Open, FileAccess.Write, FileShare.None), FileAccess.Write);
var clienStream = new FileStream(pipePath, FileMode.Open, FileAccess.Write, FileShare.None);

await server.WaitForConnectionAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,32 +142,6 @@ internal static void Unlock(SafeFileHandle handle, string? path, long position,
}
}

internal static void ValidateFileTypeForNonExtendedPaths(SafeFileHandle handle, string originalPath)
{
if (!PathInternal.IsExtended(originalPath))
{
// To help avoid stumbling into opening COM/LPT ports by accident, we will block on non file handles unless
// we were explicitly passed a path that has \\?\. GetFullPath() will turn paths like C:\foo\con.txt into
// \\.\CON, so we'll only allow the \\?\ syntax.

int fileType = handle.GetFileType();
if (fileType != Interop.Kernel32.FileTypes.FILE_TYPE_DISK)
{
int errorCode = fileType == Interop.Kernel32.FileTypes.FILE_TYPE_UNKNOWN
? Marshal.GetLastPInvokeError()
: Interop.Errors.ERROR_SUCCESS;

handle.Dispose();

if (errorCode != Interop.Errors.ERROR_SUCCESS)
{
throw Win32Marshal.GetExceptionForWin32Error(errorCode);
}
throw new NotSupportedException(SR.NotSupported_FileStreamOnNonFiles);
}
}
}

internal static unsafe void SetFileLength(SafeFileHandle handle, string? path, long length)
{
var eofInfo = new Interop.Kernel32.FILE_END_OF_FILE_INFO
Expand All @@ -188,8 +162,6 @@ internal static unsafe void SetFileLength(SafeFileHandle handle, string? path, l
}
}



internal static async Task AsyncModeCopyToAsync(SafeFileHandle handle, string? path, bool canSeek, long filePosition, Stream destination, int bufferSize, CancellationToken cancellationToken)
{
// For efficiency, we avoid creating a new task and associated state for each asynchronous read.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ internal sealed partial class Net5CompatFileStreamStrategy : FileStreamStrategy

private void Init(FileMode mode, string originalPath, FileOptions options)
{
FileStreamHelpers.ValidateFileTypeForNonExtendedPaths(_fileHandle, originalPath);

Debug.Assert(!_useAsyncIO || _fileHandle.ThreadPoolBinding != null);

// For Append mode...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal WindowsFileStreamStrategy(string path, FileMode mode, FileAccess access

try
{
Init(mode, path);
Init(mode);
}
catch
{
Expand Down Expand Up @@ -230,10 +230,8 @@ public sealed override long Seek(long offset, SeekOrigin origin)

internal sealed override void Unlock(long position, long length) => FileStreamHelpers.Unlock(_fileHandle, _path, position, length);

private void Init(FileMode mode, string originalPath)
private void Init(FileMode mode)
{
FileStreamHelpers.ValidateFileTypeForNonExtendedPaths(_fileHandle, originalPath);

// For Append mode...
if (mode == FileMode.Append)
{
Expand Down