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

Fallback to read/write if pread/pwrite fails #59846

Merged
merged 13 commits into from
Oct 11, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -162,41 +162,37 @@ private static async Task WriteByteAsync(FileStream fs, byte value)
await fs.FlushAsync();
}

private static List<string> _devicePathsAvailable;
private static List<string> GetDevicePaths()
private static Lazy<string[]> _availableDevicePaths = new Lazy<string[]>(() =>
jozkee marked this conversation as resolved.
Show resolved Hide resolved
{
if (_devicePathsAvailable is null)
List<string> paths = new();
FileStreamOptions options = new { Access = FileAccess.Write, Share = FileShare.Write };

foreach (string devicePath in new[] { "/dev/tty", "/dev/console", "/dev/null", "/dev/zero" })
{
var options = new FileStreamOptions { Access = FileAccess.Write, Share = FileShare.Write };
_devicePathsAvailable = new List<string>();
if (!File.Exists(devicePath))
{
continue;
}

foreach (string devicePath in new[] { "/dev/tty", "/dev/console", "/dev/null", "/dev/zero" })
try
{
if (!File.Exists(devicePath))
File.Open(devicePath, options).Dispose();
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
continue;
}

try
{
File.Open(devicePath, options).Dispose();
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
continue;
}

throw;
}

_devicePathsAvailable.Add(devicePath);
throw;
}

paths.Add(devicePath);
}

return _devicePathsAvailable;
}
return paths.ToArray();
});

public static IEnumerable<object[]> DevicePath_FileOptions_TestData()
{
Expand Down