Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UnixStream.cs « Mono.Unix « Mono.Posix « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 72628370c2a0b0c676ce6740bd783aee4ed8f789 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//
// Mono.Unix/UnixStream.cs
//
// Authors:
//   Jonathan Pryor (jonpryor@vt.edu)
//
// (C) 2004-2006 Jonathan Pryor
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Mono.Unix;

namespace Mono.Unix {

	public sealed class UnixStream : Stream, IDisposable
	{
		public const int InvalidFileDescriptor = -1;
		public const int StandardInputFileDescriptor = 0;
		public const int StandardOutputFileDescriptor = 1;
		public const int StandardErrorFileDescriptor = 2;

		public UnixStream (int fileDescriptor)
			: this (fileDescriptor, true) {}

		public UnixStream (int fileDescriptor, bool ownsHandle)
		{
			if (InvalidFileDescriptor == fileDescriptor)
				throw new ArgumentException (Locale.GetText ("Invalid file descriptor"), "fileDescriptor");
			
			this.fileDescriptor = fileDescriptor;
			this.owner = ownsHandle;
			
			long offset = Native.Syscall.lseek (fileDescriptor, 0, Native.SeekFlags.SEEK_CUR);
			if (offset != -1)
				canSeek = true;
			long read = Native.Syscall.read (fileDescriptor, IntPtr.Zero, 0);
			if (read != -1)
				canRead = true;
			long write = Native.Syscall.write (fileDescriptor, IntPtr.Zero, 0);
			if (write != -1)
				canWrite = true;  
		}

		private void AssertNotDisposed ()
		{
			if (fileDescriptor == InvalidFileDescriptor)
				throw new ObjectDisposedException ("Invalid File Descriptor");
		}

		public int Handle {
			get {return fileDescriptor;}
		}

		public override bool CanRead {
			get {return canRead;}
		}

		public override bool CanSeek {
			get {return canSeek;}
		}

		public override bool CanWrite {
			get {return canWrite;}
		}

		public override long Length {
			get {
				AssertNotDisposed ();
				if (!CanSeek)
					throw new NotSupportedException ("File descriptor doesn't support seeking");
				RefreshStat ();
				return stat.st_size;
			}
		}

		public override long Position {
			get {
				AssertNotDisposed ();
				if (!CanSeek)
					throw new NotSupportedException ("The stream does not support seeking");
				long pos = Native.Syscall.lseek (fileDescriptor, 0, Native.SeekFlags.SEEK_CUR);
				if (pos == -1)
					UnixMarshal.ThrowExceptionForLastError ();
				return (long) pos;
			}
			set {
				Seek (value, SeekOrigin.Begin);
			}
		}

		[CLSCompliant (false)]
		public Native.FilePermissions Protection {
			get {
				RefreshStat ();
				return stat.st_mode;
			}
			set {
				// we can't change file type with fchmod, so clear out that portion
				value &= ~Native.FilePermissions.S_IFMT;
				int r = Native.Syscall.fchmod (fileDescriptor, value);
				UnixMarshal.ThrowExceptionForLastErrorIf (r);
			}
		}

		public FileTypes FileType {
			get {
				int type = (int) Protection;
				return (FileTypes) (type & (int) UnixFileSystemInfo.AllFileTypes);
			}
			// no set as fchmod(2) won't accept changing the file type.
		}

		public FileAccessPermissions FileAccessPermissions {
			get {
				int perms = (int) Protection;
				return (FileAccessPermissions) (perms & (int) FileAccessPermissions.AllPermissions);
			}
			set {
				int perms = (int) Protection;
				perms &= (int) ~FileAccessPermissions.AllPermissions;
				perms |= (int) value;
				Protection = (Native.FilePermissions) perms;
			}
		}

		public FileSpecialAttributes FileSpecialAttributes {
			get {
				int attrs = (int) Protection;
				return (FileSpecialAttributes) (attrs & (int) UnixFileSystemInfo.AllSpecialAttributes);
			}
			set {
				int perms = (int) Protection;
				perms &= (int) ~UnixFileSystemInfo.AllSpecialAttributes;
				perms |= (int) value;
				Protection = (Native.FilePermissions) perms;
			}
		}

		public UnixUserInfo OwnerUser {
			get {RefreshStat (); return new UnixUserInfo (stat.st_uid);}
		}
                                                                                                
		public long OwnerUserId {
			get {RefreshStat (); return stat.st_uid;}
		}
                                                                                                
