OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library front_end.memory_file_system; | 5 library front_end.memory_file_system; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
10 | 10 |
11 import 'file_system.dart'; | 11 import 'file_system.dart'; |
12 | 12 |
13 /// Concrete implementation of [FileSystem] which performs its operations on an | 13 /// Concrete implementation of [FileSystem] which performs its operations on an |
14 /// in-memory virtual file system. | 14 /// in-memory virtual file system. |
15 /// | 15 /// |
16 /// Not intended to be implemented or extended by clients. | 16 /// Not intended to be implemented or extended by clients. |
17 class MemoryFileSystem implements FileSystem { | 17 class MemoryFileSystem implements FileSystem { |
18 final Map<Uri, Uint8List> _files = {}; | 18 final Map<Uri, Uint8List> _files = {}; |
| 19 final Set<Uri> _directories = new Set<Uri>(); |
19 | 20 |
20 /// The "current directory" in the in-memory virtual file system. | 21 /// The "current directory" in the in-memory virtual file system. |
21 /// | 22 /// |
22 /// This is used to convert relative URIs to absolute URIs. | 23 /// This is used to convert relative URIs to absolute URIs. |
23 /// | 24 /// |
24 /// Always ends in a trailing '/'. | 25 /// Always ends in a trailing '/'. |
25 Uri currentDirectory; | 26 Uri currentDirectory; |
26 | 27 |
27 MemoryFileSystem(Uri currentDirectory) | 28 MemoryFileSystem(Uri currentDirectory) |
28 : currentDirectory = _addTrailingSlash(currentDirectory); | 29 : currentDirectory = _addTrailingSlash(currentDirectory); |
(...skipping 24 matching lines...) Expand all Loading... |
53 | 54 |
54 @override | 55 @override |
55 int get hashCode => uri.hashCode; | 56 int get hashCode => uri.hashCode; |
56 | 57 |
57 @override | 58 @override |
58 bool operator ==(Object other) => | 59 bool operator ==(Object other) => |
59 other is MemoryFileSystemEntity && | 60 other is MemoryFileSystemEntity && |
60 other.uri == uri && | 61 other.uri == uri && |
61 identical(other._fileSystem, _fileSystem); | 62 identical(other._fileSystem, _fileSystem); |
62 | 63 |
| 64 /// Create a directory for this file system entry. |
| 65 /// |
| 66 /// If the entry already exists, either as a file, or as a directory, |
| 67 /// this is an error. |
| 68 void createDirectory() { |
| 69 if (_fileSystem._files[uri] != null) { |
| 70 throw new FileSystemException(uri, 'Entry $uri is a file.'); |
| 71 } |
| 72 if (_fileSystem._directories.contains(uri)) { |
| 73 throw new FileSystemException(uri, 'Directory $uri already exists.'); |
| 74 } |
| 75 _fileSystem._directories.add(uri); |
| 76 } |
| 77 |
63 @override | 78 @override |
64 Future<bool> exists() async => _fileSystem._files[uri] != null; | 79 Future<bool> exists() async { |
| 80 return _fileSystem._files[uri] != null || |
| 81 _fileSystem._directories.contains(uri); |
| 82 } |
65 | 83 |
66 @override | 84 @override |
67 Future<List<int>> readAsBytes() async { | 85 Future<List<int>> readAsBytes() async { |
68 List<int> contents = _fileSystem._files[uri]; | 86 List<int> contents = _fileSystem._files[uri]; |
69 if (contents == null) { | 87 if (contents == null) { |
70 throw new FileSystemException(uri, 'File $uri does not exist.'); | 88 throw new FileSystemException(uri, 'File $uri does not exist.'); |
71 } | 89 } |
72 return contents.toList(); | 90 return contents.toList(); |
73 } | 91 } |
74 | 92 |
(...skipping 22 matching lines...) Expand all Loading... |
97 /// If no file exists, one is created. If a file exists already, it is | 115 /// If no file exists, one is created. If a file exists already, it is |
98 /// overwritten. | 116 /// overwritten. |
99 void writeAsStringSync(String s) { | 117 void writeAsStringSync(String s) { |
100 // Note: the return type of UTF8.encode is List<int>, but in practice it | 118 // Note: the return type of UTF8.encode is List<int>, but in practice it |
101 // always returns Uint8List. We rely on that for efficiency, so that we | 119 // always returns Uint8List. We rely on that for efficiency, so that we |
102 // don't have to make an extra copy. | 120 // don't have to make an extra copy. |
103 _update(uri, UTF8.encode(s) as Uint8List); | 121 _update(uri, UTF8.encode(s) as Uint8List); |
104 } | 122 } |
105 | 123 |
106 void _update(Uri uri, Uint8List data) { | 124 void _update(Uri uri, Uint8List data) { |
| 125 if (_fileSystem._directories.contains(uri)) { |
| 126 throw new FileSystemException(uri, 'Entry $uri is a directory.'); |
| 127 } |
107 _fileSystem._files[uri] = data; | 128 _fileSystem._files[uri] = data; |
108 } | 129 } |
109 } | 130 } |
OLD | NEW |