UnixStream.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using System.Runtime.Versioning;
  5. namespace Ryujinx.Common.SystemInterop
  6. {
  7. [SupportedOSPlatform("linux")]
  8. [SupportedOSPlatform("macos")]
  9. public partial class UnixStream : Stream, IDisposable
  10. {
  11. private const int InvalidFd = -1;
  12. private int _fd;
  13. [LibraryImport("libc", SetLastError = true)]
  14. private static partial long read(int fd, IntPtr buf, ulong count);
  15. [LibraryImport("libc", SetLastError = true)]
  16. private static partial long write(int fd, IntPtr buf, ulong count);
  17. [LibraryImport("libc", SetLastError = true)]
  18. private static partial int close(int fd);
  19. public UnixStream(int fd)
  20. {
  21. if (InvalidFd == fd)
  22. {
  23. throw new ArgumentException("Invalid file descriptor");
  24. }
  25. _fd = fd;
  26. CanRead = read(fd, IntPtr.Zero, 0) != -1;
  27. CanWrite = write(fd, IntPtr.Zero, 0) != -1;
  28. }
  29. ~UnixStream()
  30. {
  31. Close();
  32. }
  33. public override bool CanRead { get; }
  34. public override bool CanWrite { get; }
  35. public override bool CanSeek => false;
  36. public override long Length => throw new NotSupportedException();
  37. public override long Position
  38. {
  39. get => throw new NotSupportedException();
  40. set => throw new NotSupportedException();
  41. }
  42. public override void Flush()
  43. {
  44. }
  45. public override unsafe int Read([In, Out] byte[] buffer, int offset, int count)
  46. {
  47. if (offset < 0 || offset > (buffer.Length - count) || count < 0)
  48. {
  49. throw new ArgumentOutOfRangeException();
  50. }
  51. if (buffer.Length == 0)
  52. {
  53. return 0;
  54. }
  55. long r = 0;
  56. fixed (byte* buf = &buffer[offset])
  57. {
  58. do
  59. {
  60. r = read(_fd, (IntPtr)buf, (ulong)count);
  61. } while (ShouldRetry(r));
  62. }
  63. return (int)r;
  64. }
  65. public override unsafe void Write(byte[] buffer, int offset, int count)
  66. {
  67. if (offset < 0 || offset > (buffer.Length - count) || count < 0)
  68. {
  69. throw new ArgumentOutOfRangeException();
  70. }
  71. if (buffer.Length == 0)
  72. {
  73. return;
  74. }
  75. fixed (byte* buf = &buffer[offset])
  76. {
  77. long r = 0;
  78. do {
  79. r = write(_fd, (IntPtr)buf, (ulong)count);
  80. } while (ShouldRetry(r));
  81. }
  82. }
  83. public override long Seek(long offset, SeekOrigin origin)
  84. {
  85. throw new NotSupportedException();
  86. }
  87. public override void SetLength(long value)
  88. {
  89. throw new NotSupportedException();
  90. }
  91. public override void Close()
  92. {
  93. if (_fd == InvalidFd)
  94. {
  95. return;
  96. }
  97. Flush();
  98. int r;
  99. do {
  100. r = close(_fd);
  101. } while (ShouldRetry(r));
  102. _fd = InvalidFd;
  103. }
  104. void IDisposable.Dispose()
  105. {
  106. Close();
  107. }
  108. private bool ShouldRetry(long r)
  109. {
  110. if (r == -1)
  111. {
  112. const int eintr = 4;
  113. int errno = Marshal.GetLastPInvokeError();
  114. if (errno == eintr)
  115. {
  116. return true;
  117. }
  118. throw new SystemException($"Operation failed with error 0x{errno:X}");
  119. }
  120. return false;
  121. }
  122. }
  123. }