		public UnixGroupInfo OwnerGroup {
			get {RefreshStat (); return new UnixGroupInfo (stat.st_gid);}
		}
                                                                                                
		public long OwnerGroupId {
			get {RefreshStat (); return stat.st_gid;}
		}

		private void RefreshStat ()
		{
			int r = Native.Syscall.fstat (fileDescriptor, out stat);
			UnixMarshal.ThrowExceptionForLastErrorIf (r);
		}

		public void AdviseFileAccessPattern (FileAccessPattern pattern, long offset, long len)
		{
			FileHandleOperations.AdviseFileAccessPattern (fileDescriptor, pattern, offset, len);
		}

		public void AdviseFileAccessPattern (FileAccessPattern pattern)
		{
			AdviseFileAccessPattern (pattern, 0, 0);
		}

		public override void Flush ()
		{
			int r = Native.Syscall.fsync (fileDescriptor);

			if (r == -1) {
				Native.Errno e = Native.Stdlib.GetLastError ();

				// From the man page:
				//  EROFS, EINVAL:
				//    fd is bound to a special file which does not support
				//    synchronization.
				// Sockets are such a file, and since Close() calls Flush(), and we
				// want to support manually opened sockets, we shouldn't generate an
				// exception for these errors.
				if (e == Native.Errno.EROFS || e == Native.Errno.EINVAL) {
					return;
				}

				UnixMarshal.ThrowExceptionForError (e);
			}
		}

		public override unsafe int Read ([In, Out] byte[] buffer, int offset, int count)
		{
			AssertNotDisposed ();
			AssertValidBuffer (buffer, offset, count);
			if (!CanRead)
				throw new NotSupportedException ("Stream does not support reading");
				 
			long r = 0;
			fixed (byte* buf = &buffer[offset]) {
				do {
					r = Native.Syscall.read (fileDescriptor, buf, (ulong) count);
				} while (UnixMarshal.ShouldRetrySyscall ((int) r));
			}
			if (r == -1)
				UnixMarshal.ThrowExceptionForLastError ();
			return (int) r;
		}

		private void AssertValidBuffer (byte[] buffer, int offset, int count)
		{
			if (buffer == null)
				throw new ArgumentNullException ("buffer");
			if (offset < 0)
				throw new ArgumentOutOfRangeException ("offset", "< 0");
			if (count < 0)
				throw new ArgumentOutOfRangeException ("count", "< 0");
			if (offset > buffer.Length)
				throw new ArgumentException ("destination offset is beyond array size");
			if (offset > (buffer.Length - count))
				throw new ArgumentException ("would overrun buffer");
		}

		public unsafe int ReadAtOffset ([In, Out] byte[] buffer, 
			int offset, int count, long fileOffset)
		{
			AssertNotDisposed ();
			AssertValidBuffer (buffer, offset, count);
			if (!CanRead)
				throw new NotSupportedException ("Stream does not support reading");
				 
			long r = 0;
			fixed (byte* buf = &buffer[offset]) {
				do {
					r = Native.Syscall.pread (fileDescriptor, buf, (ulong) count, fileOffset);
				} while (UnixMarshal.ShouldRetrySyscall ((int) r));
			}
			if (r == -1)
				UnixMarshal.ThrowExceptionForLastError ();
			return (int) r;
		}

		public override long Seek (long offset, SeekOrigin origin)
		{
			AssertNotDisposed ();
			if (!CanSeek)
				throw new NotSupportedException ("The File Descriptor does not support seeking");
			if (offset > int.MaxValue)
				throw new ArgumentOutOfRangeException ("offset", "too large");
					
			Native.SeekFlags sf = Native.SeekFlags.SEEK_CUR;
			switch (origin) {
				case SeekOrigin.Begin:   sf = Native.SeekFlags.SEEK_SET; break;
				case SeekOrigin.Current: sf = Native.SeekFlags.SEEK_CUR; break;
				case SeekOrigin.End:     sf = Native.SeekFlags.SEEK_END; break;
			}

			long pos = Native.Syscall.lseek (fileDescriptor, offset, sf);
			if (pos == -1)
				UnixMarshal.ThrowExceptionForLastError ();
			return (long) pos;
		}

