/
IsolatedStorageFileStream.cs
349 lines (295 loc) · 11.8 KB
/
IsolatedStorageFileStream.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Win32.SafeHandles;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.Versioning;
namespace System.IO.IsolatedStorage
{
public class IsolatedStorageFileStream : FileStream
{
private const string BackSlash = "\\";
private const int DefaultBufferSize = 1024;
private readonly FileStream _fs;
private readonly IsolatedStorageFile _isf;
private readonly string _givenPath;
private readonly string _fullPath;
public IsolatedStorageFileStream(string path, FileMode mode)
: this(path, mode, (mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), FileShare.None, null)
{
}
public IsolatedStorageFileStream(string path, FileMode mode, IsolatedStorageFile? isf)
: this(path, mode, (mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), FileShare.None, isf)
{
}
public IsolatedStorageFileStream(string path, FileMode mode, FileAccess access)
: this(path, mode, access, access == FileAccess.Read ? FileShare.Read : FileShare.None, DefaultBufferSize, null)
{
}
public IsolatedStorageFileStream(string path, FileMode mode, FileAccess access, IsolatedStorageFile? isf)
: this(path, mode, access, access == FileAccess.Read ? FileShare.Read : FileShare.None, DefaultBufferSize, isf)
{
}
public IsolatedStorageFileStream(string path, FileMode mode, FileAccess access, FileShare share)
: this(path, mode, access, share, DefaultBufferSize, null)
{
}
public IsolatedStorageFileStream(string path, FileMode mode, FileAccess access, FileShare share, IsolatedStorageFile? isf)
: this(path, mode, access, share, DefaultBufferSize, isf)
{
}
public IsolatedStorageFileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize)
: this(path, mode, access, share, bufferSize, null)
{
}
public IsolatedStorageFileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, IsolatedStorageFile? isf)
: this(path, mode, access, share, bufferSize, InitializeFileStream(path, mode, access, share, bufferSize, isf))
{
}
// On .NET Framework FileStream has an internal no arg constructor that we utilize to provide the facade. We don't have access
// to internals in .NET Core so we'll do the next best thing and contort ourselves into the SafeFileHandle constructor.
// (A path constructor would try and create the requested file and give us two open handles.)
//
// We only expose our own nested FileStream so the base class having a handle doesn't matter. Passing a new SafeFileHandle
// with ownsHandle: false avoids the parent class closing without our knowledge.
private IsolatedStorageFileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, InitializationData initializationData)
: base(new SafeFileHandle(initializationData.NestedStream.SafeFileHandle.DangerousGetHandle(), ownsHandle: false), access, bufferSize)
{
_isf = initializationData.StorageFile;
_givenPath = path;
_fullPath = initializationData.FullPath;
_fs = initializationData.NestedStream;
}
private struct InitializationData
{
public FileStream NestedStream;
public IsolatedStorageFile StorageFile;
public string FullPath;
}
// If IsolatedStorageFile is null, then we default to using a file that is scoped by user, appdomain, and assembly.
private static InitializationData InitializeFileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, IsolatedStorageFile? isf)
{
ArgumentNullException.ThrowIfNull(path);
if ((path.Length == 0) || path.Equals(BackSlash))
throw new ArgumentException(
SR.IsolatedStorage_Path);
bool createdStore = false;
if (isf == null)
{
isf = IsolatedStorageFile.GetUserStoreForDomain();
createdStore = true;
}
if (isf.Disposed)
throw new ObjectDisposedException(null, SR.IsolatedStorage_StoreNotOpen);
switch (mode)
{
case FileMode.CreateNew: // Assume new file
case FileMode.Create: // Check for New file & Unreserve
case FileMode.OpenOrCreate: // Check for new file
case FileMode.Truncate: // Unreserve old file size
case FileMode.Append: // Check for new file
case FileMode.Open: // Open existing, else exception
break;
default:
throw new ArgumentException(SR.IsolatedStorage_FileOpenMode);
}
InitializationData data = new InitializationData
{
FullPath = isf.GetFullPath(path),
StorageFile = isf
};
try
{
data.NestedStream = new FileStream(data.FullPath, mode, access, share, bufferSize, FileOptions.None);
}
catch (Exception e)
{
// Make an attempt to clean up the StorageFile if we created it
try
{
if (createdStore)
{
data.StorageFile?.Dispose();
}
}
catch
{
}
// Exception message might leak the IsolatedStorage path. The .NET Framework prevented this by calling an
// internal API which made sure that the exception message was scrubbed. However since the innerException
// is never returned to the user(GetIsolatedStorageException() does not populate the innerexception
// in retail bits we leak the path only under the debugger via IsolatedStorageException._underlyingException which
// they can any way look at via IsolatedStorageFile instance as well.
throw IsolatedStorageFile.GetIsolatedStorageException(SR.IsolatedStorage_Operation_ISFS, e);
}
return data;
}
public override bool CanRead
{
get
{
return _fs.CanRead;
}
}
public override bool CanWrite
{
get
{
return _fs.CanWrite;
}
}
public override bool CanSeek
{
get
{
return _fs.CanSeek;
}
}
public override long Length
{
get
{
return _fs.Length;
}
}
public override long Position
{
get
{
return _fs.Position;
}
set
{
_fs.Position = value;
}
}
public override bool IsAsync
{
get
{
return _fs.IsAsync;
}
}
protected override void Dispose(bool disposing)
{
try
{
if (disposing)
{
_fs?.Dispose();
}
}
finally
{
base.Dispose(disposing);
}
}
public override ValueTask DisposeAsync()
{
return
GetType() != typeof(IsolatedStorageFileStream) ? base.DisposeAsync() :
_fs != null ? _fs.DisposeAsync() :
default;
}
public override void Flush()
{
_fs.Flush();
}
public override void Flush(bool flushToDisk)
{
_fs.Flush(flushToDisk);
}
public override Task FlushAsync(CancellationToken cancellationToken)
{
return _fs.FlushAsync(cancellationToken);
}
public override void SetLength(long value)
{
_fs.SetLength(value);
}
public override int Read(byte[] buffer, int offset, int count)
{
return _fs.Read(buffer, offset, count);
}
public override int Read(System.Span<byte> buffer)
{
return _fs.Read(buffer);
}
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Threading.CancellationToken cancellationToken)
{
return _fs.ReadAsync(buffer, offset, count, cancellationToken);
}
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken)
{
return _fs.ReadAsync(buffer, cancellationToken);
}
public override int ReadByte()
{
return _fs.ReadByte();
}
public override long Seek(long offset, SeekOrigin origin)
{
// Desktop implementation of IsolatedStorage ensures that in case the size is increased the new memory is zero'ed out.
// However in this implementation we simply call the FileStream.Seek APIs which have an undefined behavior.
return _fs.Seek(offset, origin);
}
public override void Write(byte[] buffer, int offset, int count)
{
_fs.Write(buffer, offset, count);
}
public override void Write(System.ReadOnlySpan<byte> buffer)
{
_fs.Write(buffer);
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return _fs.WriteAsync(buffer, offset, count, cancellationToken);
}
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
{
return _fs.WriteAsync(buffer, cancellationToken);
}
public override void WriteByte(byte value)
{
_fs.WriteByte(value);
}
public override IAsyncResult BeginRead(byte[] array, int offset, int numBytes, AsyncCallback? userCallback, object? stateObject)
{
return _fs.BeginRead(array, offset, numBytes, userCallback, stateObject);
}
public override IAsyncResult BeginWrite(byte[] array, int offset, int numBytes, AsyncCallback? userCallback, object? stateObject)
{
return _fs.BeginWrite(array, offset, numBytes, userCallback, stateObject);
}
public override int EndRead(IAsyncResult asyncResult)
{
return _fs.EndRead(asyncResult);
}
public override void EndWrite(IAsyncResult asyncResult)
{
_fs.EndWrite(asyncResult);
}
[Obsolete("IsolatedStorageFileStream.Handle has been deprecated. Use IsolatedStorageFileStream's SafeFileHandle property instead.")]
public override IntPtr Handle
{
get { return _fs.Handle; }
}
[UnsupportedOSPlatform("macos")]
public override void Unlock(long position, long length)
{
_fs.Unlock(position, length);
}
[UnsupportedOSPlatform("macos")]
public override void Lock(long position, long length)
{
_fs.Lock(position, length);
}
public override SafeFileHandle SafeFileHandle
{
get
{
throw new IsolatedStorageException(SR.IsolatedStorage_Operation_ISFS);
}
}
}
}