aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Common/SystemInterop/UnixStream.cs
blob: 1d64499743dbe62528572279e5a34ea35be15647 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

namespace Ryujinx.Common.SystemInterop
{
    [SupportedOSPlatform("linux")]
    [SupportedOSPlatform("macos")]
    public partial class UnixStream : Stream, IDisposable
    {
        private const int InvalidFd = -1;

        private int _fd;

        [LibraryImport("libc", SetLastError = true)]
        private static partial long read(int fd, IntPtr buf, ulong count);

        [LibraryImport("libc", SetLastError = true)]
        private static partial long write(int fd, IntPtr buf, ulong count);

        [LibraryImport("libc", SetLastError = true)]
        private static partial int close(int fd);

        public UnixStream(int fd)
        {
            if (InvalidFd == fd)
            {
                throw new ArgumentException("Invalid file descriptor");
            }

            _fd = fd;
            
            CanRead = read(fd, IntPtr.Zero, 0) != -1;
            CanWrite = write(fd, IntPtr.Zero, 0) != -1;  
        }

        ~UnixStream()
        {
            Close();
        }

        public override bool CanRead { get; }
        public override bool CanWrite { get; }
        public override bool CanSeek => false;

        public override long Length => throw new NotSupportedException();

        public override long Position
        {
            get => throw new NotSupportedException();
            set => throw new NotSupportedException();
        }

        public override void Flush()
        {
        }

        public override unsafe int Read([In, Out] byte[] buffer, int offset, int count)
        {
            if (offset < 0 || offset > (buffer.Length - count) || count < 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            if (buffer.Length == 0)
            {
                return 0;
            }

            long r = 0;
            fixed (byte* buf = &buffer[offset])
            {
                do
                {
                    r = read(_fd, (IntPtr)buf, (ulong)count);
                } while (ShouldRetry(r));
            }

            return (int)r;
        }
        
        public override unsafe void Write(byte[] buffer, int offset, int count)
        {
            if (offset < 0 || offset > (buffer.Length - count) || count < 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            if (buffer.Length == 0)
            {
                return;
            }

            fixed (byte* buf = &buffer[offset])
            {
                long r = 0;
                do {
                    r = write(_fd, (IntPtr)buf, (ulong)count);
                } while (ShouldRetry(r));
            }
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotSupportedException();
        }

        public override void SetLength(long value)
        {
            throw new NotSupportedException();
        }

        public override void Close()
        {
            if (_fd == InvalidFd)
            {
                return;
            }

            Flush();

            int r;
            do {
                r = close(_fd);
            } while (ShouldRetry(r));

            _fd = InvalidFd;
        }

        void IDisposable.Dispose()
        {
            Close();
        }

        private bool ShouldRetry(long r)
        {
            if (r == -1)
            {
                const int eintr = 4;

                int errno = Marshal.GetLastPInvokeError();

                if (errno == eintr)
                {
                    return true;
                }

                throw new SystemException($"Operation failed with error 0x{errno:X}");
            }

            return false;
        }
    }
}