		public override void SetLength (long value)
		{
			AssertNotDisposed ();
			if (value < 0)
				throw new ArgumentOutOfRangeException ("value", "< 0");
			if (!CanSeek && !CanWrite)
				throw new NotSupportedException ("You can't truncating the current file descriptor");
			
			int r;
			do {
				r = Native.Syscall.ftruncate (fileDescriptor, value);
			} while (UnixMarshal.ShouldRetrySyscall (r));
			UnixMarshal.ThrowExceptionForLastErrorIf (r);
		}

		public override unsafe void Write (byte[] buffer, int offset, int count)
		{
			AssertNotDisposed ();
			AssertValidBuffer (buffer, offset, count);
			if (!CanWrite)
				throw new NotSupportedException ("File Descriptor does not support writing");

			long r = 0;
			fixed (byte* buf = &buffer[offset]) {
				do {
					r = Native.Syscall.write (fileDescriptor, buf, (ulong) count);
				} while (UnixMarshal.ShouldRetrySyscall ((int) r));
			}
			if (r == -1)
				UnixMarshal.ThrowExceptionForLastError ();
		}
		
		public unsafe void WriteAtOffset (byte[] buffer, 
			int offset, int count, long fileOffset)
		{
			AssertNotDisposed ();
			AssertValidBuffer (buffer, offset, count);
			if (!CanWrite)
				throw new NotSupportedException ("File Descriptor does not support writing");

			long r = 0;
			fixed (byte* buf = &buffer[offset]) {
				do {
					r = Native.Syscall.pwrite (fileDescriptor, buf, (ulong) count, fileOffset);
				} while (UnixMarshal.ShouldRetrySyscall ((int) r));
			}
			if (r == -1)
				UnixMarshal.ThrowExceptionForLastError ();
		}

		public void SendTo (UnixStream output)
		{
			SendTo (output, (ulong) output.Length);
		}

		[CLSCompliant (false)]
		public void SendTo (UnixStream output, ulong count)
		{
			SendTo (output.Handle, count);
		}

		[CLSCompliant (false)]
		public void SendTo (int out_fd, ulong count)
		{
			if (!CanWrite)
				throw new NotSupportedException ("Unable to write to the current file descriptor");
			long offset = Position;
			long r = Native.Syscall.sendfile (out_fd, fileDescriptor, ref offset, count);
			if (r == -1)
				UnixMarshal.ThrowExceptionForLastError ();
		}
		
		public void SetOwner (long user, long group)
		{
			AssertNotDisposed ();

			int r = Native.Syscall.fchown (fileDescriptor, 
					Convert.ToUInt32 (user), Convert.ToUInt32 (group));
			UnixMarshal.ThrowExceptionForLastErrorIf (r);
		}

		public void SetOwner (string user, string group)
		{
			AssertNotDisposed ();

			long uid = new UnixUserInfo (user).UserId;
			long gid = new UnixGroupInfo (group).GroupId;
			SetOwner (uid, gid);
		}

		public void SetOwner (string user)
		{
			AssertNotDisposed ();

			Native.Passwd pw = Native.Syscall.getpwnam (user);
			if (pw == null)
				throw new ArgumentException (Locale.GetText ("invalid username"), "user");
			long uid = pw.pw_uid;
			long gid = pw.pw_gid;
			SetOwner (uid, gid);
		}

		[CLSCompliant (false)]
		public long GetConfigurationValue (Native.PathconfName name)
		{
			AssertNotDisposed ();
			long r = Native.Syscall.fpathconf (fileDescriptor, name);
			if (r == -1 && Native.Syscall.GetLastError() != (Native.Errno) 0)
				UnixMarshal.ThrowExceptionForLastError ();
			return r;
		}

		~UnixStream ()
		{
			Close ();
		}

		public override void Close ()
		{
			if (fileDescriptor == InvalidFileDescriptor)
				return;
				
			Flush ();
			int r;
			do {
				r = Native.Syscall.close (fileDescriptor);
			} while (UnixMarshal.ShouldRetrySyscall (r));
			UnixMarshal.ThrowExceptionForLastErrorIf (r);
			fileDescriptor = InvalidFileDescriptor;
			GC.SuppressFinalize (this);
		}
		
		void IDisposable.Dispose ()
		{
			AssertNotDisposed ();
			if (owner) {
				Close ();
			}
			GC.SuppressFinalize (this);
		}

		private bool canSeek = false;
		private bool canRead = false;
		private bool canWrite = false;
		private bool owner = true;
		private int fileDescriptor = InvalidFileDescriptor;
		private Native.Stat stat;
	}
}

// vim: noexpandtab