Read about our upcoming Code of Conduct on this issue

This instance was upgraded to Heptapod 0.28.0 today

FileSystem.cs 15 KB
Newer Older
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
/* Copyright 2010-2018 Jesse McGrew
 * 
 * This file is part of ZILF.
 * 
 * ZILF is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ZILF 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
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ZILF.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Zilf.Common
{
    /// <summary>
    /// Exposes a logical file system to the compiler and interpreter.
    /// </summary>
    public interface IFileSystem
    {
        /// <summary>
        /// Tests whether a file currently exists.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <returns><see langword="true"/> if the file currently exists; otherwise, <see langword="false"/>.</returns>
        /// <remarks>
        /// This method does not test whether the file can be read or written.
        /// The file may also be created or deleted by another process after this method is called.
        /// </remarks>
        bool Exists(string path);

        /// <summary>
        /// Opens a file in read-only mode.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <returns>A stream.</returns>
        /// <exception cref="FileNotFoundException">The file does not exist.</exception>
        /// <exception cref="IOException">The file could not be opened.</exception>
        Stream OpenForReading(string path);

        /// <summary>
        /// Opens a file in write-only mode.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <returns>A stream.</returns>
        /// <exception cref="NotSupportedException">The file system does not support writing.</exception>
        /// <exception cref="IOException">The file could not be opened.</exception>
        /// <remarks>If the file already exists, it will be truncated.</remarks>
        Stream OpenForWriting(string path);

        /// <summary>
        /// Opens a file in write mode with the ability to seek and re-read data that has already been written.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <returns>A stream.</returns>
        /// <exception cref="NotSupportedException">The file system does not support writing.</exception>
        /// <exception cref="IOException">The file could not be opened.</exception>
        /// <remarks>If the file already exists, it will be truncated.</remarks>
        Stream OpenForWritingAndReading(string path);

        /// <summary>
        /// Gets the contents of a readable file as a byte array.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <returns>A byte array containing the file contents.</returns>
        /// <exception cref="FileNotFoundException">The file does not exist.</exception>
        /// <exception cref="IOException">The file could not be read.</exception>
        byte[] GetBytes(string path)
        {
            using var input = OpenForReading(path);
            using var output = new MemoryStream();
            input.CopyTo(output);
            return output.ToArray();
        }

        /// <summary>
        /// Asynchronously gets the contents of a readable file as a byte array, using a specified cancellation token.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
        /// <returns>A task that represents the asynchronous read operation.</returns>
        /// <exception cref="FileNotFoundException">The file does not exist.</exception>
        /// <exception cref="IOException">The file could not be read.</exception>
        async Task<byte[]> GetBytesAsync(string path, CancellationToken cancellationToken = default)
        {
            var input = OpenForReading(path);
            await using var _ = input.ConfigureAwait(false);

            using var output = new MemoryStream();
            await input.CopyToAsync(output, cancellationToken).ConfigureAwait(false);

            return output.ToArray();
        }

        /// <summary>
        /// Gets the contents of a readable file as a string, using UTF-8 encoding.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <returns>A string containing the file contents.</returns>
        /// <exception cref="FileNotFoundException">The file does not exist.</exception>
        /// <exception cref="IOException">The file could not be read.</exception>
        string GetText(string path)
        {
            using var input = OpenForReading(path);
            using var rdr = new StreamReader(input, Encoding.UTF8);
            return rdr.ReadToEnd();
        }

        /// <summary>
        /// Asynchronously gets the contents of a readable file as a string, using UTF-8 encoding.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <returns>A task that represents the asynchronous read operation.</returns>
        /// <exception cref="FileNotFoundException">The file does not exist.</exception>
        /// <exception cref="IOException">The file could not be read.</exception>
        async Task<string> GetTextAsync(string path)
        {
            var input = OpenForReading(path);
            await using var _ = input.ConfigureAwait(false);

            using var rdr = new StreamReader(input, Encoding.UTF8);
            return await rdr.ReadToEndAsync().ConfigureAwait(false);
        }

        /// <summary>
        /// Sets the contents of a writable file from a string, using UTF-8 encoding.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <param name="content">A string containing the desired contents of the file.</param>
        /// <exception cref="NotSupportedException">The file system does not support writing.</exception>
        /// <exception cref="IOException">The file could not be written.</exception>
        void SetText(string path, string content)
        {
            using var output = OpenForWriting(path);
            using var wtr = new StreamWriter(output, Encoding.UTF8);
            wtr.Write(content);
        }

        /// <summary>
        /// Asynchronously sets the contents of a writable file from a string, using UTF-8 encoding.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <param name="content">A string containing the desired contents of the file.</param>
        /// <returns>A task representing the asynchronous write operation.</returns>
        /// <exception cref="NotSupportedException">The file system does not support writing.</exception>
        /// <exception cref="IOException">The file could not be written.</exception>
        async Task SetTextAsync(string path, string content)
        {
            var output = OpenForWriting(path);
            await using var _ = output.ConfigureAwait(false);

            var wtr = new StreamWriter(output, Encoding.UTF8);
            await using var __ = wtr.ConfigureAwait(false);

            await wtr.WriteAsync(content).ConfigureAwait(false);
        }
    }

    /// <summary>
    /// A minimal implementation of <see cref="IFileSystem"/> representing an empty, read-only file system.
    /// </summary>
    public sealed class NullFileSystem : IFileSystem
    {
        public static NullFileSystem Instance { get; } = new NullFileSystem();

        private NullFileSystem() { }

        public bool Exists(string path) => false;

        public Stream OpenForReading(string path) => throw new FileNotFoundException(nameof(NullFileSystem), path);

        public Stream OpenForWriting(string path) => throw new NotSupportedException($"{nameof(NullFileSystem)} is read-only");

        public Stream OpenForWritingAndReading(string path) => throw new NotSupportedException($"{nameof(NullFileSystem)} is read-only");
    }

    /// <summary>
    /// An implementation of <see cref="IFileSystem"/> providing read-write access to the local file system.
    /// </summary>
    public sealed class PhysicalFileSystem : IFileSystem
    {
        public static PhysicalFileSystem Instance { get; } = new PhysicalFileSystem();

        private PhysicalFileSystem() { }

        public bool Exists(string path) => File.Exists(path);

        public Stream OpenForReading(string path) => new FileStream(path, FileMode.Open, FileAccess.Read);

        public Stream OpenForWriting(string path) => new FileStream(path, FileMode.Create, FileAccess.Write);

        public Stream OpenForWritingAndReading(string path) => new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
    }

    /// <summary>
    /// An implementation of <see cref="IFileSystem"/> providing read-write access to temporary storage in memory.
    /// </summary>
    public sealed class InMemoryFileSystem : IFileSystem
    {
        private readonly Dictionary<string, MemoryStream> writtenFiles = new();
        private readonly Dictionary<string, byte[]> readableFiles = new();

        public IEnumerable<string> Paths => readableFiles.Keys.Concat(writtenFiles.Keys);

        public static InMemoryFileSystem Of(string path, string content)
        {
            var result = new InMemoryFileSystem();
            result.SetText(path, content);
            return result;
        }

        private bool TryGetReadableFile(string path, [NotNullWhen(true)] out byte[]? bytes)
        {
            if (readableFiles.TryGetValue(path, out bytes))
                return true;

            if (!writtenFiles.TryGetValue(path, out var stream))
                return false;

            if (stream.CanWrite)
            {
                // can't read it while it's still open for writing
                return false;
            }

            bytes = stream.ToArray();
            readableFiles.Add(path, bytes);
            writtenFiles.Remove(path);
            return true;
        }

        public void Clear()
        {
            readableFiles.Clear();
            writtenFiles.Clear();
        }

        public byte[] GetBytes(string path) =>
            TryGetReadableFile(path, out var bytes) ? bytes : throw new FileNotFoundException("File not readable", path);

        public string GetText(string path) => Encoding.UTF8.GetString(GetBytes(path));

        public void SetText(string path, string content)
        {
            readableFiles[path] = Encoding.UTF8.GetBytes(content);
            writtenFiles.Remove(path);
        }

        public bool Exists(string path) => readableFiles.ContainsKey(path) || writtenFiles.ContainsKey(path);

        public Stream OpenForReading(string path) => new MemoryStream(GetBytes(path), false);

        public Stream OpenForWriting(string path)
        {
            var result = new MemoryStream();
            writtenFiles[path] = result;
            readableFiles.Remove(path);
            return result;
        }

        public Stream OpenForWritingAndReading(string path) => OpenForWriting(path);
    }

    /// <summary>
    /// An <see cref="IFileSystem"/> combinator that provides read-write access to a first file system, with
    /// reads of missing files falling back to a second file system.
    /// </summary>
    public sealed class OverlayFileSystem : IFileSystem
    {
        private readonly IFileSystem first, second;

        public OverlayFileSystem(IFileSystem first, IFileSystem second)
        {
            this.first = first;
            this.second = second;
        }

        public bool Exists(string path) => first.Exists(path) || second.Exists(path);

        public Stream OpenForReading(string path) => first.Exists(path) ? first.OpenForReading(path) : second.OpenForReading(path);

        public Stream OpenForWriting(string path) => first.OpenForWriting(path);

        public Stream OpenForWritingAndReading(string path) => first.OpenForWritingAndReading(path);
    }

    /// <summary>
    /// An <see cref="IFileSystem"/> combinator that provides a flattened, read-only view of specified
    /// subdirectories in an underlying file system.
    /// </summary>
    public sealed class LimitedFileSystem : IFileSystem
    {
        private readonly IFileSystem underlyingFileSystem;
        private readonly string[] searchDirs;

        private readonly Dictionary<string, string?> cachedPaths = new() { [""] = null, ["."] = null, [".."] = null };

        public LimitedFileSystem(string[] searchDirs)
            : this(PhysicalFileSystem.Instance, searchDirs)
        {
        }

        public LimitedFileSystem(IFileSystem underlyingFileSystem, string[] searchDirs)
        {
            this.underlyingFileSystem = underlyingFileSystem;
            this.searchDirs = Array.ConvertAll(searchDirs, Path.GetFullPath);
        }

        private string? GetFullPath(string path)
        {
            if (cachedPaths.TryGetValue(path, out var fullPath))
                return fullPath;

            if (path.Contains(Path.DirectorySeparatorChar) || path.Contains(Path.AltDirectorySeparatorChar))
                return null;

            var filename = Path.GetFileName(path);

            foreach (var d in searchDirs)
            {
                var candidate = Path.Combine(d, filename);

                if (underlyingFileSystem.Exists(candidate))
                {
                    cachedPaths.Add(path, candidate);
                    return candidate;
                }
            }

            cachedPaths.Add(path, null);
            return null;
        }

        public bool Exists(string path)
        {
            var fullPath = GetFullPath(path);
            return fullPath != null && underlyingFileSystem.Exists(fullPath);
        }

        public Stream OpenForReading(string path)
        {
            var fullPath = GetFullPath(path);

            if (fullPath == null)
                throw new FileNotFoundException("Path not allowed for reading");

            return underlyingFileSystem.OpenForReading(fullPath);
        }

        public Stream OpenForWriting(string path) => throw new NotSupportedException($"{nameof(LimitedFileSystem)} is read-only");

        public Stream OpenForWritingAndReading(string path) => throw new NotSupportedException($"{nameof(LimitedFileSystem)} is read-only");
    }
}