This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
VfsStream.cs
640 lines (569 loc) · 17.6 KB
/
VfsStream.cs
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// VfsStream.cs - System.IO.Stream wrapper around gnome-vfs.
//
// Authors: Jeroen Zwartepoorte <jeroen@xs4all.nl>
//
// Copyright (c) 2004 Jeroen Zwartepoorte
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the Lesser GNU General
// Public License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
using GLib;
using System;
using System.IO;
namespace Gnome.Vfs {
class VfsStreamAsync {
private Handle handle;
private byte[] buffer;
private int count;
private int offset;
private int bytesRemaining;
private System.AsyncCallback cback;
private object state;
private VfsStreamAsyncResult asyncResult;
public VfsStreamAsync (Handle handle, byte[] buffer, int offset,
int count, System.AsyncCallback cback, object state)
{
this.handle = handle;
this.buffer = buffer;
this.offset = offset;
this.count = count;
this.cback = cback;
bytesRemaining = count;
}
public VfsStreamAsyncResult BeginRead ()
{
asyncResult = new VfsStreamAsyncResult (state);
Async.Read (handle, out buffer[offset], (uint)count, new AsyncReadCallback (AsyncRead));
return asyncResult;
}
public VfsStreamAsyncResult BeginWrite ()
{
asyncResult = new VfsStreamAsyncResult (state);
Async.Write (handle, out buffer[offset], (uint)count, new AsyncWriteCallback (AsyncWrite));
return asyncResult;
}
private void AsyncRead (Handle handle, Result result, byte[] buf,
ulong bytesRequested, ulong bytesRead)
{
if (result == Result.Ok) {
Array.Copy (buf, 0, buffer, offset + count - bytesRemaining, (int)bytesRead);
bytesRemaining -= (int)bytesRead;
if (bytesRemaining > 0) {
buf = new byte[bytesRemaining];
Async.Read (handle, out buf[0], (uint)bytesRemaining,
new AsyncReadCallback (AsyncRead));
} else if (cback != null) {
asyncResult.SetComplete (null, count);
cback (asyncResult);
}
} else if (result == Result.ErrorEof) {
Array.Copy (buf, 0, buffer, offset + count - bytesRemaining, (int)bytesRead);
bytesRemaining -= (int)bytesRead;
asyncResult.SetComplete (null, count - bytesRemaining);
if (cback != null)
cback (asyncResult);
} else if (cback != null) {
Exception e = new IOException (Vfs.ResultToString (result));
asyncResult.SetComplete (e, -1);
cback (asyncResult);
}
}
private void AsyncWrite (Handle handle, Result result, byte[] buffer,
ulong bytesRequested, ulong bytesWritten)
{
if (result == Result.Ok) {
bytesRemaining -= (int)bytesWritten;
if (bytesRemaining > 0) {
Async.Write (handle, out buffer[offset + count - bytesRemaining],
(uint)bytesRemaining, new AsyncWriteCallback (AsyncWrite));
} else if (cback != null) {
asyncResult.SetComplete (null, count);
cback (asyncResult);
}
} else if (result == Result.ErrorEof) {
bytesRemaining -= (int)bytesWritten;
asyncResult.SetComplete (null, count - bytesRemaining);
if (cback != null)
cback (asyncResult);
} else if (cback != null) {
Exception e = new IOException (Vfs.ResultToString (result));
asyncResult.SetComplete (e, -1);
cback (asyncResult);
}
}
}
public class VfsStream : System.IO.Stream {
private Gnome.Vfs.Uri uri;
private Handle handle;
private FileMode mode;
private FileAccess access;
private bool async;
private bool canseek;
// Async state variables.
private AsyncCallback callback;
private AsyncReadCallback readCallback;
private AsyncWriteCallback writeCallback;
private bool asyncCompleted = false;
private Result asyncResult;
private ulong asyncBytesRead;
private ulong asyncBytesWritten;
public VfsStream (string uri, FileMode mode)
: this (uri, mode, false) { }
public VfsStream (string text_uri, FileMode mode, bool async)
{
if (text_uri == null)
throw new ArgumentNullException ("uri");
if (text_uri == "")
throw new ArgumentNullException ("Uri is empty");
if (mode < FileMode.CreateNew || mode > FileMode.Append)
throw new ArgumentOutOfRangeException ("mode");
if (text_uri.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Uri has invalid chars");
uri = new Gnome.Vfs.Uri (text_uri);
if (mode == FileMode.Open && !uri.Exists)
throw new FileNotFoundException ("Could not find uri \"" + text_uri + "\".");
if (mode == FileMode.CreateNew) {
string dname = uri.ExtractDirname ();
if (dname != "" && !new Uri (dname).Exists)
throw new DirectoryNotFoundException ("Could not find a part of " +
"the path \"" + dname + "\".");
}
if (async) {
callback = new AsyncCallback (OnAsyncCallback);
readCallback = new AsyncReadCallback (OnAsyncReadCallback);
writeCallback = new AsyncWriteCallback (OnAsyncWriteCallback);
}
OpenMode om = OpenMode.None;
switch (mode) {
case FileMode.CreateNew:
case FileMode.Create:
case FileMode.Truncate:
case FileMode.Append:
om = OpenMode.Write;
access = FileAccess.Write;
break;
case FileMode.OpenOrCreate:
if (uri.Exists) {
om = OpenMode.Read;
access = FileAccess.Read;
} else {
om = OpenMode.Write;
access = FileAccess.Write;
}
break;
case FileMode.Open:
om = OpenMode.Read;
access = FileAccess.Read;
break;
}
/* 644 */
FilePermissions perms = FilePermissions.UserRead |
FilePermissions.UserWrite |
FilePermissions.GroupRead |
FilePermissions.OtherRead;
Result result;
handle = null;
switch (mode) {
case FileMode.Append:
if (uri.Exists) {
if (async) {
handle = Async.Open (uri, om,
(int)Async.Priority.Default,
callback);
Wait ();
Async.Seek (handle, SeekPosition.End, 0, callback);
Wait ();
} else {
handle = Sync.Open (uri, om);
result = Sync.Seek (handle, SeekPosition.End, 0);
Vfs.ThrowException (uri, result);
}
} else {
if (async) {
handle = Async.Create (uri, om, true, perms,
(int)Async.Priority.Default,
callback);
Wait ();
} else {
handle = Sync.Create (uri, om, true, perms);
}
}
break;
case FileMode.Create:
if (uri.Exists) {
if (async) {
handle = Async.Open (uri, om,
(int)Async.Priority.Default,
callback);
Wait ();
} else {
handle = Sync.Open (uri, om);
result = uri.Truncate (0);
Vfs.ThrowException (uri, result);
}
} else {
handle = Sync.Create (uri, om, true, perms);
}
break;
case FileMode.CreateNew:
if (uri.Exists) {
throw new IOException ("Uri \"" + text_uri + "\" already exists.");
} else {
if (async) {
handle = Async.Create (uri, om, true, perms,
(int)Async.Priority.Default,
callback);
Wait ();
} else {
handle = Sync.Create (uri, om, true, perms);
}
}
break;
case FileMode.Open:
if (uri.Exists) {
if (async) {
handle = Async.Open (uri, om,
(int)Async.Priority.Default,
callback);
Wait ();
} else {
handle = Sync.Open (uri, om);
}
} else {
throw new FileNotFoundException (text_uri);
}
break;
case FileMode.OpenOrCreate:
if (uri.Exists) {
if (async) {
handle = Async.Open (uri, om,
(int)Async.Priority.Default,
callback);
Wait ();
} else {
handle = Sync.Open (uri, om);
}
} else {
if (async) {
handle = Async.Create (uri, om, true, perms,
(int)Async.Priority.Default,
callback);
Wait ();
} else {
handle = Sync.Create (uri, om, true, perms);
}
}
break;
case FileMode.Truncate:
if (uri.Exists) {
result = uri.Truncate (0);
if (async) {
handle = Async.Open (uri, om,
(int)Async.Priority.Default,
callback);
Wait ();
} else {
handle = Sync.Open (uri, om);
Vfs.ThrowException (uri, result);
}
} else {
throw new FileNotFoundException (text_uri);
}
break;
}
this.mode = mode;
this.canseek = true;
this.async = async;
}
public override bool CanRead {
get {
return access == FileAccess.Read ||
access == FileAccess.ReadWrite;
}
}
public override bool CanWrite {
get {
return access == FileAccess.Write ||
access == FileAccess.ReadWrite;
}
}
public override bool CanSeek {
get {
return canseek;
}
}
public virtual bool IsAsync {
get {
return async;
}
}
public string Uri {
get {
return uri.ToString ();
}
}
public override long Length {
get {
FileInfo info = uri.GetFileInfo ();
return info.Size;
}
}
public override long Position {
get {
if (IsAsync)
throw new NotSupportedException ("Cannot tell what the offset is in async mode");
ulong pos;
Result result = Sync.Tell (handle, out pos);
Vfs.ThrowException (Uri, result);
return (long)pos;
}
set {
Seek (value, SeekOrigin.Begin);
}
}
public override int ReadByte ()
{
if (!CanRead)
throw new NotSupportedException ("The stream does not support reading");
byte[] buffer = new byte[1];
ulong bytesRead;
Result result;
if (async) {
Async.Read (handle, out buffer[0], 1, readCallback);
Wait ();
result = asyncResult;
} else {
result = Sync.Read (handle, out buffer[0], 1UL, out bytesRead);
}
if (result == Result.ErrorEof)
return -1;
Vfs.ThrowException (Uri, result);
return buffer[0];
}
public override void WriteByte (byte value)
{
if (!CanWrite)
throw new NotSupportedException ("The stream does not support writing");
byte[] buffer = new byte[1];
buffer[0] = value;
ulong bytesWritten;
Result result;
if (async) {
Async.Write (handle, out buffer[0], 1, writeCallback);
Wait ();
result = asyncResult;
} else {
result = Sync.Write (handle, out buffer[0], 1UL, out bytesWritten);
}
Vfs.ThrowException (Uri, result);
}
public override int Read (byte[] buffer, int offset, int count)
{
if (buffer == null)
throw new ArgumentNullException ("buffer");
else if (offset < 0)
throw new ArgumentOutOfRangeException ("offset", "Must be >= 0");
else if (count < 0)
throw new ArgumentOutOfRangeException ("count", "Must be >= 0");
else if (count > buffer.Length - offset)
throw new ArgumentException ("Buffer too small, count/offset wrong");
else if (!CanRead)
throw new NotSupportedException ("The stream does not support reading");
ulong bytesRead;
Result result;
if (async) {
Async.Read (handle, out buffer[offset], (uint)count, readCallback);
Wait ();
result = asyncResult;
bytesRead = asyncBytesRead;
} else {
result = Sync.Read (handle, out buffer[offset], (ulong)count, out bytesRead);
}
if (result == Result.ErrorEof)
return 0;
Vfs.ThrowException (Uri, result);
return (int)bytesRead;
}
public override IAsyncResult BeginRead (byte[] buffer, int offset, int count,
System.AsyncCallback cback, object state)
{
if (buffer == null)
throw new ArgumentNullException ("buffer");
else if (offset < 0)
throw new ArgumentOutOfRangeException ("offset", "Must be >= 0");
else if (count < 0)
throw new ArgumentOutOfRangeException ("count", "Must be >= 0");
else if (count > buffer.Length - offset)
throw new ArgumentException ("Buffer too small, count/offset wrong");
else if (!CanRead)
throw new NotSupportedException ("The stream does not support reading");
if (!IsAsync)
return base.BeginRead (buffer, offset, count, cback, state);
VfsStreamAsync async = new VfsStreamAsync (handle, buffer, offset, count, cback, state);
return async.BeginRead ();
}
public override int EndRead (IAsyncResult result)
{
if (result == null)
throw new ArgumentNullException ("result");
if (!IsAsync)
base.EndRead (result);
if (!(result is VfsStreamAsyncResult))
throw new ArgumentException ("Invalid IAsyncResult object", "result");
VfsStreamAsyncResult asyncResult = (VfsStreamAsyncResult)result;
if (asyncResult.Done)
throw new InvalidOperationException ("EndRead already called");
asyncResult.Done = true;
while (!asyncResult.IsCompleted)
MainContext.Iteration ();
if (asyncResult.Exception != null)
throw asyncResult.Exception;
return asyncResult.NBytes;
}
public override void Write (byte[] buffer, int offset, int count)
{
if (buffer == null)
throw new ArgumentNullException ("buffer");
else if (offset < 0)
throw new ArgumentOutOfRangeException ("offset", "Must be >= 0");
else if (count < 0)
throw new ArgumentOutOfRangeException ("count", "Must be >= 0");
else if (count > buffer.Length - offset)
throw new ArgumentException ("Buffer too small, count/offset wrong");
else if (!CanWrite)
throw new NotSupportedException ("The stream does not support writing");
ulong bytesWritten;
Result result;
if (async) {
Async.Write (handle, out buffer[offset], (uint)count, writeCallback);
Wait ();
result = asyncResult;
bytesWritten = asyncBytesWritten;
} else {
result = Sync.Write (handle, out buffer[offset], (ulong)count, out bytesWritten);
}
Vfs.ThrowException (Uri, result);
}
public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
System.AsyncCallback cback, object state)
{
if (buffer == null)
throw new ArgumentNullException ("buffer");
else if (offset < 0)
throw new ArgumentOutOfRangeException ("offset", "Must be >= 0");
else if (count < 0)
throw new ArgumentOutOfRangeException ("count", "Must be >= 0");
else if (count > buffer.Length - offset)
throw new ArgumentException ("Buffer too small, count/offset wrong");
else if (!CanWrite)
throw new NotSupportedException ("The stream does not support writing");
if (!IsAsync)
return base.BeginRead (buffer, offset, count, cback, state);
VfsStreamAsync async = new VfsStreamAsync (handle, buffer, offset, count, cback, state);
return async.BeginWrite ();
}
public override void EndWrite (IAsyncResult result)
{
if (result == null)
throw new ArgumentNullException ("result");
if (!IsAsync)
base.EndWrite (result);
if (!(result is VfsStreamAsyncResult))
throw new ArgumentException ("Invalid IAsyncResult object", "result");
VfsStreamAsyncResult asyncResult = (VfsStreamAsyncResult)result;
if (asyncResult.Done)
throw new InvalidOperationException ("EndWrite already called");
asyncResult.Done = true;
while (!asyncResult.IsCompleted)
MainContext.Iteration ();
if (asyncResult.Exception != null)
throw asyncResult.Exception;
}
public override long Seek (long offset, SeekOrigin origin)
{
if (!CanSeek)
throw new NotSupportedException ("The stream does not support seeking");
if (IsAsync && origin == SeekOrigin.Current)
throw new NotSupportedException ("Cannot tell what the offset is in async mode");
SeekPosition seekPos = SeekPosition.Start;
long newOffset = -1;
switch (origin) {
case SeekOrigin.Begin:
seekPos = SeekPosition.Start;
newOffset = offset;
break;
case SeekOrigin.Current:
seekPos = SeekPosition.Current;
break;
case SeekOrigin.End:
seekPos = SeekPosition.End;
newOffset = Length + offset;
break;
}
Result result;
if (async) {
Async.Seek (handle, seekPos, offset, callback);
Wait ();
result = asyncResult;
} else {
result = Sync.Seek (handle, seekPos, offset);
}
Vfs.ThrowException (Uri, result);
return newOffset;
}
public override void SetLength (long length)
{
if (!CanSeek)
throw new NotSupportedException ("The stream does not support seeking");
else if (!CanWrite)
throw new NotSupportedException ("The stream does not support writing");
Result result = Sync.Truncate (handle, (ulong)length);
Vfs.ThrowException (Uri, result);
}
public override void Flush ()
{
}
public override void Close ()
{
Result result = Sync.Close (handle);
Vfs.ThrowException (Uri, result);
}
private void OnAsyncCallback (Handle handle, Result result)
{
asyncResult = result;
asyncCompleted = true;
}
private void OnAsyncReadCallback (Handle handle, Result result,
byte[] buffer, ulong bytes_requested,
ulong bytes_read)
{
asyncResult = result;
asyncBytesRead = bytes_read;
asyncCompleted = true;
}
private void OnAsyncWriteCallback (Handle handle, Result result,
byte[] buffer, ulong bytes_requested,
ulong bytes_written)
{
asyncResult = result;
asyncBytesWritten = bytes_written;
asyncCompleted = true;
}
private void Wait ()
{
while (!asyncCompleted)
MainContext.Iteration ();
asyncCompleted = false;
}
}
}